home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / tex / texsrc1 / Src / web / tangle / ch next >
Encoding:
Text File  |  1993-02-21  |  23.2 KB  |  669 lines

  1. % tangle.ch for C compilation with web2c.
  2. % The original version of this file was created by Howard Trickey and
  3. % Pavel Curtis.
  4. %
  5. % History:
  6. % (more recent changes in ../ChangeLog.W2C)
  7. %  10/9/82 (HT) Original version
  8. %  11/29   (HT) New version, with conversion to lowercase handled properly
  9. %               Also, new control sequence:
  10. %                       @=...text...@>   Put ...text... verbatim on a line
  11. %                                        by itself in the Pascal output.
  12. %                                        (argument must fit on one line)
  13. %               This control sequence facilitates putting #include "gcons.h"
  14. %               (for example) in files meant for the pc compiler.
  15. %               Also, changed command line usage, so that the absence of a
  16. %               change file implies no change file, rather than one with the
  17. %               same name as the web file, with .ch at the end.
  18. %  1/15/83 (HT) Changed to work with version 1.2, which incorporates the
  19. %               above change (though unbundling the output line breaking),
  20. %               so mainly had to remove stuff.
  21. %  2/17    (HT) Fixed bug that caused 0-9 in identifiers to be converted to
  22. %               Q-Y on output.
  23. %  3/18    (HT) Brought up to work with Version 1.5.  Added -r command line
  24. %               flag to cause a .rpl file to be written with all the lines
  25. %               of the .web file that were replaced because of the .ch file
  26. %               (useful for comparing with previous .rpl files, to see if a
  27. %               change file will still work with a new version of a .web file)
  28. %               Also, made it write a newline just before exit.
  29. %  4/12    (PC) Merged with Pavel's version, including adding a call to exit()
  30. %               at the end depending upon the value of history.
  31. %  4/16    (PC) Brought up to date with version 1.5 released April, 1983.
  32. %  6/28   (HWT) Brought up to date with version 1.7 released June, 1983.
  33. %               With new change file format, the -r option is now unnecessary.
  34. %  7/17   (HWT) Brought up to date with version 2.0 released July, 1983.
  35. % 12/18/83 (ETM) Brought up to date with version 2.5 released November, 1983.
  36. % 11/07/84 (ETM) Brought up to date with version 2.6.
  37. % 12/15/85 (ETM) Brought up to date with version 2.8.
  38. % 03/07/88 (ETM) Converted for use with WEB2C
  39. % 01/02/89 (PAM) Cosmetic upgrade to version 2.9
  40. % 11/30/89 (KB)  Version 4.
  41. % (more recent changes in ../ChangeLog.W2C and ./ChangeLog)
  42.  
  43.  
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. % [0] WEAVE: print only changes
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. @x
  48. \pageno=\contentspagenumber \advance\pageno by 1
  49. @y
  50. \pageno=\contentspagenumber \advance\pageno by 1
  51. \let\maybe=\iffalse
  52. \def\title{TANGLE changes for C}
  53. @z
  54.  
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. % [1] Change banner message
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. @x
  59. @d banner=='This is TANGLE, Version 4.3'
  60. @y
  61. @d banner=='This is TANGLE, Version 4.3' {more is printed later}
  62. @z
  63.  
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65. % [2] add input and output, remove other files, add ref to scan_args,
  66. % and #include external definition for exit().
  67. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68. @x
  69. @d end_of_TANGLE = 9999 {go here to wrap it up}
  70.  
  71. @p @t\4@>@<Compiler directives@>@/
  72. program TANGLE(@!web_file,@!change_file,@!Pascal_file,@!pool);
  73. label end_of_TANGLE; {go here to finish}
  74. const @<Constants in the outer block@>@/
  75. type @<Types in the outer block@>@/
  76. var @<Globals in the outer block@>@/
  77. @<Error handling procedures@>@/
  78. @y
  79. @d end_of_TANGLE = 9999 {go here to wrap it up}
  80.  
  81. @p program TANGLE;
  82. label end_of_TANGLE; {go here to finish}
  83. const @<Constants in the outer block@>@/
  84. type @<Types in the outer block@>@/
  85. var @<Globals in the outer block@>@/
  86. @<Error handling procedures@>@/
  87. @<Declaration of |scan_args|@>@/
  88. @z
  89.  
  90. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91. % [4] compiler options
  92. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  93. @x
  94. @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
  95. @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
  96. @y
  97. @=(*$C-*)@> {no range check}
  98. @!debug @=(*$C+*)@>@+ gubed {but turn everything on when debugging}
  99. @z
  100.  
  101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  102. % [8] Constants: increase id lengths
  103. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  104. @x
  105. @!stack_size=50; {number of simultaneous levels of macro expansion}
  106. @!max_id_length=12; {long identifiers are chopped to this length, which must
  107.   not exceed |line_length|}
  108. @!unambig_length=7; {identifiers must be unique if chopped to this length}
  109.   {note that 7 is more strict than \PASCAL's 8, but this can be varied}
  110. @y
  111. @!stack_size=100; {number of simultaneous levels of macro expansion}
  112. @!max_id_length=50; {long identifiers are chopped to this length, which must
  113.   not exceed |line_length|}
  114. @!unambig_length=20; {identifiers must be unique if chopped to this length}
  115. @z
  116.  
  117. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  118. % [??] The text_char type is used as an array index into xord.  The
  119. % default type `char' produces signed integers, which are bad array
  120. % indices in C.
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  122. @x
  123. @d text_char == char {the data type of characters in text files}
  124. @y
  125. @d text_char == ASCII_code {the data type of characters in text files}
  126. @z
  127.  
  128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  129. % [17] enable maximum character set
  130. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131. @x
  132. for i:=1 to @'37 do xchr[i]:=' ';
  133. for i:=@'200 to @'377 do xchr[i]:=' ';
  134. @y
  135. for i:=1 to @'37 do xchr[i]:=chr(i);
  136. for i:=@'200 to @'377 do xchr[i]:=chr(i);
  137. @z
  138.  
  139. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  140. % [20] terminal output: use standard i/o
  141. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  142. @x
  143. @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
  144. @y
  145. @d term_out==stdout
  146. @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
  147. @z
  148.  
  149. @x
  150. @<Globals...@>=
  151. @!term_out:text_file; {the terminal as an output file}
  152. @y
  153. @z
  154.  
  155. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  156. % [21] init terminal
  157. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  158. @x
  159. @ Different systems have different ways of specifying that the output on a
  160. certain file will appear on the user's terminal. Here is one way to do this
  161. on the \PASCAL\ system that was used in \.{TANGLE}'s initial development:
  162. @^system dependencies@>
  163.  
  164. @<Set init...@>=
  165. rewrite(term_out,'TTY:'); {send |term_out| output to the terminal}
  166. @y
  167. @ Different systems have different ways of specifying that the output on a
  168. certain file will appear on the user's terminal.
  169. @^system dependencies@>
  170.  
  171. @<Set init...@>=
  172.  {Nothing need be done for C.}
  173. @z
  174.  
  175. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  176. % [22] flush terminal buffer
  177. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  178. @x
  179. @d update_terminal == break(term_out) {empty the terminal output buffer}
  180. @y
  181. @d update_terminal == flush(term_out) {empty the terminal output buffer}
  182. @z
  183.  
  184. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  185. % [24] open input files
  186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  187. @x
  188. @ The following code opens the input files.  Since these files were listed
  189. in the program header, we assume that the \PASCAL\ runtime system has
  190. already checked that suitable file names have been given; therefore no
  191. additional error checking needs to be done.
  192. @^system dependencies@>
  193.  
  194. @p procedure open_input; {prepare to read |web_file| and |change_file|}
  195. begin reset(web_file); reset(change_file);
  196. end;
  197. @y
  198. @ The following code opens the input files.
  199. This happens after the |initialize| procedure has executed.
  200. That will have called the |scan_args| procedure to set up the global
  201. variables |web_name| and |chg_name| to the appropriate file
  202. names.
  203. These globals, and the |scan_args| procedure will be defined at the end
  204. where they won't disturb the module numbering.
  205. @^system dependencies@>
  206.  
  207. @p procedure open_input; {prepare to read |web_file| and |change_file|}
  208. begin
  209. reset(web_file,web_name); reset(change_file,chg_name);
  210. end;
  211. @z
  212.  
  213. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  214. % [26] Open output files (except for the pool file).
  215. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  216. @x
  217. @ The following code opens |Pascal_file| and |pool|.
  218. Since these files were listed in the program header, we assume that the
  219. \PASCAL\ runtime system has checked that suitable external file names have
  220. been given.
  221. @^system dependencies@>
  222.  
  223. @<Set init...@>=
  224. rewrite(Pascal_file); rewrite(pool);
  225. @y
  226. @ The following code opens |Pascal_file| and |pool|.
  227. Use the |scan_args| procedure to fill the global file names,
  228. according to the names given on the command line.
  229. @^system dependencies@>
  230.  
  231. @<Set init...@>=
  232. scan_args;
  233. rewrite(Pascal_file,pascal_file_name);
  234. @z
  235.  
  236. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  237. % [28] Fix f^.
  238. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  239. @x
  240.     begin buffer[limit]:=xord[f^]; get(f);
  241.     incr(limit);
  242.     if buffer[limit-1]<>" " then final_limit:=limit;
  243.     if limit=buf_size then
  244.       begin while not eoln(f) do get(f);
  245. @y
  246.     begin buffer[limit]:=xord[getc(f)];
  247.     incr(limit);
  248.     if buffer[limit-1]<>" " then final_limit:=limit;
  249.     if limit=buf_size then
  250.       begin while not eoln(f) do vgetc(f);
  251. @z
  252.  
  253. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  254. % [??] Fix `jump_out'.
  255. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  256. @x
  257. @d fatal_error(#)==begin new_line; print(#); error; mark_fatal; jump_out;
  258.   end
  259.  
  260. @<Error handling...@>=
  261. procedure jump_out;
  262. begin goto end_of_TANGLE;
  263. end;
  264. @y
  265. @d jump_out==uexit(1)
  266. @d fatal_error(#)==begin new_line; print(#); error; mark_fatal; uexit(1);
  267.   end
  268. @z
  269.  
  270. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  271. % [38] Provide for a larger `byte_mem' and `tok_mem'.
  272. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  273. @x Extra capacity:
  274. @d ww=2 {we multiply the byte capacity by approximately this amount}
  275. @d zz=3 {we multiply the token capacity by approximately this amount}
  276. @y
  277. @d ww=3 {we multiply the byte capacity by approximately this amount}
  278. @d zz=4 {we multiply the token capacity by approximately this amount}
  279. @z
  280.  
  281. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  282. % [63] Remove conversion to uppercase
  283. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  284. @x
  285.     begin if c>="a" then c:=c-@'40; {merge lowercase with uppercase}
  286. @y
  287.     begin 
  288. @z
  289.  
  290. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  291. % [64] Delayed pool file opening.
  292. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  293. @x
  294. @<Define and output a new string...@>=
  295. begin ilk[p]:=numeric; {strings are like numeric macros}
  296. if l-double_chars=2 then {this string is for a single character}
  297.   equiv[p]:=buffer[id_first+1]+@'100000
  298. else  begin equiv[p]:=string_ptr+@'100000;
  299.   l:=l-double_chars-1;
  300. @y
  301. @<Define and output a new string...@>=
  302. begin ilk[p]:=numeric; {strings are like numeric macros}
  303. if l-double_chars=2 then {this string is for a single character}
  304.   equiv[p]:=buffer[id_first+1]+@'100000
  305. else  begin
  306.   if string_ptr = 256 then  rewrite(pool,pool_file_name);
  307.   equiv[p]:=string_ptr+@'100000;
  308.   l:=l-double_chars-1;
  309. @z
  310.  
  311. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  312. % [105] Accept DIV, div, MOD, and mod
  313. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  314. @x
  315.  (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
  316.  ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) ))or@|
  317. @^uppercase@>
  318. @y
  319.   (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
  320.   ((out_contrib[1]="d")and(out_contrib[2]="i")and(out_contrib[3]="v")) or@|
  321.   ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) or@|
  322.   ((out_contrib[1]="m")and(out_contrib[2]="o")and(out_contrib[3]="d")) ))or@|
  323. @z
  324.  
  325. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  326. % [110] lowercase ids
  327. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  328. @x
  329. @^uppercase@>
  330.   if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
  331.     (out_buf[out_ptr-1]="V"))or @/
  332.      ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
  333.     (out_buf[out_ptr-1]="D")) then@/ goto bad_case
  334. @y
  335.   if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
  336.     (out_buf[out_ptr-1]="V"))or @/
  337.      ((out_buf[out_ptr-3]="d")and(out_buf[out_ptr-2]="i")and
  338.     (out_buf[out_ptr-1]="v"))or @/
  339.      ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
  340.     (out_buf[out_ptr-1]="D"))or @/
  341.      ((out_buf[out_ptr-3]="m")and(out_buf[out_ptr-2]="o")and
  342.     (out_buf[out_ptr-1]="d")) then@/ goto bad_case
  343. @z
  344.  
  345. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  346. % [114] lowercase operators (`and', `or', etc.)
  347. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  348. @x
  349. and_sign: begin out_contrib[1]:="A"; out_contrib[2]:="N"; out_contrib[3]:="D";
  350. @^uppercase@>
  351.   send_out(ident,3);
  352.   end;
  353. not_sign: begin out_contrib[1]:="N"; out_contrib[2]:="O"; out_contrib[3]:="T";
  354.   send_out(ident,3);
  355.   end;
  356. set_element_sign: begin out_contrib[1]:="I"; out_contrib[2]:="N";
  357.   send_out(ident,2);
  358.   end;
  359. or_sign: begin out_contrib[1]:="O"; out_contrib[2]:="R"; send_out(ident,2);
  360. @y
  361. and_sign: begin out_contrib[1]:="a"; out_contrib[2]:="n"; out_contrib[3]:="d";
  362.   send_out(ident,3);
  363.   end;
  364. not_sign: begin out_contrib[1]:="n"; out_contrib[2]:="o"; out_contrib[3]:="t";
  365.   send_out(ident,3);
  366.   end;
  367. set_element_sign: begin out_contrib[1]:="i"; out_contrib[2]:="n";
  368.   send_out(ident,2);
  369.   end;
  370. or_sign: begin out_contrib[1]:="o"; out_contrib[2]:="r"; send_out(ident,2);
  371. @z
  372.  
  373. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  374. % [116] Remove conversion to uppercase
  375. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  376. @x
  377. @ Single-character identifiers represent themselves, while longer ones
  378. appear in |byte_mem|. All must be converted to uppercase,
  379. with underlines removed. Extremely long identifiers must be chopped.
  380.  
  381. (Some \PASCAL\ compilers work with lowercase letters instead of
  382. uppercase. If this module of \.{TANGLE} is changed, it's also necessary
  383. to change from uppercase to lowercase in the modules that are
  384. listed in the index under ``uppercase''.)
  385. @^system dependencies@>
  386. @^uppercase@>
  387.  
  388. @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
  389.   #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
  390.  
  391. @<Cases related to identifiers@>=
  392. "A",up_to("Z"): begin out_contrib[1]:=cur_char; send_out(ident,1);
  393.   end;
  394. "a",up_to("z"): begin out_contrib[1]:=cur_char-@'40; send_out(ident,1);
  395.   end;
  396. identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
  397.   while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
  398.     begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
  399.     if out_contrib[k]>="a" then out_contrib[k]:=out_contrib[k]-@'40
  400.     else if out_contrib[k]="_" then decr(k);
  401.     end;
  402.   send_out(ident,k);
  403.   end;
  404. @y
  405. @ Single-character identifiers represent themselves, while longer ones
  406. appear in |byte_mem|. All must be converted to lowercase,
  407. with underlines removed. Extremely long identifiers must be chopped.
  408. @^system dependencies@>
  409.  
  410. @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
  411.   #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
  412.  
  413. @<Cases related to identifiers@>=
  414. "A",up_to("Z"),
  415. "a",up_to("z"): begin out_contrib[1]:=cur_char; send_out(ident,1);
  416.   end;
  417. identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
  418.   while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
  419.     begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
  420.     if out_contrib[k]="_" then decr(k);
  421.     end;
  422.   send_out(ident,k);
  423.   end;
  424. @z
  425.  
  426. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  427. % [??] Fix casting bug
  428. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  429. @x
  430. @d add_in(#)==begin accumulator:=accumulator+next_sign*(#); next_sign:=+1;
  431.   end
  432. @y
  433. @d add_in(#)==begin accumulator:=accumulator+next_sign*toint(#); next_sign:=+1;
  434.   end
  435. @z
  436.  
  437. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  438. % [179] make term_in = input
  439. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  440. @x
  441. any error stop will set |debug_cycle| to zero.
  442. @y
  443. any error stop will set |debug_cycle| to zero.
  444.  
  445. @d term_in==stdin
  446. @z
  447.  
  448. @x
  449. @!term_in:text_file; {the user's terminal as an input file}
  450. @y
  451.  
  452. @z
  453.  
  454. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  455. % [180] remove term_in reset
  456. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  457. @x
  458. reset(term_in,'TTY:','/I'); {open |term_in| as the terminal, don't do a |get|}
  459. @y
  460.  
  461. @z
  462.  
  463. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  464. % [182] write newline just before exit; use value of |history|
  465. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  466. @x
  467. print_ln(banner); {print a ``banner line''}
  468. @y
  469. print (banner); {print a ``banner line''}
  470. print_ln (version_string);
  471. @z
  472.  
  473. @x
  474. @<Print the job |history|@>;
  475. @y
  476. @<Print the job |history|@>;
  477. new_line;
  478. if (history <> spotless) and (history <> harmless_message)
  479. then uexit (1)
  480. else uexit (0);
  481. @z
  482.  
  483. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  484. % [188] system dependent changes--the |scan_args| procedure.
  485. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  486. @x
  487. This module should be replaced, if necessary, by changes to the program
  488. that are necessary to make \.{TANGLE} work at a particular installation.
  489. It is usually best to design your change file so that all changes to
  490. previous modules preserve the module numbering; then everybody's version
  491. will be consistent with the printed program. More extensive changes,
  492. which introduce new modules, can be inserted here; then only the index
  493. itself will get a new module number.
  494. @^system dependencies@>
  495. @y
  496. This module should be replaced, if necessary, by changes to the program
  497. that are necessary to make \.{TANGLE} work at a particular installation.
  498. It is usually best to design your change file so that all changes to
  499. previous modules preserve the module numbering; then everybody's version
  500. will be consistent with the printed program. More extensive changes,
  501. which introduce new modules, can be inserted here; then only the index
  502. itself will get a new module number.
  503. @^system dependencies@>
  504.  
  505. @ The user calls \.{TANGLE} with arguments on the command line.  These
  506. are either file names or flags (beginning with `\.-').  The following
  507. globals are for communicating the user's desires to the rest of the
  508. program. The various filename variables contain strings with the full
  509. names of those files, as {\mc UNIX} knows them.
  510.  
  511. There are no flags that affect \.{TANGLE} at the moment.
  512.  
  513. @d max_file_name_length==PATH_MAX
  514.  
  515. @<Globals...@>=
  516. @!web_name,@!chg_name,@!pascal_file_name,@!pool_file_name:
  517.         array[1..max_file_name_length] of char;
  518.  
  519. @ The |scan_args| procedure looks at the command line arguments and sets
  520. the |file_name| variables accordingly.  At least one file name must be
  521. present: the \.{WEB} file.  It may have an extension, or it may omit it
  522. to get |'.web'| added.  The \PASCAL\ output file name is formed by
  523. replacing the \.{WEB} file name extension by |'.p'|.  Similarly, the
  524. pool file name is formed using a |'.pool'| extension.
  525.  
  526. If there is another file name present among the arguments, it is the
  527. change file, again either with an extension or without one to get
  528. |'.ch'| An omitted change file argument means that |'/dev/null'| should
  529. be used, when no changes are desired.
  530.  
  531. @<Declaration of |scan_args|@>=
  532. procedure scan_args;
  533.   var dot_pos, slash_pos, i, a: integer; {indices}
  534.   c: char;
  535.   @!fname: array[1..max_file_name_length] of char; {temporary argument holder}
  536.   @!found_web,@!found_change: boolean; {|true| when those file names have
  537.                                         been seen}
  538. begin
  539.   found_web := false;
  540.   found_change := false;
  541.  
  542.   for a := 1 to argc - 1
  543.   do begin
  544.     argv(a,fname); {put argument number |a| into |fname|}
  545.     if fname[1] <> '-'
  546.     then begin
  547.       if not found_web
  548.       then @<Get |web_name|, |pascal_file_name|,
  549.              and |pool_file_name| variables from |fname|@>
  550.       else if not found_change
  551.       then @<Get |chg_name| from |fname|@>
  552.       else  @<Print usage error message and quit@>;
  553.     end else
  554.       @<Handle flag argument in |fname|@>;
  555.   end;
  556.     
  557.   if not found_web then @<Print usage error message and quit@>;
  558.   if not found_change then @<Set up null change file@>;
  559. end;
  560.  
  561. @ Use all of |fname| for the |web_name| if there is a |'.'| in it,
  562. otherwise add |'.web'|.  The other file names come from adding things
  563. after the dot.  The |argv| procedure will not put more than
  564. |max_file_name_length-5| characters into |fname|, and this leaves enough
  565. room in the |file_name| variables to add the extensions.
  566.  
  567. The end of a file name is marked with a |' '|, the convention assumed by 
  568. the |reset| and |rewrite| procedures.
  569.  
  570. @<Get |web_name|...@>=
  571. begin
  572.   dot_pos := -1;
  573.   slash_pos := -1;
  574.   i := 1;
  575.   while (fname[i] <> ' ') and (i <= max_file_name_length - 5)
  576.   do begin
  577.     web_name[i] := fname[i];
  578.     if fname[i] = '.' then dot_pos := i;
  579.     if fname[i] = '/' then slash_pos := i;
  580.     incr (i);
  581.   end;
  582.   web_name[i] := ' ';
  583.   
  584.   if (dot_pos = -1) or (dot_pos < slash_pos)
  585.   then begin
  586.     dot_pos := i;
  587.     web_name[dot_pos] :=   '.';
  588.     web_name[dot_pos+1] := 'w';
  589.     web_name[dot_pos+2] := 'e';
  590.     web_name[dot_pos+3] := 'b';
  591.     web_name[dot_pos+4] := ' ';
  592.   end;
  593.  
  594.   for i := 1 to dot_pos
  595.   do begin
  596.     c := web_name[i];
  597.     pascal_file_name[i] := c;
  598.     pool_file_name[i] := c;
  599.   end;
  600.  
  601.   pascal_file_name[dot_pos+1] := 'p';
  602.   pascal_file_name[dot_pos+2] := ' ';
  603.  
  604.   pool_file_name[dot_pos+1] := 'p';
  605.   pool_file_name[dot_pos+2] := 'o';
  606.   pool_file_name[dot_pos+3] := 'o';
  607.   pool_file_name[dot_pos+4] := 'l';
  608.   pool_file_name[dot_pos+5] := ' ';
  609.  
  610.   found_web := true;
  611. end
  612.  
  613. @ @<Get |chg_name|...@>=
  614. begin
  615.   dot_pos := -1;
  616.   slash_pos := -1;
  617.   i := 1;
  618.   while (fname[i] <> ' ') and (i <= max_file_name_length - 5)
  619.   do begin
  620.     chg_name[i] := fname[i];
  621.     if fname[i] = '.' then dot_pos := i;
  622.     if fname[i] = '/' then slash_pos := i;
  623.     incr (i);
  624.   end;
  625.   chg_name[i] := ' ';
  626.  
  627.   if (dot_pos = -1) or (dot_pos < slash_pos)
  628.   then begin
  629.     dot_pos := i;
  630.     chg_name[dot_pos]   := '.';
  631.     chg_name[dot_pos+1] := 'c';
  632.     chg_name[dot_pos+2] := 'h';
  633.     chg_name[dot_pos+3] := ' ';
  634.   end;
  635.  
  636.   found_change := true;
  637. end
  638.  
  639. @ @<Set up null...@>=
  640. begin
  641.         chg_name[1]:='/';
  642.         chg_name[2]:='d';
  643.         chg_name[3]:='e';
  644.         chg_name[4]:='v';
  645.         chg_name[5]:='/';
  646.         chg_name[6]:='n';
  647.         chg_name[7]:='u';
  648.         chg_name[8]:='l';
  649.         chg_name[9]:='l';
  650.         chg_name[10]:=' ';
  651. end
  652.  
  653. @ There are no flags currently used by \.{TANGLE}, but this module can be
  654. used as a hook to introduce flags.
  655.  
  656. @<Handle flag...@>=
  657. begin
  658.   @<Print usage error message and quit@>;
  659. end
  660.  
  661. @ @<Print usage error message and quit@>=
  662. begin
  663.   print_ln ('Usage: tangle webfile[.web] [changefile[.ch]].');
  664.   uexit (1);
  665. end
  666. @z
  667.