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, 0, "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"
319 A noweb error message can be any string that contains a matching pair of
323 > >, or any of the above strings.
330 noweb_try (int buf_line)
340 b = buffer[buf_line];
342 s = (char *)strstr(b, "<<");
346 s = (char *)strstr(s+2, ">>");
354 for (i=0; i<12; i++) {
356 s = (char *)strstr (b, noweb_msgs[i]);
377 <<Function prototypes>>=
379 int noweb_try (int buf_line);
384 The xlc compiler always outputs one single line for each error found, thus
385 to scan the buffer for xlc error messages it is enough to exam one input
389 <<Scan input for xlc error messages>>=
395 while (fgets(buffer[last_buf_line], 200, stdin)) {
399 output_error(1, err_line, "xlc");
408 A xlc error message is easy to identify.
409 Every error message starts with a quoted string with no spaces, a comma,
411 \begin_inset Quotes eld
415 \begin_inset Quotes erd
418 , a space, and some variable text.
419 The following routine tests if a given buffer line matches this criteria:
426 xlc_try (int buf_line)
434 t = buffer[buf_line];
438 while (*s != '"' && *s != ' ' && *s != '
444 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
459 <<Function prototypes>>=
461 int xlc_try (int buf_line);
466 The gcc compiler error messages are more complicated to scan.
467 Each error can span more than one line in the buffer.
468 The good news is that every buffer line on each error has the same pattern,
469 and share the same line number.
470 Thus the strategy will be to accumulate lines in the buffer while the reported
471 line number is still the same.
472 At the time they differ, all the accumulated lines, except the last one,
473 will belong to one single error message, which now can be output-ed to
477 Every gcc error message contains a string with no space followed by a
478 \begin_inset Quotes eld
482 \begin_inset Quotes eld
486 If the next character is a space, then this line is a header of a error
487 message and the next line will detail the line number of the source code
488 where the error was found.
489 Otherwise, the next thing is a integer number followed by another
490 \begin_inset Quotes eld
494 \begin_inset Quotes eld
500 <<Scan input for gcc error messages>>=
510 while (fgets(buffer[last_buf_line], 200, stdin)) {
512 /****** Skip lines until I find an error */
514 s = (char *)strpbrk(buffer[last_buf_line], " :");
516 if (s == NULL || *s == ' ')
518 continue; /* No gcc error found here */
522 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
524 /****** OK It is an error message, get line number */
526 err_line = atoi(s+1);
528 if (last_err_line == 0 || last_err_line == err_line) {
530 last_err_line = err_line;
532 continue; /* It's either a header or a continuation, don't output
537 /****** Completed the scan of one error message, output it to LyX
544 } while (fgets(buffer[last_buf_line], 200, stdin));
548 /****** EOF completes the scan of whatever was being scanned */
557 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
559 /****** Search first ":" in the error number */
561 s = (char *)strpbrk(buffer[last_buf_line], " :");
565 if (s == NULL || *s == ' ')
567 <<No gcc error found here, but it might terminate the scanning of a previous
570 /****** Search second ":" in the error number */
572 t = (char *)strpbrk(s+1, " :");
574 if (t == NULL || *t == ' ')
576 <<No gcc error found here, but it might terminate the scanning of a previous
579 /****** Verify if is all digits between ":" */
581 if (t != s+1+strspn(s+1, "0123456789"))
583 <<No gcc error found here, but it might terminate the scanning of a previous
589 <<No gcc error found here, but it might terminate the scanning of a previous
605 As we mentioned, when the scan of one gcc error message is completed everything
606 in the buffer except the last line is one single error message.
607 But if the scan terminates with a EOF or through finding one line that
608 does not match the gcc error message criteria, then there is no
609 \begin_inset Quotes eld
613 \begin_inset Quotes erd
616 in the buffer to be concerned with.
617 In those cases we empty the buffer completely.
624 discharge_buffer (int save_last)
628 if (last_err_line != 0) {
630 clean_gcc_messages();
632 if (save_last != 0) {
634 output_error(last_buf_line-1, last_err_line, "gcc");
636 strcpy (buffer[0], buffer[last_buf_line-1]);
638 last_err_line = err_line;
646 clean_gcc_messages();
648 output_error(last_buf_line-1, last_err_line, "gcc");
663 <<Function prototypes>>=
665 void discharge_buffer (int save_last);
671 \begin_inset Quotes eld
675 \begin_inset Quotes erd
678 superfluous information from gcc messages, namely the name of the noweb
679 file and the line number of the Error.
680 \begin_float footnote
684 For instance, some way of distinguishing between gcc Errors and Warnings
693 clean_gcc_messages ()
703 int search_len = sprintf(search, ".nw:%d:", last_err_line);
707 for (index = 0; index < last_buf_line-1; index++) {
709 tail = (char *)strstr (buffer[index], search);
713 tail = (char *) strstr (buffer[index], ".nw:");
729 head = buffer[index];
731 while (*(head++) = *(tail++));
742 <<Function prototypes>>=
744 void clean_gcc_messages ();
749 To combine the scan of noweb error messages and xlc error messages is very
751 We just try each one for every input line:
754 <<AIX system using both noweb and xlc>>=
760 while (fgets(buffer[0], 200, stdin)) {
764 output_error(1, 0, "noweb");
768 output_error(1, err_line, "xlc");
777 To combine the scan of noweb error messages and gcc error messages is simple
778 if we realize that it is not possible to find a noweb error message in
779 the middle of a gcc error message.
780 So we just repeat the gcc procedure and test for noweb error messages in
781 the beginning of the scan:
784 <<Solaris and Linux systems using both noweb and gcc>>=
794 while (fgets(buffer[last_buf_line], 200, stdin)) {
796 /****** Skip lines until I find an error */
798 if (last_buf_line == 0 && noweb_try(0)) {
800 output_error(1, 0, "noweb");
806 s = (char *)strpbrk(buffer[last_buf_line], " :");
808 if (s == NULL || *s == ' ')
810 continue; /* No gcc error found here */
814 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
816 /****** OK It is an error, get line number */
818 err_line = atoi(s+1);
820 if (last_err_line == 0 || last_err_line == err_line) {
822 last_err_line = err_line;
824 continue; /* It's either a header or a continuation, don't output
829 /****** Completed the scan of one error message, output it to LyX
836 } while (fgets(buffer[last_buf_line], 200, stdin));
840 /****** EOF completes the scan of whatever was being scanned */
849 Wrapping the code into a file
862 <<Function prototypes>>
869 To build this program, we want to add the
870 \begin_inset Quotes eld
874 \begin_inset Quotes erd
877 option in the tangle command to force gdb to load the file
886 In accordance with this, we pass the
887 \begin_inset Quotes eld
891 \begin_inset Quotes erd
899 #!/usr/local/bin/bash
901 notangle -L -Rlisterrors.c Literate.nw > listerrors.c
903 gcc -g -o listerrors listerrors.c
908 This project can be tangled and compiled from LyX if you set
914 to call a generic script that always extracts a scrap named
919 Here is a example of such generic script:
924 notangle -Rbuild-script $1 | sh