perm filename TEX.CH[TEX,SYS] blob sn#874636 filedate 1989-06-22 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00041 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00004 00002	@x Tell WEAVE to print only the changes:
C00005 00003	@x WAITS's banner:
C00006 00004	@x Switches for debugging and statistics:
C00009 00005	@x The INITEX switch:
C00012 00006	@x Compiler directives:
C00013 00007	@x Compile-time constants:
C00021 00008	@x TANGLE-time constants:
C00024 00009	@x System-dependent character set changes:
C00026 00010	@x Opening files:
C00030 00011	@x New input_ln:
C00037 00012	@x Terminal I/O:
C00040 00013	@x Special terminal controls:
C00041 00014	@x Initializing the terminal:
C00047 00015	@x Making special characters printable:
C00048 00016	@x Terminal input:
C00049 00017	@x The `E' option:
C00053 00018	@x Changes for 36-bit machines:
C00055 00019	@x Eliminating addition/subtraction of zero:
C00057 00020	@x Kludge to get around Pascal bug (with large array lower bounds)
C00058 00021	@x Special form_feed initialization:
C00060 00022	@x Date and time:
C00062 00023	@x Page number maintenance:
C00064 00024	@x Printing the page number:
C00065 00025	@x More page number maintenance:
C00068 00026	@x More page number printing:
C00071 00027	@x Pausing on input:
C00075 00028	@x Reading the first line of a file:
C00077 00029	@x Still more page number maintenance:
C00079 00030	@x Parsing file names:
C00086 00031	@x Printing file names:
C00087 00032	@x Converting file names to Pascal strings:
C00090 00033	@x Parsing file names in the buffer:
C00097 00034	@x The real file names:
C00101 00035	@x Line editor gets misspelled file name:
C00103 00036	@x Reading the first line of a file, again:
C00106 00037	@x The DVI output buffer:
C00110 00038	@x "r DVIspool":
C00113 00039	@x Page numbers on over/underfull box messages:
C00115 00040	@x The endgame:
C00118 00041	@x Final system-dependent changes:
C00130 ENDMK
C⊗;
@x Tell WEAVE to print only the changes:
	\def\?##1]{\hbox to 1in{\hfil##1.\ }}
	}
@y
	\def\?##1]{\hbox{Changes to \hbox to 1em{\hfil##1}.\ }}
	}
\let\maybe=\iffalse
@z
@x WAITS's banner:
@d banner=='This is TeX, Version 2.991' {printed when \TeX\ starts}
@y
@d banner=='This is TeX, WAITS Version 2.991' {printed when \TeX\ starts}
@z
@x Switches for debugging and statistics:
@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering
	usage statistics}
@d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering
	usage statistics}
@f stat==begin
@f tats==end
@y
Moreover, this version of \TeX\ has been instrumented so that runtime
frequency counts can be accumulated from all usage of \TeX\ over a
long period of time. Such code is delimited by `$|freq|\ldots|qerf|$'.
(As of October 22, 1986, we are no longer accumulating frequency counts.)

@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat== {change this to `$\\{stat}\equiv\.{@@\{}$' when not
	gathering usage statistics}
@d tats==@t@> {change this to `$\\{tats}\equiv\.{@@\}}$' when not
	gathering usage statistics}
@f stat==begin
@f tats==end
@#
@d freq==@{ {change this to `$\\{freq}\equiv\null$' when
	accumulating frequency counts}
@d qerf==@t@>@} {change this to `$\\{qerf}\equiv\null$' when
	accumulating frequency counts}
@f freq==begin
@f qerf==end
@z
@x The INITEX switch:
@d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
@d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
@y
@d init==@{ {change this to `$\\{init}\equiv\null$' for \.{INITEX}}
@d tini==@t@>@} {change this to `$\\{tini}\equiv\null$' for \.{INITEX}}
@z
@x Compiler directives:
@{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
@y
@{@&$C-,A+,D-,W+@}
	{no range check, catch arithmetic overflow, no debug overhead}
@!freq @{@&$D:2@}@+ qerf {except for optional frequency counters}
@!debug @{@&$C+,D:7,W+@}@+ gubed {but turn everything on when debugging}
{the `|W+|' switch catches more syntax errors}
{the `\ignorespaces|D:5|' avoids initial stop for the debugger}
{the `\ignorespaces|D:2|' augments debugger to maintain counters}
@z
@x Compile-time constants:
@!mem_max=30000; {greatest index in \TeX's internal |mem| array;
	must be strictly less than |max_halfword|;
	must be equal to |mem_top| in \.{INITEX}, otherwise |≥mem_top|}
@!mem_min=0; {smallest index in \TeX's internal |mem| array;
	must be |min_halfword| or more;
	must be equal to |mem_bot| in \.{INITEX}, otherwise |≤mem_bot|}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files and in control sequences between
	\.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|}
@!error_line=72; {width of context lines on terminal error messages}
@!half_error_line=42; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=79; {width of longest text lines output; should be at least 60}
@!stack_size=200; {maximum number of simultaneous input sources}
@!max_in_open=6; {maximum number of input files and error insertions that
	can be going on simultaneously}
@!font_max=75; {maximum internal font number; must not exceed |max_quarterword|
	and must be at most |font_base+256|}
@!font_mem_size=20000; {number of words of |font_info| for all fonts}
@!param_size=60; {maximum number of simultaneous macro parameters}
@!nest_size=40; {maximum number of semantic levels simultaneously active}
@!max_strings=3000; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
	available for the user's control sequences and font names,
	after \TeX's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all fonts and
	control sequences; must exceed |string_vacancies| by the total
	length of \TeX's own strings, which is currently about 23000}
@!save_size=600; {space for saving values outside of current group; must be
	at most |max_halfword|}
@!trie_size=8000; {space for hyphenation patterns; should be larger for
	\.{INITEX} than it is in production versions of \TeX}
@!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8}
@!file_name_size=40; {file names shouldn't be longer than this}
@!pool_name='TeXformats:TEX.POOL                     ';
	{string of length |file_name_size|; tells where the string pool appears}
@.TeXformats@>
@y
@!mem_max=56000; {greatest index in \TeX's internal |mem| array;
	must be strictly less than |max_halfword|;
	must be equal to |mem_top| in \.{INITEX}, otherwise |≥mem_top|}
@!mem_min=0; {smallest index in \TeX's internal |mem| array;
	must be |min_halfword| or more;
	must be equal to |mem_bot| in \.{INITEX}, otherwise |≤mem_bot|}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files and in control sequences between
	\.{\\csname} and \.{\\endcsname}; must not exceed |max_halfword|}
@!error_line=80; {width of context lines on terminal error messages}
@!half_error_line=50; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=80; {width of longest text lines output; should be at least 60}
@!stack_size=200; {maximum number of simultaneous input sources}
@!max_in_open=6; {maximum number of input files and error insertions that
	can be going on simultaneously}
@!font_max=100;
	{maximum internal font number; must not exceed |max_quarterword|}
@!font_mem_size=27000; {number of words of |font_info| for all fonts}
@!param_size=60; {maximum number of simultaneous macro parameters}
@!nest_size=40; {maximum number of semantic levels simultaneously active}
@!max_strings=3600; {maximum number of strings}
@!string_vacancies=18000; {the minimum number of characters that should be
	available for the user's control sequences and font names,
	after \TeX's own error messages are stored}
@!pool_size=45000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all fonts and
	control sequences; must exceed |string_vacancies| by the total
	length of \TeX's own strings, which is currently about 22500}
@!save_size=600; {space for saving values outside of current group; must be
	at most |max_halfword|}
@!trie_size=6000; {space for hyphenation patterns; should be larger for
	\.{INITEX} than it is in production versions of \TeX}
@!dvi_buf_size=800; {size of the output buffer; must be a multiple of 8}
@!file_name_size=23; {file names shouldn't be longer than this}
@!pool_name='TEX.POOL[TEX,SYS]      ';
	{string of length |file_name_size|; tells where the string pool appears}
@!count_name='<TEX!>.TXT[TEX,SYS]    '; {frequency counts go here}
@!check_delay=10; {minutes between checking the upper segment integrity}
@!write_delay=12; {|check_delay|'s between updates to the count history file}
@z
@x TANGLE-time constants:
@d mem_bot=0 {smallest index in the |mem| array dumped by \.{INITEX};
	must not be less than |mem_min|}
@d mem_top==30000 {largest index in the |mem| array dumped by \.{INITEX};
	must be substantially larger than |mem_bot|
	and not greater than |mem_max|}
@d font_base=0 {smallest internal font number; must not be less
	than |min_quarterword|}
@d hash_size=2100 {maximum number of control sequences; it should be at most
	about |(mem_max-mem_min)/10|, but 2100 is already quite generous}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions}
@y
@d mem_bot=16000 {smallest index in the |mem| array dumped by \.{INITEX};
	must not be less than |mem_min|}
@d mem_top==48000 {largest index in the |mem| array dumped by \.{INITEX};
	must be substantially larger than |mem_bot|
	and not greater than |mem_max|}
@d font_base=0 {smallest internal font number; must not be less
	than |min_quarterword|}
@d hash_size=2500 {maximum number of control sequences; it should be at most
	about |(mem_max-mem_min)/10|, but 2100 is already quite generous}
@d hash_prime=2113 {a prime number equal to about 85\% of |hash_size|}
@d hyph_size=307 {another prime; the number of \.{\\hyphenation} exceptions}
@z
@x System-dependent character set changes:
@↑character set dependencies@>
@↑system dependencies@>

@<Set init...@>=
for i←1 to @'37 do xchr[i]←' ';
@y
@↑character set dependencies@>
@↑system dependencies@>

The code shown here is intended to be used on the Stanford {\sc SAIL} system,
and at other installations like CMU and ISI where essentially the same
extended character set is used. The fact that {\mc SAIL} has |'}'| in the
wrong place turns out to cause no difficulty in this case.

@<Set initial values...@>=
for i←1 to @'37 do xchr[i]←chr(i);
xchr[@'30]←chr(@'137);
xchr[@'32]←chr(@'33); {|not_equal| sign}
xchr[@'33]←chr(@'176);
@z
@x Opening files:
@d reset_OK(#)==erstat(#)=0
@d rewrite_OK(#)==erstat(#)=0

@p function a_open_in(var f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/O'); a_open_in←reset_OK(f);
end;
@#
function a_open_out(var f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O'); a_open_out←rewrite_OK(f);
end;
@#
function b_open_in(var f:byte_file):boolean;
	{open a binary file for input}
begin reset(f,name_of_file,'/O'); b_open_in←reset_OK(f);
end;
@#
function b_open_out(var f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O'); b_open_out←rewrite_OK(f);
end;
@#
function w_open_in(var f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O'); w_open_out←rewrite_OK(f);
end;
@y
@d reset_OK(#)==erstat(#) mod @'20000 = 0
@d rewrite_OK(#)==erstat(#) mod @'20000 = 0

@p function erstat(var f:file):integer; extern;@t/2@>
@#
function a_open_in(var f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/E/O/N:9');
	{the \.{/E} switch distinguishes |form_feed| from |carriage_return|;
	the \.{/O} switch gives error control to us;
	and the \.{/N:9} switch specifies 9 buffers, which
	seems to work satisfactorily at {\mc SAIL}}
a_open_in←reset_OK(f);
end;
@#
function a_open_out(var f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O/N:2'); a_open_out←rewrite_OK(f);
end; {two buffers seems adequate for text output files}
@#
function b_open_in(var f:byte_file):boolean;
	{open a binary file for input}
begin reset(f,name_of_file,'/B:8/O/N:2'); b_open_in←reset_OK(f);
end;	{the \.{/B} switch is necessary to get byte packing}
@#
function b_open_out(var f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O/N:9/P:256'); b_open_out←rewrite_OK(f);
end;    {here we use |ary_out| so the \.{/B} switch isn't appropriate}
	{the \.{/P:256} sets file protection to `dump never'}
@#
function w_open_in(var f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O/N:9'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O/N:9'); w_open_out←rewrite_OK(f);
end;
@z
@x New input_ln:
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables called
|buffer|, |first|, and |last| that will be described in detail later; for
now, it suffices for us to know that |buffer| is an array of |ASCII_code|
values, and that |first| and |last| are indices into this array
representing the beginning and ending of a line of text.

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@y
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables
called |buffer|, |first|, and |last| that will be described in detail
later; for now, it suffices for us to know that |buffer| is an array of
|ASCII_code| values, and that |first| and |last| are indices into this
array representing the beginning and ending of a line of text.

We will read the lines first into an auxiliary buffer, in order to
save the running time of procedure-call overhead. This uses a nice
feature of \ph\ that Knuth chose not to mention in \TeX82.
@↑Knuth, Donald Ervin@>

At {\mc SAIL} we want to recognize page marks (indicated by |form_feed|
characters), and keep track of the current page number.

@d form_feed=@'14 {ASCII code used at end of a page}

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@!aux_buf:array[0..70] of text_char; {where the characters go first}
@↑system dependencies@>
@z
@x
@p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
var last_nonblank:0..buf_size; {|last| with trailing blanks removed}
begin if bypass_eoln then if not eof(f) then get(f);
	{input the first character of the line into |f↑|}
last←first; {cf.\ Matthew 19\thinspace:\thinspace30}
if eof(f) then input_ln←false
else	begin last_nonblank←first;
	while not eoln(f) do
		begin if last≥max_buf_stack then
			begin max_buf_stack←last+1;
			if max_buf_stack=buf_size then
				overflow("buffer size",buf_size);
@:TeX capacity exceeded buffer size}{\quad buffer size@>
			end;
		buffer[last]←xord[f↑]; get(f); incr(last);
		if buffer[last-1]≠" " then last_nonblank←last;
		end;
	last←last_nonblank; input_ln←true;
	end;
end;
@y
@p function input_ln(var f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
label 1,done;
var n: integer;
@!k,@!m: 0..buf_size; {indices into |buffer|}
begin if bypass_eoln then {input the first character of the line into |f↑|}
	begin if not eof(f) then get(f);
	if not eof(f) then if f↑=chr(@'12) then get(f); {skip past a |line_feed|}
	end;
last←first;
if eof(f) then input_ln←false
else	begin read(f,aux_buf:n);
	if buffer[first]=form_feed then {previous line was end-of-page}
		begin incr(page); line←1; {adjust line and page numbers}
		end;
1:	if last+n>max_buf_stack then
		if last+n≥buf_size then
			begin max_buf_stack←buf_size;
			overflow("buffer size",buf_size);
@:TeX capacity exceeded buffer size}{\quad buffer size@>
			end
		else max_buf_stack←last+n;
	if n>0 then
		begin m←last;
		if n=72 then last←m+71@+else last←m+n;
		for k←m to last-1 do buffer[k]←xord[aux_buf[k-m]];
		if n=72 then {there's more on this line}
			begin read(f,aux_buf:n); goto 1;
			end;
		end
	else if f↑=chr(form_feed) then {end of page}
		begin aux_buf[0]←f↑; n←1; goto 1;
		end;
	loop@+	begin if last=first then goto done;
		if buffer[last-1]≠" " then goto done;
		decr(last);
		end;
done:	input_ln←true;
	end;
end;
@↑system dependencies@>
@z
@x Terminal I/O:
is considered an output file the file variable is |term_out|.
@↑system dependencies@>

@<Glob...@>=
@!term_in:alpha_file; {the terminal as an input file}
@!term_out:alpha_file; {the terminal as an output file}

@ Here is how to open the terminal files
in \ph. The `\.{/I}' switch suppresses the first |get|.
@↑system dependencies@>

@d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input}
@d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output}
@y
is considered an output file the file variable is |term_out|.
On WAITS, this point is moot, since we use the built-in |TTY| file.
@↑system dependencies@>

@d term_in==TTY {the terminal as an input file}
@d term_out==TTY {the terminal as an output file}

@ Here is how to open the terminal files on WAITS: we don't do anything,
since |TTY| is always open.  Note that |eoln(term_in)| is initially |true|.
@↑system dependencies@>

@d t_open_in==do_nothing {open the terminal for text input}
@d t_open_out==do_nothing {open the terminal for text output}
@z
@x Special terminal controls:
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
@y
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == begin if inskp0 then end
	{cancel the user's cancellation of output}

@<Error handling procedures@>=
function inskp0:boolean; extern;
@z
@x Initializing the terminal:
@ The following program does the required initialization
without retrieving a possible command line.
It should be clear how to modify this routine to deal with command lines,
if the system permits them.
@↑system dependencies@>

@p function init_terminal:boolean; {gets the terminal input started}
label exit;
begin t_open_in;
loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	write_ln(term_out,'Please type the name of your input file.');
	end;
exit:end;
@y
@ The following program does the required initialization
and accepts interrupts and also retrieves a possible command line, using
new system routines due to David R. Fuchs.
@↑Fuchs, David Raymond@>

@d pto_chr(#)==ptwr1w(0,ord(#)) {put a character in the line editor}

@p procedure esci(var x:integer); extern; @t\2@>@;
	{increments |x| each time the user types escape-I or break-I;
	the program can change |x| whenever it wants to, but |x| had
	better be a global variable}
@#
function rescan:boolean; extern; @t\2@>@;
	{puts the command line into the terminal buffer,
	or returns |false| if there was no command line}
@#
function tmp_in(f:s@&t@&r@&i@&n@&g;var s:s@&t@&r@&i@&n@&g):integer; extern;
	@t\2@>@;
	{reads \.{TMPCOR} file |f| into |s|, and returns its length
		(|≤0| means error)}
@#
function cclsw: boolean; extern; @t\2@>@;
	{was program started with \.{RUN} offset of 1 (i.e., from \.{SNAIL})?}
@#
procedure ptwr1w(pty,c:integer); extern; @t\2@>@;
	{simulates typing of a character on a \.{PTY}}
@#
function init_terminal:boolean; {gets the terminal files started}
label exit;
var l:integer; {length returned by |tmp_in|}
@!line_found:boolean; {have we scanned a line?}
@!tmp_cor_buf:packed array[0..100] of char; {where |tmp_in| puts things}
begin t_open_in;
esci(interrupt);
last←first;
if cclsw then {started by \.{TEX} monitor command}
	begin l←tmp_in('TEX',tmp_cor_buf);
	loc←1;
	while (loc<l)∧(tmp_cor_buf[loc]≠'←') do incr(loc);
	incr(loc);
	while loc<l do
		begin if tmp_cor_buf[loc]>' ' then
			begin buffer[last]←xord[tmp_cor_buf[loc]]; incr(last);
			end;
		incr(loc);
		end;
	end
else
@!debug if false then@;@+gubed@;@/
if rescan then
	begin read_ln(term_in); {get first character into |term_in↑|}
	while (¬ eoln(term_in))∧(term_in↑≠';') do get(term_in);
	if term_in↑=';' then
		begin get(term_in);
		while ¬ eoln(term_in) do
			begin buffer[last]←xord[term_in↑]; incr(last); get(term_in);
			end;
		end;
	end;
line_found←(last>first);
loop@+	begin loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	if line_found then
		write_ln(term_out,'Please type the name of your input file.');
	wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	buffer[first]←0; {|input_ln| may look at |buffer[first]|}
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	line_found←true;
	end;
exit:end;
@↑system dependencies@>
@z
@x Making special characters printable:
@<Character |k| cannot be printed@>=
	(k<" ")∨(k>"~")
@y
@<Character |k| cannot be printed@>=
	(k=@'177)∨(k in [0,@'11..@'15,@'33])
@z
@x Terminal input:
begin update_terminal; {Now the user sees the prompt for sure}
@y
begin update_terminal; {Now the user sees the prompt for sure}
buffer[first]←0; {makes sure |input_ln| doesn't find a |form_feed|}
@z
@x The `E' option:
line ready to be edited. But such an extension requires some system
wizardry, so the present implementation simply types out what file should be
edited and the relevant line number.
@y
line ready to be edited. The present implementation does this by loading
the line editor with the appropriate call to the editor. We treat `\.T' the
same as `\.E', because other programs on this system invoke the editor
when the user says `\.T'.
@z
@x
"E": if base_ptr>0 then
	begin print_nl("You want to edit file ");
@.You want to edit file x@>
	print(input_stack[base_ptr].name_field);
	print(" at line "); print_int(line);
	interaction←scroll_mode; jump_out;
	end;
@y
"E","T": if base_ptr>0 then
	begin selector←new_string; pool_ptr←str_start[str_ptr];
	print("et "); print(input_stack[base_ptr].name_field);
	print_char("/"); print_int(page); print("p/");
	print_int(line); print_char("l"); print_char(carriage_return);
	if str_ptr<max_strings then
		begin pseudo_typein←str_ptr; incr(str_ptr);
		str_start[str_ptr]←pool_ptr;
		end; {|make_string| not declared |forward|}
	selector←term_and_log; interaction←scroll_mode; jump_out;
	end;
@z
@x Changes for 36-bit machines:
The values defined here are recommended for most 32-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=255 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==65535 {largest allowable value in a |halfword|}
@y
The values defined here are recommended for most 36-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=511 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==262143 {largest allowable value in a |halfword|}
@z
@x Eliminating addition/subtraction of zero:
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
@↑inner loop@>@↑system dependencies@>

@d qi(#)==#+min_quarterword
	{to put an |eight_bits| item into a quarterword}
@d qo(#)==#-min_quarterword
	{to take an |eight_bits| item out of a quarterword}
@d hi(#)==#+min_halfword
	{to put a sixteen-bit item into a halfword}
@d ho(#)==#-min_halfword
	{to take a sixteen-bit item from a halfword}
@y
The inner loop of \TeX\ will run faster with respect to compilers
that don't optimize expressions like `|x+0|' and `|x-0|', if these
macros are simplified in the obvious way when |min_quarterword=0|.
So they have been simplified here in the obvious way.
@↑inner loop@>@↑system dependencies@>

@d qi(#)==# {to put an |eight_bits| item into a quarterword}
@d qo(#)==# {to take an |eight_bits| item from a quarterword}
@d hi(#)==# {to put a sixteen-bit item into a halfword}
@d ho(#)==# {to take a sixteen-bit item from a halfword}
@z
@x Kludge to get around Pascal bug (with large array lower bounds)
@!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}
@y
@z
@x Special form_feed initialization:
cat_code("\")←escape; cat_code("%")←comment;
@y
cat_code("\")←escape; cat_code("%")←comment;
cat_code(form_feed)←car_ret;
@z
@x Date and time:
Since standard \PASCAL\ cannot provide such information, something special
is needed. The program here simply specifies July 4, 1776, at noon; but
users probably want a better approximation to the truth.

@p procedure fix_date_and_time;
begin time←12*60; {minutes since midnight}
day←4; {fourth day of the month}
month←7; {seventh month of the year}
year←1776; {Anno Domini}
end;
@y
It uses a {\mc WAITS} monitor call that puts the date in the left 18 bits
and the time in the right 18 bits.

@p procedure fix_date_and_time;
var t:integer; {accumulator}
date:integer; {raw date}
g:boolean; {garbage}
begin call_i(@'400101,,t,t,g); {that's \.{ACCTIM}}
date←t div @'1000000;
time←(t mod @'1000000) div 60;
day←(date mod 31)+1;
month←((date div 31) mod 12)+1;
year←(date div (31*12))+1964;
if month=4 then if day=1 then {April Fool}
	wterm_ln('Greetings from your user-friendly TeX System.');
end;
@z
@x Page number maintenance:
If more information about the input state is needed, it can be
included in small arrays like those shown here. For example,
the current page or segment number in the input file might be
put into a variable |@!page|, maintained for enclosing levels in
`\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
by analogy with |line_stack|.
@y
Similarly, we maintain a global variable |page| and a corresponding
|page_stack|.
@z
@x
@!line_stack : array[1..max_in_open] of integer;
@y
@!line_stack : array[1..max_in_open] of integer;
@!page : integer; {current page number in the current source file}
@!page_stack : array[1..max_in_open] of integer;
@z
@x Printing the page number:
else	begin print_nl("l."); print_int(line);
@y
else	begin if page>1 then
		begin print_nl("p."); print_int(page); print(",l.");
		end
	else print_nl("l.");
	print_int(line);
@z
@x More page number maintenance:
or |limit| or |line|.
@y
or |limit| or |line| or |page|.
@z
@x
line_stack[index]←line; start←first; state←mid_line;
@y
line_stack[index]←line; start←first; state←mid_line;
page_stack[index]←page;
@z
@x
begin first←start; line←line_stack[index];
@y
begin first←start; page←page_stack[index]; line←line_stack[index];
@z
@x More page number printing:
		print("; all text was ignored after line "); print_int(skip_line);
@y
		print("; all text was ignored after line "); print_int(skip_line);
		if skip_page>1 then
			begin print(", p."); print_int(skip_page);
			end;
@z
@x Pausing on input:
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected, each line of input is displayed
on the terminal and the transcript file, followed by `\.{=>}'.
\TeX\ waits for a response. If the response is simply |carriage_return|, the
line is accepted as it stands, otherwise the line typed is
used instead of the line in the file.

@p procedure firm_up_the_line;
var k:0..buf_size; {an index into |buffer|}
begin limit←last;
if pausing>0 then if interaction>nonstop_mode then
	begin wake_up_terminal; print_ln;
	if start<limit then for k←start to limit-1 do print(buffer[k]);
	first←limit; prompt_input("=>"); {wait for user response}
@.=>@>
	if last>first then
		begin for k←first to last-1 do {move line down in buffer}
			buffer[k+start-first]←buffer[k];
		limit←start+last-first;
		end;
	end;
end;
@y
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected,
each line of input is displayed in the transcript file, followed by `\.{=>}',
and also put into the user's line-editor buffer.
\TeX\ waits for the line to be edited, and the next line received is
used instead of the line in the file.

@p procedure firm_up_the_line;
var k:0..buf_size; {an index into |buffer|}
begin limit←last;
if pausing>0 then if interaction>nonstop_mode then
 if buffer[start]≠form_feed then
	begin wake_up_terminal; print_ln;
	if start=limit then {empty line will be made nonempty so that it's visible}
		begin buffer[start]←" "; incr(limit);
		end;
	decr(selector); {inhibit terminal output temporarily}
	for k←start to limit-1 do
		begin print_char(buffer[k]);
		pto_chr(xchr[buffer[k]]);
		end;
	print("=>"); first←start;
	if not input_ln(term_in,true) then t_open_in;
@.End of file on the terminal@>
	if last>first then for k←first to last-1 do print_char(buffer[k]);
	limit←last; print_ln; incr(selector);
	end;
end;
@z
@x Reading the first line of a file:
@ The first line of a file must be treated specially, since |input_ln|
must be told not to start with |get|.
@↑system dependencies@>

@<Input the first line of |read_file[m]|@>=
if input_ln(read_file[m],false) then read_open[m]←normal
else	begin a_close(read_file[m]); read_open[m]←closed;
	end
@y
@ The first line of a file must be treated specially, since |input_ln|
must be told not to |get|. Furthermore we want to omit the optional file
directory page present at {\mc SAIL}.
@↑system dependencies@>

@<Input the first line of |read_file[m]|@>=
if not input_ln(read_file[m],false) then
	begin a_close(read_file[m]); read_open[m]←closed;
	end
else	begin if(last-start=29)∧(buffer[start]="C")∧(buffer[start+8]=@'26) then
		begin while (read_file[m]↑≠chr(form_feed))∧(not eof(read_file[m])) do
			begin read_ln(read_file[m]); read(read_file[m],aux_buf:temp_ptr);
			end; {skip the directory}
		buffer[start]←form_feed; last←start+1;
		end;
	read_open[m]←normal;
	end
@z
@x Still more page number maintenance:
where skipping began, for use in error messages.

@<Glob...@>=
@!skip_line:integer; {skipping began here}
@y
where skipping began, for use in error messages. Also the page number.

@<Glob...@>=
@!skip_line,@!skip_page:integer; {skipping began here}
@z
@x
skip_line←line;
@y
skip_line←line; skip_page←page;
@z
@x Parsing file names:
@ The file names we shall deal with for illustrative purposes have the
following structure:  If the name contains `\.>' or `\.:', the file area
consists of all characters up to and including the final such character;
otherwise the file area is null.  If the remaining file name contains
`\..', the file extension consists of all such characters from the first
remaining `\..' to the end, otherwise the file extension is null.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@y
@ The file names we shall deal with have the following structure:
If the name contains `\.[', the file area consists of all characters
from this character to the end; otherwise the file area is null.
If the remaining file name contains `\..', the file extension consists of all
such characters from this character to the end, otherwise the file extension
is null. We can assume that there is at most one `\.[' and at most one `\..'.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.[', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@z
@x
@d TEX_area=="TeXinputs:"
@.TeXinputs@>
@d TEX_font_area=="TeXfonts:"
@.TeXfonts@>
@y
@d TEX_area=="[tex,sys]"
@d TEX_font_area=="[tfm,sys]"
@z
@x
else	begin if (c=">")∨(c=":") then
		begin area_delimiter←pool_ptr; ext_delimiter←0;
		end
	else if (c=".")∧(ext_delimiter=0) then ext_delimiter←pool_ptr;
@y
else	begin if c="[" then area_delimiter←pool_ptr
	else if c="." then ext_delimiter←pool_ptr;
@z
@x
if area_delimiter=0 then cur_area←""
else	begin cur_area←str_ptr; incr(str_ptr);
	str_start[str_ptr]←area_delimiter+1;
	end;
if ext_delimiter=0 then
	begin cur_ext←""; cur_name←make_string;
	end
else	begin cur_name←str_ptr; incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←make_string;
	end;
@y
cur_name←str_ptr;
if ext_delimiter=0 then cur_ext←""
else	begin incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←str_ptr;
	end;
if area_delimiter≤str_start[str_ptr] then
	begin cur_area←""; incr(str_ptr); str_start[str_ptr]←pool_ptr;
	end
else	begin incr(str_ptr);
	str_start[str_ptr]←area_delimiter; cur_area←make_string;
	end;
@z
@x Printing file names:
begin print(a); print(n); print(e);
@y
begin print(n); print(e); print(a);
@z
@x Converting file names to Pascal strings:
allows both lowercase and uppercase letters in the file name.
@↑system dependencies@>

@d append_to_name(#)==begin c←#; incr(k);
@y
converts lowercase letters to uppercase.

A special convention is used here with respect to font metric files: If the
file name is longer than six characters (e.g., `\.{helvetica}' or
`\.{oldenglish}'), we abbreviate it by retaining the first three and
last three characters (e.g., `\.{helica}' or `\.{oldish}').
@↑system dependencies@>

@d append_to_name(#)==begin c←#; incr(k);
	if (c≥"a")∧(c≤"z") then c←c-@'40; {convert to uppercase}
@z
@x
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
@y
if (e=".tfm")∧(length(n)>6) then
	begin for j←str_start[n] to str_start[n]+2 do
		append_to_name(str_pool[j]);
	for j←str_start[n+1]-3 to str_start[n+1]-1 do
		append_to_name(str_pool[j]);
	end
else for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
@z
@x Parsing file names in the buffer:
@d format_default_length=20 {length of the |TEX_format_default| string}
@d format_area_length=11 {length of its area part}
@d format_ext_length=4 {length of its `\.{.fmt}' part}
@y
@d format_default_length=18 {length of the |TEX_format_default| string}
@d format_area_length=9 {length of its area part}
@d format_ext_length=4 {length of its `\.{.fmt}' part}
@z
@x
TEX_format_default←'TeXformats:plain.fmt';
@.TeXformats@>
@y
TEX_format_default←'PLAIN.fmt[tex,sys]';
@z
@x
|TEX_format_default|.
@y
|TEX_format_default|; but it actually switches stuff around to keep the
file area last.
@z
@x
@!j:integer; {index into |buffer| or |TEX_format_default|}
@y
@!j:integer; {index into |buffer| or |TEX_format_default|}
@!d:integer; {a kludge}
@z
@x
for j←1 to n do append_to_name(xord[TEX_format_default[j]]);
for j←a to b do append_to_name(buffer[j]);
for j←format_default_length-format_ext_length+1 to format_default_length do
	append_to_name(xord[TEX_format_default[j]]);
@y
for j←a to b do append_to_name(buffer[j]);
if b=0 then
	begin d←format_default_length-format_area_length+1;
	n←format_default_length;
	end
else d←format_default_length-format_area_length-format_ext_length+1;
for j←d to format_default_length-format_area_length do
	append_to_name(xord[TEX_format_default[j]]);
for j←format_default_length-n+1 to format_default_length do
	append_to_name(xord[TEX_format_default[j]]);
@z
@x The real file names:
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
which simply makes a \TeX\ string from the value of |name_of_file|, should
ideally be changed to deduce the full name of file~|f|, which is the file
most recently opened, if it is possible to do this in a \PASCAL\ program.
@↑system dependencies@>

This routine might be called after string memory has overflowed, hence
we dare not use `|str_room|'.

@p function make_name_string:str_number;
var k:1..file_name_size; {index into |name_of_file|}
begin if (pool_ptr+name_length>pool_size)∨(str_ptr=max_strings)∨
 (cur_length>0) then
	make_name_string←"?"
else	begin for k←1 to name_length do append_char(xord[name_of_file[k]]);
	make_name_string←make_string;
	end;
end;
function a_make_name_string(var f:alpha_file):str_number;
begin a_make_name_string←make_name_string;
end;
function b_make_name_string(var f:byte_file):str_number;
begin b_make_name_string←make_name_string;
end;
function w_make_name_string(var f:word_file):str_number;
begin w_make_name_string←make_name_string;
end;
@y
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
due to David Fuchs, deduces the full name of file~|f| on {\mc WAITS}:
@↑system dependencies@>
@↑Fuchs, David Raymond@>

@p procedure cur_nam(var chan:f@&i@&l@&e;var s:string); extern; @t\2@>@/
function make_name_string(var @!f:@=file@>):str_number;
var @!s:packed array[1..24] of char;
@!k:1..24; {file names at {\sc SAIL} have at most 23 characters}
begin if (pool_ptr+24>pool_size)∨(str_ptr=max_strings)∨(cur_length>0) then
	make_name_string←"?"
else	begin cur_nam(f,s); k←1;
	while ord(s[k])≠0 do
		begin append_char(xord[s[k]]); incr(k);
		end;
	make_name_string←make_string;
	end;
end;
function a_make_name_string(var f:alpha_file):str_number;
begin a_make_name_string←make_name_string(f);
end;
function b_make_name_string(var f:byte_file):str_number;
begin b_make_name_string←make_name_string(f);
end;
function w_make_name_string(var f:word_file):str_number;
begin w_make_name_string←make_name_string(f);
end;
@z
@x Line editor gets misspelled file name:
begin if interaction=scroll_mode then wake_up_terminal;
@y
@!i:pool_pointer; {index into |str_pool|}
begin if interaction=scroll_mode then wake_up_terminal;
@z
@x
clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
@y
clear_terminal; {now we'll fill the line editor's buffer with the old name}
for i←str_start[cur_name] to str_start[cur_name+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_ext] to str_start[cur_ext+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_area] to str_start[cur_area+1]-1 do
	pto_chr(xchr[str_pool[i]]);
ptwr1w(0,@'214); {control-formfeed returns cursor to start of line}
prompt_input(": "); @<Scan file name in the buffer@>;
@z
@x Reading the first line of a file, again:
@ Here we have to remember to tell the |input_ln| routine not to
start with a |get|. If the file is empty, it is considered to
contain a single blank line.
@↑system dependencies@>
@↑empty line at end of file@>

@<Read the first line...@>=
begin if ¬ input_ln(cur_file,false) then do_nothing;
@y
@ Here we have to remember to tell |input_ln| not to do the first |get|,
and we also want to skip over the material on a file directory page. An
empty file is considered to contain a single blank line.
@↑system dependencies@>
@↑empty line at end of file@>

@<Read the first line...@>=
begin if input_ln(cur_file,false) then
	begin if(last-start=29)∧(buffer[start]="C")∧(buffer[start+8]=@'26) then
		begin while (cur_file↑≠chr(form_feed))∧(not eof(cur_file)) do
			begin read_ln(cur_file); read(cur_file,aux_buf:temp_ptr);
			end; {skip the directory}
		buffer[start]←form_feed; last←start+1;
		end;
	end;
page←1;
@z
@x The DVI output buffer:
@!dvi_index=0..dvi_buf_size; {an index into the output buffer}
@y
@!dvi_index=0..dvi_buf_size; {an index into the output buffer}
@!packed_bytes=packed array[dvi_index] of eight_bits;
	{buffer for \.{DVI} output}
@z
@x
@!dvi_buf:array[dvi_index] of eight_bits; {buffer for \.{DVI} output}
@y
@!dvi_buf:packed_bytes; {buffer for \.{DVI} output}
@z
@x
@p procedure write_dvi(@!a,@!b:dvi_index);
var k:dvi_index;
begin for k←a to b do write(dvi_file,dvi_buf[k]);
end;
@y
@p procedure ary_out(var f:file;@!b:packed_bytes; @!o,@!c:integer);
	extern;@t\2@>@#
procedure write_dvi(@!a,@!b:dvi_index);
begin ary_out(dvi_file,dvi_buf,a div 4,(b+1-a)div 4);@{+1000@}
end;
@z
@x "r DVIspool":
	b_close(dvi_file);
@y
	b_close(dvi_file);
	if pseudo_typein=0 then
		begin k←selector; selector←new_string;
		pool_ptr←str_start[str_ptr];
		print("r DVIspool;"); print(output_file_name);
		print("/P Maple");
		selector←k;
		if pool_ptr<pool_size then
		 if str_ptr<max_strings then {|overflow| can't occur}
			pseudo_typein←make_string;
		end;
@z
@x Page numbers on over/underfull box messages:
	print_int(line);
@y
	print_int(line);
	if page>1 then
		begin print(", p."); print_int(page);
		end;
@z
@x
	print_int(line);
@y
	print_int(line);
	if page>1 then
		begin print(", p."); print_int(page);
		end;
@z
@x The endgame:
@<Last-minute...@>=
@y
The new stuff at {\mc SAIL} has to do with preparing for what the user
presumably wants to do next, by typing it for him/her.

@<Last-minute...@>=
@z
@x
end;
@y
if (pseudo_typein≠0)∧(interaction>batch_mode) then
	begin write_ln(term_out);
	for k←str_start[pseudo_typein] to str_start[pseudo_typein+1]-1 do
		pto_chr(xchr[str_pool[k]]);
	end;
end;
@z
@x Final system-dependent changes:
This section should be replaced, if necessary, by changes to the program
that are necessary to make \TeX\ work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the published program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@↑system dependencies@>
@y
Here are the remaining things needed to make the implementation
complete at {\mc SAIL}.
@↑system dependencies@>

@ The |pseudo_typein| variable is set nonzero if the |error| routine
uses the `\.E' option to exit and edit.

@<Glob...@>=
@!pseudo_typein:str_number;

@ @<Set init...@>=
pseudo_typein←0; page←0;

@ The |mem| array is declared after the other global variables, so
that our friendly \PASCAL\ will compile things correctly.

@<Glob...@>=
@!mem : array[mem_min..mem_max] of memory_word; {the big dynamic storage area}

@ The frequency counting system makes use of two special system routines
devised by David R. Fuchs with the help of Martin Frost and Joe Weening.
It uses the capabilities of ``phantom'' jobs, which are able to keep the
upper segment of \TeX\ alive for long periods of time so that statistics
can be accumulated across many runs.
@↑Fuchs, David Raymond@>
@↑Frost, Martin Edward@>
@↑Weening, Joseph Simon@>

There's a boolean function |tex_phn| called by every \TeX\ job right at
the beginning; it returns |true| or |false| depending on whether this
\TeX\ job should be the counter phantom.  (If the job's \.{JLOG} bit is
off, then the job wants to be a phantom, so we rename it to \.{[TEX!]},
unless there is already such a job, in which case we die.)  If we are the
phantom, |tex_phn| saves away the upper segment name (for later
reference), and calculates a checksum of the upper segment, which it then
stores in the upper segment.  Then |tex_phn| returns, and it is \TeX's job
to save its counts and to call |phn_chk| now and then.

Procedure |phn_chk| checks that the upper segment has not been violated.
If it has, |phn_chk| will change the upper segment name (so that no other
lusers will get it), and it also changes the lower segment name (so that
no new \TeX's will confuse this one for a good phantom, and so that the
system will also start up a new phantom); furthermore, |phn_chk| will try
to change the code so that all users' jobs will print an error message and
stop; after this, |phn_chk| will halt the phantom (which also makes the
system start a new one).

Another thing that |phn_chk| checks is that its upper segment's name hasn't
changed; otherwise it assumes it has been killed (which causes the segment
name to to be set to \.{*SEG*}), and goes away.  This allows a new version of
\TeX\ to be put up by making the \.{TEX.DMP} file, renaming the old count file,
and then killing the old \TeX\ upper segment.  This will wipe out current
lusers, however!

If |tex_phn| determines that we are not the phantom, but just a regular \TeX\
job, then there is still some work to do.  In particular, we want to make
every effort to see that we're connected to the phantom high segment, so
that our counts are accounted for.  To do this, |tex_phn| looks for a job
named \.{[TEX!]}, and gets that job's high segment number.  If there is not
exactly one \.{[TEX!]}, then |tex_phn| gives up and lets the user run without
being accounted for (unless a phantom starts up subsequently and latches
onto this job's high segment, in which case only the counts from the
latter part of the job will be counted!).  If there is a \.{[TEX!]}, then
|tex_phn| compares its upper segment number to our upper segment number.  If
it's the same, then |tex_phn| just returns, since this job's counts will
count.  Otherwise, |tex_phn| calculates its upper segment's checksum, and
attaches \.{[TEX!]}'s upper segment.  If we find a matching checksum in the
right spot, then |tex_phn| assumes that this upper segment is OK, and lets
the user continue with it.  If the checksum comparison fails, then a nasty
message is printed, informing the user that he is using an outdated \TeX\
dump file, and |tex_phn| reattaches to the original upper segment.

@<Last-minute procedures@>=
@!freq
function @!tex_phn:boolean; extern;@t\2@>
procedure @!phn_chk; extern;@t\2@>
qerf

@ Frequency counts go to a special file.

@<Glob...@>=
@!freq @!count_file:alpha_file; {the number of counts, followed by their values}
qerf

@ Here's a type that facilitates system hackery. (\TeX\ doesn't actually
use all of these variants.)

@d dec_word==packed record
	case integer of
	0: (@!xx:0..@'777777; @!p: ↑integer);
	1: (@!z: integer);
	2: (@!lh:0..@'777777; @!rh:0..@'777777);
	3: (@!sixbit: packed array[1..6] of 0..@'77);
	4: (@!b: set of bits)
	end

@ A special procedure called |magic| is called near the beginning of \TeX,
to do the necessary stuff for frequency counts.

@<Initialize the output routines@>=
@!freq magic;@+qerf

@ And here it is; |magic| does nothing unless this \TeX\ has been started up
by the operating system with a bit set that makes it want to be phantom.

@<Last-minute procedures@>=
@!freq procedure magic;
label exit;
mtype @!bits=0..35;
var @!num_counts: integer; {number of counters pointed to in |mem|}
@!job_hrl: integer; {highest address of this job}
@!junk,@!i,@!j: integer; {temporaries}
@!success: boolean; {temporary}
@!hack,@!memry: dec_word; {two more temporaries}
begin if not tex_phn then return; {do nothing if we're not the phantom}
@<Set the value of |job_hrl|
	to the highest address of code in the upper segment@>;
@<Make pointers to the counts@>;
@<Read in the old counts, if possible@>;
@<Forever update the count file, and keep an eye on corruption@>;
exit:end;
qerf

@ @<Set the value of |job_hrl|...@>=
hack.z←@'115; hack.z←hack.p↑; job_hrl←hack.rh

@ The phantom \TeX\ uses its |mem| array to point to the frequency
counts that appear in the upper segment, since this \TeX\ does no typesetting.
Counters are characterized by the fact that the preceding word contains
the command `\.{AOSA *+1}'.

@d AOSA_code==@'354000 {machine-language op code for \.{AOSA}}

@<Make pointers to the counts@>=
num_counts←0;
memry.z←@'400001;
while memry.z<job_hrl do
	begin hack.z←memry.p↑;
	if (hack.lh=AOSA_code) and (hack.rh=memry.z+1) then
		begin incr(memry.z); {skip to the counter}
		mem[num_counts].int←memry.z; {save address of counter}
		incr(num_counts);
		end;
	incr(memry.z);
	end

@ Since we will look at accumulated counts only when \TeX\ has not
changed versions, there is no need to store a check sum with the count file.

@<Read in the old counts, if possible@>=
reset(count_file,count_name,'/O');
if not eof(count_file) then
	begin read_ln(count_file,i);
	if i=num_counts then for i←0 to num_counts-1 do
		begin memry.z←mem[i].int;
		read_ln(count_file,memry.p↑);
		end;
	end;
close(count_file)

@ @<Forever update the count file, and keep an eye on corruption@>=
loop@+	begin for i←1 to write_delay do
		begin for j←1 to check_delay do
			call_i(@'31,,60,junk,success); {``sleep'' a minute}
		phn_chk; {check the upper segment for corruption}
		end;
	rewrite(count_file,count_name); {now we'll write out the counts}
	write_ln(count_file,num_counts);
	for i←0 to num_counts-1 do
		begin memry.z←mem[i].int;
		write_ln(count_file,memry.p↑:1);
		end;
	close(count_file);
	end
@z