Debian Codegraph @

texk/web2c/tangle.ch

raw
1 % tangle.ch for C compilation with web2c.
2 %
3 % 10/9/82 (HT) Original version
4 % 11/29 (HT) New version, with conversion to lowercase handled properly
5 % Also, new control sequence:
6 % @=...text...@> Put ...text... verbatim on a line
7 % by itself in the Pascal output.
8 % (argument must fit on one line)
9 % This control sequence facilitates putting #include "gcons.h"
10 % (for example) in files meant for the pc compiler.
11 % Also, changed command line usage, so that the absence of a
12 % change file implies no change file, rather than one with the
13 % same name as the web file, with .ch at the end.
14 % 1/15/83 (HT) Changed to work with version 1.2, which incorporates the
15 % above change (though unbundling the output line breaking),
16 % so mainly had to remove stuff.
17 % 2/17 (HT) Fixed bug that caused 0-9 in identifiers to be converted to
18 % Q-Y on output.
19 % 3/18 (HT) Brought up to work with Version 1.5. Added -r command line
20 % flag to cause a .rpl file to be written with all the lines
21 % of the .web file that were replaced because of the .ch file
22 % (useful for comparing with previous .rpl files, to see if a
23 % change file will still work with a new version of a .web file)
24 % Also, made it write a newline just before exit.
25 % 4/12 (PC) Merged with Pavel's version, including adding a call to exit()
26 % at the end depending upon the value of history.
27 % 4/16 (PC) Brought up to date with version 1.5 released April, 1983.
28 % 6/28 (HWT) Brought up to date with version 1.7 released June, 1983.
29 % With new change file format, the -r option is now unnecessary.
30 % 7/17 (HWT) Brought up to date with version 2.0 released July, 1983.
31 % 12/18/83 (ETM) Brought up to date with version 2.5 released November, 1983.
32 % 11/07/84 (ETM) Brought up to date with version 2.6.
33 % 12/15/85 (ETM) Brought up to date with version 2.8.
34 % 03/07/88 (ETM) Converted for use with WEB2C
35 % 01/02/89 (PAM) Cosmetic upgrade to version 2.9
36 % 11/30/89 (KB) Version 4.
37 % (more recent changes in the ChangeLog)
38
39 @x [0.0] l.34 - Replace program title.
40 \def\title{TANGLE}
41 @y
42 \def\title{TANGLE changes for C}
43 @z
44
45 @x [0.0] l.43 - Print only changes.
46 \pageno=\contentspagenumber \advance\pageno by 1
47 @y
48 \pageno=\contentspagenumber \advance\pageno by 1
49 \let\maybe=\iffalse
50 @z
51
52 @x [1.1] l.70 - Define my_name
53 @d banner=='This is TANGLE, Version 4.6'
54 @y
55 @d my_name=='tangle'
56 @d banner=='This is TANGLE, Version 4.6'
57 @z
58
59 @x [1.2] l.79 - Eliminate the |end_of_TANGLE| label.
60 calls the `|jump_out|' procedure, which goes to the label |end_of_TANGLE|.
61
62 @d end_of_TANGLE = 9999 {go here to wrap it up}
63 @y
64 calls the `|jump_out|' procedure.
65 @z
66 @x [1.2] l.85
67 label end_of_TANGLE; {go here to finish}
68 @y
69 @z
70
71 @x [1.2] l.90 - Define and call parse_arguments.
72 procedure initialize;
73 var @<Local variables for initialization@>@/
74 begin @<Set initial values@>@/
75 @y
76 @<Define \(|parse_arguments|@>
77 procedure initialize;
78 var @<Local variables for initialization@>@/
79 begin
80 kpse_set_program_name (argv[0], my_name);
81 parse_arguments;
82 @<Set initial values@>@/
83 @z
84
85 @x [1.8] l.183 - Constants: increase id lengths, for TeX--XeT and tex2pdf.
86 @!buf_size=100; {maximum length of input line}
87 @!max_bytes=45000; {|1/ww| times the number of bytes in identifiers,
88 strings, and module names; must be less than 65536}
89 @!max_toks=65000; {|1/zz| times the number of bytes in compressed \PASCAL\ code;
90 must be less than 65536}
91 @!max_names=4000; {number of identifiers, strings, module names;
92 must be less than 10240}
93 @!max_texts=2000; {number of replacement texts, must be less than 10240}
94 @!hash_size=353; {should be prime}
95 @y
96 @!buf_size=1000; {maximum length of input line}
97 @!max_bytes=65535; {|1/ww| times the number of bytes in identifiers,
98 strings, and module names; must be less than 65536}
99 @!max_toks=65535; {|1/zz| times the number of bytes in compressed \PASCAL\ code;
100 must be less than 65536}
101 @!max_names=10239; {number of identifiers, strings, module names;
102 must be less than 10240}
103 @!max_texts=10239; {number of replacement texts, must be less than 10240}
104 @!hash_size=8501; {should be prime}
105 @z
106
107 @x [1.8] l.195
108 @!stack_size=50; {number of simultaneous levels of macro expansion}
109 @!max_id_length=12; {long identifiers are chopped to this length, which must
110 not exceed |line_length|}
111 @!unambig_length=7; {identifiers must be unique if chopped to this length}
112 {note that 7 is more strict than \PASCAL's 8, but this can be varied}
113 @y
114 @!stack_size=100; {number of simultaneous levels of macro expansion}
115 @!max_id_length=50; {long identifiers are chopped to this length, which must
116 not exceed |line_length|}
117 @!def_unambig_length=32; {identifiers must be unique if chopped to this length}
118 @z
119
120 % [12] The text_char type is used as an array index into xord. The
121 % default type `char' may produce signed integers, which are bad array
122 % indices in C.
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 @x [2.12] l.302
125 @d text_char == char {the data type of characters in text files}
126 @y
127 @d text_char == ASCII_code {the data type of characters in text files}
128 @z
129
130 @x [2.17] l.483 - enable maximum character set
131 for i:=1 to @'37 do xchr[i]:=' ';
132 for i:=@'200 to @'377 do xchr[i]:=' ';
133 @y
134 for i:=1 to @'37 do xchr[i]:=chr(i);
135 for i:=@'200 to @'377 do xchr[i]:=chr(i);
136 @z
137
138 @x [3.20] l.509 - terminal output: use standard i/o
139 @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
140 @y
141 @d term_out==stdout
142 @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
143 @z
144
145 @x [3.20] l.516
146 @<Globals...@>=
147 @!term_out:text_file; {the terminal as an output file}
148 @y
149 @z
150
151 @x [3.21] l.519 - init terminal
152 @ Different systems have different ways of specifying that the output on a
153 certain file will appear on the user's terminal. Here is one way to do this
154 on the \PASCAL\ system that was used in \.{TANGLE}'s initial development:
155 @^system dependencies@>
156
157 @<Set init...@>=
158 rewrite(term_out,'TTY:'); {send |term_out| output to the terminal}
159 @y
160 @ Different systems have different ways of specifying that the output on a
161 certain file will appear on the user's terminal.
162 @^system dependencies@>
163
164 @<Set init...@>=
165 {Nothing need be done for C.}
166 @z
167
168 @x [3.22] l.532 - flush terminal buffer
169 @d update_terminal == break(term_out) {empty the terminal output buffer}
170 @y
171 @d update_terminal == fflush(term_out) {empty the terminal output buffer}
172 @z
173
174 @x [3.24] l.548 - open input files
175 begin reset(web_file); reset(change_file);
176 @y
177 begin web_file := kpse_open_file(web_name, kpse_web_format);
178 if chg_name then change_file := kpse_open_file(chg_name, kpse_web_format);
179 @z
180
181 @x [3.26] l.558 - Open output files (except for the pool file).
182 @ The following code opens |Pascal_file| and |pool|.
183 @y
184 @ The following code opens |Pascal_file|. Opening |pool| will be deferred
185 until section~\&{64}.
186 @z
187 @x [3.26] l.565
188 rewrite(Pascal_file); rewrite(pool);
189 @y
190 rewrite (Pascal_file, pascal_name);
191 @z
192
193 @x [3.28] l.591 - Fix f^.
194 begin buffer[limit]:=xord[f^]; get(f);
195 incr(limit);
196 if buffer[limit-1]<>" " then final_limit:=limit;
197 if limit=buf_size then
198 begin while not eoln(f) do get(f);
199 @y
200 begin buffer[limit]:=xord[getc(f)];
201 incr(limit);
202 if buffer[limit-1]<>" " then final_limit:=limit;
203 if limit=buf_size then
204 begin while not eoln(f) do vgetc(f);
205 @z
206
207 @x [4.31] l.640 - Fix typo as in weave.web.
208 procedure error; {prints '\..' and location of error message}
209 @y
210 procedure error; {prints `\..' and location of error message}
211 @z
212
213 @x [4.34] l.681 - Fix `jump_out'.
214 and jumps out of the program. This is the only non-local |goto| statement
215 in \.{TANGLE}. It is used when no recovery from a particular error has
216 been provided.
217
218 Some \PASCAL\ compilers do not implement non-local |goto| statements.
219 @^system dependencies@>
220 In such cases the code that appears at label |end_of_TANGLE| should be
221 copied into the |jump_out| procedure, followed by a call to a system procedure
222 that terminates the program.
223
224 @d fatal_error(#)==begin new_line; print(#); error; mark_fatal; jump_out;
225 end
226
227 @<Error handling...@>=
228 procedure jump_out;
229 begin goto end_of_TANGLE;
230 end;
231 @y
232 and jumps out of the program.
233
234 @d jump_out==uexit(1)
235 @d fatal_error(#)==begin new_line; write(stderr, #);
236 error; mark_fatal; jump_out;
237 end
238 @z
239
240 @x [5.38] l.742 - Provide for a larger `byte_mem' and `tok_mem'. Extra capacity:
241 @d ww=2 {we multiply the byte capacity by approximately this amount}
242 @d zz=3 {we multiply the token capacity by approximately this amount}
243 @y
244 @d ww=3 {we multiply the byte capacity by approximately this amount}
245 @d zz=5 {we multiply the token capacity by approximately this amount}
246 @z
247
248 @x [5.38] l.753 - Provide for larger than 16bit numeric macros.
249 @!equiv: array [0..max_names] of sixteen_bits; {info corresponding to names}
250 @y
251 @!equiv: array [0..max_names] of integer; {info corresponding to names}
252 @z
253
254 @x [5.47] l.862 - Provide for larger than 16bit numeric macros.
255 their |equiv| value contains the corresponding numeric value plus $2^{15}$.
256 @y
257 their |equiv| value contains the corresponding numeric value plus $2^{30}$.
258 @z
259
260 @x [5.47] l.868 - Add parametric2 macros (macros that use [] to delimit arguments).
261 \yskip\hang |parametric| identifiers have been defined to be parametric macros;
262 like simple identifiers, their |equiv| value points to the replacement text.
263 @y
264 \yskip\hang |parametric| and |parametric2| identifiers have been defined to
265 be parametric macros; like simple identifiers, their |equiv| value points to
266 the replacement text.
267 @z
268 @x [5.47] l.874
269 @d parametric=3 {parametric macros have |parametric| ilk}
270 @y
271 @d parametric=3 {parametric macros have |parametric| ilk}
272 @d parametric2=4 {second type of parametric macros have this |ilk|}
273 @z
274
275 @x [6.50] l.945 - unambig_length is a variable now
276 @!chopped_id:array [0..unambig_length] of ASCII_code; {chopped identifier}
277 @y
278 @!chopped_id:array [0..max_id_length] of ASCII_code; {chopped identifier}
279 @z
280
281 @x [6.53] l.960 - Add parametric2 macros (macros that use [] to delimit arguments).
282 |numeric|, |simple|, or |parametric|.
283 @y
284 |numeric|, |simple|, |parametric|, or |parametric2|.
285 @z
286
287 @x [6.53] l.971 - unambig_length is a variable.
288 @!s:0..unambig_length; {index into |chopped_id|}
289 @y
290 @!s:0..max_id_length; {index into |chopped_id|}
291 @z
292
293 @x [6.58] l.1030 - Case smashing options/strict checking.
294 begin if buffer[i]<>"_" then
295 begin if buffer[i]>="a" then chopped_id[s]:=buffer[i]-@'40
296 @y
297 begin if (buffer[i]<>"_") or (allow_underlines and not strict_mode) then
298 begin if (strict_mode or force_uppercase)
299 and (buffer[i]>="a") and (buffer[i]<="z") then
300 chopped_id[s]:=buffer[i]-@'40
301 else if (not strict_mode and force_lowercase)
302 and (buffer[i]>="A") and (buffer[i]<="Z") then
303 chopped_id[s]:=buffer[i]+@'40
304 @z
305
306 @x [6.61] l.1086
307 else @<Define and output a new string of the pool@>;
308 @y
309 else @<Define \(and output a new string of the pool@>;
310 @z
311
312 @x [6.63] l.1102 - Case smashing options/strict checking.
313 if c<>"_" then
314 begin if c>="a" then c:=c-@'40; {merge lowercase with uppercase}
315 @y
316 if c<>"_" or (allow_underlines and not strict_mode) then
317 begin if (strict_mode or force_uppercase)
318 and (c>="a") and (c<="z") then
319 c:=c-@'40
320 else if (not strict_mode and force_lowercase)
321 and (c>="A") and (c<="Z") then
322 c:=c+@'40;
323 @z
324
325 @x [6.64] l.1123 - Delayed pool file opening / larger numerics.
326 @<Define and output a new string...@>=
327 begin ilk[p]:=numeric; {strings are like numeric macros}
328 if l-double_chars=2 then {this string is for a single character}
329 equiv[p]:=buffer[id_first+1]+@'100000
330 else begin equiv[p]:=string_ptr+@'100000;
331 l:=l-double_chars-1;
332 @y
333 @<Define \(and output a new string...@>=
334 begin ilk[p]:=numeric; {strings are like numeric macros}
335 if l-double_chars=2 then {this string is for a single character}
336 equiv[p]:=buffer[id_first+1]+@'10000000000
337 else begin
338 {Avoid creating empty pool files.}
339 if string_ptr = 256 then rewritebin (pool, pool_name);
340 equiv[p]:=string_ptr+@'10000000000;
341 l:=l-double_chars-1;
342 @z
343
344 @x [9.85] l.1502 - Add parametric2 macros (macros that use [] to delimit arguments).
345 begin if ilk[cur_name]=parametric then
346 @y
347 begin if (ilk[cur_name]=parametric) or (ilk[cur_name]=parametric2) then
348 @z
349
350 @x [9.89] l.1586 - Larger numerics.
351 numeric: begin cur_val:=equiv[a]-@'100000; a:=number;
352 @y
353 numeric: begin cur_val:=equiv[a]-@'10000000000; a:=number;
354 @z
355
356 @x [9.89] l.1590 - Add parametric2 macros (macros that use [] to delimit arguments).
357 parametric: begin @<Put a parameter on the parameter stack,
358 @y
359 parametric,parametric2: begin @<Put a parameter on the parameter stack,
360 @z
361
362 @x [9.90] l.1607 - Add parametric2 macros (macros that use [] to delimit arguments).
363 if (stack_ptr=0)or(tok_mem[zo,cur_byte]<>"(") then
364 @y
365 if (stack_ptr=0)or((ilk[a]=parametric)and(tok_mem[zo,cur_byte]<>"("))
366 or((ilk[a]=parametric2)and(tok_mem[zo,cur_byte]<>"[")) then
367 @z
368
369 @x [9.93] l.1654 - Add parametric2 macros (macros that use [] to delimit arguments).
370 bal:=1; incr(cur_byte); {skip the opening `\.('}
371 @y
372 bal:=1; incr(cur_byte); {skip the opening `\.(' or `['}
373 @z
374 @x [9.93] l.1662
375 "(": incr(bal);
376 ")": begin decr(bal);
377 @y
378 "(": if ilk[a]=parametric then incr(bal);
379 ")": if ilk[a]=parametric then begin decr(bal);
380 if bal=0 then goto done;
381 end;
382 "[": if ilk[a]=parametric2 then incr(bal);
383 "]": if ilk[a]=parametric2 then begin decr(bal);
384 @z
385
386 @x [10.105] l.1937 - Accept DIV, div, MOD, and mod
387 (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
388 ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) ))or@|
389 @^uppercase@>
390 @y
391 (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
392 ((out_contrib[1]="d")and(out_contrib[2]="i")and(out_contrib[3]="v")) or@|
393 ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) or@|
394 ((out_contrib[1]="m")and(out_contrib[2]="o")and(out_contrib[3]="d")) ))or@|
395 @z
396
397 @x [10.110] l.2005 - lowercase ids
398 @^uppercase@>
399 if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
400 (out_buf[out_ptr-1]="V"))or @/
401 ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
402 (out_buf[out_ptr-1]="D")) then@/ goto bad_case
403 @y
404 if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
405 (out_buf[out_ptr-1]="V"))or @/
406 ((out_buf[out_ptr-3]="d")and(out_buf[out_ptr-2]="i")and
407 (out_buf[out_ptr-1]="v"))or @/
408 ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
409 (out_buf[out_ptr-1]="D"))or @/
410 ((out_buf[out_ptr-3]="m")and(out_buf[out_ptr-2]="o")and
411 (out_buf[out_ptr-1]="d")) then@/ goto bad_case
412 @z
413
414 @x [11.114] l.2077 - lowercase operators (`and', `or', etc.)
415 and_sign: begin out_contrib[1]:="A"; out_contrib[2]:="N"; out_contrib[3]:="D";
416 @^uppercase@>
417 send_out(ident,3);
418 end;
419 not_sign: begin out_contrib[1]:="N"; out_contrib[2]:="O"; out_contrib[3]:="T";
420 send_out(ident,3);
421 end;
422 set_element_sign: begin out_contrib[1]:="I"; out_contrib[2]:="N";
423 send_out(ident,2);
424 end;
425 or_sign: begin out_contrib[1]:="O"; out_contrib[2]:="R"; send_out(ident,2);
426 @y
427 and_sign: begin out_contrib[1]:="a"; out_contrib[2]:="n"; out_contrib[3]:="d";
428 send_out(ident,3);
429 end;
430 not_sign: begin out_contrib[1]:="n"; out_contrib[2]:="o"; out_contrib[3]:="t";
431 send_out(ident,3);
432 end;
433 set_element_sign: begin out_contrib[1]:="i"; out_contrib[2]:="n";
434 send_out(ident,2);
435 end;
436 or_sign: begin out_contrib[1]:="o"; out_contrib[2]:="r"; send_out(ident,2);
437 @z
438
439 @x [11.116] l.2120 - Case smashing options.
440 @ Single-character identifiers represent themselves, while longer ones
441 appear in |byte_mem|. All must be converted to uppercase,
442 with underlines removed. Extremely long identifiers must be chopped.
443
444 (Some \PASCAL\ compilers work with lowercase letters instead of
445 uppercase. If this module of \.{TANGLE} is changed, it's also necessary
446 to change from uppercase to lowercase in the modules that are
447 listed in the index under ``uppercase''.)
448 @^system dependencies@>
449 @^uppercase@>
450
451 @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
452 #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
453
454 @<Cases related to identifiers@>=
455 "A",up_to("Z"): begin out_contrib[1]:=cur_char; send_out(ident,1);
456 end;
457 "a",up_to("z"): begin out_contrib[1]:=cur_char-@'40; send_out(ident,1);
458 end;
459 identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
460 while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
461 begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
462 if out_contrib[k]>="a" then out_contrib[k]:=out_contrib[k]-@'40
463 else if out_contrib[k]="_" then decr(k);
464 end;
465 send_out(ident,k);
466 end;
467 @y
468 @ Single-character identifiers represent themselves, while longer ones
469 appear in |byte_mem|. All must be converted to lowercase,
470 with underlines removed. Extremely long identifiers must be chopped.
471 @^system dependencies@>
472
473 @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
474 #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
475
476 @<Cases related to identifiers@>=
477 "A",up_to("Z"): begin if force_lowercase then out_contrib[1]:=cur_char+@'40
478 else out_contrib[1]:=cur_char;
479 send_out(ident,1);
480 end;
481 "a",up_to("z"): begin if force_uppercase then out_contrib[1]:=cur_char-@'40
482 else out_contrib[1]:=cur_char;
483 send_out(ident,1);
484 end;
485 identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
486 while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
487 begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
488 if force_uppercase
489 and (out_contrib[k]>="a") and (out_contrib[k]<="z") then
490 out_contrib[k]:=out_contrib[k]-@'40
491 else if force_lowercase
492 and (out_contrib[k]>="A") and (out_contrib[k]<="Z") then
493 out_contrib[k]:=out_contrib[k]+@'40
494 else if not allow_underlines and (out_contrib[k]="_") then decr(k);
495 end;
496 send_out(ident,k);
497 end;
498 @z
499
500 @x [11.119] l.2185 - Calculate with decimal limit value INT_MAX/10.
501 if n>=@'1463146314 then err_print('! Constant too big')
502 @y
503 if n>=214748364 then err_print('! Constant too big')
504 @z
505
506 @x [14.157] l.2862 - Larger numerics.
507 if abs(accumulator)>=@'100000 then
508 @y
509 if abs(accumulator)>=@'10000000000 then
510 @z
511
512 @x [14.157] l.2866 - Larger numerics.
513 equiv[p]:=accumulator+@'100000; {name |p| now is defined to equal |accumulator|}
514 @y
515 equiv[p]:=accumulator+@'10000000000; {name |p| now is defined to equal |accumulator|}
516 @z
517
518 @x [14.158] l.2889 - Larger numerics.
519 add_in(equiv[q]-@'100000);
520 @y
521 add_in(equiv[q]-@'10000000000);
522 @z
523
524 @x [14.160] l.2915 - Avoid numeric overflow; see also section 119.
525 repeat val:=10*val+next_control-"0"; next_control:=get_next;
526 @y
527 repeat if val>=214748364 then err_print('! Constant too big')
528 @.Constant too big@>
529 else val:=10*val+next_control-"0";
530 next_control:=get_next;
531 @z
532 @x [14.161] l.2920 - Avoid numeric overflow; see also section 119.
533 repeat val:=8*val+next_control-"0"; next_control:=get_next;
534 @y
535 repeat if val>=@'2000000000 then err_print('! Constant too big')
536 @.Constant too big@>
537 else val:=8*val+next_control-"0";
538 next_control:=get_next;
539 @z
540 @x [14.162] l.2926 - Avoid numeric overflow; see also section 119.
541 val:=16*val+next_control-"0"; next_control:=get_next;
542 @y
543 if val>=@"8000000 then err_print('! Constant too big')
544 @.Constant too big@>
545 else val:=16*val+next_control-"0";
546 next_control:=get_next;
547 @z
548
549 @x [15.165] l.2964 - Add parametric2 macros (macros that use [] to delimit arguments).
550 "(": incr(bal);
551 ")": if bal=0 then err_print('! Extra )')
552 @.Extra )@>
553 else decr(bal);
554 "'": @<Copy a string from the buffer to |tok_mem|@>;
555 "#": if t=parametric then a:=param;
556 @y
557 "(": if t=parametric then incr(bal);
558 ")": if t=parametric then if bal=0 then err_print('! Extra )')
559 @.Extra )@>
560 else decr(bal);
561 "[": if t=parametric2 then incr(bal);
562 "]": if t=parametric2 then if bal=0 then err_print('! Extra ]')
563 @.Extra ]@>
564 else decr(bal);
565 "'": @<Copy a string from the buffer to |tok_mem|@>;
566 "#": if (t=parametric)or(t=parametric2) then a:=param;
567 @z
568
569 @x [15.166] l.2988 - Add parametric2 macros (macros that use [] to delimit arguments).
570 begin if bal=1 then err_print('! Missing )')
571 else err_print('! Missing ',bal:1,' )''s');
572 @.Missing n )@>
573 while bal>0 do
574 begin app_repl(")"); decr(bal);
575 end;
576 end
577 @y
578 if t=parametric then begin
579 if bal=1 then err_print('! Missing )')
580 else err_print('! Missing ',bal:1,' )''s');
581 @.Missing n )@>
582 while bal>0 do
583 begin app_repl(")"); decr(bal);
584 end;
585 end
586 else begin
587 if bal=1 then err_print('! Missing ]')
588 else err_print('! Missing ',bal:1,' ]''s');
589 @.Missing n ]@>
590 while bal>0 do
591 begin app_repl("]"); decr(bal);
592 end;
593 end
594 @z
595
596 @x [16.173] l.3095 - Reject strings as macro names.
597 if next_control<>identifier then
598 @y
599 if (next_control<>identifier) or (buffer[id_first]="""") then
600 @z
601
602 @x [16.173] l.3107 - Add parametric2 macros (macros that use [] to delimit arguments).
603 else @<If the next text is `\.{(\#)==}', call |define_macro|
604 and |goto continue|@>;
605 @y
606 else @<If the next text is `\.{(\#)==}' or `\.{[\#]==}', call |define_macro|
607 and |goto continue|@>;
608 @z
609
610 @x [16.174] l.3114 - Add parametric2 macros (macros that use [] to delimit arguments).
611 @ @<If the next text is `\.{(\#)==}'...@>=
612 if next_control="(" then
613 begin next_control:=get_next;
614 if next_control="#" then
615 begin next_control:=get_next;
616 if next_control=")" then
617 begin next_control:=get_next;
618 if next_control="=" then
619 begin err_print('! Use == for macros');
620 @.Use == for macros@>
621 next_control:=equivalence_sign;
622 end;
623 if next_control=equivalence_sign then
624 begin define_macro(parametric); goto continue;
625 end;
626 end;
627 end;
628 end;
629 @y
630 @ @<If the next text is `\.{(\#)==}'...@>=
631 if next_control="(" then
632 begin next_control:=get_next;
633 if next_control="#" then
634 begin next_control:=get_next;
635 if next_control=")" then
636 begin next_control:=get_next;
637 if next_control="=" then
638 begin err_print('! Use == for macros');
639 @.Use == for macros@>
640 next_control:=equivalence_sign;
641 end;
642 if next_control=equivalence_sign then
643 begin define_macro(parametric); goto continue;
644 end;
645 end;
646 end;
647 end
648 else if next_control="[" then
649 begin next_control:=get_next;
650 if next_control="#" then
651 begin next_control:=get_next;
652 if next_control="]" then
653 begin next_control:=get_next;
654 if next_control="=" then
655 begin err_print('! Use == for macros');
656 @.Use == for macros@>
657 next_control:=equivalence_sign;
658 end;
659 if next_control=equivalence_sign then
660 begin define_macro(parametric2); goto continue;
661 end;
662 end;
663 end;
664 end
665 @z
666
667 @x [17.179] l.3190 - make term_in = input
668 any error stop will set |debug_cycle| to zero.
669 @y
670 any error stop will set |debug_cycle| to zero.
671
672 @d term_in==stdin
673 @z
674
675 @x [17.179] l.3198
676 @!term_in:text_file; {the user's terminal as an input file}
677 @y
678 @z
679
680 @x [17.180] l.3206 - remove term_in reset
681 reset(term_in,'TTY:','/I'); {open |term_in| as the terminal, don't do a |get|}
682 @y
683 @z
684
685 @x [18.182] l.3248 - write newline just before exit; use value of |history|
686 print_ln(banner); {print a ``banner line''}
687 @y
688 print (banner); {print a ``banner line''}
689 print_ln (version_string);
690 @z
691
692 @x [18.182] l.3252 - Eliminate the |end_of_TANGLE| label.
693 end_of_TANGLE:
694 @y
695 @z
696
697 @x [18.182] l.3256
698 @<Print the job |history|@>;
699 @y
700 @<Print the job |history|@>;
701 new_line;
702 if (history <> spotless) and (history <> harmless_message)
703 then uexit (1)
704 else uexit (0);
705 @z
706
707 @x [19.188] l.3308 - System-dependent changes.
708 This module should be replaced, if necessary, by changes to the program
709 that are necessary to make \.{TANGLE} work at a particular installation.
710 It is usually best to design your change file so that all changes to
711 previous modules preserve the module numbering; then everybody's version
712 will be consistent with the printed program. More extensive changes,
713 which introduce new modules, can be inserted here; then only the index
714 itself will get a new module number.
715 @^system dependencies@>
716 @y
717 Parse a Unix-style command line.
718
719 @d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0)
720
721 @<Define \(|parse_arguments|@> =
722 procedure parse_arguments;
723 const n_options = 9; {Pascal won't count array lengths for us.}
724 var @!long_options: array[0..n_options] of getopt_struct;
725 @!getopt_return_val: integer;
726 @!option_index: c_int_type;
727 @!current_option: 0..n_options;
728 @!len: integer;
729 begin
730 @<Define the option table@>;
731 unambig_length := def_unambig_length;
732 repeat
733 getopt_return_val := getopt_long_only (argc, argv, '', long_options,
734 address_of (option_index));
735 if getopt_return_val = -1 then begin
736 do_nothing; {End of arguments; we exit the loop below.}
737
738 end else if getopt_return_val = "?" then begin
739 usage (my_name);
740
741 end else if argument_is ('help') then begin
742 usage_help (TANGLE_HELP, nil);
743
744 end else if argument_is ('version') then begin
745 print_version_and_exit (banner, nil, 'D.E. Knuth', nil);
746
747 end else if argument_is ('mixedcase') then begin
748 force_uppercase := false;
749 force_lowercase := false;
750
751 end else if argument_is ('uppercase') then begin
752 force_uppercase := true;
753 force_lowercase := false;
754
755 end else if argument_is ('lowercase') then begin
756 force_uppercase := false;
757 force_lowercase := true;
758
759 end else if argument_is ('underlines') then begin
760 allow_underlines := true;
761
762 end else if argument_is ('strict') then begin
763 strict_mode := true;
764
765 end else if argument_is ('loose') then begin
766 strict_mode := false;
767
768 end else if argument_is ('length') then begin
769 len := atoi(optarg);
770 if (len<=0) or (len>max_id_length) then len := max_id_length;
771 unambig_length := len;
772
773 end; {Else it was a flag; |getopt| has already done the assignment.}
774 until getopt_return_val = -1;
775
776 @<Handle file name arguments@>@;
777 end;
778
779 @ Now |optind| is the index of first non-option on the command line.
780
781 @<Handle file name...@>=
782 if (optind + 1 > argc) or (optind + 3 < argc) then begin
783 write_ln (stderr, my_name, ': Need one to three file arguments.');
784 usage (my_name);
785 end;
786
787 {Supply |".web"| and |".ch"| extensions if necessary.}
788 web_name := extend_filename (cmdline (optind), 'web');
789 if optind + 2 <= argc then begin
790 {|'-'| is shortcut for an empty changefile.}
791 if strcmp(char_to_string('-'), cmdline (optind + 1)) <> 0 then
792 chg_name := extend_filename (cmdline (optind + 1), 'ch');
793 end;
794
795 if optind + 3 = argc then begin
796 {User has provided an explicit Pascal output file, possibly with path.}
797 pascal_name := extend_filename (cmdline (optind + 2), char_to_string('p'));
798 pool_name := extend_filename (remove_suffix (pascal_name), 'pool');
799 end
800 else begin
801 {Change |".web"| to |".p"| and |".pool"| and use the current directory.}
802 pascal_name := basename_change_suffix (web_name, '.web', '.p');
803 pool_name := basename_change_suffix (web_name, '.web', '.pool');
804 end;
805
806 @ Here are the options we allow. The first is one of the standard GNU options.
807 @.-help@>
808
809 @<Define the option...@> =
810 current_option := 0;
811 long_options[current_option].name := 'help';
812 long_options[current_option].has_arg := 0;
813 long_options[current_option].flag := 0;
814 long_options[current_option].val := 0;
815 incr (current_option);
816
817 @ Another of the standard options.
818 @.-version@>
819
820 @<Define the option...@> =
821 long_options[current_option].name := 'version';
822 long_options[current_option].has_arg := 0;
823 long_options[current_option].flag := 0;
824 long_options[current_option].val := 0;
825 incr (current_option);
826
827 @ Use all mixed case.
828 @.-mixedcase@>
829
830 @<Define the option...@> =
831 long_options[current_option].name := 'mixedcase';
832 long_options[current_option].has_arg := 0;
833 long_options[current_option].flag := 0;
834 long_options[current_option].val := 0;
835 incr (current_option);
836
837 @ Use all uppercase.
838 @.-uppercase@>
839
840 @<Define the option...@> =
841 long_options[current_option].name := 'uppercase';
842 long_options[current_option].has_arg := 0;
843 long_options[current_option].flag := 0;
844 long_options[current_option].val := 0;
845 incr (current_option);
846
847 @ Use all lowercase.
848 @.-lowercase@>
849
850 @<Define the option...@> =
851 long_options[current_option].name := 'lowercase';
852 long_options[current_option].has_arg := 0;
853 long_options[current_option].flag := 0;
854 long_options[current_option].val := 0;
855 incr (current_option);
856
857 @ Allow underlines.
858 @.-underlines@>
859
860 @<Define the option...@> =
861 long_options[current_option].name := 'underlines';
862 long_options[current_option].has_arg := 0;
863 long_options[current_option].flag := 0;
864 long_options[current_option].val := 0;
865 incr (current_option);
866
867 @ Strict comparisons.
868 @.-strict@>
869
870 @<Define the option...@> =
871 long_options[current_option].name := 'strict';
872 long_options[current_option].has_arg := 0;
873 long_options[current_option].flag := 0;
874 long_options[current_option].val := 0;
875 incr (current_option);
876
877 @ Loose comparisons.
878 @.-loose@>
879
880 @<Define the option...@> =
881 long_options[current_option].name := 'loose';
882 long_options[current_option].has_arg := 0;
883 long_options[current_option].flag := 0;
884 long_options[current_option].val := 0;
885 incr (current_option);
886
887 @ Loose comparisons.
888 @.-length@>
889
890 @<Define the option...@> =
891 long_options[current_option].name := 'length';
892 long_options[current_option].has_arg := 1;
893 long_options[current_option].flag := 0;
894 long_options[current_option].val := 0;
895 incr (current_option);
896
897 @ An element with all zeros always ends the list.
898
899 @<Define the option...@> =
900 long_options[current_option].name := 0;
901 long_options[current_option].has_arg := 0;
902 long_options[current_option].flag := 0;
903 long_options[current_option].val := 0;
904
905 @ Global filenames.
906
907 @<Globals...@>=
908 @!web_name,@!chg_name,@!pascal_name,@!pool_name:const_c_string;
909 @!force_uppercase,@!force_lowercase,@!allow_underlines,@!strict_mode:boolean;
910 @!unambig_length:0..max_id_length;
911 @z