| 1 |
% dvicopy.ch for C compilation with web2c. |
| 2 |
% The original version of this file was created by Monika Jayme and |
| 3 |
% Klaus Guntermann at TH Darmstadt (THD), FR Germany. |
| 4 |
% Some parts are borrowed from the changes to dvitype, vftovp and vptovf. |
| 5 |
% |
| 6 |
% July 90 THD First versions for dvicopy 0.91 and 0.92 |
| 7 |
% Aug 09 90 THD Updated to dvicopy 1.0 and released |
| 8 |
% Mar 20 91 THD Updated to dvicopy 1.2 |
| 9 |
% (more recent changes in the ChangeLog) |
| 10 |
|
| 11 |
@x [0] l.60 WEAVE: print changes only |
| 12 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 13 |
@y |
| 14 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 15 |
\let\maybe=\iffalse |
| 16 |
@z |
| 17 |
|
| 18 |
@x [1] l.98 Define my_name |
| 19 |
@d banner=='This is DVIcopy, Version 1.6' {printed when the program starts} |
| 20 |
@y |
| 21 |
@d my_name=='dvicopy' |
| 22 |
@d banner=='This is DVIcopy, Version 1.6' {printed when the program starts} |
| 23 |
@z |
| 24 |
|
| 25 |
@x [2] l.124 No random reading on stdin, may be not seekable. |
| 26 |
@d random_reading==true {should we skip around in the file?} |
| 27 |
@y |
| 28 |
@<Globals in the outer block@>= |
| 29 |
@!random_reading:boolean; {should we skip around in the file?} |
| 30 |
@z |
| 31 |
|
| 32 |
@x [3] l.135 Purge non-local 'goto'. |
| 33 |
calls the `|jump_out|' procedure, which goes to the label |final_end|. |
| 34 |
|
| 35 |
@d final_end = 9999 {go here to wrap it up} |
| 36 |
@y |
| 37 |
calls the `|jump_out|' procedure. |
| 38 |
@z |
| 39 |
@x [3] l.141 |
| 40 |
label final_end; |
| 41 |
@y |
| 42 |
@z |
| 43 |
|
| 44 |
@x [3] l.146 Set up kpathsea. |
| 45 |
procedure initialize; {this procedure gets things started properly} |
| 46 |
var @<Local variables for initialization@>@/ |
| 47 |
begin print_ln(banner);@/ |
| 48 |
@y |
| 49 |
@<Define \(|parse_arguments|@> |
| 50 |
procedure initialize; {this procedure gets things started properly} |
| 51 |
var @<Local variables for initialization@>@/ |
| 52 |
begin |
| 53 |
kpse_set_program_name (argv[0], my_name); |
| 54 |
parse_arguments; |
| 55 |
print (banner); print_ln (version_string); |
| 56 |
@z |
| 57 |
|
| 58 |
@x [5] l.178 Big enough arrays to convert the dvilj sample font document. |
| 59 |
@<Constants...@>= |
| 60 |
@!max_fonts=100; {maximum number of distinct fonts} |
| 61 |
@!max_chars=10000; {maximum number of different characters among all fonts} |
| 62 |
@!max_widths=3000; {maximum number of different characters widths} |
| 63 |
@!max_packets=5000; {maximum number of different characters packets; |
| 64 |
must be less than 65536} |
| 65 |
@!max_bytes=30000; {maximum number of bytes for characters packets} |
| 66 |
@!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level} |
| 67 |
@!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth} |
| 68 |
@!terminal_line_length=150; {maximum number of characters input in a single |
| 69 |
line of input from the terminal} |
| 70 |
@!name_length=50; {a file name shouldn't be longer than this} |
| 71 |
@y |
| 72 |
@<Constants...@>= |
| 73 |
@!max_fonts=400; {maximum number of distinct fonts} |
| 74 |
@!max_chars=750000; {maximum number of different characters among all fonts} |
| 75 |
@!max_widths=16000; {maximum number of different characters widths} |
| 76 |
@!max_packets=65530; {maximum number of different characters packets; |
| 77 |
must be less than 65536} |
| 78 |
@!max_bytes=250000; {maximum number of bytes for characters packets} |
| 79 |
@!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level} |
| 80 |
@!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth} |
| 81 |
@!terminal_line_length=256; {maximum number of characters input in a single |
| 82 |
line of input from the terminal} |
| 83 |
@z |
| 84 |
|
| 85 |
% [7] Rename the integer types, as they collide with names used by C99. |
| 86 |
% Rather than change the code all over the place, we use macros to do |
| 87 |
% the renaming. This could also be done at C preprocessor level. |
| 88 |
@x [7] l.245 |
| 89 |
@d int_32 == integer {signed 32~bit integers} |
| 90 |
@y |
| 91 |
@d int_32 == integer {signed 32~bit integers} |
| 92 |
@d int_31 == int_31_t |
| 93 |
@d int_24u == int_24u_t |
| 94 |
@d int_24 == int_24_t |
| 95 |
@d int_23 == int_23_t |
| 96 |
@d int_16u == int_16u_t |
| 97 |
@d int_16 == int_16_t |
| 98 |
@d int_15 == int_15_t |
| 99 |
@d int_8u == int_8u_t |
| 100 |
@d int_8 == int_8_t |
| 101 |
@d int_7 == int_7_t |
| 102 |
@z |
| 103 |
|
| 104 |
% [11] Redirect output, so it can go to either stdout or stderr, |
| 105 |
% depending on where the output dvi file is going. |
| 106 |
@x [11] l.311 |
| 107 |
@d print(#)==write(output,#) |
| 108 |
@d print_ln(#)==write_ln(output,#) |
| 109 |
@d new_line==write_ln(output) {start new line} |
| 110 |
@y |
| 111 |
@d print(#)==write(term_out,#) |
| 112 |
@d print_ln(#)==write_ln(term_out,#) |
| 113 |
@d new_line==write_ln(term_out) {start new line} |
| 114 |
@z |
| 115 |
|
| 116 |
@x [14] l.380 Permissive input. |
| 117 |
@!ASCII_code=" ".."~"; {a subrange of the integers} |
| 118 |
@y |
| 119 |
@!ASCII_code=0..255; {a subrange of the integers} |
| 120 |
@z |
| 121 |
|
| 122 |
% [15] The text_char type is used as an array index into xord. The |
| 123 |
% default type `char' produces signed integers, which are bad array |
| 124 |
% indices in C. |
| 125 |
@x [15] l.400 |
| 126 |
@d text_char == char {the data type of characters in text files} |
| 127 |
@d first_text_char=0 {ordinal number of the smallest element of |text_char|} |
| 128 |
@d last_text_char=127 {ordinal number of the largest element of |text_char|} |
| 129 |
@y |
| 130 |
@d text_char == ASCII_code {the data type of characters in text files} |
| 131 |
@d first_text_char=0 {ordinal number of the smallest element of |text_char|} |
| 132 |
@d last_text_char=255 {ordinal number of the largest element of |text_char|} |
| 133 |
@z |
| 134 |
|
| 135 |
@x [23] l.573 Remove non-local goto, declare jump_out as noreturn |
| 136 |
so a procedure called |jump_out| has been introduced. This procedure, which |
| 137 |
transfers control to the label |final_end| at the end of the program, |
| 138 |
contains the only non-local |@!goto| statement in \.{\title}. |
| 139 |
@^system dependencies@> |
| 140 |
Some \PASCAL\ compilers do not implement non-local |goto| statements. In |
| 141 |
such cases the |goto final_end| in |jump_out| should simply be replaced |
| 142 |
by a call on some system procedure that quietly terminates the program. |
| 143 |
@y |
| 144 |
so a procedure called |jump_out| has been introduced. |
| 145 |
@z |
| 146 |
@x [23] l.582 |
| 147 |
@d abort(#)==begin print_ln(' ',#,'.'); jump_out; |
| 148 |
end |
| 149 |
|
| 150 |
@<Error handling...@>= |
| 151 |
@<Basic printing procedures@>@; |
| 152 |
procedure close_files_and_terminate; forward; |
| 153 |
@# |
| 154 |
procedure jump_out; |
| 155 |
begin mark_fatal; close_files_and_terminate; |
| 156 |
goto final_end; |
| 157 |
end; |
| 158 |
@y |
| 159 |
@f noreturn==procedure |
| 160 |
@d abort(#)==begin write_ln(stderr, ' ',#,'.'); jump_out; |
| 161 |
end |
| 162 |
|
| 163 |
@<Error handling...@>= |
| 164 |
@<Basic printing procedures@>@; |
| 165 |
procedure close_files_and_terminate; forward; |
| 166 |
@# |
| 167 |
noreturn procedure jump_out; |
| 168 |
begin mark_fatal; close_files_and_terminate; |
| 169 |
uexit(1); |
| 170 |
end; |
| 171 |
@z |
| 172 |
|
| 173 |
@x [24] l.597 Declare confusion as noreturn |
| 174 |
says |confusion(|indication of where we are|)|. |
| 175 |
@y |
| 176 |
says |confusion|(indication of where we are). |
| 177 |
@z |
| 178 |
@x [24] l.600 |
| 179 |
procedure confusion(@!p:pckt_pointer); |
| 180 |
@y |
| 181 |
noreturn procedure confusion(@!p:pckt_pointer); |
| 182 |
@z |
| 183 |
|
| 184 |
@x [25] l.609 Declare overflow as noreturn |
| 185 |
procedure overflow(@!p:pckt_pointer;@!n:int_16u); |
| 186 |
@y |
| 187 |
noreturn procedure overflow(@!p:pckt_pointer;@!n:int_16u); |
| 188 |
@z |
| 189 |
|
| 190 |
@x [62] l.1236 cur_name is no longer a fixed-size array. |
| 191 |
@!cur_name:packed array[1..name_length] of char; {external name, |
| 192 |
with no lower case letters} |
| 193 |
@y |
| 194 |
@!cur_name:^char; |
| 195 |
@z |
| 196 |
|
| 197 |
@x [63] l.1241 Ignore arguments to make_font_name. |
| 198 |
@ For \.{TFM} and \.{VF} files we just append the appropriate extension |
| 199 |
to the file name packet; in addition a system dependent area part |
| 200 |
(usually different for \.{TFM} and \.{VF} files) is prepended if |
| 201 |
the file name packet contains no area part. |
| 202 |
@^system dependencies@> |
| 203 |
|
| 204 |
@d append_to_name(#)== |
| 205 |
if l_cur_name<name_length then |
| 206 |
begin incr(l_cur_name); cur_name[l_cur_name]:=#; |
| 207 |
end |
| 208 |
else overflow(str_name_length,name_length) |
| 209 |
@d make_font_name_end(#)== |
| 210 |
append_to_name(#[l]); make_name |
| 211 |
@d make_font_name(#)== |
| 212 |
l_cur_name:=0; for l:=1 to # do make_font_name_end |
| 213 |
@y |
| 214 |
@ Since files are actually searched through path definitions, |
| 215 |
the area definitions are ignored here. |
| 216 |
To reduce the required changes we simply ignore the parameters given |
| 217 |
to |make_font_name|. |
| 218 |
@^system dependencies@> |
| 219 |
|
| 220 |
@d append_to_name(#)== begin |
| 221 |
cur_name[l_cur_name]:=#; |
| 222 |
incr(l_cur_name); |
| 223 |
end |
| 224 |
@d make_font_name_end(#)== |
| 225 |
make_name |
| 226 |
@d make_font_name(#)== |
| 227 |
l_cur_name:=0; make_font_name_end |
| 228 |
@z |
| 229 |
|
| 230 |
% [67] No conversion of filenames in lower case, and initialize and |
| 231 |
% terminate for C strings. Eliminate now unused variable. |
| 232 |
@x [67] l.1311 |
| 233 |
@!c:char; {a character to be appended to |cur_name|} |
| 234 |
@y |
| 235 |
@z |
| 236 |
|
| 237 |
@x [67] l.1313 |
| 238 |
cur_loc:=pckt_start[n]; cur_limit:=pckt_start[n+1]; |
| 239 |
@y |
| 240 |
cur_name := xmalloc_array (char, pckt_length (n) + pckt_length (e)); |
| 241 |
cur_loc:=pckt_start[n]; cur_limit:=pckt_start[n+1]; |
| 242 |
@z |
| 243 |
|
| 244 |
@x [67] l.1318 |
| 245 |
if (b>="a")and(b<="z") then Decr(b)(("a"-"A")); {convert to upper case} |
| 246 |
@y |
| 247 |
@z |
| 248 |
|
| 249 |
@x [67] l.1321 |
| 250 |
cur_loc:=pckt_start[e]; cur_limit:=pckt_start[e+1]; |
| 251 |
while cur_loc<cur_limit do |
| 252 |
begin pckt_extract(b); append_res_to_name(xchr[b]); |
| 253 |
end; |
| 254 |
while l_cur_name<name_length do |
| 255 |
begin incr(l_cur_name); cur_name[l_cur_name]:=' '; |
| 256 |
end; |
| 257 |
@y Skip copying in the extension, kpathsea knows what to do. |
| 258 |
cur_name[l_cur_name] := 0; |
| 259 |
@z |
| 260 |
|
| 261 |
@x [91] l.1717 Lower case file name extensions. |
| 262 |
id4(".")("T")("F")("M")(tfm_ext); {file name extension for \.{TFM} files} |
| 263 |
@y |
| 264 |
id4(".")("t")("f")("m")(tfm_ext); {file name extension for \.{TFM} files} |
| 265 |
@z |
| 266 |
|
| 267 |
@x [92] l.1719 Set default directory name |
| 268 |
@ If no font directory has been specified, \.{\title} is supposed to use |
| 269 |
the default \.{TFM} directory, which is a system-dependent place where |
| 270 |
the \.{TFM} files for standard fonts are kept. |
| 271 |
The string variable |TFM_default_area| contains the name of this area. |
| 272 |
@^system dependencies@> |
| 273 |
|
| 274 |
@d TFM_default_area_name=='TeXfonts:' {change this to the correct name} |
| 275 |
@d TFM_default_area_name_length=9 {change this to the correct length} |
| 276 |
|
| 277 |
@<Glob...@>= |
| 278 |
@!TFM_default_area:packed array[1..TFM_default_area_name_length] of char; |
| 279 |
@y |
| 280 |
@ If no font directory has been specified, we search paths. |
| 281 |
@z |
| 282 |
|
| 283 |
@x [93] l.1731 Remove initialization of now-defunct array |
| 284 |
@ @<Set init...@>= |
| 285 |
TFM_default_area:=TFM_default_area_name; |
| 286 |
@y |
| 287 |
@ (No initialization to be done. Keep this module to preserve numbering.) |
| 288 |
@z |
| 289 |
|
| 290 |
@x [94] l.1739 Declare bad_tfm as noreturn |
| 291 |
procedure bad_tfm; |
| 292 |
@y |
| 293 |
noreturn procedure bad_tfm; |
| 294 |
@z |
| 295 |
|
| 296 |
@x [94] l.1746 Declare bad_font as noreturn |
| 297 |
procedure bad_font; |
| 298 |
@y |
| 299 |
noreturn procedure bad_font; |
| 300 |
@z |
| 301 |
|
| 302 |
@x [95] l.1758 Open TFM file |
| 303 |
@<TFM: Open |tfm_file|@>= |
| 304 |
make_font_name(TFM_default_area_name_length)(TFM_default_area)(tfm_ext); |
| 305 |
reset(tfm_file,cur_name); |
| 306 |
if eof(tfm_file) then |
| 307 |
@y |
| 308 |
|TFM_default_area_name_length| and |TFM_default_area| will not |
| 309 |
be used by |make_font_name|. |
| 310 |
|
| 311 |
@<TFM: Open |tfm_file|@>= |
| 312 |
make_font_name(TFM_default_area_name_length)(TFM_default_area)(tfm_ext); |
| 313 |
full_name := kpse_find_tfm (cur_name); |
| 314 |
if full_name then begin |
| 315 |
resetbin (tfm_file, full_name); |
| 316 |
free (cur_name); |
| 317 |
free (full_name); |
| 318 |
end else |
| 319 |
@z |
| 320 |
|
| 321 |
@x [104] l.1991 Compute alpha and beta. |
| 322 |
alpha:=16; |
| 323 |
while z>=@'40000000 do |
| 324 |
@y |
| 325 |
alpha:=16; |
| 326 |
if z>=@'1000000000 then abort('Character size is too large!'); |
| 327 |
while z>=@'40000000 do |
| 328 |
@z |
| 329 |
|
| 330 |
@x [108] l.2061 Declare full_name. |
| 331 |
@!dvi_loc:int_32; {where we are about to look, in |dvi_file|} |
| 332 |
@y |
| 333 |
@!dvi_loc:int_32; {where we are about to look, in |dvi_file|} |
| 334 |
@!full_name: ^char; |
| 335 |
@z |
| 336 |
|
| 337 |
@x [109] l.2068 Declare bad_dvi as noreturn |
| 338 |
procedure bad_dvi; |
| 339 |
@y |
| 340 |
noreturn procedure bad_dvi; |
| 341 |
@z |
| 342 |
|
| 343 |
@x [110] l.2077 Fix up opening the binary files |
| 344 |
@ To prepare |dvi_file| for input, we |reset| it. |
| 345 |
|
| 346 |
@<Open input file(s)@>= |
| 347 |
reset(dvi_file); {prepares to read packed bytes from |dvi_file|} |
| 348 |
dvi_loc:=0; |
| 349 |
@y |
| 350 |
@ To prepare |dvi_file| for input, we |reset| it. |
| 351 |
|
| 352 |
@<Open input file(s)@>= |
| 353 |
dvi_loc:=0; |
| 354 |
@z |
| 355 |
|
| 356 |
@x [112] l.2115 Make dvi_length() and dvi_move() work. |
| 357 |
@p function dvi_length:int_32; |
| 358 |
begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file); |
| 359 |
end; |
| 360 |
@# |
| 361 |
procedure dvi_move(@!n:int_32); |
| 362 |
begin set_pos(dvi_file,n); dvi_loc:=n; |
| 363 |
end; |
| 364 |
@y |
| 365 |
@p function dvi_length:int_32; |
| 366 |
begin xfseek(dvi_file, 0, 2, dvi_name); |
| 367 |
dvi_loc:=xftell(dvi_file, dvi_name); |
| 368 |
dvi_length:=dvi_loc; |
| 369 |
end; |
| 370 |
@# |
| 371 |
procedure dvi_move(n:int_32); |
| 372 |
begin xfseek(dvi_file, n, 0, dvi_name); |
| 373 |
dvi_loc:=n; |
| 374 |
end; |
| 375 |
@z |
| 376 |
|
| 377 |
@x [135] l.2468 Lower case file name extensions. |
| 378 |
id3(".")("V")("F")(vf_ext); {file name extension for \.{VF} files} |
| 379 |
@y |
| 380 |
id3(".")("v")("f")(vf_ext); {file name extension for \.{VF} files} |
| 381 |
@z |
| 382 |
|
| 383 |
@x [137] l.2482 Set default directory name |
| 384 |
@ If no font directory has been specified, \.{\title} is supposed to use |
| 385 |
the default \.{VF} directory, which is a system-dependent place where |
| 386 |
the \.{VF} files for standard fonts are kept. |
| 387 |
The string variable |VF_default_area| contains the name of this area. |
| 388 |
@^system dependencies@> |
| 389 |
|
| 390 |
@d VF_default_area_name=='TeXvfonts:' {change this to the correct name} |
| 391 |
@d VF_default_area_name_length=10 {change this to the correct length} |
| 392 |
|
| 393 |
@<Glob...@>= |
| 394 |
@!VF_default_area:packed array[1..VF_default_area_name_length] of char; |
| 395 |
|
| 396 |
@ @<Set init...@>= |
| 397 |
VF_default_area:=VF_default_area_name; |
| 398 |
@y |
| 399 |
@ If no font directory has been specified, \.{\title} is supposed to use |
| 400 |
the default \.{VF} directory, which is a system-dependent place where |
| 401 |
the \.{VF} files for standard fonts are kept. |
| 402 |
|
| 403 |
Actually, under UNIX the standard area is defined in an external |
| 404 |
file \.{site.h}. And the users have a path searched for fonts, |
| 405 |
by setting the \.{VFFONTS} environment variable. |
| 406 |
|
| 407 |
@ (No initialization to be done. Keep this module to preserve numbering.) |
| 408 |
@z |
| 409 |
|
| 410 |
@x [139] l.2499 Open VF file |
| 411 |
@<VF: Open |vf_file| or |goto not_found|@>= |
| 412 |
make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext); |
| 413 |
reset(vf_file,cur_name); |
| 414 |
if eof(vf_file) then |
| 415 |
@^system dependencies@> |
| 416 |
@y |
| 417 |
Do path searching. But the \.{VF} file may not exist. |
| 418 |
|
| 419 |
@<VF: Open |vf_file| or |goto not_found|@>= |
| 420 |
make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext); |
| 421 |
full_name := kpse_find_vf (cur_name); |
| 422 |
if full_name then begin |
| 423 |
resetbin (vf_file, full_name); |
| 424 |
free (cur_name); |
| 425 |
free (full_name); |
| 426 |
end else |
| 427 |
@z |
| 428 |
|
| 429 |
@x [163] l.2938 copy elements of array piece by piece |
| 430 |
@ @<VF: Start a new level@>= |
| 431 |
append_one(push); |
| 432 |
vf_move[vf_ptr]:=vf_move[vf_ptr-1]; |
| 433 |
@y |
| 434 |
@ \.{web2c} does not like array assignments. So we need to do them |
| 435 |
through a macro replacement. |
| 436 |
|
| 437 |
@d do_vf_move(#) == vf_move[vf_ptr]# := vf_move[vf_ptr-1]# |
| 438 |
@d vf_move_assign == begin do_vf_move([0][0]); do_vf_move([0][1]); |
| 439 |
do_vf_move([1][0]); do_vf_move([1][1]) |
| 440 |
end |
| 441 |
|
| 442 |
@<VF: Start a new level@>= |
| 443 |
append_one(push); |
| 444 |
vf_move_assign; |
| 445 |
@z |
| 446 |
|
| 447 |
@x [170] l.3034 and again... |
| 448 |
vf_move[vf_ptr]:=vf_move[vf_ptr-1]; |
| 449 |
@y |
| 450 |
vf_move_assign; |
| 451 |
@z |
| 452 |
|
| 453 |
@x [176] l.3139 break is fflush. |
| 454 |
@d update_terminal == break(output) {empty the terminal output buffer} |
| 455 |
@y |
| 456 |
@d update_terminal == fflush(stdout) {empty the terminal output buffer} |
| 457 |
@z |
| 458 |
|
| 459 |
@x [176] l.3149 |
| 460 |
procedure input_ln; {inputs a line from the terminal} |
| 461 |
var k:0..terminal_line_length; |
| 462 |
begin if n_opt=0 then |
| 463 |
begin print('Enter option: '); update_terminal; reset(input); |
| 464 |
if eoln(input) then read_ln(input); |
| 465 |
k:=0; pckt_room(terminal_line_length); |
| 466 |
while (k<terminal_line_length)and not eoln(input) do |
| 467 |
begin append_byte(xord[input^]); incr(k); get(input); |
| 468 |
end; |
| 469 |
end |
| 470 |
else if k_opt<n_opt then |
| 471 |
begin incr(k_opt); |
| 472 |
{Copy command line option number |k_opt| into |byte_mem| array!} |
| 473 |
end; |
| 474 |
end; |
| 475 |
@y |
| 476 |
procedure input_ln; {inputs a line from the terminal} |
| 477 |
var k:0..terminal_line_length; |
| 478 |
begin print('Enter option: '); update_terminal; |
| 479 |
{|if eoln(input) then read_ln(input);|} |
| 480 |
k:=0; pckt_room(terminal_line_length); |
| 481 |
while (k<terminal_line_length)and not eoln(input) do |
| 482 |
begin append_byte(xord[getc(input)]); incr(k); |
| 483 |
end; |
| 484 |
end; |
| 485 |
@z |
| 486 |
|
| 487 |
@x [241] l.4071 No dialog, remove unused final label. |
| 488 |
dialog; {get options} |
| 489 |
@y |
| 490 |
@z |
| 491 |
@x [241] l.4076 |
| 492 |
final_end:end. |
| 493 |
@y |
| 494 |
end. |
| 495 |
@z |
| 496 |
|
| 497 |
@x [246] l.4125 Do this later, to avoid creating empty files. |
| 498 |
@<Open output file(s)@>= |
| 499 |
rewrite(out_file); {prepares to write packed bytes to |out_file|} |
| 500 |
@y |
| 501 |
@<Open output file(s)@>= |
| 502 |
@z |
| 503 |
|
| 504 |
@x [248] l.4142 Use external routine to output bytes. |
| 505 |
@d out_byte(#) == write(out_file,#) {write next \.{DVI} byte} |
| 506 |
@y |
| 507 |
@d out_byte(#) == put_byte(#,out_file) {write next \.{DVI} byte} |
| 508 |
@z |
| 509 |
|
| 510 |
@x [260] l.4260 String declaration. |
| 511 |
@!comment:packed array[1..comm_length] of char; {preamble comment prefix} |
| 512 |
@y |
| 513 |
@!comment:const_c_string; {preamble comment prefix} |
| 514 |
@z |
| 515 |
|
| 516 |
@x [261] l.4271 Output the string from 0 to len-1, not 1 to len. |
| 517 |
for k:=1 to comm_length do append_byte(xord[comment[k]]); |
| 518 |
@y |
| 519 |
for k:=0 to comm_length - 1 do append_byte(xord[ucharcast(comment[k])]); |
| 520 |
@z |
| 521 |
|
| 522 |
@x [293] l.4481 System-dependent changes. |
| 523 |
This section should be replaced, if necessary, by changes to the program |
| 524 |
that are necessary to make \.{DVIcopy} work at a particular installation. |
| 525 |
It is usually best to design your change file so that all changes to |
| 526 |
previous sections preserve the section numbering; then everybody's version |
| 527 |
will be consistent with the printed program. More extensive changes, |
| 528 |
which introduce new sections, can be inserted here; then only the index |
| 529 |
itself will get a new section number. |
| 530 |
@^system dependencies@> |
| 531 |
@y |
| 532 |
Parse a Unix-style command line. |
| 533 |
|
| 534 |
This macro tests if its argument is the current option, as represented |
| 535 |
by the index variable |option_index|. |
| 536 |
|
| 537 |
@d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0) |
| 538 |
|
| 539 |
@<Define \(|parse_arguments|@> = |
| 540 |
procedure parse_arguments; |
| 541 |
const n_options = 5; {Pascal won't count array lengths for us.} |
| 542 |
var @!long_options: array[0..n_options] of getopt_struct; |
| 543 |
@!getopt_return_val: integer; |
| 544 |
@!option_index: c_int_type; |
| 545 |
@!current_option: 0..n_options; |
| 546 |
@!k, @!m: c_int_type; |
| 547 |
@!end_num: ^char; |
| 548 |
begin |
| 549 |
@<Define the option table@>; |
| 550 |
@<Initialize options@>; |
| 551 |
repeat |
| 552 |
getopt_return_val := getopt_long_only (argc, argv, '', long_options, |
| 553 |
address_of (option_index)); |
| 554 |
if getopt_return_val = -1 then begin |
| 555 |
do_nothing; {End of arguments; we exit the loop below.} |
| 556 |
|
| 557 |
end else if getopt_return_val = "?" then begin |
| 558 |
usage (my_name); |
| 559 |
|
| 560 |
end else if argument_is ('help') then begin |
| 561 |
usage_help (DVICOPY_HELP, nil); |
| 562 |
|
| 563 |
end else if argument_is ('version') then begin |
| 564 |
print_version_and_exit (banner, 'Peter Breitenlohner', nil, nil); |
| 565 |
|
| 566 |
end else if argument_is ('magnification') then begin |
| 567 |
out_mag := atou (optarg); |
| 568 |
|
| 569 |
end else if argument_is ('max-pages') then begin |
| 570 |
max_pages := atou (optarg); |
| 571 |
incr (cur_select); |
| 572 |
|
| 573 |
end else if argument_is ('page-start') then begin |
| 574 |
@<Determine the desired |start_count| values from |optarg|@>; |
| 575 |
|
| 576 |
end; {Else it was a flag; |getopt| has already done the assignment.} |
| 577 |
until getopt_return_val = -1; |
| 578 |
|
| 579 |
{Now |optind| is the index of first non-option on the command line. |
| 580 |
We can have zero, one, or two remaining arguments.} |
| 581 |
if (optind > argc) or (optind + 2 < argc) then begin |
| 582 |
write_ln (stderr, my_name, ': Need at most two file arguments.'); |
| 583 |
usage (my_name); |
| 584 |
end; |
| 585 |
|
| 586 |
if optind = argc then begin |
| 587 |
dvi_name := '<stdin>'; |
| 588 |
dvi_file := make_binary_file (stdin); |
| 589 |
random_reading := false; |
| 590 |
end else begin |
| 591 |
dvi_name := extend_filename (cmdline (optind), 'dvi'); |
| 592 |
resetbin (dvi_file, dvi_name); |
| 593 |
random_reading := true; |
| 594 |
end; |
| 595 |
|
| 596 |
if optind + 2 = argc then begin |
| 597 |
rewritebin (out_file, extend_filename (cmdline (optind + 1), 'dvi')); |
| 598 |
term_out := stdout; |
| 599 |
end else begin |
| 600 |
out_file := make_binary_file (stdout); |
| 601 |
term_out := stderr; |
| 602 |
end; |
| 603 |
end; |
| 604 |
|
| 605 |
@ Here is the first of the options we allow. |
| 606 |
@.-help@> |
| 607 |
|
| 608 |
@<Define the option...@> = |
| 609 |
current_option := 0; |
| 610 |
long_options[0].name := 'help'; |
| 611 |
long_options[0].has_arg := 0; |
| 612 |
long_options[0].flag := 0; |
| 613 |
long_options[0].val := 0; |
| 614 |
incr (current_option); |
| 615 |
|
| 616 |
@ Another of the standard options. |
| 617 |
@.-version@> |
| 618 |
|
| 619 |
@<Define the option...@> = |
| 620 |
long_options[current_option].name := 'version'; |
| 621 |
long_options[current_option].has_arg := 0; |
| 622 |
long_options[current_option].flag := 0; |
| 623 |
long_options[current_option].val := 0; |
| 624 |
incr (current_option); |
| 625 |
|
| 626 |
@ Magnification to apply. |
| 627 |
@.-magnification@> |
| 628 |
|
| 629 |
@<Define the option...@> = |
| 630 |
long_options[current_option].name := 'magnification'; |
| 631 |
long_options[current_option].has_arg := 1; |
| 632 |
long_options[current_option].flag := 0; |
| 633 |
long_options[current_option].val := 0; |
| 634 |
incr (current_option); |
| 635 |
|
| 636 |
@ How many pages to do. |
| 637 |
@.-max-pages@> |
| 638 |
|
| 639 |
@<Define the option...@> = |
| 640 |
long_options[current_option].name := 'max-pages'; |
| 641 |
long_options[current_option].has_arg := 1; |
| 642 |
long_options[current_option].flag := 0; |
| 643 |
long_options[current_option].val := 0; |
| 644 |
incr (current_option); |
| 645 |
|
| 646 |
@ What page to start at. |
| 647 |
@.-page-start@> |
| 648 |
|
| 649 |
@<Define the option...@> = |
| 650 |
long_options[current_option].name := 'page-start'; |
| 651 |
long_options[current_option].has_arg := 1; |
| 652 |
long_options[current_option].flag := 0; |
| 653 |
long_options[current_option].val := 0; |
| 654 |
incr (current_option); |
| 655 |
|
| 656 |
@ Parsing the starting page specification is a bit complicated. |
| 657 |
(This is the same as in \.{DVItype}.) |
| 658 |
|
| 659 |
@<Determine the desired |start_count|...@> = |
| 660 |
k := 0; {which \.{\\count} register we're on} |
| 661 |
m := 0; {position in |optarg|} |
| 662 |
while optarg[m] do begin |
| 663 |
if optarg[m] = "*" then begin |
| 664 |
start_there[k] := false; |
| 665 |
incr (m); |
| 666 |
|
| 667 |
end else if optarg[m] = "." then begin |
| 668 |
incr (k); |
| 669 |
if k >= 10 then begin |
| 670 |
write_ln (stderr, my_name, ': More than ten count registers specified.'); |
| 671 |
uexit (1); |
| 672 |
end; |
| 673 |
incr (m); |
| 674 |
|
| 675 |
end else begin |
| 676 |
start_count[k] := strtol (optarg + m, address_of (end_num), 10); |
| 677 |
if end_num = optarg + m then begin |
| 678 |
write_ln (stderr, my_name, ': -page-start values must be numeric or *.'); |
| 679 |
uexit (1); |
| 680 |
end; |
| 681 |
start_there[k] := true; |
| 682 |
m := m + end_num - (optarg + m); |
| 683 |
end; |
| 684 |
end; |
| 685 |
start_vals := k; |
| 686 |
selected := false; |
| 687 |
|
| 688 |
@ An element with all zeros always ends the list. |
| 689 |
|
| 690 |
@<Define the option...@> = |
| 691 |
long_options[current_option].name := 0; |
| 692 |
long_options[current_option].has_arg := 0; |
| 693 |
long_options[current_option].flag := 0; |
| 694 |
long_options[current_option].val := 0; |
| 695 |
|
| 696 |
@ @<Glob...@> = |
| 697 |
@!term_out:text; |
| 698 |
@!dvi_name:const_c_string; |
| 699 |
@z |