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