summaryrefslogtreecommitdiff
path: root/support/makeprog/webtoc/makeprog.ch
blob: 708e176efd825736f5c71a9527fa15c4e8a46d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
%   Change file for the MAKEPROG processor, for use with WEB to C.
%   This file was created for TANGLE by Howard Trickey and Pavel Curtis.
%       Modified for MAKEPROG by Klaus Guntermann at THD

% History:
%  10/9/82 (HT) Original version
%  11/29   (HT) New version, with conversion to lowercase handled properly
%               Also, new control sequence:
%                       @=...text...@>   Put ...text... verbatim on a line
%                                        by itself in the Pascal output.
%                                        (argument must fit on one line)
%               This control sequence facilitates putting #include "gcons.h"
%               (for example) in files meant for the pc compiler.
%               Also, changed command line usage, so that the absence of a
%               change file implies no change file, rather than one with the
%               same name as the web file, with .ch at the end.
%  1/15/83 (HT) Changed to work with version 1.2, which incorporates the
%               above change (though unbundling the output line breaking),
%               so mainly had to remove stuff.
%  2/17    (HT) Fixed bug that caused 0-9 in identifiers to be converted to
%               Q-Y on output.
%  3/18    (HT) Brought up to work with Version 1.5.  Added -r command line
%               flag to cause a .rpl file to be written with all the lines
%               of the .web file that were replaced because of the .ch file
%               (useful for comparing with previous .rpl files, to see if a
%               change file will still work with a new version of a .web file)
%               Also, made it write a newline just before exit.
%  4/12    (PC) Merged with Pavel's version, including adding a call to exit()
%               at the end depending upon the value of history.
%  4/16    (PC) Brought up to date with version 1.5 released April, 1983.
%  6/28   (HWT) Brought up to date with version 1.7 released June, 1983.
%    With new change file format, the -r option is now unnecessary.
%  7/17   (HWT) Brought up to date with version 2.0 released July, 1983.
% 12/18   (ETM) Brought up to date with version 2.5 released November, 1983.
% 11/7/84   (ETM) Brought up to date with version 2.6.
% 12/15/85 (ETM) Brought up to date with version 2.8.
%  3/ 7/88 (ETM) Converted for use with WEB2C
% 04/07/89 (-kg) Changed for MAKEPROG

% NOTE: The module numbers refer to the red-covered listing (Version 2).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [0] WEAVE: print only changes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 x
\pageno=\contentspagenumber \advance\pageno by 1
 y
\pageno=\contentspagenumber \advance\pageno by 1
\let\maybe=\iffalse
\def\title{TANGLE changes C}
 z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [1] Change banner message
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@d banner=='This is MAKEPROG, Version 1.0.1.'
@y
@d banner=='This is MAKEPROG, C Version 1.0.1.'
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [2] add input and output, remove other files, add ref to scan_args,
% [2]       and #include external definition for exit().
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@d end_of_MAKEPROG = 9999 {go here to wrap it up}

@p @t\4@>@<Compiler directives@>@/
program MAKEPROG(@!doc_file,@!change_file,@!prog_file);
label end_of_MAKEPROG; {go here to finish}
const @<Constants in the outer block@>@;
type @<Types in the outer block@>@;
var @<Globals in the outer block@>@;
@t\4@>@<Error handling procedures@>@;
@y
@d end_of_MAKEPROG = 9999 {go here to wrap it up}
@d eof==feof

@p @t\4@>@<Compiler directives@>@/
program MAKEPROG;
label end_of_MAKEPROG; {go here to finish}
const @<Constants in the outer block@>@;
type @<Types in the outer block@>@;
var @<Globals in the outer block@>@;
@t\4@>@<Error handling procedures@>@;
@<Declaration of |scan_args|@>@/
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [4] compiler options
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@{@&@=$D-@> @} {no debug overhead}
@!debug @{@&@=$D+@> @}@+ gubed @; {but turn everything on when debugging}
@y
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [17] Allow tabs in input and output files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
\MAKEPROG{} 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.

@<Set init...@>=
for i:=1 to " "-1 do xchr[i]:=' ';
@y
\MAKEPROG{} 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.
Here we add the tab character to the untranslated ones.
@<Set init...@>=
for i:=1 to " "-1 do xchr[i]:=' ';
xchr[tab_mark]:=chr(tab_mark);
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [20] terminal output: use standard i/o
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@d print(#)==write(term_out,#) {`|print|' means write on the terminal}
@y
@d term_out==stdout
@d print(#)==write(term_out,#) {`|print|' means write on the terminal}
@z

@x
@<Globals...@>=
@!term_out:text_file; {the terminal as an output file}
@y

@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [21] init terminal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
Different systems have different ways of specifying that the
output on a certain file will appear on the user's terminal.
Here is one way to do this on the \PASCAL{} system that was
used in \.{TANGLE}'s initial development.
@^system dependencies@>

@<Set init...@>=
rewrite(term_out,'TTY:'); {send |term_out| output to the terminal}
@y
Different systems have different ways of specifying that the output on a
certain file will appear on the user's terminal.
@^system dependencies@>

@<Set init...@>=
 {Nothing need be done C}
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [22] flush terminal buffer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@d update_terminal == break(term_out) {empty the terminal output buffer}
@y
@d update_terminal == flush(term_out) {empty the terminal output buffer}
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [24] open input files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
The following code opens the input 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.
@^system dependencies@>

@< Set init... @>=
reset(doc_file); reset(change_file);
@y
The following code opens the input files.
Use the |scan_args| procedure to fill the global file names,
according to the names given on the command line.
These globals, and the |scan_args| procedure will be defined at the end
where they won't disturb the module numbering.
@^system dependencies@>

@< Set init... @>=
scan_args;
reset(doc_file,doc_file_name); reset(change_file,change_file_name);
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [26] open output files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
The following code opens |prog_file|.  Since this file is
listed in the program header, we assume that the \PASCAL\
runtime system has checked that a suitable external file
name have been given.
@^system dependencies@>

@<Set init...@>=
rewrite(prog_file);
@y
The following code opens |prog_file|.
@^system dependencies@>

@<Set init...@>=
rewrite(prog_file,prog_file_name);
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fix f^
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
   if eof(f) then input_ln:=false
   else  begin
      while not eoln(f) do
	 begin buffer[limit]:=xord[f^]; get(f);
	 incr(limit);
	 if (buffer[limit-1]<>" ") and (buffer[limit-1]<>tab_mark) then
	    final_limit:=limit;
	 if limit=buf_size then
	    begin while not eoln(f) do get(f);
@y
if eof(f) then input_ln:=false
else  begin while not eoln(f) do
    begin buffer[limit]:=xord[getc(f)];
    incr(limit);
    if (buffer[limit-1]<>" ") and (buffer[limit-1]<>tab_mark) then
	 final_limit:=limit;
    if limit=buf_size then
      begin while not eoln(f) do vgetc(f);
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fix jump_out
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
@d fatal_error(#)==begin print_nl(#); error; mark_fatal; jump_out;
  end

@<Error handling...@>=
procedure jump_out;
begin goto end_of_MAKEPROG;
end;
@y
@d jump_out==uexit(1)
@d fatal_error(#)==begin new_line; print(#); error; mark_fatal; uexit(1);
  end
@z


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [182] write newline just before exit; use value of |history|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
\noindent We have defined some procedures, and it is time to
use them---here is where \MAKEPROG{} starts, and where it
ends.
@^system dependencies@>
@y
\noindent We have defined some procedures, and it is time to
use them---here is where \MAKEPROG{} starts, and where it
ends.
@^system dependencies@>

@d UNIXexit==e@&x@&i@&t
@z

@x
   @<Print the job |history|@>;
@y
   @<Print the job |history|@>;
   new_line;
   if (history <> spotless) and (history <> harmless_message) then
      UNIXexit(1)
   else
      UNIXexit(0);
@z

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [188] system dependent changes--the |scan_args| procedure.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@x
\noindent This module should be replaced, if necessary, by
changes to the program that are necessary to make
\MAKEPROG{} work at a particular installation.	It is
usually best to design your change file so that all changes
to previous modules preserve the module numbering; then
everybody's version will be consistent with the printed
program.  More extensive changes, which introduce new
modules, can be inserted here; then only the index itself
will get a new module number.
@^system dependencies@>
@y
@^system dependencies@>

The user calls \.{MAKEPROG} with arguments on the command line.
These are either file names or flags (beginning with '-').
The following globals are for communicating the user's desires to the rest
of the program. The various |file_name| variables contain strings with
the full names of those files, as UNIX knows them.

There are no flags that affect \.{MAKEPROG} at the moment.

@d max_file_name_length=120
@d max_extension=5

@<Globals...@>=
@!doc_file_name,@!change_file_name,@!prog_file_name:
        array[1..max_file_name_length] of char;
@!prog_extension: array[1..max_file_name_length] of char;

@ The |scan_args| procedure looks at the command line arguments and sets
the |file_name| variables accordingly.
At least one file name must be present: the \.{DOC} file.  It may have
an extension, or it may omit it to get |'.doc'| added.
The output file name is formed by replacing the \.{DOC} file
name extension by |'.prg'|.
The output file will be created in the current directory.

If there is another file name present among the arguments, it is the
change file, again either with an extension or without one to get |'.ch'|
An omitted change file argument means that |'/dev/null'| should be used,
when no changes are desired.

@<Declaration of |scan_args|@>=
procedure scan_args;
var doc_dot_pos,dot_pos,i,a,slash_pos: integer; {indices}
c: char;
@!fname: array[1..max_file_name_length] of char; {temporary argument holder}
@!found_doc,@!found_change: boolean; {|true| when those file names have
                                        been seen}
begin
prog_extension[1]:='p';
prog_extension[2]:='r';
prog_extension[3]:='o';
prog_extension[4]:='g';
prog_extension[5]:=' ';
found_doc:=false;
found_change:=false;
for a:=1 to argc-1 do begin
        argv(a,fname); {put argument number |a| into |fname|}
        if fname[1]<>'-' then begin
		if not found_doc then
			@<Get |doc_file_name| and |prog_file_name|
				variables from |fname|@>
                else if not found_change then
                        @<Get |change_file_name| from |fname|@>
                else  @<Print usage error message and quit@>;
                end
        else @<Handle flag argument in |fname|@>;
        end;
if not found_doc then @<Print usage error message and quit@>;
@<Add the proper extension@>;
if not found_change then @<Set up null change file@>;
end;

@ Use all of |fname| for the |doc_file_name| if there is a |'.'| in it,
otherwise add |'.doc'|.
The other file names come from adding things after the dot.
If the name contained directory specifications, we reset the
dot occurred flag and do not use the directory part of the name
for the output files.
The |argv| procedure will not put more than
|max_file_name_length-max_extension|
characters into |fname|, and this leaves enough room in the |file_name|
variables to add the extensions.

The end of a file name is marked with a |' '|, the convention assumed by 
the |reset| and |rewrite| procedures.

@<Get |doc_file_name|...@>=
begin
slash_pos:=0;
dot_pos:=-1;
i:=1;
while (fname[i]<>' ') and (i<=max_file_name_length-max_extension) do begin
	doc_file_name[i]:=fname[i];
	if fname[i]='.' then dot_pos:=i else
	if fname[i]='/' then begin
	   slash_pos:=i; dot_pos:=-1; {reset}
	end;
	incr(i);
        end;
if dot_pos=-1 then begin
        dot_pos:=i;
	doc_file_name[dot_pos]:='.';
	doc_file_name[dot_pos+1]:='d';
	doc_file_name[dot_pos+2]:='o';
	doc_file_name[dot_pos+3]:='c';
	doc_file_name[dot_pos+4]:=' ';
        end;
dot_pos:=dot_pos-slash_pos; {may be unchanged if no slash}
for i:=1 to dot_pos do begin
	prog_file_name[i]:=
	   doc_file_name[i+slash_pos];
        end;
doc_dot_pos:=dot_pos;
found_doc:=true;
end

@ This section must be called when the doc file was found
and the doc dot position is determined.
@<Add the proper extension@>=
begin
i:=1;
if prog_extension[1]=' ' then prog_file_name[dot_pos]:=' '
else
while prog_extension[i]<>' ' do
begin prog_file_name[doc_dot_pos+i]:=prog_extension[i];
	i:=i+1;
end;
end

@ @<Get |change_file_name|...@>=
begin
dot_pos:=-1;
i:=1;
while (fname[i]<>' ') and (i<=max_file_name_length-max_extension)
do begin
        change_file_name[i]:=fname[i];
	if fname[i]='.' then dot_pos:=i else
	if fname[i]='/' then dot_pos:=-1;
        incr(i);
        end;
if dot_pos=-1 then begin
        dot_pos:=i;
        change_file_name[dot_pos]:='.';
        change_file_name[dot_pos+1]:='c';
        change_file_name[dot_pos+2]:='h';
        change_file_name[dot_pos+3]:=' ';
        end;
found_change:=true;
end

@ @<Set up null...@>=
begin
        change_file_name[1]:='/';
        change_file_name[2]:='d';
        change_file_name[3]:='e';
        change_file_name[4]:='v';
        change_file_name[5]:='/';
        change_file_name[6]:='n';
        change_file_name[7]:='u';
        change_file_name[8]:='l';
        change_file_name[9]:='l';
        change_file_name[10]:=' ';
end

@ \.{MAKEPROG} accepts the intended extension of a created file as an
option to overwrite the default extension 'prog'.

@<Handle flag...@>=
begin
  i:=1;
  while (i<max_extension+1) and (fname[i+1]<>' ') do
  begin
	prog_extension[i]:=fname[i+1];
	i:=i+1;
  end;
  prog_extension[i]:=' ';
  if i>max_extension then
       @<Print usage error message and quit@>;
end

@ @<Print usage error message and quit@>=
begin
print_nl('! Usage: makeprog [-progext] docfile[.doc] [changefile[.ch]]');
error;
jump_out;
end

@z