Debian Codegraph @

texk/web2c/pktogf.ch

raw
1 % pktogf.ch for C compilation with web2c.
2 %
3 % 09/19/88 Pierre A. MacKay version 1.0.
4 % 12/02/89 Karl Berry cosmetic changes.
5 % 02/04/90 Karl new file-searching routines.
6 % (more recent changes in the ChangeLog)
7 %
8 % One major change in output format is incorporated by this change
9 % file. The local pktogf preamble comment is ignored and the
10 % dated METAFONT comment is passed through unaltered. This
11 % provides a continuous check on the origin of fonts in both
12 % gf and pk formats. PKtoGF runs silently unless it is given the
13 % -v switch in the command line.
14
15 @x [0] l.22
16 \def\title{PKtoGF}
17 @y
18 \def\title{PK$\,$\lowercase{to}$\,$GF changes for C}
19 @z
20
21 @x [0] WEAVE: print changes only
22 \pageno=\contentspagenumber \advance\pageno by 1
23 @y
24 \pageno=\contentspagenumber \advance\pageno by 1
25 \let\maybe=\iffalse
26 @z
27
28 @x [2] Define my_name
29 @d banner=='This is PKtoGF, Version 1.1'
30 @y
31 @d my_name=='pktogf'
32 @d banner=='This is PKtoGF, Version 1.1'
33 @z
34
35 @x [4] No global labels.
36 @ Both the input and output come from binary files. On line interaction
37 is handled through \PASCAL's standard |input| and |output| files.
38
39 @d print_ln(#)==write_ln(output,#)
40 @d print(#)==write(output,#)
41
42 @p program PKtoGF(input, output);
43 label @<Labels in the outer block@>@/
44 @y
45 @ Both the input and output come from binary files. On line
46 interaction is handled through \PASCAL's standard |input| and |output|
47 files. For C compilation terminal input and output is directed to
48 |stdin| and |stdout|. In this program there is no terminal input.
49 Since the terminal output is really not very interesting, it is
50 produced only when the \.{-v} command line flag is presented.
51
52 @d print_ln(#)==if verbose then write_ln(output, #)
53 @d print(#)==if verbose then write(output, #)
54
55 @p program PKtoGF(input, output);
56 @z
57
58 @x [still 4] Parse command line and initialize path searching.
59 procedure initialize; {this procedure gets things started properly}
60 var i:integer; {loop index for initializations}
61 begin print_ln(banner);@/
62 @y
63 @<Define \(|parse_arguments|@>
64 procedure initialize; {this procedure gets things started properly}
65 var i:integer; {loop index for initializations}
66 begin
67 kpse_set_program_name (argv[0], my_name);
68 kpse_init_prog ('PKTOGF', 0, nil, nil);
69 parse_arguments;
70 print_ln(banner);@/
71 @z
72
73 @x [5] Eliminate the |final_end| label
74 @ If the program has to stop prematurely, it goes to the
75 `|final_end|'.
76
77 @d final_end=9999 {label for the end of it all}
78
79 @<Labels...@>=final_end;
80 @y
81 @ This module is deleted, because it is only useful for
82 a non-local |goto|\unskip, which we don't use in C.
83 @z
84
85 % [6] Remove terminal_line_length, since there is no dialog, and
86 % name_length, since there is no maximum size.
87 @x [6]
88 @<Constants...@>=
89 @!name_length=80; {maximum length of a file name}
90 @!terminal_line_length=132; {maximum length of an input line}
91 @y
92 @<Constants...@>=
93 @z
94
95 @x [still 6] Dynamic allocation of |row_counts| array.
96 @!max_counts=400; {maximum number of run counts in a raster line}
97 @y
98 @!MAX_COUNTS=400; {initial number of run counts in a raster line}
99 @z
100
101 @x [8] Just exit, instead of doing a nonlocal goto.
102 nested procedure, so the procedure called |jump_out| has been added to transfer
103 to the very end of the program with an error message.
104
105 @d abort(#)==begin print_ln(' ',#); jump_out; end
106
107 @p procedure jump_out;
108 begin goto final_end;
109 end;
110 @y
111 nested procedure, so we might want to |abort|
112 the program with an error message.
113
114 @d abort(#)==begin verbose:=true; print_ln(#); uexit(1);
115 end
116 @z
117
118 @x [10] Pascal Web's char
119 @d text_char == char {the data type of characters in text files}
120 @y
121 @d char == 0..255
122 @d text_char == char {the data type of characters in text files}
123 @z
124
125 @x [30] Remove an unused variable (de-linting)
126 function pk_packed_num : integer ;
127 var i, j, k : integer ;
128 @y
129 function pk_packed_num : integer ;
130 var i, j : integer ;
131 @z
132
133 @x [40] Use path searching to open |pk_file|.
134 @p procedure open_gf_file; {prepares to write packed bytes in a |gf_file|}
135 begin rewrite(gf_file,gf_name);
136 gf_loc := 0 ;
137 end;
138 @#
139 procedure open_pk_file; {prepares the input for reading}
140 begin reset(pk_file,pk_name);
141 pk_loc := 0 ;
142 end;
143 @y
144 In C, we do path searching based on the user's environment or the
145 default path, via the Kpathsea library.
146
147 @p procedure open_pk_file; {prepares to read packed bytes in |pk_file|}
148 begin
149 {Don't use |kpse_find_pk|; we want the exact file or nothing.}
150 pk_name := cmdline (optind);
151 pk_file := kpse_open_file (cmdline (optind), kpse_pk_format);
152 if pk_file then begin
153 cur_loc := 0;
154 end;
155 end;
156 @#
157 procedure open_gf_file; {prepares to write packed bytes in |gf_file|}
158 begin
159 {If an explicit output filename isn't given, we construct it from |pk_name|.}
160 if optind + 1 = argc then begin
161 gf_name := basename_change_suffix (pk_name, 'pk', 'gf');
162 end else begin
163 gf_name := cmdline (optind + 1);
164 end;
165 rewritebin (gf_file, gf_name);
166 gf_loc := 0;
167 end;
168 @z
169
170 @x [41] No arbitrary limit on filename length.
171 @ We need a place to store the names of the input and output files, as well
172 as a byte counter for the output file.
173
174 @<Glob...@>=
175 @!gf_name,@!pk_name:packed array[1..name_length] of char; {names of input
176 and output files}
177 @y
178 @ No arbitrary limit on filename length.
179
180 @<Glob...@>=
181 @!gf_name,@!pk_name:c_string; {names of input and output files}
182 @z
183
184 @x [42] Define gf_byte (in place of Pascal procedure).
185 @ We need a procedure that will write a byte to the \.{GF} file. If the
186 particular system
187 @^system dependencies@>
188 requires buffering, here is the place to do it.
189
190 @p procedure gf_byte (i : integer) ;
191 begin gf_file^ := i ;
192 put(gf_file) ;
193 incr(gf_loc) ;
194 end;
195 @y
196 @ Byte output is handled by a C definition.
197
198 @d gf_byte(#)==begin put_byte(#, gf_file); incr(gf_loc) end
199 @z
200
201 @x [43] Use the |get_byte| routines from dvitype (renamed).
202 @ We also need a function that will get a single byte from the \.{PK} file.
203 Again, buffering may be done in this procedure.
204
205 @p function pk_byte : eight_bits ;
206 var nybble, temp : eight_bits ;
207 begin
208 temp := pk_file^ ;
209 get(pk_file) ;
210 pk_loc := pk_loc + 1 ;
211 pk_byte := temp ;
212 end ;
213 @y
214 @ We shall use a set of simple functions to read the next byte or
215 bytes from |pk_file|. There are seven possibilities, each of which is
216 treated as a separate function in order to minimize the overhead for
217 subroutine calls.
218 @^system dependencies@>
219
220 @d pk_byte==get_byte
221 @d pk_loc==cur_loc
222
223 @p function get_byte:integer; {returns the next byte, unsigned}
224 var b:eight_bits;
225 begin if eof(pk_file) then get_byte:=0
226 else begin read(pk_file,b); incr(cur_loc); get_byte:=b;
227 end;
228 end;
229 @#
230 function signed_byte:integer; {returns the next byte, signed}
231 var b:eight_bits;
232 begin read(pk_file,b); incr(cur_loc);
233 if b<128 then signed_byte:=b @+ else signed_byte:=b-256;
234 end;
235 @#
236 function get_two_bytes:integer; {returns the next two bytes, unsigned}
237 var a,@!b:eight_bits;
238 begin read(pk_file,a); read(pk_file,b);
239 cur_loc:=cur_loc+2;
240 get_two_bytes:=a*256+b;
241 end;
242 @#
243 function signed_pair:integer; {returns the next two bytes, signed}
244 var a,@!b:eight_bits;
245 begin read(pk_file,a); read(pk_file,b);
246 cur_loc:=cur_loc+2;
247 if a<128 then signed_pair:=a*256+b
248 else signed_pair:=(a-256)*256+b;
249 end;
250 @#
251 @{
252 function get_three_bytes:integer; {returns the next three bytes, unsigned}
253 var a,@!b,@!c:eight_bits;
254 begin read(pk_file,a); read(pk_file,b); read(pk_file,c);
255 cur_loc:=cur_loc+3;
256 get_three_bytes:=(a*256+b)*256+c;
257 end;
258 @{
259 @/
260 @}
261 function signed_trio:integer; {returns the next three bytes, signed}
262 var a,@!b,@!c:eight_bits;
263 begin read(pk_file,a); read(pk_file,b); read(pk_file,c);
264 cur_loc:=cur_loc+3;
265 if a<128 then signed_trio:=(a*256+b)*256+c
266 else signed_trio:=((a-256)*256+b)*256+c;
267 end;
268 @}
269 @#
270 function signed_quad:integer; {returns the next four bytes, signed}
271 var a,@!b,@!c,@!d:eight_bits;
272 begin read(pk_file,a); read(pk_file,b); read(pk_file,c); read(pk_file,d);
273 cur_loc:=cur_loc+4;
274 if a<128 then signed_quad:=((a*256+b)*256+c)*256+d
275 else signed_quad:=(((a-256)*256+b)*256+c)*256+d;
276 end;
277 @z
278
279 @x [45] Adapt the DVItype functions.
280 @ As we are reading the packed file, we often need to fetch 16 and 32 bit
281 quantities. Here we have two procedures to do this.
282
283 @p function signed_byte : integer ;
284 var a : integer ;
285 begin
286 a := pk_byte ;
287 if a > 127 then
288 a := a - 256 ;
289 signed_byte := a ;
290 end ;
291 @#
292 function get_16 : integer ;
293 var a : integer ;
294 begin
295 a := pk_byte ;
296 get_16 := a * 256 + pk_byte ;
297 end ;
298 @#
299 function signed_16 : integer ;
300 var a : integer ;
301 begin
302 a := signed_byte ;
303 signed_16 := a * 256 + pk_byte ;
304 end ;
305 @#
306 function get_32 : integer ;
307 var a : integer ;
308 begin
309 a := get_16 ;
310 if a > 32767 then a := a - 65536 ;
311 get_32 := a * 65536 + get_16 ;
312 end ;
313 @y
314 @ We put definitions here to access the \.{DVItype} functions supplied
315 above. (|signed_byte| is already taken care of).
316
317 @d get_16==get_two_bytes
318 @d signed_16==signed_pair
319 @d get_32==signed_quad
320 @z
321
322 @x [46] Remove unused function gf_sbyte.
323 @p procedure gf_sbyte(i : integer) ;
324 begin
325 if i < 0 then
326 i := i + 256 ;
327 gf_byte(i) ;
328 end ;
329 @#
330 procedure gf_16(i : integer) ;
331 @y
332 @p procedure gf_16(i : integer) ;
333 @z
334
335 @x [46] Fix unlikely but possible overflow in addition in gf_quad.
336 i := (i + 1073741824) + 1073741824 ;
337 @y
338 {|i<0| at this point, but a compiler is permitted to rearrange the
339 order of the additions, which would cause wrong results
340 in the unlikely event of a non-2's-complement representation.}
341 i := i + 1073741824;
342 i := i + 1073741824;
343 @z
344
345 @x [49] Preserve the METAFONT comment.
346 j := pk_byte ;
347 for i := 1 to j do hppp := pk_byte ;
348 gf_byte(comm_length) ;
349 for i := 1 to comm_length do
350 gf_byte(xord[comment[i]]) ;
351 @y
352 j := pk_byte ;
353 gf_byte(j) ;
354 print('{') ;
355 for i := 1 to j do begin
356 hppp:=pk_byte;
357 gf_byte(hppp) ;
358 print(xchr[xord[hppp]]);
359 end;
360 print_ln('}') ;
361 @z
362
363 @x [51] Dynamic allocation of |row_counts| array.
364 @ @<Set init...@>=
365 @y
366 @ @<Set init...@>=
367 row_counts := xmalloc_array (integer, MAX_COUNTS);
368 max_counts := MAX_COUNTS;
369 @z
370
371 @x [still 51] Since we preserve the METAFONT comment, this is unneeded.
372 comment := preamble_comment ;
373 @y
374 @z
375
376 @x [63] Remove unused variable.
377 @!nybble : eight_bits ; {the current nybble}
378 @y
379 @z
380
381 @x [still 63] Dynamic allocation of |row_counts| array.
382 @!row_counts : array [0..max_counts] of integer ;
383 @y
384 @!max_counts : integer ;
385 @!row_counts : ^integer ;
386 @z
387
388 @x [65] Dynamic allocation of |row_counts| array.
389 print_ln('A character had too many run counts') ;
390 jump_out ;
391 @y
392 print_ln('Reallocated row_counts array to ', (max_counts+MAX_COUNTS):1,
393 ' items from ', max_counts:1, '.');
394 max_counts := max_counts + MAX_COUNTS;
395 row_counts := xrealloc_array (row_counts, integer, max_counts);
396 @z
397
398 @x [71] There is no terminal communication.
399 @* Terminal communication.
400 We must get the file names and determine whether input is to be in
401 hexadecimal or binary. To do this, we use the standard input path
402 name. We need a procedure to flush the input buffer. For most systems,
403 this will be an empty statement. For other systems, a |print_ln| will
404 provide a quick fix. We also need a routine to get a line of input from
405 the terminal. On some systems, a simple |read_ln| will do. Finally,
406 a macro to print a string to the first blank is required.
407
408 @d flush_buffer == begin end
409 @d get_line(#) == if eoln(input) then read_ln(input) ;
410 i := 1 ;
411 while not (eoln(input) or eof(input)) do begin
412 #[i] := input^ ;
413 incr(i) ;
414 get(input) ;
415 end ;
416 #[i] := ' '
417 @y
418 @* Terminal communication.
419 Since this program runs entirely on command-line arguments, there
420 is no terminal communication.
421 @z
422
423 @x [72] There is no dialog.
424 @ @p procedure dialog ;
425 var i : integer ; {index variable}
426 buffer : packed array [1..name_length] of char; {input buffer}
427 begin
428 for i := 1 to name_length do begin
429 gf_name[i] := ' ' ;
430 pk_name[i] := ' ' ;
431 end;
432 print('Input file name: ') ;
433 flush_buffer ;
434 get_line(pk_name) ;
435 print('Output file name: ') ;
436 flush_buffer ;
437 get_line(gf_name) ;
438 end ;
439 @y
440 @ \.{pktogf.web} has a |dialog| procedure here.
441 @z
442
443 @x [73] There is no dialog and no |final_end| label.
444 dialog ;
445 @y
446 @z
447 @x [73]
448 final_end :
449 @y
450 @z
451
452 @x [74] System-dependent changes.
453 This section should be replaced, if necessary, by changes to the program
454 that are necessary to make \.{PKtoGF} work at a particular installation.
455 Any additional routines should be inserted here.
456 @^system dependencies@>
457 @y
458 Parse a Unix-style command line.
459
460 @d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0)
461
462 @<Define \(|parse_arguments|@> =
463 procedure parse_arguments;
464 const n_options = 3; {Pascal won't count array lengths for us.}
465 var @!long_options: array[0..n_options] of getopt_struct;
466 @!getopt_return_val: integer;
467 @!option_index: c_int_type;
468 @!current_option: 0..n_options;
469 begin
470 @<Initialize the option variables@>;
471 @<Define the option table@>;
472 repeat
473 getopt_return_val := getopt_long_only (argc, argv, '', long_options,
474 address_of (option_index));
475 if getopt_return_val = -1 then begin
476 do_nothing; {End of arguments; we exit the loop below.}
477
478 end else if getopt_return_val = "?" then begin
479 usage (my_name);
480
481 end else if argument_is ('help') then begin
482 usage_help (PKTOGF_HELP, nil);
483
484 end else if argument_is ('version') then begin
485 print_version_and_exit (banner, nil, 'Tomas Rokicki', nil);
486
487 end; {Else it was a flag; |getopt| has already done the assignment.}
488 until getopt_return_val = -1;
489
490 {Now |optind| is the index of first non-option on the command line.
491 We must have one or two remaining arguments.}
492 if (optind + 1 <> argc) and (optind + 2 <> argc) then begin
493 write_ln (stderr, my_name, ': Need one or two file arguments.');
494 usage (my_name);
495 end;
496 end;
497
498 @ Here are the options we allow. The first is one of the standard GNU options.
499 @.-help@>
500
501 @<Define the option...@> =
502 current_option := 0;
503 long_options[current_option].name := 'help';
504 long_options[current_option].has_arg := 0;
505 long_options[current_option].flag := 0;
506 long_options[current_option].val := 0;
507 incr (current_option);
508
509 @ Another of the standard options.
510 @.-version@>
511
512 @<Define the option...@> =
513 long_options[current_option].name := 'version';
514 long_options[current_option].has_arg := 0;
515 long_options[current_option].flag := 0;
516 long_options[current_option].val := 0;
517 incr (current_option);
518
519 @ Print progress information?
520
521 @<Define the option...@> =
522 long_options[current_option].name := 'verbose';
523 long_options[current_option].has_arg := 0;
524 long_options[current_option].flag := address_of (verbose);
525 long_options[current_option].val := 1;
526 incr (current_option);
527
528 @ @<Glob...@> =
529 @!verbose: c_int_type;
530
531 @ @<Initialize the option...@> =
532 verbose := false;
533
534 @ An element with all zeros always ends the list.
535
536 @<Define the option...@> =
537 long_options[current_option].name := 0;
538 long_options[current_option].has_arg := 0;
539 long_options[current_option].flag := 0;
540 long_options[current_option].val := 0;
541 @z