home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume6 / lj_filter / laser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  9.8 KB  |  453 lines

  1. /*
  2.  *            L A S E R . C 
  3.  *
  4.  * $Revision: 1.5 $
  5.  *
  6.  * $Log:    laser.c,v $
  7.  * Revision 1.5  86/07/14  17:51:09  ron
  8.  * Prepared for distribution
  9.  * 
  10.  * Revision 1.4  86/07/14  16:34:19  ron
  11.  * Cleaned up code, generally made more human readable.
  12.  * 
  13.  * Revision 1.3  86/07/14  15:05:31  ron
  14.  * Basically re-wrote the whole damned thing to support all, well most
  15.  * of the LaserJet features that are available.  Instead of simply supporting
  16.  * those that were originally used.  The program now supports all of the
  17.  * HP font cartridges with most of the attributes available...
  18.  * 
  19.  * Revision 1.2  86/07/14  09:46:30  ron
  20.  * Added distinctive RCS header
  21.  * 
  22.  */
  23. #ifndef lint
  24. static char RCSid[] = "@(#)$Header: laser.c,v 1.5 86/07/14 17:51:09 ron Exp $";
  25. #endif
  26.  
  27. #include <stdio.h>
  28.  
  29.  
  30. #define    LPRCMD "/usr/bin/lp 2>/dev/null 2>&1"
  31.  
  32. /*  If your system can handle it... */
  33. /* #define BIGHELPTEXT        /* ... mine can't */
  34.  
  35. #ifndef    BIGHELPTEXT
  36. #define HELP() fprintf (stderr, "Please read the manpage.\n");
  37. #define USAGE() fprintf (stderr, "Please read the manpage.\n");
  38.  
  39. #else
  40. #define USAGE() fprintf (stderr, \
  41.     ">>>> Type 'laser -H' to display a help screen.\n")
  42. #define HELP()    fprintf (stderr, \
  43. "Laser Command Options:\n\
  44.   c    --    Character Set\n\
  45.     (1)  Roman-8        (2)  USASCII  (3)  Roman Ext. (4)  Danish/Norwegian\n\
  46.     (5)  United Kingdom (6)  French   (7)  German     (8)  Italian\n\
  47.     (9)  Swedish       (10)  Spanish (11)  Legal     (12)  Linedraw\n\
  48.    (13)  Math8         (14)  Math8a  (15)  Math8b    (16)  Math7\n\
  49.    (17)  PiFont        (18)  PiFonta\n\
  50.   o    --    Orientation (Portrait or Landscape, P or L)\n\
  51.   t    --    Typeface\n\
  52.     (1)  Courier        (2)  Line Printer   (3)  Helvetica\n\
  53.     (4)  Times Roman    (5)  Prestige Elite (6)  Gothic\n\
  54.   h    --    Point Size (character height)\n\
  55.     (1)  7 Pt. (2)  8 Pt. (3)  8.5 Pt. (4)  10 Pt. (5)  12 Pt. (6)  14.4 Pt.\n\
  56.   p    --    Pitch\n\
  57.     (1)  10 cpi (2)  12 cpi (3)  16.66 cpi\n\
  58.   s    --    Spacing (Proportional or Fixed, P or F)\n\
  59.   l    --    Lines per inch (1, 2, 3, 4, 6, 8, 12, 16, 24)\n\
  60.   i    --    Italic\n\
  61.   b    --    Bold\n\
  62.   O    --    Offset from left edge (number of spaces)\n\
  63.   P    --    Page Step (number of extra lines)\n\
  64.   F    --    Forms Length (number of lines in form)\n\
  65.   w    --    Send results to standard output\n")
  66. #endif
  67.  
  68. static char *Symbols[] = 
  69. {
  70.     "\033(8U",     /* Roman-8 */
  71.     "\033(0U",    /* USASCII */
  72.         "\033(0E",    /* Roman Ext. */
  73.     "\033(0D",    /* Danish/Norwegian */
  74.     "\033(1E",    /* United Kingdom */
  75.     "\033(0F",    /* French */
  76.     "\033(0G",    /* German */
  77.     "\033(0I",    /* Italian */
  78.     "\033(0S",    /* Swedish/Finnish */
  79.     "\033(1S",    /* Spanish */
  80.     "\033(1U",    /* Legal */
  81.     "\033(0B",    /* Line Draw */
  82.     "\033(8M",    /* Math8 */
  83.     "\033(0Q",    /* Math8a */
  84.     "\033(1Q",    /* Math8b */
  85.     "\033(0M",    /* Math7 */
  86.     "\033(15U",    /* PiFont */
  87.     "\033(2Q",    /* PiFonta */
  88.     0
  89. };
  90.  
  91. static char *Spacing[] = 
  92. {
  93.     "\033(s1P",    /* Proportional */
  94.     "\033(s0P",    /* Fixed */
  95.     0
  96. };
  97.  
  98. static char *Type[] =
  99. {            /* Yes I know their not in order... */
  100.     "\033(s3T",    /* Courier (default) */
  101.     "\033(s0T",    /* Line Printer */
  102.     "\033(s4T",    /* Helvetica */
  103.     "\033(s5T",     /* Times Roman */
  104.     "\033(s8T",     /* Prestige Elite */
  105.     "\033(s6T",     /* Gothic */
  106.     "\033(s1T",    /* Pica */
  107.     "\033(s7T",    /* Script */
  108.     "\033(s9T",    /* Caslon */
  109.     "\033(s10T",    /* Orator */
  110.     0
  111. };
  112.  
  113. static char *Orientation[] = 
  114. {
  115.     "\033&l0O",     /* Portrait */
  116.     "\033&l1O",    /* Landscape */
  117.     0
  118. };
  119.  
  120. static char *Point[] = 
  121. {
  122.     "\033(s7V",    /* 7 Pt. */
  123.     "\033(s8V",    /* 8 Pt. */
  124.     "\033(s8.5V",    /* 8.5 Pt. */
  125.     "\033(s10V",     /* 10 Pt. */
  126.     "\033(s12V",    /* 12 Pt. */
  127.     "\033(s14.4V",    /* 14.4 Pt */
  128.     0
  129. };
  130.  
  131. static char *Pitch[] =
  132. {
  133.     "\033(s10H",    /* 10 CPI */
  134.     "\033(s12H",    /* 12 CPI */
  135.     "\033(s16.6H",    /* 16.66 CPI */
  136.     "",        /* no pitch for proportional spacing */
  137.     0
  138. };
  139.  
  140. static char *Lpi[] = 
  141. {
  142.     "\033&l7.27c66F", /* 66 lines per page with 1/2 at top and bot */
  143.     "\033&l1d66F",    /* 1 lpi */
  144.     "\033&l2d66F",    /* 2 lpi */
  145.     "\033&l3d66F",    /* 3 lpi */
  146.     "\033&l4d66F",    /* 4 lpi */
  147.     "\033&l6d66F",    /* 6 lpi */
  148.     "\033&l8d66F",    /* 8 lpi */
  149.     "\033&l12d66F",   /* 12 lpi */
  150.     "\033&l16d66F",   /* 16 lpi */
  151.     "\033&l24d66F",   /* 24 lpi */
  152.     0
  153. };
  154.  
  155. static char *Italic = "\033(s1S";
  156.  
  157. static char *Bold   = "\033(s3B";
  158.  
  159. #define        CTRL(c)        ('c' & 037)
  160.  
  161. /*
  162.  *  Defaults
  163.  */
  164.  
  165. int    sym = 1;
  166. int    type = 0;
  167. int    spc = 1;
  168. int    orient = 0;
  169. int    ps = 3;
  170. int    ptch = 0;
  171. int    lpi = 0;
  172. int    offset = 0;
  173. int    stdoutput = 0;
  174. int    pagestep = 8;
  175. int    forms = 66;
  176. int    italic = 0;
  177. int    bold = 0;
  178. int    Debug = 0;
  179.  
  180. main (argc, argv)
  181.     int argc;
  182.     char *argv[];
  183. {
  184.     FILE           *fp, *fopen();
  185.     extern char    *optarg;
  186.     extern int      optind;
  187.     int             i, c, errflg = 0;
  188.  
  189.     while ((c = getopt(argc, argv, "c:o:t:h:l:s:p:O:P:F:biHwd")) != EOF) {
  190.         switch (c) {
  191.         case 'd':
  192.             ++Debug;
  193.             break;
  194.  
  195.         case 'l':
  196.             lpi = atoi(optarg);
  197.             switch (lpi) {
  198.             case 1: case 2: case 3: case 4: case 6:
  199.             case 8: case 12: case 16: case 24:
  200.                 if (Debug)
  201.                     fprintf(stderr, "lpi = %d, ", lpi);
  202.                 break;
  203.             default:
  204.                 printf("Lines Per Inch values must be one of 1, 2, 3, 4, 6, 8, 12, 16, or 24.\n");
  205.                 USAGE();
  206.                 exit(1);
  207.             }
  208.             break;
  209.         case 'o':
  210.             if (*optarg == 'p' || *optarg == 'P')
  211.                 orient = 0;
  212.             else if (*optarg == 'l' || *optarg == 'L')
  213.                 orient = 1;
  214.             else {
  215.                 printf("Orientation must be specified as either 'P' or 'L'.\n");
  216.                 USAGE();
  217.                 exit(1);
  218.             }
  219.             if (Debug)
  220.                 fprintf (stderr,"orientation -> %c, ", *optarg);
  221.             break;
  222.  
  223.         case 't':
  224.             type = atoi(optarg);
  225.             --type;
  226.             if (type < 0 || type > 10) {
  227.                 printf("Typeface selection must be in the range of 1 (one) to 6 (six).\n");
  228.                 USAGE();
  229.                 exit(1);
  230.             }
  231.             /* Set up some defaults in case none of these are
  232.                chosen explicitly */
  233.             switch (type) {
  234.             case 0:
  235.                 sym = orient = ptch = 0; spc = 1; ps = 4;
  236.                 break;
  237.             case 1:
  238.                 sym = spc = 1; orient = 0; ps = ptch = 2;
  239.                 break;
  240.             case 2:
  241.                 sym = spc = 0; orient = 1; ps = ptch = 3;
  242.                 break;
  243.             case 3:
  244.                 sym = spc = orient = 0; ps = 4; ptch = 1;
  245.                 break;
  246.             case 4:
  247.                 sym = orient = 0; spc = 1; ps = 3; ptch = 1;
  248.                 break;
  249.             case 5:
  250.                 sym = orient = 0; spc = 1; ps = 4; ptch = 1;
  251.                 break;
  252.             case 6:
  253.                 sym = orient = 0; spc = ptch = 1; ps = 4;
  254.                 break;
  255.             }
  256.             if (Debug)
  257.                 fprintf (stderr, "typeface = %d, ", type);
  258.             break;
  259.  
  260.         case 's':
  261.             if (*optarg == 'P' || *optarg == 'p') {
  262.                 spc = 0;
  263.                 ptch = 3;
  264.             }
  265.             else if (*optarg == 'F' || *optarg == 'f')
  266.                 spc = 1;
  267.             else {
  268.                 printf("Spacing must be specified as either 'P' or 'F'\n");
  269.                 USAGE();
  270.                 exit(1);
  271.             }
  272.             if (Debug)
  273.                 fprintf (stderr, "spacing -> %c, ", *optarg);
  274.             break;
  275.  
  276.         case 'h':
  277.             ps = atoi(optarg);
  278.             --ps;
  279.             if (ps < 0 || ps > 6) {
  280.                 printf("Point size must be specified within the range of 1 (one) to 6 (six)\n");
  281.                 USAGE();
  282.                 exit(1);
  283.             }
  284.             if (Debug)
  285.                 fprintf (stderr, "point size = %d, ", ps);
  286.             break;
  287.  
  288.         case 'p':
  289.             ptch = atoi(optarg);
  290.             --ptch;
  291.             if (ptch < 0 || ptch > 3) {
  292.                 printf("Pitch must be specified within the range of 1 (one) to 6 (six).\n");
  293.                 USAGE();
  294.                 exit(1);
  295.             }
  296.             if (Debug)
  297.                 fprintf (stderr, "pitch = %d, ", ptch);
  298.             break;
  299.  
  300.         case 'c':    /* Symbol Set selection */
  301.             sym = atoi(optarg);
  302.             --sym;
  303.             if (sym < 0 || sym > 18) {
  304.                 printf("Character set selection must be between the range of 1 (one) and 18 (eighteen).\n");
  305.                 USAGE();
  306.                 exit(1);
  307.             }
  308.             if (Debug)
  309.                 fprintf(stderr, "symbol set = %d, ", sym);
  310.             break;
  311.  
  312.         case 'O':    /* Offset from left margin */
  313.             offset = atoi(optarg);
  314.             if (offset < 0 || offset > 80) {
  315.                 printf("Offset value must be specified between the range of 0 (zero) and 80 (eighty).\n");
  316.                 USAGE();
  317.                 exit(1);
  318.             }
  319.             if (Debug)
  320.                 fprintf(stderr, "offset = %d, ", offset);
  321.             break;
  322.  
  323.         case 'P':
  324.             pagestep = atoi(optarg);
  325.             if (pagestep < 0 || pagestep > 66) {
  326.                 printf("Page Step value must be specified between the range of 1 (one) and 66 (sixty six).\n");
  327.                 USAGE();
  328.                 exit(1);
  329.             }
  330.             if (Debug)
  331.                 fprintf (stderr, "page step = %d, ", pagestep);
  332.             break;
  333.  
  334.         case 'F':
  335.             forms = atoi(optarg);
  336.             if (forms < 0) {
  337.                 printf("Forms value must be greater than 0 (zero).\n");
  338.                 USAGE();
  339.                 exit(1);
  340.             }
  341.             if (Debug)
  342.                 fprintf (stderr, "forms = %d, ", forms);
  343.             break;
  344.  
  345.         case 'i':
  346.             italic++;
  347.             if (Debug)
  348.                 fprintf (stderr, "italics set, ");
  349.             break;
  350.  
  351.         case 'b':
  352.             bold++;
  353.             if (Debug)
  354.                 fprintf (stderr, "bold set, ");
  355.             break;
  356.  
  357.         case 'w':    /* Standard Output */
  358.             if (Debug)
  359.                 fprintf (stderr, "results to stdout, ");
  360.             stdoutput++;
  361.             break;
  362.  
  363.         case 'H':
  364.             HELP();
  365.             exit (1);
  366.  
  367.         default:
  368.             errflg++;
  369.             break;
  370.         }
  371.     }
  372.  
  373.     if (errflg) {
  374.         fprintf (stderr, "laser [ - {options} ] [ files ] ... \n");
  375.         USAGE();
  376.         exit(1);
  377.     }
  378.  
  379.     if (Debug) fprintf (stderr, "\b \n");
  380.  
  381.     if (argc == optind)
  382.         sendjob(stdin);
  383.     else
  384.         for (; optind < argc; optind++)
  385.             if ((fp = fopen(argv[optind], "r")) == NULL) {
  386.                 fprintf(stderr, "laser: can't open %s\n", argv[optind]);
  387.                 exit(1);
  388.             } else {
  389.                 sendjob(fp);
  390.                 fclose(fp);
  391.             }
  392.     exit(0);
  393. }
  394.  
  395. sendjob (fp)
  396.     FILE *fp;
  397. {
  398.     FILE    *pp, *popen();
  399.     int pg = pagestep, c, lines;
  400.  
  401.     if (! stdoutput) {
  402.         if ((pp = popen (LPRCMD, "w")) == NULL) {
  403.         fprintf(stderr,"laser: cannot pipe to line printer device.\n");
  404.         exit (1);
  405.         }
  406.     } else 
  407.         pp = stdout;
  408.  
  409.             /* reset        perf skip ON */
  410.     fprintf (pp, "\033E%s%s%s%s%s%s%s\033&l1L\r",
  411.         Symbols[sym],
  412.         Spacing[spc], 
  413.         Orientation[orient],
  414.         Type[type],
  415.         Point[ps],
  416.         Lpi[lpi], 
  417.         Pitch[ptch]);
  418.  
  419.     if (forms != 66)
  420.         fprintf (pp, "\033&l%dF\r", forms);
  421.  
  422.     if (bold)
  423.         fprintf (pp, "%s\r", Bold);
  424.  
  425.     if (italic)
  426.         fprintf (pp, "%s\r", Italic);
  427.  
  428.     if (offset)
  429.         fprintf (pp, "\033&a%dL\r", offset);
  430.  
  431.     while ((c = getc (fp)) != EOF) {
  432.         if (c == CTRL(l)) {
  433.             while (lines < forms) {
  434.                 putc ('\n', pp);
  435.                 lines++;
  436.                 }
  437.             continue;    /* eat the ^L char */
  438.         }
  439.         if (c == CTRL(j)) {
  440.             if (lines == forms) {
  441.                 while (pg-- > 0)
  442.                     putc ('\n', pp);
  443.                 lines = 0;
  444.                 continue;
  445.             } else
  446.                 lines++;
  447.         }
  448.         putc (c, pp);
  449.     }
  450.     fprintf (pp, "\033E"); /* reset the printer to default settings */
  451.     pclose (pp);
  452. }
  453.