home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 3.3J / os33j.iso / NextLibrary / TeX / tex / src / dvips / dvips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-27  |  32.9 KB  |  1,031 lines

  1. /*
  2.  *   This is the main routine.
  3.  */
  4. #ifndef DEFRES
  5. #define DEFRES (300)
  6. #endif
  7.  
  8. #include "dvips.h" /* The copyright notice there is included too! */
  9. #ifndef SYSV
  10. extern char *strtok() ; /* some systems don't have this in strings.h */
  11. #endif
  12. #ifdef VMS
  13. #define GLOBAL globaldef
  14. #include climsgdef
  15. #include ctype
  16. #include descrip
  17. #endif
  18. /*
  19.  *   First we define some globals.
  20.  */
  21. #ifdef VMS
  22.     static char ofnme[252],infnme[252],pap[40],thh[20];
  23. #endif
  24. fontdesctype *fonthead ;      /* list of all fonts mentioned so far */
  25. fontdesctype *curfnt ;        /* the currently selected font */
  26. sectiontype *sections ;       /* sections to process document in */
  27. Boolean manualfeed ;          /* manual feed? */
  28. Boolean compressed ;          /* compressed? */
  29. Boolean safetyenclose ;
  30.                           /* enclose in save/restore for stupid spoolers? */
  31. Boolean removecomments = 1 ;  /* remove comments from included PS? */
  32. Boolean nosmallchars ;        /* disable small char optimization for X4045? */
  33. Boolean cropmarks ;           /* add cropmarks? */
  34. Boolean abspage = 0 ;         /* are page numbers absolute? */
  35. Boolean tryepsf = 0 ;         /* should we try to make it espf? */
  36. Boolean secure = 0 ;          /* make safe for suid */
  37. int collatedcopies = 1 ;      /* how many collated copies? */
  38. int sectioncopies = 1 ;       /* how many times to repeat each section? */
  39. integer pagecopies = 1 ;          /* how many times to repeat each page? */
  40. shalfword linepos = 0 ;       /* where are we on the line being output? */
  41. integer maxpages ;            /* the maximum number of pages */
  42. Boolean notfirst, notlast ;   /* true if a first page was specified */
  43. Boolean evenpages, oddpages ; /* true if doing only even/only odd pages */
  44. Boolean pagelist ;            /* true if using page ranges */
  45. Boolean sendcontrolD ;        /* should we send a control D at end? */
  46. integer firstpage ;           /* the number of the first page if specified */
  47. integer lastpage ;
  48. integer firstseq ;
  49. integer lastseq ;
  50. integer hpapersize, vpapersize ; /* horizontal and vertical paper size */
  51. integer hoff, voff ;          /* horizontal and vertical offsets */
  52. integer maxsecsize ;          /* the maximum size of a section */
  53. integer firstboploc ;         /* where the first bop is */
  54. Boolean sepfiles ;            /* each section in its own file? */
  55. int numcopies ;               /* number of copies of each page to print */
  56. char *oname ;                 /* output file name */
  57. char *iname ;                 /* dvi file name */
  58. char *fulliname ;             /* same, with current working directory */
  59. char *strings ;               /* strings for program */
  60. char *nextstring, *maxstring ; /* string pointers */
  61. FILE *dvifile, *bitfile ;     /* dvi and output files */
  62. quarterword *curpos ;        /* current position in virtual character packet */
  63. quarterword *curlim ;         /* final byte in virtual character packet */
  64. fontmaptype *ffont ;          /* first font in current frame */
  65. real conv ;                   /* conversion ratio, pixels per DVI unit */
  66. real vconv ;                  /* conversion ratio, pixels per DVI unit */
  67. real alpha ;                  /* conversion ratio, DVI unit per TFM unit */
  68. integer mag ;                 /* the magnification of this document */
  69. integer num, den ;            /* the numerator and denominator */
  70. int overridemag ;             /* substitute for mag value in DVI file? */
  71. int actualdpi = DEFRES ;      /* the actual resolution of the printer */
  72. int vactualdpi = DEFRES ;     /* the actual resolution of the printer */
  73. int maxdrift ;                /* max pixels away from true rounded position */
  74. int vmaxdrift ;               /* max pixels away from true rounded position */
  75. char *paperfmt ;              /* command-line paper format */
  76. int landscape = 0 ;           /* landscape mode */
  77. integer fontmem ;             /* memory remaining in printer */
  78. integer pagecount ;           /* page counter for the sections */
  79. integer pagenum ;             /* the page number we currently look at */
  80. long bytesleft ;              /* number of bytes left in raster */
  81. quarterword *raster ;         /* area for raster manipulations */
  82. integer hh, vv ;              /* horizontal and vertical pixel positions */
  83. char *tfmpath = TFMPATH ;     /* pointer to directories for tfm files */
  84. char *pkpath = PKPATH ;       /* pointer to directories for pk files */
  85. char *vfpath = VFPATH ;       /* pointer to directories for vf files */
  86. char *figpath = FIGPATH ;     /* pointer to directories for figure files */
  87. char *headerpath = HEADERPATH ; /* pointer to directories for header files */
  88. char *configpath = CONFIGPATH;  /* where to find config files */
  89. char *infont ;                  /* is the file we are downloading a font? */
  90. #ifndef PICTPATH
  91. #ifndef __THINK__
  92. #define PICTPATH "."
  93. #else
  94. #define PICTPATH ":"
  95. #endif
  96. #endif
  97. char *pictpath = PICTPATH ;   /* where IFF/etc. pictures are found */
  98. #ifdef SEARCH_SUBDIRECTORIES
  99. char *fontsubdirpath = FONTSUBDIRPATH ;
  100. #endif
  101. #ifdef FONTLIB
  102. char *flipath = FLIPATH ;     /* pointer to directories for fli files */
  103. char *fliname = FLINAME ;     /* pointer to names of fli files */
  104. #endif
  105. integer swmem ;               /* font memory in the PostScript printer */
  106. int quiet ;                   /* should we only print errors to stderr? */
  107. int filter ;                  /* act as filter default output to stdout,
  108.                                                default input to stdin? */
  109. int prettycolumn ;            /* the column we are at when running pretty */
  110. int gargc ;                   /* global argument count */
  111. char **gargv ;                /* global argument vector */
  112. int totalpages = 0 ;          /* total number of pages */
  113. Boolean reverse ;             /* are we going reverse? */
  114. Boolean usesPSfonts ;         /* do we use local PostScript fonts? */
  115. Boolean usesspecial ;         /* do we use \special? */
  116. Boolean headers_off ;         /* do we send headers or not? */
  117. Boolean usescolor ;           /* IBM: color - do we use colors? */
  118. char *headerfile ;            /* default header file */
  119. char *warningmsg ;            /* a message to write, if set in config file */
  120. Boolean multiplesects ;       /* more than one section? */
  121. Boolean disablecomments ;     /* should we suppress any EPSF comments? */
  122. char *printer ;               /* what printer to send this to? */
  123. char *mfmode ;                /* default MF mode */
  124. frametype frames[MAXFRAME] ;  /* stack for virtual fonts */
  125. fontdesctype *baseFonts[256] ; /* base fonts for dvi file */
  126. integer pagecost;               /* memory used on the page being prescanned */
  127. int delchar;                    /* characters to delete from prescanned page */
  128. integer fsizetol;               /* max dvi units error for psfile font sizes */
  129. Boolean includesfonts;          /* are fonts used in included psfiles? */
  130. fontdesctype *fonthd[MAXFONTHD];/* list headers for included fonts of 1 name */
  131. int nextfonthd;                 /* next unused fonthd[] index */
  132. char xdig[256];                 /* table for reading hexadecimal digits */
  133. char banner[] = BANNER ;        /* our startup message */
  134. Boolean noenv ;                 /* ignore PRINTER envir variable? */
  135. Boolean dopprescan = 0 ;        /* do we do a scan before the prescan? */
  136. integer lastheadermem ;         /* how much mem did the last header require? */
  137. extern int dontmakefont ;
  138. struct papsiz *papsizes ;       /* all available paper size */
  139. int headersready ;              /* ready to check headers? */
  140. #ifdef MSDOS
  141. char *mfjobname = NULL;         /* name of mfjob file if given */
  142. FILE *mfjobfile = NULL;         /* mfjob file for font making instructions */
  143. #endif
  144. #ifdef DEBUG
  145. integer debug_flag = 0;
  146. #endif /* DEBUG */
  147. char queryline[256];                /* interactive query of options */
  148. int qargc;
  149. char *qargv[32];
  150. char queryoptions;
  151. /*
  152.  *   This routine calls the following externals:
  153.  */
  154. extern void outbangspecials() ;
  155. extern void prescanpages() ;
  156. extern void pprescanpages() ;
  157. extern void initprinter() ;
  158. extern void cleanprinter() ;
  159. extern void dosection() ;
  160. extern void getdefaults() ;
  161. extern void cmdout() ;
  162. extern void numout() ;
  163. extern void initcolor() ;
  164. extern int add_header() ;
  165. extern int ParsePages() ;
  166. extern void checkenv() ;
  167. extern void getpsinfo(), revpslists() ;
  168. #ifdef FONTLIB
  169. extern void fliload() ;
  170. #endif
  171. #ifdef __THINK__
  172. int dcommand(char ***);
  173. #endif
  174.  
  175. /* Declare the routine to get the current working directory.  */
  176.  
  177. #ifndef IGNORE_CWD
  178. #ifndef HAVE_GETCWD
  179. extern char *getwd (); /* said to be faster than getcwd (SunOS man page) */
  180. #define getcwd(b, len)  getwd(b) /* used here only when b nonnull */
  181. #else
  182. #ifdef ANSI
  183. extern char *getcwd (char *, int);
  184. #else
  185. extern char *getcwd ();
  186. #endif /* not ANSI */
  187. #endif /* not HAVE_GETWD */
  188. #if defined(SYSV) || defined(VMS) || defined(MSDOS)
  189. #define MAXPATHLEN (256)
  190. #else
  191. #include <sys/param.h>          /* for MAXPATHLEN */
  192. #endif
  193. #endif /* not IGNORE_CWD */
  194.  
  195. static char *helparr[] = {
  196. #ifndef VMCMS
  197. "    Usage: dvips [options] filename[.dvi]",
  198. #else
  199. "    VM/CMS Usage:",
  200. "           dvips fname [ftype [fmode]] [options]",
  201. "or",
  202. "           dvips fname[.ftype[.fmode]] [options]",
  203. #endif
  204. "a*  Conserve memory, not time      y # Multiply by dvi magnification",
  205. "b # Page copies, for posters e.g.  A   Print only odd (TeX) pages",
  206. "c # Uncollated copies              B   Print only even (TeX) pages",
  207. "d # Debugging                      C # Collated copies",
  208. "e # Maxdrift value                 D # Resolution",
  209. "f*  Run as filter                  E*  Try to create EPSF",
  210. "h f Add header file                F*  Send control-D at end",
  211. "i*  Separate file per section      K*  Pull comments from inclusions",
  212. "k*  Print crop marks               M*  Don't make fonts",
  213. "l # Last page                      N*  No structured comments",
  214. "m*  Manual feed                    O c Set/change paper offset",
  215. "n # Maximum number of pages        P s Load config.$s",
  216. "o f Output file                    R   Run securely",
  217. "p # First page                     S # Max section size in pages",
  218. "q*  Run quietly                    T c Specify desired page size",
  219. "r*  Reverse order of pages         U*  Disable string param trick",
  220. "s*  Enclose output in save/restore X # Horizontal resolution",
  221. "t s Paper format                   Y # Vertical resolution",
  222. "x # Override dvi magnification     Z*  Compress bitmap fonts",
  223. /* "-   Interactive query of options", */
  224. "    # = number   f = file   s = string  * = suffix, `0' to turn off",
  225. "    c = comma-separated dimension pair (e.g., 3.2in,-32.1cm)", 0} ;
  226. void help() {
  227.    char **p ;
  228.    for (p=helparr; *p; p++)
  229.       fprintf(stderr, " %s\n", *p) ;
  230. }
  231. /*
  232.  *   This error routine prints an error message; if the first
  233.  *   character is !, it aborts the job.
  234.  */
  235. static char *progname ;
  236. void
  237. error(s)
  238.         char *s ;
  239. {
  240.    extern void exit() ;
  241.  
  242.    if (prettycolumn > 0)
  243.         fprintf(stderr,"\n");
  244.    prettycolumn = 0;
  245.    (void)fprintf(stderr, "%s: %s\n", progname, s) ;
  246.    if (*s=='!') {
  247.       if (bitfile != NULL) {
  248.          cleanprinter() ;
  249.       }
  250.       exit(1) ; /* fatal */
  251.    }
  252. }
  253. /*
  254.  *   This is our malloc that checks the results.  We debug the
  255.  *   allocations but not the frees, since memory fragmentation
  256.  *   might be such that we can never use the free'd memory and
  257.  *   it's wise to be conservative.  The only real place we free
  258.  *   is when repacking *huge* characters anyway.
  259.  */
  260. #ifdef DEBUG
  261. static integer totalalloc = 0 ;
  262. #endif
  263. char *mymalloc(n)
  264. integer n ;
  265. {
  266.    char *p ;
  267.  
  268. #ifdef SMALLMALLOC
  269.    if (n > 65500L)
  270.       error("! can't allocate more than 64K!") ;
  271. #endif
  272.    if (n <= 0) /* catch strange 0 mallocs in flib.c without breaking code */
  273.       n = 1 ;
  274. #ifdef DEBUG
  275.    totalalloc += n ;
  276.    if (dd(D_MEM)) {
  277. #ifdef SHORTINT
  278.       fprintf(stderr, "Alloc %ld\n", n) ;
  279. #else
  280.       fprintf(stderr, "Alloc %d\n", n) ;
  281. #endif
  282.    }
  283. #endif
  284.    p = malloc(n) ;
  285.    if (p == NULL)
  286.       error("! no memory") ;
  287.    return p ;
  288. }
  289. /*
  290.  *   Initialize sets up all the globals and data structures.
  291.  */
  292. void
  293. initialize()
  294. {
  295.    int i;
  296.    char *s;
  297.  
  298.    nextfonthd = 0;
  299.    for (i=0; i<256; i++)
  300.       xdig[i] = 0;
  301.    i = 0;
  302.    for (s="0123456789ABCDEF"; *s!=0; s++)
  303.       xdig[*s] = i++;
  304.    i = 10;
  305.    for (s="abcdef"; *s!=0; s++)
  306.       xdig[*s] = i++;
  307.    strings = mymalloc((integer)STRINGSIZE) ;
  308.    maxpages = 100000 ;
  309.    numcopies = 1 ;
  310.    nextstring = strings ;
  311.    iname = fulliname = strings ;
  312.    *nextstring++ = 0 ;
  313.    maxstring = strings + STRINGSIZE - 200 ;
  314.    bitfile = NULL ;
  315.    bytesleft = 0 ;
  316.    swmem = SWMEM ;
  317.    oname = OUTPATH ;
  318.    sendcontrolD = 0 ;
  319.    multiplesects = 0 ;
  320.    disablecomments = 0 ;
  321.    maxdrift = -1 ;
  322.    vmaxdrift = -1 ;
  323. }
  324. /*
  325.  *   This routine copies a string into the string `pool', safely.
  326.  */
  327. char *
  328. newstring(s)
  329.    char *s ;
  330. {
  331.    int l ;
  332.  
  333.    if (s == NULL)
  334.       return(NULL) ;
  335.    l = strlen(s) ;
  336.    if (nextstring + l >= maxstring)
  337.       error("! out of string space") ;
  338.    (void)strcpy(nextstring, s) ;
  339.    s = nextstring ;
  340.    nextstring += l + 1 ;
  341.    return(s) ;
  342. }
  343. void newoutname() {
  344.    static int seq = 0 ;
  345.    static char *seqptr = 0 ;
  346.    char *p ;
  347.  
  348.    if (oname == 0 || *oname == 0)
  349.       error("! need an output file name to specify separate files") ;
  350.    if (*oname != '!') {
  351.       if (seqptr == 0) {
  352.          oname = newstring(oname) ;
  353.          seqptr = 0 ;
  354.          for (p = oname; *p; p++)
  355.             if (*p == '.')
  356.                seqptr = p + 1 ;
  357.          if (seqptr == 0)
  358.             seqptr = p ;
  359.          nextstring += 5 ; /* make room for the number, up to five digits */
  360.       }
  361.       sprintf(seqptr, "%03d", ++seq) ;
  362.    }
  363. }
  364. /*
  365.  *   This routine reverses a list, where a list is defined to be any
  366.  *   structure whose first element is a pointer to another such structure.
  367.  */
  368. void *revlist(p)
  369. void *p ;
  370. {
  371.    struct list {
  372.       struct list *next ;
  373.    } *pp = (struct list *)p, *qq = 0, *tt ;
  374.  
  375.    while (pp) {
  376.       tt = pp->next ;
  377.       pp->next = qq ;
  378.       qq = pp ;
  379.       pp = tt ;
  380.    }
  381.    return (void *)qq ;
  382. }
  383. /* this asks for a new set of arguments from the command line */
  384. void
  385. queryargs()
  386. {
  387.    fputs("Options: ",stdout);
  388.    fgets(queryline,256,stdin);
  389.    qargc=1;
  390.    if ( (qargv[1] = strtok(queryline," \n")) != (char *)NULL ) {
  391.       qargc=2;
  392.       while ( ((qargv[qargc] = strtok((char *)NULL," \n")) != (char *)NULL)
  393.             && (qargc < 31) )
  394.          qargc++;
  395.    }
  396.    qargv[qargc] = (char *)NULL;
  397. }
  398.  
  399. /*
  400.  *   Finally, our main routine.
  401.  */
  402. extern void handlepapersize() ;
  403. #ifdef VMS
  404. main()
  405. #else
  406. void main(argc, argv)
  407.         int argc ;
  408.         char *argv[] ;
  409. #endif
  410. {
  411.    extern void exit() ;
  412.    int i, lastext = -1 ;
  413.    int firstext = -1 ;
  414.    register sectiontype *sects ;
  415.  
  416. #ifdef __THINK__
  417.    argc = dcommand(&argv) ; /* do I/O stream redirection */
  418. #endif
  419. #ifdef VMS        /* Grab the command-line buffer */
  420.    short len_arg;
  421.    $DESCRIPTOR( verb_dsc, "DVIPS ");    /* assume the verb is always DVIPS */
  422.    struct dsc$descriptor_d temp_dsc = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0};
  423.  
  424.    progname = &thh[0] ;
  425.    strcpy(progname,"DVIPS%ERROR");
  426.  
  427.    lib$get_foreign( &temp_dsc, 0, &len_arg, 0);    /* Get the command line */
  428.    str$prefix(&temp_dsc, &verb_dsc);        /* prepend the VERB     */
  429.    len_arg += verb_dsc.dsc$w_length;        /* update the length    */
  430.    temp_dsc.dsc$a_pointer[len_arg] = '\0';    /* terminate the string */
  431.    gargv = &temp_dsc.dsc$a_pointer;        /* point to the buffer  */
  432.    gargc = 1 ;                    /* only one big argv    */
  433. #else
  434.    progname = argv[0] ;
  435.    gargv = argv ;
  436.    gargc = argc ;
  437. /* we sneak a look at the first arg in case it's debugging */
  438. #ifdef DEBUG
  439.    if (argc > 1 && strncmp(argv[1], "-d", 2)==0) {
  440.       if (argv[1][2]==0) {
  441.          if (sscanf(argv[2], "%d", &debug_flag)==0)
  442.             debug_flag = 0 ;
  443.       } else {
  444.          if (sscanf(argv[1]+2, "%d", &debug_flag)==0)
  445.             debug_flag = 0 ;
  446.       }
  447.    }
  448. #endif
  449. #endif
  450.    initialize() ;
  451.    checkenv(0) ;
  452.    getdefaults(CONFIGFILE) ;
  453.    getdefaults((char *)0) ;
  454. /*
  455.  *   This next whole big section of code is straightforward; we just scan
  456.  *   the options.  An argument can either immediately follow its option letter
  457.  *   or be separated by spaces.  Any argument not preceded by '-' and an
  458.  *   option letter is considered a file name; the program complains if more
  459.  *   than one file name is given, and uses stdin if none is given.
  460.  */
  461. #ifdef VMS
  462. vmscli();
  463. #else
  464.    queryoptions = 0;
  465.    do
  466.    {
  467.       for (i=1; i<argc; i++) {
  468.          if (*argv[i]=='-') {
  469.             char *p=argv[i]+2 ;
  470.             char c=argv[i][1] ;
  471.             switch (c) {
  472. case '-':
  473.                queryoptions = 1;
  474.                break;
  475. case 'a':
  476.                dopprescan = (*p != '0') ;
  477.                break ;
  478. case 'b':
  479.                if (*p == 0 && argv[i+1])
  480.                   p = argv[++i] ;
  481.                if (sscanf(p, "%d", &pagecopies)==0)
  482.                   error("! Bad number of page copies option (-b).") ;
  483.                if (pagecopies < 1 || pagecopies > 1000)
  484.                   error("! can only print one to a thousand page copies") ;
  485.                break ;
  486. case 'c' :
  487.                if (*p == 0 && argv[i+1])
  488.                   p = argv[++i] ;
  489.                if (sscanf(p, "%d", &numcopies)==0)
  490.                   error("! Bad number of copies option (-c).") ;
  491.                break ;
  492. case 'd' :
  493. #ifdef DEBUG
  494.                if (*p == 0 && argv[i+1])
  495.                   p = argv[++i];
  496.                if (sscanf(p, "%d", &debug_flag)==0)
  497.                   error("! Bad debug option (-d).");
  498.                break;
  499. #else
  500.                error("not compiled in debug mode") ;
  501.                break ;
  502. #endif /* DEBUG */
  503. case 'e' :
  504.                if (*p == 0 && argv[i+1])
  505.                   p = argv[++i] ;
  506.                if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
  507.                   error("! Bad maxdrift option (-e).") ;
  508.                vmaxdrift = maxdrift;
  509.                break ;
  510. case 'f' :
  511.                filter = (*p != '0') ;
  512.                if (filter)
  513.                   oname = "" ;
  514.                noenv = 1 ;
  515.                sendcontrolD = 0 ;
  516.                break ;
  517. case 'h' : case 'H' :
  518.                if (*p == 0 && argv[i+1])
  519.                   p = argv[++i] ;
  520.                if (strcmp(p, "-") == 0)
  521.                   headers_off = 1 ;
  522.                else
  523.                   (void)add_header(p) ;
  524.                break ;
  525. case 'i':
  526.                sepfiles = (*p != '0') ;
  527.                break ;
  528. case 'k':
  529.                cropmarks = (*p != '0') ;
  530.                break ;
  531. case 'R':
  532.                secure = 1 ;
  533.                break ;
  534. case 'S':
  535.                if (*p == 0 && argv[i+1])
  536.                   p = argv[++i] ;
  537.                if (sscanf(p, "%d", &maxsecsize)==0)
  538.                   error("! Bad section size arg (-S).") ;
  539.                break ;
  540. case 'm' :
  541.                manualfeed = (*p != '0') ;
  542.                break ;
  543. case 'n' :
  544.                if (*p == 0 && argv[i+1])
  545.                   p = argv[++i] ;
  546. #ifdef SHORTINT
  547.                if (sscanf(p, "%ld", &maxpages)==0)
  548. #else        /* ~SHORTINT */
  549.                if (sscanf(p, "%d", &maxpages)==0)
  550. #endif        /* ~SHORTINT */
  551.                   error("! Bad number of pages option (-n).") ;
  552.                break ;
  553. case 'o' :
  554.                if (*p == 0 && argv[i+1] && *argv[i+1]!='-')
  555.                   p = argv[++i] ;
  556.                oname = p ;
  557.                noenv = 1 ;
  558.                sendcontrolD = 0 ;
  559.                break ;
  560. case 'O' :
  561.                if (*p == 0 && argv[i+1])
  562.                   p = argv[++i] ;
  563.                handlepapersize(p, &hoff, &voff) ;
  564.                break ;
  565. case 'T' :
  566.                if (*p == 0 && argv[i+1])
  567.                   p = argv[++i] ;
  568.                handlepapersize(p, &hpapersize, &vpapersize) ;
  569.                if (landscape) {
  570.                   error(
  571.               "both landscape and papersize specified; ignoring landscape") ;
  572.                   landscape = 0 ;
  573.                }
  574.                break ;
  575. case 'p' :
  576. #ifdef MSDOS
  577.                /* check for emTeX job file (-pj=filename) */
  578.                if (*p == 'j') {
  579.                  p++;
  580.                  if (*p == '=')
  581.                    p++;
  582.                  mfjobname = newstring(p);
  583.                  break;
  584.                }
  585.                /* must be page number instead */
  586. #endif
  587.                if (*p == 'p') {  /* a -pp specifier for a page list? */
  588.                   p++ ;
  589.                   if (*p == 0 && argv[i+1])
  590.                      p = argv[++i] ;
  591.                   if (ParsePages(p))
  592.                      error("! Bad page list specifier (-pp).") ;
  593.                   pagelist = 1 ;
  594.                   break ;
  595.                }
  596.                if (*p == 0 && argv[i+1])
  597.                   p = argv[++i] ;
  598.                if (*p == '=') {
  599.                   abspage = 1 ;
  600.                   p++ ;
  601.                }
  602. #ifdef SHORTINT
  603.                switch(sscanf(p, "%ld.%ld", &firstpage, &firstseq)) {
  604. #else        /* ~SHORTINT */
  605.                switch(sscanf(p, "%d.%d", &firstpage, &firstseq)) {
  606. #endif        /* ~SHORTINT */
  607. case 1:           firstseq = 0 ;
  608. case 2:           break ;
  609. default:
  610.                   error("! Bad first page option (-p).") ;
  611.                }
  612.                notfirst = 1 ;
  613.                break ;
  614. case 'l':
  615.                if (*p == 0 && argv[i+1])
  616.                   p = argv[++i] ;
  617.                if (*p == '=') {
  618.                   abspage = 1 ;
  619.                   p++ ;
  620.                }
  621. #ifdef SHORTINT
  622.                switch(sscanf(p, "%ld.%ld", &lastpage, &lastseq)) {
  623. #else        /* ~SHORTINT */
  624.                switch(sscanf(p, "%d.%d", &lastpage, &lastseq)) {
  625. #endif        /* ~SHORTINT */
  626. case 1:           lastseq = 0 ;
  627. case 2:           break ;
  628. default:
  629.                   error("! Bad last page option (-p).") ;
  630.                }
  631.                notlast = 1 ;
  632.                break ;
  633. case 'A':
  634.                oddpages = 1 ;
  635.                break ;
  636. case 'B':
  637.                evenpages = 1 ;
  638.                break ;
  639. case 'q' : case 'Q' :
  640.                quiet = (*p != '0') ;
  641.                break ;
  642. case 'r' :
  643.                reverse = (*p != '0') ;
  644.                break ;
  645. case 't' :
  646.                if (*p == 0 && argv[i+1])
  647.                   p = argv[++i] ;
  648.                if (strcmp(p, "landscape") == 0) {
  649.                   if (hpapersize || vpapersize)
  650.                      error(
  651.              "both landscape and papersize specified; ignoring landscape") ;
  652.                   else
  653.                      landscape = 1 ;
  654.                } else
  655.                   paperfmt = p ;
  656.                break ;
  657. case 'x' : case 'y' :
  658.                if (*p == 0 && argv[i+1])
  659.                   p = argv[++i] ;
  660.                if (sscanf(p, "%d", &mag)==0 || mag < 10 ||
  661.                           mag > 100000)
  662.                   error("! Bad magnification parameter (-x).") ;
  663.                overridemag = (c == 'x' ? 1 : -1) ;
  664.                break ;
  665. case 'C' :
  666.                if (*p == 0 && argv[i+1])
  667.                   p = argv[++i] ;
  668.                if (sscanf(p, "%d", &collatedcopies)==0)
  669.                   error("! Bad number of collated copies option (-C).") ;
  670.                break ;
  671. case 'D' :
  672.                if (*p == 0 && argv[i+1])
  673.                   p = argv[++i] ;
  674.                if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  675.                           actualdpi > 10000)
  676.                   error("! Bad dpi parameter (-D).") ;
  677.                vactualdpi = actualdpi;
  678.                break ;
  679. case 'E' :
  680.                tryepsf = (*p != '0') ;
  681.                break ;
  682. case 'K' :
  683.                removecomments = (*p != '0') ;
  684.                break ;
  685. case 'U' :
  686.                nosmallchars = (*p != '0') ;
  687.                break ;
  688. case 'X' :
  689.                if (*p == 0 && argv[i+1])
  690.                   p = argv[++i] ;
  691.                if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  692.                           actualdpi > 10000)
  693.                   error("! Bad dpi parameter (-D).") ;
  694.                break ;
  695. case 'Y' :
  696.                if (*p == 0 && argv[i+1])
  697.                   p = argv[++i] ;
  698.                if (sscanf(p, "%d", &vactualdpi)==0 || vactualdpi < 10 ||
  699.                           vactualdpi > 10000)
  700.                   error("! Bad dpi parameter (-D).") ;
  701.                vactualdpi = vactualdpi;
  702.                break ;
  703. case 'F' :
  704.                sendcontrolD = (*p != '0') ;
  705.                break ;
  706. case 'M':
  707.                dontmakefont = (*p != '0') ;
  708.                break ;
  709. case 'N' :
  710.                disablecomments = (*p != '0') ;
  711.                break ;
  712. case 'P' :
  713.                if (*p == 0 && argv[i+1])
  714.                   p = argv[++i] ;
  715.                printer = p ;
  716.                noenv = 1 ;
  717.                getdefaults("") ;
  718.                break ;
  719. case 's' :
  720.                safetyenclose = (*p != '0') ;
  721.                break ;
  722. case 'Z' :
  723.                compressed = (*p != '0') ;
  724.                break ;
  725. case '?' :
  726.                (void)fprintf(stderr, banner) ;
  727.                help() ;
  728.                break ;
  729. default:
  730.                error(
  731.               "! Bad option, not one of acdefhiklmnopqrstxCDEFKMNOPSTUXYZ?") ;
  732.             }
  733.          } else {
  734.             if (*iname == 0) {
  735.                register char *p ;
  736.    
  737.                lastext = 0 ;
  738.                iname = nextstring ;
  739.                p = argv[i] ;
  740.                while (*p) {
  741.                   *nextstring = *p++ ;
  742.                   if (*nextstring == '.')
  743.                      lastext = nextstring - iname ;
  744.                   else if (*nextstring == '/' || *nextstring == ':')
  745.                      lastext = 0 ;
  746.                   nextstring++ ;
  747.                }
  748.                *nextstring++ = '.' ;
  749.                *nextstring++ = 'd' ;
  750.                *nextstring++ = 'v' ;
  751.                *nextstring++ = 'i' ;
  752.                *nextstring++ = 0 ;
  753.             } else
  754.                error("! Two input file names specified.") ;
  755.          }
  756.       }
  757.       if (noenv == 0) {
  758.          register char *p ;
  759.          extern char *getenv() ;
  760.          if (p = getenv("PRINTER")) {
  761.             strcpy(nextstring, "config.") ;
  762.             strcat(nextstring, p) ;
  763.             getdefaults(nextstring) ;
  764.          }
  765.       }
  766.       if (queryoptions != 0) {            /* get new options */
  767.          (void)fprintf(stderr, banner) ;
  768.          help() ;
  769.          queryargs();
  770.          if (qargc == 1)
  771.            queryoptions = 0;
  772.          else {
  773.            qargv[0] = argv[0];
  774.            argc=qargc;
  775.            argv=qargv;
  776.          }
  777.       }
  778.    } while (queryoptions != 0) ;
  779. #endif
  780.    checkenv(1) ;
  781. /*
  782.  *   The logic here is a bit convoluted.  Since all `additional'
  783.  *   PostScript font information files are loaded *before* the master
  784.  *   one, and yet they should be able to override the master one, we
  785.  *   have to add the information in the master list to the *ends* of
  786.  *   the hash chain.  We do this by reversing the lists, adding them
  787.  *   to the front, and then reversing them again.
  788.  */
  789.    revpslists() ;
  790.    getpsinfo((char *)NULL) ;
  791.    revpslists() ;
  792.    if (!quiet)
  793.       (void)fprintf(stderr, banner) ;
  794.    if (*iname) {
  795.       dvifile = fopen(iname, READBIN) ;
  796. /*
  797.  *   Allow names like a.b.
  798.  */
  799.       if (dvifile == 0) {
  800.          iname[strlen(iname)-4] = 0 ; /* remove the .dvi suffix */
  801.          dvifile = fopen(iname, READBIN) ;
  802.       }
  803.    }
  804.    if (oname[0] == '-' && oname[1] == 0)
  805.       oname[0] = 0 ;
  806.    if (*oname == 0 && ! filter) {
  807.       oname = nextstring ;
  808. #ifndef VMCMS  /* get stuff before LAST "." */
  809.       lastext = strlen(iname) - 1 ;
  810.       while (iname[lastext] != '.' && lastext > 0)
  811.          lastext-- ;
  812.       if (iname[lastext] != '.')
  813.          lastext = strlen(iname) - 1 ;
  814. #else   /* for VM/CMS we take the stuff before FIRST "." */
  815.       lastext = strchr(iname,'.') - iname ;
  816.       if ( lastext <= 0 )     /* if no '.' in "iname" */
  817.          lastext = strlen(iname) -1 ;
  818. #endif
  819. #ifdef MVSXA /* IBM: MVS/XA */
  820.       if (strchr(iname, '(') != NULL  &&
  821.           strchr(iname, ')') != NULL) {
  822.       firstext = strchr(iname, '(') - iname + 1;
  823.       lastext = strrchr(iname, ')') - iname - 1;
  824.          }
  825.       else {
  826.       if (strrchr(iname, '.') != NULL) {
  827.       lastext = strrchr(iname, '.') - iname - 1;
  828.            }
  829.          else lastext = strlen(iname) - 1 ;
  830.       if (strchr(iname, '\'') != NULL)
  831.          firstext = strchr(iname, '.') - iname + 1;
  832.          else firstext = 0;
  833.       }
  834. #endif  /* IBM: MVS/XA */
  835. #ifdef MVSXA /* IBM: MVS/XA */
  836.       for (i=firstext; i<=lastext; i++)
  837. #else
  838.       for (i=0; i<=lastext; i++)
  839. #endif
  840.          *nextstring++ = iname[i] ;
  841.       if (iname[lastext] != '.')
  842.          *nextstring++ = '.' ;
  843. #ifndef VMCMS
  844.       *nextstring++ = 'p' ;
  845.       *nextstring++ = 's' ;
  846. #else  /* might as well keep things uppercase */
  847.       *nextstring++ = 'P' ;
  848.       *nextstring++ = 'S' ;
  849. #endif
  850.       *nextstring++ = 0 ;
  851.    }
  852. #ifdef DEBUG
  853.    if (dd(D_PATHS)) {
  854. #ifdef SHORTINT
  855.         (void)fprintf(stderr,"input file %s output file %s swmem %ld\n",
  856. #else /* ~SHORTINT */
  857.            (void)fprintf(stderr,"input file %s output file %s swmem %d\n",
  858. #endif /* ~SHORTINT */
  859.            iname, oname, swmem) ;
  860.    (void)fprintf(stderr,"tfm path %s\npk path %s\n", tfmpath, pkpath) ;
  861.    (void)fprintf(stderr,"fig path %s\nvf path %s\n", figpath, vfpath) ;
  862.    (void)fprintf(stderr,"config path %s\nheader path %s\n",
  863.                   configpath, headerpath) ;
  864. #ifdef FONTLIB
  865.    (void)fprintf(stderr,"fli path %s\nfli names %s\n", flipath, fliname) ;
  866. #endif
  867.    } /* dd(D_PATHS) */
  868. #endif /* DEBUG */
  869. /*
  870.  *   Now we try to open the dvi file.
  871.  */
  872.    if (warningmsg)
  873.       error(warningmsg) ;
  874.    headersready = 1 ;
  875.    headerfile = (compressed? CHEADERFILE : HEADERFILE) ;
  876.    (void)add_header(headerfile) ;
  877.    if (*iname != 0) {
  878.       fulliname = nextstring ;
  879. #ifndef IGNORE_CWD
  880.       if (*iname != '/') {
  881.         getcwd(nextstring, MAXPATHLEN + 2);
  882.         while (*nextstring++) ;
  883. #ifdef VMS        /* VMS doesn't need the '/' character appended */
  884.         nextstring--;    /* so just back up one byte. */
  885. #else
  886.         *(nextstring-1) = '/' ;
  887. #endif
  888.       }
  889. #endif
  890.       strcpy(nextstring,iname) ;
  891.       while (*nextstring++) ; /* advance nextstring past fulliname */
  892.    } else if (filter)
  893.       dvifile = stdin;
  894.    else {
  895.       help() ;
  896.       exit(0) ;
  897.    }
  898.    initcolor() ;
  899.    if (dvifile==NULL) {
  900.       extern char errbuf[];
  901.       (void)sprintf(errbuf,"! DVI file <%s> can't be opened.", iname) ;
  902.       error("! DVI file can't be opened.") ;
  903.    }
  904.    if (fseek(dvifile, 0L, 0) < 0)
  905.       error("! DVI file must not be a pipe.") ;
  906. #ifdef FONTLIB
  907.    fliload();    /* read the font libaries */
  908. #endif
  909. /*
  910.  *   Now we do our main work.
  911.  */
  912.    swmem += fontmem ;
  913.    if (maxdrift < 0) {
  914.       if (actualdpi <= 599)
  915.          maxdrift = actualdpi / 100 ;
  916.       else if (actualdpi < 1199)
  917.          maxdrift = actualdpi / 200 + 3 ;
  918.       else
  919.          maxdrift = actualdpi / 400 + 6 ;
  920.    }
  921.    if (vmaxdrift < 0) {
  922.       if (vactualdpi <= 599)
  923.          vmaxdrift = vactualdpi / 100 ;
  924.       else if (vactualdpi < 1199)
  925.          vmaxdrift = vactualdpi / 200 + 3 ;
  926.       else
  927.          vmaxdrift = vactualdpi / 400 + 6 ;
  928.    }
  929.    if (dopprescan)
  930.       pprescanpages() ;
  931.    prescanpages() ;
  932. #ifdef MSDOS
  933.    if (mfjobfile != (FILE*)NULL) {
  934.      char answer[5];
  935.      fputs("}\n",mfjobfile);
  936.      fclose(mfjobfile);
  937.      fputs("Exit to make missing fonts now (y/n)? ",stdout);
  938.      fgets(answer,4,stdin);
  939.      if (*answer=='y' || *answer=='Y')
  940.        exit(8); /* exit with errorlevel 8 for emTeX dvidrv */
  941.    }
  942. #endif
  943.    if (includesfonts)
  944.       (void)add_header(IFONTHEADER) ;
  945.    if (usesPSfonts)
  946.       (void)add_header(PSFONTHEADER) ;
  947.    if (usesspecial)
  948.       (void)add_header(SPECIALHEADER) ;
  949.    if (usescolor)  /* IBM: color */
  950.       (void)add_header(COLORHEADER) ;
  951.    papsizes = (struct papsiz *)revlist((void *)papsizes) ;
  952.    sects = sections ;
  953.    if (sects == NULL || sects->next == NULL) {
  954.       sectioncopies = collatedcopies ;
  955.       collatedcopies = 1 ;
  956.    } else {
  957.       totalpages *= collatedcopies ;
  958.       if (! sepfiles)
  959.          multiplesects = 1 ;
  960.    }
  961.    totalpages *= pagecopies ;
  962.    if (tryepsf) {
  963.       if (totalpages != 1 || paperfmt || landscape || manualfeed ||
  964.           collatedcopies > 1 || numcopies > 1 || cropmarks ||
  965.           *iname == 0) {
  966.          error("Can't make it EPSF, sorry") ;
  967.          tryepsf = 0 ;
  968.       }
  969.    }
  970.    if (! sepfiles) {
  971.       initprinter(0) ;
  972.       outbangspecials() ;
  973.    }
  974.    for (i=0; i<collatedcopies; i++) {
  975.       sects = sections ;
  976.       while (sects != NULL) {
  977.          if (sepfiles) {
  978.             newoutname() ;
  979.             if (! quiet) {
  980.                if (prettycolumn + strlen(oname) + 6 > STDOUTSIZE) {
  981.                   fprintf(stderr, "\n") ;
  982.                   prettycolumn = 0 ;
  983.                }
  984.                (void)fprintf(stderr, "(-> %s) ", oname) ;
  985.                prettycolumn += strlen(oname) + 6 ;
  986.             }
  987.             initprinter(sects->numpages) ;
  988.             outbangspecials() ;
  989.          } else if (! quiet) {
  990.             if (prettycolumn > STDOUTSIZE) {
  991.                fprintf(stderr, "\n") ;
  992.                prettycolumn = 0 ;
  993.             }
  994.             (void)fprintf(stderr, ". ") ;
  995.             prettycolumn += 2 ;
  996.          }
  997.          (void)fflush(stderr) ;
  998.          dosection(sects, sectioncopies) ;
  999.          sects = sects->next ;
  1000.          if (sepfiles)
  1001.             cleanprinter() ;
  1002.       }
  1003.    }
  1004.    if (! sepfiles)
  1005.       cleanprinter() ;
  1006.    if (! quiet)
  1007.       (void)fprintf(stderr, "\n") ;
  1008. #ifdef DEBUG
  1009.    if (dd(D_MEM)) {
  1010. #ifdef SHORTINT
  1011.       fprintf(stderr, "Total memory allocated:  %ld\n", totalalloc) ;
  1012. #else
  1013.       fprintf(stderr, "Total memory allocated:  %d\n", totalalloc) ;
  1014. #endif
  1015.    }
  1016. #endif
  1017.    exit(0) ;
  1018.    /*NOTREACHED*/
  1019. }
  1020. #ifdef VMS
  1021. #include "[]vmscli.c"
  1022. #endif
  1023.  
  1024. #ifdef VMCMS  /* IBM: VM/CMS */
  1025. #include "dvipscms.h"
  1026. #endif
  1027.  
  1028. #ifdef MVSXA  /* IBM: MVS/XA */
  1029. #include "dvipsmvs.h"
  1030. #endif
  1031.