home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / psroff / part04 / troff2.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-17  |  10.9 KB  |  473 lines

  1. /*    Copyright 1985, 1986, 1987, 1988 Chris Lewis
  2.         All Rights Reserved
  3.  
  4.     Permission to copy and further distribute is freely given provided 
  5.     this copyright notice remains intact and that this software is not 
  6.     sold for profit.
  7.  
  8.     Project:    Generic Troff drivers
  9.     Module:        troff2.c
  10.     Author:     Chris Lewis
  11.     Specs:        Troff interpreter, needs a driver to work.
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. #ifndef    SVR3
  17. #ifndef    lint
  18. static char SCCSid[] = "@(#)troff2.c: 1.16 Copyright 89/07/17 10:58:29 Chris Lewis";
  19. #endif
  20. #else
  21. #ident  "@(#)troff2.c: 1.16 Copyright 89/07/17 10:58:29 Chris Lewis"
  22. #endif
  23.  
  24. #define    ESC    0x80
  25. #define    FLASH    0x00
  26. #define    CONTROL    0x40
  27. #define    LEAD    0x60
  28. #define    SIZE    0x50
  29.  
  30. #define    DOWN    0
  31. #define    UP    1
  32. #define    LOWER    2
  33. #define    UPPER    3
  34. #define    FORWARD    4
  35. #define    BACKWARD 5
  36.  
  37. char ptab[15] = { 7, 8, 10, 11, 12, 14, 18, 9, 6, 16, 20, 22, 24, 28, 36};
  38. int    points;
  39.  
  40. long    xpos, ypos;
  41.  
  42. char *words[] = {
  43.     "down", "up", "lower", "upper", "forward", "backwards"
  44.     };
  45.  
  46.  
  47. #define    CTOINT(val,sig)    ((~c)&sig)
  48.  
  49. double pagelength = 11 * TROFFRESOLUTION;
  50.  
  51. #define    CFONT (((mag == UPPER)<<2)|((rail == UPPER)<<1)|(tilt==DOWN))
  52. #ifdef    FONT4
  53. #define    calcfont    font = (CFONT >> 1)
  54. #else
  55. #define    calcfont    font = CFONT
  56. #endif
  57.  
  58. char *progname;
  59. char *device = {"alw"};
  60.  
  61. double calc();
  62.  
  63. char *driver;
  64.  
  65. main(argc, argv)
  66. int    argc;
  67. char    **argv; {
  68.     register int nc, c;
  69.     register int i,j;
  70.     register int units;
  71.     register int font, rail, mag, tilt, half, escape, lead;
  72.  
  73.     extern int getopt();
  74.     extern char *optarg;
  75.  
  76.     progname = strrchr(argv[0], "/");
  77.     if (progname)
  78.         progname++;
  79.     else
  80.         progname = argv[0];
  81.     
  82.     if (strlen(progname) >= 2)
  83.         driver = progname + strlen(progname) - 2;
  84.     else
  85.         driver = "ZZ";
  86.  
  87.     while((c = getopt(argc, argv, "T:VDd:l:m")) != EOF)
  88.         switch (c) {
  89.         case 'T':
  90.             device = optarg;
  91.             break;
  92.         case 'm':
  93.             metrics = 1;
  94.             break;
  95.         case 'V':
  96.             printf("%s: version %s\n", progname, T2VERSION);
  97.             exit(0);
  98.         case 'D':
  99.             diagFile = fopen("diagnostics", "w");
  100.             if (!diagFile) {
  101.             fprintf(stderr, "%s: Could not open diagnostics file\n",
  102.                 progname);
  103.             exit(1);
  104.             }
  105.             break;
  106.         case 'd':
  107.             driver = optarg;
  108.             break;
  109.         case 'l':
  110.             pagelength = calc(optarg);
  111.             if (pagelength == 0) {
  112.             fprintf(stderr, "%s: Bad pagelength %s\n", progname, 
  113.                 optarg);
  114.             exit(1);
  115.             }
  116.             break;
  117.         case '?':
  118.         default:
  119.             usage();
  120.             exit(1);
  121.         }
  122.     
  123.     getdriver(driver);
  124.  
  125.     if (be->beprolog)
  126.         (*be->beprolog)();
  127.  
  128.     while (!feof(stdin)) {
  129.         c = getc(stdin);
  130.         switch(c) {
  131.         case 0x00:
  132.             DEBUGPRINTF("NOP\n");
  133.             break;
  134.         /* Flash (print character) codes */
  135.         case 0x01: case 0x02: case 0x03: case 0x04: 
  136.         case 0x05: case 0x06: case 0x07: case 0x08: 
  137.         case 0x09: case 0x0a: case 0x0b: case 0x0c: 
  138.         case 0x0d: case 0x0e: case 0x0f: case 0x10: 
  139.         case 0x11: case 0x12: case 0x13: case 0x14:
  140.         case 0x15: case 0x16: case 0x17: case 0x18: 
  141.         case 0x19: case 0x1a: case 0x1b: case 0x1c: 
  142.         case 0x1d: case 0x1e: case 0x1f: case 0x20: 
  143.         case 0x21: case 0x22: case 0x23: case 0x24: 
  144.         case 0x25: case 0x26: case 0x27: case 0x28:
  145.         case 0x29: case 0x2a: case 0x2b: case 0x2c: 
  146.         case 0x2d: case 0x2e: case 0x2f: case 0x30: 
  147.         case 0x31: case 0x32: case 0x33: case 0x34: 
  148.         case 0x35: case 0x36: case 0x37: case 0x38: 
  149.         case 0x39: case 0x3a: case 0x3b: case 0x3c:
  150.         case 0x3d: case 0x3e: case 0x3f:
  151.             /* This is terribly kludgey:
  152.                 In 432 units per inch, 4752 is 11 inches.
  153.                 When we go beyond this, we subtract 4752
  154.                 continually until we're back in range.
  155.             */
  156.             while (ypos >= pagelength) {
  157.                    ypos -= pagelength;
  158.                 DEBUGPRINTF("Page break\n", 0, 0);
  159.                 if (be->bepage)
  160.                 (be->bepage) ();
  161.             }
  162.             c = c&0x3f;
  163.             /*    Find the C/A/T code */
  164.             if (half == UPPER) {
  165.                 if (c > 46) {
  166.                 fprintf(stderr, "%s: Illegal upper flash: %d\n", 
  167.                     progname, c);
  168.                 exit(1);
  169.                 }
  170.                 nc = c + 62;
  171.             } else
  172.                 nc = c - 1;
  173.  
  174.             DEBUGPRINTF("nc=%d;pnts=%d;x,y=%ld,%ld;fnt=%d;",
  175.                 nc,points,xpos,ypos,font);
  176.             DEBUGPRINTF("rail=%s;mag=%s;tilt=%s;half=%s\n",
  177.                 words[rail], words[mag],words[tilt], words[half]);
  178.  
  179.             if (be->beputchar)
  180.                 (*be->beputchar)(xpos, ypos, font, points, nc);
  181.             
  182.             break;
  183.         /* Control codes */
  184.         case 0x40:
  185.             DEBUGPRINTF("Initialize\n");
  186.             xpos = 0;
  187.             ypos = -153;
  188.             escape = FORWARD;
  189.             lead = FORWARD;
  190.             half = LOWER;
  191.             rail = LOWER;
  192.             mag = LOWER;
  193.             tilt = DOWN;
  194.             calcfont;
  195.             break;
  196.         case 0x41:
  197.             DEBUGPRINTF("Rail lower\n");
  198.             rail = LOWER;
  199.             calcfont;
  200.             break;
  201.         case 0x42:
  202.             DEBUGPRINTF("Rail upper\n");
  203.             rail = UPPER;
  204.             calcfont;
  205.             break;
  206.         case 0x43:
  207.             DEBUGPRINTF("Mag upper\n");
  208.             mag = UPPER;
  209.             calcfont;
  210.             break;
  211.         case 0x44:
  212.             DEBUGPRINTF("Mag lower\n");
  213.             mag = LOWER;
  214.             calcfont;
  215.             break;
  216.         case 0x45:
  217.             DEBUGPRINTF("half lower\n");
  218.             half = LOWER;
  219.             break;
  220.         case 0x46:
  221.             DEBUGPRINTF("half upper\n");
  222.             half = UPPER;
  223.             break;
  224.         case 0x47:
  225.             DEBUGPRINTF("Escape forward\n");
  226.             escape = FORWARD;
  227.             break;
  228.         case 0x48:
  229.             DEBUGPRINTF("Escape backward\n");
  230.             escape = BACKWARD;
  231.             break;
  232.         case 0x49:
  233.             DEBUGPRINTF("STOP\n");
  234.             break;
  235.         case 0x4a:
  236.             DEBUGPRINTF("Lead forward\n");
  237.             lead = FORWARD;
  238.             break;
  239.         case 0x4b:
  240.             DEBUGPRINTF("Software cut!\n");
  241.             break;
  242.         case 0x4c:
  243.             DEBUGPRINTF("Lead backward\n");
  244.             lead = BACKWARD;
  245.             break;
  246.         case 0x4d: 
  247. #ifdef    SPECIAL
  248. #define    SPSIZ    256
  249.             {
  250.                 static char specialbuf[SPSIZ], *sbp;
  251.                 sbp = specialbuf;
  252.                 while((c = getc(stdin)) != EOF && c != '\n') {
  253.                 if (sbp < &specialbuf[SPSIZ]-2)
  254.                 *sbp++ = c;
  255.                 }
  256.                 *sbp = '\0';
  257.                 
  258.                 DEBUGPRINTF("SPECIAL SEQUENCE: %s!\n", specialbuf);
  259.                 dospecial(specialbuf);
  260.                 break;
  261.             }
  262. #else
  263.             fprintf(stderr, "%s: Illegal C/A/T code - %s\n",
  264.                 progname, "special sequences are disabled");
  265.             exit(1);
  266. #endif
  267.         case 0x4e:
  268.             DEBUGPRINTF("Tilt up\n");
  269.             tilt = UP;
  270.             calcfont;
  271.             break;
  272.         case 0x4f:
  273.             DEBUGPRINTF("Tilt down\n");
  274.             tilt = DOWN;
  275.             calcfont;
  276.             break;
  277.  
  278.         /* Size changes */
  279.         case 0x50: case 0x51: case 0x52: case 0x53:
  280.         case 0x54: case 0x55: case 0x56: case 0x57:
  281.         case 0x58: case 0x59: case 0x5a: case 0x5b:
  282.         case 0x5c: case 0x5d: case 0x5e: case 0x5f:
  283.             /* basic code is the points = ptab[c] line.
  284.                The rest of the brain-damage is due to the
  285.                fact that switching from some of the fonts
  286.                to some of the others blow the horizontal
  287.                position - see the cat(5) manual page
  288.                about single vs. doubler point sizes. */
  289.             {    static lastc = 0;
  290.             c &= 0xf;
  291.             points = ptab[c];
  292.  
  293.             DEBUGPRINTF("SPoints:%d;lastc:%5d;c:%d;xpos:%d\n", 
  294.                 points, lastc, c, xpos);
  295.             if (lastc <= 8 && c > 8) /* single -> doubler */
  296.                 xpos -= 55;
  297.             else if (lastc > 8 && c <= 8) /* doubler -> single */
  298.                 xpos += 55;
  299.  
  300.             DEBUGPRINTF("EPoints:%d;lastc:%5d;c:%d;xpos:%d\n", 
  301.                 points, lastc, c, xpos);
  302.             lastc = c;
  303.             break;
  304.             }
  305.         /* Lead (vertical motion) codes */
  306.         case 0x60: case 0x61: case 0x62: case 0x63:
  307.         case 0x64: case 0x65: case 0x66: case 0x67:
  308.         case 0x68: case 0x69: case 0x6a: case 0x6b:
  309.         case 0x6c: case 0x6d: case 0x6e: case 0x6f:
  310.         case 0x70: case 0x71: case 0x72: case 0x73:
  311.         case 0x74: case 0x75: case 0x76: case 0x77:
  312.         case 0x78: case 0x79: case 0x7a: case 0x7b:
  313.         case 0x7c: case 0x7d: case 0x7e: case 0x7f:
  314.  
  315.             DEBUGPRINTF("Lead(vertical) %02x\n", c&0x1f);
  316.             units = CTOINT(c,0x1f);
  317.             if (lead == FORWARD)
  318.                 ypos += 3*units;
  319.             else
  320.                 ypos -= 3*units;
  321.             break;
  322.         /* Escape (horizontal motion) codes */
  323.         case 0x80: case 0x81: case 0x82: case 0x83:
  324.         case 0x84: case 0x85: case 0x86: case 0x87:
  325.         case 0x88: case 0x89: case 0x8a: case 0x8b:
  326.         case 0x8c: case 0x8d: case 0x8e: case 0x8f:
  327.         case 0x90: case 0x91: case 0x92: case 0x93:
  328.         case 0x94: case 0x95: case 0x96: case 0x97:
  329.         case 0x98: case 0x99: case 0x9a: case 0x9b:
  330.         case 0x9c: case 0x9d: case 0x9e: case 0x9f:
  331.         case 0xa0: case 0xa1: case 0xa2: case 0xa3:
  332.         case 0xa4: case 0xa5: case 0xa6: case 0xa7:
  333.         case 0xa8: case 0xa9: case 0xaa: case 0xab:
  334.         case 0xac: case 0xad: case 0xae: case 0xaf:
  335.         case 0xb0: case 0xb1: case 0xb2: case 0xb3:
  336.         case 0xb4: case 0xb5: case 0xb6: case 0xb7:
  337.         case 0xb8: case 0xb9: case 0xba: case 0xbb:
  338.         case 0xbc: case 0xbd: case 0xbe: case 0xbf:
  339.         case 0xc0: case 0xc1: case 0xc2: case 0xc3:
  340.         case 0xc4: case 0xc5: case 0xc6: case 0xc7:
  341.         case 0xc8: case 0xc9: case 0xca: case 0xcb:
  342.         case 0xcc: case 0xcd: case 0xce: case 0xcf:
  343.         case 0xd0: case 0xd1: case 0xd2: case 0xd3:
  344.         case 0xd4: case 0xd5: case 0xd6: case 0xd7:
  345.         case 0xd8: case 0xd9: case 0xda: case 0xdb:
  346.         case 0xdc: case 0xdd: case 0xde: case 0xdf:
  347.         case 0xe0: case 0xe1: case 0xe2: case 0xe3:
  348.         case 0xe4: case 0xe5: case 0xe6: case 0xe7:
  349.         case 0xe8: case 0xe9: case 0xea: case 0xeb:
  350.         case 0xec: case 0xed: case 0xee: case 0xef:
  351.         case 0xf0: case 0xf1: case 0xf2: case 0xf3:
  352.         case 0xf4: case 0xf5: case 0xf6: case 0xf7:
  353.         case 0xf8: case 0xf9: case 0xfa: case 0xfb:
  354.         case 0xfc: case 0xfd: case 0xfe:
  355.  
  356.             units = CTOINT(c,0x7f);
  357.             if (escape == FORWARD)
  358.                 xpos += units;
  359.             else
  360.                 xpos -= units;
  361.             DEBUGPRINTF("ESC (hor): %02x\n", c&0x7f);
  362.             break;
  363.  
  364.         case 0xff:
  365.             DEBUGPRINTF("Illegal: %02x\n", c);
  366.             break;
  367.         }
  368.     }
  369.     if (be->bepage)
  370.         (*be->bepage)();
  371.     if (be->beepilog)
  372.         (*be->beepilog) ();
  373.     exit(0);
  374. }
  375.  
  376. usage() {
  377.     fprintf(stderr, "usage: %s [-D]\n", progname);
  378. }
  379.  
  380. double
  381. calc(s)
  382. register char *s; {
  383.     double retval = 0.0;
  384.     char scale;
  385.     switch (sscanf(s, "%lf%c", &retval, &scale)) {
  386.     case 0:
  387.     case EOF:
  388.         retval = 0.0;
  389.     case 1:
  390.         retval *= TROFFRESOLUTION; /* default is inches */
  391.         break;
  392.     case 2:
  393.         switch(scale) {
  394.         default:
  395.             fprintf(stderr, "%s: bad scale %c\n", scale);
  396.             retval = 0.0;
  397.         case 'i':
  398.             retval *= TROFFRESOLUTION;
  399.         case 'u':
  400.             break;
  401.         case 'c':
  402.             retval *= TROFFRESOLUTION * 50. / 127.;
  403.             break;
  404.         case 'P':
  405.             retval *= 72.;
  406.             break;
  407.         }
  408.         break;
  409.     }
  410.     return(retval);
  411. }
  412.  
  413. #ifdef    SPECIAL
  414.  
  415. dospecial(string)
  416. char *string; {
  417.     char buffer[512];
  418.     char b2[4];
  419.     buffer[0] = '\0';
  420.  
  421.     DEBUGPRINTF("Dospecial: (%d) %s\n", strlen(string), string);
  422.     while (*string)
  423.     switch(*string) {
  424.         case 'F':
  425.         /*if (parmchk(2, string))
  426.             return;*/
  427.         if (be->befontsel)
  428.             (*be->befontsel)(*(string+1), string+2);
  429.         *string ='\0';
  430.         return;
  431. #ifdef    FORM
  432.         case 'O':
  433.         if (be->beoverlay)
  434.             (*be->beoverlay)(string+1);
  435.         *string = '\0';
  436.         return;
  437. #endif
  438.         case 'i':
  439.         strcpy(buffer, "b");
  440.  
  441.         case 'I':
  442.         strcat(buffer, "include ");
  443.         strcat(buffer, string+1);
  444.         strcpy(b2, ".");
  445.         strcat(b2, driver);
  446.         interp(buffer, (be->bexlat && *string == 'F') ? be->bexlat:
  447.             FNULL, b2);
  448.         return;
  449.  
  450.         case 'B':
  451.         if (be->bebin)
  452.             (*be->bebin)(string+1);
  453.         string += 2;
  454.         break;
  455.         default:
  456.         fprintf(stderr, "%s: Illegal sequence %s\n", progname, string);
  457.         return;
  458.     }
  459. }
  460.  
  461. parmchk(cnt, string)
  462. int cnt; char *string; {
  463.     register int i;
  464.     for (i = 0; i < cnt; i++)
  465.     if (!string[i]) {
  466.         fprintf(stderr, "%s: Bad %d parameter command: %s\n", progname,
  467.         cnt, string);
  468.         return(1);
  469.     }
  470.     return(0);
  471. }
  472. #endif
  473.