1 #LyX 1.1 created this file. For more info see http://www.lyx.org/
3 \textclass literate-article
14 \paperorientation portrait
17 \paragraph_separation indent
19 \quotes_language english
23 \paperpagestyle default
27 LyX and Literate Programming
34 edmar-w-jr@technologist.com
38 Modified by Bernard Michael Hurley bernardh@westherts.ac.uk ---- Don't blame
39 Edmar for any errors that have crept in!
51 \begin_inset LatexCommand \tableofcontents{}
61 After typesetting a document, LyX scans the LaTeX log file looking for errors.
62 For each error found, the line number is obtained and a error box is displayed
63 in the LyX screen at that position.
66 To use this feature to view compilation errors while working with literate
67 documents, we need a program that filters the compilation errors and puts
68 them in a format suitable for LyX reading it.
72 In this document we present a filter that recognizes compilation error messages
73 from noweb, gnu C, and the IBM C compiler (xlc).
76 The filter is required to read from standard input, parse for error messages
77 and copy the error messages to the standard output.
78 During the output process, the filter must present the error messages in
79 a format that LyX can interpret, currently, the LaTeX error message format.
80 Of course, nothing will prevent future LyX releases from being able to
81 read other formats as well (like gcc error messages for example).
82 This mechanism is necessary to fully explore the literate programming tool's
93 main (int argc, char **argv)
103 <<Scan input for noweb error messages>>
109 <<Scan input for xlc error messages>>
115 <<AIX system using both noweb and xlc>>
123 <<Solaris and Linux systems using both noweb and gcc>>
131 <<Scan input for gcc error messages>>
139 <<Scan input for gcc error messages>>
148 <<Function prototypes>>=
150 int main (int argc, char **argv);
158 We resort to some global variables to allow access from several different
160 These are the buffer and related pointers used during the parse of the
164 <<Global variables>>=
166 char buffer[200][200];
180 The output format mimics the TeX error messages format.
181 This function prints a number of lines residing in the global variable
186 , a program name and line number.
187 There is no special requirement on the input strings, they can be anything.
188 \begin_float footnote
191 This function has been slightly changed from EW's original to make scanning
192 a bit easier with LaTeX::scanLogFile().
193 The test has been added because LyX can crash if empty lines are allowed
194 here --- I can't figure out why! --- BMH
202 output_error (int buf_size, int error_line, char *tool)
210 fprintf(stdout, "! Build Error: ==> %s ==>
214 fprintf(stdout, " ...
224 for (i=0; i<buf_size; i++)
226 if (strlen(buffer[i]) != 0)
228 fprintf(stdout, "%s", buffer[i]);
241 <<Function prototypes>>=
243 void output_error (int buf_size, int error_line, char *tool);
248 Functions Implementation
251 Both noweave and notangle routines, always output one single line for each
252 error found, thus to scan the buffer for noweb error messages is enough
253 to exam one input line at a time.
254 Note that the noweb software does not provide a line error number, so all
255 errors boxes related to noweb messages will be displayed at the beginning
259 <<Scan input for noweb error messages>>=
265 while (fgets(buffer[0], 200, stdin)) {
269 output_error(1, err_line, "noweb");
278 The examination itself is very inefficient.
279 Unfortunately noweb doesn't have any characteristic that would help to
280 identify one of its error messages.
281 The solution is to collect all possible output messages in an array of
282 strings, and turn the examination process into a linear search in this
286 <<Global variables>>=
288 char *noweb_msgs[] = {
290 "couldn't open file",
292 "couldn't open temporary file",
294 "error writing temporary file",
300 "Bad format sequence",
302 "Can't open output file",
304 "Can't open temporary file",
306 "Capacity exceeded:",
308 "Ignoring unknown option -",
310 "This can't happen:",
312 "non-numeric line number in"
318 char *noweb_msgs_mimic_gcc[] = {
320 ": unescaped << in documentation chunk"
327 A noweb error message can be any string that contains a matching pair of
331 > >, or any of the above strings
338 noweb_try (int buf_line)
348 b = buffer[buf_line];
354 for (i=0; i<1; i++) {
356 s = (char *)strstr (b, noweb_msgs_mimic_gcc[i]);
360 t = (char *)strchr(buffer[buf_line], ':');
362 err_line = atoi(t+1);
364 t = buffer[buf_line];
368 while (*(t++) = *(s++));
376 s = (char *)strstr(b, "<<");
380 s = (char *)strstr(s+2, ">>");
390 for (i = 0; i < 12; ++i) {
392 s = (char *)strstr (b, noweb_msgs[i]);
411 <<Function prototypes>>=
413 int noweb_try (int buf_line);
418 The xlc compiler always outputs one single line for each error found, thus
419 to scan the buffer for xlc error messages it is enough to exam one input
423 <<Scan input for xlc error messages>>=
429 while (fgets(buffer[last_buf_line], 200, stdin)) {
433 output_error(1, err_line, "xlc");
442 A xlc error message is easy to identify.
443 Every error message starts with a quoted string with no spaces, a comma,
445 \begin_inset Quotes eld
449 \begin_inset Quotes erd
452 , a space, and some variable text.
453 The following routine tests if a given buffer line matches this criteria:
460 xlc_try (int buf_line)
468 t = buffer[buf_line];
472 while (*s != '"' && *s != ' ' && *s != '
478 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
493 <<Function prototypes>>=
495 int xlc_try (int buf_line);
500 The gcc compiler error messages are more complicated to scan.
501 Each error can span more than one line in the buffer.
502 The good news is that every buffer line on each error has the same pattern,
503 and share the same line number.
504 Thus the strategy will be to accumulate lines in the buffer while the reported
505 line number is still the same.
506 At the time they differ, all the accumulated lines, except the last one,
507 will belong to one single error message, which now can be output-ed to
511 Every gcc error message contains a string with no space followed by a
512 \begin_inset Quotes eld
516 \begin_inset Quotes eld
520 If the next character is a space, then this line is a header of a error
521 message and the next line will detail the line number of the source code
522 where the error was found.
523 Otherwise, the next thing is a integer number followed by another
524 \begin_inset Quotes eld
528 \begin_inset Quotes eld
534 <<Scan input for gcc error messages>>=
544 while (fgets(buffer[last_buf_line], 200, stdin)) {
546 /****** Skip lines until I find an error */
548 s = (char *)strpbrk(buffer[last_buf_line], " :");
550 if (s == NULL || *s == ' ')
552 continue; /* No gcc error found here */
556 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
558 /****** OK It is an error message, get line number */
560 err_line = atoi(s+1);
562 if (last_err_line == 0 || last_err_line == err_line) {
564 last_err_line = err_line;
566 continue; /* It's either a header or a continuation, don't output
571 /****** Completed the scan of one error message, output it to LyX
578 } while (fgets(buffer[last_buf_line], 200, stdin));
582 /****** EOF completes the scan of whatever was being scanned */
591 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
593 /****** Search first ":" in the error number */
595 s = (char *)strpbrk(buffer[last_buf_line], " :");
599 if (s == NULL || *s == ' ')
601 <<No gcc error found here, but it might terminate the scanning of a previous
604 /****** Search second ":" in the error number */
606 t = (char *)strpbrk(s+1, " :");
608 if (t == NULL || *t == ' ')
610 <<No gcc error found here, but it might terminate the scanning of a previous
613 /****** Verify if is all digits between ":" */
615 if (t != s+1+strspn(s+1, "0123456789"))
617 <<No gcc error found here, but it might terminate the scanning of a previous
623 <<No gcc error found here, but it might terminate the scanning of a previous
639 As we mentioned, when the scan of one gcc error message is completed everything
640 in the buffer except the last line is one single error message.
641 But if the scan terminates with a EOF or through finding one line that
642 does not match the gcc error message criteria, then there is no
643 \begin_inset Quotes eld
647 \begin_inset Quotes erd
650 in the buffer to be concerned with.
651 In those cases we empty the buffer completely.
658 discharge_buffer (int save_last)
662 if (last_err_line != 0) {
664 clean_gcc_messages();
666 if (save_last != 0) {
668 output_error(last_buf_line-1, last_err_line, "gcc");
670 strcpy (buffer[0], buffer[last_buf_line-1]);
672 last_err_line = err_line;
680 clean_gcc_messages();
682 output_error(last_buf_line-1, last_err_line, "gcc");
697 <<Function prototypes>>=
699 void discharge_buffer (int save_last);
705 \begin_inset Quotes eld
709 \begin_inset Quotes erd
712 superfluous information from gcc messages, namely the name of the noweb
713 file and the line number of the Error.
714 \begin_float footnote
718 For instance, some way of distinguishing between gcc Errors and Warnings
727 clean_gcc_messages ()
737 int search_len = sprintf(search, ".nw:%d:", last_err_line);
741 for (index = 0; index < last_buf_line-1; index++) {
743 tail = (char *)strstr (buffer[index], search);
747 tail = (char *) strstr (buffer[index], ".nw:");
763 head = buffer[index];
765 while (*(head++) = *(tail++));
776 <<Function prototypes>>=
778 void clean_gcc_messages ();
783 To combine the scan of noweb error messages and xlc error messages is very
785 We just try each one for every input line:
788 <<AIX system using both noweb and xlc>>=
794 while (fgets(buffer[0], 200, stdin)) {
798 output_error(1, err_line, "noweb");
802 output_error(1, err_line, "xlc");
811 To combine the scan of noweb error messages and gcc error messages is simple
812 if we realize that it is not possible to find a noweb error message in
813 the middle of a gcc error message.
814 So we just repeat the gcc procedure and test for noweb error messages in
815 the beginning of the scan:
818 <<Solaris and Linux systems using both noweb and gcc>>=
828 while (fgets(buffer[last_buf_line], 200, stdin)) {
830 /****** Skip lines until I find an error */
832 if (last_buf_line == 0 && noweb_try(0)) {
834 output_error(1, err_line, "noweb");
840 s = (char *)strpbrk(buffer[last_buf_line], " :");
842 if (s == NULL || *s == ' ')
844 continue; /* No gcc error found here */
848 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
850 /****** OK It is an error, get line number */
852 err_line = atoi(s+1);
854 if (last_err_line == 0 || last_err_line == err_line) {
856 last_err_line = err_line;
858 continue; /* It's either a header or a continuation, don't output
863 /****** Completed the scan of one error message, output it to LyX
870 } while (fgets(buffer[last_buf_line], 200, stdin));
874 /****** EOF completes the scan of whatever was being scanned */
883 Wrapping the code into a file
896 <<Function prototypes>>
903 To build this program, we want to add the
904 \begin_inset Quotes eld
908 \begin_inset Quotes erd
911 option in the tangle command to force gdb to load the file
920 In accordance with this, we pass the
921 \begin_inset Quotes eld
925 \begin_inset Quotes erd
933 #!/usr/local/bin/bash
935 notangle -L -Rlisterrors.c Literate.nw > listerrors.c
937 gcc -g -o listerrors listerrors.c
942 This project can be tangled and compiled from LyX if you set
948 to call a generic script that always extracts a scrap named
953 Here is a example of such generic script:
958 notangle -Rbuild-script $1 | sh