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