| 1 |
/* |
| 2 |
* This program converts dvi files to dvi files; |
| 3 |
* the command line options are: |
| 4 |
* |
| 5 |
* page n is first page selected -f n |
| 6 |
* page n is last page selected -l n |
| 7 |
* print at most n pages -n n |
| 8 |
* include pages (ranges allowed) -i { n1..n2 | n1 }[,...] |
| 9 |
* exclude pages (ranges allowed) -x { n1..n2 | n1 }[,...] |
| 10 |
* work in quiet mode -q |
| 11 |
* reverse pages -r |
| 12 |
* select even -m 2:1 |
| 13 |
* select odd -m 2:0 |
| 14 |
* print both on same page -m 2:0,1(5.5in,0in) |
| 15 |
* do folded brochures -m 4:-3,0(5.5in,0in) |
| 16 |
* -m 4:1,-2(5.5in,0in) |
| 17 |
* round up number of pages to a |
| 18 |
* multiple of n -p n |
| 19 |
* sort pages to print one from top -j |
| 20 |
* next one from end |
| 21 |
* etc. |
| 22 |
* |
| 23 |
* The original program is by Tomas Rokicki (version 0.5) but it was |
| 24 |
* modified and improved by Esteban ZIMANYI [email protected] |
| 25 |
* to give version 1.0. |
| 26 |
* To give version 1.1 it was improved by [email protected]. |
| 27 |
* |
| 28 |
* This version has been tested for the IBM PC and compatibles under |
| 29 |
* compilers Turbo C 2.0 and Microsoft C 6.0 and for Atari ST under |
| 30 |
* compiler Turbo C 2.0 with MiNTLIB. |
| 31 |
* |
| 32 |
*/ |
| 33 |
#define SEEK_SET 0 |
| 34 |
#define SEEK_CUR 1 |
| 35 |
#define SEEK_END 2 |
| 36 |
|
| 37 |
#define BANNER "\nThis is dvidvi " VERSION " (" TL_VERSION ")," \ |
| 38 |
" Copyright 1988-1991 Radical Eye Software\n" \ |
| 39 |
"Improved from 1.0 to 1.1, 1994, Markus Kohm\n" |
| 40 |
#define BUG_ADDRESS PACKAGE_BUGREPORT |
| 41 |
#define STRINGSIZE (500L) /* maximum number of strings in program */ |
| 42 |
|
| 43 |
#ifdef HAVE_CONFIG_H |
| 44 |
#include <config.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifndef VMS |
| 48 |
#include <stdlib.h> |
| 49 |
#ifdef WIN32 |
| 50 |
#include <malloc.h> |
| 51 |
#endif |
| 52 |
#include <stdio.h> |
| 53 |
#include <string.h> |
| 54 |
#else /* VMS */ |
| 55 |
#include "sys$library:stdio.h" /* AKT: added sys$library: */ |
| 56 |
#include <alloc.h> |
| 57 |
#endif /* VMS */ |
| 58 |
#define MAXPPERP (32) |
| 59 |
|
| 60 |
void error(const char *); |
| 61 |
|
| 62 |
/* defines READBIN, WRITEBIN, PATHSEP and IS_DIR_SEP*/ |
| 63 |
|
| 64 |
#ifdef HAVE_UNISTD_H |
| 65 |
#include <unistd.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef KPATHSEA |
| 69 |
#include <kpathsea/c-fopen.h> |
| 70 |
#define READBIN FOPEN_RBIN_MODE |
| 71 |
#ifdef VMS |
| 72 |
#define WRITEBIN "wb" /* VMS doesn't have FOPEN_WBIN_MODE defined */ |
| 73 |
#else |
| 74 |
#define WRITEBIN FOPEN_WBIN_MODE |
| 75 |
#endif |
| 76 |
#include <kpathsea/c-pathch.h> |
| 77 |
#define PATHSEP ENV_SEP |
| 78 |
#ifdef WIN32 |
| 79 |
#include <kpathsea/config.h> |
| 80 |
#include <kpathsea/variable.h> |
| 81 |
#undef fopen |
| 82 |
#undef fprintf |
| 83 |
#define fopen fsyscp_fopen |
| 84 |
#define fprintf win32_fprintf |
| 85 |
#endif |
| 86 |
#else /* not KPATHSEA */ |
| 87 |
#if defined(__TOS__) |
| 88 |
#define READBIN "rb" /* TOS must use binary mode */ |
| 89 |
#define WRITEBIN "wb" |
| 90 |
#define PATHSEP ';' |
| 91 |
#define IS_DIR_SEP(c) ((c) == '\\') |
| 92 |
#elif defined (MSDOS) || defined(WIN32) |
| 93 |
#define READBIN "rb" /* MSDOS must use binary mode */ |
| 94 |
#define WRITEBIN "wb" |
| 95 |
#define PATHSEP ';' |
| 96 |
#define IS_DIR_SEP(c) ((c) == '\\' || (c) == '/') |
| 97 |
#elif defined(VMS) |
| 98 |
#define READBIN "rb" /* VMS must use binary mode */ |
| 99 |
#define WRITEBIN "wb" |
| 100 |
#define PATHSEP ',' |
| 101 |
#define IS_DIR_SEP ((c) == ':') |
| 102 |
#else |
| 103 |
#define READBIN "r" /* UNIX doesn't care */ |
| 104 |
#define WRITEBIN "w" |
| 105 |
#define PATHSEP ':' |
| 106 |
#define IS_DIR_SEP ((c) == '/') |
| 107 |
#endif |
| 108 |
#endif /* not KPATHSEA */ |
| 109 |
|
| 110 |
/* |
| 111 |
* Type declarations. integer must be a 32-bit signed; shalfword must |
| 112 |
* be a sixteen-bit signed; halfword must be a sixteen-bit unsigned; |
| 113 |
* quarterword must be an eight-bit unsigned. |
| 114 |
*/ |
| 115 |
typedef long integer; |
| 116 |
typedef short shalfword ; |
| 117 |
typedef unsigned short halfword ; |
| 118 |
typedef unsigned char quarterword ; |
| 119 |
typedef short Boolean ; |
| 120 |
|
| 121 |
/* |
| 122 |
* Some globals to keep everyone happy. |
| 123 |
*/ |
| 124 |
integer numpages ; /* the total number of pages in the dvi file. */ |
| 125 |
integer TeXfonts[256] ; /* information about each font */ |
| 126 |
char fontseen[256] ; /* have we defined this font yet? */ |
| 127 |
int modulo ; /* our mod value */ |
| 128 |
integer pagemodulo ; /* additional mod value for number of pages */ |
| 129 |
struct pagespec { |
| 130 |
int pageno, reversed ; |
| 131 |
long hoffset, voffset ; /* in scaled points */ |
| 132 |
} pages[MAXPPERP] ; /* the organization of the pages on output */ |
| 133 |
int pagesperpage ; /* how many pages crammed onto each page? */ |
| 134 |
FILE *infile ; /* input dvi file (cannot be a stream) */ |
| 135 |
FILE *outfile ; /* output dvi file */ |
| 136 |
|
| 137 |
char *temp ; /* a temporary place to put things */ |
| 138 |
char *nextstring, *maxstring ; |
| 139 |
const char *oname=NULL ; /* output dvi file name */ |
| 140 |
char *iname ; /* input dvi file name */ |
| 141 |
char *strings ; /* pointer of the string pool */ |
| 142 |
|
| 143 |
const char banner[] = BANNER ; /* the startup message */ |
| 144 |
integer inlength ; /* the length of the input dvi file */ |
| 145 |
integer postloc ; /* location of the postamble */ |
| 146 |
integer mag ; /* magnification factor */ |
| 147 |
integer pagecount ; /* number of actual pages */ |
| 148 |
integer landscape = 0; /* if landscape special, here it is! */ |
| 149 |
int rem0special ; /* should we remove the first first-page special? */ |
| 150 |
integer prevpp = -1 ; /* previous page pointer on output */ |
| 151 |
integer outputpages ; /* number of pages output */ |
| 152 |
integer dviloc ; /* our position in the output file */ |
| 153 |
integer pagefake ; /* number of pages, rounded up to multiple of |
| 154 |
pagemodulo */ |
| 155 |
|
| 156 |
Boolean firsttransf = 0, lasttransf = 0; |
| 157 |
Boolean jumpingpages = 0; |
| 158 |
integer firstpage ; /* first page selected (option -p) */ |
| 159 |
integer lastpage ; /* last page selected (option -l) */ |
| 160 |
integer maxpages ; /* maximum number of page selected (option -n) */ |
| 161 |
short quiet ; /* quiet mode (option -q) */ |
| 162 |
Boolean exctransf[40][2] ; /* if the ranges of pages to exclude (option -x) |
| 163 |
have to be transformed */ |
| 164 |
integer exclude[40][2] ; /* the ranges of pages to exclude (option -x) |
| 165 |
It is supposed that there are at most 40 ranges |
| 166 |
to exclude in the command line */ |
| 167 |
short excludeseq ; /* number of ranges to exclude (option -x) */ |
| 168 |
Boolean inctransf[40][2] ; /* if the ranges of pages to exclude (option -x) |
| 169 |
have to be transformed */ |
| 170 |
integer include[40][2] ; /* the ranges of pages to include (option -i) |
| 171 |
It is supposed that there are at most 40 ranges |
| 172 |
to include in the command line */ |
| 173 |
short includeseq ; /* number of ranges to include (option -i) */ |
| 174 |
integer *pageloc ; |
| 175 |
integer *pagenumbers ; |
| 176 |
int prettycolumn ; /* the column we are at when running pretty */ |
| 177 |
|
| 178 |
#ifdef ASCIIPTEX |
| 179 |
int ptexdvi ; /* true if dvi file is extended (TATEKUMI) */ |
| 180 |
#endif |
| 181 |
|
| 182 |
/* |
| 183 |
* This array holds values that indicate the length of a command, if |
| 184 |
* we aren't concerned with that command (which is most of them) or |
| 185 |
* zero, if it is a special case. This makes running through the |
| 186 |
* dvi file a lot easier (and probably faster) than any form of |
| 187 |
* dispatch table, especially since we really don't care what the |
| 188 |
* pages are made of. |
| 189 |
*/ |
| 190 |
short comlen[256] = { |
| 191 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0-15 */ |
| 192 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 16-31 */ |
| 193 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 32-47 */ |
| 194 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 48-63 */ |
| 195 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 64-79 */ |
| 196 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80-95 */ |
| 197 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 96-111 */ |
| 198 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 112-127 */ |
| 199 |
2, 3, 4, 5, 9, 2, 3, 4, 5, 9, 1, 0, 0, 1, 1, 2, /* 128-143 */ |
| 200 |
3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 2, 3, 4, /* 144-159 */ |
| 201 |
5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, /* 160-175 */ |
| 202 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 176-191 */ |
| 203 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 192-207 */ |
| 204 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 208-223 */ |
| 205 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, /* 224-239 */ |
| 206 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };/* 240-255 */ |
| 207 |
|
| 208 |
/* |
| 209 |
* Input bytes from the dvi file. |
| 210 |
* These routines could probably be sped up significantly; but they are |
| 211 |
* very machine dependent, so I will leave such tuning to the installer. |
| 212 |
* They simply get and return bytes in batches of one, two, three, and four, |
| 213 |
* updating the current position as necessary. |
| 214 |
*/ |
| 215 |
|
| 216 |
static void |
| 217 |
abortpage(void) |
| 218 |
{ |
| 219 |
error("! unexpected eof on DVI file") ; |
| 220 |
} |
| 221 |
|
| 222 |
static shalfword dvibyte(void) |
| 223 |
{ |
| 224 |
register shalfword i ; |
| 225 |
if ((i=getc(infile))==EOF) |
| 226 |
abortpage() ; |
| 227 |
return(i) ; |
| 228 |
} |
| 229 |
|
| 230 |
static halfword twobytes(void) |
| 231 |
{ |
| 232 |
register halfword i ; |
| 233 |
i = dvibyte() ; |
| 234 |
return(i*256+dvibyte()) ; } |
| 235 |
|
| 236 |
#if 0 /* not used */ |
| 237 |
static integer threebytes(void) |
| 238 |
{ |
| 239 |
register integer i ; |
| 240 |
i = twobytes() ; |
| 241 |
return(i*256+dvibyte()) ; } |
| 242 |
#endif |
| 243 |
|
| 244 |
static shalfword |
| 245 |
signedbyte(void) |
| 246 |
{ |
| 247 |
register shalfword i ; |
| 248 |
if ((i=getc(infile))==EOF) |
| 249 |
abortpage() ; |
| 250 |
if (i<128) return(i) ; |
| 251 |
else return(i-256) ; |
| 252 |
} |
| 253 |
|
| 254 |
static shalfword |
| 255 |
signedpair(void) |
| 256 |
{ |
| 257 |
register shalfword i ; |
| 258 |
i = signedbyte() ; |
| 259 |
return(i*256+dvibyte()) ; |
| 260 |
} |
| 261 |
|
| 262 |
#if 0 /* not used */ |
| 263 |
static integer |
| 264 |
signedtrio(void) |
| 265 |
{ |
| 266 |
register integer i ; |
| 267 |
i = signedpair() ; |
| 268 |
return(i*256+dvibyte()) ; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
static integer |
| 273 |
signedquad(void) |
| 274 |
{ |
| 275 |
register integer i ; |
| 276 |
i = signedpair() ; |
| 277 |
return(i*65536+twobytes()) ; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Routines for the transformation of the pages specified with |
| 282 |
* the @ parameter into the actual number of page from the file |
| 283 |
* |
| 284 |
* trans(p,q) looks for the q'th occurrence of the page numbered p |
| 285 |
* transformpages makes the transformation for the options -f -i -l -x |
| 286 |
*/ |
| 287 |
|
| 288 |
static integer transf(integer p, halfword q) |
| 289 |
{ |
| 290 |
int i=0, j=0; |
| 291 |
|
| 292 |
while( j != q && i < pagecount ) |
| 293 |
if ( pagenumbers[i++] == p ) j++; |
| 294 |
if ( j != q ) error ("! Page specified not found"); |
| 295 |
return (i++); |
| 296 |
} |
| 297 |
|
| 298 |
static void transformpages(void) |
| 299 |
{ |
| 300 |
int i; |
| 301 |
|
| 302 |
if (firsttransf) firstpage=transf(firstpage,firsttransf); |
| 303 |
if (lasttransf) lastpage=transf(lastpage,lasttransf); |
| 304 |
for ( i= 0; i< 40; i++) { |
| 305 |
if (exctransf[i][0]) exclude[i][0]=transf(exclude[i][0],exctransf[i][0]); |
| 306 |
if (exctransf[i][1]) exclude[i][1]=transf(exclude[i][1],exctransf[i][1]); |
| 307 |
if (inctransf[i][0]) include[i][0]=transf(include[i][0],inctransf[i][0]); |
| 308 |
if (inctransf[i][1]) include[i][1]=transf(include[i][1],inctransf[i][1]); |
| 309 |
} |
| 310 |
} |
| 311 |
/* |
| 312 |
* |
| 313 |
*/ |
| 314 |
static integer fontdeflen(integer p) |
| 315 |
{ |
| 316 |
fseek(infile,p+14,SEEK_SET); |
| 317 |
return (16L+dvibyte()+dvibyte()); |
| 318 |
} |
| 319 |
|
| 320 |
/* |
| 321 |
* Simulation of dvibuf, get the character located at the position |
| 322 |
* p in the input dvi file |
| 323 |
*/ |
| 324 |
static unsigned char dvibuf(integer p) |
| 325 |
{ |
| 326 |
fseek(infile,p,SEEK_SET); |
| 327 |
return(dvibyte()); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
* Read a string of length n from the file into the string temp |
| 332 |
*/ |
| 333 |
static void stringdvibuf(integer p, integer n) |
| 334 |
{ |
| 335 |
fseek(infile,p,SEEK_SET); |
| 336 |
while ( n-- > 0 ) |
| 337 |
*temp++ = dvibyte(); |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
* Print a usage error messsage, and quit. |
| 342 |
*/ |
| 343 |
static void usage(void) { |
| 344 |
(void)fprintf(stderr,banner); |
| 345 |
(void)fprintf(stderr,"Usage: dvidvi [options] input[.dvi] [output]\n"); |
| 346 |
(void)fprintf(stderr,"where options are:\n"); |
| 347 |
(void)fprintf(stderr," [-f n] first page printed [-l n] last page printed\n"); |
| 348 |
(void)fprintf(stderr," [-n n] print at most n pages \n"); |
| 349 |
(void)fprintf(stderr," [-i {n1..n2 | n3}[,...]] include pages\n"); |
| 350 |
(void)fprintf(stderr," [-x {n1..n2 | n3}[,...]] exclude pages\n"); |
| 351 |
(void)fprintf(stderr," [-q] quiet mode [-r] reverse pages\n"); |
| 352 |
(void)fprintf(stderr," [-m modulo:pagespecs] [-j] jumping pages\n"); |
| 353 |
(void)fprintf(stderr," [-p pagemodulo]\n"); |
| 354 |
(void)fprintf(stderr,"\nEmail bug reports to %s.\n", BUG_ADDRESS); |
| 355 |
#ifndef VMS |
| 356 |
exit(1) ; |
| 357 |
#else /* VMS */ |
| 358 |
exit(0x10000002) ; |
| 359 |
/* AKT: was exit(1) */ |
| 360 |
#endif /* VMS */ |
| 361 |
} |
| 362 |
/* |
| 363 |
* Print an error message, and exit if it is fatal. |
| 364 |
*/ |
| 365 |
void error(const char *s) |
| 366 |
{ |
| 367 |
(void)fprintf(stderr, "%s\n", s) ; /* AKT: was dvidvi: %s */ |
| 368 |
if (*s == '!') |
| 369 |
#ifndef VMS |
| 370 |
exit(1) ; |
| 371 |
#else /* VMS */ |
| 372 |
exit(0x10000002) ; |
| 373 |
/* AKT: was exit(1) */ |
| 374 |
#endif /* VMS */ |
| 375 |
} |
| 376 |
/* |
| 377 |
* This function calculates approximately (whole + num/den) * sf. |
| 378 |
* No need for real extreme accuracy; one ten thousandth of an |
| 379 |
* inch should be sufficient. |
| 380 |
* |
| 381 |
* No `sf' parameter means to use an old one; inches are assumed |
| 382 |
* originally. |
| 383 |
* |
| 384 |
* Assumptions: |
| 385 |
* |
| 386 |
* 0 <= num < den <= 10000 |
| 387 |
* 0 <= whole |
| 388 |
*/ |
| 389 |
integer defaultscale = 4736286 ; |
| 390 |
static integer scale(integer whole, integer num, integer den, integer sf) |
| 391 |
{ |
| 392 |
integer v ; |
| 393 |
|
| 394 |
if (sf) |
| 395 |
defaultscale = sf ; |
| 396 |
else |
| 397 |
sf = defaultscale ; |
| 398 |
v = whole * sf + num * (sf / den) ; |
| 399 |
if (v / sf != whole || v < 0 || v > 0x40000000L) |
| 400 |
error("! arithmetic overflow in parameter") ; |
| 401 |
sf = sf % den ; |
| 402 |
v += (sf * num * 2 + den) / (2 * den) ; |
| 403 |
return(v) ; |
| 404 |
} |
| 405 |
/* |
| 406 |
* Multiplies *p by 1000 and divides it by mag. Avoiding overflow. |
| 407 |
* |
| 408 |
* 1 <= mag <= 1000000 ; |
| 409 |
* 0 <= *p <= 2^30 |
| 410 |
* |
| 411 |
* (Could blow up if a parameter * mag / 1000 > 2^30 sp.) |
| 412 |
*/ |
| 413 |
static void scalemag(long *p) |
| 414 |
{ |
| 415 |
int negative ; |
| 416 |
|
| 417 |
negative = 0 ; |
| 418 |
if (*p < 0) { |
| 419 |
negative = 1 ; |
| 420 |
*p = - *p ; |
| 421 |
} |
| 422 |
*p = 1000 * (*p / mag) + (2000 * (*p % mag) + mag) / (2 * mag) ; |
| 423 |
if (negative) |
| 424 |
*p = - *p ; |
| 425 |
} |
| 426 |
/* |
| 427 |
* Convert a sequence of digits into an integer; return -1 if no digits. |
| 428 |
* Advance the passed pointer as well. |
| 429 |
*/ |
| 430 |
static integer myatol(char **s) |
| 431 |
{ |
| 432 |
register char *p ; |
| 433 |
register integer result ; |
| 434 |
|
| 435 |
result = 0 ; |
| 436 |
p = *s ; |
| 437 |
while ('0' <= *p && *p <= '9') { |
| 438 |
if (result > 100000000) |
| 439 |
error("! arithmetic overflow in parameter") ; |
| 440 |
result = 10 * result + *p++ - '0' ; |
| 441 |
} |
| 442 |
if (p == *s) |
| 443 |
usage() ; |
| 444 |
*s = p ; |
| 445 |
return(result) ; |
| 446 |
} |
| 447 |
/* |
| 448 |
* Get a dimension, allowing all the various extensions, and |
| 449 |
* defaults. |
| 450 |
*/ |
| 451 |
static integer myatodim(char **s) |
| 452 |
{ |
| 453 |
register integer w, num, den ; |
| 454 |
register char *p ; |
| 455 |
int negative = 0 ; |
| 456 |
|
| 457 |
p = *s ; |
| 458 |
if (**s == '-') { |
| 459 |
(*s)++ ; |
| 460 |
negative = 1 ; |
| 461 |
} |
| 462 |
w = myatol(s) ; |
| 463 |
if (w < 0) |
| 464 |
usage() ; |
| 465 |
p = *s ; |
| 466 |
num = 0 ; |
| 467 |
den = 1 ; |
| 468 |
if (*p == '.') { |
| 469 |
p++ ; |
| 470 |
while ('0' <= *p && *p <= '9') { |
| 471 |
if (den < 1000) { |
| 472 |
den *= 10 ; |
| 473 |
num = num * 10 + *p - '0' ; |
| 474 |
} |
| 475 |
p++ ; |
| 476 |
} |
| 477 |
} |
| 478 |
/* |
| 479 |
* Allowed units are `in', `cm', `mm', `pt', `sp', `cc', `dd', and `pc'; |
| 480 |
* must be in lower case. |
| 481 |
*/ |
| 482 |
if (*p == 'c' && p[1] == 'm') { |
| 483 |
/* centimeters need to be multiplied by 72.27 * 2^16 / 2.54, or 1 864 680 */ |
| 484 |
w = scale(w, num, den, 1864680L) ; |
| 485 |
} else if (*p == 'p' && p[1] == 't') { |
| 486 |
/* real points need to be multiplied by 2^16 */ |
| 487 |
w = scale(w, num, den, 65536L) ; |
| 488 |
} else if (*p == 'p' && p[1] == 'c') { |
| 489 |
/* picas need to be multiplied by 65536 * 12, or 786 432 */ |
| 490 |
w = scale(w, num, den, 786432L) ; |
| 491 |
} else if (*p == 'm' && p[1] == 'm') { |
| 492 |
/* millimeters need to be multiplied by 72.27 * 2^16 / 25.4, or 186 468 */ |
| 493 |
w = scale(w, num, den, 186468L) ; |
| 494 |
} else if (*p == 's' && p[1] == 'p') { |
| 495 |
/* scaled points are already taken care of; simply round */ |
| 496 |
w = scale(w, num, den, 1L) ; |
| 497 |
} else if (*p == 'b' && p[1] == 'p') { |
| 498 |
/* big points need to be multiplied by 72.27 * 65536 / 72, or 65782 */ |
| 499 |
w = scale(w, num, den, 65782L) ; |
| 500 |
} else if (*p == 'd' && p[1] == 'd') { |
| 501 |
/* didot points need to be multiplied by 65536 * 1238 / 1157, or 70124 */ |
| 502 |
w = scale(w, num, den, 70124L) ; |
| 503 |
} else if (*p == 'c' && p[1] == 'c') { |
| 504 |
/* cicero need to be multiplied by 65536 * 1238 / 1157 * 12, or 841 489 */ |
| 505 |
w = scale(w, num, den, 841489L) ; |
| 506 |
} else if (*p == 'i' && p[1] == 'n') { |
| 507 |
/* inches need to be multiplied by 72.27 * 65536, or 4 736 286 */ |
| 508 |
w = scale(w, num, den, 4736286L) ; |
| 509 |
} else { |
| 510 |
/* use default values */ |
| 511 |
w = scale(w, num, den, 0L) ; |
| 512 |
p -= 2 ; |
| 513 |
} |
| 514 |
p += 2 ; |
| 515 |
*s = p ; |
| 516 |
return(negative?-w:w) ; |
| 517 |
} |
| 518 |
/* |
| 519 |
* This function determine if the page has to be printed |
| 520 |
* depending on the values of options -f, -l, -i and -x |
| 521 |
*/ |
| 522 |
|
| 523 |
static short selectedpage (integer n) |
| 524 |
{ |
| 525 |
short i; |
| 526 |
|
| 527 |
if ( firstpage > n || n > lastpage ) return(0); |
| 528 |
for ( i=0 ; i < excludeseq ; i++ ) |
| 529 |
if ( exclude[i][0] <= n && n <= exclude[i][1] ) |
| 530 |
return(0); |
| 531 |
if (includeseq == 0) return (1); |
| 532 |
for ( i=0 ; i < includeseq ; i++ ) |
| 533 |
if ( include[i][0] <= n && n <= include[i][1] ) |
| 534 |
return(1); |
| 535 |
return(0); |
| 536 |
} |
| 537 |
|
| 538 |
/* |
| 539 |
* Initialize sets up all the globals and data structures. |
| 540 |
*/ |
| 541 |
static void |
| 542 |
initialize(void) |
| 543 |
{ |
| 544 |
int i; |
| 545 |
/* initialize values in case of option -m is not specified */ |
| 546 |
modulo = 1 ; |
| 547 |
pagemodulo = 0; |
| 548 |
pages[0].hoffset = 0 ; |
| 549 |
pages[0].voffset = 0 ; |
| 550 |
pages[0].pageno = 0 ; |
| 551 |
pages[0].reversed = 0 ; |
| 552 |
pagesperpage = 1 ; |
| 553 |
for ( i= 0; i< 40; i++) { |
| 554 |
exctransf[i][0] = exctransf[i][1] = 0; |
| 555 |
inctransf[i][0] = inctransf[i][1] = 0; |
| 556 |
} |
| 557 |
firsttransf = lasttransf = 0; |
| 558 |
|
| 559 |
excludeseq = 0; |
| 560 |
includeseq = 0; |
| 561 |
firstpage = 1; |
| 562 |
lastpage = 1000000; |
| 563 |
maxpages = 1000000; |
| 564 |
quiet = 0; |
| 565 |
strings =(char *) malloc(STRINGSIZE) ; |
| 566 |
if (strings == 0) |
| 567 |
error("! no memory for strings") ; |
| 568 |
maxpages = 100000 ; |
| 569 |
nextstring = strings ; |
| 570 |
iname = strings ; |
| 571 |
*nextstring++ = 0 ; |
| 572 |
maxstring = strings + STRINGSIZE - 200; |
| 573 |
} |
| 574 |
|
| 575 |
/* |
| 576 |
* Parse the arguments to the routine, and stuff everything away |
| 577 |
* into those globals above. |
| 578 |
*/ |
| 579 |
static void processargs(int argc, char *argv[]) |
| 580 |
{ |
| 581 |
char *q ; |
| 582 |
int i, pageno, lastext = -1 ; |
| 583 |
long hoffset, voffset ; |
| 584 |
int reversed ; |
| 585 |
|
| 586 |
initialize (); |
| 587 |
if (argc < 2 || argc > 8) |
| 588 |
usage() ; |
| 589 |
|
| 590 |
/* |
| 591 |
* This next whole big section of code is straightforward; we just scan |
| 592 |
* the options. An argument can either immediately follow its option letter |
| 593 |
* or be separated by spaces. Any argument not preceded by '-' and an |
| 594 |
* option letter is considered a file name. |
| 595 |
*/ |
| 596 |
for (i=1; i<argc; i++) { |
| 597 |
if (*argv[i]=='-') { |
| 598 |
char *p=argv[i]+2 ; |
| 599 |
char c=argv[i][1] ; |
| 600 |
switch (c) { |
| 601 |
case 'f' : |
| 602 |
if (*p == 0 && argv[i+1]) |
| 603 |
p = argv[++i] ; |
| 604 |
if (*p == '(' && *(p+1) == '@' ) { |
| 605 |
p+=2; firsttransf = myatol(&p); p++; |
| 606 |
} |
| 607 |
else if (*p == '@') { |
| 608 |
p++; firsttransf = 1 ; |
| 609 |
} |
| 610 |
if(sscanf(p, "%ld", &firstpage)==0) |
| 611 |
error("! Bad first page option (-f).") ; |
| 612 |
break ; |
| 613 |
case 'i' : |
| 614 |
if (*p == 0 && argv[i+1]) |
| 615 |
p = argv[++i] ; |
| 616 |
while (*p != 0) { |
| 617 |
if (*p == '(' && *(p+1) == '@' ) { |
| 618 |
p+=2; inctransf[includeseq][0] = myatol(&p); p++; |
| 619 |
} |
| 620 |
else if (*p == '@') { |
| 621 |
p++; inctransf[includeseq][0] = 1 ; |
| 622 |
} |
| 623 |
include[includeseq][0]=myatol(&p); |
| 624 |
if (*p == '.' && *(p+1) == '.' ) { |
| 625 |
p += 2; |
| 626 |
if (*p == '(' && *(p+1) == '@' ) { |
| 627 |
p+=2; inctransf[includeseq][1] = myatol(&p); p++; |
| 628 |
} |
| 629 |
else if (*p == '@') { |
| 630 |
p++; inctransf[includeseq][1] = 1 ; |
| 631 |
} |
| 632 |
include[includeseq][1] = myatol(&p); |
| 633 |
} |
| 634 |
else { |
| 635 |
include[includeseq][1] = include[includeseq][0]; |
| 636 |
inctransf[includeseq][0] = inctransf[includeseq][1] ; |
| 637 |
} |
| 638 |
includeseq++ ; |
| 639 |
if (*p != ',' && *p !=0 ) |
| 640 |
error("! Bad page range option (-i).") ; |
| 641 |
if (*p == ',' ) |
| 642 |
p++ ; |
| 643 |
} /* while */ |
| 644 |
break ; |
| 645 |
case 'l': |
| 646 |
if (*p == 0 && argv[i+1]) |
| 647 |
p = argv[++i] ; |
| 648 |
if (*p == '(' && *(p+1) == '@' ) { |
| 649 |
p+=2; lasttransf = myatol(&p); p++; |
| 650 |
} |
| 651 |
else if (*p == '@') { |
| 652 |
p++; lasttransf = 1 ; |
| 653 |
} |
| 654 |
if(sscanf(p, "%ld", &lastpage)==0) |
| 655 |
error("! Bad last page option (-l).") ; |
| 656 |
break ; |
| 657 |
case 'p' : |
| 658 |
if (*p == 0 && argv[i+1]) |
| 659 |
p = argv[++i] ; |
| 660 |
if (sscanf(p, "%ld", &pagemodulo)==0) |
| 661 |
error("! Bad pagemodulo option (-p).") ; |
| 662 |
break ; |
| 663 |
case 'm' : |
| 664 |
if (*p == 0 && argv[i+1]) |
| 665 |
p = argv[++i] ; |
| 666 |
/* |
| 667 |
* Is there a modulo supplied? Grab it if so; otherwise default to 1. |
| 668 |
*/ |
| 669 |
for (q=p; *q != 0; q++) |
| 670 |
if (*q == ':') |
| 671 |
break ; |
| 672 |
if (*q == ':') { |
| 673 |
modulo = myatol(&p) ; |
| 674 |
if (*p != ':') |
| 675 |
usage() ; |
| 676 |
if (modulo < 1 || modulo > MAXPPERP) |
| 677 |
error("! modulo must lie between 1 and 32") ; |
| 678 |
p++ ; |
| 679 |
} |
| 680 |
/* |
| 681 |
* This loop grabs all of the page specifications. |
| 682 |
*/ |
| 683 |
pagesperpage = 0 ; |
| 684 |
while (*p != 0) { |
| 685 |
if (pagesperpage >= MAXPPERP) |
| 686 |
error("! too many page specifications") ; |
| 687 |
if (*p == '-') { |
| 688 |
reversed = 1 ; |
| 689 |
p++ ; |
| 690 |
} else |
| 691 |
reversed = 0 ; |
| 692 |
if (*p == 0 || *p == '(' || *p == ',') |
| 693 |
pageno = 0 ; |
| 694 |
else |
| 695 |
pageno = myatol(&p) ; |
| 696 |
if (*p == '(') { |
| 697 |
p++ ; |
| 698 |
hoffset = myatodim(&p) ; |
| 699 |
if (*p++ != ',') |
| 700 |
usage() ; |
| 701 |
voffset = myatodim(&p) ; |
| 702 |
if (*p++ != ')') |
| 703 |
usage() ; |
| 704 |
} else { |
| 705 |
hoffset = 0 ; |
| 706 |
voffset = 0 ; |
| 707 |
} |
| 708 |
pages[pagesperpage].hoffset = hoffset ; |
| 709 |
pages[pagesperpage].voffset = voffset ; |
| 710 |
pages[pagesperpage].pageno = pageno ; |
| 711 |
pages[pagesperpage].reversed = reversed ; |
| 712 |
pagesperpage++ ; |
| 713 |
if (*p == ',') |
| 714 |
p++ ; |
| 715 |
} /* while */ |
| 716 |
break ; |
| 717 |
case 'n' : |
| 718 |
if (*p == 0 && argv[i+1]) |
| 719 |
p = argv[++i] ; |
| 720 |
if (sscanf(p, "%ld", &maxpages)==0) |
| 721 |
error("! Bad number of pages option (-n).") ; |
| 722 |
break ; |
| 723 |
case 'q' : case 'Q' : |
| 724 |
quiet = 0 ; |
| 725 |
break ; |
| 726 |
case 'r' : case 'R' : |
| 727 |
pages[0].reversed = 1; |
| 728 |
break ; |
| 729 |
case 'j' : case 'J' : |
| 730 |
jumpingpages = 1; |
| 731 |
break; |
| 732 |
case 'x' : |
| 733 |
if (*p == 0 && argv[i+1]) |
| 734 |
p = argv[++i] ; |
| 735 |
while (*p != 0) { |
| 736 |
if (*p == '(' && *(p+1) == '@' ) { |
| 737 |
p+=2; exctransf[excludeseq][0] = myatol(&p); p++; |
| 738 |
} |
| 739 |
else if (*p == '@') { |
| 740 |
p++; exctransf[excludeseq][0] = 1 ; |
| 741 |
} |
| 742 |
exclude[excludeseq][0]=myatol(&p); |
| 743 |
if (*p == '.' && *(p+1) == '.' ) { |
| 744 |
p += 2; |
| 745 |
if (*p == '(' && *(p+1) == '@' ) { |
| 746 |
p+=2; exctransf[excludeseq][1] = myatol(&p); p++; |
| 747 |
} |
| 748 |
else if (*p == '@') { |
| 749 |
p++; exctransf[excludeseq][1] = 1 ; |
| 750 |
} |
| 751 |
exclude[excludeseq][1] = myatol(&p); |
| 752 |
} |
| 753 |
else { |
| 754 |
exclude[excludeseq][1] = exclude[excludeseq][0]; |
| 755 |
exctransf[excludeseq][0] = exctransf[excludeseq][1] ; |
| 756 |
} |
| 757 |
excludeseq++ ; |
| 758 |
if (*p != ',' && *p !=0 ) |
| 759 |
error("! Bad page range option (-x).") ; |
| 760 |
if (*p == ',' ) |
| 761 |
p++ ; |
| 762 |
} /* while */ |
| 763 |
break ; |
| 764 |
case '?' : |
| 765 |
usage() ; |
| 766 |
break ; |
| 767 |
default: |
| 768 |
fprintf(stderr, "dvidvi: Bad option %c\n", c); |
| 769 |
error("! Try -? for more information") ; |
| 770 |
} /* switch c */ |
| 771 |
} else { /* this a file name */ |
| 772 |
if (*iname == 0) { /* input file name */ |
| 773 |
register char *p ; |
| 774 |
|
| 775 |
lastext = 0 ; |
| 776 |
iname = nextstring ; |
| 777 |
p = argv[i] ; |
| 778 |
while (*p) { |
| 779 |
*nextstring = *p++ ; |
| 780 |
if (*nextstring == '.') |
| 781 |
lastext = nextstring - iname ; |
| 782 |
else if (IS_DIR_SEP(*nextstring) || *nextstring == ':') |
| 783 |
lastext = 0 ; |
| 784 |
nextstring++ ; |
| 785 |
} |
| 786 |
if (lastext == 0) { |
| 787 |
lastext = nextstring - iname ; |
| 788 |
*nextstring++ = '.' ; |
| 789 |
*nextstring++ = 'd' ; |
| 790 |
*nextstring++ = 'v' ; |
| 791 |
*nextstring++ = 'i' ; |
| 792 |
} |
| 793 |
*nextstring++ = 0 ; |
| 794 |
if ((infile=fopen(iname, READBIN))==NULL) |
| 795 |
error("! can't open input file") ; |
| 796 |
|
| 797 |
} else { /* output file name */ |
| 798 |
register char *p ; |
| 799 |
|
| 800 |
lastext = 0 ; |
| 801 |
oname = nextstring ; |
| 802 |
p = argv[i] ; |
| 803 |
while (*p) { |
| 804 |
*nextstring = *p++ ; |
| 805 |
if (*nextstring == '.') |
| 806 |
lastext = nextstring - oname ; |
| 807 |
else if (IS_DIR_SEP(*nextstring) || *nextstring == ':') |
| 808 |
lastext = 0 ; |
| 809 |
nextstring++ ; |
| 810 |
} |
| 811 |
if (lastext == 0) { |
| 812 |
lastext = nextstring - oname ; |
| 813 |
*nextstring++ = '.' ; |
| 814 |
*nextstring++ = 'd' ; |
| 815 |
*nextstring++ = 'v' ; |
| 816 |
*nextstring++ = 'i' ; |
| 817 |
} |
| 818 |
*nextstring++ = 0 ; |
| 819 |
if (freopen(oname, WRITEBIN, stdout)==NULL) |
| 820 |
error("! can't open output file") ; |
| 821 |
} /* end output file name */ |
| 822 |
} /* else argument with '-' */ |
| 823 |
} /* for */ |
| 824 |
if (*iname == 0) { |
| 825 |
(void)fprintf(stderr, banner) ; |
| 826 |
error("! no input file specified"); |
| 827 |
} |
| 828 |
/* Inserted by [email protected] 3.8.1994 */ |
| 829 |
if(!oname) { |
| 830 |
oname="stdout"; |
| 831 |
if (!isatty(fileno(stdout))) |
| 832 |
SET_BINARY(fileno(stdout)); |
| 833 |
} |
| 834 |
|
| 835 |
if (*oname != 0 && !quiet) { |
| 836 |
(void)fprintf(stderr, banner) ; |
| 837 |
(void)fprintf(stderr, "%s -> %s\n",iname,oname); |
| 838 |
temp = nextstring ; |
| 839 |
} |
| 840 |
} |
| 841 |
/* |
| 842 |
* Grabs a pointer, and checks it for validity. |
| 843 |
*/ |
| 844 |
static integer ptr(integer where) |
| 845 |
{ |
| 846 |
fseek(infile,where,SEEK_SET); |
| 847 |
where = signedquad() ; |
| 848 |
if (where < -1L || where > inlength) |
| 849 |
error("! dvi file malformed; impossible pointer") ; |
| 850 |
return(where) ; |
| 851 |
} |
| 852 |
|
| 853 |
/* |
| 854 |
* This routine store the page locations by tracing the pointers backwards. |
| 855 |
*/ |
| 856 |
static void searchpageloc(void) |
| 857 |
{ |
| 858 |
integer p,num ; |
| 859 |
|
| 860 |
num=pagecount-1; |
| 861 |
for (p = ptr(postloc+1); num > 0; num--) { |
| 862 |
if (dvibuf(p) != 139) |
| 863 |
error("! missed a bop somehow") ; |
| 864 |
else { |
| 865 |
pageloc[num]=p; |
| 866 |
pagenumbers[num]=signedquad(); |
| 867 |
} |
| 868 |
p = ptr(p+41) ; |
| 869 |
} |
| 870 |
pageloc[num]=p; |
| 871 |
(void)dvibuf(p); |
| 872 |
pagenumbers[num]=signedquad(); |
| 873 |
} |
| 874 |
/* |
| 875 |
* This routine simply reads the entire dvi file, and then initializes |
| 876 |
* some values about it. |
| 877 |
*/ |
| 878 |
static void readdvifile(void) { |
| 879 |
integer p ; |
| 880 |
unsigned char c,d,e ; |
| 881 |
|
| 882 |
fseek(infile, 0L, 2) ; |
| 883 |
inlength = ftell(infile) ; |
| 884 |
if (inlength < 10) |
| 885 |
error("! dvi file too short") ; |
| 886 |
fseek(infile,-3L,SEEK_CUR); |
| 887 |
for (p=inlength - 3; p > 0; p--) { |
| 888 |
c = dvibyte (); d = dvibyte (); e = dvibyte (); |
| 889 |
if (c == 2 && d == 0xdf /* dave fuchs */ && |
| 890 |
e == 0xdf) |
| 891 |
break ; |
| 892 |
#ifdef ASCIIPTEX |
| 893 |
if (c == 3 && d == 0xdf /* dave fuchs */ && |
| 894 |
e == 0xdf) |
| 895 |
break ; |
| 896 |
#endif |
| 897 |
fseek(infile,-4L,SEEK_CUR); |
| 898 |
} |
| 899 |
if (p < 10) |
| 900 |
error("! rather short dvi file, ain't it?") ; |
| 901 |
postloc = ptr(p-4) ; |
| 902 |
fseek(infile,postloc+5L,SEEK_SET); |
| 903 |
if (signedquad() != 25400000 || |
| 904 |
signedquad() != 473628672) |
| 905 |
error("! change this program to support non-TeX num/den values") ; |
| 906 |
mag = signedquad() ; |
| 907 |
if (mag < 1 || mag > 1000000) |
| 908 |
error("! impossible magnification value") ; |
| 909 |
fseek(infile,postloc+27L,SEEK_SET); |
| 910 |
pagecount = twobytes() ; |
| 911 |
if (pagecount < 1 || pagecount > 1000000) |
| 912 |
error("! impossible page count value") ; |
| 913 |
/* |
| 914 |
* That's enough error checking; we probably have a correct dvi file. |
| 915 |
* Let's convert all the values we got from the command line into |
| 916 |
* units that we can actually use in the dvi file. |
| 917 |
*/ |
| 918 |
for (p=0; p<pagesperpage; p++) { |
| 919 |
scalemag(&(pages[p].hoffset)) ; |
| 920 |
scalemag(&(pages[p].voffset)) ; |
| 921 |
} |
| 922 |
/* |
| 923 |
* Now let's grab us some font pointers. |
| 924 |
*/ |
| 925 |
pageloc = (integer *) calloc (pagecount, sizeof (integer)); |
| 926 |
if (pageloc == NULL) |
| 927 |
error("! Not enough memory for allocation of page pointers"); |
| 928 |
pagenumbers = (integer *) calloc (pagecount, sizeof (integer)); |
| 929 |
if (pagenumbers == NULL) |
| 930 |
error("! Not enough memory for allocation of page numbers array"); |
| 931 |
searchpageloc(); |
| 932 |
transformpages(); |
| 933 |
free(pagenumbers); |
| 934 |
p = postloc + 29 ; |
| 935 |
while (1) { |
| 936 |
c=dvibuf(p); |
| 937 |
if (c == 249) |
| 938 |
break ; |
| 939 |
if (c == 138) |
| 940 |
p++ ; |
| 941 |
else if (c == 243) { |
| 942 |
TeXfonts[dvibyte()] = p ; |
| 943 |
p += fontdeflen(p) ; |
| 944 |
} else |
| 945 |
error("! only nop's and font def's allowed in postamble") ; |
| 946 |
} |
| 947 |
/* |
| 948 |
* Now we check for a landscape special. It should be the |
| 949 |
* *first* thing in the page that is at all complicated. |
| 950 |
*/ |
| 951 |
|
| 952 |
p = pageloc[0L] + 45 ; |
| 953 |
c=dvibuf(p); |
| 954 |
while (comlen[c]) { |
| 955 |
p += comlen[c] ; |
| 956 |
c = dvibuf(p); |
| 957 |
} |
| 958 |
if (c == 239) { |
| 959 |
stringdvibuf(p+2L,9L); |
| 960 |
if (strncmp(temp, "landscape", 9)==0) { |
| 961 |
landscape = p ; |
| 962 |
rem0special = 1 ; |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
/* |
| 967 |
* Output a single byte, keeping track of where we are. |
| 968 |
*/ |
| 969 |
static void outdvibyte(unsigned char c) |
| 970 |
{ |
| 971 |
fputc(c, stdout) ; |
| 972 |
dviloc++ ; |
| 973 |
} |
| 974 |
/* |
| 975 |
* Send out two bytes. |
| 976 |
*/ |
| 977 |
static void outdvi2(integer v) |
| 978 |
{ |
| 979 |
outdvibyte((unsigned char)(v >> 8)) ; |
| 980 |
outdvibyte((unsigned char)(v & 255)) ; |
| 981 |
} |
| 982 |
/* |
| 983 |
* Send out a longword. |
| 984 |
*/ |
| 985 |
static void outdviquad(integer v) |
| 986 |
{ |
| 987 |
outdvi2(v >> 16) ; |
| 988 |
outdvi2(v & 65535) ; |
| 989 |
} |
| 990 |
/* |
| 991 |
* This routine just copies some stuff from the buffer on out. |
| 992 |
* Suppose the file is positioned correctly before |
| 993 |
*/ |
| 994 |
static void putbuf(integer length) |
| 995 |
{ |
| 996 |
while ( length-- > 0 ) |
| 997 |
outdvibyte((unsigned char)dvibyte()) ; |
| 998 |
} |
| 999 |
/* |
| 1000 |
* This routine outputs a string, terminated by null. |
| 1001 |
*/ |
| 1002 |
static void putstr(unsigned const char *s) |
| 1003 |
{ |
| 1004 |
while (*s) |
| 1005 |
outdvibyte(*s++) ; |
| 1006 |
} |
| 1007 |
/* |
| 1008 |
* Here we write the preamble to the dvi file. |
| 1009 |
*/ |
| 1010 |
static void writepreamble(void) { |
| 1011 |
/* just copy the first 14 bytes of the file */ |
| 1012 |
fseek(infile,0L,SEEK_SET); |
| 1013 |
putbuf(14L) ; |
| 1014 |
/* and put our identifier. */ |
| 1015 |
putstr((unsigned const char *)"\015dvidvi output") ; |
| 1016 |
} |
| 1017 |
/* |
| 1018 |
* This routine writes out a font definition. |
| 1019 |
*/ |
| 1020 |
static void putfontdef(int f) |
| 1021 |
{ |
| 1022 |
integer p,q ; |
| 1023 |
|
| 1024 |
p = TeXfonts[f] ; |
| 1025 |
q=fontdeflen(p) ; |
| 1026 |
fseek(infile,p,SEEK_SET); |
| 1027 |
putbuf(q) ; |
| 1028 |
} |
| 1029 |
/* |
| 1030 |
* The postamble is next. |
| 1031 |
*/ |
| 1032 |
static void writepostamble(void) { |
| 1033 |
int i ; |
| 1034 |
integer p ; |
| 1035 |
|
| 1036 |
p = dviloc ; |
| 1037 |
outdvibyte(248) ; |
| 1038 |
outdviquad(prevpp) ; |
| 1039 |
fseek(infile,postloc+5,SEEK_SET); |
| 1040 |
putbuf(20L) ; |
| 1041 |
outdvi2(twobytes()+1L) ; /* increase stack depth by 1 */ |
| 1042 |
outdvi2(outputpages) ; |
| 1043 |
for (i=0; i<256; i++) |
| 1044 |
if (fontseen[i]) |
| 1045 |
putfontdef(i) ; |
| 1046 |
outdvibyte(249) ; |
| 1047 |
outdviquad(p) ; |
| 1048 |
#ifdef ASCIIPTEX |
| 1049 |
if (ptexdvi) |
| 1050 |
outdvibyte(3) ; |
| 1051 |
else |
| 1052 |
#endif |
| 1053 |
outdvibyte(2) ; |
| 1054 |
outdviquad(0xdfdfdfdfL) ; |
| 1055 |
while (dviloc & 3) |
| 1056 |
outdvibyte(0xdf) ; |
| 1057 |
fclose(stdout) ; |
| 1058 |
} |
| 1059 |
/* |
| 1060 |
* This routine starts a page, by writing out a bop command. |
| 1061 |
*/ |
| 1062 |
static void beginpage(void) { |
| 1063 |
int i ; |
| 1064 |
integer p ; |
| 1065 |
|
| 1066 |
p = dviloc ; |
| 1067 |
outdvibyte(139) ; |
| 1068 |
outdviquad(outputpages+1) ; |
| 1069 |
for (i=0; i<9; i++) |
| 1070 |
outdviquad(0L) ; |
| 1071 |
outdviquad(prevpp) ; |
| 1072 |
prevpp = p ; |
| 1073 |
} |
| 1074 |
/* |
| 1075 |
* This routine sends out a page. We need to handle the |
| 1076 |
* landscape special, though. |
| 1077 |
*/ |
| 1078 |
static void dopage(integer num) |
| 1079 |
{ |
| 1080 |
register integer p ; |
| 1081 |
register int len ; |
| 1082 |
integer v, oldp ; |
| 1083 |
unsigned char c; |
| 1084 |
|
| 1085 |
/* |
| 1086 |
* We want to take the base 10 log of the number. It's probably |
| 1087 |
* small, so we do it quick. |
| 1088 |
*/ |
| 1089 |
if (! quiet) { |
| 1090 |
int t = num+1, i = 0 ; |
| 1091 |
if (t < 0) { |
| 1092 |
t = -t ; |
| 1093 |
i++ ; |
| 1094 |
} |
| 1095 |
do { |
| 1096 |
i++ ; |
| 1097 |
t /= 10 ; |
| 1098 |
} while (t > 0) ; |
| 1099 |
if (i + prettycolumn > 76) { |
| 1100 |
fprintf(stderr, "\n") ; |
| 1101 |
prettycolumn = 0 ; |
| 1102 |
} |
| 1103 |
prettycolumn += i + 1 ; |
| 1104 |
(void)fprintf(stderr, "[%ld", num+1) ; |
| 1105 |
(void)fflush(stderr) ; |
| 1106 |
} |
| 1107 |
p = pageloc[num] + 45 ; |
| 1108 |
c=dvibuf(p); |
| 1109 |
while (c != 140) { |
| 1110 |
if ((len=comlen[c]) > 0) { /* most commands are simple */ |
| 1111 |
outdvibyte(c); |
| 1112 |
putbuf((long)len-1) ; |
| 1113 |
p += len ; |
| 1114 |
} else { /* but there are a few we need to treat specially */ |
| 1115 |
len = c ; |
| 1116 |
if (171 <= len && len <= 235) { |
| 1117 |
p++ ; |
| 1118 |
if (len == 235) |
| 1119 |
{ len = dvibyte (); p++ ;} |
| 1120 |
else |
| 1121 |
len -= 171 ; |
| 1122 |
if (!fontseen[len]) { |
| 1123 |
putfontdef(len) ; |
| 1124 |
fontseen[len] = 1 ; |
| 1125 |
fseek(infile,p,SEEK_SET); |
| 1126 |
} |
| 1127 |
if (len < 64) |
| 1128 |
outdvibyte((unsigned char)(171 + len)) ; |
| 1129 |
else { |
| 1130 |
outdvibyte(235) ; |
| 1131 |
outdvibyte((unsigned char)len) ; |
| 1132 |
} |
| 1133 |
} else { |
| 1134 |
v = 0 ; |
| 1135 |
oldp = p++ ; |
| 1136 |
switch(len) { |
| 1137 |
case 242: v = dvibyte() ; p++ ; break ; |
| 1138 |
case 241: v = (v << 8) + dvibyte() ; p++ ; break ; |
| 1139 |
case 240: v = (v << 8) + dvibyte() ; p++ ; break ; |
| 1140 |
case 239: v = (v << 8) + dvibyte() ; p++ ; |
| 1141 |
/* |
| 1142 |
* Remove a landscape special on page 0, if one is found. |
| 1143 |
*/ stringdvibuf(oldp + len - 327,9); |
| 1144 |
if (num || ! rem0special || |
| 1145 |
strncmp(temp, "landscape", 9)) { |
| 1146 |
fseek (infile,oldp,SEEK_SET); |
| 1147 |
putbuf(v + len - 237) ; |
| 1148 |
} |
| 1149 |
p = oldp + v + len - 237 ; |
| 1150 |
fseek(infile,p,SEEK_SET); |
| 1151 |
break ; |
| 1152 |
case 243: case 244: case 245: case 246: |
| 1153 |
p += len - 230 ; |
| 1154 |
p += dvibuf(p) ; |
| 1155 |
p += dvibyte() + 2 ; |
| 1156 |
fseek(infile,p,SEEK_SET); |
| 1157 |
break ; |
| 1158 |
#ifdef ASCIIPTEX |
| 1159 |
case 255: |
| 1160 |
ptexdvi = 1 ; |
| 1161 |
outdvibyte(len); |
| 1162 |
break ; |
| 1163 |
#endif |
| 1164 |
default: fprintf(stderr, "Bad dvi command was %d at %ld\n", len, p) ; |
| 1165 |
error("! lost sync dvi in file lost dvi sync file in") ; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
} |
| 1169 |
c=dvibyte(); |
| 1170 |
} |
| 1171 |
if (! quiet) { |
| 1172 |
(void)fprintf(stderr, "] ") ; |
| 1173 |
(void)fflush(stderr) ; |
| 1174 |
prettycolumn += 2 ; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
/* |
| 1178 |
* Here we end a page. Simple enough. |
| 1179 |
*/ |
| 1180 |
static void endpage(void) { |
| 1181 |
outputpages++ ; |
| 1182 |
outdvibyte(140) ; |
| 1183 |
} |
| 1184 |
/* |
| 1185 |
* This is our main routine for output, which runs through all the |
| 1186 |
* pages we need to output. |
| 1187 |
*/ |
| 1188 |
static void writedvifile(void) { |
| 1189 |
integer pagenum ; |
| 1190 |
int ppp ; |
| 1191 |
integer actualpageno, lastpageno ; |
| 1192 |
struct pagespec *ps ; |
| 1193 |
Boolean beginp ; |
| 1194 |
|
| 1195 |
#ifdef ASCIIPTEX |
| 1196 |
ptexdvi = 0 ; |
| 1197 |
#endif |
| 1198 |
|
| 1199 |
writepreamble() ; |
| 1200 |
if ( !pagemodulo ) |
| 1201 |
pagefake = (pagecount + modulo - 1) / modulo * modulo ; |
| 1202 |
else |
| 1203 |
pagefake = (pagecount + pagemodulo - 1) / pagemodulo * pagemodulo ; |
| 1204 |
if ( maxpages < pagefake ) pagefake = maxpages; |
| 1205 |
for (lastpageno = -1, pagenum = 0; pagenum < pagefake / ( jumpingpages + 1); pagenum += modulo) { |
| 1206 |
beginp = 1 ; |
| 1207 |
for (ppp = 0, ps=pages; ppp < pagesperpage; ppp++, ps++) { |
| 1208 |
if (ps->reversed) |
| 1209 |
actualpageno = pagefake - pagenum - modulo + ps->pageno ; |
| 1210 |
else |
| 1211 |
actualpageno = pagenum + ps->pageno ; |
| 1212 |
if ( jumpingpages ) |
| 1213 |
ps->reversed = !ps->reversed; |
| 1214 |
if ( actualpageno == lastpageno ) { |
| 1215 |
pagenum = pagefake; |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
if (actualpageno < pagecount && selectedpage(actualpageno+1) ) { |
| 1219 |
if (beginp) { |
| 1220 |
beginpage() ; |
| 1221 |
beginp = 0 ; |
| 1222 |
} |
| 1223 |
if (landscape) { |
| 1224 |
fseek(infile,landscape,SEEK_SET); |
| 1225 |
putbuf(dvibuf(landscape+1)+2L) ; |
| 1226 |
landscape = 0 ; |
| 1227 |
} |
| 1228 |
if (pagesperpage) |
| 1229 |
outdvibyte(141) ; |
| 1230 |
if (ps->hoffset) { |
| 1231 |
outdvibyte(146) ; |
| 1232 |
outdviquad(ps->hoffset) ; |
| 1233 |
} |
| 1234 |
if ( pagemodulo ) |
| 1235 |
lastpageno = actualpageno; |
| 1236 |
if (ps->voffset) { |
| 1237 |
outdvibyte(160) ; |
| 1238 |
outdviquad(ps->voffset) ; |
| 1239 |
} |
| 1240 |
dopage(actualpageno) ; |
| 1241 |
if (pagesperpage) |
| 1242 |
outdvibyte(142) ; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
if (beginp != 1) endpage() ; |
| 1246 |
} |
| 1247 |
writepostamble() ; |
| 1248 |
if (! quiet) { |
| 1249 |
(void)fprintf(stderr, "\n") ; |
| 1250 |
(void)fflush(stderr) ; |
| 1251 |
prettycolumn = 0 ; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
int main(int argc, char *argv[]) |
| 1255 |
{ |
| 1256 |
#if defined(WIN32) && defined(KPATHSEA) |
| 1257 |
int ac; |
| 1258 |
char **av, *enc; |
| 1259 |
if (argc>1) { |
| 1260 |
kpse_set_program_name(argv[0], "dvidvi"); |
| 1261 |
enc = kpse_var_value("command_line_encoding"); |
| 1262 |
if (get_command_line_args_utf8(enc, &ac, &av)) { |
| 1263 |
argc = ac; |
| 1264 |
argv = av; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
#endif |
| 1268 |
processargs(argc, argv) ; |
| 1269 |
readdvifile() ; |
| 1270 |
writedvifile() ; |
| 1271 |
return 0 ; |
| 1272 |
} |
| 1273 |
|