home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 109_01 / zlpr.c < prev   
Text File  |  1985-03-10  |  3KB  |  164 lines

  1. /*
  2.     Line printer formatter 
  3.  
  4.     Written by Leor Zolman
  5.            May 28, 1980
  6.  
  7.     First prints all files named on the command line, and then
  8.     asks for names of more files to print until a null line is typed.
  9.     Control-Q aborts current printing and goes to next file.
  10.  
  11.     Paper should be positioned ready to print on the first page; each
  12.     file is always printed in an even number of pages so that new files
  13.     always start on the same phase of fan-fold paper.
  14.  
  15.     Tabs are expanded into spaces.
  16. */
  17.  
  18. #include "bdscio.h"
  19.  
  20. #define FF 0x0c        /* formfeed character, or zero if not supported */
  21. #define PGLEN 54    /* lines per lineprinter page */
  22. #define ON 1
  23. #define OFF 0
  24.  
  25. int pgflag;
  26. int pg;
  27. int count;
  28.  
  29. int colno, linesleft;
  30.  
  31. /*{} force form-feed */
  32.  
  33. main(argc,argv)
  34. char **argv;
  35. {
  36.     int i, pgno, fd;
  37.     char date[30], linebuf[135];    /* date and line buffers */
  38.     char fnbuf[30], *fname;        /* filename buffer & ptr */
  39.     char ibuf[BUFSIZ];        /* buffered input buffer */
  40.     char *gets();
  41.     char *pgstr;
  42.     char pgnmbuf[10];
  43.  
  44.     pg = count = 0;
  45.     pgflag= ON;
  46.     pgno = colno = 0;
  47.     linesleft = PGLEN; 
  48.     printf("What is today's date? ");
  49.       gets(date);
  50.  
  51.     while (1)
  52.     {
  53.         if (argc-1)
  54.          {
  55.             fname = *++argv;
  56.             argc--;
  57.          }
  58.         else
  59.          {
  60.             printf("\nEnter file to print, or CR if done: ");
  61.             if(!*(fname=gets(fnbuf))) break;
  62.             printf("\nEnter page to print, CR for all: ");
  63.             if (*(pgstr=gets(pgnmbuf))) {
  64.                 pg=atoi(pgstr);
  65.                 }
  66.             else pg=0;
  67.          }
  68.  
  69.         if ((fd = fopen(fname,ibuf)) == ERROR)
  70.          {
  71.             printf("Can't open %s\n",fname);
  72.             continue;
  73.          }
  74.         else printf("\nPrinting %-13s",fname);
  75.  
  76. /*}}{{ force form-feed */
  77.  
  78.  
  79.         for (pgno = 1; ; pgno++)
  80.          {
  81.             if (pg) {
  82.                 if (pg==pgno) {
  83.                     pgflag=ON;
  84.                     }
  85.                 else pgflag=OFF;
  86.                 }
  87.             else pgflag=ON;
  88.             putchar('*');
  89.             sprintf(linebuf,"\n%28s%-13s%5s%-3d%20s\n\n",
  90.                 "file: ",fname,"page ",pgno,date);
  91.             linepr(linebuf);
  92.  
  93.         loop:    if (!fgets(linebuf,ibuf)) break;
  94.             if (kbhit() && getchar() == 0x11) break;
  95.             if (linepr(linebuf)) continue;
  96.             if (linesleft > 2) goto loop;
  97.             formfeed();
  98.          }
  99.         formfeed();
  100.         if (pgno % 2) formfeed();
  101.         fabort(fd);
  102.     }
  103. }
  104.  
  105. /*
  106.     Print a line of text out on the list device, and
  107.     return true if a formfeed was encountered in the
  108.     text.
  109. */
  110.  
  111. linepr(string)
  112. char *string;
  113. {
  114.     char c, ffflag;
  115.     ffflag = 0;
  116.     while (c = *string++)
  117.       switch (c) {
  118.         case '{':
  119.         count+=1;
  120.         putlpr(c);
  121.         break;
  122.         case '}':
  123.         count-=1;
  124.         if (count==0) ffflag=1;
  125.         putlpr(c);
  126.         break;
  127.         case FF:
  128.         ffflag = 1;
  129.         break;
  130.         case '\n':    
  131.         putlpr('\r');
  132.         putlpr('\n');
  133.         colno = 0;
  134.         linesleft--;
  135.         break;
  136.  
  137.         case '\t':
  138.         do {
  139.           putlpr(' ');
  140.           colno++;
  141.         } while (colno % 8);
  142.         break;
  143.  
  144.         default:                    
  145.         putlpr(c);
  146.         colno++;
  147.     }
  148.     if (ffflag) formfeed();
  149.     return ffflag;
  150. }
  151.  
  152. putlpr(c)
  153. char c;
  154. {
  155.     if (pgflag) bios(5,c);
  156. }
  157.  
  158. formfeed()
  159. {
  160.     if (FF) putlpr(FF);
  161.     else while (linesleft--) putlpr('\n');
  162.     linesleft = PGLEN;
  163. }
  164.