GHC new build system megapatch
[ghc-hetmet.git] / utils / unlit / unlit.c
1 /* unlit.c                                   Wed Dec  5 17:16:24 GMT 1990
2  *
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
10  * and explanations.  
11  *
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
31  * are reported.
32  *
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.
38  *
39  * Modified to understand \begin{code} ... \end{code} used in Glasgow.  -- LA
40  * And \begin{pseudocode} ... \end{pseudocode}.  -- LA
41  */
42
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47
48 #define NULLSTR        ((char *)0)
49 #define DEFNCHAR       '>'
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"
58
59 #define BEGINCODE "\\begin{code}"
60 #define LENBEGINCODE 12
61 #define ENDCODE "\\end{code}"
62 #define LENENDCODE 10
63 #ifdef PSEUDOCODE
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
70 #endif
71
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)
75
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 */
82
83 static char* prefix_str = NULL; /* Prefix output with a string */
84
85 static char *ofilename = NULL;
86
87 /* complain(file,line,what)
88  *
89  * print error message `what' for `file' at `line'.  The error is suppressed
90  * if noisy is not set.
91  */
92
93 void complain(char *file, int lin, char *what)
94 {
95     if (noisy) {
96         if (file)
97             fprintf(stderr, "%s ", file);
98         fprintf(stderr,"line %d: %s\n",lin,what);
99         errors++;
100     }
101 }
102
103 void writeerror(void)
104 {
105     if (!strcmp(ofilename,"-")) {
106         fprintf(stderr, CANNOTWRITESTDOUT);
107     } else {
108         fprintf(stderr, CANNOTWRITE, ofilename);
109     }
110     exit(1);
111 }
112
113 void myputc(char c, FILE *ostream)
114 {
115     if (putc(c,ostream) == EOF) {
116         writeerror();
117     }   
118 }
119
120 #define TABPOS 8
121
122 /* As getc, but does TAB expansion */
123 int
124 egetc(istream)
125 FILE *istream;
126 {
127     static int spleft = 0;
128     static int linepos = 0;
129     int c;
130
131     if (spleft > 0) {
132         spleft--;
133         linepos++;
134         return ' ';
135     }
136     c = getc(istream);
137     if (c == EOF)
138         return c;
139     else if (c == '\n' || c == '\f') {
140         linepos = 0;
141         return c;
142     } else if (c == '\t') {
143         spleft = TABPOS - linepos % TABPOS;
144         spleft--;
145         linepos++;
146         return ' ';
147     } else {
148         linepos++;
149         return c;
150     }
151
152 }
153
154 /* readline(istream, ostream)
155  *
156  * Read a line from the input stream `istream', and return a value
157  * indicating whether that line was:
158  *     BLANK (whitespace only),
159  *     DEFN  (first character is DEFNCHAR),
160  *     TEXT  (a line of text)
161  *     BEGIN (a \begin{code} line)
162  *     PSEUDO (a \begin{pseodocode} line)
163  *     HASH  (a preprocessor line)
164  * or  END   (indicating an EOF).
165  * Lines of type DEFN are copied to the output stream `ostream'
166  * (without the leading DEFNCHAR).  BLANK and TEXT lines are
167  * replaced by empty (i.e. blank lines) in the output stream, so
168  * that error messages refering to line numbers in the output file
169  * can also be used to locate the corresponding line in the input
170  * stream.
171  */
172
173 line readline(istream,ostream)
174 FILE *istream, *ostream; {
175     int c, c1;
176     char buf[100];
177     int i;
178
179     c = egetc(istream);
180
181     if (c==EOF)
182         return END;
183   
184     if ( c == '#' ) {
185       if ( ignore_shebang ) {
186          c1 = egetc(istream);
187          if ( c1 == '!' ) {
188            while (c=egetc(istream), !isLineTerm(c)) ;
189            return SHEBANG;
190          }
191          myputc(c, ostream);
192          c=c1;
193       }
194       if ( leavecpp ) {
195         myputc(c, ostream);
196         while (c=egetc(istream), !isLineTerm(c))
197             myputc(c,ostream);
198         myputc('\n',ostream);
199         return HASH;
200       }
201     }
202
203     if (c==DEFNCHAR) {
204         myputc(' ',ostream);
205         while (c=egetc(istream), !isLineTerm(c))
206             myputc(c,ostream);
207         myputc('\n',ostream);
208         return DEFN;
209     }
210
211     if (!crunchnl)
212         myputc('\n',ostream);
213
214     while (isWhitespace(c))
215         c=egetc(istream);
216     if (isLineTerm(c))
217         return BLANK;
218
219     i = 0;
220     buf[i++] = c;
221     while (c=egetc(istream), !isLineTerm(c))
222         if (i < sizeof buf - 1)
223             buf[i++] = c;
224     while(i > 0 && isspace(buf[i-1]))
225         i--;
226     buf[i] = 0;
227     if (strcmp(buf, BEGINCODE) == 0)
228         return BEGIN;
229 #ifdef PSEUDOCODE
230     else if (strcmp(buf, BEGINPSEUDOCODE) == 0)
231         return PSEUDO;
232 #endif
233     else
234         return TEXT;
235 }
236
237
238 /* unlit(file,istream,ostream)
239  *
240  * Copy the file named `file', accessed using the input stream `istream'
241  * to the output stream `ostream', removing any comments and checking
242  * for bad use of literate script features:
243  *  - there should be at least one BLANK line between a DEFN and TEXT
244  *  - there should be at least one DEFN line in a script.
245  */
246
247 void unlit(char *file, FILE *istream, FILE *ostream)
248 {
249     line last, this=START;
250     int  linesread=0;
251     int  defnsread=0;
252
253     do {
254         last = this;
255         this = readline(istream, ostream);
256         linesread++;
257         if (this==DEFN)
258             defnsread++;
259         if (last==DEFN && this==TEXT)
260             complain(file, linesread-1, MISSINGBLANK);
261         if (last==TEXT && this==DEFN)
262             complain(file, linesread, MISSINGBLANK);
263         if (this == BEGIN) {
264             /* start of code, copy to end */
265             char lineb[1000];
266             for(;;) {
267                 if (fgets(lineb, sizeof lineb, istream) == NULL) {
268                     complain(file, linesread, MISSINGENDCODE);
269                     exit(1);
270                 }
271                 linesread++;
272                 if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
273                     myputc('\n', ostream);
274                     break;
275                 }
276                 fputs(lineb, ostream);
277             }
278             defnsread++;
279         }
280 #ifdef PSEUDOCODE
281         if (this == PSEUDO) {
282             char lineb[1000];
283             for(;;) {
284                 if (fgets(lineb, sizeof lineb, istream) == NULL) {
285                     complain(file, linesread, MISSINGENDPSEUDOCODE);
286                     exit(1);
287                 }
288                 linesread++;
289                 myputc('\n', ostream);
290                 if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
291                     break;
292                 }
293             }
294         }
295 #endif
296     } while(this!=END);
297
298     if (defnsread==0)
299         complain(file,linesread,EMPTYSCRIPT);
300 }
301
302 /* main(argc, argv)
303  *
304  * Main program.  Processes command line arguments, looking for leading:
305  *  -q  quiet mode - do not complain about bad literate script files
306  *  -n  noisy mode - complain about bad literate script files.
307  *  -r  remove cpp droppings in output.
308  *  -P  don't output any CPP line pragmas.
309  * Expects two additional arguments, a file name for the input and a file
310  * name for the output file.  These two names must normally be distinct.
311  * An exception is made for the special name "-" which can be used in either
312  * position to specify the standard input or the standard output respectively.
313  */
314
315 int main(int argc,char **argv)
316 {
317     FILE *istream, *ostream;
318     char *file;
319
320     for (argc--, argv++; argc > 0; argc--, argv++)
321         if (strcmp(*argv,"-n")==0)
322             noisy = 1;
323         else if (strcmp(*argv,"-q")==0)
324             noisy = 0;
325         else if (strcmp(*argv,"-c")==0)
326             crunchnl = 1;
327         else if (strcmp(*argv,"-P")==0)
328             no_line_pragma = 1;
329         else if (strcmp(*argv,"-h")==0) {
330           if (argc > 1) {
331             argc--; argv++;
332             if (prefix_str) 
333               free(prefix_str);
334             prefix_str = (char*)malloc(sizeof(char)*(1+strlen(*argv)));
335             if (prefix_str) 
336               strcpy(prefix_str, *argv);
337           }
338         } else if (strcmp(*argv,"-#")==0)
339             ignore_shebang = 0;
340         else
341             break;
342
343     if (argc!=2) {
344         fprintf(stderr, USAGE);
345         exit(1);
346     }
347
348     if (strcmp(argv[0],argv[1])==0 && strcmp(argv[0],"-")!=0) {
349         fprintf(stderr, DISTINCTNAMES);
350         exit(1);
351     }
352
353     file = argv[0];
354     if (strcmp(argv[0], "-")==0) {
355         istream = stdin;
356         file    = "stdin";
357     }
358     else
359         if ((istream=fopen(argv[0], "r")) == NULL) {
360             fprintf(stderr, CANNOTOPEN, argv[0]);
361             exit(1);
362         }
363
364     ofilename=argv[1];
365     if (strcmp(argv[1], "-")==0) 
366         ostream = stdout; 
367     else
368         if ((ostream=fopen(argv[1], "w")) == NULL)  {
369             fprintf(stderr, CANNOTOPEN, argv[1]);
370             exit(1);
371         }
372
373     /* Prefix the output with line pragmas */
374     if (!no_line_pragma && prefix_str) {
375       /* Both GHC and CPP understand the #line pragma.
376        * We used to throw in both a #line and a {-# LINE #-} pragma
377        * here, but CPP doesn't understand {-# LINE #-} so it thought
378        * the line numbers were off by one.  We could put the {-# LINE
379        * #-} before the #line, but there's no point since GHC
380        * understands #line anyhow.  --SDM 8/2003
381        */
382       fprintf(ostream, "#line 1 \"%s\"\n", prefix_str);
383     }
384
385     unlit(file, istream, ostream);
386
387     if (istream != stdin) fclose(istream);
388     if (ostream != stdout) {
389         if (fclose(ostream) == EOF) {
390             writeerror();
391         }
392     }
393
394     exit(errors==0 ? 0 : 1);
395 }