1 /* unlit.c Wed Dec 5 17:16:24 GMT 1990
3 * Literate script filter. In contrast with the format used by most
4 * programming languages, a literate script is a program in which
5 * comments are given the leading role, whilst program text must be
6 * explicitly flagged as such by placing a `>' character in the first
7 * column on each line. It is hoped that this style of programming will
8 * encourage the writing of accurate and clearly documented programs
9 * in which the writer may include motivating arguments, examples
12 * Unlit is a filter that can be used to strip all of the comment lines
13 * out of a literate script file. The command format for unlit is:
14 * unlit [-n] [-q] ifile ofile
15 * where ifile and ofile are the names of the input (literate script) and
16 * output (raw program) files respectively. Either of these names may
17 * be `-' representing the standard input or the standard output resp.
18 * A number of rules are used in an attempt to guard against the most
19 * common errors that are made when writing literate scripts:
20 * 1) Empty script files are not permitted. A file in which no lines
21 * begin with `>' usually indicates a file in which the programmer
22 * has forgotten about the literate script convention.
23 * 2) A line containing part of program definition (i.e. preceeded by `>')
24 * cannot be used immediately before or after a comment line unless
25 * the comment line is blank. This error usually indicates that
26 * the `>' character has been omitted from a line in a section of
27 * program spread over a number of lines.
28 * Using the -q (quiet) flag suppresses the signalling of these error
29 * conditions. The default behaviour can be selected explicitly using
30 * the -n (noisy) option so that any potential errors in the script file
33 * The original idea for the use of literate scripts is due to Richard
34 * Bird of the programming Research Group, Oxford and was initially
35 * adopted for use in the implementation of the functional programming
36 * language Orwell used for teaching in Oxford. This idea has subsequently
37 * been borrowed in a number of other language implementations.
39 * Modified to understand \begin{code} ... \end{code} used in Glasgow. -- LA
40 * And \begin{pseudocode} ... \end{pseudocode}. -- LA
48 #define NULLSTR ((char *)0)
50 #define MISSINGBLANK "unlit: Program line next to comment"
51 #define EMPTYSCRIPT "unlit: No definitions in file (perhaps you forgot the '>'s?)"
52 #define USAGE "usage: unlit [-q] [-n] [-c] [-#] [-P] [-h label] file1 file2\n"
53 #define CANNOTOPEN "unlit: cannot open \"%s\"\n"
54 #define CANNOTWRITE "unlit: error writing \"%s\"\n"
55 #define CANNOTWRITESTDOUT "unlit: error writing standard output\n"
56 #define DISTINCTNAMES "unlit: input and output filenames must differ\n"
57 #define MISSINGENDCODE "unlit: missing \\end{code}\n"
59 #define BEGINCODE "\\begin{code}"
60 #define LENBEGINCODE 12
61 #define ENDCODE "\\end{code}"
64 /* According to Will Partain, the inventor of pseudocode, this gone now. */
65 #define MISSINGENDPSEUDOCODE "unlit: missing \\end{pseudocode}\n"
66 #define BEGINPSEUDOCODE "\\begin{pseudocode}"
67 #define LENBEGINPSEUDOCODE 18
68 #define ENDPSEUDOCODE "\\end{pseudocode}"
69 #define LENENDPSEUDOCODE 16
72 typedef enum { START, BLANK, TEXT, DEFN, BEGIN, /*PSEUDO,*/ END, HASH, SHEBANG } line;
73 #define isWhitespace(c) (c==' ' || c=='\t' || c=='\r')
74 #define isLineTerm(c) (c=='\n' || c==EOF)
76 static int noisy = 1; /* 0 => keep quiet about errors, 1 => report errors */
77 static int errors = 0; /* count the number of errors reported */
78 static int crunchnl = 0; /* don't print \n for removed lines */
79 static int leavecpp = 1; /* leave preprocessor lines */
80 static int ignore_shebang = 1; /* Leave out shebang (#!) lines */
81 static int no_line_pragma = 0; /* Leave out initial line pragma */
83 static char* prefix_str = NULL; /* Prefix output with a string */
85 static char *ofilename = NULL;
87 /* complain(file,line,what)
89 * print error message `what' for `file' at `line'. The error is suppressed
90 * if noisy is not set.
93 complain(file, lin, what)
99 fprintf(stderr, "%s ", file);
100 fprintf(stderr,"line %d: %s\n",lin,what);
107 if (!strcmp(ofilename,"-")) {
108 fprintf(stderr, CANNOTWRITESTDOUT);
110 fprintf(stderr, CANNOTWRITE, ofilename);
118 if (putc(c,ostream) == EOF) {
125 /* As getc, but does TAB expansion */
130 static int spleft = 0;
131 static int linepos = 0;
142 else if (c == '\n' || c == '\f') {
145 } else if (c == '\t') {
146 spleft = TABPOS - linepos % TABPOS;
157 /* readline(istream, ostream)
159 * Read a line from the input stream `istream', and return a value
160 * indicating whether that line was:
161 * BLANK (whitespace only),
162 * DEFN (first character is DEFNCHAR),
163 * TEXT (a line of text)
164 * BEGIN (a \begin{code} line)
165 * PSEUDO (a \begin{pseodocode} line)
166 * HASH (a preprocessor line)
167 * or END (indicating an EOF).
168 * Lines of type DEFN are copied to the output stream `ostream'
169 * (without the leading DEFNCHAR). BLANK and TEXT lines are
170 * replaced by empty (i.e. blank lines) in the output stream, so
171 * that error messages refering to line numbers in the output file
172 * can also be used to locate the corresponding line in the input
176 line readline(istream,ostream)
177 FILE *istream, *ostream; {
188 if ( ignore_shebang ) {
191 while (c=egetc(istream), !isLineTerm(c)) ;
199 while (c=egetc(istream), !isLineTerm(c))
201 myputc('\n',ostream);
208 while (c=egetc(istream), !isLineTerm(c))
210 myputc('\n',ostream);
215 myputc('\n',ostream);
217 while (isWhitespace(c))
224 while (c=egetc(istream), !isLineTerm(c))
225 if (i < sizeof buf - 1)
227 while(i > 0 && isspace(buf[i-1]))
230 if (strcmp(buf, BEGINCODE) == 0)
233 else if (strcmp(buf, BEGINPSEUDOCODE) == 0)
241 /* unlit(file,istream,ostream)
243 * Copy the file named `file', accessed using the input stream `istream'
244 * to the output stream `ostream', removing any comments and checking
245 * for bad use of literate script features:
246 * - there should be at least one BLANK line between a DEFN and TEXT
247 * - there should be at least one DEFN line in a script.
250 unlit(file, istream, ostream)
254 line last, this=START;
260 this = readline(istream, ostream);
264 if (last==DEFN && this==TEXT)
265 complain(file, linesread-1, MISSINGBLANK);
266 if (last==TEXT && this==DEFN)
267 complain(file, linesread, MISSINGBLANK);
269 /* start of code, copy to end */
272 if (fgets(lineb, sizeof lineb, istream) == NULL) {
273 complain(file, linesread, MISSINGENDCODE);
277 if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
278 myputc('\n', ostream);
281 fputs(lineb, ostream);
286 if (this == PSEUDO) {
289 if (fgets(lineb, sizeof lineb, istream) == NULL) {
290 complain(file, linesread, MISSINGENDPSEUDOCODE);
294 myputc('\n', ostream);
295 if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
304 complain(file,linesread,EMPTYSCRIPT);
309 * Main program. Processes command line arguments, looking for leading:
310 * -q quiet mode - do not complain about bad literate script files
311 * -n noisy mode - complain about bad literate script files.
312 * -r remove cpp droppings in output.
313 * -P don't output any CPP line pragmas.
314 * Expects two additional arguments, a file name for the input and a file
315 * name for the output file. These two names must normally be distinct.
316 * An exception is made for the special name "-" which can be used in either
317 * position to specify the standard input or the standard output respectively.
323 FILE *istream, *ostream;
326 for (argc--, argv++; argc > 0; argc--, argv++)
327 if (strcmp(*argv,"-n")==0)
329 else if (strcmp(*argv,"-q")==0)
331 else if (strcmp(*argv,"-c")==0)
333 else if (strcmp(*argv,"-P")==0)
335 else if (strcmp(*argv,"-h")==0) {
340 prefix_str = (char*)malloc(sizeof(char)*(1+strlen(*argv)));
342 strcpy(prefix_str, *argv);
344 } else if (strcmp(*argv,"-#")==0)
350 fprintf(stderr, USAGE);
354 if (strcmp(argv[0],argv[1])==0 && strcmp(argv[0],"-")!=0) {
355 fprintf(stderr, DISTINCTNAMES);
360 if (strcmp(argv[0], "-")==0) {
365 if ((istream=fopen(argv[0], "r")) == NULL) {
366 fprintf(stderr, CANNOTOPEN, argv[0]);
371 if (strcmp(argv[1], "-")==0)
374 if ((ostream=fopen(argv[1], "w")) == NULL) {
375 fprintf(stderr, CANNOTOPEN, argv[1]);
379 /* Prefix the output with line pragmas */
380 if (!no_line_pragma && prefix_str) {
381 /* Both GHC and CPP understand the #line pragma.
382 * We used to throw in both a #line and a {-# LINE #-} pragma
383 * here, but CPP doesn't understand {-# LINE #-} so it thought
384 * the line numbers were off by one. We could put the {-# LINE
385 * #-} before the #line, but there's no point since GHC
386 * understands #line anyhow. --SDM 8/2003
388 fprintf(ostream, "#line 1 \"%s\"\n", prefix_str);
391 unlit(file, istream, ostream);
393 if (istream != stdin) fclose(istream);
394 if (ostream != stdout) {
395 if (fclose(ostream) == EOF) {
400 exit(errors==0 ? 0 : 1);