xref: /illumos-gate/usr/src/cmd/eqn/text.c (revision 2a8bcb4e)
1779fc935Sceastha /*
2779fc935Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3779fc935Sceastha  * Use is subject to license terms.
4779fc935Sceastha  */
5779fc935Sceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
15779fc935Sceastha #include "e.h"
16779fc935Sceastha #include "e.def"
177c478bd9Sstevel@tonic-gate #include <locale.h>
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate int	csp;
207c478bd9Sstevel@tonic-gate int	psp;
217c478bd9Sstevel@tonic-gate #define	CSSIZE	400
227c478bd9Sstevel@tonic-gate char	cs[420];
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate int	lf, rf;	/* temporary spots for left and right fonts */
257c478bd9Sstevel@tonic-gate 
26779fc935Sceastha void name4(int, int);
27779fc935Sceastha void roman(int);
28779fc935Sceastha void shim(void);
29779fc935Sceastha int trans(int, char *);
30779fc935Sceastha 
31779fc935Sceastha void
text(int t,char * p1)32779fc935Sceastha text(int t, char *p1)
33779fc935Sceastha {
347c478bd9Sstevel@tonic-gate 	int c;
357c478bd9Sstevel@tonic-gate 	char *p;
367c478bd9Sstevel@tonic-gate 	tbl *tp, *lookup();
37*b5cda42eSceastha 	extern tbl *restbl[];
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	yyval = oalloc();
407c478bd9Sstevel@tonic-gate 	ebase[yyval] = 0;
417c478bd9Sstevel@tonic-gate #ifndef NEQN
427c478bd9Sstevel@tonic-gate 	eht[yyval] = VERT(EM(1.0, EFFPS(ps)));	/* ht in machine units */
43779fc935Sceastha #else	/* NEQN */
447c478bd9Sstevel@tonic-gate 	eht[yyval] = VERT(2);	/* 2 half-spaces */
45779fc935Sceastha #endif	/* NEQN */
467c478bd9Sstevel@tonic-gate 	lfont[yyval] = rfont[yyval] = ROM;
477c478bd9Sstevel@tonic-gate 	if (t == QTEXT)
487c478bd9Sstevel@tonic-gate 		p = p1;
49779fc935Sceastha 	else if (t == SPACE)
507c478bd9Sstevel@tonic-gate 		p = "\\ ";
51779fc935Sceastha 	else if (t == THIN)
527c478bd9Sstevel@tonic-gate 		p = "\\|";
53779fc935Sceastha 	else if (t == TAB)
547c478bd9Sstevel@tonic-gate 		p = "\\t";
55*b5cda42eSceastha 	else if ((tp = lookup(restbl, p1, NULL)) != NULL)
567c478bd9Sstevel@tonic-gate 		p = tp->defn;
577c478bd9Sstevel@tonic-gate 	else {
587c478bd9Sstevel@tonic-gate 		lf = rf = 0;
59779fc935Sceastha 		for (csp = psp = 0; (c = p1[psp++]) != '\0'; ) {
607c478bd9Sstevel@tonic-gate 			rf = trans(c, p1);
617c478bd9Sstevel@tonic-gate 			if (lf == 0)
627c478bd9Sstevel@tonic-gate 				lf = rf;	/* save first */
63779fc935Sceastha 			if (csp > CSSIZE)
64779fc935Sceastha 				error(FATAL, gettext(
65779fc935Sceastha 				    "converted token %.25s... too long"), p1);
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 		cs[csp] = '\0';
687c478bd9Sstevel@tonic-gate 		p = cs;
697c478bd9Sstevel@tonic-gate 		lfont[yyval] = lf;
707c478bd9Sstevel@tonic-gate 		rfont[yyval] = rf;
717c478bd9Sstevel@tonic-gate 	}
72779fc935Sceastha 	if (dbg)
73779fc935Sceastha 		printf(".\t%dtext: S%d <- %s; b=%d,h=%d,lf=%c,rf=%c\n",
74779fc935Sceastha 		    t, yyval, p, ebase[yyval], eht[yyval], lfont[yyval],
75779fc935Sceastha 		    rfont[yyval]);
767c478bd9Sstevel@tonic-gate 	printf(".ds %d \"%s\n", yyval, p);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
79779fc935Sceastha int
trans(int c,char * p1)80779fc935Sceastha trans(int c, char *p1)
81779fc935Sceastha {
827c478bd9Sstevel@tonic-gate 	int f;
837c478bd9Sstevel@tonic-gate 	f = ROM;
84779fc935Sceastha 	switch (c) {
857c478bd9Sstevel@tonic-gate 	case '0': case '1': case '2': case '3': case '4':
867c478bd9Sstevel@tonic-gate 	case '5': case '6': case '7': case '8': case '9':
877c478bd9Sstevel@tonic-gate 	case ':': case ';': case '!': case '%':
887c478bd9Sstevel@tonic-gate 	case '(': case '[': case ')': case ']':
897c478bd9Sstevel@tonic-gate 	case ',':
907c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
917c478bd9Sstevel@tonic-gate 			shim();
927c478bd9Sstevel@tonic-gate 		roman(c); break;
937c478bd9Sstevel@tonic-gate 	case '.':
947c478bd9Sstevel@tonic-gate 		if (rf == ROM)
957c478bd9Sstevel@tonic-gate 			roman(c);
967c478bd9Sstevel@tonic-gate 		else
977c478bd9Sstevel@tonic-gate 			cs[csp++] = c;
987c478bd9Sstevel@tonic-gate 		f = rf;
997c478bd9Sstevel@tonic-gate 		break;
1007c478bd9Sstevel@tonic-gate 	case '|':
1017c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1027c478bd9Sstevel@tonic-gate 			shim();
1037c478bd9Sstevel@tonic-gate 		shim(); roman(c); shim(); break;
1047c478bd9Sstevel@tonic-gate 	case '=':
1057c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1067c478bd9Sstevel@tonic-gate 			shim();
107779fc935Sceastha 		name4('e', 'q');
1087c478bd9Sstevel@tonic-gate 		break;
1097c478bd9Sstevel@tonic-gate 	case '+':
1107c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1117c478bd9Sstevel@tonic-gate 			shim();
1127c478bd9Sstevel@tonic-gate 		name4('p', 'l');
1137c478bd9Sstevel@tonic-gate 		break;
1147c478bd9Sstevel@tonic-gate 	case '>': case '<':
1157c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1167c478bd9Sstevel@tonic-gate 			shim();
117779fc935Sceastha 		if (p1[psp] == '=') {	/* look ahead for == <= >= */
118779fc935Sceastha 			name4(c, '=');
1197c478bd9Sstevel@tonic-gate 			psp++;
1207c478bd9Sstevel@tonic-gate 		} else {
121779fc935Sceastha 			cs[csp++] = c;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 	case '-':
1257c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1267c478bd9Sstevel@tonic-gate 			shim();
127779fc935Sceastha 		if (p1[psp] == '>') {
128779fc935Sceastha 			name4('-', '>'); psp++;
1297c478bd9Sstevel@tonic-gate 		} else {
130779fc935Sceastha 			name4('m', 'i');
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		break;
1337c478bd9Sstevel@tonic-gate 	case '/':
1347c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1357c478bd9Sstevel@tonic-gate 			shim();
136779fc935Sceastha 		name4('s', 'l');
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 	case '~': case ' ':
1397c478bd9Sstevel@tonic-gate 		shim(); shim(); break;
1407c478bd9Sstevel@tonic-gate 	case '^':
1417c478bd9Sstevel@tonic-gate 		shim(); break;
1427c478bd9Sstevel@tonic-gate 	case '\\':	/* troff - pass 2 or 3 more chars */
1437c478bd9Sstevel@tonic-gate 		if (rf == ITAL)
1447c478bd9Sstevel@tonic-gate 			shim();
1457c478bd9Sstevel@tonic-gate 		cs[csp++] = c; cs[csp++] = c = p1[psp++]; cs[csp++] = p1[psp++];
146779fc935Sceastha 		if (c == '(') cs[csp++] = p1[psp++];
147779fc935Sceastha 		if (c == '*' && cs[csp-1] == '(') {
1487c478bd9Sstevel@tonic-gate 			cs[csp++] = p1[psp++];
1497c478bd9Sstevel@tonic-gate 			cs[csp++] = p1[psp++];
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		break;
1527c478bd9Sstevel@tonic-gate 	case '\'':
153779fc935Sceastha 		cs[csp++] = '\\';
154779fc935Sceastha 		cs[csp++] = 'f';
155779fc935Sceastha 		cs[csp++] = rf == ITAL ? ITAL : ROM;
156779fc935Sceastha 		name4('f', 'm');
1577c478bd9Sstevel@tonic-gate 		cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = 'P';
158779fc935Sceastha 		f = rf == ITAL ? ITAL : ROM;
1597c478bd9Sstevel@tonic-gate 		break;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	case 'f':
1627c478bd9Sstevel@tonic-gate 		if (ft == ITAL) {
1637c478bd9Sstevel@tonic-gate 			cs[csp++] = '\\'; cs[csp++] = '^';
1647c478bd9Sstevel@tonic-gate 			cs[csp++] = 'f';
165779fc935Sceastha 
166779fc935Sceastha 			/* trying | instead of ^ */
167779fc935Sceastha 			cs[csp++] = '\\'; cs[csp++] = '|';
168779fc935Sceastha 
1697c478bd9Sstevel@tonic-gate 			f = ITAL;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 		else
1727c478bd9Sstevel@tonic-gate 			cs[csp++] = 'f';
1737c478bd9Sstevel@tonic-gate 		break;
1747c478bd9Sstevel@tonic-gate 	case 'j':
1757c478bd9Sstevel@tonic-gate 		if (ft == ITAL) {
1767c478bd9Sstevel@tonic-gate 			cs[csp++] = '\\'; cs[csp++] = '^';
1777c478bd9Sstevel@tonic-gate 			cs[csp++] = 'j';
1787c478bd9Sstevel@tonic-gate 			f = ITAL;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		else
1817c478bd9Sstevel@tonic-gate 			cs[csp++] = 'j';
1827c478bd9Sstevel@tonic-gate 		break;
1837c478bd9Sstevel@tonic-gate 	default:
1847c478bd9Sstevel@tonic-gate 		cs[csp++] = c;
185779fc935Sceastha 		f = ft == ITAL ? ITAL : ROM;
1867c478bd9Sstevel@tonic-gate 		break;
1877c478bd9Sstevel@tonic-gate 	}
188779fc935Sceastha 	return (f);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
191779fc935Sceastha void
shim(void)192779fc935Sceastha shim(void)
193779fc935Sceastha {
1947c478bd9Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = '|';
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
197779fc935Sceastha void
roman(int c)198779fc935Sceastha roman(int c)
199779fc935Sceastha {
2007c478bd9Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = ROM;
2017c478bd9Sstevel@tonic-gate 	cs[csp++] = c;
2027c478bd9Sstevel@tonic-gate 	cs[csp++] = '\\'; cs[csp++] = 'f'; cs[csp++] = 'P';
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
205779fc935Sceastha void
name4(int c1,int c2)206779fc935Sceastha name4(int c1, int c2)
207779fc935Sceastha {
2087c478bd9Sstevel@tonic-gate 	cs[csp++] = '\\';
2097c478bd9Sstevel@tonic-gate 	cs[csp++] = '(';
2107c478bd9Sstevel@tonic-gate 	cs[csp++] = c1;
2117c478bd9Sstevel@tonic-gate 	cs[csp++] = c2;
2127c478bd9Sstevel@tonic-gate }
213