home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / PSD.ZIP / PSD.C next >
Text File  |  1988-05-23  |  12KB  |  330 lines

  1. /***************************************************************************
  2. *               PSD (PostScript Display)
  3. *       Copyright 1986, 1987, 1988 MARTECH Consulting Group
  4. *                            3457 Iris Court
  5. *                             Boulder, CO 80302
  6. *                           (303) 447-0832
  7. *       Distributed free for non-commercial use only.
  8. *
  9. * Creates a Postscript file from one or more ASCII input files.
  10. * Prints in 10 pt. Courier (default) or 8 pt. Times-Bold in either
  11. * Portrait (default) or Landscape mode.  
  12. * (8 pt. allows a wide report to print in Portrait mode on 
  13. * 8.5 inch paper; Courier evenly spaces all characters to keep
  14. * columns aligned.)
  15. * Courier 10 pt. is used because the LaserWriter has it permanently in ROM.
  16. * With Times-Bold actual print widths vary with character content.  
  17. * Tests with numeric characters produced the following widths 
  18. * on 8.5x11 paper:
  19. *       Times-Bold, 8 pt.; Portrait: 139
  20. *       Times-Bold, 8 pt.; Landscape: 188
  21. *       Courier, 10 pt.; Portrait: 93
  22. *       Courier, 10 pt.; Landscape: 125
  23. * On 14 inch paper the landscape widths would be about 232 characters
  24. * for Times-Bold and 154 characters for Courier.
  25. *
  26. * This program assumes that input line length does not exceed print width.
  27. * A PostScript printer will simply truncate anything that doesn't fit.
  28. *
  29. * Normally, prints 56 lines per page in Portrait mode
  30. *       or 40 lines per page in Landscape mode (can be changed).
  31. * Strips all unprintable characters.
  32. *       Tabs are interpreted as five spaces.
  33. *       Formfeed creates a new page, but will not create
  34. *       a blank page.
  35. *
  36. * Sample call:      psd inputfile inputfile ...
  37. *
  38. * With parameters:   psd/T/L/F/C/N/nn inputfile ...
  39. *       where:
  40. *       /T indicates Times-Bold,
  41. *       /L indicates Landscape mode,
  42. *       /C is provided for users of Microsoft C;
  43. *               the .LST files are 61 lines per page and contain
  44. *               formfeeds.
  45. *       /F instructs the program to ignore any formfeeds encountered.
  46. *       /N specifies no page numbering; default is to page number.
  47. *
  48. *       /nn specifies the number of lines per page, e.g. 55,
  49. *       over-riding the defaults.  To format under control of
  50. *       formfeeds, specify a number longer than the longest page.
  51. *       No error checking is done and the program assumes all
  52. *       of the lines will fit on each page.  In portrait mode,
  53. *       only 60 lines fit, unless you specify the /C option,
  54. *       which changes the default top margin from 1.0 to .5 inches 
  55. *       but assumes portrait mode.
  56. *
  57. *       Parameters may be specified in any sequence; with
  58. *       or without an intervening / (slash), e.g.
  59. *         PSD/L35fn testfile         is the same as
  60. *         PSD/L/35/f/n testfile
  61. *       Any other characters are ignored.
  62. *
  63. * The PSD environment variable is used to specify the default output.
  64. * It can be a printer address or a file.  Usually it will be the address
  65. * of a port that is under control of SPOOL, e.g. PSD=LPT1.
  66. * If output is sent to a file that already exists, the output
  67. * will be appended.
  68. *
  69. *
  70. ***************************************************************************/
  71.  
  72. /* NOTE: adapted from LWP, which also provided direct async support, XON/XOFF
  73. control, etc.  The OS/2 SPOOL and MODE provide all of this.  However,
  74. SPOOL does not seem to pass through the EOD from the end-of-the file, so
  75. some PostScript printers may continue expecting input until they time out.
  76. */
  77.  
  78. #include <stdio.h>
  79. #include <string.h>
  80. #include <ctype.h>
  81. #include <stdlib.h>
  82. #include <signal.h>
  83. #include <time.h>
  84.  
  85. extern  int main(int argc,char * *argv);
  86. extern  int newpage(void );
  87. extern  int openline(int k);
  88. extern  int pagenum(void );
  89. extern  int instruct(void );
  90.  
  91. #define xputs(s) fputs(s,fp2)
  92. #define xputc(c) fputc(c,fp2)
  93.  
  94.                                 /* external variables */
  95. FILE *fp1,*fp2;
  96. char timestring[25]="\0";
  97. int pageno=1,page=0;
  98. char *infile;
  99.  
  100. main (argc,argv)                /* main starts here */
  101. int argc;
  102. char *argv[];
  103.  
  104. {                                  /* define some variables */
  105. int ch,k=0;
  106. int j=1,formfeed=1;
  107. int lines=55,i=0;
  108. char *size="10";
  109. char *s;
  110. char *font="Courier   ";
  111. char *start="36 720 translate            \n";
  112. char *environ;
  113. long ltime;
  114. struct tm *newtime;
  115.  
  116. /*********** start executing here ***********/
  117.  
  118. time (<ime);                  /* set date, time string for page numbering */
  119. newtime=localtime(<ime);
  120. strncpy(timestring,asctime(newtime),24);
  121.  
  122. if (argc < 2)                /* check for existence of parm list */
  123.         {
  124.        instruct();
  125.        exit(0);
  126.         }
  127. environ=getenv("PSD");
  128. if (environ == NULL)
  129.      {
  130.      environ=getenv("LWP");   /* for compatibility with previous version */
  131.      if (environ == NULL)
  132.           {
  133.           printf("Environment variable PSD not found. Aborting.\n");
  134.           exit(1);
  135.           }
  136.      }
  137.  
  138. if (strpbrk(argv[j],"/") != NULL)        /* Is first parm a modifier? */
  139.         {
  140.         if (strpbrk(argv[j],"Tt")!=NULL)     /* Does it contain a 'T' */
  141.                 {
  142.                 strcpy(font,"Times-Bold");      /* Yes, font is Times-Bold */
  143.                 strcpy(size," 8");
  144.                 }
  145.         if (strpbrk(argv[j],"Ll")!=NULL)     /* Does it contain an 'L' */
  146.                 {                           /* Yes, mode is Landscape */
  147.                 lines=39;
  148.                 strcpy(start,"540 756 translate -90 rotate\n");
  149.                 }
  150.         if (strpbrk(argv[j],"Cc")!=NULL)      /* Does it contain a 'C' */
  151.                 {                           /* C.LST file, use 61 lines */
  152.                 lines=61;
  153.                 strcpy(start,"36 756 translate            \n");
  154.                 }
  155.         if (strpbrk(argv[j],"Ff")!=NULL)    /* F:ignore formfeeds? */
  156.                 formfeed=0;
  157.         if (strpbrk(argv[j],"Nn")!=NULL)    /* N:don't page number */
  158.                 pageno=0;
  159.         s=strpbrk(argv[j],"0123456789");    /* ##:lines per page */
  160.         if (s!=NULL)
  161.                 lines=atoi(s);         /* note:no error checking! */
  162.         ++j;
  163.         }
  164.  
  165. /* loop through specified input files */
  166. while (strlen(argv[j]) > 0)
  167. {
  168.  
  169. if ((fp1=fopen(argv[j],"r"))==NULL)
  170.         {printf("Unable to open input %s\n",argv[j++]); continue;}
  171. else
  172.     infile=argv[j];            /* input file name */
  173.     while (i<strlen(infile))        /* make it uppercase for printing */
  174.         {
  175.         infile[i]=toupper(infile[i]);
  176.         ++i;
  177.         }
  178.     i=0;            /* reset i to use again */
  179.     
  180. if ((fp2=fopen(environ,"a"))==NULL)
  181.       {printf("Unable to open output %s\n",environ);exit(1);}
  182.  
  183. /* set up PostScript definitions, etc. */
  184.  
  185. xputs("/ss{0 yline moveto show\n");         /* define show line */
  186. xputs("/yline yline 12 sub def}def\n");     /* define line spacing */
  187. xputs("/");                                 /* define font and size */
  188. xputs(font);
  189. xputs(" findfont ");
  190. xputs(size);
  191. xputs(" scalefont setfont\n");
  192. xputs("/yline 0 def\n");                    
  193. xputs(start);                               /* move to top of page */
  194. xputs("0 0 moveto\n");
  195. xputs("/savename save def\n");              /* save status */
  196.  
  197. /* start processing input characters */
  198.  
  199. while ((ch=fgetc(fp1))!=EOF)
  200. {
  201.         switch(ch)
  202.         {
  203.         case'\n':                           /* end-of-line */
  204.                 {
  205.                         if (k)              /* if line open, close it */
  206.                                 {xputs(")ss\n");k=0;}
  207.                         else
  208.                                 xputs("()ss\n");     /* put blank line */
  209.                         if(++i>lines)           /* if endpage, show it */
  210.                                 {
  211.                                 newpage();
  212.                                 i=0;
  213.                                 }
  214.                         break;
  215.                 }
  216.         case'\050':                     /* left paren */
  217.                 k=openline(k);
  218.                 xputs("\\(");
  219.                 break;
  220.         case'\051':                     /* right paren */
  221.                 k=openline(k);
  222.                 xputs("\\)");
  223.                 break;
  224.         case'\t':                       /* tab */
  225.                 k=openline(k);
  226.                 xputs("     ");        /* five spaces */
  227.                 break;
  228.         case'\\':                       /* back slash */
  229.                 k=openline(k);
  230.                 xputs("\\\\");
  231.                 break;
  232.         case'\014':                     /* formfeed */
  233.                 if (k)                    /* close open line */
  234.                         {xputs(")ss\n");k=0;++i;}
  235.                 if (i && formfeed)            
  236.                         {                /* if page open, print it */
  237.                         newpage();
  238.                         i=0;
  239.                         }
  240.                 break;
  241.         default:                        /* all other characters */
  242.                 if (isprint(ch))        /* is it printable? */
  243. /* may want to delete above test to let some special characters through */
  244.                 {
  245.                 k=openline(k);            /* open a line if necessary */
  246.                 xputc(ch);                  /* put the char */
  247.                 }
  248.                 break;
  249.         }
  250. }
  251.  
  252. /* EOF: close up any open lines, pages */
  253. if (k)                          /* if line open, close it */
  254.         {xputs(")ss\n");++i;}
  255. if (i)                          /* if page open, print it */
  256.     {
  257.     pagenum();
  258.     xputs("showpage\n");        /* and clean up everything on printer */
  259.     }
  260. else xputs("savename restore\n");        /* otherwise, restore printer */
  261.  
  262. xputs("\004");                      /* end-of-file for insurance */
  263.  
  264. fclose(fp1);                            /* clean up and go home */
  265. fclose(fp2);
  266.     
  267.     
  268. printf("File: %s %u pages on %s\n",argv[j],page,environ);
  269. j++;
  270. page=0;
  271. }
  272.  
  273. return(0);
  274. }
  275.  
  276. /**************** FUNCTION DEFINITIONS ************************************/
  277.  
  278. int newpage()              /* close page and print it */
  279.         {
  280.         pagenum();        /* add page numbering */
  281.         xputs("copypage erasepage\n");
  282.         xputs("savename restore\n/savename save def\n");
  283.         return(0);
  284.         }
  285.  
  286. int openline(k)        /* if line not open, open it */
  287. int k;
  288.         {
  289.         if (!k)
  290.              (xputs("("));
  291.         return(1);
  292.         }
  293.  
  294. int pagenum()
  295.         {
  296.         char pageline[80];
  297.         ++page;
  298.         if (pageno) 
  299.                 xputs("-90 rotate 200 -15 moveto\n");
  300.                 sprintf(pageline,"(%s, Page %u, %s)show\n",infile,page,timestring);
  301.                 xputs(pageline);
  302.         return(0);
  303.         }
  304.  
  305. int instruct()
  306.     {
  307.      printf(" Sample call:      PSD inputfile inputfile ...\n");
  308.      printf(" With parameters:  PSD/T/L/F/C/N/nn inputfile ...\n");
  309.      printf(" (wildcards are supported for file names)\n");
  310.     printf("       where:\n");
  311.     printf("       /T indicates Times-Bold 8 pt.; default is Courier 10 pt.\n");
  312.     printf("       /L indicates Landscape mode; default is Portrait.,\n");
  313.      printf("       /C is provided for users of Microsoft C;\n");
  314.     printf("               the .LST files are 61 lines per page and contain\n");
  315.     printf("               formfeeds.  It also reduces the top margin to 1/2 inch.\n");
  316.     printf("       /F instructs the program to ignore any formfeeds encountered.\n");
  317.     printf("       /N specifies no page numbering; default is to page number.\n");
  318.     printf("       /nn specifies the number of lines per page, e.g. 55,\n");
  319.     printf("       over-riding the defaults of 40 (Landscape) and 56 (Portrait).\n");  
  320.     printf("            To format under control of formfeeds in the text,\n");
  321.     printf("            specify a number longer than the longest page.\n");
  322.     printf("       Parameters may be specified in any sequence; with\n");
  323.     printf("       or without an intervening / (slash). Other characters are ignored.\n");
  324.      printf("       The environment variable PSD= must specify the \n");
  325.      printf("       output device or file, usually a port controlled by SPOOL,\n");
  326.      printf("       e.g. PSD=LPT1.\n");
  327.      return(0);
  328.     }
  329. /************************* END OF PROGRAM LWP *********************************/
  330.