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