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