xref: /onnv-gate/usr/src/cmd/eqn/text.c (revision 413:1d1413a1c800)
1364Sceastha /*
2364Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3364Sceastha  * Use is subject to license terms.
4364Sceastha  */
5364Sceastha 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
14364Sceastha 
15364Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
17364Sceastha #include "e.h"
18364Sceastha #include "e.def"
190Sstevel@tonic-gate #include <locale.h>
200Sstevel@tonic-gate 
210Sstevel@tonic-gate int	csp;
220Sstevel@tonic-gate int	psp;
230Sstevel@tonic-gate #define	CSSIZE	400
240Sstevel@tonic-gate char	cs[420];
250Sstevel@tonic-gate 
260Sstevel@tonic-gate int	lf, rf;	/* temporary spots for left and right fonts */
270Sstevel@tonic-gate 
28364Sceastha void name4(int, int);
29364Sceastha void roman(int);
30364Sceastha void shim(void);
31364Sceastha int trans(int, char *);
32364Sceastha 
33364Sceastha void
text(int t,char * p1)34364Sceastha text(int t, char *p1)
35364Sceastha {
360Sstevel@tonic-gate 	int c;
370Sstevel@tonic-gate 	char *p;
380Sstevel@tonic-gate 	tbl *tp, *lookup();
39*413Sceastha 	extern tbl *restbl[];
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	yyval = oalloc();
420Sstevel@tonic-gate 	ebase[yyval] = 0;
430Sstevel@tonic-gate #ifndef NEQN
440Sstevel@tonic-gate 	eht[yyval] = VERT(EM(1.0, EFFPS(ps)));	/* ht in machine units */
45364Sceastha #else	/* NEQN */
460Sstevel@tonic-gate 	eht[yyval] = VERT(2);	/* 2 half-spaces */
47364Sceastha #endif	/* NEQN */
480Sstevel@tonic-gate 	lfont[yyval] = rfont[yyval] = ROM;
490Sstevel@tonic-gate 	if (t == QTEXT)
500Sstevel@tonic-gate 		p = p1;
51364Sceastha 	else if (t == SPACE)
520Sstevel@tonic-gate 		p = "\\ ";
53364Sceastha 	else if (t == THIN)
540Sstevel@tonic-gate 		p = "\\|";
55364Sceastha 	else if (t == TAB)
560Sstevel@tonic-gate 		p = "\\t";
57*413Sceastha 	else if ((tp = lookup(restbl, p1, NULL)) != NULL)
580Sstevel@tonic-gate 		p = tp->defn;
590Sstevel@tonic-gate 	else {
600Sstevel@tonic-gate 		lf = rf = 0;
61364Sceastha 		for (csp = psp = 0; (c = p1[psp++]) != '\0'; ) {
620Sstevel@tonic-gate 			rf = trans(c, p1);
630Sstevel@tonic-gate 			if (lf == 0)
640Sstevel@tonic-gate 				lf = rf;	/* save first */
65364Sceastha 			if (csp > CSSIZE)
66364Sceastha 				error(FATAL, gettext(
67364Sceastha 				    "converted token %.25s... too long"), p1);
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 		cs[csp] = '\0';
700Sstevel@tonic-gate 		p = cs;
710Sstevel@tonic-gate 		lfont[yyval] = lf;
720Sstevel@tonic-gate 		rfont[yyval] = rf;
730Sstevel@tonic-gate 	}
74364Sceastha 	if (dbg)
75364Sceastha 		printf(".\t%dtext: S%d <- %s; b=%d,h=%d,lf=%c,rf=%c\n",
76364Sceastha 		    t, yyval, p, ebase[yyval], eht[yyval], lfont[yyval],
77364Sceastha 		    rfont[yyval]);
780Sstevel@tonic-gate 	printf(".ds %d \"%s\n", yyval, p);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
81364Sceastha int
trans(int c,char * p1)82364Sceastha trans(int c, char *p1)
83364Sceastha {
840Sstevel@tonic-gate 	int f;
850Sstevel@tonic-gate 	f = ROM;
86364Sceastha 	switch (c) {
870Sstevel@tonic-gate 	case '0': case '1': case '2': case '3': case '4':
880Sstevel@tonic-gate 	case '5': case '6': case '7': case '8': case '9':
890Sstevel@tonic-gate 	case ':': case ';': case '!': case '%':
900Sstevel@tonic-gate 	case '(': case '[': case ')': case ']':
910Sstevel@tonic-gate 	case ',':
920Sstevel@tonic-gate 		if (rf == ITAL)
930Sstevel@tonic-gate 			shim();
940Sstevel@tonic-gate 		roman(c); break;
950Sstevel@tonic-gate 	case '.':
960Sstevel@tonic-gate 		if (rf == ROM)
970Sstevel@tonic-gate 			roman(c);
980Sstevel@tonic-gate 		else
990Sstevel@tonic-gate 			cs[csp++] = c;
1000Sstevel@tonic-gate 		f = rf;
1010Sstevel@tonic-gate 		break;
1020Sstevel@tonic-gate 	case '|':
1030Sstevel@tonic-gate 		if (rf == ITAL)
1040Sstevel@tonic-gate 			shim();
1050Sstevel@tonic-gate 		shim(); roman(c); shim(); break;
1060Sstevel@tonic-gate 	case '=':
1070Sstevel@tonic-gate 		if (rf == ITAL)
1080Sstevel@tonic-gate 			shim();
109364Sceastha 		name4('e', 'q');
1100Sstevel@tonic-gate 		break;
1110Sstevel@tonic-gate 	case '+':
1120Sstevel@tonic-gate 		if (rf == ITAL)
1130Sstevel@tonic-gate 			shim();
1140Sstevel@tonic-gate 		name4('p', 'l');
1150Sstevel@tonic-gate 		break;
1160Sstevel@tonic-gate 	case '>': case '<':
1170Sstevel@tonic-gate 		if (rf == ITAL)
1180Sstevel@tonic-gate 			shim();
119364Sceastha 		if (p1[psp] == '=') {	/* look ahead for == <= >= */
120364Sceastha 			name4(c, '=');
1210Sstevel@tonic-gate 			psp++;
1220Sstevel@tonic-gate 		} else {
123364Sceastha 			cs[csp++] = c;
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		break;
1260Sstevel@tonic-gate 	case '-':
1270Sstevel@tonic-gate 		if (rf == ITAL)
1280Sstevel@tonic-gate 			shim();
129364Sceastha 		if (p1[psp] == '>') {
130364Sceastha 			name4('-', '>'); psp++;
1310Sstevel@tonic-gate 		} else {
132364Sceastha 			name4('m', 'i');
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		break;
1350Sstevel@tonic-gate 	case '/':
1360Sstevel@tonic-gate 		if (rf == ITAL)
1370Sstevel@tonic-gate 			shim();
138364Sceastha 		name4('s', 'l');
1390Sstevel@tonic-gate 		break;
1400Sstevel@tonic-gate 	case '~': case ' ':
1410Sstevel@tonic-gate 		shim(); shim(); break;
1420Sstevel@tonic-gate 	case '^':
1430Sstevel@tonic-gate 		shim(); break;
1440Sstevel@tonic-gate 	case '\\':	/* troff - pass 2 or 3 more chars */
1450Sstevel@tonic-gate 		if (rf == ITAL)
1460Sstevel@tonic-gate 			shim();
1470Sstevel@tonic-gate 		cs[csp++] = c; cs[csp++] = c = p1[psp++]; cs[csp++] = p1[psp++];
148364Sceastha 		if (c == '(') cs[csp++] = p1[psp++];
149364Sceastha 		if (c == '*' && cs[csp-1] == '(') {
1500Sstevel@tonic-gate 			cs[csp++] = p1[psp++];
1510Sstevel@tonic-gate 			cs[csp++] = p1[psp++];
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 		break;
1540Sstevel@tonic-gate 	case '\'':
155364Sceastha 		cs[csp++] = '\\';
156364Sceastha 		cs[csp++] = 'f';
157364Sceastha 		cs[csp++] = rf == ITAL ? ITAL : ROM;
158364Sceastha 		name4('f', 'm');
1590Sstevel@tonic-gate 		cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = 'P';
160364Sceastha 		f = rf == ITAL ? ITAL : ROM;
1610Sstevel@tonic-gate 		break;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	case 'f':
1640Sstevel@tonic-gate 		if (ft == ITAL) {
1650Sstevel@tonic-gate 			cs[csp++] = '\\'; cs[csp++] = '^';
1660Sstevel@tonic-gate 			cs[csp++] = 'f';
167364Sceastha 
168364Sceastha 			/* trying | instead of ^ */
169364Sceastha 			cs[csp++] = '\\'; cs[csp++] = '|';
170364Sceastha 
1710Sstevel@tonic-gate 			f = ITAL;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		else
1740Sstevel@tonic-gate 			cs[csp++] = 'f';
1750Sstevel@tonic-gate 		break;
1760Sstevel@tonic-gate 	case 'j':
1770Sstevel@tonic-gate 		if (ft == ITAL) {
1780Sstevel@tonic-gate 			cs[csp++] = '\\'; cs[csp++] = '^';
1790Sstevel@tonic-gate 			cs[csp++] = 'j';
1800Sstevel@tonic-gate 			f = ITAL;
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 		else
1830Sstevel@tonic-gate 			cs[csp++] = 'j';
1840Sstevel@tonic-gate 		break;
1850Sstevel@tonic-gate 	default:
1860Sstevel@tonic-gate 		cs[csp++] = c;
187364Sceastha 		f = ft == ITAL ? ITAL : ROM;
1880Sstevel@tonic-gate 		break;
1890Sstevel@tonic-gate 	}
190364Sceastha 	return (f);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
193364Sceastha void
shim(void)194364Sceastha shim(void)
195364Sceastha {
1960Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = '|';
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
199364Sceastha void
roman(int c)200364Sceastha roman(int c)
201364Sceastha {
2020Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = ROM;
2030Sstevel@tonic-gate 	cs[csp++] = c;
2040Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = 'P';
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
207364Sceastha void
name4(int c1,int c2)208364Sceastha name4(int c1, int c2)
209364Sceastha {
2100Sstevel@tonic-gate 	cs[csp++] = '\\';
2110Sstevel@tonic-gate 	cs[csp++] = '(';
2120Sstevel@tonic-gate 	cs[csp++] = c1;
2130Sstevel@tonic-gate 	cs[csp++] = c2;
2140Sstevel@tonic-gate }
215