1 #LyX 1.3 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!
53 This example program is provided for educational use only.
54 The functionality in this C program has been superceded by the equivalent
57 examples/listerrors.lyx
59 which should be installed in the LyX scripts directory.
76 \begin_inset LatexCommand \tableofcontents{}
86 After typesetting a document, LyX scans the LaTeX log file looking for errors.
87 For each error found, the line number is obtained and a error box is displayed
88 in the LyX screen at that position.
91 To use this feature to view compilation errors while working with literate
92 documents, we need a program that filters the compilation errors and puts
93 them in a format suitable for LyX reading it.
97 In this document we present a filter that recognizes compilation error messages
98 from noweb, gnu C, and the IBM C compiler (xlc).
101 The filter is required to read from standard input, parse for error messages
102 and copy the error messages to the standard output.
103 During the output process, the filter must present the error messages in
104 a format that LyX can interpret, currently, the LaTeX error message format.
105 Of course, nothing will prevent future LyX releases from being able to
106 read other formats as well (like gcc error messages for example).
107 This mechanism is necessary to fully explore the literate programming tool's
118 main (int argc, char **argv)
124 switch (argv[1][0]) {
128 <<Scan input for noweb error messages>>
134 <<Scan input for xlc error messages>>
140 <<AIX system using both noweb and xlc>>
148 <<Solaris and Linux systems using both noweb and gcc>>
156 <<Scan input for gcc error messages>>
164 <<Scan input for gcc error messages>>
173 <<Function prototypes>>=
175 int main (int argc, char **argv);
183 We resort to some global variables to allow access from several different
185 These are the buffer and related pointers used during the parse of the
189 <<Global variables>>=
191 char buffer[200][200];
205 The output format mimics the TeX error messages format.
206 This function prints a number of lines residing in the global variable
211 , a program name and line number.
212 There is no special requirement on the input strings, they can be anything.
218 This function has been slightly changed from EW's original to make scanning
219 a bit easier with LaTeX::scanLogFile().
220 The test has been added because LyX can crash if empty lines are allowed
221 here --- I can't figure out why! --- BMH
231 output_error (int buf_size, int error_line, char *tool)
239 fprintf(stdout, "! Build Error: ==> %s ==>
243 fprintf(stdout, " ...
253 for (i=0; i<buf_size; i++)
255 if (strlen(buffer[i]) != 0)
257 fprintf(stdout, "%s", buffer[i]);
270 <<Function prototypes>>=
272 void output_error (int buf_size, int error_line, char *tool);
277 Functions Implementation
280 Both noweave and notangle routines, always output one single line for each
281 error found, thus to scan the buffer for noweb error messages is enough
282 to exam one input line at a time.
283 Note that the noweb software does not provide a line error number, so all
284 errors boxes related to noweb messages will be displayed at the beginning
288 <<Scan input for noweb error messages>>=
294 while (fgets(buffer[0], 200, stdin)) {
298 output_error(1, err_line, "noweb");
307 The examination itself is very inefficient.
308 Unfortunately noweb doesn't have any characteristic that would help to
309 identify one of its error messages.
310 The solution is to collect all possible output messages in an array of
311 strings, and turn the examination process into a linear search in this
315 <<Global variables>>=
317 char *noweb_msgs[] = {
319 "couldn't open file",
321 "couldn't open temporary file",
323 "error writing temporary file",
329 "Bad format sequence",
331 "Can't open output file",
333 "Can't open temporary file",
335 "Capacity exceeded:",
337 "Ignoring unknown option -",
339 "This can't happen:",
341 "non-numeric line number in"
347 char *noweb_msgs_mimic_gcc[] = {
349 ": unescaped << in documentation chunk"
356 A noweb error message can be any string that contains a matching pair of
360 > >, or any of the above strings
367 noweb_try (int buf_line)
377 b = buffer[buf_line];
383 for (i=0; i<1; i++) {
385 s = (char *)strstr (b, noweb_msgs_mimic_gcc[i]);
389 t = (char *)strchr(buffer[buf_line], ':');
391 err_line = atoi(t+1);
393 t = buffer[buf_line];
397 while (*(t++) = *(s++));
405 s = (char *)strstr(b, "<<");
409 s = (char *)strstr(s+2, ">>");
419 for (i = 0; i < 12; ++i) {
421 s = (char *)strstr (b, noweb_msgs[i]);
440 <<Function prototypes>>=
442 int noweb_try (int buf_line);
447 The xlc compiler always outputs one single line for each error found, thus
448 to scan the buffer for xlc error messages it is enough to exam one input
452 <<Scan input for xlc error messages>>=
458 while (fgets(buffer[last_buf_line], 200, stdin)) {
462 output_error(1, err_line, "xlc");
471 A xlc error message is easy to identify.
472 Every error message starts with a quoted string with no spaces, a comma,
474 \begin_inset Quotes eld
478 \begin_inset Quotes erd
481 , a space, and some variable text.
482 The following routine tests if a given buffer line matches this criteria:
489 xlc_try (int buf_line)
497 t = buffer[buf_line];
501 while (*s != '"' && *s != ' ' && *s != '
507 if (*t != '"' || *s != '"' || strncmp(s+1, ", line ", 7) != 0)
522 <<Function prototypes>>=
524 int xlc_try (int buf_line);
529 The gcc compiler error messages are more complicated to scan.
530 Each error can span more than one line in the buffer.
531 The good news is that every buffer line on each error has the same pattern,
532 and share the same line number.
533 Thus the strategy will be to accumulate lines in the buffer while the reported
534 line number is still the same.
535 At the time they differ, all the accumulated lines, except the last one,
536 will belong to one single error message, which now can be output-ed to
540 Every gcc error message contains a string with no space followed by a
541 \begin_inset Quotes eld
545 \begin_inset Quotes eld
549 If the next character is a space, then this line is a header of a error
550 message and the next line will detail the line number of the source code
551 where the error was found.
552 Otherwise, the next thing is a integer number followed by another
553 \begin_inset Quotes eld
557 \begin_inset Quotes eld
563 <<Scan input for gcc error messages>>=
573 while (fgets(buffer[last_buf_line], 200, stdin)) {
575 /****** Skip lines until I find an error */
577 s = (char *)strpbrk(buffer[last_buf_line], " :");
579 if (s == NULL || *s == ' ')
581 continue; /* No gcc error found here */
585 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
587 /****** OK It is an error message, get line number */
589 err_line = atoi(s+1);
591 if (last_err_line == 0 || last_err_line == err_line) {
593 last_err_line = err_line;
595 continue; /* It's either a header or a continuation, don't output
600 /****** Completed the scan of one error message, output it to LyX
607 } while (fgets(buffer[last_buf_line], 200, stdin));
611 /****** EOF completes the scan of whatever was being scanned */
620 <<gcc error message criteria is to find a "...:999:" or a "...: ">>=
622 /****** Search first ":" in the error number */
624 s = (char *)strpbrk(buffer[last_buf_line], " :");
628 if (s == NULL || *s == ' ')
630 <<No gcc error found here, but it might terminate the scanning of a previous
633 /****** Search second ":" in the error number */
635 t = (char *)strpbrk(s+1, " :");
637 if (t == NULL || *t == ' ')
639 <<No gcc error found here, but it might terminate the scanning of a previous
642 /****** Verify if is all digits between ":" */
644 if (t != s+1+strspn(s+1, "0123456789"))
646 <<No gcc error found here, but it might terminate the scanning of a previous
652 <<No gcc error found here, but it might terminate the scanning of a previous
668 As we mentioned, when the scan of one gcc error message is completed everything
669 in the buffer except the last line is one single error message.
670 But if the scan terminates with a EOF or through finding one line that
671 does not match the gcc error message criteria, then there is no
672 \begin_inset Quotes eld
676 \begin_inset Quotes erd
679 in the buffer to be concerned with.
680 In those cases we empty the buffer completely.
687 discharge_buffer (int save_last)
691 if (last_err_line != 0) {
693 clean_gcc_messages();
695 if (save_last != 0) {
697 output_error(last_buf_line-1, last_err_line, "gcc");
699 strcpy (buffer[0], buffer[last_buf_line-1]);
701 last_err_line = err_line;
709 clean_gcc_messages();
711 output_error(last_buf_line-1, last_err_line, "gcc");
726 <<Function prototypes>>=
728 void discharge_buffer (int save_last);
734 \begin_inset Quotes eld
738 \begin_inset Quotes erd
741 superfluous information from gcc messages, namely the name of the noweb
742 file and the line number of the Error.
749 For instance, some way of distinguishing between gcc Errors and Warnings
760 clean_gcc_messages ()
770 int search_len = sprintf(search, ".nw:%d:", last_err_line);
774 for (index = 0; index < last_buf_line-1; index++) {
776 tail = (char *)strstr (buffer[index], search);
780 tail = (char *) strstr (buffer[index], ".nw:");
796 head = buffer[index];
798 while (*(head++) = *(tail++));
809 <<Function prototypes>>=
811 void clean_gcc_messages ();
816 To combine the scan of noweb error messages and xlc error messages is very
818 We just try each one for every input line:
821 <<AIX system using both noweb and xlc>>=
827 while (fgets(buffer[0], 200, stdin)) {
831 output_error(1, err_line, "noweb");
835 output_error(1, err_line, "xlc");
844 To combine the scan of noweb error messages and gcc error messages is simple
845 if we realize that it is not possible to find a noweb error message in
846 the middle of a gcc error message.
847 So we just repeat the gcc procedure and test for noweb error messages in
848 the beginning of the scan:
851 <<Solaris and Linux systems using both noweb and gcc>>=
861 while (fgets(buffer[last_buf_line], 200, stdin)) {
863 /****** Skip lines until I find an error */
865 if (last_buf_line == 0 && noweb_try(0)) {
867 output_error(1, err_line, "noweb");
873 s = (char *)strpbrk(buffer[last_buf_line], " :");
875 if (s == NULL || *s == ' ')
877 continue; /* No gcc error found here */
881 <<gcc error message criteria is to find a "...:999:" or a "...: ">>
883 /****** OK It is an error, get line number */
885 err_line = atoi(s+1);
887 if (last_err_line == 0 || last_err_line == err_line) {
889 last_err_line = err_line;
891 continue; /* It's either a header or a continuation, don't output
896 /****** Completed the scan of one error message, output it to LyX
903 } while (fgets(buffer[last_buf_line], 200, stdin));
907 /****** EOF completes the scan of whatever was being scanned */
916 Wrapping the code into a file
929 <<Function prototypes>>
936 To build this program, we want to add the
937 \begin_inset Quotes eld
941 \begin_inset Quotes erd
944 option in the tangle command to force gdb to load the file
953 In accordance with this, we pass the
954 \begin_inset Quotes eld
958 \begin_inset Quotes erd
968 if [ -z "$NOWEB_SOURCE" ]; then NOWEB_SOURCE=Literate.nw; fi
970 notangle -L -Rlisterrors.c ${NOWEB_SOURCE} > listerrors.c
972 gcc -g -o listerrors listerrors.c
977 This project can be tangled and compiled from LyX if you set
983 to call a generic script that always extracts a scrap named
988 Here is a example of such generic script:
993 notangle -Rbuild-script $1 | env NOWEB_SOURCE=$1 sh