§1  SCANWEAVE  INTRODUCTION 1

1* . Introduction. This program takes a TEX file generated by and strips out the sections which have not been changed, outputting the changed sections to a second, greatly reduced TEX file. The index, section names, and table of contents are dropped as well. If the program bears an uncanny resemblance to Donald Knuth’s , this is no accident, since it was cloned from . While was developed on a VAX system using VAX Pascal, rather than the Stanford system on which was developed, the system dependencies of the latter were used in so the modifications to implement it on a particular system are almost exactly the same as for and .

The program uses a few features of the local Pascal compiler that may need to be changed in other installations:

1) Case statements have a default.

2) Input-output routines may need to be adapted for use with a particular character set and/or for printing messages on the user’s terminal.

These features are also present in the Pascal version of TEX, where they are used in a similar (but more complex) way. System-dependent portions of can be identified by looking at the entries for ‘system dependencies’ in the index below.

The “banner line” defined here should be changed whenever is modified.

For the VAX/VMS versions of , we add a second decimal point and number to the main version number, so we can differentiate between changes to the standard and changes local to the VAX/VMS version.

define banner ≡ ´ ␣ ␣ ␣ ␣ ␣ ´

2* . The program begins with a fairly normal header, made up of pieces that will mostly be filled in later. The TEX input comes from file tex file and the new TEX output goes to file out tex file.

If it is necessary to abort the job because of a fatal error, the program calls the ‘jump out ’ procedure, which goes to the label end of SCANWEAVE .

define end of SCANWEAVE = 9999 { go here to wrap it up } ⟨ Compiler directives 4* ⟩ program SCANWEAVE ;

label end of SCANWEAVE ; { go here to finish }

const ⟨ Constants in the outer block 8 ⟩

type ⟨ Types in the outer block 11 ⟩

var ⟨ Globals in the outer block 9 ⟩ ⟨ VAX/VMS procedures 43* ⟩ ⟨ Error handling procedures 28 ⟩

procedure initialize; var ⟨ Local variables for initialization 16 ⟩ begin ⟨ Set initial values 10 ⟩ end;

3* . Some of this code is optional for use when debugging only; such material is enclosed between the delimiters debug and gubed. Other parts, delimited by stat and tats, are optionally included if statistics about ’s line counts are desired.

define debug ≡ { change this to ‘debug ≡ ’ when debugging }

define gubed ≡ { change this to ‘gubed ≡ ’ when debugging }

format debug ≡ begin

format gubed ≡ end

define stat ≡ { change this to ‘stat ≡ ’ when gathering usage statistics }

define tats ≡ { change this to ‘tats ≡ ’ when gathering usage statistics }

format stat ≡ begin

format tats ≡ end

2  INTRODUCTION  SCANWEAVE §4

4* . VAX/VMS Pascal allows the user to specify “attributes” in the program source that affect the translation of the program into machine language. Attributes are denoted by keywords; they are set off from the rest of the source text by enclosing them in brackets. The directives shown below cause range checking and other redundant code to be eliminated when is being generated, and they give the program access to definitions in the “environment” file $ , which describes every VAX/VMS system service and VAX-11 RMS routine. Arithmetic overflow will be detected in all cases. ⟨ Compiler directives 4* ⟩ ≡

[check (none), inherit (´ $ ´ )]

{ no range check, catch arithmetic overflow, no debug overhead }

This code is used in section 2*.

5. Labels are given symbolic names by the following definitions. We insert the label ‘exit :’ just before the ‘end’ of a procedure in which we have used the ‘return’ statement defined below; the label ‘restart ’ is occasionally used at the very beginning of a procedure; and the label ‘reswitch’ is occasionally used just prior to a case statement in which some cases change the conditions and we wish to branch to the newly applicable case. Loops that are set up with the loop construction defined below are commonly exited by going to ‘done’ or to ‘found ’ or to ‘not found ’, and they are sometimes repeated by going to ‘continue’.

define exit = 10 { go here to leave a procedure } define restart = 20 { go here to start a procedure again } define reswitch = 21 { go here to start a case statement again } define continue = 22 { go here to resume a loop } define done = 30 { go here to exit a loop } define found = 31 { go here when you’ve found it } define not found = 32 { go here when you’ve found something else }

6. Here are some macros for common programming idioms.

define incr (#) ≡ # ← # + 1 { increase a variable by unity } define decr (#) ≡ # ← # − 1 { decrease a variable by unity } define loop ≡ while true do { repeat over and over until a goto happens } define do nothing ≡ { empty statement } define return ≡ goto exit { terminate a procedure call } format return ≡ nil format loop ≡ xclause 7* . We assume that case statements may include a default case that applies if no matching label is found. Thus, we shall use constructions like

case x of

1: ⟨ code for x = 1 ⟩; 3: ⟨ code for x = 3 ⟩; othercases ⟨ code for x ≠ 1 and x ≠ 3 ⟩ endcases

since most Pascal compilers have plugged this hole in the language by incorporating some sort of default mechanism. For example, the compiler used to develop and TEX allows ‘others :’ as a default label, and other Pascals allow syntaxes like ‘else’ or ‘otherwise’ or ‘otherwise:’, etc. The definitions of othercases and endcases should be changed to agree with local conventions. (Of course, if no default mechanism is available, the case statements of this program must be extended by listing all remaining cases. The original author of (Knuth) would have taken the trouble to modify it so that such extensions were done automatically, if he had not wanted to encourage Pascal compiler writers to make this important change in Pascal, where it belongs.)

define othercases ≡ otherwise { default for cases not listed explicitly } define endcases ≡ end { follows the default case in an extended case statement } format othercases ≡ else format endcases ≡ end

8. The following constants need to be defined. ⟨ Constants in the outer block 8 ⟩ ≡

buf size = 1000; { maximum length of input line }

out buf size = 144; { length of output buffer, should be twice line length }

See also section 46*. This code is used in section 2*.

9. A global variable called history will contain one of four values at the end of every run: spotless means that no unusual messages were printed; harmless message means that a message of possible interest was printed but no serious errors were detected; error message means that at least one error was found; fatal message means that the program terminated abnormally. The value of history does not influence the behavior of the program; it is simply computed for the convenience of systems that might want to use such information.

define spotless = 0 { history value for normal jobs }

define harmless message = 1 { history value when non-serious info was printed } define error message = 2 { history value when an error was noted } define fatal message = 3 { history value when we had to stop prematurely } define mark harmless ≡ if history = spotless then history ← harmless message define mark error ≡ history ← error message define mark fatal ≡ history ← fatal message

⟨ Globals in the outer block 9 ⟩ ≡ history : spotless . . fatal message; { how bad was this run? }

See also sections 13, 20*, 23, 25, 31, and 45*. This code is used in section 2*.

10. ⟨ Set initial values 10 ⟩ ≡ history ← spotless ;

See also sections 14, 17, 18, 21*, and 52*. This code is used in section 2*.

4  THE CHARACTER SET  SCANWEAVE §11

11. The character set. One of the main goals in the design of has been to make it readily portable between a wide variety of computers. Yet by its very nature must use a greater variety of characters than most computer programs deal with, and character encoding is one of the areas in which existing machines differ most widely from each other.

To resolve this problem, all input to and (and !) is converted to an internal sevenbit code that is essentially standard ASCII, the “American Standard Code for Information Interchange.” The conversion is done immediately when each character is read in. Conversely, characters are converted from ASCII to the user’s external representation just before they are output.

Such an internal code is relevant to users of only because it is the code used for preprocessed constants like . If you are writing a program in that makes use of such one-character constants, you should convert your input to ASCII form, like , and do. Otherwise ’s internal coding scheme does not affect you.

Here is a table of the standard visible ASCII codes:

0  1  2  3  4  5  6  7

          ´ 040  ␣       !       "       #       $       %       &       '
          ´ 050  (       )       *       +       ,       -       .       /
          ´ 060  0       1       2       3       4       5       6       7
          ´ 070  8       9       :       ;       <       =       >       ?
          ´ 100  @       A       B       C       D       E       F       G
          ´ 110  H       I       J       K       L       M       N       O
          ´ 120  P       Q       R       S       T       U       V       W
          ´ 130  X       Y       Z       [       \       ]       ^_
          ´ 140  `       a       b       c       d       e       f       g
          ´ 150  h       i       j       k       l       m       n       o
          ´ 160  p       q       r       s       t       u       v       w
          ´ 170  x       y       z       {       |       }       ~

(Actually, of course, code ´ 040 is an invisible blank space.) Code ´ 136 was once an upward arrow ( ), and code ´ 137 was once a left arrow ( ), in olden times when the first draft of ASCII code was prepared; but works with today’s standard ASCII in which those codes represent circumflex and underline as shown. ⟨ Types in the outer block 11 ⟩ ≡

ASCII code = 0 . . 127; { seven-bit numbers, a subrange of the integers }

See also sections 42* and 44*. This code is used in section 2*.

§12  SCANWEAVE  THE CHARACTER SET 5

12* . The original Pascal compiler was designed in the late 60s, when six-bit character sets were common, so it did not make provision for lowercase letters. Nowadays, of course, we need to deal with both capital and small letters in a convenient way, so assumes that it is being used with a Pascal whose character set contains at least the characters of standard ASCII as listed above. Some Pascal compilers use the original name char for the data type associated with the characters in text files, while other Pascals consider char to be a 64-element subrange of a larger data type that has some other name.

In order to accommodate this difference, we shall use the name text char to stand for the data type of the characters in the input and output files. We shall also assume that text char consists of the elements chr (first text char ) through chr (last text char ), inclusive. The following definitions should be adjusted if necessary.

define text char ≡ char { the data type of characters in text files }

define first text char = 0 { ordinal number of the smallest element of text char }

define last text char = 127 { ordinal number of the largest element of text char }

define text file ≡ text

13. The  ,  and  processors convert between ASCII code and the user’s external

character set by means of arrays xord and xchr that are analogous to Pascal’s ord and chr functions. ⟨ Globals in the outer block 9 ⟩ +≡ xord : array [text char ] of ASCII code; { specifies conversion of input characters } xchr : array [ASCII code] of text char ; { specifies conversion of output characters } 14. If we assume that every system using is able to read and write the visible characters of standard ASCII (although not necessarily using the ASCII codes to represent them), the following assignment statements initialize most of the xchr array properly, without needing any system-dependent changes. For example, the statement ´ ´ ´ that appears in the present file might be encoded in, say, EBCDIC code on the external medium on which it resides, but will convert from this external code to ASCII and back again. Therefore the assignment statement ´ ´ will appear in the corresponding Pascal file, and Pascal will compile this statement so that xchr [65] receives the character in the external (char ) code. Note that it would be quite incorrect to say ´ , because is a constant of type integer , not char , and because we have = 65 regardless of the external character set. ⟨ Set initial values 10 ⟩ +≡

xchr [´ 40 ] ← ´ ␣´ ; xchr [´ 41 ] ← ´ ´ ; xchr [´ 42 ] ← ´ ´ ; xchr [´ 43 ] ← ´ #´ ; xchr [´ 44 ] ← ´ $´ ; xchr [´ 45 ] ← ´ %´ ; xchr [´ 46 ] ← ´ ´ ; xchr [´ 47 ] ← ´ ´ ´ ´ ; xchr [´ 50 ] ← ´ ´ ; xchr [´ 51 ] ← ´ ´ ; xchr [´ 52 ] ← ´ ´ ; xchr [´ 53 ] ← ´ ´ ; xchr [´ 54 ] ← ´ ´ ; xchr [´ 55 ] ← ´ ´ ; xchr [´ 56 ] ← ´ ´ ; xchr [´ 57 ] ← ´ ´ ; xchr [´ 60 ] ← ´ ´ ; xchr [´ 61 ] ← ´ ´ ; xchr [´ 62 ] ← ´ ´ ; xchr [´ 63 ] ← ´ ´ ; xchr [´ 64 ] ← ´ ´ ; xchr [´ 65 ] ← ´ ´ ; xchr [´ 66 ] ← ´ ´ ; xchr [´ 67 ] ← ´ ´ ; xchr [´ 70 ] ← ´ ´ ; xchr [´ 71 ] ← ´ ´ ; xchr [´ 72 ] ← ´ ´ ; xchr [´ 73 ] ← ´ ´ ; xchr [´ 74 ] ← ´ ´ ; xchr [´ 75 ] ← ´ ´ ; xchr [´ 76 ] ← ´ ´ ; xchr [´ 77 ] ← ´ ´ ; xchr [´ 100 ] ← ´ ´ ; xchr [´ 101 ] ← ´ ´ ; xchr [´ 102 ] ← ´ ´ ; xchr [´ 103 ] ← ´ ´ ; xchr [´ 104 ] ← ´ ´ ; xchr [´ 105 ] ← ´ ´ ; xchr [´ 106 ] ← ´ ´ ; xchr [´ 107 ] ← ´ ´ ; xchr [´ 110 ] ← ´ ´ ; xchr [´ 111 ] ← ´ ´ ; xchr [´ 112 ] ← ´ ´ ; xchr [´ 113 ] ← ´ ´ ; xchr [´ 114 ] ← ´ ´ ; xchr [´ 115 ] ← ´ ´ ; xchr [´ 116 ] ← ´ ´ ; xchr [´ 117 ] ← ´ ´ ; xchr [´ 120 ] ← ´ ´ ; xchr [´ 121 ] ← ´ ´ ; xchr [´ 122 ] ← ´ ´ ; xchr [´ 123 ] ← ´ ´ ; xchr [´ 124 ] ← ´ ´ ; xchr [´ 125 ] ← ´ ´ ; xchr [´ 126 ] ← ´ ´ ; xchr [´ 127 ] ← ´ ´ ; xchr [´ 130 ] ← ´ ´ ; xchr [´ 131 ] ← ´ ´ ; xchr [´ 132 ] ← ´ ´ ; xchr [´ 133 ] ← ´ ´ ; xchr [´ 134 ] ← ´ ´ ; xchr [´ 135 ] ← ´ ´ ; xchr [´ 136 ] ← ´ ´ ; xchr [´ 137 ] ← ´ ´ ; xchr [´ 140 ] ← ´ ` ´ ; xchr [´ 141 ] ← ´ ´ ; xchr [´ 142 ] ← ´ ´ ; xchr [´ 143 ] ← ´ ´ ; xchr [´ 144 ] ← ´ ´ ; xchr [´ 145 ] ← ´ ´ ; xchr [´ 146 ] ← ´ ´ ; xchr [´ 147 ] ← ´ ´ ; xchr [´ 150 ] ← ´ ´ ; xchr [´ 151 ] ← ´ ´ ; xchr [´ 152 ] ← ´ ´ ; xchr [´ 153 ] ← ´ ´ ; xchr [´ 154 ] ← ´ ´ ; xchr [´ 155 ] ← ´ ´ ; xchr [´ 156 ] ← ´ ´ ; xchr [´ 157 ] ← ´ ´ ; xchr [´ 160 ] ← ´ ´ ; xchr [´ 161 ] ← ´ ´ ; xchr [´ 162 ] ← ´ ´ ; xchr [´ 163 ] ← ´ ´ ; xchr [´ 164 ] ← ´ ´ ; xchr [´ 165 ] ← ´ ´ ; xchr [´ 166 ] ← ´ ´ ; xchr [´ 167 ] ← ´ ´ ; xchr [´ 170 ] ← ´ ´ ; xchr [´ 171 ] ← ´ ´ ; xchr [´ 172 ] ← ´ ´ ; xchr [´ 173 ] ← ´ ´ ; xchr [´ 174 ] ← ´ ´ ; xchr [´ 175 ] ← ´ ´ ; xchr [´ 176 ] ← ´ ´ ; xchr [0] ← ´ ␣´ ; xchr [´ 177 ] ← ´ ␣´ ; { these ASCII codes are not used }

15. Some of the ASCII codes below ´ 40 have been given symbolic names in  ,  and

because they are used with a special meaning.

define tab mark = ´ 11 { ASCII code used as tab-skip } define line feed = ´ 12 { ASCII code thrown away at end of line } define form feed = ´ 14 { ASCII code used at end of page } define carriage return = ´ 15 { ASCII code used at end of line }

16. When we initialize the xord array and the remaining parts of xchr , it will be convenient to make use of an index variable, i. ⟨ Local variables for initialization 16 ⟩ ≡ i: 0 . . last text char ;

This code is used in section 2*.

17. Here now is the system-dependent part of the character set. If is being implemented on a gardenvariety Pascal for which only standard ASCII codes will appear in the input and output files, you don’t need to make any changes here. But at MIT, for example, the code in this module should be changed to

for i ← 1 to ´ 37 do xchr [i] ← chr (i);

’s character set is essentially identical to MIT’s, even with respect to characters less than ´ 40 .

Changes to the present module will make more friendly on computers that have an extended character set, so that one can type things like instead of . If you have an extended set of characters that are easily incorporated into text files, you can assign codes arbitrarily here, giving an xchr equivalent to whatever characters the users of are allowed to have in their input files, provided that unsuitable characters do not correspond to special codes like carriage return that are listed above.

(The present file does not contain any of the non-ASCII characters, because it is intended to be used with all implementations of . It is based on , which was originally created on a Stanford system that has a convenient extended character set, then “sanitized” by applying another program that transliterated all of the non-standard characters into standard equivalents.) ⟨ Set initial values 10 ⟩ +≡

for i ← 1 to ´ 37 do xchr [i] ← ´ ␣´ ;

18. The following system-independent code makes the xord array contain a suitable inverse to the information in xchr . ⟨ Set initial values 10 ⟩ +≡

for i ← first text char to last text char do xord [chr (i)] ← ´ 40 ;

for i ← 1 to ´ 176 do xord [xchr [i]] ← i;

8  INPUT AND OUTPUT  SCANWEAVE §19

19. Input and output. The input conventions of this program are intended to be very much like those of TEX (except, of course, that they are much simpler, because much less needs to be done). Furthermore they are identical to those of and . Therefore people who need to make modifications to all four systems should be able to do so without too many headaches.

20* . Terminal output is done by writing on file term out , which is assumed to consist of characters of type text char :

define print (#) ≡ write ln(term out , #) { ‘print ’ means write on the terminal }

define print ln(#) ≡ write ln(term out , #, chr (13), chr (10)) { ‘print ’ and then start new line } define new line ≡ write ln(term out , chr (13), chr (10)) { start new line } define print nl (#) ≡ { print information starting on a new line }

begin new line; print (#); end

⟨ Globals in the outer block 9 ⟩ +≡ term out : text file; { the terminal as an output file } term in: text file; { the user’s terminal as an input file }

21* . Different systems have different ways of specifying that the output on a certain file will appear on the user’s terminal. Here is how to open the terminal file under VAX/VMS Pascal. ⟨ Set initial values 10 ⟩ +≡

open(term out , ´ $ ´ , carriage control ← none, record length ← 16383); rewrite(term out );

22* . The update terminal procedure is called when we want to make sure that everything we have output to the terminal so far has actually left the computer’s internal buffers and been sent.

define update terminal ≡ { empty the terminal output buffer }

23. The main input comes from tex file. ⟨ Globals in the outer block 9 ⟩ +≡ tex file: text file; { primary input } out tex file: text file; { output file }

24. The following code opens the fixed input and output files. Since these files were listed in the program header, we assume that the Pascal runtime system has already checked that suitable file names have been given; therefore no additional error checking needs to be done. procedure open input ; { prepare to read tex file and write to out tex file }

begin reset (tex file); rewrite(out tex file);

end;

25. Input goes into an array called buffer . ⟨ Globals in the outer block 9 ⟩ +≡ buffer : array [0 . . buf size] of ASCII code;

§26  SCANWEAVE  INPUT AND OUTPUT 9

26* . The input ln procedure brings the next line of input from the specified file into the buffer array and returns the value true, unless the file has already been entirely read, in which case it returns false. The conventions of TEX are followed; i.e., ASCII code numbers representing the next line of the file are input into buffer [0], buffer [1], …, buffer [limit − 1]; trailing blanks are ignored; and the global variable limit is set to the length of the line. The value of limit must be strictly less than buf size.

We assume that none of the ASCII code values of buffer [j] for 0 ≤ j < limit is equal to 0, ´ 177 , line feed , form feed , or carriage return.

Since the inner loop of input ln is part of ’s “inner loop”—each character of input comes in at this place—it is wise to reduce system overhead by making use of special routines that read in an entire array of characters at once. VAX/VMS Pascal allows us to use a ‘varying ’ string to read an entire line from the file. function input ln(var f : text file): boolean; { inputs a line or returns false }

label done;

var

aux buf : varying [buf size] of char ; { intermediate input buffer }

k: 0 . . buf size; { index into buffer } begin limit ← 0; if status (f) ≠ 0 then input ln ← false else begin read ln(f, aux buf , error ← continue); limit ← length(aux buf );

if limit ≥ buf size then begin print nl (´ ␣ ␣ ␣ ␣ ´ ); loc ← 0; error ; limit ← buf size − 1; end;

for k ← 1 to limit do buffer [k − 1] ← xord [aux buf [k]]; while limit > 1 do

if buffer [limit − 1] = ␣ then decr (limit ) else goto done; done: input ln ← true;

end; end;

27* . The output ln procedure writes the next line of output from the buffer array to the specified file. procedure output ln(var f : text file); { outputs a line }

var ch: char ; { current output character } temp: 0 . . buf size; aux buf : varying [buf size] of char ; { intermediate input buffer } begin if limit > 0 then

begin for temp ← 0 to limit − 1 do begin aux buf [temp + 1] ← xchr [buffer [temp]]; end; end;

aux buf .length ← limit ; write ln(f, aux buf , error ← continue);

10  REPORTING ERRORS TO THE USER  SCANWEAVE §28

end; 28. Reporting errors to the user.

Syntax errors are reported to the user by saying

‘err print (´ ␣ ␣ ´ )’,

followed by ‘jump out ’ if no recovery from the error is provided. This will print the error message followed by an indication of where the error was spotted in the source file. Note that no period follows the error message, since the error routine will automatically supply a period.

The actual error indications are provided by a procedure called error . define err print (#) ≡

begin new line; print (#); error ; end

⟨ Error handling procedures 28 ⟩ ≡ procedure error ; { prints ’ ’ and location of error message }

var j: 0 . . out buf size; { index into out buf }

k, l: 0 . . buf size; { indices into buffer } begin ⟨ Print error location 29 ⟩; update terminal ; mark error ; end;

See also section 30. This code is used in section 2*.

29. The error locations can be indicated by using the global variables loc and line, which tell respectively the first unlooked-at position in buffer and the current line number. This routine should be modified on systems whose standard text editor has special line-numbering conventions. ⟨ Print error location 29 ⟩ ≡

begin print (´ ␣ ´ ); print ln(´ ´ , tex line : 1, ´ ´ ); if loc ≥ limit then l ← limit else l ← loc; for k ← 1 to l do

if buffer [k − 1] = tab mark then print (´ ␣´ )

else print (xchr [buffer [k − 1]]); { print the characters already read } new line; for k ← 1 to l do print (´ ␣´ ); { space out the next line } for k ← l + 1 to limit do print (xchr [buffer [k − 1]]); { print the part not yet read } new line; { this separates the message from future asterisks } end

This code is used in section 28.

§30  SCANWEAVE  REPORTING ERRORS TO THE USER 11

30. The jump out procedure just cuts across all active procedure levels and jumps out of the program. This is the only non-local goto statement in . It is used when no recovery from a particular error has been provided.

Some Pascal compilers do not implement non-local goto statements. In such cases the code that appears at label end of SCANWEAVE should be copied into the jump out procedure, followed by a call to a system procedure that terminates the program.

define fatal error (#) ≡ begin new line; print (#); error ; mark fatal ; jump out ; end

⟨ Error handling procedures 28 ⟩ +≡ procedure jump out ;

begin goto end of SCANWEAVE ;

end;

31. ⟨ Globals in the outer block 9 ⟩ +≡ tex line: integer ; { the number of the current line in the main TEX file } out tex line: integer ; { the number of the line in the output TEX file } limit : 0 . . buf size; { the last character position occupied in the buffer } loc: 0 . . buf size; { the next character position to be read from the buffer } input has ended : boolean; { if true, there is no more input } output enabled : boolean; { if true, we are copying input lines to output }

32. ⟨ Initialize the input system 32 ⟩ ≡ open input ; limit ← 0; loc ← 1; buffer [0] ← ␣ ; tex line ← 0; out tex line ← 0; input has ended ← false; output enabled ← true;

This code is used in section 37*.

33. In this section we determine whether the current line is the beginning of a section (‘ ’ or ‘ ’ at beginning of line, followed immediately by a section number) and, if so, whether the section has been modified (‘ ’ following the number). We update the output enabled flag according. Additionally, if the index section is detected (‘ ’), we set the input has ended flag to terminate the program and set output enabled to false to keep the command from being copied to the output file.

define numeric digit at (#) ≡ ((buffer [#] ≤ ) ∧ (buffer [#] ≥ ))

define third char matches (#) ≡ (buffer [temp index + 2] = #)

define second char matches (#) ≡ (buffer [temp index + 1] = #) ∧ third char matches

define char matches (#) ≡ (buffer [temp index ] = #)

define three chars match(#) ≡ char matches (#) ∧ second char matches ⟨ Scan the line 33 ⟩ ≡

temp index ← 1;

if (limit > 3) ∧ (buffer [0] = ) then

if (char matches ( ) ∨ char matches ( )) ∧ numeric digit at (2) then

⟨ Search for ‘ ’; set output enabled if found 34 ⟩ else if three chars match( )( )( ) then

begin output enabled ← false; input has ended ← true; end;

This code is used in section 36.

34. Starting at the first digit of the section number, search for ‘ ’, which indicates that this is a changed section. Discontinue the search if ‘ ’ is found or the current position is no longer a numeric digit, which means we have moved past the section number without finding it. ⟨ Search for ‘ ’; set output enabled if found 34 ⟩ ≡

begin output enabled ← false; keep looking ← true; temp index ← 3;

while (¬output enabled ) ∧ keep looking do begin output enabled ← three chars match( )( )( ); keep looking ← numeric digit at (temp index ); incr (temp index ); end;

end

This code is used in section 33.

35. The command to generate the table of contents (‘ ’) is normally the last line in a TEX file generated

by  . Part of its function is to terminate TEX gracefully by generating a ‘  ’ command or equivalent
after printing the contents. Since we are dropping the ‘  ’ command, we must issue the ‘  ’ command

directly. ⟨ Add ‘ ’ command to end of output 35 ⟩ ≡

buffer [0] ← ; buffer [1] ← ; buffer [2] ← ; buffer [3] ← ; limit ← 4; output ln(out tex file); incr (out tex line);

This code is used in section 37*.

36. The get line procedure is called to read in the next line and scan it. We will output an “I’m alive!” dot to the terminal every 100 input lines and a new line every 2000. procedure get line; { inputs the next line }

label exit ;

var keep looking : boolean; temp index : integer ;

begin input has ended ← ¬input ln(tex file);

if input has ended then

begin output enabled ← false; return;

end else begin incr (tex line);

if (tex line mod 100) = 0 then begin print (´ ´ ); if (tex line mod 2000) = 0 then new line; end;

⟨ Scan the line 33 ⟩; buffer [limit ] ← ␣ ; end;

exit : end;

§37  SCANWEAVE  THE MAIN PROGRAM 13

37* . The main program. We have defined plenty of procedures, and it is time to put the last pieces of the puzzle in place. Here is where starts, and where it ends.

begin initialize; ⟨ Initialize the input system 32 ⟩; print ln(banner ); { print a “banner line” } ⟨ Read the input 38 ⟩;

end of SCANWEAVE : ⟨ Add ‘ ’ command to end of output 35 ⟩; stat ⟨ Print statistics about line counts 39 ⟩; tats if history < fatal message then

begin close(out tex file, disposition ← save, error ← continue); end;

⟨ Print the job history 40 ⟩; ⟨ Convert history to program exit status 51* ⟩; end.

38. In this procedure we read the input a line at time. We output an “I’m alive!” dot to the terminal every 100 output lines and a new line every 2000. ⟨ Read the input 38 ⟩ ≡

while ¬input has ended do begin get line;

if output enabled then

begin output ln(out tex file); incr (out tex line); end;

end;

This code is used in section 37*.

39. ⟨ Print statistics about line counts 39 ⟩ ≡ stat print nl (´ ␣ ␣ ´ ); print nl (tex line : 1, ´ ␣ ␣ ␣ ␣ ´ ); print nl (out tex line : 1, ´ ␣ ␣ ␣ ␣ ␣ ´ ); print nl (´ ␣´ ); tats

This code is used in section 37*.

40. Some implementations may wish to pass the history value to the operating system so that it can be used to govern whether or not other programs are started. Here we simply report the history to the user. ⟨ Print the job history 40 ⟩ ≡

case history of

spotless : print nl (´ ␣ ␣ ␣ ´ ); harmless message: print nl (´ ␣ ␣ ␣ ␣ ␣ ␣ ´ ); error message: print nl (´ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ´ ); fatal message: print nl (´ ␣ ␣ ␣ ␣ ␣ ␣ ´ ); end { there are no other cases }

This code is used in section 37*.

14  SYSTEM-DEPENDENT CHANGES  SCANWEAVE §41

41* . System-dependent changes. Here are the remaining changes to the program that are necessary to make work on VAX/VMS.

42* . Here is the stuff for magic file operations. ⟨ Types in the outer block 11 ⟩ +≡

unsafe file = [unsafe] file of char ;

fab pointer = ↑fab$type;

nam pointer = ↑nam$type;

43* . ⟨ VAX/VMS procedures 43* ⟩ ≡ function pas$fab(var f : unsafe file): fab pointer ; external ;

See also sections 47*, 48*, 49*, and 50*. This code is used in section 2*.

44* . VAX/VMS command argument names.

define arg size ≡ 255 ⟨ Types in the outer block 11 ⟩ +≡

arg buf = packed array [1 . . arg size] of char ;

45* . Temporary buffer for storing argument strings. ⟨ Globals in the outer block 9 ⟩ +≡ arg : arg buf ; arg len: 0 . . arg size;

46* . VAX/VMS CLI interface routines. ⟨ Constants in the outer block 8 ⟩ +≡

cli$ absent = ˝ 000381F0;

cli$ concat = ˝ 0003FD29;

cli$ defaulted = ˝ 0003FD21;

cli$ locneg = ˝ 00038230;

cli$ locpres = ˝ 0003FD31;

cli$ negated = ˝ 000381F8;

cli$ present = ˝ 0003FD19;

47* . ⟨ VAX/VMS procedures 43* ⟩ +≡ function cli$present (%stdescr name : packed array [l . . u : integer ] of char ): integer ; external ; function cli$get value(%stdescr name : packed array [l1 . . u1 : integer ] of char ;

§48  SCANWEAVE  SYSTEM-DEPENDENT CHANGES 15

%stdescr retbuf : packed array [l2 . . u2 : integer ] of char ): integer ; external ; 48* . Check for the presence of a command line argument; return 1 if the argument is specified, 0 if the argument is not present, and -1 if the argument is explicitly negated. If the argument is present and has a value, place its value in arg and set arg len to its length. ⟨ VAX/VMS procedures 43* ⟩ +≡ function get arg (nam : packed array [l . . u : integer ] of char ;

has val : boolean): integer ; label done, exit ; var status : integer ; k: 0 . . arg size; begin get arg ← 0; arg len ← 0; status ← cli$present (nam); if (status = cli$ negated ) ∨ (status = cli$ locneg ) then get arg ← −1 else if (status = cli$ concat ) ∨ (status = cli$ defaulted ) ∨ (status = cli$ present ) ∨ (status = cli$ locpres )

then

begin if has val then

begin if ¬odd (cli$get value(nam, arg )) then return; arg len ← arg size; while arg len > 0 do

if arg [arg len] = ´ ␣´ then decr (arg len) else goto done; done: end; get arg ← 1; end;

exit : end;

49* . The following procedures initialize the various RMS control blocks. The processing is similar for all block types; the code clears the block to zero, then sets the block id and block length fields. ⟨ VAX/VMS procedures 43* ⟩ +≡ procedure init fab(var f : fab$type);

type bytearray = packed array [1 . . fab$c bln] of 0 . . 255; var i: integer ; begin for i ← 1 to fab$c bln do f::bytearray [i] ← 0; f.fab$b bid ← fab$c bid ; f.fab$b bln ← fab$c bln; end;

procedure init nam(var n : nam$type); type bytearray = packed array [1 . . nam$c bln] of 0 . . 255; var i: integer ; begin for i ← 1 to nam$c bln do n::bytearray [i] ← 0; n.nam$b bid ← nam$c bid ; n.nam$b bln ← nam$c bln; end; 50* . Augment the string currently in arg to produce a complete file specification; produce an output parse if out parse is true, otherwise an input file. Use as default the file type passed as ext . If scrounge is true, use the principal input file, tex file, for additional defaults. ⟨ VAX/VMS procedures 43* ⟩ +≡ procedure add defaults (ext : packed array [l . . u : integer ] of char ;

scrounge, out parse: boolean); var fab: fab$type; nam: nam$type; def , tmp: arg buf ; f: fab pointer ; i: 0 . . arg size; begin for i ← arg len + 1 to arg size do arg [i] ← ´ ␣´ ; for i ← 1 to arg size do def [i] ← ´ ␣´ ; def [1] ← ´ ´ ; for i ← 1 to length(ext ) do def [i + 1] ← ext [i]; for i ← 1 to arg size do tmp[i] ← ´ ␣´ ; init fab(fab); init nam(nam); fab.fab$l dna ← iaddress (def ); fab.fab$b dns ← arg size; fab.fab$l fna ← iaddress (arg ); fab.fab$b fns ← arg size; fab.fab$l nam ← iaddress (nam); nam.nam$l esa ← iaddress (tmp); nam.nam$b ess ← arg size; if out parse then fab.fab$l fop ← fab$m ofp;

if scrounge then

begin f ← pas$fab(tex file); nam.nam$l rlf ← f↑.fab$l nam;

end; if odd ($parse(fab)) then

begin arg ← tmp; arg len ← nam.nam$b esl ;

end; end;

51* . The $exit system service passes a program status value back to whoever invoked ; we set sts$m inhib msg to keep error messages from appearing. It would be more in keeping with VAX/VMS conventions to use lib$stop to report error conditions (allowing exception handlers to gain control before the program exits), but setting the sts$m inhib msg causes the VAX/VMS Pascal default exception handler to reset the status value to sts$k normal . ⟨ Convert history to program exit status 51* ⟩ ≡

begin if history = harmless message then $exit (sts$k warning + sts$m inhib msg ) else if history = error message then $exit (sts$k error + sts$m inhib msg )

else if history = fatal message then $exit (sts$k severe + sts$m inhib msg ); end

This code is used in section 37*.

52* . The following code collects command line arguments from VAX/VMS, and connects external files to tex file, out tex file and out change file. It resets the input files just to make sure they are accessible; they are reset again at the beginning of the first pass.

define add input defaults (#) ≡ add defaults (#, false) define add output defaults (#) ≡ add defaults (#, true) define setup error (#) ≡

begin { report error during command argument processing } new line; print (#, ´ ␣´ , arg : arg len); mark fatal ; jump out ; end

⟨ Set initial values 10 ⟩ +≡ if get arg (´ ´ , true) ≤ 0 then setup error (´ ␣ ␣ ␣ ␣ ´ ); add input defaults (´ ´ , false); open(tex file, arg , readonly , error ← continue); if status (tex file) ≤ 0 then reset (tex file, error ← continue); if status (tex file) > 0 then setup error (´ ␣ ´ ´ ␣ ␣ ␣ ´ ); if get arg (´ ´ , true) ≤ 0 then do nothing ; add output defaults (´ ´ , true); open(out tex file, arg , new , disposition ← delete, error ← continue); if status (out tex file) ≤ 0 then rewrite(out tex file, error ← continue); if status (out tex file) > 0 then setup error (´ ␣ ´ ´ ␣ ␣ ␣ ␣ ␣´ );

18  INDEX  SCANWEAVE §53

53* . Index. Here is a cross-reference table for the processor. All modules in which an identifier is used are listed with that identifier, except that reserved words are indexed only when they appear in format definitions, and the appearances of identifiers in module names are not indexed. Underlined entries correspond to where the identifier was declared. Error messages and a few other things like “ASCII code” are indexed here too.

The following sections were changed by the change file: 1, 2, 3, 4, 7, 12, 20, 21, 22, 26, 27, 37, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53.

$exit : 51* . $parse: 50* . add defaults : \underline{50*} , 52* . add input defaults : \underline{52*} . add output defaults : \underline{52*} . arg : \underline{45*} , 48* , 50* , 52* . arg buf : \underline{44*} , 45* , 50* . arg len: \underline{45*} , 48* , 50* , 52* . arg size: \underline{44*} , 45* , 48* , 50* . ASCII code: 11. ASCII code: \underline{11}, 13, 25, 26* . aux buf : \underline{26*} , \underline{27*} . banner : \underline{1*} , 37* . begin: 3* . boolean: 26* , 31, 36, 48* , 50* . buf size: \underline{8}, 25, 26* , 27* , 28, 31. buffer : \underline{25}, 26* , 27* , 28, 29, 32, 33, 35, 36. bytearray : \underline{49*} . : 52* . : 52* . carriage return: \underline{15}, 17, 26* . ch: \underline{27*} . char : 12* , 14, 26* , 27* , 42* , 44* , 47* , 48* , 50* . char matches : \underline{33}. check : 4* . chr : 12* , 13, 17, 18, 20* . cli$ absent : 46* . cli$ concat : 46* , 48* . cli$ defaulted : 46* , 48* . cli$ locneg : 46* , 48* . cli$ locpres : 46* , 48* . cli$ negated : 46* , 48* . cli$ present : 46* , 48* . cli$get value: \underline{47*} , 48* . cli$present : \underline{47*} , 48* . continue: \underline{5}. debug: \underline{3*} . decr : \underline{6}, 26* , 48* . def : \underline{50*} . do nothing : \underline{6}, 52* . done: \underline{5}, 26* , \underline{48*} . else: 7* . end: 3* , 7* . end of SCANWEAVE : \underline{2*} , 30, 37* . endcases: \underline{7*} . err print : \underline{28}. error : 26* , \underline{28}, 30. error message: \underline{9}, 40, 51* . exit : \underline{5}, 6, 36, \underline{48*} . ext : \underline{50*} . external : 43* , 47* . f: \underline{26*} , \underline{27*} , \underline{43*} , \underline{49*} . fab: \underline{50*} . fab$b bid : 49* . fab$b bln: 49* . fab$b dns : 50* . fab$b fns : 50* . fab$c bid : 49* . fab$c bln: 49* . fab$l dna: 50* . fab$l fna: 50* . fab$l fop: 50* . fab$l nam: 50* . fab$m ofp: 50* . fab$type: 42* , 49* , 50* . fab pointer : \underline{42*} , 43* , 50* . false: 26* , 32, 33, 34, 36, 52* . fatal error : \underline{30}. fatal message: \underline{9}, 37* , 40, 51* . first text char : \underline{12*} , 18. form feed : \underline{15}, 26* . found : \underline{5}. get arg : \underline{48*} , 52* . get line: \underline{36}, 38. gubed: \underline{3*} . harmless message: \underline{9}, 40, 51* . has val : \underline{48*} . history : \underline{9}, 10, 37* , 40, 51* . i: \underline{16}, \underline{49*} . iaddress : 50* . incr : \underline{6}, 34, 35, 36, 38. inherit : 4* . init fab: \underline{49*} , 50* . init nam: \underline{49*} , 50* . initialize: \underline{2*} , 37* . inner loop: 26* . : 26* . input has ended : \underline{31}, 32, 33, 36, 38. input ln: \underline{26*} , 36. integer : 14, 31, 36, 47* , 48* , 49* , 50* .

§53  SCANWEAVE  INDEX 19

j: \underline{28}. jump out : 2* , 28, \underline{30}, 52* . k: \underline{28}. keep looking : 34, \underline{36}. l: \underline{28}. last text char : \underline{12*} , 16, 18. length: 27* . lib$stop: 51* . limit : 26* , 27* , 29, \underline{31}, 32, 33, 35, 36. line: 29. line feed : \underline{15}, 26* . line length: 8. loc: 26* , 29, \underline{31}, 32. loop: \underline{6}. l1 : 47* . l2 : 47* . mark error : \underline{9}, 28. mark fatal : \underline{9}, 30, 52* . mark harmless : \underline{9}. n: \underline{49*} . nam: \underline{48*} , \underline{50*} . nam$b bid : 49* . nam$b bln: 49* . nam$b esl : 50* . nam$b ess : 50* . nam$c bid : 49* . nam$c bln: 49* . nam$l esa: 50* . nam$l rlf : 50* . nam$type: 42* , 49* , 50* . nam pointer : \underline{42*} . new line: \underline{20*} , 28, 29, 30, 36, 52* . nil: 6. : 52* . none: 4* , 21* . not found : \underline{5}. numeric digit at : \underline{33}, 34. odd : 48* , 50* . open input : \underline{24}, 32. ord : 13. othercases: \underline{7*} . others : 7* . otherwise: 7* . out buf : 28. out buf size: \underline{8}, 28. out change file: 52* . out parse: \underline{50*} . out tex file: 2* , \underline{23}, 24, 35, 37* , 38, 52* . out tex line: \underline{31}, 32, 35, 38, 39. output enabled : \underline{31}, 32, 33, 34, 36, 38. output ln: \underline{27*} , 35, 38. Overflow in arithmetic: 4* . pas$fab: 43* , 50* . print : \underline{20*} , 28, 29, 30, 36, 52* . print ln: \underline{20*} , 29, 37* . print nl : \underline{20*} , 26* , 39, 40. read ln: 26* . reset : 24, 52* . restart : \underline{5}. reswitch: \underline{5}. retbuf : 47* . return: 5, \underline{6}. rewrite: 21* , 24, 52* . SCANWEAVE : \underline{2*} . scrounge: \underline{50*} . second char matches : \underline{33}. setup error : \underline{52*} . spotless : \underline{9}, 10, 40. stat: \underline{3*} . status : 26* , \underline{48*} , 52* . sts$k error : 51* . sts$k normal : 51* . sts$k severe: 51* . sts$k warning : 51* . sts$m inhib msg : 51* . system dependencies: \underline{1*} , 2* , 4* , 7* , 12* , 17, 20* , 21* , 22* , 24, 26* , 29, 30, 37* , 40, 42* , 43* , 44* , 46* , 47* , 48* , 49* , 50* , 51* , 52* .

tab mark : \underline{15}, 29.

tats: \underline{3*} . temp: \underline{27*} . temp index : 33, 34, \underline{36}. term in: \underline{20*} . term out : \underline{20*} , 21* . tex file: 2* , \underline{23}, 24, 36, 50* , 52* . tex line: 29, \underline{31}, 32, 36, 39. text : 12* . text char : \underline{12*} , 13, 20* . text file: \underline{12*} , 20* , 23, 26* , 27* . third char matches : \underline{33}. three chars match: \underline{33}, 34. tmp: \underline{50*} . true: 6, 26* , 31, 32, 33, 34, 52* . unsafe: 42* . unsafe file: \underline{42*} , 43* . update terminal : \underline{22*} , 28. u1 : 47* . u2 : 47* . var: \underline{43*} . varying : 26* , 27* . write ln: 20* , 27* . xchr : \underline{13}, 14, 16, 17, 18, 27* , 29. xclause: 6. xord : \underline{13}, 16, 18, 26* .

20  NAMES OF THE SECTIONS  SCANWEAVE §53

⟨ Add ‘ ’ command to end of output 35 ⟩ Used in section 37*. ⟨ Compiler directives 4* ⟩ Used in section 2*. ⟨ Constants in the outer block 8, 46* ⟩ Used in section 2*. ⟨ Convert history to program exit status 51* ⟩ Used in section 37*. ⟨ Error handling procedures 28, 30 ⟩ Used in section 2*.

⟨ Globals in the outer block 9, 13, 20*, 23, 25, 31, 45* ⟩ Used in section 2*.

⟨ Initialize the input system 32 ⟩ Used in section 37*. ⟨ Local variables for initialization 16 ⟩ Used in section 2*. ⟨ Print error location 29 ⟩ Used in section 28. ⟨ Print statistics about line counts 39 ⟩ Used in section 37*.

⟨ Print the job history 40 ⟩ Used in section 37*. ⟨ Read the input 38 ⟩ Used in section 37*. ⟨ Scan the line 33 ⟩ Used in section 36.

⟨ Search for ‘ ’; set output enabled if found 34 ⟩ Used in section 33.

⟨ Set initial values 10, 14, 17, 18, 21*, 52* ⟩ Used in section 2*. ⟨ Types in the outer block 11, 42*, 44* ⟩ Used in section 2*. ⟨ VAX/VMS procedures 43*, 47*, 48*, 49*, 50* ⟩ Used in section 2*.

The SCANWEAVE processor

(Version 1.0) (VAX/VMS Version 1.0.0, April 1986)

The changes required to create this VAX/VMS version of are the sections marked with asterisks (*) below.

Section Page

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 The character set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4 Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 8 Reporting errors to the user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 10 The main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 13 System-dependent changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 14 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 18