1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
3 \textclass literate-article
15 \use_numerical_citations 0
16 \paperorientation portrait
19 \paragraph_separation indent
21 \quotes_language english
25 \paperpagestyle default
29 LyX and Literate Programming
36 edmar-w-jr@technologist.com
42 Modified by Bernard Michael Hurley bernardh@westherts.ac.uk ---- Don't blame
43 Edmar for any errors that have crept in!
63 \begin_inset LatexCommand \tableofcontents{}
73 After typesetting a document, LyX scans the LaTeX log file looking for errors.
74 For each error found, the line number is obtained and a error box is displayed
75 in the LyX screen at that position.
78 To use this feature to view compilation errors while working with literate
79 documents, we need a program that filters the compilation errors and puts
80 them in a format suitable for LyX reading it.
84 In this document we present a filter that recognizes compilation error messages
85 from noweb, gnu C, and the IBM C compiler (xlc).
88 The filter is required to read from standard input, parse for error messages
89 and copy the error messages to the standard output.
90 During the output process, the filter must present the error messages in
91 a format that LyX can interpret, currently, the LaTeX error message format.
92 Of course, nothing will prevent future LyX releases from being able to
93 read other formats as well (like gcc error messages for example).
94 This mechanism is necessary to fully explore the literate programming tool's
105 main (int argc, char **argv)
111 switch (argv[1][0]) {
115 <<Scan input for noweb error messages>>
121 <<Scan input for xlc error messages>>
127 <<AIX system using both noweb and xlc>>
135 <<Solaris and Linux systems using both noweb and gcc>>
143 <<Scan input for gcc error messages>>
151 <<Scan input for gcc error messages>>
160 <<Function prototypes>>=
162 int main (int argc, char **argv);
170 We resort to some global variables to allow access from several different
172 These are the buffer and related pointers used during the parse of the
176 <<Global variables>>=
178 char buffer[200][200];
192 The output format mimics the TeX error messages format.
193 This function prints a number of lines residing in the global variable
198 , a program name and line number.
199 There is no special requirement on the input strings, they can be anything.
205 This function has been slightly changed from EW's original to make scanning
206 a bit easier with LaTeX::scanLogFile().
207 The test has been added because LyX can crash if empty lines are allowed
208 here --- I can't figure out why! --- BMH
218 output_error (int buf_size, int error_line, char *tool)
226 fprintf(stdout, "! Build Error: ==> %s ==>
230 fprintf(stdout, " ...
240 for (i=0; i<buf_size; i++)
242 if (strlen(buffer[i]) != 0)
244 fprintf(stdout, "%s", buffer[i]);
257 <<Function prototypes>>=
259 void output_error (int buf_size, int error_line, char *tool);
264 Functions Implementation
267 Both noweave and notangle routines, always output one single line for each
268 error found, thus to scan the buffer for noweb error messages is enough
269 to exam one input line at a time.
270 Note that the noweb software does not provide a line error number, so all
271 errors boxes related to noweb messages will be displayed at the beginning
275 <<Scan input for noweb error messages>>=
281 while (fgets(buffer[0], 200, stdin)) {
285 output_error(1, err_line, "noweb");
294 The examination itself is very inefficient.
295 Unfortunately noweb doesn't have any characteristic that would help to
296 identify one of its error messages.
297 The solution is to collect all possible output messages in an array of
298 strings, and turn the examination process into a linear search in this
302 <<Global variables>>=
304 char *noweb_msgs[] = {
306 "couldn't open file",
308 "couldn't open temporary file",
310 "error writing temporary file",
316 "Bad format sequence",
318 "Can't open output file",
320 "Can't open temporary file",
322 "Capacity exceeded:",
324 "Ignoring unknown option -",
326 "This can't happen:",
328 "non-numeric line number in"
334 char *noweb_msgs_mimic_gcc[] = {
336 ": unescaped << in documentation chunk"
343 A noweb error message can be any string that contains a matching pair of
347 > >, or any of the above strings
354 noweb_try (int buf_line)
364 b = buffer[buf_line];
370 for (i=0; i<1; i++) {
372 s = (char *)strstr (b, noweb_msgs_mimic_gcc[i]);
376 t = (char *)strchr(buffer[buf_line], ':');
378 err_line = atoi(t+1);
380 t = buffer[buf_line];
384 while (*(t++) = *(s++));
392 s = (char *)strstr(b, "<<");
396 s = (char *)strstr(s+2, ">>");
406 for (i = 0; i < 12; ++i) {
408 s = (char *)strstr (b, noweb_msgs[i]);
427 <<Function prototypes>>=
429 int noweb_try (int buf_line);
434 The xlc compiler always outputs one single line for each error found, thus
435 to scan the buffer for xlc error messages it is enough to exam one input
439 <<Scan input for xlc error messages>>=
445 while (fgets(buffer[last_buf_line], 200, stdin)) {
449 output_error(1, err_line, "xlc");
458 A xlc error message is easy to identify.
459 Every error message starts with a quoted string with no spaces, a comma,
461 \begin_inset Quotes eld
465 \begin_inset Quotes erd
468 , a space, and some variable text.
469 The following routine tests if a given buffer line matches this criteria:
476 xlc_try (int buf_line)
484 t = buffer[buf_line];
488 while (*s != '"' && *s != ' ' && *s != '
494 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
509 <<Function prototypes>>=
511 int xlc_try (int buf_line);
516 The gcc compiler error messages are more complicated to scan.
517 Each error can span more than one line in the buffer.
518 The good news is that every buffer line on each error has the same pattern,
519 and share the same line number.
520 Thus the strategy will be to accumulate lines in the buffer while the reported
521 line number is still the same.
522 At the time they differ, all the accumulated lines, except the last one,
523 will belong to one single error message, which now can be output-ed to
527 Every gcc error message contains a string with no space followed by a
528 \begin_inset Quotes eld
532 \begin_inset Quotes eld
536 If the next character is a space, then this line is a header of a error
537 message and the next line will detail the line number of the source code
538 where the error was found.
539 Otherwise, the next thing is a integer number followed by another
540 \begin_inset Quotes eld
544 \begin_inset Quotes eld
550 <<Scan input for gcc error messages>>=
560 while (fgets(buffer[last_buf_line], 200, stdin)) {
562 /****** Skip lines until I find an error */
564 s = (char *)strpbrk(buffer[last_buf_line], " :");
566 if (s == NULL || *s == ' ')
568 continue; /* No gcc error found here */
572 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
574 /****** OK It is an error message, get line number */
576 err_line = atoi(s+1);
578 if (last_err_line == 0 || last_err_line == err_line) {
580 last_err_line = err_line;
582 continue; /* It's either a header or a continuation, don't output
587 /****** Completed the scan of one error message, output it to LyX
594 } while (fgets(buffer[last_buf_line], 200, stdin));
598 /****** EOF completes the scan of whatever was being scanned */
607 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
609 /****** Search first ":" in the error number */
611 s = (char *)strpbrk(buffer[last_buf_line], " :");
615 if (s == NULL || *s == ' ')
617 <<No gcc error found here, but it might terminate the scanning of a previous
620 /****** Search second ":" in the error number */
622 t = (char *)strpbrk(s+1, " :");
624 if (t == NULL || *t == ' ')
626 <<No gcc error found here, but it might terminate the scanning of a previous
629 /****** Verify if is all digits between ":" */
631 if (t != s+1+strspn(s+1, "0123456789"))
633 <<No gcc error found here, but it might terminate the scanning of a previous
639 <<No gcc error found here, but it might terminate the scanning of a previous
655 As we mentioned, when the scan of one gcc error message is completed everything
656 in the buffer except the last line is one single error message.
657 But if the scan terminates with a EOF or through finding one line that
658 does not match the gcc error message criteria, then there is no
659 \begin_inset Quotes eld
663 \begin_inset Quotes erd
666 in the buffer to be concerned with.
667 In those cases we empty the buffer completely.
674 discharge_buffer (int save_last)
678 if (last_err_line != 0) {
680 clean_gcc_messages();
682 if (save_last != 0) {
684 output_error(last_buf_line-1, last_err_line, "gcc");
686 strcpy (buffer[0], buffer[last_buf_line-1]);
688 last_err_line = err_line;
696 clean_gcc_messages();
698 output_error(last_buf_line-1, last_err_line, "gcc");
713 <<Function prototypes>>=
715 void discharge_buffer (int save_last);
721 \begin_inset Quotes eld
725 \begin_inset Quotes erd
728 superfluous information from gcc messages, namely the name of the noweb
729 file and the line number of the Error.
736 For instance, some way of distinguishing between gcc Errors and Warnings
747 clean_gcc_messages ()
757 int search_len = sprintf(search, ".nw:%d:", last_err_line);
761 for (index = 0; index < last_buf_line-1; index++) {
763 tail = (char *)strstr (buffer[index], search);
767 tail = (char *) strstr (buffer[index], ".nw:");
783 head = buffer[index];
785 while (*(head++) = *(tail++));
796 <<Function prototypes>>=
798 void clean_gcc_messages ();
803 To combine the scan of noweb error messages and xlc error messages is very
805 We just try each one for every input line:
808 <<AIX system using both noweb and xlc>>=
814 while (fgets(buffer[0], 200, stdin)) {
818 output_error(1, err_line, "noweb");
822 output_error(1, err_line, "xlc");
831 To combine the scan of noweb error messages and gcc error messages is simple
832 if we realize that it is not possible to find a noweb error message in
833 the middle of a gcc error message.
834 So we just repeat the gcc procedure and test for noweb error messages in
835 the beginning of the scan:
838 <<Solaris and Linux systems using both noweb and gcc>>=
848 while (fgets(buffer[last_buf_line], 200, stdin)) {
850 /****** Skip lines until I find an error */
852 if (last_buf_line == 0 && noweb_try(0)) {
854 output_error(1, err_line, "noweb");
860 s = (char *)strpbrk(buffer[last_buf_line], " :");
862 if (s == NULL || *s == ' ')
864 continue; /* No gcc error found here */
868 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
870 /****** OK It is an error, get line number */
872 err_line = atoi(s+1);
874 if (last_err_line == 0 || last_err_line == err_line) {
876 last_err_line = err_line;
878 continue; /* It's either a header or a continuation, don't output
883 /****** Completed the scan of one error message, output it to LyX
890 } while (fgets(buffer[last_buf_line], 200, stdin));
894 /****** EOF completes the scan of whatever was being scanned */
903 Wrapping the code into a file
916 <<Function prototypes>>
923 To build this program, we want to add the
924 \begin_inset Quotes eld
928 \begin_inset Quotes erd
931 option in the tangle command to force gdb to load the file
940 In accordance with this, we pass the
941 \begin_inset Quotes eld
945 \begin_inset Quotes erd
955 notangle -L -Rlisterrors.c ${NOWEB_SOURCE} > listerrors.c
957 gcc -g -o listerrors listerrors.c
962 This project can be tangled and compiled from LyX if you set
968 to call a generic script that always extracts a scrap named
973 Here is a example of such generic script:
978 notangle -Rbuild-script $1 | env NOWEB_SOURCE=$1 sh