xref: /illumos-gate/usr/src/cmd/tbl/t8.c (revision c7a0678d)
1 /*
2  * Copyright 1996 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  /* t8.c: write out one line of output table */
16 # include "t..c"
17 # include <locale.h>
18 # define realsplit ((ct=='a'||ct=='n') && table[nl][c].rcol)
19 
20 int watchout;
21 int once;
22 int topat[MAXCOL];
23 
24 void	puttext(char *, char *, char *);
25 void	funnies(int, int);
26 void	putfont(char *);
27 void	putsize(char *);
28 
29 /*
30  * parameters
31  *
32  * i:	line number for deciding format
33  * nl:	line number for finding data   usually identical
34  */
35 void
36 putline(int i, int nl)
37 {
38 int c, lf, ct, form, lwid, vspf, ip = -1, cmidx, exvspen, vforml;
39 int vct, chfont;
40 char *s, *size, *fn;
41 watchout=vspf=exvspen=0;
42 if (i==0) once=0;
43 if (i==0 && ( allflg || boxflg || dboxflg))
44 	fullwide(0,   dboxflg? '=' : '-');
45 if (instead[nl]==0 && fullbot[nl] ==0)
46 for(c=0; c<ncol; c++)
47 	{
48 	s = table[nl][c].col;
49 	if (s==0) continue;
50 	if (vspen(s))
51 		{
52 		for(ip=nl; ip<nlin; ip=next(ip))
53 			if (!vspen(s=table[ip][c].col)) break;
54 		if (s>(char *)0 && s<(char *)128)
55 		fprintf(tabout, ".ne \\n(%c|u+\\n(.Vu\n",s);
56 		continue;
57 		}
58 	if (point(s)) continue;
59 	fprintf(tabout, ".ne \\n(%c|u+\\n(.Vu\n",s);
60 	watchout=1;
61 	}
62 if (linestop[nl])
63 	fprintf(tabout, ".mk #%c\n", linestop[nl]+'a'-1);
64 lf = prev(nl);
65 if (instead[nl])
66 	{
67 	puts(instead[nl]);
68 	return;
69 	}
70 if (fullbot[nl])
71 	{
72 	switch (ct=fullbot[nl])
73 		{
74 		case '=':
75 		case '-':
76 			fullwide(nl,ct);
77 		}
78 	return;
79 	}
80 for(c=0; c<ncol; c++)
81 	{
82 	if (instead[nl]==0 && fullbot[nl]==0)
83 	if (vspen(table[nl][c].col)) vspf=1;
84 	if (lf>=0)
85 		if (vspen(table[lf][c].col)) vspf=1;
86 	}
87 if (vspf)
88 	{
89 	fprintf(tabout, ".nr #^ \\n(\\*(#du\n");
90 	fprintf(tabout, ".nr #- \\n(#^\n"); /* current line position relative to bottom */
91 	}
92 vspf=0;
93 chfont=0;
94 for(c=0; c<ncol; c++)
95 	{
96 	s = table[nl][c].col;
97 	if (s==0) continue;
98 	chfont |= (int)(font[stynum[nl]][c]);
99 	if (point(s) ) continue;
100 	lf=prev(nl);
101 	if (lf>=0 && vspen(table[lf][c].col))
102 		fprintf(tabout, ".if (\\n(%c|+\\n(^%c-1v)>\\n(#- .nr #- +(\\n(%c|+\\n(^%c-\\n(#--1v)\n",s,'a'+c,s,'a'+c);
103 	else
104 		fprintf(tabout, ".if (\\n(%c|+\\n(#^-1v)>\\n(#- .nr #- +(\\n(%c|+\\n(#^-\\n(#--1v)\n",s,s);
105 	}
106 if (allflg && once>0 )
107 	fullwide(i,'-');
108 once=1;
109 runtabs(i, nl);
110 if (allh(nl) && !pr1403)
111 	{
112 	fprintf(tabout, ".nr %d \\n(.v\n", SVS);
113 	fprintf(tabout, ".vs \\n(.vu-\\n(.sp\n");
114 	}
115 if (chfont)
116 	fprintf(tabout, ".nr %2d \\n(.f\n", S1);
117 fprintf(tabout, ".nr 35 1m\n");
118 fprintf(tabout, "\\&");
119 vct = 0;
120 for(c=0; c<ncol; c++)
121 	{
122 	if (watchout==0 && i+1<nlin && (lf=left(i,c, &lwid))>=0)
123 		{
124 		tohcol(c);
125 		drawvert(lf, i, c, lwid);
126 		vct += 2;
127 		}
128 	if (rightl && c+1==ncol) continue;
129 	vforml=i;
130 	for(lf=prev(nl); lf>=0 && vspen(table[lf][c].col); lf=prev(lf))
131 		vforml= lf;
132 	form= ctype(vforml,c);
133 	if (form != 's')
134 		{
135 		ct = c+CLEFT;
136 		if (form=='a') ct = c+CMID;
137 		if (form=='n' && table[nl][c].rcol && lused[c]==0) ct= c+CMID;
138 		fprintf(tabout, "\\h'|\\n(%du'", ct);
139 		}
140 	s= table[nl][c].col;
141 	fn = font[stynum[vforml]][c];
142 	size = csize[stynum[vforml]][c];
143 	if (*size==0)size=0;
144 	switch(ct=ctype(vforml, c))
145 		{
146 		case 'n':
147 		case 'a':
148 			if (table[nl][c].rcol)
149 				{
150 			   if (lused[c]) /*Zero field width*/
151 				{
152 				ip = prev(nl);
153 				if (ip>=0)
154 				if (vspen(table[ip][c].col))
155 					{
156 					if (exvspen==0)
157 						{
158 						fprintf(tabout, "\\v'-(\\n(\\*(#du-\\n(^%cu", c+'a');
159 						if (cmidx)
160 							fprintf(tabout, "-((\\n(#-u-\\n(^%cu)/2u)", c+'a');
161 						vct++;
162 						fprintf(tabout, "'");
163 						exvspen=1;
164 						}
165 					}
166 				fprintf(tabout, "%c%c",F1,F2);
167 				puttext(s,fn,size);
168 				fprintf(tabout, "%c",F1);
169 				}
170 				s= table[nl][c].rcol;
171 				form=1;
172 				break;
173 				}
174 			/* FALLTHROUGH */
175 		case 'c':
176 			form=3; break;
177 		case 'r':
178 			form=2; break;
179 		case 'l':
180 			form=1; break;
181 		case '-':
182 		case '=':
183 			if (real(table[nl][c].col))
184 				fprintf(stderr,gettext("%s: line %d: Data ignored on table line %d\n"), ifile, iline-1, i+1);
185 			makeline(i,c,ct);
186 			continue;
187 		default:
188 			continue;
189 		}
190 	if (realsplit ? rused[c]: used[c]) /*Zero field width*/
191 		{
192 		/* form: 1 left, 2 right, 3 center adjust */
193 		if (ifline(s))
194 			{
195 			makeline(i,c,ifline(s));
196 			continue;
197 			}
198 		if (filler(s))
199 			{
200 			printf("\\l'|\\n(%du\\&%s'", c+CRIGHT, s+2);
201 			continue;
202 			}
203 		ip = prev(nl);
204 		cmidx = ctop[stynum[nl]][c]==0;
205 		if (ip>=0)
206 		if (vspen(table[ip][c].col))
207 			{
208 			if (exvspen==0)
209 				{
210 				fprintf(tabout, "\\v'-(\\n(\\*(#du-\\n(^%cu", c+'a');
211 				if (cmidx)
212 					fprintf(tabout, "-((\\n(#-u-\\n(^%cu)/2u)", c+'a');
213 				vct++;
214 				fprintf(tabout, "'");
215 				}
216 			}
217 		fprintf(tabout, "%c", F1);
218 		if (form!= 1)
219 			fprintf(tabout, "%c", F2);
220 		if (vspen(s))
221 			vspf=1;
222 		else
223 		puttext(s, fn, size);
224 		if (form !=2)
225 			fprintf(tabout, "%c", F2);
226 		fprintf(tabout, "%c", F1);
227 		}
228 	if (ip>=0)
229 	if (vspen(table[ip][c].col))
230 		{
231 		exvspen = (c+1 < ncol) && vspen(table[ip][c+1].col) &&
232 			(topat[c] == topat[c+1]) &&
233 			(cmidx == (ctop [stynum[nl]][c+1]==0)) && (left(i,c+1,&lwid)<0);
234 		if (exvspen==0)
235 			{
236 			fprintf(tabout, "\\v'(\\n(\\*(#du-\\n(^%cu", c+'a');
237 			if (cmidx)
238 				fprintf(tabout, "-((\\n(#-u-\\n(^%cu)/2u)", c+'a');
239 			vct++;
240 			fprintf(tabout, "'");
241 			}
242 		}
243 	else
244 		exvspen=0;
245 	/* if lines need to be split for gcos here is the place for a backslash */
246 	if (vct > 7 && c < ncol)
247 		{
248 		fprintf(tabout, "\n.sp-1\n\\&");
249 		vct=0;
250 		}
251 	}
252 fprintf(tabout, "\n");
253 if (allh(nl) && !pr1403) fprintf(tabout, ".vs \\n(%du\n", SVS);
254 if (watchout)
255 	funnies(i,nl);
256 if (vspf)
257 	{
258 	for(c=0; c<ncol; c++)
259 		if (vspen(table[nl][c].col) && (nl==0 || (lf=prev(nl))<0 || !vspen(table[lf][c].col)))
260 			{
261 			fprintf(tabout, ".nr ^%c \\n(#^u\n", 'a'+c);
262 			topat[c]=nl;
263 			}
264 	}
265 }
266 
267 void
268 puttext(char *s, char *fn, char *size)
269 {
270 if (point(s))
271 	{
272 	putfont(fn);
273 	putsize(size);
274 	fprintf(tabout, "%s",s);
275 	if (*fn>0) fprintf(tabout, "\\f\\n(%2d", S1);
276 	if (size!=0) putsize("0");
277 	}
278 }
279 
280 void
281 funnies(int stl, int lin)
282 {
283 /* write out funny diverted things */
284 int c, s, pl, lwid, dv, lf, ct;
285 char *fn;
286 fprintf(tabout, ".mk ##\n"); /* rmember current vertical position */
287 fprintf(tabout, ".nr %d \\n(##\n", S1); /* bottom position */
288 for(c=0; c<ncol; c++)
289 	{
290 	s = (int)table[lin][c].col;
291 	if (point(s)) continue;
292 	if (s==0) continue;
293 	fprintf(tabout, ".sp |\\n(##u-1v\n");
294 	fprintf(tabout, ".nr %d ", SIND);
295 	for(pl=stl; pl>=0 && !isalpha(ct=ctype(pl,c)); pl=prev(pl))
296 		;
297 	switch (ct)
298 		{
299 		case 'n':
300 		case 'c':
301 			fprintf(tabout, "(\\n(%du+\\n(%du-\\n(%c-u)/2u\n",c+CLEFT,c-1+ctspan(lin,c)+CRIGHT, s);
302 			break;
303 		case 'l':
304 			fprintf(tabout, "\\n(%du\n",c+CLEFT);
305 			break;
306 		case 'a':
307 			fprintf(tabout, "\\n(%du\n",c+CMID);
308 			break;
309 		case 'r':
310 			fprintf(tabout, "\\n(%du-\\n(%c-u\n", c+CRIGHT, s);
311 			break;
312 		}
313 	fprintf(tabout, ".in +\\n(%du\n", SIND);
314 	fn=font[stynum[stl]][c];
315 	putfont(fn);
316 	pl = prev(stl);
317 	if (stl>0 && pl>=0 && vspen(table[pl][c].col))
318 		{
319 		fprintf(tabout, ".sp |\\n(^%cu\n", 'a'+c);
320 		if (ctop[stynum[stl]][c]==0)
321 			{
322 			fprintf(tabout, ".nr %d \\n(#-u-\\n(^%c-\\n(%c|+1v\n",TMP, 'a'+c, s);
323 			fprintf(tabout, ".if \\n(%d>0 .sp \\n(%du/2u\n", TMP, TMP);
324 			}
325 		}
326 	fprintf(tabout, ".%c+\n",s);
327 	fprintf(tabout, ".in -\\n(%du\n", SIND);
328 	if (*fn>0) putfont("P");
329 	fprintf(tabout, ".mk %d\n", S2);
330 	fprintf(tabout, ".if \\n(%d>\\n(%d .nr %d \\n(%d\n", S2, S1, S1, S2);
331 	}
332 fprintf(tabout, ".sp |\\n(%du\n", S1);
333 for(c=dv=0; c<ncol; c++)
334 	{
335 	if (stl+1< nlin && (lf=left(stl,c,&lwid))>=0)
336 		{
337 		if (dv++ == 0)
338 			fprintf(tabout, ".sp -1\n");
339 		tohcol(c);
340 		dv++;
341 		drawvert(lf, stl, c, lwid);
342 		}
343 	}
344 if (dv)
345 	fprintf(tabout,"\n");
346 }
347 
348 void
349 putfont(char *fn)
350 {
351 if (fn && *fn)
352 	fprintf(tabout,  fn[1] ? "\\f(%.2s" : "\\f%.2s",  fn);
353 }
354 
355 void
356 putsize(char *s)
357 {
358 if (s && *s)
359 	fprintf(tabout, "\\s%s",s);
360 }
361