home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / output.c < prev    next >
C/C++ Source or Header  |  1993-09-04  |  32KB  |  1,221 lines

  1. /*
  2.  *   These routines do most of the communicating with the printer.
  3.  *
  4.  *   LINELENGTH tells the maximum line length to send out.  It's been
  5.  *   reduced to 72 because sometimes PostScript files are included in
  6.  *   mail messages and many mailers mutilate longer lines.
  7.  */
  8. #define LINELENGTH (72)
  9. #include "dvips.h" /* The copyright notice in that file is included too! */
  10. #include <ctype.h>
  11. /*
  12.  *   The external routines called here:
  13.  */
  14. extern void error() ;
  15. extern void send_headers() ;
  16. extern int add_header() ;
  17. extern FILE *search() ;
  18. extern char *getenv() ;
  19. extern void makepsname() ;
  20. extern void handlepapersize() ;
  21. extern void findbb() ;
  22. /*
  23.  *   These are the external variables used by these routines.
  24.  */
  25. extern integer hh, vv ;
  26. extern fontdesctype *curfnt ;
  27. extern FILE *bitfile ;
  28. extern char *oname ;
  29. extern Boolean secure ;
  30. extern Boolean reverse ;
  31. extern Boolean removecomments ;
  32. extern Boolean sendcontrolD, disablecomments, multiplesects ;
  33. extern Boolean usesPSfonts, headers_off ;
  34. extern Boolean safetyenclose ;
  35. extern Boolean cropmarks ;
  36. extern Boolean tryepsf ;
  37. extern int numcopies ;
  38. extern int collatedcopies ;
  39. extern integer pagecopies ;
  40. extern int totalpages ;
  41. extern integer pagenum ;
  42. extern Boolean manualfeed ;
  43. extern int landscape ;
  44. extern int quiet ;
  45. extern int prettycolumn ;
  46. extern int actualdpi, vactualdpi ;
  47. extern char *iname, *nextstring ;
  48. extern char *paperfmt ;
  49. extern char *headerpath ;
  50. extern char errbuf[] ;
  51. extern shalfword linepos ;
  52. extern char *figpath ;
  53. extern struct header_list *ps_fonts_used ;
  54. extern char banner[] ;
  55. extern int gargc ;
  56. extern char **gargv ;
  57. extern struct papsiz *papsizes ;
  58. extern integer hpapersize, vpapersize ;
  59. char preamblecomment[256] ; /* usually "TeX output ..." */
  60. /*
  61.  *   We need a few statics to take care of things.
  62.  */
  63. static integer rhh, rvv ;
  64. static int instring ;
  65. static Boolean lastspecial = 1 ;
  66. static shalfword d ;
  67. static Boolean popened = 0 ;
  68. int lastfont ; /* exported to dospecial to fix rotate.tex problem */
  69. static void chrcmd();                   /* just a forward declaration */
  70. static char strbuffer[LINELENGTH + 20], *strbp = strbuffer ;
  71. static struct papsiz *finpapsiz ;
  72. static struct papsiz defpapsiz = {
  73.    0, 40258437L, 52099154L, "letter", ""
  74. } ;
  75. #ifdef CREATIONDATE
  76. #if !defined(VMS) && !defined(MSDOS) && !defined(AMIGA) /* VAXC/MSDOS don't like/need this !! */
  77. #include <sys/types.h>
  78. #include <sys/time.h> /* time(), at least on BSD Unix */
  79. #endif
  80. #include <time.h> /* asctime() and localtime(), at least on BSD Unix */
  81. static time_t jobtime;
  82. #endif
  83. /*
  84.  *   This routine copies a file down the pipe.  Search path uses the
  85.  *   header path.
  86.  *
  87.  *   We add code to handle the case of MS-DOS font files.
  88.  *
  89.  *   Format:  80 {01,02} four byte length in littleendian order data
  90.  *   repeated possibly multiple times.
  91.  */
  92. static char *hxdata = "0123456789ABCDEF" ;
  93. static int infigure ;
  94. extern char *infont ;
  95. static char *begbinary = "\n%%BeginBinary:" ;
  96. void
  97. copyfile(s)
  98.         char *s ;
  99. {
  100.    FILE *f = NULL ;
  101.    int c, prevc = '\n' ;
  102.    long len ;
  103.    char *bbmatch = begbinary ;
  104. #ifdef VMCMS
  105.    register char *lastdirsep ;
  106.    register char *trunc_s ;
  107.    trunc_s = s ;
  108. #endif
  109. #ifdef MVSXA
  110.    register char *lastdirsep ;
  111.    register char *trunc_s ;
  112.    trunc_s = s ;
  113. #endif
  114.  
  115.    switch (infigure) {
  116.    case 1:
  117. /*
  118.  *   Look in headerpath too, just in case.  This allows common header
  119.  *   or figure files to be installed in the .../ps directory.
  120.  */
  121.       f = search(figpath, s, READ) ;
  122.       if (f == 0)
  123.          f = search(headerpath, s, READ) ;
  124. #ifdef VMCMS
  125.       lastdirsep = strrchr(s, '/') ;
  126.       if ( NULL != lastdirsep ) trunc_s = lastdirsep + 1 ;
  127.       (void)sprintf(errbuf,
  128.    "Couldn't find figure file %s with CMS name %s; continuing", s, trunc_s) ;
  129. #else
  130. #ifdef MVSXA
  131.       lastdirsep = strrchr(s, '/') ;
  132.       if ( NULL != lastdirsep ) trunc_s = lastdirsep + 1 ;
  133.       (void)sprintf(errbuf,
  134.     "Couldn't find figure file %s with MVS name %s; continuing", s, trunc_s) ;
  135. #else
  136.       (void)sprintf(errbuf, "Couldn't find figure file %s; continuing", s) ;
  137. #endif
  138. #endif
  139.       break ;
  140.    default:
  141.       f = search(headerpath, s, READ) ;
  142.       (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
  143.       break ;
  144. #ifndef VMCMS
  145. #ifndef MVSXA
  146. #ifndef VMS
  147. #ifndef MSDOS
  148. #ifndef __THINK__
  149. #ifndef AMIGA
  150.    case 2:
  151. #ifdef SECURE
  152.       (void)sprintf(errbuf, "<%s>: Tick filename execution disabled", s) ;
  153. #else
  154.       if (secure == 0)
  155.          f = popen(s, "r") ;
  156.       (void)sprintf(errbuf, "Failure to execute %s; continuing", s) ;
  157. #endif
  158.       break;
  159. #endif
  160. #endif
  161. #endif
  162. #endif
  163. #endif
  164. #endif
  165.    }
  166.    if (f==NULL)
  167.       error(errbuf) ;
  168.    else {
  169.       if (! quiet) {
  170.          if (strlen(s) + prettycolumn > STDOUTSIZE) {
  171.             fprintf(stderr, "\n") ;
  172.             prettycolumn = 0 ;
  173.          }
  174. #ifdef VMCMS
  175.          (void)fprintf(stderr, "<%s>", trunc_s) ;
  176. #else
  177. #ifdef MVSXA
  178.          (void)fprintf(stderr, "<%s>", trunc_s) ;
  179. #else
  180.          (void)fprintf(stderr, "<%s>", s) ;
  181. #endif
  182. #endif
  183.          (void)fflush(stderr) ;
  184.          prettycolumn += 2 + strlen(s) ;
  185.       }
  186.       if (linepos != 0)
  187.          (void)putc('\n', bitfile) ;
  188.       if (! disablecomments)
  189.          if (infigure)
  190.             (void)fprintf(bitfile, "%%%%BeginDocument: %s\n", s) ;
  191.          else if (infont)
  192.             (void)fprintf(bitfile, "%%%%BeginFont: %s\n", infont) ;
  193.          else
  194.             (void)fprintf(bitfile, "%%%%BeginProcSet: %s\n", s) ;
  195.       c = getc(f) ;
  196.       if (c == 0x80) {
  197. #if defined MSDOS || defined OS2
  198.          (void)fclose(f) ;  /* close MSDOS font file */
  199.          f = search(headerpath, s, READBIN) ;  /* reopen in BINARY mode */
  200.          (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
  201.          if (f==NULL)
  202.            error(errbuf) ;
  203.          c = getc(f);
  204. #endif
  205.          while (1) {
  206.             c = getc(f) ;
  207.             switch(c) {
  208. case 1:
  209. case 2:
  210.                len = getc(f) ;
  211.                len += getc(f) * 256L ;
  212.                len += getc(f) * 65536L ;
  213.                len += getc(f) * 256L * 65536 ;
  214.                if (c == 1) {
  215.                   while (len > 0) {
  216.                      c = getc(f) ;
  217.                      if (c == EOF) {
  218.                         error("premature EOF in MS-DOS font file") ;
  219.                         len = 0 ;
  220.                      } else {
  221.                         if (c == 13)
  222.                            (void)putc('\n', bitfile) ;
  223.                         else
  224.                            (void)putc(c, bitfile) ;
  225.                         len-- ;
  226.                      }
  227.                   }
  228.                } else {
  229.                   putc('\n', bitfile) ;
  230.                   prevc = 0 ;
  231.                   while (len > 0) {
  232.                      c = getc(f) ;
  233.                      if (c == EOF) {
  234.                         error("premature EOF in MS-DOS font file") ;
  235.                         len = 0 ;
  236.                      } else {
  237.                         (void)putc(hxdata[c >> 4], bitfile) ;
  238.                         (void)putc(hxdata[c & 15], bitfile) ;
  239.                         len-- ;
  240.                         prevc += 2 ;
  241.                         if (prevc >= 76) {
  242.                            putc('\n', bitfile) ;
  243.                            prevc = 0 ;
  244.                         }
  245.                      }
  246.                   }
  247.                }
  248.                break ;
  249. case 3:
  250.                goto msdosdone ;
  251. default:
  252.                error("saw type other than 1, 2, or 3 in MS-DOS font file") ;
  253.                break ;
  254.             }
  255.             c = getc(f) ;
  256.             if (c == EOF)
  257.                break ;
  258.             if (c != 0x80) {
  259.                error("saw non-MSDOS header in MSDOS font file") ;
  260.                break ;
  261.             }
  262.          }
  263. msdosdone:
  264.          prevc = 0 ;
  265.       } else {
  266.          if (c != EOF) {
  267.             while (1) {
  268.                if (c == *bbmatch) {
  269.                   bbmatch++ ;
  270.                   if (*bbmatch == '\0') {
  271.                      integer size = 0 ;
  272.  
  273.                      if (removecomments)
  274.                         (void)fputs(begbinary, bitfile) ;
  275.                      (void)putc(c, bitfile) ;
  276.                      while (1) {
  277.                         c = getc(f) ;
  278.                         if (c