1 #LyX 1.4.0cvs created this file. For more info see http://www.lyx.org/
5 \textclass literate-article
10 \paperfontsize default
17 \paperorientation portrait
20 \paragraph_separation indent
22 \quotes_language english
26 \paperpagestyle default
27 \tracking_changes false
35 LyX and Literate Programming
44 edmar-w-jr@technologist.com
48 \begin_layout Standard
50 Modified by Bernard Michael Hurley bernardh@westherts.ac.uk ---- Don't blame
51 Edmar for any errors that have crept in!
59 \begin_layout Abstract
65 This example program is provided for educational use only.
66 The functionality in this C program has been superceded by the equivalent
69 examples/listerrors.lyx
71 which should be installed in the LyX scripts directory.
80 \begin_layout Standard
91 \begin_layout Standard
94 \begin_inset LatexCommand \tableofcontents{}
101 \begin_layout Section
106 \begin_layout Standard
108 After typesetting a document, LyX scans the LaTeX log file looking for errors.
109 For each error found, the line number is obtained and a error box is displayed
110 in the LyX screen at that position.
113 \begin_layout Standard
115 To use this feature to view compilation errors while working with literate
116 documents, we need a program that filters the compilation errors and puts
117 them in a format suitable for LyX reading it.
121 \begin_layout Standard
123 In this document we present a filter that recognizes compilation error messages
124 from noweb, gnu C, and the IBM C compiler (xlc).
127 \begin_layout Standard
129 The filter is required to read from standard input, parse for error messages
130 and copy the error messages to the standard output.
131 During the output process, the filter must present the error messages in
132 a format that LyX can interpret, currently, the LaTeX error message format.
133 Of course, nothing will prevent future LyX releases from being able to
134 read other formats as well (like gcc error messages for example).
135 This mechanism is necessary to fully explore the literate programming tool's
139 \begin_layout Section
150 main (int argc, char **argv)
156 switch (argv[1][0]) {
160 <<Scan input for noweb error messages>>
166 <<Scan input for xlc error messages>>
172 <<AIX system using both noweb and xlc>>
180 <<Solaris and Linux systems using both noweb and gcc>>
188 <<Scan input for gcc error messages>>
196 <<Scan input for gcc error messages>>
207 <<Function prototypes>>=
209 int main (int argc, char **argv);
214 \begin_layout Section
219 \begin_layout Standard
221 We resort to some global variables to allow access from several different
223 These are the buffer and related pointers used during the parse of the
229 <<Global variables>>=
231 char buffer[200][200];
242 \begin_layout Section
247 \begin_layout Standard
249 The output format mimics the TeX error messages format.
250 This function prints a number of lines residing in the global variable
255 , a program name and line number.
256 There is no special requirement on the input strings, they can be anything.
260 \begin_layout Standard
262 This function has been slightly changed from EW's original to make scanning
263 a bit easier with LaTeX::scanLogFile().
264 The test has been added because LyX can crash if empty lines are allowed
265 here --- I can't figure out why! --- BMH
279 output_error (int buf_size, int error_line, char *tool)
287 fprintf(stdout, "! Build Error: ==> %s ==>
291 fprintf(stdout, " ...
301 for (i=0; i<buf_size; i++)
303 if (strlen(buffer[i]) != 0)
305 fprintf(stdout, "%s", buffer[i]);
320 <<Function prototypes>>=
322 void output_error (int buf_size, int error_line, char *tool);
327 \begin_layout Section
329 Functions Implementation
332 \begin_layout Standard
334 Both noweave and notangle routines, always output one single line for each
335 error found, thus to scan the buffer for noweb error messages is enough
336 to exam one input line at a time.
337 Note that the noweb software does not provide a line error number, so all
338 errors boxes related to noweb messages will be displayed at the beginning
344 <<Scan input for noweb error messages>>=
350 while (fgets(buffer[0], 200, stdin)) {
354 output_error(1, err_line, "noweb");
363 \begin_layout Standard
365 The examination itself is very inefficient.
366 Unfortunately noweb doesn't have any characteristic that would help to
367 identify one of its error messages.
368 The solution is to collect all possible output messages in an array of
369 strings, and turn the examination process into a linear search in this
375 <<Global variables>>=
377 char *noweb_msgs[] = {
379 "couldn't open file",
381 "couldn't open temporary file",
383 "error writing temporary file",
389 "Bad format sequence",
391 "Can't open output file",
393 "Can't open temporary file",
395 "Capacity exceeded:",
397 "Ignoring unknown option -",
399 "This can't happen:",
401 "non-numeric line number in"
407 char *noweb_msgs_mimic_gcc[] = {
409 ": unescaped << in documentation chunk"
416 \begin_layout Standard
418 A noweb error message can be any string that contains a matching pair of
422 > >, or any of the above strings
431 noweb_try (int buf_line)
441 b = buffer[buf_line];
447 for (i=0; i<1; i++) {
449 s = (char *)strstr (b, noweb_msgs_mimic_gcc[i]);
453 t = (char *)strchr(buffer[buf_line], ':');
455 err_line = atoi(t+1);
457 t = buffer[buf_line];
461 while (*(t++) = *(s++));
469 s = (char *)strstr(b, "<<");
473 s = (char *)strstr(s+2, ">>");
483 for (i = 0; i < 12; ++i) {
485 s = (char *)strstr (b, noweb_msgs[i]);
506 <<Function prototypes>>=
508 int noweb_try (int buf_line);
513 \begin_layout Standard
515 The xlc compiler always outputs one single line for each error found, thus
516 to scan the buffer for xlc error messages it is enough to exam one input
522 <<Scan input for xlc error messages>>=
528 while (fgets(buffer[last_buf_line], 200, stdin)) {
532 output_error(1, err_line, "xlc");
541 \begin_layout Standard
543 A xlc error message is easy to identify.
544 Every error message starts with a quoted string with no spaces, a comma,
546 \begin_inset Quotes eld
550 \begin_inset Quotes erd
553 , a space, and some variable text.
554 The following routine tests if a given buffer line matches this criteria:
563 xlc_try (int buf_line)
571 t = buffer[buf_line];
575 while (*s != '"' && *s != ' ' && *s != '
581 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
598 <<Function prototypes>>=
600 int xlc_try (int buf_line);
605 \begin_layout Standard
607 The gcc compiler error messages are more complicated to scan.
608 Each error can span more than one line in the buffer.
609 The good news is that every buffer line on each error has the same pattern,
610 and share the same line number.
611 Thus the strategy will be to accumulate lines in the buffer while the reported
612 line number is still the same.
613 At the time they differ, all the accumulated lines, except the last one,
614 will belong to one single error message, which now can be output-ed to
618 \begin_layout Standard
620 Every gcc error message contains a string with no space followed by a
621 \begin_inset Quotes eld
625 \begin_inset Quotes eld
629 If the next character is a space, then this line is a header of a error
630 message and the next line will detail the line number of the source code
631 where the error was found.
632 Otherwise, the next thing is a integer number followed by another
633 \begin_inset Quotes eld
637 \begin_inset Quotes eld
645 <<Scan input for gcc error messages>>=
655 while (fgets(buffer[last_buf_line], 200, stdin)) {
657 /****** Skip lines until I find an error */
659 s = (char *)strpbrk(buffer[last_buf_line], " :");
661 if (s == NULL || *s == ' ')
663 continue; /* No gcc error found here */
667 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
669 /****** OK It is an error message, get line number */
671 err_line = atoi(s+1);
673 if (last_err_line == 0 || last_err_line == err_line) {
675 last_err_line = err_line;
677 continue; /* It's either a header or a continuation, don't output
682 /****** Completed the scan of one error message, output it to LyX
689 } while (fgets(buffer[last_buf_line], 200, stdin));
693 /****** EOF completes the scan of whatever was being scanned */
704 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
706 /****** Search first ":" in the error number */
708 s = (char *)strpbrk(buffer[last_buf_line], " :");
712 if (s == NULL || *s == ' ')
714 <<No gcc error found here, but it might terminate the scanning of a previous
717 /****** Search second ":" in the error number */
719 t = (char *)strpbrk(s+1, " :");
721 if (t == NULL || *t == ' ')
723 <<No gcc error found here, but it might terminate the scanning of a previous
726 /****** Verify if is all digits between ":" */
728 if (t != s+1+strspn(s+1, "0123456789"))
730 <<No gcc error found here, but it might terminate the scanning of a previous
738 <<No gcc error found here, but it might terminate the scanning of a previous
754 \begin_layout Standard
756 As we mentioned, when the scan of one gcc error message is completed everything
757 in the buffer except the last line is one single error message.
758 But if the scan terminates with a EOF or through finding one line that
759 does not match the gcc error message criteria, then there is no
760 \begin_inset Quotes eld
764 \begin_inset Quotes erd
767 in the buffer to be concerned with.
768 In those cases we empty the buffer completely.
777 discharge_buffer (int save_last)
781 if (last_err_line != 0) {
783 clean_gcc_messages();
785 if (save_last != 0) {
787 output_error(last_buf_line-1, last_err_line, "gcc");
789 strcpy (buffer[0], buffer[last_buf_line-1]);
791 last_err_line = err_line;
799 clean_gcc_messages();
801 output_error(last_buf_line-1, last_err_line, "gcc");
818 <<Function prototypes>>=
820 void discharge_buffer (int save_last);
825 \begin_layout Standard
828 \begin_inset Quotes eld
832 \begin_inset Quotes erd
835 superfluous information from gcc messages, namely the name of the noweb
836 file and the line number of the Error.
840 \begin_layout Standard
843 For instance, some way of distinguishing between gcc Errors and Warnings
858 clean_gcc_messages ()
868 int search_len = sprintf(search, ".nw:%d:", last_err_line);
872 for (index = 0; index < last_buf_line-1; index++) {
874 tail = (char *)strstr (buffer[index], search);
878 tail = (char *) strstr (buffer[index], ".nw:");
894 head = buffer[index];
896 while (*(head++) = *(tail++));
909 <<Function prototypes>>=
911 void clean_gcc_messages ();
916 \begin_layout Standard
918 To combine the scan of noweb error messages and xlc error messages is very
920 We just try each one for every input line:
925 <<AIX system using both noweb and xlc>>=
931 while (fgets(buffer[0], 200, stdin)) {
935 output_error(1, err_line, "noweb");
939 output_error(1, err_line, "xlc");
948 \begin_layout Standard
950 To combine the scan of noweb error messages and gcc error messages is simple
951 if we realize that it is not possible to find a noweb error message in
952 the middle of a gcc error message.
953 So we just repeat the gcc procedure and test for noweb error messages in
954 the beginning of the scan:
959 <<Solaris and Linux systems using both noweb and gcc>>=
969 while (fgets(buffer[last_buf_line], 200, stdin)) {
971 /****** Skip lines until I find an error */
973 if (last_buf_line == 0 && noweb_try(0)) {
975 output_error(1, err_line, "noweb");
981 s = (char *)strpbrk(buffer[last_buf_line], " :");
983 if (s == NULL || *s == ' ')
985 continue; /* No gcc error found here */
989 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
991 /****** OK It is an error, get line number */
993 err_line = atoi(s+1);
995 if (last_err_line == 0 || last_err_line == err_line) {
997 last_err_line = err_line;
999 continue; /* It's either a header or a continuation, don't output
1004 /****** Completed the scan of one error message, output it to LyX
1007 discharge_buffer(1);
1011 } while (fgets(buffer[last_buf_line], 200, stdin));
1015 /****** EOF completes the scan of whatever was being scanned */
1017 discharge_buffer(0);
1024 \begin_layout Section
1026 Wrapping the code into a file
1035 #include <strings.h>
1039 <<Global variables>>
1041 <<Function prototypes>>
1048 \begin_layout Standard
1050 To build this program, we want to add the
1051 \begin_inset Quotes eld
1055 \begin_inset Quotes erd
1058 option in the tangle command to force gdb to load the file
1067 In accordance with this, we pass the
1068 \begin_inset Quotes eld
1072 \begin_inset Quotes erd
1084 if [ -z "$NOWEB_SOURCE" ]; then NOWEB_SOURCE=Literate.nw; fi
1086 notangle -L -Rlisterrors.c ${NOWEB_SOURCE} > listerrors.c
1088 gcc -g -o listerrors listerrors.c
1093 \begin_layout Standard
1095 This project can be tangled and compiled from LyX if you set
1101 to call a generic script that always extracts a scrap named
1106 Here is a example of such generic script:
1109 \begin_layout LyX-Code
1113 notangle -Rbuild-script $1 | env NOWEB_SOURCE=$1 sh
1116 \begin_layout LyX-Code