home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d795 / pstools.lha / PSTools / PSTools1.lha / source / dvips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  17.3 KB  |  563 lines

  1. /*
  2.  *   This is the main routine.
  3.  */
  4. #ifndef DEFRES
  5. #define DEFRES (300)
  6. #endif
  7.  
  8. #include "structures.h" /* The copyright notice there is included too! */
  9. #ifdef VMS
  10. #define GLOBAL globaldef
  11. #include climsgdef
  12. #include ctype
  13. #include descrip
  14. #endif
  15. /*
  16.  *   First we define some globals.
  17.  */
  18. #ifdef VMS
  19.     static char ofnme[252],infnme[252],pap[40],thh[20];
  20. #endif
  21. fontdesctype *fonthead ;      /* list of all fonts mentioned so far */
  22. fontdesctype *curfnt ;        /* the currently selected font */
  23. sectiontype *sections ;       /* sections to process document in */
  24. Boolean manualfeed ;          /* manual feed? */
  25. Boolean compressed ;          /* compressed? */
  26. Boolean safetyenclose ;       /* enclose in save/restore for stupid spoolers? */
  27. Boolean removecomments = 1 ;  /* remove comments from included PS? */
  28. Boolean nosmallchars ;        /* disable small char optimization for X4045? */
  29. int collatedcopies = 1 ;      /* how many collated copies? */
  30. int sectioncopies = 1 ;       /* how many times to repeat each section? */
  31. shalfword linepos = 0 ;       /* where are we on the line being output? */
  32. integer maxpages ;            /* the maximum number of pages */
  33. Boolean notfirst, notlast ;   /* true if a first page was specified */
  34. Boolean sendcontrolD ;        /* should we send a control D at end? */
  35. integer firstpage ;           /* the number of the first page if specified */
  36. integer lastpage ;
  37. integer firstseq ;
  38. integer lastseq ;
  39. int numcopies ;               /* number of copies of each page to print */
  40. char *oname ;                 /* output file name */
  41. char *iname ;                 /* dvi file name */
  42. char *strings ;               /* strings for program */
  43. char *nextstring, *maxstring ; /* string pointers */
  44. FILE *dvifile, *bitfile ;     /* dvi and output files */
  45. quarterword *curpos ;         /* current position in virtual character packet */
  46. quarterword *curlim ;         /* final byte in virtual character packet */
  47. fontmaptype *ffont ;          /* first font in current frame */
  48. real conv ;                   /* conversion ratio, pixels per DVI unit */
  49. real vconv ;                  /* conversion ratio, pixels per DVI unit */
  50. real alpha ;                  /* conversion ratio, DVI unit per TFM unit */
  51. integer mag ;                 /* the magnification of this document */
  52. Boolean overridemag ;         /* substitute for mag value in DVI file? */
  53. int actualdpi = DEFRES ;      /* the actual resolution of the printer */
  54. int vactualdpi = DEFRES ;      /* the actual resolution of the printer */
  55. int maxdrift ;                /* max pixels away from true rounded position */
  56. int vmaxdrift ;                /* max pixels away from true rounded position */
  57. char *paperfmt                /* paper format */
  58. #ifdef DEFPFMT
  59.     = DEFPFMT ;
  60. #else
  61.     ;
  62. #endif
  63. int landscape = 0 ;           /* landscape mode */
  64. integer fontmem ;             /* memory remaining in printer */
  65. integer pagecount ;           /* page counter for the sections */
  66. integer pagenum ;             /* the page number we currently look at */
  67. long bytesleft ;              /* number of bytes left in raster */
  68. quarterword *raster ;         /* area for raster manipulations */
  69. integer hh, vv ;              /* horizontal and vertical pixel positions */
  70. char *tfmpath = TFMPATH ;     /* pointer to directories for tfm files */
  71. char *pkpath = PKPATH ;       /* pointer to directories for pk files */
  72. char *vfpath = VFPATH ;       /* pointer to directories for vf files */
  73. char *figpath = FIGPATH ;     /* pointer to directories for figure files */
  74. char *headerpath = HEADERPATH ; /* pointer to directories for header files */
  75. char *configpath = CONFIGPATH;  /* where to find config files */
  76. #ifdef SEARCH_SUBDIRECTORIES
  77. char *fontsubdirpath = FONTSUBDIRPATH ;
  78. #endif
  79. #ifdef FONTLIB
  80. char *flipath = FLIPATH ;     /* pointer to directories for fli files */
  81. char *fliname = FLINAME ;     /* pointer to names of fli files */
  82. #endif
  83. integer swmem ;               /* font memory in the PostScript printer */
  84. int quiet ;                   /* should we only print errors to stderr? */
  85. int filter ;                  /* act as filter default output to stdout,
  86.                                                default input to stdin? */
  87. int prettycolumn ;            /* the column we are at when running pretty */
  88. int totalpages = 0 ;          /* total number of pages */
  89. Boolean reverse ;             /* are we going reverse? */
  90. Boolean usesPSfonts ;         /* do we use local PostScript fonts? */
  91. Boolean usesspecial ;         /* do we use \special? */
  92. Boolean headers_off ;         /* do we send headers or not? */
  93. char *headerfile ;            /* default header file */
  94. char *warningmsg ;            /* a message to write, if set in config file */
  95. Boolean multiplesects ;       /* more than one section? */
  96. Boolean disablecomments ;     /* should we suppress any EPSF comments? */
  97. char *printer ;               /* what printer to send this to? */
  98. char *mfmode ;                /* default MF mode */
  99. frametype frames[MAXFRAME] ;  /* stack for virtual fonts */
  100. fontdesctype *baseFonts[256] ; /* base fonts for dvi file */
  101. integer pagecost;               /* memory used on the page being prescanned */
  102. int delchar;                    /* characters to delete from prescanned page */
  103. integer fsizetol;               /* max dvi units error for psfile font sizes */
  104. Boolean includesfonts;          /* are fonts used in included psfiles? */
  105. fontdesctype *fonthd[MAXFONTHD];/* list headers for included fonts of 1 name */
  106. int nextfonthd;                 /* next unused fonthd[] index */
  107. char xdig[256];                 /* table for reading hexadecimal digits */
  108. char banner[] = BANNER ;        /* our startup message */
  109. Boolean noenv ;                 /* ignore PRINTER envir variable? */
  110. extern int dontmakefont ;
  111. #ifdef DEBUG
  112. integer debug_flag = 0;
  113. #endif /* DEBUG */
  114. /*
  115.  *   This routine calls the following externals:
  116.  */
  117. extern void outbangspecials() ;
  118. extern void prescanpages() ;
  119. extern void initprinter() ;
  120. extern void cleanprinter() ;
  121. extern void dosection() ;
  122. extern void getdefaults() ;
  123. extern void cmdout() ;
  124. extern void numout() ;
  125. extern int add_header() ;
  126. extern void checkenv() ;
  127. extern void getpsinfo() ;
  128. #ifdef FONTLIB
  129. extern void fliload() ;
  130. #endif
  131. /*
  132.  *   This error routine prints an error message; if the first
  133.  *   character is !, it aborts the job.
  134.  */
  135. static char *progname ;
  136. void
  137. error(s)
  138.     char *s ;
  139. {
  140.  
  141.    (void)fprintf(stderr, "%s: %s\n", progname, s) ;
  142.    if (*s=='!') {
  143.       if (bitfile != NULL) {
  144.          cleanprinter() ;
  145.       }
  146.       exit(1) ; /* fatal */
  147.    }
  148. }
  149.  
  150. /*
  151.  *   Initialize sets up all the globals and data structures.
  152.  */
  153. void
  154. initialize()
  155. {
  156.    int i;
  157.    char *s;
  158.  
  159.    nextfonthd = 0;
  160.    for (i=0; i<256; i++)
  161.       xdig[i] = 0;
  162.    i = 0;
  163.    for (s="0123456789ABCDEF"; *s!=0; s++)
  164.       xdig[*s] = i++;
  165.    i = 10;
  166.    for (s="abcdef"; *s!=0; s++)
  167.       xdig[*s] = i++;
  168.    strings = malloc(STRINGSIZE) ;
  169.    if (strings == 0)
  170.       error("! no memory for strings") ;
  171.    maxpages = 100000 ;
  172.    numcopies = 1 ;
  173.    nextstring = strings ;
  174.    iname = strings ;
  175.    *nextstring++ = 0 ;
  176.    maxstring = strings + STRINGSIZE - 200 ;
  177.    bitfile = NULL ;
  178.    bytesleft = 0 ;
  179.    swmem = SWMEM ;
  180.    oname = OUTPATH ;
  181.    sendcontrolD = 0 ;
  182.    multiplesects = 0 ;
  183.    disablecomments = 0 ;
  184.    maxdrift = -1 ;
  185.    vmaxdrift = -1 ;
  186. }
  187. /*
  188.  *   This routine copies a string into the string `pool', safely.
  189.  */
  190. char *
  191. newstring(s)
  192.    char *s ;
  193. {
  194.    int l ;
  195.  
  196.    if (s == NULL)
  197.       return(NULL) ;
  198.    l = strlen(s) ;
  199.    if (nextstring + l >= maxstring)
  200.       error("! out of string space") ;
  201.    (void)strcpy(nextstring, s) ;
  202.    s = nextstring ;
  203.    nextstring += l + 1 ;
  204.    return(s) ;
  205. }
  206. /*
  207.  *   Finally, our main routine.
  208.  */
  209. #ifdef VMS
  210. main()
  211. #else
  212. void main(argc, argv)
  213.     int argc ;
  214.     char *argv[] ;
  215. #endif
  216. {
  217.    int i, lastext = -1 ;
  218.    register sectiontype *sects ;
  219.  
  220. #ifdef VMS
  221.    progname = &thh[0] ;
  222.    strcpy(progname,"DVIPS%ERROR");
  223. #else
  224.    progname = argv[0] ;
  225. /* we sneak a look at the first arg in case it's debugging */
  226. #ifdef DEBUG
  227.    if (argc > 1 && strncmp(argv[1], "-d", 2)==0) {
  228.       if (sscanf(argv[1]+2, "%d", &debug_flag)==0)
  229.          debug_flag = 0 ;
  230.    }
  231. #endif
  232. #endif
  233.    initialize() ;
  234.    checkenv(0) ;
  235.    getdefaults(CONFIGFILE) ;
  236.    getdefaults((char *)0) ;
  237. /*
  238.  *   This next whole big section of code is straightforward; we just scan
  239.  *   the options.  An argument can either immediately follow its option letter
  240.  *   or be separated by spaces.  Any argument not preceded by '-' and an
  241.  *   option letter is considered a file name; the program complains if more
  242.  *   than one file name is given, and uses stdin if none is given.
  243.  */
  244. #ifdef VMS
  245. vmscli();
  246. #else
  247.    for (i=1; i<argc; i++) {
  248.       if (*argv[i]=='-') {
  249.          char *p=argv[i]+2 ;
  250.          char c=argv[i][1] ;
  251.          switch (c) {
  252. case 'c' :
  253.             if (*p == 0 && argv[i+1])
  254.                p = argv[++i] ;
  255.             if (sscanf(p, "%d", &numcopies)==0)
  256.                error("! Bad number of copies option (-c).") ;
  257.             break ;
  258. case 'd' :
  259. #ifdef DEBUG
  260.         if (*p == 0 && argv[i+1])
  261.            p = argv[++i];
  262.         if (sscanf(p, "%d", &debug_flag)==0)
  263.            error("! Bad debug option (-d).");
  264.         break;
  265. #else
  266.             error("not compiled in debug mode") ;
  267.             break ;
  268. #endif /* DEBUG */
  269. case 'e' :
  270.             if (*p == 0 && argv[i+1])
  271.                p = argv[++i] ;
  272.             if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
  273.                error("! Bad maxdrift option (-e).") ;
  274.         vmaxdrift = maxdrift;
  275.             break ;
  276. case 'f' :
  277.             filter = (*p != '0') ;
  278.             if (filter)
  279.                oname = "" ;
  280.             noenv = 1 ;
  281.             sendcontrolD = 0 ;
  282.             break ;
  283. case 'h' : case 'H' :
  284.             if (*p == 0 && argv[i+1])
  285.                p = argv[++i] ;
  286.             if (strcmp(p, "-") == 0)
  287.                headers_off = 1 ;
  288.             else
  289.                (void)add_header(p) ;
  290.             break ;
  291. case 'm' :
  292.             manualfeed = (*p != '0') ;
  293.             break ;
  294. case 'n' :
  295.             if (*p == 0 && argv[i+1])
  296.                p = argv[++i] ;
  297.             if (sscanf(p, "%d", &maxpages)==0)
  298.                error("! Bad number of pages option (-n).") ;
  299.             break ;
  300. case 'o' : case 'O' :
  301.             if (*p == 0 && argv[i+1] && *argv[i+1]!='-')
  302.                p = argv[++i] ;
  303.             oname = p ;
  304.             noenv = 1 ;
  305.             sendcontrolD = 0 ;
  306.             break ;
  307. case 'p' :
  308.             if (*p == 0 && argv[i+1])
  309.                p = argv[++i] ;
  310.             switch(sscanf(p, "%d.%d", &firstpage, &firstseq)) {
  311. case 1:        firstseq = 0 ;
  312. case 2:        break ;
  313. default:
  314.                error("! Bad first page option (-p).") ;
  315.             }
  316.             notfirst = 1 ;
  317.             break ;
  318. case 'l':
  319.             if (*p == 0 && argv[i+1])
  320.                p = argv[++i] ;
  321.             switch(sscanf(p, "%d.%d", &lastpage, &lastseq)) {
  322. case 1:        lastseq = 0 ;
  323. case 2:        break ;
  324. default:
  325.                error("! Bad last page option (-p).") ;
  326.             }
  327.             notlast = 1 ;
  328.             break ;
  329. case 'q' : case 'Q' :
  330.             quiet = (*p != '0') ;
  331.             break ;
  332. case 'r' :
  333.             reverse = (*p != '0') ;
  334.             break ;
  335. case 't' :
  336.             if (*p == 0 && argv[i+1])
  337.                p = argv[++i] ;
  338.             if (strcmp(p, "landscape") == 0)
  339.                landscape = 1;
  340.             else
  341.                paperfmt = p ;
  342.             break ;
  343. case 'x' :
  344.             if (*p == 0 && argv[i+1])
  345.                p = argv[++i] ;
  346.             if (sscanf(p, "%d", &mag)==0 || mag < 10 ||
  347.                        mag > 100000)
  348.                error("! Bad magnification parameter (-x).") ;
  349.             overridemag = 1 ;
  350.             break ;
  351. case 'C' :
  352.             if (*p == 0 && argv[i+1])
  353.                p = argv[++i] ;
  354.             if (sscanf(p, "%d", &collatedcopies)==0)
  355.                error("! Bad number of collated copies option (-C).") ;
  356.             break ;
  357. case 'D' :
  358.             if (*p == 0 && argv[i+1])
  359.                p = argv[++i] ;
  360.             if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  361.                        actualdpi > 10000)
  362.                error("! Bad dpi parameter (-D).") ;
  363.         vactualdpi = actualdpi;
  364.             break ;
  365. case 'K' :
  366.             removecomments = (*p != '0') ;
  367.             break ;
  368. case 'U' :
  369.             nosmallchars = (*p != '0') ;
  370.             break ;
  371. case 'X' :
  372.             if (*p == 0 && argv[i+1])
  373.                p = argv[++i] ;
  374.             if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  375.                        actualdpi > 10000)
  376.                error("! Bad dpi parameter (-D).") ;
  377.             break ;
  378. case 'Y' :
  379.             if (*p == 0 && argv[i+1])
  380.                p = argv[++i] ;
  381.             if (sscanf(p, "%d", &vactualdpi)==0 || vactualdpi < 10 ||
  382.                        vactualdpi > 10000)
  383.                error("! Bad dpi parameter (-D).") ;
  384.         vactualdpi = vactualdpi;
  385.             break ;
  386. case 'F' :
  387.             sendcontrolD = (*p != '0') ;
  388.             break ;
  389. case 'M':
  390.             dontmakefont = 1 ;
  391.             break ;
  392. case 'N' :
  393.             disablecomments = (*p != '0') ;
  394.             break ;
  395. case 'P' :
  396.             if (*p == 0 && argv[i+1])
  397.                p = argv[++i] ;
  398.             printer = p ;
  399.             noenv = 1 ;
  400.             getdefaults("") ;
  401.             break ;
  402. case 'R' :
  403.             reverse = 0 ;
  404.             break ;
  405. case 's' :
  406.             safetyenclose = (*p != '0') ;
  407.             break ;
  408. case 'Z' :
  409.             compressed = (*p != '0') ;
  410.             break ;
  411. case '?' :
  412.             (void)fprintf(stderr, banner) ;
  413.             break ;
  414. default:
  415.             error("! Bad option, not one of cefhlmnopqrtxCDFKNPUXYZ?") ;
  416.          }
  417.       } else {
  418.          if (*iname == 0) {
  419.             register char *p ;
  420.  
  421.             lastext = 0 ;
  422.             iname = nextstring ;
  423.             p = argv[i] ;
  424.             while (*p) {
  425.                *nextstring = *p++ ;
  426.                if (*nextstring == '.')
  427.                   lastext = nextstring - iname ;
  428.                else if (*nextstring == '/' || *nextstring == ':')
  429.                   lastext = 0 ;
  430.                nextstring++ ;
  431.             }
  432.             if (lastext == 0) {
  433.                lastext = nextstring - iname ;
  434.                *nextstring++ = '.' ;
  435.                *nextstring++ = 'd' ;
  436.                *nextstring++ = 'v' ;
  437.                *nextstring++ = 'i' ;
  438.             }
  439.             *nextstring++ = 0 ;
  440.          } else
  441.             error("! Two input file names specified.") ;
  442.       }
  443.    }
  444.    if (noenv == 0) {
  445.       register char *p ;
  446.       if (p = getenv("PRINTER")) {
  447.          strcpy(nextstring, "config.") ;
  448.          strcat(nextstring, p) ;
  449.          getdefaults(nextstring) ;
  450.       }
  451.    }
  452. #endif
  453.    checkenv(1) ;
  454.    getpsinfo() ;
  455.    if (!quiet)
  456.       (void)fprintf(stderr, banner) ;
  457.    if (oname[0] == '-' && oname[1] == 0)
  458.       oname[0] = 0 ;
  459.    if (*oname == 0 && ! filter) {
  460.       oname = nextstring ;
  461.       for (i=0; i<=lastext; i++)
  462.          *nextstring++ = iname[i] ;
  463.       *nextstring++ = 'p' ;
  464.       *nextstring++ = 's' ;
  465.       *nextstring++ = 0 ;
  466.    }
  467. #ifdef DEBUG
  468.    if (dd(D_PATHS)) {
  469. #ifdef SHORTINT
  470.     (void)fprintf(stderr,"input file %s output file %s swmem %ld\n",
  471. #else /* ~SHORTINT */
  472.        (void)fprintf(stderr,"input file %s output file %s swmem %d\n",
  473. #endif /* ~SHORTINT */
  474.            iname, oname, swmem) ;
  475.    (void)fprintf(stderr,"tfm path %s\npk path %s\n", tfmpath, pkpath) ;
  476.    (void)fprintf(stderr,"fig path %s\nvf path %s\n", figpath, vfpath) ;
  477.    (void)fprintf(stderr,"config path %s\nheader path %s\n", 
  478.                   configpath, headerpath) ;
  479. #ifdef FONTLIB
  480.    (void)fprintf(stderr,"fli path %s\nfli names %s\n", flipath, fliname) ;
  481. #endif
  482.    } /* dd(D_PATHS) */
  483. #endif /* DEBUG */
  484. /*
  485.  *   Now we try to open the dvi file.
  486.  */
  487.    if (warningmsg)
  488.       error(warningmsg) ;
  489.    headerfile = (compressed? CHEADERFILE : HEADERFILE) ;
  490.    (void)add_header(headerfile) ;
  491.    if (*iname != 0)
  492.       dvifile = fopen(iname, READBIN) ;
  493.    else if (filter)
  494.       dvifile = stdin;
  495.    else
  496.       error("! No input filename supplied.") ;
  497.    if (dvifile==NULL)
  498.       error("! DVI file can't be opened.") ;
  499. #ifdef FONTLIB
  500.    fliload();    /* read the font libaries */
  501. #endif
  502. /*
  503.  *   Now we do our main work.
  504.  */
  505.    if (maxdrift < 0) {
  506.       if (actualdpi <= 599)
  507.          maxdrift = actualdpi / 100 ;
  508.       else if (actualdpi < 1199)
  509.          maxdrift = actualdpi / 200 + 3 ;
  510.       else
  511.          maxdrift = actualdpi / 400 + 6 ;
  512.    }
  513.    if (vmaxdrift < 0) {
  514.       if (vactualdpi <= 599)
  515.          vmaxdrift = vactualdpi / 100 ;
  516.       else if (vactualdpi < 1199)
  517.          vmaxdrift = vactualdpi / 200 + 3 ;
  518.       else
  519.          vmaxdrift = vactualdpi / 400 + 6 ;
  520.    }
  521.    prescanpages() ;
  522.    if (includesfonts)
  523.       (void)add_header(IFONTHEADER) ;
  524.    if (usesPSfonts)
  525.       (void)add_header(PSFONTHEADER) ;
  526.    if (usesspecial)
  527.       (void)add_header(SPECIALHEADER) ;
  528.    sects = sections ;
  529.    if (sects == NULL || sects->next == NULL) {
  530.       sectioncopies = collatedcopies ;
  531.       collatedcopies = 1 ;
  532.    } else {
  533.       totalpages *= collatedcopies ;
  534.       multiplesects = 1 ;
  535.    }
  536.    initprinter() ;
  537.    outbangspecials() ;
  538.    for (i=0; i<collatedcopies; i++) {
  539.       sects = sections ;
  540.       while (sects != NULL) {
  541.          if (! quiet) {
  542.             if (prettycolumn > 77) {
  543.                fprintf(stderr, "\n") ;
  544.                prettycolumn = 0 ;
  545.             }
  546.             (void)fprintf(stderr, ". ") ;
  547.             prettycolumn += 2 ;
  548.          }
  549.          (void)fflush(stderr) ;
  550.          dosection(sects, sectioncopies) ;
  551.          sects = sects->next ;
  552.       }
  553.    }
  554.    cleanprinter() ;
  555.    if (! quiet)
  556.       (void)fprintf(stderr, "\n") ;
  557.    exit(0) ;
  558.    /*NOTREACHED*/
  559. }
  560. #ifdef VMS
  561. #include "vms/vmscli.c"
  562. #endif
  563.