xref: /illumos-gate/usr/src/cmd/eqn/paren.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #include "e.h"
16 
17 extern int max();
18 
19 void brack(int, char *, char *, char *);
20 
21 void
22 paren(int leftc, int p1, int rightc)
23 {
24 	int n, m, h1, j, b1, v;
25 	h1 = eht[p1]; b1 = ebase[p1];
26 	yyval = p1;
27 #ifndef NEQN
28 	lfont[yyval] = rfont[yyval] = 0;
29 	n = (h1 + EM(1.0, EFFPS(ps)) - 1) / EM(1.0, EFFPS(ps));
30 #else	/* NEQN */
31 	n = max(b1+VERT(1), h1-b1-VERT(1)) / VERT(1);
32 #endif	/* NEQN */
33 	if (n < 2) n = 1;
34 	m = n-2;
35 	if (leftc == '{' || rightc == '}') {
36 		if ((n % 2) == 0) {
37 			n++;
38 		}
39 		if (n < 3) n = 3;
40 		m = n-3;
41 	}
42 #ifndef NEQN
43 	eht[yyval] = VERT(EM(n, ps));
44 	ebase[yyval] = b1 + (eht[yyval]-h1)/2;
45 	v = b1 - h1/2 + VERT(EM(0.4, ps));
46 #else	/* NEQN */
47 	eht[yyval] = VERT(2 * n);
48 	ebase[yyval] = (n)/2 * VERT(2);
49 	if (n%2 == 0)
50 		ebase[yyval] -= VERT(1);
51 	v = b1 - h1/2 + VERT(1);
52 #endif	/* NEQN */
53 	printf(".ds %d \\|\\v'%du'", yyval, v);
54 	switch (leftc) {
55 		case 'n':	/* nothing */
56 		case '\0':
57 			break;
58 		case 'f':	/* floor */
59 			if (n <= 1)
60 				printf("\\(lf");
61 			else
62 				brack(m, "\\(bv", "\\(bv", "\\(lf");
63 			break;
64 		case 'c':	/* ceiling */
65 			if (n <= 1)
66 				printf("\\(lc");
67 			else
68 				brack(m, "\\(lc", "\\(bv", "\\(bv");
69 			break;
70 		case '{':
71 			printf("\\b'\\(lt");
72 			for (j = 0; j < m; j += 2) printf("\\(bv");
73 			printf("\\(lk");
74 			for (j = 0; j < m; j += 2) printf("\\(bv");
75 			printf("\\(lb'");
76 			break;
77 		case '(':
78 			brack(m, "\\(lt", "\\(bv", "\\(lb");
79 			break;
80 		case '[':
81 			brack(m, "\\(lc", "\\(bv", "\\(lf");
82 			break;
83 		case '|':
84 			brack(m, "\\(bv", "\\(bv", "\\(bv");
85 			break;
86 		default:
87 			brack(m, (char *)&leftc, (char *)&leftc,
88 			    (char *)&leftc);
89 			break;
90 		}
91 	printf("\\v'%du'\\*(%d", -v, p1);
92 	if (rightc) {
93 		printf("\\|\\v'%du'", v);
94 		switch (rightc) {
95 			case 'f':	/* floor */
96 				if (n <= 1)
97 					printf("\\(rf");
98 				else
99 					brack(m, "\\(bv", "\\(bv", "\\(rf");
100 				break;
101 			case 'c':	/* ceiling */
102 				if (n <= 1)
103 					printf("\\(rc");
104 				else
105 					brack(m, "\\(rc", "\\(bv", "\\(bv");
106 				break;
107 			case '}':
108 				printf("\\b'\\(rt");
109 				for (j = 0; j < m; j += 2) printf("\\(bv");
110 				printf("\\(rk");
111 				for (j = 0; j < m; j += 2) printf("\\(bv");
112 				printf("\\(rb'");
113 				break;
114 			case ']':
115 				brack(m, "\\(rc", "\\(bv", "\\(rf");
116 				break;
117 			case ')':
118 				brack(m, "\\(rt", "\\(bv", "\\(rb");
119 				break;
120 			case '|':
121 				brack(m, "\\(bv", "\\(bv", "\\(bv");
122 				break;
123 			default:
124 				brack(m, (char *)&rightc, (char *)&rightc,
125 				    (char *)&rightc);
126 				break;
127 		}
128 		printf("\\v'%du'", -v);
129 	}
130 	printf("\n");
131 	if (dbg)
132 		printf(".\tcurly: h=%d b=%d n=%d v=%d l=%c, r=%c\n",
133 		    eht[yyval], ebase[yyval], n, v, leftc, rightc);
134 }
135 
136 void
137 brack(int m, char *t, char *c, char *b)
138 {
139 	int j;
140 	printf("\\b'%s", t);
141 	for (j = 0; j < m; j++)
142 		printf("%s", c);
143 	printf("%s'", b);
144 }
145