1 #LyX 1.5.0svn created this file. For more info see http://www.lyx.org/
5 \textclass literate-article
10 \font_typewriter default
11 \font_default_family default
17 \paperfontsize default
25 \paperorientation portrait
28 \paragraph_separation indent
30 \quotes_language english
33 \paperpagestyle default
34 \tracking_changes false
42 LyX and Literate Programming
51 edmar-w-jr@technologist.com
55 \begin_layout Standard
57 Modified by Bernard Michael Hurley bernardh@westherts.ac.uk ---- Don't blame
58 Edmar for any errors that have crept in!
66 \begin_layout Abstract
72 This example program is provided for educational use only.
73 The functionality in this C program has been superceded by the equivalent
78 examples/listerrors.lyx
80 which should be installed in the LyX scripts directory.
89 \begin_layout Standard
100 \begin_layout Standard
103 \begin_inset LatexCommand tableofcontents
109 \begin_layout Section
114 \begin_layout Standard
116 After typesetting a document, LyX scans the LaTeX log file looking for errors.
117 For each error found, the line number is obtained and a error box is displayed
118 in the LyX screen at that position.
121 \begin_layout Standard
123 To use this feature to view compilation errors while working with literate
124 documents, we need a program that filters the compilation errors and puts
125 them in a format suitable for LyX reading it.
129 \begin_layout Standard
131 In this document we present a filter that recognizes compilation error messages
132 from noweb, gnu C, and the IBM C compiler (xlc).
135 \begin_layout Standard
137 The filter is required to read from standard input, parse for error messages
138 and copy the error messages to the standard output.
139 During the output process, the filter must present the error messages in
140 a format that LyX can interpret, currently, the LaTeX error message format.
141 Of course, nothing will prevent future LyX releases from being able to
142 read other formats as well (like gcc error messages for example).
143 This mechanism is necessary to fully explore the literate programming tool's
147 \begin_layout Section
158 main (int argc, char **argv)
164 switch (argv[1][0]) {
168 <<Scan input for noweb error messages>>
174 <<Scan input for xlc error messages>>
180 <<AIX system using both noweb and xlc>>
188 <<Solaris and Linux systems using both noweb and gcc>>
196 <<Scan input for gcc error messages>>
204 <<Scan input for gcc error messages>>
215 <<Function prototypes>>=
217 int main (int argc, char **argv);
222 \begin_layout Section
227 \begin_layout Standard
229 We resort to some global variables to allow access from several different
231 These are the buffer and related pointers used during the parse of the
237 <<Global variables>>=
239 char buffer[200][200];
250 \begin_layout Section
255 \begin_layout Standard
257 The output format mimics the TeX error messages format.
258 This function prints a number of lines residing in the global variable
265 , a program name and line number.
266 There is no special requirement on the input strings, they can be anything.
270 \begin_layout Standard
272 This function has been slightly changed from EW's original to make scanning
273 a bit easier with LaTeX::scanLogFile().
274 The test has been added because LyX can crash if empty lines are allowed
275 here --- I can't figure out why! --- BMH
289 output_error (int buf_size, int error_line, char *tool)
297 fprintf(stdout, "! Build Error: ==> %s ==>
301 fprintf(stdout, " ...
311 for (i=0; i<buf_size; i++)
313 if (strlen(buffer[i]) != 0)
315 fprintf(stdout, "%s", buffer[i]);
330 <<Function prototypes>>=
332 void output_error (int buf_size, int error_line, char *tool);
337 \begin_layout Section
339 Functions Implementation
342 \begin_layout Standard
344 Both noweave and notangle routines, always output one single line for each
345 error found, thus to scan the buffer for noweb error messages is enough
346 to exam one input line at a time.
347 Note that the noweb software does not provide a line error number, so all
348 errors boxes related to noweb messages will be displayed at the beginning
354 <<Scan input for noweb error messages>>=
360 while (fgets(buffer[0], 200, stdin)) {
364 output_error(1, err_line, "noweb");
373 \begin_layout Standard
375 The examination itself is very inefficient.
376 Unfortunately noweb doesn't have any characteristic that would help to
377 identify one of its error messages.
378 The solution is to collect all possible output messages in an array of
379 strings, and turn the examination process into a linear search in this
385 <<Global variables>>=
387 char *noweb_msgs[] = {
389 "couldn't open file",
391 "couldn't open temporary file",
393 "error writing temporary file",
399 "Bad format sequence",
401 "Can't open output file",
403 "Can't open temporary file",
405 "Capacity exceeded:",
407 "Ignoring unknown option -",
409 "This can't happen:",
411 "non-numeric line number in"
417 char *noweb_msgs_mimic_gcc[] = {
419 ": unescaped << in documentation chunk"
426 \begin_layout Standard
428 A noweb error message can be any string that contains a matching pair of
432 > >, or any of the above strings
441 noweb_try (int buf_line)
451 b = buffer[buf_line];
457 for (i=0; i<1; i++) {
459 s = (char *)strstr (b, noweb_msgs_mimic_gcc[i]);
463 t = (char *)strchr(buffer[buf_line], ':');
465 err_line = atoi(t+1);
467 t = buffer[buf_line];
471 while (*(t++) = *(s++));
479 s = (char *)strstr(b, "<<");
483 s = (char *)strstr(s+2, ">>");
493 for (i = 0; i < 12; ++i) {
495 s = (char *)strstr (b, noweb_msgs[i]);
516 <<Function prototypes>>=
518 int noweb_try (int buf_line);
523 \begin_layout Standard
525 The xlc compiler always outputs one single line for each error found, thus
526 to scan the buffer for xlc error messages it is enough to exam one input
532 <<Scan input for xlc error messages>>=
538 while (fgets(buffer[last_buf_line], 200, stdin)) {
542 output_error(1, err_line, "xlc");
551 \begin_layout Standard
553 A xlc error message is easy to identify.
554 Every error message starts with a quoted string with no spaces, a comma,
556 \begin_inset Quotes eld
560 \begin_inset Quotes erd
563 , a space, and some variable text.
564 The following routine tests if a given buffer line matches this criteria:
573 xlc_try (int buf_line)
581 t = buffer[buf_line];
585 while (*s != '"' && *s != ' ' && *s != '
591 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
608 <<Function prototypes>>=
610 int xlc_try (int buf_line);
615 \begin_layout Standard
617 The gcc compiler error messages are more complicated to scan.
618 Each error can span more than one line in the buffer.
619 The good news is that every buffer line on each error has the same pattern,
620 and share the same line number.
621 Thus the strategy will be to accumulate lines in the buffer while the reported
622 line number is still the same.
623 At the time they differ, all the accumulated lines, except the last one,
624 will belong to one single error message, which now can be output-ed to
628 \begin_layout Standard
630 Every gcc error message contains a string with no space followed by a
631 \begin_inset Quotes eld
635 \begin_inset Quotes eld
639 If the next character is a space, then this line is a header of a error
640 message and the next line will detail the line number of the source code
641 where the error was found.
642 Otherwise, the next thing is a integer number followed by another
643 \begin_inset Quotes eld
647 \begin_inset Quotes eld
655 <<Scan input for gcc error messages>>=
665 while (fgets(buffer[last_buf_line], 200, stdin)) {
667 /****** Skip lines until I find an error */
669 s = (char *)strpbrk(buffer[last_buf_line], " :");
671 if (s == NULL || *s == ' ')
673 continue; /* No gcc error found here */
677 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
679 /****** OK It is an error message, get line number */
681 err_line = atoi(s+1);
683 if (last_err_line == 0 || last_err_line == err_line) {
685 last_err_line = err_line;
687 continue; /* It's either a header or a continuation, don't output
692 /****** Completed the scan of one error message, output it to LyX
699 } while (fgets(buffer[last_buf_line], 200, stdin));
703 /****** EOF completes the scan of whatever was being scanned */
714 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
716 /****** Search first ":" in the error number */
718 s = (char *)strpbrk(buffer[last_buf_line], " :");
722 if (s == NULL || *s == ' ')
724 <<No gcc error found here, but it might terminate the scanning of a previous
727 /****** Search second ":" in the error number */
729 t = (char *)strpbrk(s+1, " :");
731 if (t == NULL || *t == ' ')
733 <<No gcc error found here, but it might terminate the scanning of a previous
736 /****** Verify if is all digits between ":" */
738 if (t != s+1+strspn(s+1, "0123456789"))
740 <<No gcc error found here, but it might terminate the scanning of a previous
748 <<No gcc error found here, but it might terminate the scanning of a previous
764 \begin_layout Standard
766 As we mentioned, when the scan of one gcc error message is completed everything
767 in the buffer except the last line is one single error message.
768 But if the scan terminates with a EOF or through finding one line that
769 does not match the gcc error message criteria, then there is no
770 \begin_inset Quotes eld
774 \begin_inset Quotes erd
777 in the buffer to be concerned with.
778 In those cases we empty the buffer completely.
787 discharge_buffer (int save_last)
791 if (last_err_line != 0) {
793 clean_gcc_messages();
795 if (save_last != 0) {
797 output_error(last_buf_line-1, last_err_line, "gcc");
799 strcpy (buffer[0], buffer[last_buf_line-1]);
801 last_err_line = err_line;
809 clean_gcc_messages();
811 output_error(last_buf_line-1, last_err_line, "gcc");
828 <<Function prototypes>>=
830 void discharge_buffer (int save_last);
835 \begin_layout Standard
838 \begin_inset Quotes eld
842 \begin_inset Quotes erd
845 superfluous information from gcc messages, namely the name of the noweb
846 file and the line number of the Error.
850 \begin_layout Standard
853 For instance, some way of distinguishing between gcc Errors and Warnings
868 clean_gcc_messages ()
878 int search_len = sprintf(search, ".nw:%d:", last_err_line);
882 for (index = 0; index < last_buf_line-1; index++) {
884 tail = (char *)strstr (buffer[index], search);
888 tail = (char *) strstr (buffer[index], ".nw:");
904 head = buffer[index];
906 while (*(head++) = *(tail++));
919 <<Function prototypes>>=
921 void clean_gcc_messages ();
926 \begin_layout Standard
928 To combine the scan of noweb error messages and xlc error messages is very
930 We just try each one for every input line:
935 <<AIX system using both noweb and xlc>>=
941 while (fgets(buffer[0], 200, stdin)) {
945 output_error(1, err_line, "noweb");
949 output_error(1, err_line, "xlc");
958 \begin_layout Standard
960 To combine the scan of noweb error messages and gcc error messages is simple
961 if we realize that it is not possible to find a noweb error message in
962 the middle of a gcc error message.
963 So we just repeat the gcc procedure and test for noweb error messages in
964 the beginning of the scan:
969 <<Solaris and Linux systems using both noweb and gcc>>=
979 while (fgets(buffer[last_buf_line], 200, stdin)) {
981 /****** Skip lines until I find an error */
983 if (last_buf_line == 0 && noweb_try(0)) {
985 output_error(1, err_line, "noweb");
991 s = (char *)strpbrk(buffer[last_buf_line], " :");
993 if (s == NULL || *s == ' ')
995 continue; /* No gcc error found here */
999 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
1001 /****** OK It is an error, get line number */
1003 err_line = atoi(s+1);
1005 if (last_err_line == 0 || last_err_line == err_line) {
1007 last_err_line = err_line;
1009 continue; /* It's either a header or a continuation, don't output
1014 /****** Completed the scan of one error message, output it to LyX
1017 discharge_buffer(1);
1021 } while (fgets(buffer[last_buf_line], 200, stdin));
1025 /****** EOF completes the scan of whatever was being scanned */
1027 discharge_buffer(0);
1034 \begin_layout Section
1036 Wrapping the code into a file
1045 #include <strings.h>
1049 <<Global variables>>
1051 <<Function prototypes>>
1058 \begin_layout Standard
1060 To build this program, we want to add the
1061 \begin_inset Quotes eld
1065 \begin_inset Quotes erd
1068 option in the tangle command to force gdb to load the file
1081 In accordance with this, we pass the
1082 \begin_inset Quotes eld
1086 \begin_inset Quotes erd
1098 if [ -z "$NOWEB_SOURCE" ]; then NOWEB_SOURCE=Literate.nw; fi
1100 notangle -L -Rlisterrors.c ${NOWEB_SOURCE} > listerrors.c
1102 gcc -g -o listerrors listerrors.c
1107 \begin_layout Standard
1109 This project can be tangled and compiled from LyX if you set
1117 to call a generic script that always extracts a scrap named
1124 Here is a example of such generic script:
1127 \begin_layout LyX-Code
1131 notangle -Rbuild-script $1 | env NOWEB_SOURCE=$1 sh
1134 \begin_layout LyX-Code