]> git.lyx.org Git - lyx.git/blob - lib/scripts/listerrors
5142f6dff0420500321d0873e00a03d61ee855b4
[lyx.git] / lib / scripts / listerrors
1 #!/usr/bin/python
2 """reformat noweb and compiler errors for LyX.
3
4 Expects to read from stdin and output to stdout.
5 """
6
7 __author__ = "Kayvan A. Sylvan <kayvan@sylvan.com>"
8 __date__ = "$Date: 2002/03/19 21:42:48 $"
9 __version__ = "$Revision: 1.1 $"
10 __credits__ = """Edmar Wienskoski Jr. <edmar-w-jr@technologist.com>
11     original Literate support for LyX.
12 Bernard Michael Hurley <berhardh@westherts.ac.uk>
13     modifications to original listerrors."""
14 __copyright__ = "Copyright 2002 - The LyX team."
15
16 import sys
17
18 def write_error(msg, tool = "noweb", line_number = 1):
19   """Write out the given message in TeX error style.
20
21   called like: write_error(msg, tool, line_number)."""
22   print "! Build Error: ==> %s ==>\n" % (tool),
23   print " ...\n\nl.%d ...\n" % (line_number),
24   if type(msg) == type("str"): # simple string
25     print msg
26   else: # some kind of list (sequence or tuple)
27     for m in msg:
28         if m != "": print m,
29     print
30
31 __lines = [] # lines pushed back
32
33 def getline(file = sys.stdin):
34   """read a line from internal stack or from file.
35
36   optional file argument defaults to sys.stdin."""
37   global __lines
38   lines = __lines
39   if lines:
40     line = lines[-1]
41     lines = lines[:-1]
42   else:
43     line = file.readline()
44   return line
45
46 def pushline(line):
47   "push a line onto the pushback stack."
48   global __lines
49   lines = __lines
50   lines += (line,) # push a list onto the stack, not individual letters
51
52 def main():
53   """Entry point for listerrors. Takes no options.
54
55   Reads stdin and writes to stdout. Filter errors"""
56
57   while 1:
58     line = getline()
59     if line == "": break
60     try_patterns_dispatch = [ noweb_try, gcc_try, xlc_try ]
61     for predicate in try_patterns_dispatch:
62       if predicate(line): break
63 def noweb_try(line):
64   """see if line is a noweb error.
65
66   Returns 1 on success, 0 otherwise. Outputs on stdout."""
67   retval = 0
68   if line.find(": unescaped << in documentation chunk") != -1:
69     line_parts = line.split(':')
70     num_str = line_parts[1]
71     num_len = len(num_str)
72     i = 0
73     while i < num_len and num_str[i].isdigit(): i += 1
74     if i == num_len:
75       write_error(":" + line_parts[2], "noweb", int(num_str))
76       retval = 1
77   if (not retval):
78     left = line.find("<<")
79     if (left != -1) and ((left + 2) < len(line)) and \
80        (line[left+2:].find(">>") != -1):
81       write_error(line, "noweb");
82       retval = 1;
83   if (not retval):
84     msgs_to_try = ("couldn't open file",
85       "couldn't open temporary file",
86       "error writing temporary file",
87       "ill-formed option",
88       "unknown option",
89       "Bad format sequence",
90       "Can't open output file",
91       "Can't open temporary file",
92       "Capacity exceeded:",
93       "Ignoring unknown option -",
94       "This can't happen:",
95       "non-numeric line number in")
96     for msg in msgs_to_try:
97       if line.find(msg) != -1:
98         write_error(line, "noweb")
99         retval = 1
100         break
101   return retval
102
103 def gcc_try(line):
104   """See if line is a gcc error. Read ahead to handle all the lines.
105
106   Returns 1 on success, 0 otherwise. Outputs on stdout."""
107   retval = 0
108   first_space = line.find(' ')
109   if first_space > 1: # The smallest would be "X: "
110     if line[first_space - 1] == ':':
111       header_to_see = line[:first_space - 1]
112       next_line = getline()
113       if next_line and next_line[:first_space - 1] == header_to_see:
114         num_end = first_space
115         while next_line[num_end].isdigit(): num_end += 1
116         if num_end > first_space: # good!
117           num_str = next_line[first_space:num_end]
118           msgs = []
119           msgs += (line[first_space:],)
120           msgs += (next_line[num_end + 1:],)
121           header_to_see = next_line[:num_end]
122           next_line = getline()
123           while next_line and next_line[:num_end] == header_to_see:
124             msgs += (next_line[num_end + 1:],)
125             next_line = getline()
126           if next_line: pushline(next_line)
127           write_error(msgs, "gcc", int(num_str))
128           retval = 1
129         else: # oops! Not a gcc error.
130           pushline(next_line)
131       elif next_line:
132         pushline(next_line) # return this line to input stream
133   return retval
134
135 def xlc_try(line):
136   """see if line is an xlc error.
137
138   Returns 1 on success, 0 otherwise. Outputs on stdout."""
139   retval = 0
140   if line[0] == '"': # This is the first character of all xlc errors
141     next_quote = line.find('"', 1)
142     first_space = line.find(' ')
143     if (next_quote != -1) and (first_space > next_quote): # no space inisde quotes
144       if line[first_space - 1:first_space + 6] == ", line ":
145         num_start = num_end = first_space + 6
146         while line[num_end].isdigit(): num_end += 1
147         if num_end > num_start:
148           write_error(line, "xlc", int(line[num_start : num_end]))
149           retval = 1
150   return retval
151   
152
153 if __name__ == "__main__":
154   main()