home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / list / pr2-pr3.lbr / PR3.CZ / PR3.C
Text File  |  1988-02-16  |  6KB  |  330 lines

  1. /*    PR.C  ---  a file print utility for Epson and Gemini printers    */
  2.  
  3. #include "stdio.h"
  4.  
  5. #define WYSE_50        /* comment out if WYSE-50 isn't being used */
  6.  
  7. #define EPSON        /* comment out if Epson printer isn't being
  8.                used, Gemini printer will then be presumed */
  9.  
  10. #define LF 10
  11. #define ESC 27
  12. #define LINEMAX 60
  13. #define MAXCOL 105
  14. #define TAB_SIZE 8
  15. #define MAXLINE 255
  16.  
  17. #ifdef WYSE_50
  18. #define HM_WIDTH 46
  19. #endif
  20.  
  21. #define PRN_NS "\033O"
  22. #define PRN_RP "\033@"
  23. #define PRN_S5 "\033A\005"
  24. #define PRN_S8 "\0330"
  25. #define PRN_SS "\033S\001"
  26. #define PRN_LSG "\033K\340\001"
  27.  
  28. #ifdef EPSON
  29. #define PRN_CS 15
  30. #define PRN_S21 "\033J\025"
  31. #else
  32. #define PRN_CS "\033B\003"    /* ctrl-O reportedly doesn't always work on Gemini */
  33. #define PRN_S21 "\033J\016"    /* space 14/144 instead of 21/216 */
  34. #endif
  35.  
  36. #define strchr index        /* the more 'modern' name of the function */
  37.  
  38. typedef int BOOLEAN;
  39.  
  40. extern char chset[96][8];
  41.  
  42. #ifdef WYSE_50
  43. char message[40];
  44. #endif
  45.  
  46. unsigned pg_num = 1;
  47. BOOLEAN cflag = FALSE, lflag = FALSE, nflag = FALSE;
  48.  
  49. main(argc, argv)
  50. int argc;
  51. char *argv[];
  52. {
  53.     FILE *fp, *fopen();
  54.     char *fname, *index();
  55.     int s, i, pg_len = 80;
  56.  
  57.     if (argc == 1 || (argc == 2 && *argv[1] == '-') || argc > 3)
  58.         pr_usage();
  59.     if (*argv[1] == '-')
  60.     {
  61.         for (i = 1; s = argv[1][i]; i++)
  62.         {
  63.             switch (s)
  64.             {
  65.                 case 'C':
  66.                     cflag = TRUE;
  67.                     pg_len = 144;
  68.                     break;
  69.                 case 'L':
  70.                     lflag = TRUE;
  71.                     break;
  72.                 case 'N':
  73.                     nflag = TRUE;
  74.                     break;
  75.                 default:
  76.                     pr_usage();
  77.                     break;
  78.             }
  79.         }
  80.     }
  81.     --argc;
  82.     if ((fp = fopen(argv[argc], "r")) == NULL)
  83.         exit(fprintf(stderr, "\tError:  unable to open %s\n", argv[argc]));
  84.     if ((fname = strchr(argv[argc], ':')) == NULL)
  85.         fname = argv[argc];
  86.     else
  87.         fname++;
  88.     if (lflag)
  89.         landscape(fname, fp);
  90.     else
  91.         pr_text(fname, pg_len, fp);
  92.     fclose(fp);
  93. #ifdef WYSE_50
  94.     hostmsg(" ");
  95. #endif
  96.     exit();
  97. }
  98.  
  99. void pr_usage()
  100. {
  101.     exit(fprintf(stderr, "\tUsage:  pr [-cln] [d:]filename.ext\n"));
  102. }
  103.  
  104. void pr_text(filename, pg_len, fp)
  105. char *filename;
  106. int pg_len;
  107. FILE *fp;
  108. {
  109.     char line[MAXLINE], raw_line[MAXLINE];
  110.     int position, ln_cnt = 0, ln_num = 0;
  111.  
  112. #ifdef EPSON
  113.     fprintf(stdlst, "%s%s%c", PRN_RP, PRN_NS, PRN_CS);
  114. #else
  115.     fprintf(stdlst, "%s%s%s", PRN_RP, PRN_NS, PRN_CS);
  116. #endif
  117.     if (cflag)
  118.         fprintf(stdlst, "%s%s", PRN_SS, PRN_S5);
  119.     else
  120.         fprintf(stdlst, "%s", PRN_S8);
  121.     pr_title(filename);
  122.     while (fgets(raw_line, MAXLINE, fp))
  123.     {
  124.         if (++ln_cnt > pg_len)
  125.         {
  126.             pr_title(filename, ++pg_num);
  127.             ln_cnt = 1;
  128.         }
  129.         position = left_margin();
  130.         if (nflag)
  131.         {
  132.             fprintf(stdlst, "%05u:  ", ++ln_num);
  133.             position += 8;
  134.         }
  135.         translate(raw_line, line, position);
  136.         fprintf(stdlst, "%s", line);
  137.     }
  138.     fprintf(stdlst, "%c%s", '\f', PRN_RP);
  139. }
  140.  
  141. void pr_title(filename)
  142. char *filename;
  143. {
  144.     int num_spaces, width = 112, position;
  145.  
  146.     num_spaces = width - strlen(filename);
  147.     if (pg_num != 1)
  148.         fprintf(stdlst, "%c", '\f');
  149. #ifdef WYSE_50
  150.     sprintf(message, "Printing Page %03u of %s", pg_num, filename);
  151.     hostmsg(message);
  152. #endif
  153.     position = left_margin();
  154.     fprintf(stdlst, "%s", filename);
  155.     while (num_spaces--)
  156.         fprintf(stdlst, "%c", ' ');
  157.     fprintf(stdlst, "Page %03u\n\n\n", pg_num);
  158. }
  159.  
  160. void translate(source, destination, position)
  161. char *source, *destination;
  162. int position;
  163. {
  164.     int num_spaces, ch;
  165.  
  166.     while (*source)
  167.     {
  168.         ch = *source++ & 0x7F;
  169.         switch (ch)
  170.         {
  171.             case '\t':
  172.                 num_spaces = TAB_SIZE - (position % TAB_SIZE);
  173.                 position += num_spaces;
  174.                 while (num_spaces--)
  175.                     *destination++ = ' ';
  176.                 break;
  177.             default:
  178.                 if (!special(ch))
  179.                 {
  180.                     *destination++ = ch;
  181.                     position++;
  182.                 }
  183.                 break;
  184.         }
  185.     }
  186.     *destination = '\0';
  187. }
  188.  
  189. int left_margin()
  190. {
  191.     int num_spaces = TAB_SIZE;
  192.  
  193.     while (num_spaces--)
  194.         fprintf(stdlst, "%c", ' ');
  195.     return (TAB_SIZE);
  196. }
  197.  
  198. BOOLEAN special(ch)
  199. char ch;
  200. {
  201.     if (ch == '\t' || ch == '\r' || ch == LF)
  202.         return (FALSE);
  203.     else
  204.         return (iscntrl(ch));
  205. }
  206.  
  207. #ifdef WYSE_50
  208. void hostmsg(string)
  209. char *string;
  210. {
  211.     int blanks;
  212.  
  213.     blanks = HM_WIDTH - strlen(string);
  214.     fprintf(stderr, "%cF%s", ESC, string);
  215.     while (--blanks)
  216.         fprintf(stderr, "%c", ' ');
  217.     fprintf(stderr, "%c", '\r');
  218. }
  219. #endif
  220.  
  221. landscape(filename, fp)
  222. char *filename;
  223. FILE *fp;
  224. {
  225.     int columns;
  226.     char pagebuf[LINEMAX][MAXCOL];
  227.  
  228.     fprintf(stdlst, "%s%s", PRN_RP, PRN_NS);
  229.     do
  230.     {
  231.         columns = getpage(fp, pagebuf);
  232.         fillpage(pagebuf, columns);
  233.         printpage(filename, pagebuf, columns);
  234.     } while (!feof(fp));
  235.     fprintf(stdlst, "%s", PRN_RP);
  236. }
  237.  
  238. int getpage(fp, pagebuf)
  239. FILE *fp;
  240. char pagebuf[LINEMAX][MAXCOL];
  241. {
  242.     register char *chptr;
  243.     register int i = 0, j = 0;
  244.     int maxcols = 0, character;
  245.  
  246.     chptr = &pagebuf[i][0];
  247.     do
  248.     {
  249.         character = getc(fp);
  250.         if (character == '\n' || character == EOF || character == '\f')
  251.         {
  252.             *chptr = '\0';
  253.             if (j > maxcols)
  254.                 maxcols = j;
  255.             j = 0;
  256.             chptr = &pagebuf[++i][0];
  257.             if (character != '\n')
  258.                 break;
  259.         }
  260.         else if (character == '\t')
  261.             while (j < MAXCOL)
  262.             {
  263.                 *chptr++ = ' ';
  264.                 j++;
  265.                 if ((j % 8) == 0)
  266.                     break;
  267.             }
  268.         else
  269.         {
  270.             *chptr++ = character;
  271.             j++;
  272.         }
  273.         if (!(j < MAXCOL))
  274.         {
  275.             j--;
  276.             *--chptr = '\0';
  277.             if (j > maxcols)
  278.                 maxcols = j;
  279.             j = 0;
  280.             chptr = &pagebuf[++i][0];
  281.         }
  282.     } while (i < LINEMAX);
  283.     while (i < LINEMAX)
  284.         pagebuf[i++][0] = '\0';
  285.     return (maxcols);
  286. }
  287.  
  288. void fillpage(pagebuf, cols)
  289. char pagebuf[LINEMAX][MAXCOL];
  290. int cols;
  291. {
  292.     int len;
  293.     register int i, j;
  294.     register char *chptr;
  295.  
  296.     for (i = 0; i < LINEMAX; i++)
  297.     {
  298.         len = strlen(pagebuf[i]);
  299.         for (j = len, chptr = &pagebuf[i][len]; j < cols; j++)
  300.             *chptr++ = ' ';
  301.     }
  302. }
  303.  
  304. void printpage(filename, pagebuf, cols)
  305. char *filename, pagebuf[LINEMAX][MAXCOL];
  306. int cols;
  307. {
  308.     char outchar;
  309.     register int i, j, k;
  310.     register char *chset_ptr;
  311.  
  312. #ifdef WYSE_50
  313.     sprintf(message, "Printing Page %03u of %s", pg_num++, filename);
  314.     hostmsg(message);
  315. #endif
  316.     for (j = 0; j < cols; j++)
  317.     {
  318.         fprintf(stdlst, "%s", PRN_LSG);
  319.         for (i = LINEMAX - 1; i >= 0; i--)
  320.         {
  321.             outchar = pagebuf[i][j];
  322.             chset_ptr = &chset[outchar - ' '][0];
  323.             for (k = 0; k < 8; k++)
  324.                 putc(*chset_ptr++, stdlst);
  325.         }
  326.         fprintf(stdlst, "%s%c", PRN_S21, '\r');
  327.     }
  328.     fprintf(stdlst, "%c", '\f');
  329. }
  330.