Debian Codegraph @

texk/web2c/cwebdir/examples/extex.w

raw
1 \datethis
2 @* Introduction. This program is a simple filter that inputs \TEX/ or \.{CWEB}
3 files and outputs its best guess at the ``words'' they contain. The word
4 list can then be passed to a spelling check routine such as {\tt wordtest}.
5
6 If this program is invoked with the name `{\tt excweb}', it will apply
7 special rules based on the syntax of \.{CWEB} files. Otherwise it will
8 use only the \TEX/ conventions. (Note that \UNIX/'s {\tt ln} command
9 allows a program to be invoked with more than one name although it
10 appears only once in the computer's memory.)
11
12 The \TEX/ conventions adopted here say that words are what remain
13 after you remove nonletters, control sequences,
14 comments triggered by \.\% marks, and material enclosed
15 within \.{\$...\$} or \.{\$\$...\$\$}. However, an apostrophe within
16 a word will be retained. The plain \TEX/ control
17 sequences for accented characters and special text characters, namely
18 $$\vbox{\halign{&\.{\\#}\hfil\qquad\cr
19 '&`&\relax\^&"&\relax\~&=&.&u&v\cr
20 H&t&c&d&b&oe&OE&ae&AE\cr
21 aa&AA&o&O&l&L&ss&i&j\cr}}$$
22 will also be retained, so that users can treat them as parts of words.
23 A blank space
24 following any of the alphabetic control sequences in this list will be carried
25 along too. If any of these control sequences is followed by \.\{, everything
26 up to the next \.\} will also be retained. Thus, for example, the
27 construction `\.{m\\=\{\\i\}n\\u\ us}' will be considered a single word,
28 in spite of the control sequences and the space between the two u's.
29 Discretionary hyphens `\.{\char`\\-}' are treated in the same way as accents.
30
31 The \.{CWEB} conventions are essentially the same as the \TEX/ conventions,
32 in the \TEX/ parts of a \.{CWEB} file. The \CEE/ parts of the file
33 are blanked out.
34
35 No attempt is made to reach a high level of artificial intelligence,
36 which would be able to truly understand the input file. Tricky users can
37 confuse us. But we claim that devious tricks are their problem, not ours.
38
39 @ So here goes. The main idea is to keep a one-character lookahead
40 buffer, called |c|, which is set to zero when the character has been
41 processed. A giant switch to various cases, depending on the value of~|c|,
42 keeps everything moving.
43
44 If you don't like |goto| statements, don't read this. (And don't read
45 any other programs that simulate finite-state automata.)
46
47 @c
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <string.h>
52 @#
53 @<Global variables@>@;
54 @<Procedures@>@;
55 @#
56 int main(
57 int argc, /* the number of arguments (should be 1, but this isn't checked) */
58 char *argv[]) /* the arguments (|*argv| is the program name) */
59 { @+ (void)argc;
60 @<Local variables@>;
61 if (strlen(*argv)>=6 && strcmp(*argv+strlen(*argv)-6,"excweb")==0) {
62 web=1;
63 @<Adjust tables for \.{CWEB} mode@>;
64 } else web=0;
65 comment=skipping=c=0;
66 main_cycle: if (c) goto big_switch;
67 restart: c=get();
68 big_switch: switch(c) {
69 @<Special cases of the giant switch where we don't just discard |c|@>@;
70 case EOF: exit(0);
71 default: goto restart;
72 }
73 @<Labeled code segments, which exit by explicit |goto|@>;
74 }
75
76 @ @<Global variables@>=
77 int c; /* one-character look-see buffer */
78
79 @ @<Local variables@>=
80 int web; /* are we looking for \.{CWEB} constructs? */
81 int comment; /* are we inside a \CEE/ comment in a \.{CWEB} document? */
82 int skipping; /* are we skipping \CEE/ code in a \.{CWEB} document? */
83 int save_skipping; /* value of |skipping| outside current \CEE/ mode */
84 register int cc; /* temporary buffer */
85
86 @* Simple cases.
87 Let's do some of the easiest things first, in order to get the hang of
88 this program. Several special characters will cause us to ignore everything
89 until the first appearance of something else.
90
91 @d discard_to(x) {@+while (get()!=x) ;@+}
92 @d discard_to_dol {@+for (cc=c,c=get();c!='$' || cc=='\\';cc=c,c=get())
93 if (cc=='\\' && c==cc) c='\0';@+}
94
95 @<Special cases...@>=
96 case '%': discard_to('\n');@+goto restart;
97 case '$': c=getchar();
98 if (c!='$') discard_to_dol@;
99 else { /* after \.{\$\$} we discard everything to the next \.{\$\$} */
100 do discard_to_dol@;
101 while (getchar()!='$');
102 }
103 goto restart;
104
105 @ The `|get|' procedure in the code above is like \Cee's standard
106 `|getchar|', except that it immediately terminates execution at the end of
107 the input file. Otherwise malformed input files could lead to
108 infinite loops.
109
110 @<Procedures@>=
111 int get(void)
112 {@+register int x;
113 x=getchar();
114 if (x==EOF) exit(0);
115 return x;
116 }
117
118 @ More complex behavior is handled by jumping out of the |switch| statement
119 to one of the routines following it. None of the cases say |break|, so
120 the code following the switch statement is accessible only via |goto|.
121
122 @<Special cases...@>=
123 case 'a': case 'A':
124 case 'b': case 'B':
125 case 'c': case 'C':
126 case 'd': case 'D':
127 case 'e': case 'E':
128 case 'f': case 'F':
129 case 'g': case 'G':
130 case 'h': case 'H':
131 case 'i': case 'I':
132 case 'j': case 'J':
133 case 'k': case 'K':
134 case 'l': case 'L':
135 case 'm': case 'M':
136 case 'n': case 'N':
137 case 'o': case 'O':
138 case 'p': case 'P':
139 case 'q': case 'Q':
140 case 'r': case 'R':
141 case 's': case 'S':
142 case 't': case 'T':
143 case 'u': case 'U':
144 case 'v': case 'V':
145 case 'w': case 'W':
146 case 'x': case 'X':
147 case 'y': case 'Y':
148 case 'z': case 'Z':
149 goto out_word;
150
151 @ When letters appear in |stdin|, we pass them immediately through to |stdout|
152 with little further ado.
153 An apostrophe is rejected unless it is immediately followed by a letter.
154
155 @<Labeled code...@>=
156 out_word: putchar(c);
157 continue_word: c=getchar();
158 checkout_word:
159 if (isalpha(c)) goto out_word;
160 if (c=='\'') {
161 c=getchar();
162 if (isalpha(c)) {
163 putchar('\'');@+goto out_word;
164 }
165 goto end_word;
166 }
167 if (c=='\\' && controlseq()) goto control_seq_in_word;
168 end_word: putchar('\n');
169 goto main_cycle;
170
171 @* Control sequences. The |controlseq()| function is the only
172 delicate part of this program. After a backslash has been scanned,
173 |controlseq| looks to see if the next characters define one of the
174 special plain \TEX/ macros listed above. If so, the control sequence
175 and its immediately following argument (if any) are output and
176 |controlseq| returns a nonzero value. If not, nothing is output and
177 |controlseq| returns zero. In both cases the value of |c| will be
178 nonzero if and only if |controlseq| has had to look ahead at a
179 character it decided not to process.
180
181 @ @<Labeled code...@>=
182 control_seq_in_word: if (!c) goto continue_word;
183 goto checkout_word;
184
185 @ @<Special cases...@>=
186 case '\\': if (controlseq()) goto control_seq_in_word;
187 goto main_cycle;
188
189 @ @<Procedures@>=
190 int controlseq(void)
191 {
192 int l; /* number of letters in the control sequence */
193 char a,b; /* the first two characters after `\.\\' */
194 l=0;
195 a=c=getchar();
196 while (isalpha(c)) {
197 l++;
198 c=getchar();
199 if (l==1) b=c;
200 }
201 if (l==0) c=getchar();
202 @<Check for special plain \TEX/ control sequences;
203 output them and |return 1| if found@>;
204 return 0;
205 }
206
207 @ @d pair(x,y) (a==x && b==y)
208
209 @<Check for special...@>=
210 if ((a>='"' && a<='~' && ptab[a-'"']==l) ||
211 (l==2 && (pair('a','e') || pair('A','E')@|
212 || pair('o','e') || pair('O','E')@|
213 || pair('a','a') || pair('A','A') || pair('s','s')))) {
214 putchar('\\');
215 putchar(a);
216 if (l==2) putchar(b);
217 if (l && c==' ') {
218 putchar(' '); /* optional space after alphabetic control sequence */
219 c=getchar();
220 }
221 if (c=='{') {
222 do {putchar(c);
223 c=get();
224 } while (c!='}'); /* optional argument after special control sequence */
225 putchar(c);
226 c=0;
227 }
228 return 1;
229 }
230
231 @ The |ptab| entries for nonletters are 0 when the control sequence is
232 special, otherwise~1; the conventions for letters are reversed.
233
234 @<Global...@>=
235 char ptab[]={0,1,1,1,1,0, /* \.{\\"} and \.{\\'} */
236 1,1,1,1,1,0,0,1, /* \.{\\-} and \.{\\.} */
237 1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1, /* \.{\\=} */
238 1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1, /* \.{\\H}, \.{\\L}, \.{\\O} */
239 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1, /* \.{\\\^} */
240 0,0,1,1,1,0,0,0, /* \.{\\`}, \.{\\b}, \.{\\c}, \.{\\d} */
241 0,1,1,0,1,0,0,1, /* \.{\\i}, \.{\\j}, \.{\\l}, \.{\\o} */
242 0,0,0,0,1,1,1,0, /* \.{\\t}, \.{\\u}, \.{\\v} */
243 0,0,0,1,1,1,0}; /* \.{\\\~} */
244
245 @ In \.{CWEB} the \TEX/ control sequence `\.{\\.}' denotes the typewriter
246 font used for strings, not the dot-over accent. We must modify
247 |ptab| to reflect this unfortunate (but too-late-too-change) design decision.
248
249 @<Adjust tables for \.{CWEB} mode@>=
250 ptab[12]=1;
251
252 @* CWEB considerations.
253 We're finished now with all that would be needed if we only wanted to
254 handle \TEX/. For \.{CWEB} a bit more should be done.
255
256 The \.{CWEB} escape character is \.{@@}, and the character following
257 it tells us what mode we should enter. In \TEX/ mode we should not
258 only do what we normally do for \TEX/ files, we should also ignore
259 material delimited by \.{\char"7C...\char"7C}, being careful to recognize when
260 |'|'| is part of a string (as it just was). And we should stop \TEX/
261 mode if we scan \.{*/} while in a \CEE/ comment.
262
263 @<Special cases...@>=
264 case '@@': if (web) goto do_web;
265 goto restart;
266 case '|': if (web>1) {
267 save_skipping=skipping;
268 goto skip_C_prime;
269 }
270 goto restart;
271 case '*': if (!comment) goto restart;
272 c=getchar();
273 if (c=='/') {
274 comment=0;
275 goto skip_C;
276 }
277 goto big_switch;
278
279 @ The characters that follow \.@@ in a \.{CWEB} file can be classified into
280 a few types that have distinct implications for {\tt excweb}.
281
282 @d nop 0 /* control code that doesn't matter to us */
283 @d start_section 1 /* control code that begins a \.{CWEB} section */
284 @d start_C 2 /* control code that begins \CEE/ code */
285 @d start_name 3 /* control code that begins a section name */
286 @d start_index 4 /* control code for \.{CWEB} index entry */
287 @d start_insert 5 /* control code for \CEE/ material ended by `\.{@@>}' */
288 @d end_item 6 /* `\.{@@>}' */
289 @d ignore_line 7 /* `\.{@@i}' or `\.{@@l}' */
290
291 @<Global...@>=
292 char wtab[]={start_section,nop,nop,nop,nop,nop,nop,nop, /* \.{\ !"\#\$\%\&'} */
293 start_name,nop,start_section,nop,nop,nop,start_index,nop, /* \.{()*+,-./} */
294 nop,nop,nop,nop,nop,nop,nop,nop, /* \.{01234567} */
295 nop,nop,start_index,nop,start_name,start_insert,end_item,nop,
296 /* \.{89:;<=>?} */
297 nop,nop,nop,start_C,start_C,nop,start_C,nop, /* \.{@@ABCDEFG} */
298 nop,ignore_line,nop,nop,ignore_line,nop,nop,nop, /* \.{HIJKLMNO} */
299 start_C,start_insert,nop,start_C,start_insert,nop,nop,nop, /* \.{PQRSTUVW} */
300 nop,nop,nop,nop,nop,nop,start_index,nop, /* \.{XYZ[\\]\^\_} */
301 nop,nop,nop,start_C,start_C,nop,start_C,nop, /* \.{`abcdefg} */
302 nop,ignore_line,nop,nop,ignore_line,nop,nop,nop, /* \.{hijklmno} */
303 start_C,start_insert,nop,start_C,start_insert}; /* \.{pqrst} */
304
305 @ We do not leave \TEX/ mode until the first \.{WEB} section has begun.
306
307 @<Labeled code...@>=
308 do_web: c=getchar();
309 if (c<' ' || c>'t') goto restart;
310 switch(wtab[c-' ']){
311 case nop: case start_index: goto restart;
312 case start_section: web++; /* out of ``limbo'' */
313 comment=skipping=0;@+goto restart;
314 case start_C: if (web>1) goto skip_C;
315 goto restart;
316 case start_name: case start_insert: if (web>1) skipping=1;
317 goto restart;
318 case end_item: if (skipping) goto skip_C;
319 goto restart;
320 case ignore_line: discard_to('\n');
321 goto restart;
322 }
323
324 @ The final piece of program we need is a sub-automaton to pass over
325 the \CEE/ parts of a \.{CWEB} document. The main subtlety here is that
326 we don't want to get out of synch while scanning over a supposed
327 string constant or verbatim insert.
328
329 @<Labeled code...@>=
330 skip_C: save_skipping=2;
331 skip_C_prime: skipping=1;
332 while (1) {
333 c=get();
334 C_switch: switch(c) {
335 case '/': c=get();
336 if (c!='*') goto C_switch;
337 comment=1; @+@=/* fall through */@>@; /* to the next case, returning to \TEX/ mode */
338 case '|': if (save_skipping==2) continue; /* |'|'| as \CEE/ operator */
339 skipping=save_skipping;@+goto restart; /* |'|'| as \.{CWEB} delimiter */
340 case '@@': c=getchar();
341 inner_switch: if (c<' ' || c>'t') continue;
342 switch(wtab[c-' ']) {
343 case nop: case start_C: case end_item: continue;
344 case start_section: web++;
345 comment=skipping=0;@+goto restart;
346 case start_name: case start_index: goto restart;
347 case start_insert: do discard_to('@@')@; while ((c=getchar())=='@@');
348 goto inner_switch; /* now |c| should equal |'>'| */
349 case ignore_line: discard_to('\n');
350 continue;
351 }
352 case '\'': case '"':
353 while ((cc=get())!=c && cc!='\n')
354 if (cc=='\\') getchar();
355 else if (cc=='@@') {
356 cc=getchar();
357 if (cc!='@@') { /* syntax error, we try to recover */
358 c=cc; goto inner_switch;
359 }
360 };
361 continue; /* \.{CWEB} strings do not extend past one line */
362 default: continue;
363 }
364 }
365
366 @* Index.