| 1 |
/* Copyright (c) 1987, 1989, 2012, 2020 University of Maryland Department of |
| 2 |
Computer Science. |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
a copy of this software and associated documentation files (the |
| 6 |
"Software"), to deal in the Software without restriction, including |
| 7 |
without limitation, the rights to use, copy, modify, merge, publish, |
| 8 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
permit persons to whom the Software is furnished to do so, subject to |
| 10 |
the following conditions: The above copyright notice, this permission |
| 11 |
notice and the disclaimer statement shall be included in all copies |
| 12 |
or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* DVI page rearrangement program |
| 25 |
* |
| 26 |
* Reads DVI version 2 files and rearranges pages into signatures, |
| 27 |
* writing a new DVI file. |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifdef HAVE_CONFIG_H |
| 31 |
#include <config.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef KPATHSEA |
| 35 |
#include <kpathsea/config.h> |
| 36 |
#include <kpathsea/c-fopen.h> |
| 37 |
#include <kpathsea/getopt.h> |
| 38 |
#if defined(WIN32) |
| 39 |
#include <kpathsea/variable.h> |
| 40 |
#endif |
| 41 |
#else |
| 42 |
#define FOPEN_RBIN_MODE "rb" |
| 43 |
#define FOPEN_WBIN_MODE "wb" |
| 44 |
#define SET_BINARY(x) (void)0 |
| 45 |
extern char *optarg; |
| 46 |
extern int optind; |
| 47 |
#endif |
| 48 |
#if defined(WIN32) |
| 49 |
#include <sys/stat.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#include "types.h" |
| 53 |
#include "dviclass.h" |
| 54 |
#include "dvicodes.h" |
| 55 |
#include "error.h" |
| 56 |
#include "fio.h" |
| 57 |
#include "gripes.h" |
| 58 |
#include "search.h" |
| 59 |
#include <stdio.h> |
| 60 |
#include <ctype.h> |
| 61 |
#include "seek.h" |
| 62 |
#include "common.h" |
| 63 |
|
| 64 |
#define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',') |
| 65 |
|
| 66 |
char *ProgName; |
| 67 |
|
| 68 |
/* Globals */ |
| 69 |
char serrbuf[BUFSIZ]; /* buffer for stderr */ |
| 70 |
|
| 71 |
/* |
| 72 |
* We will try to keep output lines shorter than MAXCOL characters. |
| 73 |
*/ |
| 74 |
#define MAXCOL 75 |
| 75 |
|
| 76 |
/* |
| 77 |
* We use the following structure to keep track of fonts we have seen. |
| 78 |
* The final DVI file lists only the fonts it uses. |
| 79 |
*/ |
| 80 |
struct fontinfo { |
| 81 |
i32 fi_newindex; /* font number in output file */ |
| 82 |
int fi_reallyused; /* true => used on a page we copied */ |
| 83 |
i32 fi_checksum; /* the checksum */ |
| 84 |
i32 fi_mag; /* the magnification */ |
| 85 |
i32 fi_designsize; /* the design size */ |
| 86 |
short fi_n1; /* the name header length */ |
| 87 |
short fi_n2; /* the name body length */ |
| 88 |
char *fi_name; /* the name itself */ |
| 89 |
}; |
| 90 |
|
| 91 |
int Signature; /* #pages per signature (multiple of 4) */ |
| 92 |
|
| 93 |
int SFlag; /* true => -s, silent operation */ |
| 94 |
|
| 95 |
struct search *FontFinder; /* maps from input indicies to fontinfo */ |
| 96 |
i32 NextOutputFontIndex; /* generates output indicies */ |
| 97 |
i32 CurrentFontIndex; /* current (old) index in input */ |
| 98 |
i32 OutputFontIndex; /* current (new) index in ouput */ |
| 99 |
|
| 100 |
const char *DVIFileName; /* name of input DVI file */ |
| 101 |
FILE *inf; /* the input file itself */ |
| 102 |
FILE *outf; /* the output DVI file */ |
| 103 |
|
| 104 |
long *StartOfPage; /* The file positions of the input pages */ |
| 105 |
long StartOfPageSpace; /* Number of entries of StartOfPage array */ |
| 106 |
|
| 107 |
long StartOfLastPage; /* The file position just before we |
| 108 |
started the last page */ |
| 109 |
long CurrentPosition; /* The current position of the file */ |
| 110 |
|
| 111 |
int UseThisPage; /* true => current page is selected */ |
| 112 |
|
| 113 |
i32 InputPageNumber; /* current absolute page in old DVI file */ |
| 114 |
int NumberOfOutputPages; /* number of pages in new DVI file */ |
| 115 |
#ifdef ASCIIPTEX |
| 116 |
int ptexdvi; /* true if dvi file is extended (TATEKUMI) */ |
| 117 |
int inverse; /* true if 'right-hand side binding' */ |
| 118 |
#endif |
| 119 |
|
| 120 |
i32 Numerator; /* numerator from DVI file */ |
| 121 |
i32 Denominator; /* denominator from DVI file */ |
| 122 |
i32 DVIMag; /* magnification from DVI file */ |
| 123 |
|
| 124 |
i32 Count[10]; /* the 10 \count variables */ |
| 125 |
|
| 126 |
/* save some string space: we use this a lot */ |
| 127 |
char writeerr[] = "error writing DVI file"; |
| 128 |
|
| 129 |
#ifndef KPATHSEA |
| 130 |
void *malloc(), *realloc(); |
| 131 |
void free(); |
| 132 |
#endif |
| 133 |
/* |
| 134 |
* You may get lint warnings about sprintf's return value. |
| 135 |
* Older versions of 4BSD have `char *sprintf()'. ANSI and |
| 136 |
* SysV use `int sprintf()'; so ignore the warnings. |
| 137 |
*/ |
| 138 |
|
| 139 |
/* |
| 140 |
* Lint gets somewhat confused over putc. |
| 141 |
*/ |
| 142 |
#ifdef lint |
| 143 |
#undef putc |
| 144 |
#ifdef ultrix /* grr */ |
| 145 |
#define putc(c, f) fputc((char)(c), f) |
| 146 |
#else |
| 147 |
#define putc(c, f) fputc((int)(c), f) |
| 148 |
#endif |
| 149 |
#endif |
| 150 |
|
| 151 |
static void WriteFont(struct fontinfo *fi); |
| 152 |
static void PutFontSelector(i32 index); |
| 153 |
static void ScanDVIFile(void); |
| 154 |
static void HandleDVIFile(void); |
| 155 |
#ifdef ASCIIPTEX |
| 156 |
static void HandleDVIFileRighty(void); |
| 157 |
#endif |
| 158 |
static int HandlePage(void); |
| 159 |
static void PutEmptyPage(void); |
| 160 |
|
| 161 |
/* |
| 162 |
* Print a message to stderr, with an optional leading space, and handling |
| 163 |
* long line wraps. |
| 164 |
*/ |
| 165 |
static void |
| 166 |
message(int space, const char *str, int len) |
| 167 |
{ |
| 168 |
static int beenhere; |
| 169 |
static int col; |
| 170 |
|
| 171 |
if (!beenhere) |
| 172 |
space = 0, beenhere++; |
| 173 |
if (len == 0) |
| 174 |
len = strlen(str); |
| 175 |
col += len; |
| 176 |
if (space) { |
| 177 |
if (col >= MAXCOL) |
| 178 |
(void) putc('\n', stderr), col = len; |
| 179 |
else |
| 180 |
(void) putc(' ', stderr), col++; |
| 181 |
} |
| 182 |
while (--len >= 0) |
| 183 |
(void) putc(*str++, stderr); |
| 184 |
(void) fflush(stderr); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* Start a page (process a DVI_BOP). |
| 189 |
*/ |
| 190 |
static void |
| 191 |
BeginPage(void) |
| 192 |
{ |
| 193 |
register i32 *i; |
| 194 |
|
| 195 |
OutputFontIndex = -1; /* new page requires respecifying font */ |
| 196 |
for (i = Count; i < &Count[10]; i++) |
| 197 |
fGetLong(inf, *i); |
| 198 |
(void) GetLong(inf); /* previous page pointer */ |
| 199 |
|
| 200 |
if (!UseThisPage) |
| 201 |
return; |
| 202 |
|
| 203 |
putbyte(outf, DVI_BOP); |
| 204 |
for (i = Count; i < &Count[10]; i++) |
| 205 |
PutLong(outf, *i); |
| 206 |
PutLong(outf, StartOfLastPage); |
| 207 |
if (ferror(outf)) |
| 208 |
error(1, -1, writeerr); |
| 209 |
|
| 210 |
StartOfLastPage = CurrentPosition; |
| 211 |
CurrentPosition += 45; /* we just wrote this much */ |
| 212 |
|
| 213 |
if (!SFlag) { /* write nice page usage messages */ |
| 214 |
register int z = 0; |
| 215 |
register int mlen = 0; |
| 216 |
char msg[80]; |
| 217 |
|
| 218 |
(void) sprintf(msg, "[%ld", (long)Count[0]); |
| 219 |
mlen = strlen(msg); |
| 220 |
for (i = &Count[1]; i < &Count[10]; i++) { |
| 221 |
if (*i == 0) { |
| 222 |
z++; |
| 223 |
continue; |
| 224 |
} |
| 225 |
while (--z >= 0) |
| 226 |
msg[mlen++] = '.', msg[mlen++] = '0'; |
| 227 |
z = 0; |
| 228 |
(void) sprintf(msg + mlen, ".%ld", (long)*i); |
| 229 |
mlen += strlen(msg + mlen); |
| 230 |
} |
| 231 |
message(1, msg, mlen); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* End a page (process a DVI_EOP). |
| 237 |
*/ |
| 238 |
static void |
| 239 |
EndPage(void) |
| 240 |
{ |
| 241 |
|
| 242 |
if (!UseThisPage) |
| 243 |
return; |
| 244 |
if (!SFlag) |
| 245 |
message(0, "]", 1); |
| 246 |
putbyte(outf, DVI_EOP); |
| 247 |
if (ferror(outf)) |
| 248 |
error(1, -1, writeerr); |
| 249 |
CurrentPosition++; |
| 250 |
NumberOfOutputPages++; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* For each of the fonts used in the new DVI file, write out a definition. |
| 255 |
*/ |
| 256 |
/* ARGSUSED */ |
| 257 |
static void |
| 258 |
PostAmbleFontEnumerator(char *addr, i32 key) |
| 259 |
{ |
| 260 |
|
| 261 |
if (((struct fontinfo *)addr)->fi_reallyused) |
| 262 |
WriteFont((struct fontinfo *)addr); |
| 263 |
} |
| 264 |
|
| 265 |
static void |
| 266 |
HandlePostAmble(void) |
| 267 |
{ |
| 268 |
register i32 c; |
| 269 |
|
| 270 |
(void) GetLong(inf); /* previous page pointer */ |
| 271 |
if (GetLong(inf) != Numerator) |
| 272 |
GripeMismatchedValue("numerator"); |
| 273 |
if (GetLong(inf) != Denominator) |
| 274 |
GripeMismatchedValue("denominator"); |
| 275 |
if (GetLong(inf) != DVIMag) |
| 276 |
GripeMismatchedValue("\\magnification"); |
| 277 |
|
| 278 |
putbyte(outf, DVI_POST); |
| 279 |
PutLong(outf, StartOfLastPage); |
| 280 |
PutLong(outf, Numerator); |
| 281 |
PutLong(outf, Denominator); |
| 282 |
PutLong(outf, DVIMag); |
| 283 |
c = GetLong(inf); |
| 284 |
PutLong(outf, c); /* tallest page height */ |
| 285 |
c = GetLong(inf); |
| 286 |
PutLong(outf, c); /* widest page width */ |
| 287 |
c = GetWord(inf); |
| 288 |
PutWord(outf, c); /* DVI stack size */ |
| 289 |
PutWord(outf, NumberOfOutputPages); |
| 290 |
StartOfLastPage = CurrentPosition; /* point at post */ |
| 291 |
CurrentPosition += 29; /* count all those `put's */ |
| 292 |
#ifdef notdef |
| 293 |
(void) GetWord(inf); /* skip original number of pages */ |
| 294 |
#endif |
| 295 |
|
| 296 |
/* |
| 297 |
* just ignore all the incoming font definitions; we are done with |
| 298 |
* input file |
| 299 |
*/ |
| 300 |
|
| 301 |
/* |
| 302 |
* run through the FontFinder table and dump definitions for the |
| 303 |
* fonts we have used. |
| 304 |
*/ |
| 305 |
SEnumerate(FontFinder, PostAmbleFontEnumerator); |
| 306 |
|
| 307 |
putbyte(outf, DVI_POSTPOST); |
| 308 |
PutLong(outf, StartOfLastPage); /* actually start of postamble */ |
| 309 |
#ifdef ASCIIPTEX |
| 310 |
if(ptexdvi) |
| 311 |
putbyte(outf, DVI_PTEXVERSION); |
| 312 |
else |
| 313 |
#endif |
| 314 |
putbyte(outf, DVI_VERSION); |
| 315 |
putbyte(outf, DVI_FILLER); |
| 316 |
putbyte(outf, DVI_FILLER); |
| 317 |
putbyte(outf, DVI_FILLER); |
| 318 |
putbyte(outf, DVI_FILLER); |
| 319 |
CurrentPosition += 10; |
| 320 |
while (CurrentPosition & 3) { |
| 321 |
putbyte(outf, DVI_FILLER); |
| 322 |
CurrentPosition++; |
| 323 |
} |
| 324 |
if (ferror(outf)) |
| 325 |
error(1, -1, writeerr); |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Write a font definition to the output file |
| 330 |
*/ |
| 331 |
static void |
| 332 |
WriteFont(struct fontinfo *fi) |
| 333 |
{ |
| 334 |
register int l; |
| 335 |
register char *s; |
| 336 |
|
| 337 |
if (fi->fi_newindex < 256) { |
| 338 |
putbyte(outf, DVI_FNTDEF1); |
| 339 |
putbyte(outf, fi->fi_newindex); |
| 340 |
CurrentPosition += 2; |
| 341 |
} else if (fi->fi_newindex < 65536) { |
| 342 |
putbyte(outf, DVI_FNTDEF2); |
| 343 |
PutWord(outf, fi->fi_newindex); |
| 344 |
CurrentPosition += 3; |
| 345 |
} else if (fi->fi_newindex < 16777216) { |
| 346 |
putbyte(outf, DVI_FNTDEF3); |
| 347 |
Put3Byte(outf, fi->fi_newindex); |
| 348 |
CurrentPosition += 4; |
| 349 |
} else { |
| 350 |
putbyte(outf, DVI_FNTDEF4); |
| 351 |
PutLong(outf, fi->fi_newindex); |
| 352 |
CurrentPosition += 5; |
| 353 |
} |
| 354 |
PutLong(outf, fi->fi_checksum); |
| 355 |
PutLong(outf, fi->fi_mag); |
| 356 |
PutLong(outf, fi->fi_designsize); |
| 357 |
putbyte(outf, fi->fi_n1); |
| 358 |
putbyte(outf, fi->fi_n2); |
| 359 |
l = fi->fi_n1 + fi->fi_n2; |
| 360 |
CurrentPosition += 14 + l; |
| 361 |
s = fi->fi_name; |
| 362 |
while (--l >= 0) |
| 363 |
putbyte(outf, *s++); |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* Handle the preamble. Someday we should update the comment field. |
| 368 |
*/ |
| 369 |
static void |
| 370 |
HandlePreAmble(void) |
| 371 |
{ |
| 372 |
register int n, c; |
| 373 |
|
| 374 |
c = getc(inf); |
| 375 |
if (c == EOF) |
| 376 |
GripeUnexpectedDVIEOF(); |
| 377 |
if (c != DVI_PRE) |
| 378 |
GripeMissingOp("PRE"); |
| 379 |
if (getc(inf) != DVI_VERSION) |
| 380 |
error(1, 0, "%s is not a DVI version %d file", |
| 381 |
DVIFileName, DVI_VERSION); |
| 382 |
Numerator = GetLong(inf); |
| 383 |
Denominator = GetLong(inf); |
| 384 |
DVIMag = GetLong(inf); |
| 385 |
putbyte(outf, DVI_PRE); |
| 386 |
putbyte(outf, DVI_VERSION); |
| 387 |
PutLong(outf, Numerator); |
| 388 |
PutLong(outf, Denominator); |
| 389 |
PutLong(outf, DVIMag); |
| 390 |
|
| 391 |
n = UnSign8(GetByte(inf)); |
| 392 |
CurrentPosition = 15 + n; /* well, almost */ |
| 393 |
putbyte(outf, n); |
| 394 |
while (--n >= 0) { |
| 395 |
c = GetByte(inf); |
| 396 |
putbyte(outf, c); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
int |
| 401 |
main(int argc, char **argv) |
| 402 |
{ |
| 403 |
register int c; |
| 404 |
register char *s; |
| 405 |
char *outname = NULL; |
| 406 |
#if defined(WIN32) && defined(KPATHSEA) |
| 407 |
int ac; |
| 408 |
char **av, *enc; |
| 409 |
|
| 410 |
if (argc>1) { |
| 411 |
kpse_set_program_name(argv[0], "dvibook"); |
| 412 |
enc = kpse_var_value("command_line_encoding"); |
| 413 |
if (get_command_line_args_utf8(enc, &ac, &av)) { |
| 414 |
argc = ac; |
| 415 |
argv = av; |
| 416 |
} |
| 417 |
} |
| 418 |
#endif |
| 419 |
|
| 420 |
Signature = 0; |
| 421 |
|
| 422 |
#ifdef ASCIIPTEX |
| 423 |
inverse = 0; |
| 424 |
#endif |
| 425 |
ProgName = *argv; |
| 426 |
setbuf(stderr, serrbuf); |
| 427 |
|
| 428 |
#ifdef ASCIIPTEX |
| 429 |
while ((c = getopt(argc, argv, "i:o:s:qr")) != EOF) { |
| 430 |
#else |
| 431 |
while ((c = getopt(argc, argv, "i:o:s:q")) != EOF) { |
| 432 |
#endif |
| 433 |
switch (c) { |
| 434 |
|
| 435 |
case 'q': /* silent */ |
| 436 |
SFlag++; |
| 437 |
break; |
| 438 |
|
| 439 |
case 'i': |
| 440 |
if (DVIFileName != NULL) |
| 441 |
goto usage; |
| 442 |
DVIFileName = optarg; |
| 443 |
break; |
| 444 |
|
| 445 |
case 'o': |
| 446 |
if (outname != NULL) |
| 447 |
goto usage; |
| 448 |
outname = optarg; |
| 449 |
break; |
| 450 |
|
| 451 |
case 's': |
| 452 |
if (Signature != 0) |
| 453 |
goto usage; |
| 454 |
Signature = atoi(optarg); |
| 455 |
if (Signature <= 0) |
| 456 |
error(1, -1, "-s parameter must be > 0"); |
| 457 |
if (Signature % 4 != 0) |
| 458 |
error(1, -1, |
| 459 |
"-s parameter must be a multiple of 4"); |
| 460 |
break; |
| 461 |
#ifdef ASCIIPTEX |
| 462 |
case 'r': |
| 463 |
inverse = 1; |
| 464 |
break; |
| 465 |
#endif |
| 466 |
|
| 467 |
case '?': |
| 468 |
usage: |
| 469 |
(void) fprintf(stderr, |
| 470 |
"dvibook in SeeTeX Ver.%s (%s)\n", VERSION, TL_VERSION); |
| 471 |
#ifdef ASCIIPTEX |
| 472 |
(void) fprintf(stderr, "\ |
| 473 |
Usage: %s [-s signature] [-q] [-i infile] [-o outfile] [-r] [infile [outfile]]\n", |
| 474 |
ProgName); |
| 475 |
#else |
| 476 |
(void) fprintf(stderr, "\ |
| 477 |
Usage: %s [-s signature] [-q] [-i infile] [-o outfile] [infile [outfile]]\n", |
| 478 |
ProgName); |
| 479 |
#endif |
| 480 |
(void) fprintf(stderr, |
| 481 |
"\nEmail bug reports to %s.\n", BUG_ADDRESS); |
| 482 |
(void) fflush(stderr); |
| 483 |
exit(1); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
while (optind < argc) { |
| 488 |
s = argv[optind++]; |
| 489 |
c = *s; |
| 490 |
if (DVIFileName == NULL) |
| 491 |
DVIFileName = s; |
| 492 |
else if (outname == NULL) |
| 493 |
outname = s; |
| 494 |
else |
| 495 |
goto usage; |
| 496 |
} |
| 497 |
#if defined(WIN32) |
| 498 |
if (DVIFileName == NULL && outname == NULL) { |
| 499 |
struct stat sti, sto; |
| 500 |
int fdi = fileno(stdin), fdo = fileno(stdout); |
| 501 |
if (isatty(fdi) == 0 && fstat(fdi, &sti) == 0 && S_ISFIFO(sti.st_mode) |
| 502 |
&& isatty(fdo) == 0 && fstat(fdo, &sto) == 0 && S_ISFIFO(sto.st_mode)) { |
| 503 |
goto usage; |
| 504 |
} |
| 505 |
} |
| 506 |
#endif |
| 507 |
if (DVIFileName == NULL) { |
| 508 |
DVIFileName = "`stdin'"; |
| 509 |
inf = stdin; |
| 510 |
if (!isatty(fileno(inf))) |
| 511 |
SET_BINARY(fileno(inf)); |
| 512 |
else |
| 513 |
goto usage; |
| 514 |
} else if ((inf = fopen(DVIFileName, FOPEN_RBIN_MODE)) == 0) |
| 515 |
error(1, -1, "cannot read %s", DVIFileName); |
| 516 |
if (outname == NULL) { |
| 517 |
outf = stdout; |
| 518 |
if (!isatty(fileno(outf))) |
| 519 |
SET_BINARY(fileno(outf)); |
| 520 |
} |
| 521 |
else if ((outf = fopen(outname, FOPEN_WBIN_MODE)) == 0) |
| 522 |
error(1, -1, "cannot write %s", outname); |
| 523 |
|
| 524 |
if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0) |
| 525 |
error(1, 0, "cannot create font finder (out of memory?)"); |
| 526 |
|
| 527 |
/* copy inf to TEMP file if not seekable */ |
| 528 |
if ((inf = SeekFile(inf)) == NULL) { |
| 529 |
error(1, 0, "can't seek file"); |
| 530 |
} |
| 531 |
#ifdef ASCIIPTEX |
| 532 |
ptexdvi = 0; |
| 533 |
#endif |
| 534 |
InputPageNumber = 0; |
| 535 |
StartOfLastPage = -1; |
| 536 |
StartOfPageSpace = 32; |
| 537 |
StartOfPage = malloc(sizeof(long) * StartOfPageSpace); |
| 538 |
if (!StartOfPage) { |
| 539 |
error(1, -1, "cannot allocate list of pages; out of memory"); |
| 540 |
} |
| 541 |
|
| 542 |
HandlePreAmble(); |
| 543 |
ScanDVIFile(); |
| 544 |
#ifdef ASCIIPTEX |
| 545 |
if(inverse) |
| 546 |
HandleDVIFileRighty(); |
| 547 |
else |
| 548 |
#endif |
| 549 |
HandleDVIFile(); |
| 550 |
free(StartOfPage); |
| 551 |
HandlePostAmble(); |
| 552 |
if (!SFlag) |
| 553 |
(void) fprintf(stderr, "\nWrote %d page%s, %ld bytes\n", |
| 554 |
NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s", |
| 555 |
(long)CurrentPosition); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
* Handle a font definition. |
| 561 |
*/ |
| 562 |
static void |
| 563 |
HandleFontDef(i32 index) |
| 564 |
{ |
| 565 |
register struct fontinfo *fi; |
| 566 |
register int i; |
| 567 |
register char *s; |
| 568 |
int def = S_CREATE | S_EXCL; |
| 569 |
|
| 570 |
if (!UseThisPage) { |
| 571 |
if ((fi = (struct fontinfo *)SSearch(FontFinder, index, &def)) == 0) { |
| 572 |
if (def & S_COLL) |
| 573 |
error(1, 0, "font %ld already defined", (long)index); |
| 574 |
else |
| 575 |
error(1, 0, "cannot stash font %ld (out of memory?)", |
| 576 |
(long)index); |
| 577 |
} |
| 578 |
fi->fi_reallyused = 0; |
| 579 |
fi->fi_checksum = GetLong(inf); |
| 580 |
fi->fi_mag = GetLong(inf); |
| 581 |
fi->fi_designsize = GetLong(inf); |
| 582 |
fi->fi_n1 = UnSign8(GetByte(inf)); |
| 583 |
fi->fi_n2 = UnSign8(GetByte(inf)); |
| 584 |
i = fi->fi_n1 + fi->fi_n2; |
| 585 |
if ((s = malloc((unsigned)i)) == 0) |
| 586 |
GripeOutOfMemory(i, "font name"); |
| 587 |
fi->fi_name = s; |
| 588 |
while (--i >= 0) |
| 589 |
*s++ = GetByte(inf); |
| 590 |
} else { |
| 591 |
(void) GetLong(inf); |
| 592 |
(void) GetLong(inf); |
| 593 |
(void) GetLong(inf); |
| 594 |
i = UnSign8(GetByte(inf)); |
| 595 |
i += UnSign8(GetByte(inf)); |
| 596 |
while (--i >= 0) |
| 597 |
(void) GetByte(inf); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
/* |
| 602 |
* Handle a \special. |
| 603 |
*/ |
| 604 |
static void |
| 605 |
HandleSpecial(int c, int l, i32 p) |
| 606 |
{ |
| 607 |
register int i; |
| 608 |
|
| 609 |
if (UseThisPage) { |
| 610 |
putbyte(outf, c); |
| 611 |
switch (l) { |
| 612 |
|
| 613 |
case DPL_UNS1: |
| 614 |
putbyte(outf, p); |
| 615 |
CurrentPosition += 2; |
| 616 |
break; |
| 617 |
|
| 618 |
case DPL_UNS2: |
| 619 |
PutWord(outf, p); |
| 620 |
CurrentPosition += 3; |
| 621 |
break; |
| 622 |
|
| 623 |
case DPL_UNS3: |
| 624 |
Put3Byte(outf, p); |
| 625 |
CurrentPosition += 4; |
| 626 |
break; |
| 627 |
|
| 628 |
case DPL_SGN4: |
| 629 |
PutLong(outf, p); |
| 630 |
CurrentPosition += 5; |
| 631 |
break; |
| 632 |
|
| 633 |
default: |
| 634 |
panic("HandleSpecial l=%d", l); |
| 635 |
/* NOTREACHED */ |
| 636 |
} |
| 637 |
CurrentPosition += p; |
| 638 |
while (--p >= 0) { |
| 639 |
i = getc(inf); |
| 640 |
putbyte(outf, i); |
| 641 |
} |
| 642 |
if (feof(inf)) |
| 643 |
GripeUnexpectedDVIEOF(); |
| 644 |
if (ferror(outf)) |
| 645 |
error(1, -1, writeerr); |
| 646 |
} else |
| 647 |
while (--p >= 0) |
| 648 |
(void) getc(inf); |
| 649 |
} |
| 650 |
|
| 651 |
static void |
| 652 |
ReallyUseFont(void) |
| 653 |
{ |
| 654 |
register struct fontinfo *fi; |
| 655 |
int look = S_LOOKUP; |
| 656 |
|
| 657 |
fi = (struct fontinfo *)SSearch(FontFinder, CurrentFontIndex, &look); |
| 658 |
if (fi == NULL) |
| 659 |
error(1, 0, "DVI file requested font %ld without defining it", |
| 660 |
(long)CurrentFontIndex); |
| 661 |
if (fi->fi_reallyused == 0) { |
| 662 |
fi->fi_reallyused++; |
| 663 |
fi->fi_newindex = NextOutputFontIndex++; |
| 664 |
WriteFont(fi); |
| 665 |
} |
| 666 |
if (fi->fi_newindex != OutputFontIndex) { |
| 667 |
PutFontSelector(fi->fi_newindex); |
| 668 |
OutputFontIndex = fi->fi_newindex; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Write a font selection command to the output file |
| 674 |
*/ |
| 675 |
static void |
| 676 |
PutFontSelector(i32 index) |
| 677 |
{ |
| 678 |
|
| 679 |
if (index < 64) { |
| 680 |
putbyte(outf, index + DVI_FNTNUM0); |
| 681 |
CurrentPosition++; |
| 682 |
} else if (index < 256) { |
| 683 |
putbyte(outf, DVI_FNT1); |
| 684 |
putbyte(outf, index); |
| 685 |
CurrentPosition += 2; |
| 686 |
} else if (index < 65536) { |
| 687 |
putbyte(outf, DVI_FNT2); |
| 688 |
PutWord(outf, index); |
| 689 |
CurrentPosition += 3; |
| 690 |
} else if (index < 16777216) { |
| 691 |
putbyte(outf, DVI_FNT3); |
| 692 |
Put3Byte(outf, index); |
| 693 |
CurrentPosition += 4; |
| 694 |
} else { |
| 695 |
putbyte(outf, DVI_FNT4); |
| 696 |
PutLong(outf, index); |
| 697 |
CurrentPosition += 5; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
/* |
| 702 |
* The following table describes the length (in bytes) of each of the DVI |
| 703 |
* commands that we can simply copy, starting with DVI_SET1 (128). |
| 704 |
*/ |
| 705 |
char oplen[128] = { |
| 706 |
0, 0, 0, 0, /* DVI_SET1 .. DVI_SET4 */ |
| 707 |
9, /* DVI_SETRULE */ |
| 708 |
0, 0, 0, 0, /* DVI_PUT1 .. DVI_PUT4 */ |
| 709 |
9, /* DVI_PUTRULE */ |
| 710 |
1, /* DVI_NOP */ |
| 711 |
0, /* DVI_BOP */ |
| 712 |
0, /* DVI_EOP */ |
| 713 |
1, /* DVI_PUSH */ |
| 714 |
1, /* DVI_POP */ |
| 715 |
2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */ |
| 716 |
1, /* DVI_W0 */ |
| 717 |
2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */ |
| 718 |
1, /* DVI_X0 */ |
| 719 |
2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */ |
| 720 |
2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */ |
| 721 |
1, /* DVI_Y0 */ |
| 722 |
2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */ |
| 723 |
1, /* DVI_Z0 */ |
| 724 |
2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */ |
| 725 |
0, /* DVI_FNTNUM0 (171) */ |
| 726 |
0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */ |
| 727 |
0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */ |
| 728 |
0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */ |
| 729 |
0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */ |
| 730 |
0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */ |
| 731 |
0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */ |
| 732 |
0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */ |
| 733 |
0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */ |
| 734 |
0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */ |
| 735 |
0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */ |
| 736 |
0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */ |
| 737 |
0, /* DVI_PRE */ |
| 738 |
0, /* DVI_POST */ |
| 739 |
0, /* DVI_POSTPOST */ |
| 740 |
#ifdef ASCIIPTEX |
| 741 |
0, 0, 0, 0, 0, /* 250 .. 254 */ |
| 742 |
0, /* DVI_DIR */ |
| 743 |
#else |
| 744 |
0, 0, 0, 0, 0, 0, /* 250 .. 255 */ |
| 745 |
#endif |
| 746 |
}; |
| 747 |
|
| 748 |
/* |
| 749 |
* Here we scan the input DVI file and record pointers to the pages. |
| 750 |
*/ |
| 751 |
static void |
| 752 |
ScanDVIFile(void) |
| 753 |
{ |
| 754 |
long *tmp; |
| 755 |
|
| 756 |
UseThisPage = 0; |
| 757 |
|
| 758 |
StartOfPage[InputPageNumber] = ftell(inf); |
| 759 |
while (HandlePage()) { /* scan DVI file */ |
| 760 |
++InputPageNumber; |
| 761 |
if (InputPageNumber >= StartOfPageSpace) { |
| 762 |
StartOfPageSpace *= 2; |
| 763 |
tmp = realloc(StartOfPage, sizeof(long) * StartOfPageSpace); |
| 764 |
if (!tmp) { |
| 765 |
error(1, -1, "cannot grow list of pages; out of memory"); |
| 766 |
} |
| 767 |
StartOfPage = tmp; |
| 768 |
} |
| 769 |
|
| 770 |
StartOfPage[InputPageNumber] = ftell(inf); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
/* |
| 775 |
* Here we read the input DVI file and write relevant pages to the |
| 776 |
* output DVI file. We also keep track of font changes, handle font |
| 777 |
* definitions, and perform some other housekeeping. |
| 778 |
*/ |
| 779 |
static void |
| 780 |
HandleDVIFile(void) |
| 781 |
{ |
| 782 |
int CurrentPage, ActualPage, MaxPage; |
| 783 |
|
| 784 |
UseThisPage = 1; |
| 785 |
|
| 786 |
MaxPage = InputPageNumber + (4-InputPageNumber%4)%4; |
| 787 |
|
| 788 |
if (!Signature) |
| 789 |
Signature = MaxPage; |
| 790 |
for (CurrentPage = 0; CurrentPage < MaxPage; CurrentPage++) { |
| 791 |
ActualPage = CurrentPage - CurrentPage%Signature; |
| 792 |
switch (CurrentPage%4) { |
| 793 |
case 0: |
| 794 |
case 3: |
| 795 |
ActualPage += Signature-1-(CurrentPage%Signature)/2; |
| 796 |
break; |
| 797 |
case 1: |
| 798 |
case 2: |
| 799 |
ActualPage += (CurrentPage%Signature)/2; |
| 800 |
break; |
| 801 |
} |
| 802 |
if (ActualPage < InputPageNumber) { |
| 803 |
if (fseek(inf, StartOfPage[ActualPage], 0) == -1) |
| 804 |
error(1, -1, |
| 805 |
"can't seek page %d", ActualPage+1); |
| 806 |
HandlePage(); |
| 807 |
} else |
| 808 |
PutEmptyPage(); |
| 809 |
} |
| 810 |
if (fseek(inf, StartOfPage[InputPageNumber]+1, 0) == -1) |
| 811 |
error(1, -1, "can't seek last page"); |
| 812 |
} |
| 813 |
|
| 814 |
#ifdef ASCIIPTEX |
| 815 |
/* |
| 816 |
* Here we read the input DVI file and write relevant pages to the |
| 817 |
* output DVI file. We also keep track of font changes, handle font |
| 818 |
* definitions, and perform some other housekeeping. |
| 819 |
*/ |
| 820 |
static void |
| 821 |
HandleDVIFileRighty(void) |
| 822 |
{ |
| 823 |
int CurrentPage, ActualPage, MaxPage; |
| 824 |
|
| 825 |
UseThisPage = 1; |
| 826 |
|
| 827 |
MaxPage = InputPageNumber + (4-InputPageNumber%4)%4; |
| 828 |
|
| 829 |
if (!Signature) |
| 830 |
Signature = MaxPage; |
| 831 |
for (CurrentPage = 0; CurrentPage < MaxPage; CurrentPage++) { |
| 832 |
ActualPage = CurrentPage - CurrentPage%Signature; |
| 833 |
switch (CurrentPage%4) { |
| 834 |
case 1: /* case 0: */ |
| 835 |
case 2: /* case 3: */ |
| 836 |
ActualPage += Signature-1-(CurrentPage%Signature)/2; |
| 837 |
break; |
| 838 |
case 0: /* case 1: */ |
| 839 |
case 3: /* case 2: */ |
| 840 |
ActualPage += (CurrentPage%Signature)/2; |
| 841 |
break; |
| 842 |
} |
| 843 |
if (ActualPage < InputPageNumber) { |
| 844 |
if (fseek(inf, StartOfPage[ActualPage], 0) == -1) |
| 845 |
error(1, -1, |
| 846 |
"can't seek page %d", ActualPage+1); |
| 847 |
HandlePage(); |
| 848 |
} else |
| 849 |
PutEmptyPage(); |
| 850 |
} |
| 851 |
if (fseek(inf, StartOfPage[InputPageNumber]+1, 0) == -1) |
| 852 |
error(1, -1, "can't seek last page"); |
| 853 |
} |
| 854 |
#endif |
| 855 |
|
| 856 |
static int |
| 857 |
HandlePage(void) |
| 858 |
{ |
| 859 |
register int c, l; |
| 860 |
register i32 p = 0; /* avoid uninitialized warning */ |
| 861 |
register int CurrentFontOK = 0; |
| 862 |
int doingpage = 0; |
| 863 |
|
| 864 |
/* Only way out is via "return" statement */ |
| 865 |
for (;;) { |
| 866 |
c = getc(inf); /* getc() returns unsigned values */ |
| 867 |
if (DVI_IsChar(c)) { |
| 868 |
/* |
| 869 |
* Copy chars, note font usage, but ignore if |
| 870 |
* page is not interesting. |
| 871 |
*/ |
| 872 |
if (!UseThisPage) |
| 873 |
continue; |
| 874 |
if (!CurrentFontOK) { |
| 875 |
ReallyUseFont(); |
| 876 |
CurrentFontOK++; |
| 877 |
} |
| 878 |
putbyte(outf, c); |
| 879 |
CurrentPosition++; |
| 880 |
continue; |
| 881 |
} |
| 882 |
if (DVI_IsFont(c)) { /* note font change */ |
| 883 |
CurrentFontIndex = c - DVI_FNTNUM0; |
| 884 |
CurrentFontOK = 0; |
| 885 |
continue; |
| 886 |
} |
| 887 |
if (c == EOF) |
| 888 |
GripeUnexpectedDVIEOF(); |
| 889 |
if ((l = (oplen - 128)[c]) != 0) { /* simple copy */ |
| 890 |
if (!UseThisPage) { |
| 891 |
while (--l > 0) |
| 892 |
(void) getc(inf); |
| 893 |
continue; |
| 894 |
} |
| 895 |
CurrentPosition += l; |
| 896 |
putbyte(outf, c); |
| 897 |
while (--l > 0) { |
| 898 |
c = getc(inf); |
| 899 |
putbyte(outf, c); |
| 900 |
} |
| 901 |
if (ferror(outf)) |
| 902 |
error(1, -1, writeerr); |
| 903 |
continue; |
| 904 |
} |
| 905 |
if ((l = DVI_OpLen(c)) != 0) { |
| 906 |
/* |
| 907 |
* Handle other generics. |
| 908 |
* N.B.: there should only be unsigned parameters |
| 909 |
* here (save SGN4), for commands with negative |
| 910 |
* parameters have been taken care of above. |
| 911 |
*/ |
| 912 |
switch (l) { |
| 913 |
|
| 914 |
case DPL_UNS1: |
| 915 |
p = getc(inf); |
| 916 |
break; |
| 917 |
|
| 918 |
case DPL_UNS2: |
| 919 |
fGetWord(inf, p); |
| 920 |
break; |
| 921 |
|
| 922 |
case DPL_UNS3: |
| 923 |
fGet3Byte(inf, p); |
| 924 |
break; |
| 925 |
|
| 926 |
case DPL_SGN4: |
| 927 |
fGetLong(inf, p); |
| 928 |
break; |
| 929 |
|
| 930 |
default: |
| 931 |
panic("HandleDVIFile l=%d", l); |
| 932 |
} |
| 933 |
|
| 934 |
/* |
| 935 |
* Now that we have the parameter, perform the |
| 936 |
* command. |
| 937 |
*/ |
| 938 |
switch (DVI_DT(c)) { |
| 939 |
|
| 940 |
case DT_SET: |
| 941 |
case DT_PUT: |
| 942 |
if (!UseThisPage) |
| 943 |
continue; |
| 944 |
if (!CurrentFontOK) { |
| 945 |
ReallyUseFont(); |
| 946 |
CurrentFontOK++; |
| 947 |
} |
| 948 |
putbyte(outf, c); |
| 949 |
switch (l) { |
| 950 |
|
| 951 |
case DPL_UNS1: |
| 952 |
putbyte(outf, p); |
| 953 |
CurrentPosition += 2; |
| 954 |
continue; |
| 955 |
|
| 956 |
case DPL_UNS2: |
| 957 |
PutWord(outf, p); |
| 958 |
CurrentPosition += 3; |
| 959 |
continue; |
| 960 |
|
| 961 |
case DPL_UNS3: |
| 962 |
Put3Byte(outf, p); |
| 963 |
CurrentPosition += 4; |
| 964 |
continue; |
| 965 |
|
| 966 |
case DPL_SGN4: |
| 967 |
PutLong(outf, p); |
| 968 |
CurrentPosition += 5; |
| 969 |
continue; |
| 970 |
} |
| 971 |
|
| 972 |
case DT_FNT: |
| 973 |
CurrentFontIndex = p; |
| 974 |
CurrentFontOK = 0; |
| 975 |
continue; |
| 976 |
|
| 977 |
case DT_XXX: |
| 978 |
HandleSpecial(c, l, p); |
| 979 |
continue; |
| 980 |
|
| 981 |
case DT_FNTDEF: |
| 982 |
HandleFontDef(p); |
| 983 |
continue; |
| 984 |
|
| 985 |
#ifdef ASCIIPTEX |
| 986 |
case DT_DIR: |
| 987 |
ptexdvi = 1; |
| 988 |
|
| 989 |
putbyte(outf, c); |
| 990 |
putbyte(outf, p); |
| 991 |
CurrentPosition += 2; |
| 992 |
continue; |
| 993 |
#endif |
| 994 |
|
| 995 |
default: |
| 996 |
panic("HandleDVIFile DVI_DT(%d)=%d", |
| 997 |
c, DVI_DT(c)); |
| 998 |
} |
| 999 |
continue; |
| 1000 |
} |
| 1001 |
|
| 1002 |
switch (c) { /* handle the few remaining cases */ |
| 1003 |
|
| 1004 |
case DVI_BOP: |
| 1005 |
if (doingpage) |
| 1006 |
GripeUnexpectedOp("BOP (during page)"); |
| 1007 |
BeginPage(); |
| 1008 |
doingpage = 1; |
| 1009 |
break; |
| 1010 |
|
| 1011 |
case DVI_EOP: |
| 1012 |
if (!doingpage) |
| 1013 |
GripeUnexpectedOp("EOP (outside page)"); |
| 1014 |
EndPage(); |
| 1015 |
doingpage = 0; |
| 1016 |
return(1); |
| 1017 |
|
| 1018 |
case DVI_PRE: |
| 1019 |
GripeUnexpectedOp("PRE"); |
| 1020 |
/* NOTREACHED */ |
| 1021 |
|
| 1022 |
case DVI_POST: |
| 1023 |
if (doingpage) |
| 1024 |
GripeUnexpectedOp("POST (inside page)"); |
| 1025 |
return(0); |
| 1026 |
|
| 1027 |
case DVI_POSTPOST: |
| 1028 |
GripeUnexpectedOp("POSTPOST"); |
| 1029 |
/* NOTREACHED */ |
| 1030 |
|
| 1031 |
default: |
| 1032 |
GripeUndefinedOp(c); |
| 1033 |
/* NOTREACHED */ |
| 1034 |
} |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* write an empty page to fill out space */ |
| 1039 |
static void |
| 1040 |
PutEmptyPage(void) |
| 1041 |
{ |
| 1042 |
int i; |
| 1043 |
|
| 1044 |
putbyte(outf, DVI_BOP); |
| 1045 |
PutLong(outf, -1L); |
| 1046 |
for (i = 1; i < 10; i++) /* set all sub counts to 0 */ |
| 1047 |
PutLong(outf, 0L); |
| 1048 |
PutLong(outf, StartOfLastPage); |
| 1049 |
putbyte(outf, DVI_EOP); |
| 1050 |
if (!SFlag) { /* write nice page usage messages */ |
| 1051 |
const char *msg = "[*]"; |
| 1052 |
message(1, msg, strlen(msg)); |
| 1053 |
} |
| 1054 |
if (ferror(outf)) |
| 1055 |
error(1, -1, writeerr); |
| 1056 |
|
| 1057 |
StartOfLastPage = CurrentPosition; |
| 1058 |
CurrentPosition += 46; /* we just wrote this much */ |
| 1059 |
NumberOfOutputPages++; |
| 1060 |
} |