| 1 |
% pktype.ch for C compilation with web2c. |
| 2 |
% |
| 3 |
% 09/27/88 Pierre A. MacKay Version 2.2. |
| 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 |
% There is no terminal input to this program. |
| 9 |
% Output is to stdout, and may, of course, be redirected. |
| 10 |
|
| 11 |
@x [0] l.25 |
| 12 |
\def\(#1){} % this is used to make section names sort themselves better |
| 13 |
@y |
| 14 |
\def\({} % this is used to make section names sort themselves better |
| 15 |
@z |
| 16 |
|
| 17 |
@x [0] l.27 |
| 18 |
\def\title{PKtype} |
| 19 |
@y |
| 20 |
\def\title{PK$\,$\lowercase{type} changes for C} |
| 21 |
@z |
| 22 |
|
| 23 |
@x [0] WEAVE: print changes only. |
| 24 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 25 |
@y |
| 26 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 27 |
\let\maybe=\iffalse |
| 28 |
@z |
| 29 |
|
| 30 |
@x [2] Define my_name |
| 31 |
@d banner=='This is PKtype, Version 2.3' {printed when the program starts} |
| 32 |
@y |
| 33 |
@d my_name=='pktype' |
| 34 |
@d banner=='This is PKtype, Version 2.3' {printed when the program starts} |
| 35 |
@z |
| 36 |
|
| 37 |
@x [4] Redirect output to stdout. |
| 38 |
@d t_print_ln(#)==write_ln(typ_file,#) |
| 39 |
@y |
| 40 |
@d typ_file==stdout |
| 41 |
@d t_print_ln(#)==write_ln(typ_file,#) |
| 42 |
@z |
| 43 |
|
| 44 |
@x [4] No global labels or constants. |
| 45 |
label @<Labels in the outer block@>@/ |
| 46 |
const @<Constants in the outer block@>@/ |
| 47 |
type @<Types in the outer block@>@/ |
| 48 |
var @<Globals in the outer block@>@/ |
| 49 |
procedure initialize; {this procedure gets things started properly} |
| 50 |
var i:integer; {loop index for initializations} |
| 51 |
begin print_ln(banner);@/ |
| 52 |
@y |
| 53 |
type @<Types in the outer block@>@/ |
| 54 |
var @<Globals in the outer block@>@/ |
| 55 |
@<Define \(|parse_arguments|@> |
| 56 |
procedure initialize; {this procedure gets things started properly} |
| 57 |
var i:integer; {loop index for initializations} |
| 58 |
begin |
| 59 |
kpse_set_program_name (argv[0], my_name); |
| 60 |
kpse_init_prog ('PKTYPE', 0, nil, nil); |
| 61 |
parse_arguments; |
| 62 |
print(banner); print_ln(version_string);@/ |
| 63 |
@z |
| 64 |
|
| 65 |
@x [5] Remove the unused label. |
| 66 |
@ If the program has to stop prematurely, it goes to the |
| 67 |
`|final_end|'. |
| 68 |
|
| 69 |
@d final_end=9999 {label for the end of it all} |
| 70 |
|
| 71 |
@<Labels...@>=final_end; |
| 72 |
@y |
| 73 |
@ This module is deleted, because it is only useful for |
| 74 |
a non-local |goto|\unskip, which we don't use in C. |
| 75 |
@z |
| 76 |
|
| 77 |
% [6] No need for |name_length| or |terminal_line_length|. Since these |
| 78 |
% were the only constants, the <Constants...> module is no longer needed. |
| 79 |
@x [6] |
| 80 |
@<Constants...@>= |
| 81 |
@!name_length=80; {maximum length of a file name} |
| 82 |
@!terminal_line_length=132; {maximum length of an input line} |
| 83 |
@y |
| 84 |
@z |
| 85 |
|
| 86 |
@x [8] Change abort to get rid of non-local goto. |
| 87 |
@ It is possible that a malformed packed file (heaven forbid!) or some other |
| 88 |
error might be detected by this program. Such errors might occur in a deeply |
| 89 |
nested procedure, so the procedure called |jump_out| has been added to transfer |
| 90 |
to the very end of the program with an error message. |
| 91 |
|
| 92 |
@d abort(#)==begin print_ln(' ',#); t_print_ln(' ',#); jump_out; end |
| 93 |
|
| 94 |
@p procedure jump_out; |
| 95 |
begin goto final_end; |
| 96 |
end; |
| 97 |
@y |
| 98 |
@ We use a call to the external C exit to avoid a non-local |goto|\unskip. |
| 99 |
|
| 100 |
@d abort(#)==begin print_ln(#); uexit(1) end |
| 101 |
|
| 102 |
@z |
| 103 |
|
| 104 |
@x [10] Pascal Web's char |
| 105 |
@d text_char == char {the data type of characters in text files} |
| 106 |
@y |
| 107 |
@d char == 0..255 |
| 108 |
@d text_char == char {the data type of characters in text files} |
| 109 |
@z |
| 110 |
|
| 111 |
@x [31] Remove typ_file from globals. |
| 112 |
@ @<Glob...@>= |
| 113 |
@!pk_file:byte_file; {where the input comes from} |
| 114 |
@!typ_file:text_file; {where the final output goes} |
| 115 |
@^system dependencies@> |
| 116 |
@y |
| 117 |
@ @<Glob...@>= |
| 118 |
@!pk_file:byte_file; {where the input comes from} |
| 119 |
@^system dependencies@> |
| 120 |
@z |
| 121 |
|
| 122 |
@x [32] Redo open_pk_file; scrap open_typ_file. |
| 123 |
@ To prepare these files for input and output, we |reset| and |rewrite| them. |
| 124 |
An extension of \PASCAL\ is needed, since we want to associate files |
| 125 |
with external names that are specified dynamically (i.e., not |
| 126 |
known at compile time). The following code assumes that `|reset(f,s)|' |
| 127 |
does this, when |f| is a file variable and |s| is a string variable that |
| 128 |
specifies the file name. If |eof(f)| is true immediately after |
| 129 |
|reset(f,s)| has acted, we assume that no file named |s| is accessible. |
| 130 |
@^system dependencies@> |
| 131 |
|
| 132 |
@p procedure open_pk_file; {prepares the input for reading} |
| 133 |
begin reset(pk_file,pk_name); |
| 134 |
pk_loc := 0 ; |
| 135 |
end; |
| 136 |
@# |
| 137 |
procedure open_typ_file; {prepares to write text data to the |typ_file|} |
| 138 |
begin rewrite(typ_file,typ_name); |
| 139 |
end; |
| 140 |
@y |
| 141 |
@ In C, do path searching. |
| 142 |
|
| 143 |
@p procedure open_pk_file; {prepares to read packed bytes in |pk_file|} |
| 144 |
begin |
| 145 |
{Don't use |kpse_find_pk|; we want the exact file or nothing.} |
| 146 |
pk_file := kpse_open_file (cmdline (1), kpse_pk_format); |
| 147 |
cur_loc := 0; |
| 148 |
end; |
| 149 |
@z |
| 150 |
|
| 151 |
@x [33] Change pk_loc to cur_loc, and use C strings, not arrays. |
| 152 |
@!pk_name,@!typ_name:packed array[1..name_length] of char; {name of input |
| 153 |
and output files} |
| 154 |
@!pk_loc:integer; {how many bytes have we read?} |
| 155 |
@y |
| 156 |
@!pk_name:c_string; {name of input and output files} |
| 157 |
@!cur_loc:integer; {how many bytes have we read?} |
| 158 |
@z |
| 159 |
|
| 160 |
@x [34] Use modified routines to access pk_file. |
| 161 |
@ We also need a function that will get a single byte from the \.{pk} file. |
| 162 |
|
| 163 |
@p function pk_byte : eight_bits ; |
| 164 |
var temp : eight_bits ; |
| 165 |
begin |
| 166 |
temp := pk_file^ ; |
| 167 |
get(pk_file) ; |
| 168 |
incr(pk_loc) ; |
| 169 |
pk_byte := temp ; |
| 170 |
end ; |
| 171 |
@y |
| 172 |
@ We shall use a set of simple functions to read the next byte or |
| 173 |
bytes from |pk_file|. There are seven possibilities, each of which is |
| 174 |
treated as a separate function in order to minimize the overhead for |
| 175 |
subroutine calls. We comment out the ones we don't need. |
| 176 |
@^system dependencies@> |
| 177 |
|
| 178 |
@d pk_byte==get_byte |
| 179 |
@d pk_loc==cur_loc |
| 180 |
|
| 181 |
@p function get_byte:integer; {returns the next byte, unsigned} |
| 182 |
var b:eight_bits; |
| 183 |
begin if eof(pk_file) then get_byte:=0 |
| 184 |
else begin read(pk_file,b); incr(cur_loc); get_byte:=b; |
| 185 |
end; |
| 186 |
end; |
| 187 |
@# |
| 188 |
@{ |
| 189 |
function signed_byte:integer; {returns the next byte, signed} |
| 190 |
var b:eight_bits; |
| 191 |
begin read(pk_file,b); incr(cur_loc); |
| 192 |
if b<128 then signed_byte:=b @+ else signed_byte:=b-256; |
| 193 |
end; |
| 194 |
@} |
| 195 |
@# |
| 196 |
function get_two_bytes:integer; {returns the next two bytes, unsigned} |
| 197 |
var a,@!b:eight_bits; |
| 198 |
begin read(pk_file,a); read(pk_file,b); |
| 199 |
cur_loc:=cur_loc+2; |
| 200 |
get_two_bytes:=a*256+b; |
| 201 |
end; |
| 202 |
@# |
| 203 |
@{ |
| 204 |
function signed_pair:integer; {returns the next two bytes, signed} |
| 205 |
var a,@!b:eight_bits; |
| 206 |
begin read(pk_file,a); read(pk_file,b); |
| 207 |
cur_loc:=cur_loc+2; |
| 208 |
if a<128 then signed_pair:=a*256+b |
| 209 |
else signed_pair:=(a-256)*256+b; |
| 210 |
end; |
| 211 |
@} |
| 212 |
@/ |
| 213 |
@{ |
| 214 |
function get_three_bytes:integer; {returns the next three bytes, unsigned} |
| 215 |
var a,@!b,@!c:eight_bits; |
| 216 |
begin read(pk_file,a); read(pk_file,b); read(pk_file,c); |
| 217 |
cur_loc:=cur_loc+3; |
| 218 |
get_three_bytes:=(a*256+b)*256+c; |
| 219 |
end; |
| 220 |
@} |
| 221 |
@/ |
| 222 |
@{ |
| 223 |
function signed_trio:integer; {returns the next three bytes, signed} |
| 224 |
var a,@!b,@!c:eight_bits; |
| 225 |
begin read(pk_file,a); read(pk_file,b); read(pk_file,c); |
| 226 |
cur_loc:=cur_loc+3; |
| 227 |
if a<128 then signed_trio:=(a*256+b)*256+c |
| 228 |
else signed_trio:=((a-256)*256+b)*256+c; |
| 229 |
end; |
| 230 |
@} |
| 231 |
@# |
| 232 |
function signed_quad:integer; {returns the next four bytes, signed} |
| 233 |
var a,@!b,@!c,@!d:eight_bits; |
| 234 |
begin read(pk_file,a); read(pk_file,b); read(pk_file,c); read(pk_file,d); |
| 235 |
cur_loc:=cur_loc+4; |
| 236 |
if a<128 then signed_quad:=((a*256+b)*256+c)*256+d |
| 237 |
else signed_quad:=(((a-256)*256+b)*256+c)*256+d; |
| 238 |
end; |
| 239 |
@z |
| 240 |
|
| 241 |
@x [35] Don't need the <Open files> module. |
| 242 |
@ Now we are ready to open the files. |
| 243 |
|
| 244 |
@<Open files@>= |
| 245 |
open_pk_file ; |
| 246 |
open_typ_file ; |
| 247 |
t_print_ln(banner) ; |
| 248 |
t_print('Input file: ') ; |
| 249 |
i := 1 ; |
| 250 |
while pk_name[i] <> ' ' do begin |
| 251 |
t_print(pk_name[i]) ; incr(i) ; |
| 252 |
end ; |
| 253 |
t_print_ln(' ') |
| 254 |
@y |
| 255 |
@ This module was needed when output was directed to |typ_file|. |
| 256 |
It is not needed when output goes to |stdout|. |
| 257 |
@z |
| 258 |
|
| 259 |
@x [36] Redefine get_16 and get_32. |
| 260 |
@p function get_16 : integer ; |
| 261 |
var a : integer ; |
| 262 |
begin a := pk_byte ; get_16 := a * 256 + pk_byte ; end ; |
| 263 |
@# |
| 264 |
function get_32 : integer ; |
| 265 |
var a : integer ; |
| 266 |
begin a := get_16 ; if a > 32767 then a := a - 65536 ; |
| 267 |
get_32 := a * 65536 + get_16 ; end ; |
| 268 |
@y |
| 269 |
@d get_16==get_two_bytes |
| 270 |
@d get_32==signed_quad |
| 271 |
@z |
| 272 |
|
| 273 |
% [52] web2c can't handle the implied serialism in Pascal write |
| 274 |
% statements. (From [email protected].) |
| 275 |
@x [52] |
| 276 |
pk_yyy : t_print_ln((pk_loc-1):1,': Num special: ',get_32:1) ; |
| 277 |
@y |
| 278 |
pk_yyy : begin t_print((pk_loc-1):1); |
| 279 |
t_print_ln (': Num special: ',get_32:1) ; end; |
| 280 |
@z |
| 281 |
|
| 282 |
@x [53--54] Eliminate the ``Terminal communication'' chapter. |
| 283 |
@* Terminal communication. |
| 284 |
We must get the file names and determine whether input is to be in |
| 285 |
hexadecimal or binary. To do this, we use the standard input path |
| 286 |
name. We need a procedure to flush the input buffer. For most systems, |
| 287 |
this will be an empty statement. For other systems, a |print_ln| will |
| 288 |
provide a quick fix. We also need a routine to get a line of input from |
| 289 |
the terminal. On some systems, a simple |read_ln| will do. Finally, |
| 290 |
a macro to print a string to the first blank is required. |
| 291 |
|
| 292 |
@d flush_buffer == begin end |
| 293 |
@d get_line(#) == if eoln(input) then read_ln(input) ; |
| 294 |
i := 1 ; |
| 295 |
while not (eoln(input) or eof(input)) do begin |
| 296 |
#[i] := input^ ; |
| 297 |
incr(i) ; |
| 298 |
get(input) ; |
| 299 |
end ; |
| 300 |
#[i] := ' ' |
| 301 |
|
| 302 |
@ @p procedure dialog ; |
| 303 |
var i : integer ; {index variable} |
| 304 |
buffer : packed array [1..name_length] of char; {input buffer} |
| 305 |
begin |
| 306 |
for i := 1 to name_length do begin |
| 307 |
typ_name[i] := ' ' ; |
| 308 |
pk_name[i] := ' ' ; |
| 309 |
end; |
| 310 |
print('Input file name: ') ; |
| 311 |
flush_buffer ; |
| 312 |
get_line(pk_name) ; |
| 313 |
print('Output file name: ') ; |
| 314 |
flush_buffer ; |
| 315 |
get_line(typ_name) ; |
| 316 |
end ; |
| 317 |
@y |
| 318 |
@* Terminal communication. There isn't any. |
| 319 |
|
| 320 |
@ So there is no |procedure dialog|. |
| 321 |
@z |
| 322 |
|
| 323 |
@x [55] Restructure the main program. |
| 324 |
dialog ; |
| 325 |
@<Open files@> ; |
| 326 |
@y |
| 327 |
open_pk_file ; |
| 328 |
@z |
| 329 |
@x [55] |
| 330 |
final_end : |
| 331 |
@y |
| 332 |
@z |
| 333 |
|
| 334 |
@x [56] System-dependent changes. |
| 335 |
This section should be replaced, if necessary, by changes to the program |
| 336 |
that are necessary to make \.{PKtype} work at a particular installation. |
| 337 |
Any additional routines should be inserted here. |
| 338 |
@^system dependencies@> |
| 339 |
@y |
| 340 |
Parse a Unix-style command line. |
| 341 |
|
| 342 |
@d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0) |
| 343 |
|
| 344 |
@<Define \(|parse_arguments|@> = |
| 345 |
procedure parse_arguments; |
| 346 |
const n_options = 2; {Pascal won't count array lengths for us.} |
| 347 |
var @!long_options: array[0..n_options] of getopt_struct; |
| 348 |
@!getopt_return_val: integer; |
| 349 |
@!option_index: c_int_type; |
| 350 |
@!current_option: 0..n_options; |
| 351 |
begin |
| 352 |
@<Define the option table@>; |
| 353 |
repeat |
| 354 |
getopt_return_val := getopt_long_only (argc, argv, '', long_options, |
| 355 |
address_of (option_index)); |
| 356 |
if getopt_return_val = -1 then begin |
| 357 |
do_nothing; |
| 358 |
|
| 359 |
end else if getopt_return_val = '?' then begin |
| 360 |
usage (my_name); |
| 361 |
|
| 362 |
end else if argument_is ('help') then begin |
| 363 |
usage_help (PKTYPE_HELP, nil); |
| 364 |
|
| 365 |
end else if argument_is ('version') then begin |
| 366 |
print_version_and_exit (banner, nil, 'Tomas Rokicki', nil); |
| 367 |
|
| 368 |
end; {Else it was just a flag; |getopt| has already done the assignment.} |
| 369 |
until getopt_return_val = -1; |
| 370 |
|
| 371 |
{Now |optind| is the index of first non-option on the command line.} |
| 372 |
if (optind + 1 <> argc) then begin |
| 373 |
write_ln (stderr, my_name, ': Need exactly one file argument.'); |
| 374 |
usage (my_name); |
| 375 |
end; |
| 376 |
end; |
| 377 |
|
| 378 |
@ Here are the options we allow. The first is one of the standard GNU options. |
| 379 |
@.-help@> |
| 380 |
|
| 381 |
@<Define the option...@> = |
| 382 |
current_option := 0; |
| 383 |
long_options[current_option].name := 'help'; |
| 384 |
long_options[current_option].has_arg := 0; |
| 385 |
long_options[current_option].flag := 0; |
| 386 |
long_options[current_option].val := 0; |
| 387 |
incr (current_option); |
| 388 |
|
| 389 |
@ Another of the standard options. |
| 390 |
@.-version@> |
| 391 |
|
| 392 |
@<Define the option...@> = |
| 393 |
long_options[current_option].name := 'version'; |
| 394 |
long_options[current_option].has_arg := 0; |
| 395 |
long_options[current_option].flag := 0; |
| 396 |
long_options[current_option].val := 0; |
| 397 |
incr (current_option); |
| 398 |
|
| 399 |
@ An element with all zeros always ends the list. |
| 400 |
|
| 401 |
@<Define the option...@> = |
| 402 |
long_options[current_option].name := 0; |
| 403 |
long_options[current_option].has_arg := 0; |
| 404 |
long_options[current_option].flag := 0; |
| 405 |
long_options[current_option].val := 0; |
| 406 |
@z |