b15e56829b8a66903c48b17e16d4db22243697b2
[ghc-hetmet.git] / ghc / 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 <stdio.h>
44 #include <ctype.h>
45
46 #define NULLSTR        ((char *)0)
47 #define DEFNCHAR       '>'
48 #define MISSINGBLANK   "unlit: Program line next to comment"
49 #define EMPTYSCRIPT    "unlit: No definitions in file (perhaps you forgot the '>'s?)"
50 #define USAGE          "usage: unlit [-q] [-n] [-c] file1 file2\n"
51 #define CANNOTOPEN     "unlit: cannot open \"%s\"\n"
52 #define DISTINCTNAMES  "unlit: input and output filenames must differ\n"
53 #define MISSINGENDCODE "unlit: missing \\end{code}\n"
54
55 #define BEGINCODE "\\begin{code}"
56 #define LENBEGINCODE 12
57 #define ENDCODE "\\end{code}"
58 #define LENENDCODE 10
59 #ifdef PSEUDOCODE
60 /* According to Will Partain, the inventor of pseudocode, this gone now. */
61 #define MISSINGENDPSEUDOCODE "unlit: missing \\end{pseudocode}\n"
62 #define BEGINPSEUDOCODE "\\begin{pseudocode}"
63 #define LENBEGINPSEUDOCODE 18
64 #define ENDPSEUDOCODE "\\end{pseudocode}"
65 #define LENENDPSEUDOCODE 16
66 #endif
67
68 typedef enum { START, BLANK, TEXT, DEFN, BEGIN, /*PSEUDO,*/ END, HASH, SHEBANG } line;
69 #define isWhitespace(c)  (c==' '  || c=='\t')
70 #define isLineTerm(c)    (c=='\n' || c==EOF)
71
72 static int noisy  = 1;   /* 0 => keep quiet about errors, 1 => report errors */
73 static int errors = 0;   /* count the number of errors reported              */
74 static int crunchnl = 0; /* don't print \n for removed lines                 */
75 static int leavecpp = 1; /* leave preprocessor lines */
76 static int ignore_shebang = 1; /* Leave out shebang (#!) lines */
77
78 /* complain(file,line,what)
79  *
80  * print error message `what' for `file' at `line'.  The error is suppressed
81  * if noisy is not set.
82  */
83
84 complain(file, lin, what)
85 char *file;
86 char *what;
87 int lin; {
88     if (noisy) {
89         if (file)
90             fprintf(stderr, "%s ", file);
91         fprintf(stderr,"line %d: %s\n",lin,what);
92         errors++;
93     }
94 }
95
96 #define TABPOS 8
97
98 /* As getc, but does TAB expansion */
99 int
100 egetc(istream)
101 FILE *istream;
102 {
103     static int spleft = 0;
104     static int linepos = 0;
105     int c;
106
107     if (spleft > 0) {
108         spleft--;
109         linepos++;
110         return ' ';
111     }
112     c = getc(istream);
113     if (c == EOF)
114         return c;
115     else if (c == '\n' || c == '\f') {
116         linepos = 0;
117         return c;
118     } else if (c == '\t') {
119         spleft = TABPOS - linepos % TABPOS;
120         spleft--;
121         linepos++;
122         return ' ';
123     } else {
124         linepos++;
125         return c;
126     }
127
128 }
129
130 /* readline(istream, ostream)
131  *
132  * Read a line from the input stream `istream', and return a value
133  * indicating whether that line was:
134  *     BLANK (whitespace only),
135  *     DEFN  (first character is DEFNCHAR),
136  *     TEXT  (a line of text)
137  *     BEGIN (a \begin{code} line)
138  *     PSEUDO (a \begin{pseodocode} line)
139  *     HASH  (a preprocessor line)
140  * or  END   (indicating an EOF).
141  * Lines of type DEFN are copied to the output stream `ostream'
142  * (without the leading DEFNCHAR).  BLANK and TEXT lines are
143  * replaced by empty (i.e. blank lines) in the output stream, so
144  * that error messages refering to line numbers in the output file
145  * can also be used to locate the corresponding line in the input
146  * stream.
147  */
148
149 line readline(istream,ostream)
150 FILE *istream, *ostream; {
151     int c, c1;
152     char buf[100];
153     int i;
154
155     c = egetc(istream);
156
157     if (c==EOF)
158         return END;
159   
160     if ( c == '#' ) {
161       if ( ignore_shebang ) {
162          c1 = egetc(istream);
163          if ( c1 == '!' ) {
164            while (c=egetc(istream), !isLineTerm(c)) ;
165            return SHEBANG;
166          }
167          putc(c, ostream);
168          c=c1;
169       }
170       if ( leavecpp ) {
171         putc(c, ostream);
172         while (c=egetc(istream), !isLineTerm(c))
173             putc(c,ostream);
174         putc('\n',ostream);
175         return HASH;
176       }
177     }
178
179     if (c==DEFNCHAR) {
180 /*      putc(' ',ostream);*/
181         while (c=egetc(istream), !isLineTerm(c))
182             putc(c,ostream);
183         putc('\n',ostream);
184         return DEFN;
185     }
186
187     if (!crunchnl)
188         putc('\n',ostream);
189
190     while (isWhitespace(c))
191         c=egetc(istream);
192     if (isLineTerm(c))
193         return BLANK;
194
195     i = 0;
196     buf[i++] = c;
197     while (c=egetc(istream), !isLineTerm(c))
198         if (i < sizeof buf - 1)
199             buf[i++] = c;
200     while(i > 0 && isspace(buf[i-1]))
201         i--;
202     buf[i] = 0;
203     if (strcmp(buf, BEGINCODE) == 0)
204         return BEGIN;
205 #ifdef PSEUDOCODE
206     else if (strcmp(buf, BEGINPSEUDOCODE) == 0)
207         return PSEUDO;
208 #endif
209     else
210         return TEXT;
211 }
212
213
214 /* unlit(file,istream,ostream)
215  *
216  * Copy the file named `file', accessed using the input stream `istream'
217  * to the output stream `ostream', removing any comments and checking
218  * for bad use of literate script features:
219  *  - there should be at least one BLANK line between a DEFN and TEXT
220  *  - there should be at least one DEFN line in a script.
221  */
222
223 unlit(file, istream, ostream)
224 char *file;
225 FILE *istream;
226 FILE *ostream; {
227     line last, this=START;
228     int  linesread=0;
229     int  defnsread=0;
230
231     do {
232         last = this;
233         this = readline(istream, ostream);
234         linesread++;
235         if (this==DEFN)
236             defnsread++;
237         if (last==DEFN && this==TEXT)
238             complain(file, linesread-1, MISSINGBLANK);
239         if (last==TEXT && this==DEFN)
240             complain(file, linesread, MISSINGBLANK);
241         if (this == BEGIN) {
242             /* start of code, copy to end */
243             char lineb[1000];
244             for(;;) {
245                 if (fgets(lineb, sizeof lineb, istream) == NULL) {
246                     complain(file, linesread, MISSINGENDCODE);
247                     exit(1);
248                 }
249                 linesread++;
250                 if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) {
251                     putc('\n', ostream);
252                     break;
253                 }
254                 fputs(lineb, ostream);
255             }
256             defnsread++;
257         }
258 #ifdef PSEUDOCODE
259         if (this == PSEUDO) {
260             char lineb[1000];
261             for(;;) {
262                 if (fgets(lineb, sizeof lineb, istream) == NULL) {
263                     complain(file, linesread, MISSINGENDPSEUDOCODE);
264                     exit(1);
265                 }
266                 linesread++;
267                 putc('\n', ostream);
268                 if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) {
269                     break;
270                 }
271             }
272         }
273 #endif
274     } while(this!=END);
275
276     if (defnsread==0)
277         complain(file,linesread,EMPTYSCRIPT);
278 }
279
280 /* main(argc, argv)
281  *
282  * Main program.  Processes command line arguments, looking for leading:
283  *  -q  quiet mode - do not complain about bad literate script files
284  *  -n  noisy mpde - complain about bad literate script files.
285  *  -r  remove cpp droppings in output.
286  * Expects two additional arguments, a file name for the input and a file
287  * name for the output file.  These two names must normally be distinct.
288  * An exception is made for the special name "-" which can be used in either
289  * position to specify the standard input or the standard output respectively.
290  */
291
292 main(argc,argv)
293 int argc;
294 char **argv; {
295     FILE *istream, *ostream;
296     char *file;
297
298     for (argc--, argv++; argc > 0; argc--, argv++)
299         if (strcmp(*argv,"-n")==0)
300             noisy = 1;
301         else if (strcmp(*argv,"-q")==0)
302             noisy = 0;
303         else if (strcmp(*argv,"-c")==0)
304             crunchnl = 1;
305         else if (strcmp(*argv,"-#")==0)
306             ignore_shebang = 0;
307         else
308             break;
309
310     if (argc!=2) {
311         fprintf(stderr, USAGE);
312         exit(1);
313     }
314
315     if (strcmp(argv[0],argv[1])==0 && strcmp(argv[0],"-")!=0) {
316         fprintf(stderr, DISTINCTNAMES);
317         exit(1);
318     }
319
320     file = argv[0];
321     if (strcmp(argv[0], "-")==0) {
322         istream = stdin;
323         file    = "stdin";
324     }
325     else
326         if ((istream=fopen(argv[0], "r")) == NULL) {
327             fprintf(stderr, CANNOTOPEN, argv[0]);
328             exit(1);
329         }
330
331     if (strcmp(argv[1], "-")==0) 
332         ostream = stdout; 
333     else
334         if ((ostream=fopen(argv[1], "w")) == NULL)  {
335             fprintf(stderr, CANNOTOPEN, argv[1]);
336             exit(1);
337         }
338
339     unlit(file, istream, ostream);
340
341     fclose(istream);
342     fclose(ostream);
343
344     exit(errors==0 ? 0 : 1);
345 }