Debian Codegraph @

texk/web2c/gftype.ch

raw
1 % gftype.ch for C compilation with web2c.
2 %
3 % 09/27/88 Pierre A. MacKay version 2.2.
4 % 11/10/88 (PAM) Bugs reported by Irwin Meisels.
5 % Corrected floating-point printout, and restored options
6 % info printout to make trap look better.
7 % 12/02/89 Karl Berry version 3.
8 % (more recent changes in the ChangeLog)
9 %
10 % The C version of GFtype uses command line switches to
11 % request mnemonic ouput or pixel image output.
12 % The default is a restrained output which merely assures you
13 % that all characters are there and reports their position, tfm_width
14 % and escapement. The -m switch turns on mnemonics, the -i switch
15 % turns on images. There is no terminal input to this program.
16 % Output is to stdout, and may, of course, be redirected.
17
18 @x [0] l.27
19 \def\(#1){} % this is used to make section names sort themselves better
20 @y
21 \def\({} % this is used to make section names sort themselves better
22 @z
23
24 @x [0] l.30
25 \def\title{GFtype}
26 @y
27 \def\title{GF$\,$\lowercase{type} changes for C}
28 @z
29
30 @x [0] WEAVE: print changes only.
31 \pageno=\contentspagenumber \advance\pageno by 1
32 @y
33 \pageno=\contentspagenumber \advance\pageno by 1
34 \let\maybe=\iffalse
35 @z
36
37 @x [1] Define my_name
38 @d banner=='This is GFtype, Version 3.1' {printed when the program starts}
39 @y
40 @d my_name=='gftype'
41 @d banner=='This is GFtype, Version 3.1' {printed when the program starts}
42 @z
43
44 @x [3] No global labels.
45 @d print(#)==write(#)
46 @d print_ln(#)==write_ln(#)
47 @d print_nl==write_ln
48
49 @p program GF_type(@!gf_file,@!output);
50 label @<Labels in the outer block@>@/
51 const @<Constants in the outer block@>@/
52 type @<Types in the outer block@>@/
53 var @<Globals in the outer block@>@/
54 procedure initialize; {this procedure gets things started properly}
55 var i:integer; {loop index for initializations}
56 begin print_ln(banner);@/
57 @y
58 @d print(#)==write(stdout, #)
59 @d print_ln(#)==write_ln(stdout, #)
60 @d print_nl==write_ln(stdout)
61
62 @p program GF_type(@!gf_file,@!output);
63 const @<Constants in the outer block@>@/
64 type @<Types in the outer block@>@/
65 var @<Globals in the outer block@>@/
66 @<Define \(|parse_arguments|@>
67 procedure initialize; {this procedure gets things started properly}
68 var i:integer; {loop index for initializations}
69 @!bound_default:integer; {temporary for setup}
70 @!bound_name:const_cstring; {temporary for setup}
71 begin
72 kpse_set_program_name (argv[0], my_name);
73 kpse_init_prog ('GFTYPE', 0, nil, nil);
74 parse_arguments;
75 print(banner);print_ln(version_string);
76 @z
77
78 @x [4] Eliminate the |final_end| label.
79 @ If the program has to stop prematurely, it goes to the
80 `|final_end|'.
81
82 @d final_end=9999 {label for the end of it all}
83
84 @<Labels...@>=final_end;
85 @y
86 @ This module is deleted, because it is only useful for
87 a non-local |goto|\unskip, which we can't use in C.
88
89 Instead, we define parameters settable at runtime.
90
91 @<Glob...@>=
92 @!line_length:integer; {\\{xxx} strings will not produce lines longer than this}
93 @!max_rows:integer; {largest possible vertical extent of pixel image array}
94 @!max_cols:integer; {largest possible horizontal extent of pixel image array}
95 @!max_row:integer; {current vertical extent of pixel image array}
96 @!max_col:integer; {current horizontal extent of pixel image array}
97 @z
98
99 @x [5] Parameters settable at runtime.
100 @ Four parameters can be changed at compile time to extend or
101 @y
102 @ Three parameters can be changed at run time to extend or
103 @z
104
105 @x [5] Remove |terminal_line_length|, increase others.
106 @<Constants...@>=
107 @!terminal_line_length=150; {maximum number of characters input in a single
108 line of input from the terminal}
109 @!line_length=79; {\\{xxx} strings will not produce lines longer than this}
110 @!max_row=79; {vertical extent of pixel image array}
111 @!max_col=79; {horizontal extent of pixel image array}
112 @y
113 @d def_line_length = 500 {default |line_length| value}
114 @d max_image = 8191 {largest possible extent of \MF's pixel image array}
115
116 @<Constants...@>=
117 @!inf_line_length=20;
118 @!sup_line_length=1023;
119 @z
120
121 @x [6] Parameters settable at runtime, dynamic allocation.
122 @d negate(#) == #:=-# {change the sign of a variable}
123 @y
124 @d negate(#) == #:=-# {change the sign of a variable}
125 @#
126 @d const_chk(#) == begin if # < inf@&# then # := inf@&# else
127 if # > sup@&# then # := sup@&# end
128 {|setup_bound_var| stuff duplicated in \.{tex.ch}.}
129 @d setup_bound_var(#) == bound_default := #; setup_bound_var_end
130 @d setup_bound_var_end(#) == bound_name := #; setup_bound_var_end_end
131 @d setup_bound_var_end_end(#) ==
132 setup_bound_variable (address_of (#), bound_name, bound_default);
133
134 @<Set init...@>=
135 {See comments in \.{tex.ch} for why the name has to be duplicated.}
136 setup_bound_var (def_line_length)('line_length')(line_length);
137 {\\{xxx} strings will not produce lines longer than this}
138 setup_bound_var (max_image)('max_rows')(max_rows);
139 {largest allowed vertical extent of pixel image array}
140 setup_bound_var (max_image)('max_cols')(max_cols);
141 {largest allowed horizontal extent of pixel image array}
142 const_chk (line_length);
143 if max_rows > max_image then max_rows := max_image;
144 if max_cols > max_image then max_cols := max_image;
145 image_array := nil;
146 @z
147
148 @x [7] Purge 'final_end' label.
149 Such errors might be discovered inside of subroutines inside of subroutines,
150 so a procedure called |jump_out| has been introduced. This procedure, which
151 simply transfers control to the label |final_end| at the end of the program,
152 contains the only non-local |goto| statement in \.{GFtype}.
153 @y
154 Such errors might be discovered inside of subroutines inside of subroutines,
155 so we might want to |abort| the program with an error message.
156 @z
157
158 @x [7] Remove jump_out, and make `abort' end with a newline.
159 @d abort(#)==begin print(' ',#); jump_out;
160 end
161 @d bad_gf(#)==abort('Bad GF file: ',#,'!')
162 @.Bad GF file@>
163
164 @p procedure jump_out;
165 begin goto final_end;
166 end;
167 @y
168 @d abort(#)==begin write_ln(stderr, #); uexit(1); end
169 @d bad_gf(#)==abort('Bad GF file: ',#,'!')
170 @.Bad GF file@>
171 @z
172
173 @x [9] Pascal Web's char
174 @d text_char == char {the data type of characters in text files}
175 @y
176 @d char == 0..255
177 @d text_char == char {the data type of characters in text files}
178 @z
179
180 @x [22] Redo open_gf_file to do path searching.
181 @ To prepare this file for input, we |reset| it.
182
183 @p procedure open_gf_file; {prepares to read packed bytes in |gf_file|}
184 begin reset(gf_file);
185 cur_loc:=0;
186 end;
187 @y
188
189 @ In C, we do path searching based on the user's environment or the
190 default path.
191
192 @p procedure open_gf_file; {prepares to read packed bytes in |gf_file|}
193 begin
194 gf_file := kpse_open_file (cmdline (optind), kpse_gf_format);
195 cur_loc := 0;
196 @<Print all the selected options@>;
197 end;
198 @z
199
200 @x [25] Since we set these from getopt, they have to be ints, more's the pity.
201 @!wants_mnemonics: boolean; {controls mnemonic output}
202 @!wants_pixels: boolean; {controls pixel output}
203 @y
204 @!wants_mnemonics: c_int_type; {controls mnemonic output}
205 @!wants_pixels: c_int_type; {controls pixel output}
206 @z
207
208 % [26] Initialize wants_pixels and wants_mnemonics to false implicitly
209 % by virtue of being global variables. Can't do it here, since this is
210 % after the option parsing.
211 @x [26]
212 wants_mnemonics:=true; wants_pixels:=true;
213 @y
214 @z
215
216 @x [27] No input, and no dialog.
217 @ The |input_ln| routine waits for the user to type a line at his or her
218 terminal; then it puts ASCII-code equivalents for the characters on that line
219 into the |buffer| array. The |term_in| file is used for terminal input,
220 and |term_out| for terminal output.
221 @^system dependencies@>
222
223 @<Glob...@>=
224 @!buffer:array[0..terminal_line_length] of ASCII_code;
225 @!term_in:text_file; {the terminal, considered as an input file}
226 @!term_out:text_file; {the terminal, considered as an output file}
227 @y
228 @ There is no terminal input. The options for running this
229 program are offered through command line options.
230 @z
231
232 @x [29] `input_ln' is not needed.
233 @p procedure input_ln; {inputs a line from the terminal}
234 var k:0..terminal_line_length;
235 begin update_terminal; reset(term_in);
236 if eoln(term_in) then read_ln(term_in);
237 k:=0;
238 while (k<terminal_line_length)and not eoln(term_in) do
239 begin buffer[k]:=xord[term_in^]; incr(k); get(term_in);
240 end;
241 buffer[k]:=" ";
242 end;
243 @y
244 @z
245
246 @x [30--34] Eliminate `dialog' and its friends.
247 @ This is humdrum.
248
249 @p function lower_casify(@!c:ASCII_code):ASCII_code;
250 begin
251 if (c>="A") and (c<="Z") then lower_casify:=c+"a"-"A"
252 else lower_casify:=c;
253 end;
254
255 @ The selected options are put into global variables by the |dialog|
256 procedure, which is called just as \.{GFtype} begins.
257 @^system dependencies@>
258
259 @p procedure dialog;
260 label 1,2;
261 begin rewrite(term_out); {prepare the terminal for output}
262 write_ln(term_out,banner);@/
263 @<Determine whether the user |wants_mnemonics|@>;
264 @<Determine whether the user |wants_pixels|@>;
265 @<Print all the selected options@>;
266 end;
267
268 @ @<Determine whether the user |wants_mnemonics|@>=
269 1: write(term_out,'Mnemonic output? (default=no, ? for help): ');
270 @.Mnemonic output?@>
271 input_ln;
272 buffer[0]:=lower_casify(buffer[0]);
273 if buffer[0]<>"?" then
274 wants_mnemonics:=(buffer[0]="y")or(buffer[0]="1")or(buffer[0]="t")
275 else begin write(term_out,'Type Y for complete listing,');
276 write_ln(term_out,' N for errors/images only.');
277 goto 1;
278 end
279
280 @ @<Determine whether the user |wants_pixels|@>=
281 2: write(term_out,'Pixel output? (default=yes, ? for help): ');
282 @.Pixel output?@>
283 input_ln;
284 buffer[0]:=lower_casify(buffer[0]);
285 if buffer[0]<>"?" then
286 wants_pixels:=(buffer[0]="y")or(buffer[0]="1")or(buffer[0]="t")
287 or(buffer[0]=" ")
288 else begin write(term_out,'Type Y to list characters pictorially');
289 write_ln(term_out,' with *''s, N to omit this option.');
290 goto 2;
291 end
292
293 @ After the dialog is over, we print the options so that the user
294 can see what \.{GFtype} thought was specified.
295
296 @y
297 @ This was so humdrum that we got rid of it. (module 30)
298
299 @ The dialog procedure module is eliminated. (module 31)
300
301 @ So is its first part. (module 32)
302
303 @ So is its second part. (module 33)
304
305 @ After the command-line switches have been processed,
306 we print the options so that the user
307 can see what \.{GFtype} thought was specified.
308 @z
309
310 @x [37] Dynamic allocation.
311 @d image==image_array[m,n]
312
313 @<Glob...@>=
314 @!image_array: packed array [0..max_col,0..max_row] of pixel;
315 @y
316 @d image==image_array[m + (max_col + 1) * n]
317
318 @<Glob...@>=
319 @!image_array:^pixel;
320 @z
321
322 @x [38] Dynamic allocation.
323 begin max_subcol:=max_m_stated-min_m_stated-1;
324 if max_subcol>max_col then max_subcol:=max_col;
325 max_subrow:=max_n_stated-min_n_stated;
326 if max_subrow>max_row then max_subrow:=max_row;
327 n:=0;
328 while n<=max_subrow do
329 begin m:=0;
330 while m<=max_subcol do
331 begin image:=white; incr(m);
332 end;
333 incr(n);
334 end;
335 end
336 @y
337 begin max_col:=max_m_stated-min_m_stated-1;
338 if max_col>max_cols then max_col:=max_cols;
339 max_row:=max_n_stated-min_n_stated;
340 if max_row>max_rows then max_row:=max_rows;
341 if (max_row >= 0) and (max_col >= 0) then
342 image_array := xcalloc_array (pixel, max_col, max_row);
343 end
344 @z
345
346 @x [39] Dynamic allocation.
347 @ @<Glob...@>=
348 @!max_subrow,@!max_subcol:integer; {size of current subarray of interest}
349 @y
350 @ With |image_array| allocated dynamically these are the same.
351
352 @d max_subrow == max_row {vertical size of current subarray of interest}
353 @d max_subcol == max_col {horizontal size of current subarray of interest}
354 @z
355
356 @x [40] Dynamic allocation.
357 else print_ln('(The character is entirely blank.)');
358 @y
359 else print_ln('(The character is entirely blank.)');
360 if (max_row >= 0) and (max_col >= 0) then
361 begin libc_free (image_array);
362 image_array := nil;
363 end;
364 @z
365
366 @x [45] Change chr to xchr (should be changed in the web source).
367 print(chr(ord('0')+(s div unity))); s:=10*(s mod unity); delta:=delta*10;
368 @y
369 print(xchr[ord('0')+(s div unity)]); s:=10*(s mod unity); delta:=delta*10;
370 @z
371
372 @x [48] Break up the first oversized case statement (or yacc breaks).
373 sixty_four_cases(new_row_0), sixty_four_cases(new_row_0+64),
374 thirty_seven_cases(new_row_0+128): first_par:=o-new_row_0;
375 @y
376 sixty_four_cases(new_row_0): first_par:=o-new_row_0;
377 sixty_four_cases(new_row_0+64): first_par:=o-new_row_0;
378 thirty_seven_cases(new_row_0+128): first_par:=o-new_row_0;
379 @z
380
381 @x [48] Diagnose impossible cases.
382 end;
383 @y
384 othercases abort('internal error')
385 endcases;
386 @z
387
388 @x [51] Break up the second oversized case statement.
389 sixty_four_cases(new_row_0), sixty_four_cases(new_row_0+64),
390 thirty_seven_cases(new_row_0+128):
391 @<Translate a |new_row| command@>;
392 @t\4@>@<Cases for commands |no_op|, |pre|, |post|, |post_post|, |boc|,
393 and |eoc|@>@;
394 four_cases(xxx1): @<Translate an |xxx| command@>;
395 yyy: @<Translate a |yyy| command@>;
396 othercases error('undefined command ',o:1,'!')
397 @.undefined command@>
398 endcases
399 @y
400 sixty_four_cases(new_row_0): @<Translate a |new_row| command@>;
401 sixty_four_cases(new_row_0+64): @<Translate a |new_row| command@>;
402 thirty_seven_cases(new_row_0+128): @<Translate a |new_row| command@>;
403 @t\4@>@<Cases for commands |no_op|, |pre|, |post|, |post_post|, |boc|,
404 and |eoc|@>@;
405 four_cases(xxx1): @<Translate an |xxx| command@>;
406 yyy: @<Translate a |yyy| command@>;
407 othercases error('undefined command ',o:1,'!')
408 @.undefined command@>
409 endcases
410 @z
411
412 @x [66] No label and no dialog; finish last line written.
413 @p begin initialize; {get all variables initialized}
414 dialog; {set up all the options}
415 @<Process the preamble@>;
416 @<Translate all the characters@>;
417 print_nl;
418 read_postamble;
419 print('The file had ',total_chars:1,' character');
420 if total_chars<>1 then print('s');
421 print(' altogether.');
422 @.The file had n characters...@>
423 final_end:end.
424 @y
425 @p begin initialize; {get all variables initialized}
426 @<Process the preamble@>;
427 @<Translate all the characters@>;
428 print_nl;
429 read_postamble;
430 print('The file had ',total_chars:1,' character');
431 if total_chars<>1 then print('s');
432 print_ln(' altogether.');
433 @.The file had n characters...@>
434 end.
435 @z
436
437 @x [73] System-dependent changes.
438 This section should be replaced, if necessary, by changes to the program
439 that are necessary to make \.{GFtype} work at a particular installation.
440 It is usually best to design your change file so that all changes to
441 previous sections preserve the section numbering; then everybody's version
442 will be consistent with the printed program. More extensive changes,
443 which introduce new sections, can be inserted here; then only the index
444 itself will get a new section number.
445 @^system dependencies@>
446 @y
447 Parse a Unix-style command line.
448
449 @d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0)
450 @d do_nothing == {empty statement}
451
452 @<Define \(|parse_arguments|@> =
453 procedure parse_arguments;
454 const n_options = 4; {Pascal won't count array lengths for us.}
455 var @!long_options: array[0..n_options] of getopt_struct;
456 @!getopt_return_val: integer;
457 @!option_index: c_int_type;
458 @!current_option: 0..n_options;
459 begin
460 @<Define the option table@>;
461 repeat
462 getopt_return_val := getopt_long_only (argc, argv, '', long_options,
463 address_of (option_index));
464 if getopt_return_val = -1 then begin
465 do_nothing; {End of arguments; we exit the loop below.}
466
467 end else if getopt_return_val = "?" then begin
468 usage (my_name);
469
470 end else if argument_is ('help') then begin
471 usage_help (GFTYPE_HELP, nil);
472
473 end else if argument_is ('version') then begin
474 print_version_and_exit (banner, nil, 'D.R. Fuchs', nil);
475
476 end; {Else it was a flag.}
477 until getopt_return_val = -1;
478
479 {Now |optind| is the index of first non-option on the command line.
480 We must have one remaining argument.}
481 if (optind + 1 <> argc) then begin
482 write_ln (stderr, my_name, ': Need exactly one file argument.');
483 usage (my_name);
484 end;
485 end;
486
487 @ Here are the options we allow. The first is one of the standard GNU options.
488 @.-help@>
489
490 @<Define the option...@> =
491 current_option := 0;
492 long_options[current_option].name := 'help';
493 long_options[current_option].has_arg := 0;
494 long_options[current_option].flag := 0;
495 long_options[current_option].val := 0;
496 incr (current_option);
497
498 @ Another of the standard options.
499 @.-version@>
500
501 @<Define the option...@> =
502 long_options[current_option].name := 'version';
503 long_options[current_option].has_arg := 0;
504 long_options[current_option].flag := 0;
505 long_options[current_option].val := 0;
506 incr (current_option);
507
508 @ Translate commands?
509 @<Define the option...@> =
510 long_options[current_option].name := 'mnemonics';
511 long_options[current_option].has_arg := 0;
512 long_options[current_option].flag := address_of (wants_mnemonics);
513 long_options[current_option].val := 1;
514 incr (current_option);
515
516 @ Show pixels?
517 @<Define the option...@> =
518 long_options[current_option].name := 'images';
519 long_options[current_option].has_arg := 0;
520 long_options[current_option].flag := address_of (wants_pixels);
521 long_options[current_option].val := 1;
522 incr (current_option);
523
524 @ An element with all zeros always ends the list.
525
526 @<Define the option...@> =
527 long_options[current_option].name := 0;
528 long_options[current_option].has_arg := 0;
529 long_options[current_option].flag := 0;
530 long_options[current_option].val := 0;
531 @z