118507Sjaap #ifndef lint
2*18508Sjaap static char sccsid[] = "@(#)tc.c	1.2	(CWI)	1.2	85/03/26";
318507Sjaap #endif lint
418507Sjaap /*
518507Sjaap  *	drive 4014 scope
618507Sjaap  */
718507Sjaap 
818507Sjaap /*
918507Sjaap output language from troff:
1018507Sjaap all numbers are character strings
1118507Sjaap 
1218507Sjaap sn	size in points
1318507Sjaap fn	font as number from 1-n
1418507Sjaap cx	ascii character x
1518507Sjaap Cxyz	funny char xyz. terminated by white space
1618507Sjaap Hn	go to absolute horizontal position n
1718507Sjaap Vn	go to absolute vertical position n (down is positive)
1818507Sjaap hn	go n units horizontally (relative)
1918507Sjaap vn	ditto vertically
2018507Sjaap nnc	move right nn, then print c (exactly 2 digits!)
2118507Sjaap 		(this wart is an optimization that shrinks output file size
2218507Sjaap 		 about 35% and run-time about 15% while preserving ascii-ness)
2318507Sjaap Dt ...\n	draw operation 't':
2418507Sjaap 	Dl x y		line from here by x,y
2518507Sjaap 	Dc d		circle of diameter d with left side here
2618507Sjaap 	De x y		ellipse of axes x,y with left side here
2718507Sjaap 	Da x y r	arc counter-clockwise by x,y of radius r
2818507Sjaap 	D~ x y x y ...	wiggly line by x,y then x,y ...
2918507Sjaap nb a	end of line (information only -- no action needed)
3018507Sjaap 	b = space before line, a = after
3118507Sjaap p	new page begins -- set v to 0
3218507Sjaap #...\n	comment
3318507Sjaap x ...\n	device control functions:
3418507Sjaap 	x i	init
3518507Sjaap 	x T s	name of device is s
3618507Sjaap 	x r n h v	resolution is n/inch
3718507Sjaap 		h = min horizontal motion, v = min vert
3818507Sjaap 	x p	pause (can restart)
3918507Sjaap 	x s	stop -- done for ever
4018507Sjaap 	x t	generate trailer
4118507Sjaap 	x f n s	font position n contains font s
4218507Sjaap 	x H n	set character height to n
4318507Sjaap 	x S n	set slant to N
4418507Sjaap 
4518507Sjaap 	Subcommands like "i" are often spelled out like "init".
4618507Sjaap */
4718507Sjaap 
4818507Sjaap #include	<stdio.h>
4918507Sjaap #include	<signal.h>
5018507Sjaap #include	<ctype.h>
5118507Sjaap 
5218507Sjaap #include "../dev.h"
5318507Sjaap #define	NFONT	10
5418507Sjaap 
5518507Sjaap int	output	= 0;	/* do we do output at all? */
5618507Sjaap int	nolist	= 0;	/* output page list if > 0 */
5718507Sjaap int	olist[20];	/* pairs of page numbers */
5818507Sjaap 
5918507Sjaap int	erase	= 1;
6018507Sjaap float	aspect	= 1.5;	/* default aspect ratio */
6118507Sjaap int	(*sigint)();
6218507Sjaap int	(*sigquit)();
6318507Sjaap 
6418507Sjaap struct dev dev;
6518507Sjaap struct font *fontbase[NFONT];
6618507Sjaap short	psizes[]	={ 11, 16, 22, 36, 0};	/* approx sizes available */
6718507Sjaap short	*pstab		= psizes;
6818507Sjaap int	nsizes	= 4;
6918507Sjaap int	pscode[]	={ ';', ':', '9', '8'};
7018507Sjaap int	nfonts;
7118507Sjaap int	smnt;	/* index of first special font */
7218507Sjaap int	nchtab;
7318507Sjaap char	*chname;
7418507Sjaap short	*chtab;
7518507Sjaap char	*fitab[NFONT];
7618507Sjaap char	*widthtab[NFONT];	/* widtab would be a better name */
7718507Sjaap char	*codetab[NFONT];	/* device codes */
7818507Sjaap 
7918507Sjaap #define	FATAL	1
8018507Sjaap #define	BMASK	0377
8118507Sjaap int	keepon 	= 0;
8218507Sjaap int	dbg	= 0;
8318507Sjaap long	lineno	= 0;
8418507Sjaap int	res	= 972;		/* input assumed computed according to this resolution */
8518507Sjaap 				/* initial value to avoid 0 divide */
8618507Sjaap FILE	*tf	= stdout;	/* output file */
8718507Sjaap char	*fontdir	= "/usr/lib/font";
8818507Sjaap extern char devname[];
8918507Sjaap 
9018507Sjaap FILE *fp	= stdin;	/* input file pointer */
9118507Sjaap 
main(argc,argv)9218507Sjaap main(argc, argv)
9318507Sjaap char *argv[];
9418507Sjaap {
9518507Sjaap 	char buf[BUFSIZ];
9618507Sjaap 	float atof();
9718507Sjaap 	int done();
9818507Sjaap 
9918507Sjaap 	setbuf(stdout, buf);
10018507Sjaap 	while (argc > 1 && argv[1][0] == '-') {
10118507Sjaap 		switch (argv[1][1]) {
10218507Sjaap 		case 'T':
10318507Sjaap 			if (strcmp(&argv[1][2], "cat") == 0) {	/* use the old one */
10418507Sjaap 				if (fork() == 0) {
10518507Sjaap 					execv("/usr/bin/oldtc", argv);
10618507Sjaap 					fprintf(stderr, "tc: can't find oldtc\n");
10718507Sjaap 				}
10818507Sjaap 				wait();
10918507Sjaap 				exit(1);
11018507Sjaap 			}
11118507Sjaap 			break;
11218507Sjaap 		case 'a':
11318507Sjaap 			aspect = atof(&argv[1][2]);
11418507Sjaap 			break;
11518507Sjaap 		case 'e':
11618507Sjaap 			erase = 0;
11718507Sjaap 			break;
11818507Sjaap 		case 'o':
11918507Sjaap 			outlist(&argv[1][2]);
12018507Sjaap 			break;
12118507Sjaap 		case 'd':
12218507Sjaap 			dbg = atoi(&argv[1][2]);
12318507Sjaap 			if (dbg == 0) dbg = 1;
12418507Sjaap 			break;
12518507Sjaap 		case 'c':
12618507Sjaap 			keepon = 1;
12718507Sjaap 			break;
12818507Sjaap 		}
12918507Sjaap 		argc--;
13018507Sjaap 		argv++;
13118507Sjaap 	}
13218507Sjaap 
13318507Sjaap 	sigint = signal(SIGINT, done);
13418507Sjaap 	sigquit = signal(SIGQUIT, SIG_IGN);
13518507Sjaap 	if (argc <= 1)
13618507Sjaap 		conv(stdin);
13718507Sjaap 	else
13818507Sjaap 		while (--argc > 0) {
13918507Sjaap 			if (strcmp(*++argv, "-") == 0)
14018507Sjaap 				fp = stdin;
14118507Sjaap 			else if ((fp = fopen(*argv, "r")) == NULL)
14218507Sjaap 				error(FATAL, "can't open %s", *argv);
14318507Sjaap 			conv(fp);
14418507Sjaap 			fclose(fp);
14518507Sjaap 		}
14618507Sjaap 	done();
14718507Sjaap }
14818507Sjaap 
outlist(s)14918507Sjaap outlist(s)	/* process list of page numbers to be printed */
15018507Sjaap char *s;
15118507Sjaap {
15218507Sjaap 	int n1, n2, i;
15318507Sjaap 
15418507Sjaap 	nolist = 0;
15518507Sjaap 	while (*s) {
15618507Sjaap 		n1 = 0;
15718507Sjaap 		if (isdigit(*s))
15818507Sjaap 			do
15918507Sjaap 				n1 = 10 * n1 + *s++ - '0';
16018507Sjaap 			while (isdigit(*s));
16118507Sjaap 		else
16218507Sjaap 			n1 = -9999;
16318507Sjaap 		n2 = n1;
16418507Sjaap 		if (*s == '-') {
16518507Sjaap 			s++;
16618507Sjaap 			n2 = 0;
16718507Sjaap 			if (isdigit(*s))
16818507Sjaap 				do
16918507Sjaap 					n2 = 10 * n2 + *s++ - '0';
17018507Sjaap 				while (isdigit(*s));
17118507Sjaap 			else
17218507Sjaap 				n2 = 9999;
17318507Sjaap 		}
17418507Sjaap 		olist[nolist++] = n1;
17518507Sjaap 		olist[nolist++] = n2;
17618507Sjaap 		if (*s != '\0')
17718507Sjaap 			s++;
17818507Sjaap 	}
17918507Sjaap 	olist[nolist] = 0;
18018507Sjaap 	if (dbg)
18118507Sjaap 		for (i=0; i<nolist; i += 2)
18218507Sjaap 			printf("%3d %3d\n", olist[i], olist[i+1]);
18318507Sjaap }
18418507Sjaap 
in_olist(n)18518507Sjaap in_olist(n)	/* is n in olist? */
18618507Sjaap int n;
18718507Sjaap {
18818507Sjaap 	int i;
18918507Sjaap 
19018507Sjaap 	if (nolist == 0)
19118507Sjaap 		return(1);	/* everything is included */
19218507Sjaap 	for (i = 0; i < nolist; i += 2)
19318507Sjaap 		if (n >= olist[i] && n <= olist[i+1])
19418507Sjaap 			return(1);
19518507Sjaap 	return(0);
19618507Sjaap }
19718507Sjaap 
conv(fp)19818507Sjaap conv(fp)
19918507Sjaap register FILE *fp;
20018507Sjaap {
20118507Sjaap 	register int c, k;
20218507Sjaap 	int m, n, i, n1, m1;
20318507Sjaap 	char str[100], buf[300];
20418507Sjaap 
20518507Sjaap 	while ((c = getc(fp)) != EOF) {
20618507Sjaap 		switch (c) {
20718507Sjaap 		case '\n':	/* when input is text */
20818507Sjaap 			lineno++;
20918507Sjaap 		case ' ':
21018507Sjaap 		case 0:		/* occasional noise creeps in */
21118507Sjaap 			break;
21218507Sjaap 		case '{':	/* push down current environment */
21318507Sjaap 			t_push();
21418507Sjaap 			break;
21518507Sjaap 		case '}':
21618507Sjaap 			t_pop();
21718507Sjaap 			break;
21818507Sjaap 		case '0': case '1': case '2': case '3': case '4':
21918507Sjaap 		case '5': case '6': case '7': case '8': case '9':
22018507Sjaap 			/* two motion digits plus a character */
22118507Sjaap 			hmot((c-'0')*10 + getc(fp)-'0');
22218507Sjaap 			put1(getc(fp));
22318507Sjaap 			break;
22418507Sjaap 		case 'c':	/* single ascii character */
22518507Sjaap 			put1(getc(fp));
22618507Sjaap 			break;
22718507Sjaap 		case 'C':
22818507Sjaap 			fscanf(fp, "%s", str);
22918507Sjaap 			put1s(str);
23018507Sjaap 			break;
23118507Sjaap 		case 't':	/* straight text */
23218507Sjaap 			fgets(buf, sizeof(buf), fp);
23318507Sjaap 			lineno++;
23418507Sjaap 			t_text(buf);
23518507Sjaap 			break;
23618507Sjaap 		case 'D':	/* draw function */
23718507Sjaap 			fgets(buf, sizeof(buf), fp);
23818507Sjaap 			lineno++;
23918507Sjaap 			switch (buf[0]) {
24018507Sjaap 			case 'l':	/* draw a line */
24118507Sjaap 				sscanf(buf+1, "%d %d", &n, &m);
24218507Sjaap 				drawline(n, m, ".");
24318507Sjaap 				break;
24418507Sjaap 			case 'c':	/* circle */
24518507Sjaap 				sscanf(buf+1, "%d", &n);
24618507Sjaap 				drawcirc(n);
24718507Sjaap 				break;
24818507Sjaap 			case 'e':	/* ellipse */
24918507Sjaap 				sscanf(buf+1, "%d %d", &m, &n);
25018507Sjaap 				drawellip(m, n);
25118507Sjaap 				break;
25218507Sjaap 			case 'a':	/* arc */
25318507Sjaap 				sscanf(buf+1, "%d %d %d %d", &n, &m, &n1, &m1);
25418507Sjaap 				drawarc(n, m, n1, m1);
25518507Sjaap 				break;
25618507Sjaap 			case '~':	/* wiggly line */
25718507Sjaap 				drawwig(buf+1);
25818507Sjaap 				break;
25918507Sjaap 			default:
26018507Sjaap 				error(FATAL, "unknown drawing function %s\n", buf);
26118507Sjaap 				break;
26218507Sjaap 			}
26318507Sjaap 			break;
26418507Sjaap 		case 's':
26518507Sjaap 			fscanf(fp, "%d", &n);	/* ignore fractional sizes */
26618507Sjaap 			setsize(t_size(n));
26718507Sjaap 			break;
26818507Sjaap 		case 'f':
26918507Sjaap 			fscanf(fp, "%s", str);
27018507Sjaap 			setfont(t_font(str));
27118507Sjaap 			break;
27218507Sjaap 		case 'H':	/* absolute horizontal motion */
27318507Sjaap 			/* fscanf(fp, "%d", &n); */
27418507Sjaap 			while ((c = getc(fp)) == ' ')
27518507Sjaap 				;
27618507Sjaap 			k = 0;
27718507Sjaap 			do {
27818507Sjaap 				k = 10 * k + c - '0';
27918507Sjaap 			} while (isdigit(c = getc(fp)));
28018507Sjaap 			ungetc(c, fp);
28118507Sjaap 			hgoto(k);
28218507Sjaap 			break;
28318507Sjaap 		case 'h':	/* relative horizontal motion */
28418507Sjaap 			/* fscanf(fp, "%d", &n); */
28518507Sjaap 			while ((c = getc(fp)) == ' ')
28618507Sjaap 				;
28718507Sjaap 			k = 0;
28818507Sjaap 			do {
28918507Sjaap 				k = 10 * k + c - '0';
29018507Sjaap 			} while (isdigit(c = getc(fp)));
29118507Sjaap 			ungetc(c, fp);
29218507Sjaap 			hmot(k);
29318507Sjaap 			break;
29418507Sjaap 		case 'w':	/* word space */
29518507Sjaap 			break;
29618507Sjaap 		case 'V':
29718507Sjaap 			fscanf(fp, "%d", &n);
29818507Sjaap 			vgoto(n);
29918507Sjaap 			break;
30018507Sjaap 		case 'v':
30118507Sjaap 			fscanf(fp, "%d", &n);
30218507Sjaap 			vmot(n);
30318507Sjaap 			break;
30418507Sjaap 		case 'p':	/* new page */
30518507Sjaap 			fscanf(fp, "%d", &n);
30618507Sjaap 			t_page(n);
30718507Sjaap 			break;
30818507Sjaap 		case 'n':	/* end of line */
30918507Sjaap 			while (getc(fp) != '\n')
31018507Sjaap 				;
31118507Sjaap 			t_newline();
31218507Sjaap 			break;
31318507Sjaap 		case '#':	/* comment */
31418507Sjaap 			while (getc(fp) != '\n')
31518507Sjaap 				;
31618507Sjaap 			lineno++;
31718507Sjaap 			break;
31818507Sjaap 		case 'x':	/* device control */
31918507Sjaap 			devcntrl(fp);
32018507Sjaap 			lineno++;
32118507Sjaap 			break;
32218507Sjaap 		default:
32318507Sjaap 			error(!FATAL, "unknown input character %o %c\n", c, c);
32418507Sjaap 			while (getc(fp) != '\n')
32518507Sjaap 				;
32618507Sjaap 		}
32718507Sjaap 	}
32818507Sjaap }
32918507Sjaap 
devcntrl(fp)33018507Sjaap devcntrl(fp)	/* interpret device control functions */
33118507Sjaap FILE *fp;
33218507Sjaap {
33318507Sjaap 	char str[20];
33418507Sjaap 	int c, n;
33518507Sjaap 
33618507Sjaap 	fscanf(fp, "%s", str);
33718507Sjaap 	switch (str[0]) {	/* crude for now */
33818507Sjaap 	case 'i':	/* initialize */
33918507Sjaap 		fileinit();
34018507Sjaap 		t_init(0);
34118507Sjaap 		break;
34218507Sjaap 	case 'T':	/* device name */
34318507Sjaap 		fscanf(fp, "%s", devname);
34418507Sjaap 		break;
34518507Sjaap 	case 't':	/* trailer */
34618507Sjaap 		t_trailer();
34718507Sjaap 		break;
34818507Sjaap 	case 'p':	/* pause -- can restart */
34918507Sjaap 		t_reset('p');
35018507Sjaap 		break;
35118507Sjaap 	case 's':	/* stop */
35218507Sjaap 		t_reset('s');
35318507Sjaap 		break;
35418507Sjaap 	case 'r':	/* resolution assumed when prepared */
35518507Sjaap 		fscanf(fp, "%d", &res);
35618507Sjaap 		break;
35718507Sjaap 	case 'f':	/* font used */
35818507Sjaap 		fscanf(fp, "%d %s", &n, str);
35918507Sjaap 		loadfont(n, str);
36018507Sjaap 		break;
36118507Sjaap 	}
36218507Sjaap 	while (getc(fp) != '\n')	/* skip rest of input line */
36318507Sjaap 		;
36418507Sjaap }
36518507Sjaap 
fileinit()36618507Sjaap fileinit()	/* read in font and code files, etc. */
36718507Sjaap {
36818507Sjaap }
36918507Sjaap 
fontprint(i)37018507Sjaap fontprint(i)	/* debugging print of font i (0,...) */
37118507Sjaap {
37218507Sjaap }
37318507Sjaap 
loadcode(n,nw)37418507Sjaap loadcode(n, nw)	/* load codetab on position n (0...); #chars is nw */
37518507Sjaap int n, nw;
37618507Sjaap {
37718507Sjaap }
37818507Sjaap 
loadfont(n,s)37918507Sjaap loadfont(n, s)	/* load font info for font s on position n (1...) */
38018507Sjaap int n;
38118507Sjaap char *s;
38218507Sjaap {
38318507Sjaap }
38418507Sjaap 
38518507Sjaap #define	ESC	033
38618507Sjaap #define	MAXY	(3071-100)
38718507Sjaap #define	US	037	/* text mode */
38818507Sjaap #define	GS	035	/* graphics mode */
38918507Sjaap #define	FF	014
39018507Sjaap 
error(f,s,a1,a2,a3,a4,a5,a6,a7)39118507Sjaap error(f, s, a1, a2, a3, a4, a5, a6, a7) {
39218507Sjaap 	fprintf(stderr, "%c%c%c", US, ESC, ';');	/* reset terminal sensibly */
39318507Sjaap 	fprintf(stderr, "tc: ");
39418507Sjaap 	fprintf(stderr, s, a1, a2, a3, a4, a5, a6, a7);
39518507Sjaap 	fprintf(stderr, " near line %ld\n", lineno);
39618507Sjaap 	if (f)
39718507Sjaap 		done(2);
39818507Sjaap }
39918507Sjaap 
40018507Sjaap 
40118507Sjaap /*
40218507Sjaap 	Here beginneth all the stuff that really depends
40318507Sjaap 	on the 202 (we hope).
40418507Sjaap */
40518507Sjaap 
40618507Sjaap 
40718507Sjaap char	devname[20]	= "4014";
40818507Sjaap 
40918507Sjaap #define	oput(c)	if (output) putchar(c); else;
41018507Sjaap 
41118507Sjaap int	stopped = 0;
41218507Sjaap int	ohx	= -1;
41318507Sjaap int	ohy	= -1;
41418507Sjaap int	oxb	= -1;
41518507Sjaap int	oly	= -1;
41618507Sjaap int	olx	= -1;
41718507Sjaap int	skip;
41818507Sjaap int	size	= 1;
41918507Sjaap int	font	= 1;		/* current font */
42018507Sjaap int	hpos;		/* horizontal position where we are supposed to be next (left = 0) */
42118507Sjaap int	vpos;		/* current vertical position (down positive) */
42218507Sjaap 
42318507Sjaap int	horig;		/* h origin of current block; hpos rel to this */
42418507Sjaap int	vorig;		/* v origin of current block; vpos rel to this */
42518507Sjaap 
42618507Sjaap int	DX	= 10;	/* step size in x for drawing */
42718507Sjaap int	DY	= 10;	/* step size in y for drawing */
42818507Sjaap int	drawdot	= '.';	/* draw with this character */
42918507Sjaap int	drawsize = 1;	/* shrink by this factor when drawing */
43018507Sjaap 
t_init(reinit)43118507Sjaap t_init(reinit)	/* initialize device */
43218507Sjaap int reinit;
43318507Sjaap {
43418507Sjaap 	fflush(stdout);
43518507Sjaap 	stopped = 0;
43618507Sjaap 	if (erase) {
43718507Sjaap 		oput(ESC);
43818507Sjaap 		oput(FF);
43918507Sjaap 		oput(US);
44018507Sjaap 	}
44118507Sjaap 	hpos = vpos = 0;
44218507Sjaap 	setsize(t_size(10));	/* start somewhere */
44318507Sjaap 	sendpt();
44418507Sjaap }
44518507Sjaap 
44618507Sjaap #define	MAXSTATE	5
44718507Sjaap 
44818507Sjaap struct state {
44918507Sjaap 	int	ssize;
45018507Sjaap 	int	sfont;
45118507Sjaap 	int	shpos;
45218507Sjaap 	int	svpos;
45318507Sjaap 	int	shorig;
45418507Sjaap 	int	svorig;
45518507Sjaap };
45618507Sjaap struct	state	state[MAXSTATE];
45718507Sjaap struct	state	*statep = state;
45818507Sjaap 
t_push()45918507Sjaap t_push()	/* begin a new block */
46018507Sjaap {
46118507Sjaap 	hflush();
46218507Sjaap 	statep->ssize = size;
46318507Sjaap 	statep->sfont = font;
46418507Sjaap 	statep->shorig = horig;
46518507Sjaap 	statep->svorig = vorig;
46618507Sjaap 	statep->shpos = hpos;
46718507Sjaap 	statep->svpos = vpos;
46818507Sjaap 	horig = hpos;
46918507Sjaap 	vorig = vpos;
47018507Sjaap 	hpos = vpos = 0;
47118507Sjaap 	if (statep++ >= state+MAXSTATE)
47218507Sjaap 		error(FATAL, "{ nested too deep");
47318507Sjaap 	hpos = vpos = 0;
47418507Sjaap }
47518507Sjaap 
t_pop()47618507Sjaap t_pop()	/* pop to previous state */
47718507Sjaap {
47818507Sjaap 	if (--statep < state)
47918507Sjaap 		error(FATAL, "extra }");
48018507Sjaap 	size = statep->ssize;
48118507Sjaap 	font = statep->sfont;
48218507Sjaap 	hpos = statep->shpos;
48318507Sjaap 	vpos = statep->svpos;
48418507Sjaap 	horig = statep->shorig;
48518507Sjaap 	vorig = statep->svorig;
48618507Sjaap }
48718507Sjaap 
48818507Sjaap int	np;	/* number of pages seen */
48918507Sjaap int	npmax;	/* high-water mark of np */
49018507Sjaap int	pgnum[100];	/* their actual numbers */
49118507Sjaap long	pgadr[100];	/* their seek addresses */
49218507Sjaap 
t_page(n)49318507Sjaap t_page(n)	/* do whatever new page functions */
49418507Sjaap {
49518507Sjaap 	long ftell();
49618507Sjaap 	int c, m, i;
49718507Sjaap 	char buf[100], *bp;
49818507Sjaap 
49918507Sjaap 	pgnum[np++] = n;
50018507Sjaap 	pgadr[np] = ftell(fp);
50118507Sjaap 	if (np > npmax)
50218507Sjaap 		npmax = np;
50318507Sjaap 	if (output == 0) {
50418507Sjaap 		output = in_olist(n);
50518507Sjaap 		t_init(1);
50618507Sjaap 		return;
50718507Sjaap 	}
50818507Sjaap 	/* have just printed something, and seen p<n> for next one */
50918507Sjaap 	vgoto(11 * res - 100);
51018507Sjaap 	sendpt();
51118507Sjaap 	oput(US);
51218507Sjaap 	fflush(stdout);
51318507Sjaap 
51418507Sjaap 
51518507Sjaap 	if (keepon) {
51618507Sjaap 		t_init(1);
51718507Sjaap 		return;
51818507Sjaap 	}
51918507Sjaap   next:
52018507Sjaap 	for (bp = buf; (*bp = readch()); )
52118507Sjaap 		if (*bp++ == '\n')
52218507Sjaap 			break;
52318507Sjaap 	*bp = 0;
52418507Sjaap 	switch (buf[0]) {
52518507Sjaap 	case 0:
526*18508Sjaap 	case 'q':
52718507Sjaap 		done();
52818507Sjaap 		break;
52918507Sjaap 	case '\n':
53018507Sjaap 		if (stopped)
53118507Sjaap 			done();
53218507Sjaap 		output = in_olist(n);
53318507Sjaap 		t_init(1);
53418507Sjaap 		return;
53518507Sjaap 	case '!':
53618507Sjaap 		callunix(&buf[1]);
53718507Sjaap 		fputs("!\n", stderr);
53818507Sjaap 		break;
53918507Sjaap 	case 'e':
54018507Sjaap 		erase = 1 - erase;
54118507Sjaap 		break;
54218507Sjaap 	case 'a':
54318507Sjaap 		aspect = atof(&buf[1]);
54418507Sjaap 		break;
54518507Sjaap 	case '-':
54618507Sjaap 	case 'p':
54718507Sjaap 		m = atoi(&buf[1]) + 1;
54818507Sjaap 		if (fp == stdin) {
54918507Sjaap 			fputs("you can't; it's not a file\n", stderr);
55018507Sjaap 			break;
55118507Sjaap 		}
55218507Sjaap 		if (np - m <= 0) {
55318507Sjaap 			fputs("too far back\n", stderr);
55418507Sjaap 			break;
55518507Sjaap 		}
55618507Sjaap 		np -= m;
55718507Sjaap 		fseek(fp, pgadr[np], 0);
55818507Sjaap 		output = 1;
55918507Sjaap 		t_init(1);
56018507Sjaap 		return;
56118507Sjaap 	case '0': case '1': case '2': case '3': case '4':
56218507Sjaap 	case '5': case '6': case '7': case '8': case '9':
56318507Sjaap 		m = atoi(&buf[0]);
56418507Sjaap 		for (i = 0; i < npmax; i++)
56518507Sjaap 			if (m == pgnum[i])
56618507Sjaap 				break;
56718507Sjaap 		if (i >= npmax || fp == stdin) {
56818507Sjaap 			fputs("you can't\n", stderr);
56918507Sjaap 			break;
57018507Sjaap 		}
57118507Sjaap 		np = i + 1;
57218507Sjaap 		fseek(fp, pgadr[np], 0);
57318507Sjaap 		output = 1;
57418507Sjaap 		t_init(1);
57518507Sjaap 		return;
57618507Sjaap 	case 'o':
57718507Sjaap 		outlist(&buf[1]);
57818507Sjaap 		output = 0;
57918507Sjaap 		t_init(1);
58018507Sjaap 		return;
58118507Sjaap 	case '?':
58218507Sjaap 		fputs("!cmd	unix cmd\n", stderr);
58318507Sjaap 		fputs("p	print this page again\n", stderr);
58418507Sjaap 		fputs("-n	go back n pages\n", stderr);
58518507Sjaap 		fputs("n	print page n (previously printed)\n", stderr);
58618507Sjaap 		fputs("o...	set the -o output list to ...\n", stderr);
587*18508Sjaap 		fputs("q	quit\n", stderr);
58818507Sjaap 		fputs("en	n=0 -> don't erase; n=1 -> erase\n", stderr);
58918507Sjaap 		fputs("an	sets aspect ratio to n\n", stderr);
59018507Sjaap 		break;
59118507Sjaap 	default:
59218507Sjaap 		fputs("?\n", stderr);
59318507Sjaap 		break;
59418507Sjaap 	}
59518507Sjaap 	goto next;
59618507Sjaap }
59718507Sjaap 
t_newline()59818507Sjaap t_newline()	/* do whatever for the end of a line */
59918507Sjaap {
60018507Sjaap 	hpos = 0;
60118507Sjaap }
60218507Sjaap 
t_size(n)60318507Sjaap t_size(n)	/* convert integer to internal size number*/
60418507Sjaap int n;
60518507Sjaap {
60618507Sjaap 	int i;
60718507Sjaap 
60818507Sjaap 	if (n <= pstab[0])
60918507Sjaap 		return(1);
61018507Sjaap 	else if (n >= pstab[nsizes-1])
61118507Sjaap 		return(nsizes);
61218507Sjaap 	for (i = 0; n > pstab[i]; i++)
61318507Sjaap 		;
61418507Sjaap 	return(i+1);
61518507Sjaap }
61618507Sjaap 
t_font(s)61718507Sjaap t_font(s)	/* convert string to internal font number */
61818507Sjaap char *s;
61918507Sjaap {
62018507Sjaap 	int n;
62118507Sjaap 
62218507Sjaap 	n = atoi(s);
62318507Sjaap 	if (n < 1 || n > nfonts)
62418507Sjaap 		n = 1;
62518507Sjaap 	return(n);
62618507Sjaap }
62718507Sjaap 
t_text(s)62818507Sjaap t_text(s)	/* print string s as text */
62918507Sjaap char *s;
63018507Sjaap {
63118507Sjaap 	int c, w;
63218507Sjaap 	char str[100];
63318507Sjaap 
63418507Sjaap 	if (!output)
63518507Sjaap 		return;
63618507Sjaap 	w = res / 2 * pstab[size-1] / 72;
63718507Sjaap 	while ((c = *s++) != '\n') {
63818507Sjaap 		if (c == '\\') {
63918507Sjaap 			switch (c = *s++) {
64018507Sjaap 			case '\\':
64118507Sjaap 			case 'e':
64218507Sjaap 				put1('\\');
64318507Sjaap 				break;
64418507Sjaap 			case '(':
64518507Sjaap 				str[0] = *s++;
64618507Sjaap 				str[1] = *s++;
64718507Sjaap 				str[2] = '\0';
64818507Sjaap 				put1s(str);
64918507Sjaap 				break;
65018507Sjaap 			}
65118507Sjaap 		} else {
65218507Sjaap 			put1(c);
65318507Sjaap 		}
65418507Sjaap 		hmot(w);
65518507Sjaap 	}
65618507Sjaap }
65718507Sjaap 
t_reset(c)65818507Sjaap t_reset(c)
65918507Sjaap {
66018507Sjaap 	int n;
66118507Sjaap 
66218507Sjaap 	output = 1;
66318507Sjaap 	fflush(stdout);
66418507Sjaap 	if (c == 's') {
66518507Sjaap 		stopped = 1;
66618507Sjaap 		t_page(9999);
66718507Sjaap 	}
66818507Sjaap }
66918507Sjaap 
t_trailer()67018507Sjaap t_trailer()
67118507Sjaap {
67218507Sjaap }
67318507Sjaap 
hgoto(n)67418507Sjaap hgoto(n)
67518507Sjaap {
67618507Sjaap 	hpos = n;	/* this is where we want to be */
67718507Sjaap 			/* before printing a character, */
67818507Sjaap 			/* have to make sure it's true */
67918507Sjaap }
68018507Sjaap 
hmot(n)68118507Sjaap hmot(n)	/* generate n units of horizontal motion */
68218507Sjaap int n;
68318507Sjaap {
68418507Sjaap 	hgoto(hpos + n);
68518507Sjaap }
68618507Sjaap 
hflush()68718507Sjaap hflush()	/* actual horizontal output occurs here */
68818507Sjaap {
68918507Sjaap 	if (output)
69018507Sjaap 		sendpt();
69118507Sjaap }
69218507Sjaap 
vgoto(n)69318507Sjaap vgoto(n)
69418507Sjaap {
69518507Sjaap 	vpos = n;
69618507Sjaap }
69718507Sjaap 
vmot(n)69818507Sjaap vmot(n)	/* generate n units of vertical motion */
69918507Sjaap int n;
70018507Sjaap {
70118507Sjaap 	vgoto(vpos + n);	/* ignores rounding */
70218507Sjaap }
70318507Sjaap 
put1s(s)70418507Sjaap put1s(s)	/* s is a funny char name */
70518507Sjaap char *s;
70618507Sjaap {
70718507Sjaap 	int i;
70818507Sjaap 	char *p;
70918507Sjaap 	extern char *spectab[];
71018507Sjaap 	static char prev[10] = "";
71118507Sjaap 	static int previ;
71218507Sjaap 
71318507Sjaap 	if (!output)
71418507Sjaap 		return;
71518507Sjaap 	if (strcmp(s, prev) != 0) {
71618507Sjaap 		previ = -1;
71718507Sjaap 		for (i = 0; spectab[i] != 0; i += 2)
71818507Sjaap 			if (strcmp(spectab[i], s) == 0) {
71918507Sjaap 				strcpy(prev, s);
72018507Sjaap 				previ = i;
72118507Sjaap 				break;
72218507Sjaap 			}
72318507Sjaap 	}
72418507Sjaap 	if (previ >= 0) {
72518507Sjaap 		hflush();
72618507Sjaap 		oput(US);
72718507Sjaap 		for (p = spectab[previ+1]; *p; p++)
72818507Sjaap 			oput(*p);
72918507Sjaap 	} else
73018507Sjaap 		prev[0] = 0;
73118507Sjaap }
73218507Sjaap 
put1(c)73318507Sjaap put1(c)	/* output char c */
73418507Sjaap int c;
73518507Sjaap {
73618507Sjaap 	if (!output)
73718507Sjaap 		return;
73818507Sjaap 	hflush();
73918507Sjaap 	oput(US);
74018507Sjaap 	oput(c);
74118507Sjaap }
74218507Sjaap 
setsize(n)74318507Sjaap setsize(n)	/* set point size to n (internal) */
74418507Sjaap int n;
74518507Sjaap {
74618507Sjaap 
74718507Sjaap 	if (!output)
74818507Sjaap 		return;
74918507Sjaap 	if (n == size)
75018507Sjaap 		return;	/* already there */
75118507Sjaap 	oput(ESC);
75218507Sjaap 	oput(pscode[n-1]);
75318507Sjaap 	size = n;
75418507Sjaap }
75518507Sjaap 
t_fp(n,s)75618507Sjaap t_fp(n, s)	/* font position n now contains font s */
75718507Sjaap int n;
75818507Sjaap char *s;
75918507Sjaap {
76018507Sjaap }
76118507Sjaap 
setfont(n)76218507Sjaap setfont(n)	/* set font to n */
76318507Sjaap int n;
76418507Sjaap {
76518507Sjaap }
76618507Sjaap 
done()76718507Sjaap done()
76818507Sjaap {
76918507Sjaap 	output = 1;
77018507Sjaap 	hgoto(0);
77118507Sjaap 	vgoto(11 * res - 100);	/* bottom of page */
77218507Sjaap 	sendpt();
77318507Sjaap 	oput(US);
77418507Sjaap 	oput(ESC);
77518507Sjaap 	oput(';');
77618507Sjaap 	oput(US);
77718507Sjaap 	fflush(stdout);
77818507Sjaap 	exit(0);
77918507Sjaap }
78018507Sjaap 
callunix(line)78118507Sjaap callunix(line)
78218507Sjaap char line[];
78318507Sjaap {
78418507Sjaap 	int rc, status, unixpid;
78518507Sjaap 	if( (unixpid=fork())==0 ) {
78618507Sjaap 		signal(SIGINT,sigint); signal(SIGQUIT,sigquit);
78718507Sjaap 		close(0); dup(2);
78818507Sjaap 		execl("/bin/sh", "-sh", "-c", line, 0);
78918507Sjaap 		exit(255);
79018507Sjaap 	}
79118507Sjaap 	else if(unixpid == -1)
79218507Sjaap 		return;
79318507Sjaap 	else{	signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
79418507Sjaap 		while( (rc = wait(&status)) != unixpid && rc != -1 ) ;
79518507Sjaap 		signal(SIGINT, done); signal(SIGQUIT,sigquit);
79618507Sjaap 	}
79718507Sjaap }
readch()79818507Sjaap readch(){
79918507Sjaap 	char c;
80018507Sjaap 	if (read(2,&c,1)<1) c=0;
80118507Sjaap 	return(c);
80218507Sjaap }
sendpt()80318507Sjaap sendpt(){
80418507Sjaap 	int hy,xb,ly,hx,lx;
80518507Sjaap 	int xx, yy;
80618507Sjaap 	float fx, fy;
80718507Sjaap 
80818507Sjaap 
80918507Sjaap 	fx = hpos + horig;
81018507Sjaap 	fy = vpos + vorig;
81118507Sjaap 	xx = (fx * MAXY / 11) / res * aspect + 0.5;
81218507Sjaap 	yy = MAXY - (fy * MAXY / 11) / res + 0.5;
81318507Sjaap 	oput(GS);
81418507Sjaap 	hy = ((yy>>7) & 037);
81518507Sjaap 	xb = ((xx & 03) + ((yy<<2) & 014) & 017);
81618507Sjaap 	ly = ((yy>>2) & 037);
81718507Sjaap 	hx = ((xx>>7) & 037);
81818507Sjaap 	lx = ((xx>>2) & 037);
81918507Sjaap 	if(hy != ohy)oput(hy | 040);
82018507Sjaap 	if(xb != oxb)oput(xb | 0140);
82118507Sjaap 	if((ly != oly) || (hx != ohx) || (xb != oxb))
82218507Sjaap 		oput(ly | 0140);
82318507Sjaap 	if(hx != ohx)oput(hx | 040);
82418507Sjaap 	oput(lx | 0100);
82518507Sjaap 	ohy = hy;
82618507Sjaap 	oxb = xb;
82718507Sjaap 	oly = ly;
82818507Sjaap 	ohx = hx;
82918507Sjaap 	olx = lx;
83018507Sjaap }
83118507Sjaap 
83218507Sjaap char *spectab[] ={
83318507Sjaap 	"em", "--",
83418507Sjaap 	"en", "-",
83518507Sjaap 	"hy", "-",
83618507Sjaap 	"ff", "ff",
83718507Sjaap 	"fi", "fi",
83818507Sjaap 	"fl", "fl",
83918507Sjaap 	"Fi", "ffi",
84018507Sjaap 	"Fl", "ffl",
84118507Sjaap 	"ct", "\033\016Z\bM\033\017",	/*cent sign*/
84218507Sjaap 	"de", "\033\016J\033\017",	/*degree*/
84318507Sjaap 	"dg", "\033\016M\b_\033\017",	/*dagger*/
84418507Sjaap 	"rg", "\033\016O\b&\033\017",	/*registered*/
84518507Sjaap 	"bu", "\033\016O\b~\033\017",	/*bullet*/
84618507Sjaap 	"fm", "'",
84718507Sjaap 	"co", "\033\016O\b#\033\017",	/*copyright*/
84818507Sjaap 	"sq", "\033\016L\033\017",	/*square*/
84918507Sjaap 	"*q", "\033\016(\bM\033\017",	/*psi*/
85018507Sjaap 	"*h", "\033\016o\b_\033\017",	/*theta*/
85118507Sjaap 	"*n", "v\b)",	/*nu*/
85218507Sjaap 	"*m", "\033\016V\b,\033\017",	/*mu*/
85318507Sjaap 	"*l", "\033\016)\b?\033\017",	/*lambda*/
85418507Sjaap 	"*i", "\033\016I\033\017",	/*iota*/
85518507Sjaap 	"*z", "S\b\033\016Z\033\017",	/*zeta*/
85618507Sjaap 	"*s", "o\b\'",	/*sigma*/
85718507Sjaap 	"*d", "o\b\033\0165\033\017",	/*delta*/
85818507Sjaap 	"*b", "\033\016b\033\017",	/*beta*/
85918507Sjaap 	"*c", "\033\016e\bc\033\017",	/*xi*/
86018507Sjaap 	"*y", "j\b\033\016C\033\017",	/*eta*/
86118507Sjaap 	"*f", "\033\016O\bM\033\017",	/*phi*/
86218507Sjaap 	"*u", "\033\016(\033\017",	/*upsilon*/
86318507Sjaap 	"*k", "\033\016k\033\017",	/*kappa*/
86418507Sjaap 	"*p", "T\b\033\016S\033\017",	/*pi*/
86518507Sjaap 	"da", "\033\016U\033\017",	/*down arrow*/
86618507Sjaap 	"*a", "\033\016A\033\017",	/*alpha*/
86718507Sjaap 	"or", "|",
86818507Sjaap 	"*x", "l\b/",	/*chi*/
86918507Sjaap 	"*e", "\033\016E\033\017",	/*epsilon*/
87018507Sjaap 	"*o", "\033\016O\033\017",	/*omicron*/
87118507Sjaap 	"<-", "\033\016[\033\017",	/*left arrow*/
87218507Sjaap 	"*r", "\033\016R\033\017",	/*rho*/
87318507Sjaap 	"ua", "\033\016Y\033\017",	/*up arrow*/
87418507Sjaap 	"*t", "\033\016N\033\017",	/*tau*/
87518507Sjaap 	"ul", "_",
87618507Sjaap 	"ru", "_",
87718507Sjaap 	"\\_", "_",
87818507Sjaap 	"*Q", "I\b\033\016(\033\017",	/*Psi*/
87918507Sjaap 	"bs", "\033\016O\bJ\033\017",	/*bell system sign*/
88018507Sjaap 	"if", "\033\016W\bX\033\017",	/*infinity*/
88118507Sjaap 	"*g", "`\b/",	/*gamma*/
88218507Sjaap 	"ip", "\033\016X\bF\033\017",	/*improper superset*/
88318507Sjaap 	"pt", "\033\016A\033\017",	/*proportional to*/
88418507Sjaap 	"rh", "\033\016\\\b]\033\017",	/*right hand*/
88518507Sjaap 	"*w", "\033\016W\033\017",	/*omega*/
88618507Sjaap 	"gr", "\033\016G\033\017",	/*gradient*/
88718507Sjaap 	"*F", "I\033\016\bO\033\017",	/*Phi*/
88818507Sjaap 	"*H", "O\b=",	/*Theta*/
88918507Sjaap 	"*W", "O\b_",	/*Omega*/
89018507Sjaap 	"cu", "\033\016V\033\017",	/*cup (union)*/
89118507Sjaap 	"rn", "\033\016@\033\017",	/*root en*/
89218507Sjaap 	"ts", "s",	/*terminal sigma*/
89318507Sjaap 	"*L", "\033\016)\bK\033\017",	/*Lambda*/
89418507Sjaap 	"\\-", "-",
89518507Sjaap 	"*G", "\033\016S\bK\033\017",	/*Gamma*/
89618507Sjaap 	"is", "\033\016i\033\017",	/*integral sign*/
89718507Sjaap 	"Sl", "l",
89818507Sjaap 	"*P", "\033\016t\b'\033\017",	/*Pi*/
89918507Sjaap 	"sb", "\033\016Z\033\017",	/*subset of*/
90018507Sjaap 	"sp", "\033\016X\033\017",	/*superset of*/
90118507Sjaap 	"ap", "\033\016T\033\017",	/*approximates*/
90218507Sjaap 	"pd", "o\b`",	/*partial derivative*/
90318507Sjaap 	"*D", "\033\016H\033\017",	/*Delta*/
90418507Sjaap 	"sr", "\033\016I\b'\033\017",	/*square root*/
90518507Sjaap 	"*S", ">\b\033\016F\b@\033\017",	/*Sigma*/
90618507Sjaap 	"~~", "\033\016T\bF\033\017",	/*approx =*/
90718507Sjaap 	"*C", "\033\016_\bF\b@\033\017",	/*Xi*/
90818507Sjaap 	"sl", "/",
90918507Sjaap 	"ca", "\033\016C\033\017",	/*cap (intersection)*/
91018507Sjaap 	"U", "\033\016y\033\017",	/*Upsilon*/
91118507Sjaap 	"no", "\033\016|\033\017",	/*not*/
91218507Sjaap 	"rc", "|",	/*right ceiling (rt of ")*/
91318507Sjaap 	"lt", "|",	/*left top (of big curly)*/
91418507Sjaap 	"bv", "|",	/*bold vertical*/
91518507Sjaap 	"lk", "|",	/*left center of big curly bracket*/
91618507Sjaap 	"lb", "|",	/*left bottom*/
91718507Sjaap 	"rt", "|",	/*right top*/
91818507Sjaap 	"rk", "|",	/*right center of big curly bracket*/
91918507Sjaap 	"rb", "|",	/*right bot*/
92018507Sjaap 	"rf", "|",	/*right floor (rb of ")*/
92118507Sjaap 	"lf", "|",	/*left floor (left bot of big sq bract)*/
92218507Sjaap 	"lc", "|",	/*left ceiling (lt of ")*/
92318507Sjaap 	"mu", "\033\016=\033\017",	/*multiply*/
92418507Sjaap 	"di", "\033\016+\033\017",	/*divide*/
92518507Sjaap 	"+-", "+\b_",	/*plus-minus*/
92618507Sjaap 	"<=", "\033\016$\033\017",	/*<=*/
92718507Sjaap 	">=", "\033\016^\033\017",	/*>=*/
92818507Sjaap 	"==", "=\b_",	/*identically equal*/
92918507Sjaap 	"!=", "\033\016*\033\017",	/*not equal*/
93018507Sjaap 	"aa", "'",
93118507Sjaap 	"ga", "`",
93218507Sjaap 	"lh", "\033\016|\b[\033\017",	/*left hand*/
93318507Sjaap 	"mo", "\033\016c\b_\033\017",	/*member of*/
93418507Sjaap 	"es", "\033\016O\b/\033\017",	/*empty set*/
93518507Sjaap 	"dd", "\033\016%\bM\033\017",	/*dbl dagger*/
93618507Sjaap 	"br", "|",	/*box rule*/
93718507Sjaap 	"vr", "|",	/* vertical rule */
93818507Sjaap 	"ib", "\033\016Z\bF\033\017",	/*improper subset*/
93918507Sjaap 	"ci", "\033\016O\033\017",	/*circle*/
94018507Sjaap 	"eq", "=",
94118507Sjaap 	"pl", "+",
94218507Sjaap 	"mi", "-",
94318507Sjaap 	"12", "1/2",
94418507Sjaap 	"14", "1/4",
94518507Sjaap 	"34", "3/4",
94618507Sjaap 	"->", "\033\016]\033\017",	/*right arrow*/
94718507Sjaap 	"sc", "g\b\033\016C\033\017",	/*section mark*/
94818507Sjaap 	"**", "*",
94918507Sjaap 	"l.", ".",
95018507Sjaap 	"L.", ".",
95118507Sjaap 	"bx", "[\b]",
95218507Sjaap 	"ob", "o",	/* open bullet */
95318507Sjaap 	"cd", ",",	/* cedilla */
95418507Sjaap 	"..", "\033\016!\033\017",	/* umlaut */
95518507Sjaap 	0, 0,
95618507Sjaap };
957