1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 /*	from OpenSolaris "t6.c	1.3	05/06/02 SMI"	 SVr4.0 1.1		*/
17 
18 /*
19  * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
20  * Portions Copyright (c) 2015 Carsten Kunze
21  *
22  * Sccsid @(#)t6.c	1.6 (gritter) 2/8/06
23  */
24 
25  /* t6.c: compute tab stops */
26 # define tx(a) ((a)!=(char *)0 && !point((intptr_t)(a)))
27 # include "t..c"
28 # include <inttypes.h>
29 void
maktab(void)30 maktab(void)
31 {
32 # define FN(i,c) font[stynum[i]][c]
33 # define SZ(i,c) csize[stynum[i]][c]
34 	/* define the tab stops of the table */
35 	int icol, ilin, tsep, k, ik, vforml, il, text;
36 	int doubled[MAXCOL], acase[MAXCOL];
37 	char *s;
38 	char space[40];
39 	for(icol=0; icol <ncol; icol++)
40 	{
41 		doubled[icol] = acase[icol] = 0;
42 		fprintf(tabout, ".nr %d 0\n", icol+CRIGHT);
43 		for(text=0; text<2; text++)
44 		{
45 			if (text) {
46 				warnoff();
47 				fprintf(tabout, ".%02d\n.rm %02d\n", icol+80, icol+80);
48 				warnon();
49 			}
50 			for(ilin=0; ilin<nlin; ilin++)
51 			{
52 				if (instead[ilin]|| fullbot[ilin]) continue;
53 				vforml=ilin;
54 				for(il=prev(ilin); il>=0 && vspen(table[il][icol].col); il=prev(il))
55 					vforml=il;
56 				if (fspan(vforml,icol)) continue;
57 				if (filler(table[ilin][icol].col)) continue;
58 				switch(ctype(vforml,icol))
59 				{
60 				case 'a':
61 					acase[icol]=1;
62 					s = table[ilin][icol].col;
63 					if (tx(s) && text)
64 					{
65 						if (doubled[icol]==0)
66 							fprintf(tabout, ".nr %d 0\n.nr %d 0\n",S1,S2);
67 						doubled[icol]=1;
68 						nreg(space, sizeof(space), s,
69 						    '-');
70 						fprintf(tabout, ".if %s>\\n(%d .nr %d %s\n",space,S2,S2,space);
71 					}
72 				case 'n':
73 					if (table[ilin][icol].rcol!=0)
74 					{
75 						if (doubled[icol]==0 && text==0)
76 							fprintf(tabout, ".nr %d 0\n.nr %d 0\n", S1, S2);
77 						doubled[icol]=1;
78 						if (real(s=table[ilin][icol].col) && !vspen(s))
79 						{
80 							if (tx(s) != text) continue;
81 							fprintf(tabout, ".nr %d ", TMP);
82 							wide(s, FN(vforml,icol), SZ(vforml,icol)); fprintf(tabout, "\n");
83 							fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n", S1, TMP, S1, TMP);
84 						}
85 						if (text==0 && real(s=table[ilin][icol].rcol) && !vspen(s) && !barent(s))
86 						{
87 							fprintf(tabout, ".nr %d \\w%c%s%c\n",TMP, F1, s, F1);
88 							fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",S2,TMP,S2,TMP);
89 						}
90 						continue;
91 					}
92 				case 'r':
93 				case 'c':
94 				case 'l':
95 					if (real(s=table[ilin][icol].col) && !vspen(s))
96 					{
97 						if (tx(s) != text) continue;
98 						fprintf(tabout, ".nr %d ", TMP);
99 						wide(s, FN(vforml,icol), SZ(vforml,icol)); fprintf(tabout, "\n");
100 						fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n", icol+CRIGHT, TMP, icol+CRIGHT, TMP);
101 					}
102 				}
103 			}
104 		}
105 		if (acase[icol])
106 		{
107 			fprintf(tabout, ".if \\n(%d>=\\n(%d .nr %d \\n(%du+2n\n",S2,icol+CRIGHT,icol+CRIGHT,S2);
108 		}
109 		if (doubled[icol])
110 		{
111 			fprintf(tabout, ".nr %d \\n(%d\n", icol+CMID, S1);
112 			fprintf(tabout, ".nr %d \\n(%d+\\n(%d\n",TMP,icol+CMID,S2);
113 			fprintf(tabout, ".if \\n(%d>\\n(%d .nr %d \\n(%d\n",TMP,icol+CRIGHT,icol+CRIGHT,TMP);
114 			fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d +(\\n(%d-\\n(%d)/2\n",TMP,icol+CRIGHT,icol+CMID,icol+CRIGHT,TMP);
115 		}
116 		if (cll[icol][0])
117 		{
118 			fprintf(tabout, ".nr %d %sn\n", TMP, cll[icol]);
119 			fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",icol+CRIGHT, TMP, icol+CRIGHT, TMP);
120 		}
121 		for(ilin=0; ilin<nlin; ilin++)
122 			if ((k=lspan(ilin, icol)))
123 			{
124 				s=table[ilin][icol-k].col;
125 				if (!real(s) || barent(s) || vspen(s) ) continue;
126 				fprintf(tabout, ".nr %d ", TMP);
127 				wide(table[ilin][icol-k].col, FN(ilin,icol-k), SZ(ilin,icol-k));
128 				for(ik=k; ik>=0; ik--)
129 				{
130 					fprintf(tabout, "-\\n(%d",CRIGHT+icol-ik);
131 					if (!expflg && ik>0) fprintf(tabout, "-%dn", sep[icol-ik]);
132 				}
133 				fprintf(tabout, "\n");
134 				fprintf(tabout, ".if \\n(%d>0 .nr %d \\n(%d/%d\n", TMP, TMP, TMP, k);
135 				fprintf(tabout, ".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
136 				for(ik=0; ik<k; ik++)
137 				{
138 					if (doubled[icol-k+ik])
139 						fprintf(tabout, ".nr %d +\\n(%d/2\n", icol-k+ik+CMID, TMP);
140 					fprintf(tabout, ".nr %d +\\n(%d\n", icol-k+ik+CRIGHT, TMP);
141 				}
142 			}
143 	}
144 	if (textflg) untext();
145 	/* if even requested, make all columns widest width */
146 # define TMP1 S1
147 # define TMP2 S2
148 	if (evenflg)
149 	{
150 		fprintf(tabout, ".nr %d 0\n", TMP);
151 		for(icol=0; icol<ncol; icol++)
152 		{
153 			if (evenup[icol]==0) continue;
154 			fprintf(tabout, ".if \\n(%d>\\n(%d .nr %d \\n(%d\n",
155 			    icol+CRIGHT, TMP, TMP, icol+CRIGHT);
156 		}
157 		for(icol=0; icol<ncol; icol++)
158 		{
159 			if (evenup[icol]==0)
160 				/* if column not evened just retain old interval */
161 				continue;
162 			if (doubled[icol])
163 				fprintf(tabout, ".nr %d (100*\\n(%d/\\n(%d)*\\n(%d/100\n",
164 				    icol+CMID, icol+CMID, icol+CRIGHT, TMP);
165 				/* that nonsense with the 100's and parens tries
166 				   to avoid overflow while proportionally shifting
167 				   the middle of the number */
168 			fprintf(tabout, ".nr %d \\n(%d\n", icol+CRIGHT, TMP);
169 		}
170 	}
171 	/* now adjust for total table width */
172 	for(tsep=icol=0; icol<ncol; icol++)
173 		tsep+= sep[icol];
174 	if (expflg)
175 	{
176 		fprintf(tabout, ".nr %d 0", TMP);
177 		for(icol=0; icol<ncol; icol++)
178 			fprintf(tabout, "+\\n(%d", icol+CRIGHT);
179 		fprintf(tabout, "\n");
180 		fprintf(tabout, ".nr %d \\n(.l-\\n(.i-\\n(%d%s\n", TMP, TMP,
181 		    (utf8 || tlp) && (boxflg || dboxflg || allflg) ? "-1n" : "");
182 		if (boxflg || dboxflg || allflg)
183 			tsep += 1;
184 		else
185 			tsep -= sep[ncol-1];
186 		fprintf(tabout, ".nr %d \\n(%d/%d\n", TMP, TMP,  tsep);
187 		fprintf(tabout, ".if \\n(%d<1n .nr %d 1n\n", TMP, TMP);
188 	}
189 	else if (xcolflg) {
190 		fprintf(tabout, ".nr %d 0", TMP);
191 		for(icol=0; icol<ncol; icol++)
192 			fprintf(tabout, "+\\n(%d", icol+CRIGHT);
193 		fprintf(tabout, "\n");
194 		fprintf(tabout, ".nr %d \\n(.l-\\n(.i-\\n(%d-%dn/%d\n", TMP,
195 		    TMP, tsep + ((boxflg || dboxflg || allflg) ?
196 		    (utf8 || tlp) ? 2 : 1 : -1), xcolflg);
197 		for(icol=0; icol<ncol; icol++) {
198 			if (!xcol[icol]) continue;
199 			fprintf(tabout, ".nr %d +\\n(%d\n", icol+CRIGHT, TMP);
200 		}
201 		fprintf(tabout, ".nr %d 1n\n", TMP);
202 	}
203 	else
204 		fprintf(tabout, ".nr %d 1n\n", TMP);
205 	fprintf(tabout, ".nr %d 0\n",CRIGHT-1);
206 	tsep= (boxflg || allflg || dboxflg || left1flg) ? 1 : 0;
207 	for(icol=0; icol<ncol; icol++)
208 	{
209 		fprintf(tabout, ".nr %d \\n(%d+(%d*\\n(%d)\n",icol+CLEFT, icol+CRIGHT-1, tsep, TMP);
210 		fprintf(tabout, ".nr %d +\\n(%d\n",icol+CRIGHT, icol+CLEFT);
211 		if (doubled[icol])
212 		{
213 			/* the next line is last-ditch effort to avoid zero field width */
214 			/*fprintf(tabout, ".if \\n(%d=0 .nr %d 1\n",icol+CMID, icol+CMID);*/
215 			fprintf(tabout, ".nr %d +\\n(%d\n", icol+CMID, icol+CLEFT);
216 			/*  fprintf(tabout, ".if n .if \\n(%d%%24>0 .nr %d +12u\n",icol+CMID, icol+CMID); */
217 		}
218 		tsep=sep[icol];
219 	}
220 	if (rightl)
221 		fprintf(tabout, ".nr %d (\\n(%d+\\n(%d)/2\n",ncol+CRIGHT-1, ncol+CLEFT-1, ncol+CRIGHT-2);
222 	fprintf(tabout, ".nr TW \\n(%d\n", ncol+CRIGHT-1);
223 	if (boxflg || allflg || dboxflg)
224 		fprintf(tabout, ".nr TW +%d*\\n(%d\n", sep[ncol-1], TMP);
225 	fprintf(tabout,
226 	    ".if t .if \\n(TW>\\n(.l .tm Table at line %d file %s is too wide - \\n(TW units\n", iline-1, ifile);
227 	return;
228 }
229 void
wide(char * s,char * fn,char * size)230 wide(char *s, char *fn, char *size)
231 {
232 	char space[40];
233 	if (point((intptr_t)s))
234 	{
235 		fprintf(tabout, "\\w%c", F1);
236 		if (*fn>0) putfont(fn);
237 		if (*size) putsize(size);
238 		fprintf(tabout, "%s", s);
239 		if (*fn>0) putfont("P");
240 		if (*size) putsize("0");
241 		fprintf(tabout, "%c",F1);
242 	}
243 	else
244 		fprintf(tabout, "%s", nreg(space, sizeof(space), s, '-'));
245 }
246 int
filler(char * s)247 filler(char *s)
248 {
249 return (point((intptr_t)s) && s[0]=='\\' && s[1] == 'R');
250 }
251