home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1985, 1986, 1987, 1988 Chris Lewis
- All Rights Reserved
-
- Permission to copy and further distribute is freely given provided
- this copyright notice remains intact and that this software is not
- sold for profit.
-
- Project: Generic Troff drivers
- Module: troff2.c
- Author: Chris Lewis
- Specs: Troff interpreter, needs a driver to work.
- */
-
- #include "defs.h"
-
- #ifndef SVR3
- #ifndef lint
- static char SCCSid[] = "@(#)troff2.c: 1.16 Copyright 89/07/17 10:58:29 Chris Lewis";
- #endif
- #else
- #ident "@(#)troff2.c: 1.16 Copyright 89/07/17 10:58:29 Chris Lewis"
- #endif
-
- #define ESC 0x80
- #define FLASH 0x00
- #define CONTROL 0x40
- #define LEAD 0x60
- #define SIZE 0x50
-
- #define DOWN 0
- #define UP 1
- #define LOWER 2
- #define UPPER 3
- #define FORWARD 4
- #define BACKWARD 5
-
- char ptab[15] = { 7, 8, 10, 11, 12, 14, 18, 9, 6, 16, 20, 22, 24, 28, 36};
- int points;
-
- long xpos, ypos;
-
- char *words[] = {
- "down", "up", "lower", "upper", "forward", "backwards"
- };
-
-
- #define CTOINT(val,sig) ((~c)&sig)
-
- double pagelength = 11 * TROFFRESOLUTION;
-
- #define CFONT (((mag == UPPER)<<2)|((rail == UPPER)<<1)|(tilt==DOWN))
- #ifdef FONT4
- #define calcfont font = (CFONT >> 1)
- #else
- #define calcfont font = CFONT
- #endif
-
- char *progname;
- char *device = {"alw"};
-
- double calc();
-
- char *driver;
-
- main(argc, argv)
- int argc;
- char **argv; {
- register int nc, c;
- register int i,j;
- register int units;
- register int font, rail, mag, tilt, half, escape, lead;
-
- extern int getopt();
- extern char *optarg;
-
- progname = strrchr(argv[0], "/");
- if (progname)
- progname++;
- else
- progname = argv[0];
-
- if (strlen(progname) >= 2)
- driver = progname + strlen(progname) - 2;
- else
- driver = "ZZ";
-
- while((c = getopt(argc, argv, "T:VDd:l:m")) != EOF)
- switch (c) {
- case 'T':
- device = optarg;
- break;
- case 'm':
- metrics = 1;
- break;
- case 'V':
- printf("%s: version %s\n", progname, T2VERSION);
- exit(0);
- case 'D':
- diagFile = fopen("diagnostics", "w");
- if (!diagFile) {
- fprintf(stderr, "%s: Could not open diagnostics file\n",
- progname);
- exit(1);
- }
- break;
- case 'd':
- driver = optarg;
- break;
- case 'l':
- pagelength = calc(optarg);
- if (pagelength == 0) {
- fprintf(stderr, "%s: Bad pagelength %s\n", progname,
- optarg);
- exit(1);
- }
- break;
- case '?':
- default:
- usage();
- exit(1);
- }
-
- getdriver(driver);
-
- if (be->beprolog)
- (*be->beprolog)();
-
- while (!feof(stdin)) {
- c = getc(stdin);
- switch(c) {
- case 0x00:
- DEBUGPRINTF("NOP\n");
- break;
- /* Flash (print character) codes */
- case 0x01: case 0x02: case 0x03: case 0x04:
- case 0x05: case 0x06: case 0x07: case 0x08:
- case 0x09: case 0x0a: case 0x0b: case 0x0c:
- case 0x0d: case 0x0e: case 0x0f: case 0x10:
- case 0x11: case 0x12: case 0x13: case 0x14:
- case 0x15: case 0x16: case 0x17: case 0x18:
- case 0x19: case 0x1a: case 0x1b: case 0x1c:
- case 0x1d: case 0x1e: case 0x1f: case 0x20:
- case 0x21: case 0x22: case 0x23: case 0x24:
- case 0x25: case 0x26: case 0x27: case 0x28:
- case 0x29: case 0x2a: case 0x2b: case 0x2c:
- case 0x2d: case 0x2e: case 0x2f: case 0x30:
- case 0x31: case 0x32: case 0x33: case 0x34:
- case 0x35: case 0x36: case 0x37: case 0x38:
- case 0x39: case 0x3a: case 0x3b: case 0x3c:
- case 0x3d: case 0x3e: case 0x3f:
- /* This is terribly kludgey:
- In 432 units per inch, 4752 is 11 inches.
- When we go beyond this, we subtract 4752
- continually until we're back in range.
- */
- while (ypos >= pagelength) {
- ypos -= pagelength;
- DEBUGPRINTF("Page break\n", 0, 0);
- if (be->bepage)
- (be->bepage) ();
- }
- c = c&0x3f;
- /* Find the C/A/T code */
- if (half == UPPER) {
- if (c > 46) {
- fprintf(stderr, "%s: Illegal upper flash: %d\n",
- progname, c);
- exit(1);
- }
- nc = c + 62;
- } else
- nc = c - 1;
-
- DEBUGPRINTF("nc=%d;pnts=%d;x,y=%ld,%ld;fnt=%d;",
- nc,points,xpos,ypos,font);
- DEBUGPRINTF("rail=%s;mag=%s;tilt=%s;half=%s\n",
- words[rail], words[mag],words[tilt], words[half]);
-
- if (be->beputchar)
- (*be->beputchar)(xpos, ypos, font, points, nc);
-
- break;
- /* Control codes */
- case 0x40:
- DEBUGPRINTF("Initialize\n");
- xpos = 0;
- ypos = -153;
- escape = FORWARD;
- lead = FORWARD;
- half = LOWER;
- rail = LOWER;
- mag = LOWER;
- tilt = DOWN;
- calcfont;
- break;
- case 0x41:
- DEBUGPRINTF("Rail lower\n");
- rail = LOWER;
- calcfont;
- break;
- case 0x42:
- DEBUGPRINTF("Rail upper\n");
- rail = UPPER;
- calcfont;
- break;
- case 0x43:
- DEBUGPRINTF("Mag upper\n");
- mag = UPPER;
- calcfont;
- break;
- case 0x44:
- DEBUGPRINTF("Mag lower\n");
- mag = LOWER;
- calcfont;
- break;
- case 0x45:
- DEBUGPRINTF("half lower\n");
- half = LOWER;
- break;
- case 0x46:
- DEBUGPRINTF("half upper\n");
- half = UPPER;
- break;
- case 0x47:
- DEBUGPRINTF("Escape forward\n");
- escape = FORWARD;
- break;
- case 0x48:
- DEBUGPRINTF("Escape backward\n");
- escape = BACKWARD;
- break;
- case 0x49:
- DEBUGPRINTF("STOP\n");
- break;
- case 0x4a:
- DEBUGPRINTF("Lead forward\n");
- lead = FORWARD;
- break;
- case 0x4b:
- DEBUGPRINTF("Software cut!\n");
- break;
- case 0x4c:
- DEBUGPRINTF("Lead backward\n");
- lead = BACKWARD;
- break;
- case 0x4d:
- #ifdef SPECIAL
- #define SPSIZ 256
- {
- static char specialbuf[SPSIZ], *sbp;
- sbp = specialbuf;
- while((c = getc(stdin)) != EOF && c != '\n') {
- if (sbp < &specialbuf[SPSIZ]-2)
- *sbp++ = c;
- }
- *sbp = '\0';
-
- DEBUGPRINTF("SPECIAL SEQUENCE: %s!\n", specialbuf);
- dospecial(specialbuf);
- break;
- }
- #else
- fprintf(stderr, "%s: Illegal C/A/T code - %s\n",
- progname, "special sequences are disabled");
- exit(1);
- #endif
- case 0x4e:
- DEBUGPRINTF("Tilt up\n");
- tilt = UP;
- calcfont;
- break;
- case 0x4f:
- DEBUGPRINTF("Tilt down\n");
- tilt = DOWN;
- calcfont;
- break;
-
- /* Size changes */
- case 0x50: case 0x51: case 0x52: case 0x53:
- case 0x54: case 0x55: case 0x56: case 0x57:
- case 0x58: case 0x59: case 0x5a: case 0x5b:
- case 0x5c: case 0x5d: case 0x5e: case 0x5f:
- /* basic code is the points = ptab[c] line.
- The rest of the brain-damage is due to the
- fact that switching from some of the fonts
- to some of the others blow the horizontal
- position - see the cat(5) manual page
- about single vs. doubler point sizes. */
- { static lastc = 0;
- c &= 0xf;
- points = ptab[c];
-
- DEBUGPRINTF("SPoints:%d;lastc:%5d;c:%d;xpos:%d\n",
- points, lastc, c, xpos);
- if (lastc <= 8 && c > 8) /* single -> doubler */
- xpos -= 55;
- else if (lastc > 8 && c <= 8) /* doubler -> single */
- xpos += 55;
-
- DEBUGPRINTF("EPoints:%d;lastc:%5d;c:%d;xpos:%d\n",
- points, lastc, c, xpos);
- lastc = c;
- break;
- }
- /* Lead (vertical motion) codes */
- case 0x60: case 0x61: case 0x62: case 0x63:
- case 0x64: case 0x65: case 0x66: case 0x67:
- case 0x68: case 0x69: case 0x6a: case 0x6b:
- case 0x6c: case 0x6d: case 0x6e: case 0x6f:
- case 0x70: case 0x71: case 0x72: case 0x73:
- case 0x74: case 0x75: case 0x76: case 0x77:
- case 0x78: case 0x79: case 0x7a: case 0x7b:
- case 0x7c: case 0x7d: case 0x7e: case 0x7f:
-
- DEBUGPRINTF("Lead(vertical) %02x\n", c&0x1f);
- units = CTOINT(c,0x1f);
- if (lead == FORWARD)
- ypos += 3*units;
- else
- ypos -= 3*units;
- break;
- /* Escape (horizontal motion) codes */
- case 0x80: case 0x81: case 0x82: case 0x83:
- case 0x84: case 0x85: case 0x86: case 0x87:
- case 0x88: case 0x89: case 0x8a: case 0x8b:
- case 0x8c: case 0x8d: case 0x8e: case 0x8f:
- case 0x90: case 0x91: case 0x92: case 0x93:
- case 0x94: case 0x95: case 0x96: case 0x97:
- case 0x98: case 0x99: case 0x9a: case 0x9b:
- case 0x9c: case 0x9d: case 0x9e: case 0x9f:
- case 0xa0: case 0xa1: case 0xa2: case 0xa3:
- case 0xa4: case 0xa5: case 0xa6: case 0xa7:
- case 0xa8: case 0xa9: case 0xaa: case 0xab:
- case 0xac: case 0xad: case 0xae: case 0xaf:
- case 0xb0: case 0xb1: case 0xb2: case 0xb3:
- case 0xb4: case 0xb5: case 0xb6: case 0xb7:
- case 0xb8: case 0xb9: case 0xba: case 0xbb:
- case 0xbc: case 0xbd: case 0xbe: case 0xbf:
- case 0xc0: case 0xc1: case 0xc2: case 0xc3:
- case 0xc4: case 0xc5: case 0xc6: case 0xc7:
- case 0xc8: case 0xc9: case 0xca: case 0xcb:
- case 0xcc: case 0xcd: case 0xce: case 0xcf:
- case 0xd0: case 0xd1: case 0xd2: case 0xd3:
- case 0xd4: case 0xd5: case 0xd6: case 0xd7:
- case 0xd8: case 0xd9: case 0xda: case 0xdb:
- case 0xdc: case 0xdd: case 0xde: case 0xdf:
- case 0xe0: case 0xe1: case 0xe2: case 0xe3:
- case 0xe4: case 0xe5: case 0xe6: case 0xe7:
- case 0xe8: case 0xe9: case 0xea: case 0xeb:
- case 0xec: case 0xed: case 0xee: case 0xef:
- case 0xf0: case 0xf1: case 0xf2: case 0xf3:
- case 0xf4: case 0xf5: case 0xf6: case 0xf7:
- case 0xf8: case 0xf9: case 0xfa: case 0xfb:
- case 0xfc: case 0xfd: case 0xfe:
-
- units = CTOINT(c,0x7f);
- if (escape == FORWARD)
- xpos += units;
- else
- xpos -= units;
- DEBUGPRINTF("ESC (hor): %02x\n", c&0x7f);
- break;
-
- case 0xff:
- DEBUGPRINTF("Illegal: %02x\n", c);
- break;
- }
- }
- if (be->bepage)
- (*be->bepage)();
- if (be->beepilog)
- (*be->beepilog) ();
- exit(0);
- }
-
- usage() {
- fprintf(stderr, "usage: %s [-D]\n", progname);
- }
-
- double
- calc(s)
- register char *s; {
- double retval = 0.0;
- char scale;
- switch (sscanf(s, "%lf%c", &retval, &scale)) {
- case 0:
- case EOF:
- retval = 0.0;
- case 1:
- retval *= TROFFRESOLUTION; /* default is inches */
- break;
- case 2:
- switch(scale) {
- default:
- fprintf(stderr, "%s: bad scale %c\n", scale);
- retval = 0.0;
- case 'i':
- retval *= TROFFRESOLUTION;
- case 'u':
- break;
- case 'c':
- retval *= TROFFRESOLUTION * 50. / 127.;
- break;
- case 'P':
- retval *= 72.;
- break;
- }
- break;
- }
- return(retval);
- }
-
- #ifdef SPECIAL
-
- dospecial(string)
- char *string; {
- char buffer[512];
- char b2[4];
- buffer[0] = '\0';
-
- DEBUGPRINTF("Dospecial: (%d) %s\n", strlen(string), string);
- while (*string)
- switch(*string) {
- case 'F':
- /*if (parmchk(2, string))
- return;*/
- if (be->befontsel)
- (*be->befontsel)(*(string+1), string+2);
- *string ='\0';
- return;
- #ifdef FORM
- case 'O':
- if (be->beoverlay)
- (*be->beoverlay)(string+1);
- *string = '\0';
- return;
- #endif
- case 'i':
- strcpy(buffer, "b");
-
- case 'I':
- strcat(buffer, "include ");
- strcat(buffer, string+1);
- strcpy(b2, ".");
- strcat(b2, driver);
- interp(buffer, (be->bexlat && *string == 'F') ? be->bexlat:
- FNULL, b2);
- return;
-
- case 'B':
- if (be->bebin)
- (*be->bebin)(string+1);
- string += 2;
- break;
- default:
- fprintf(stderr, "%s: Illegal sequence %s\n", progname, string);
- return;
- }
- }
-
- parmchk(cnt, string)
- int cnt; char *string; {
- register int i;
- for (i = 0; i < cnt; i++)
- if (!string[i]) {
- fprintf(stderr, "%s: Bad %d parameter command: %s\n", progname,
- cnt, string);
- return(1);
- }
- return(0);
- }
- #endif
-