xref: /titanic_52/usr/src/cmd/tbl/t5.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * Copyright 2003 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  /* t5.c: read data for table */
18 # include "t..c"
19 
20 void	permute(void);
21 
22 void
23 gettbl(void)
24 {
25 int icol, ch;
26 cstore=cspace= chspace();
27 textflg=0;
28 for (nlin=nslin=0; gets1(cstore, MAXSTR); nlin++)
29 	{
30 	stynum[nlin]=nslin;
31 	if (prefix(".TE", cstore))
32 		{
33 		leftover=0;
34 		break;
35 		}
36 	if (prefix(".TC", cstore) || prefix(".T&", cstore))
37 		{
38 		readspec();
39 		nslin++;
40 		}
41 	if (nlin>=MAXLIN)
42 		{
43 		leftover=cstore;
44 		break;
45 		}
46 	fullbot[nlin]=0;
47 	if (cstore[0] == '.' && !isdigit((unsigned char)cstore[1]))
48 		{
49 		instead[nlin] = cstore;
50 		while (*cstore++);
51 		continue;
52 		}
53 	else instead[nlin] = 0;
54 	if (nodata(nlin))
55 		{
56 		if (ch = oneh(nlin))
57 			fullbot[nlin]= ch;
58 		nlin++;
59 		nslin++;
60 		instead[nlin]=(char *)0;
61 		fullbot[nlin]=0;
62 		}
63 	table[nlin] = (struct colstr *) alocv((ncol+2)*sizeof(table[0][0]));
64 	if (cstore[1]==0)
65 	switch(cstore[0])
66 		{
67 		case '_': fullbot[nlin]= '-'; continue;
68 		case '=': fullbot[nlin]= '='; continue;
69 		}
70 	stynum[nlin] = nslin;
71 	nslin = min(nslin+1, nclin-1);
72 	for (icol = 0; icol <ncol; icol++)
73 		{
74 		table[nlin][icol].col = cstore;
75 		table[nlin][icol].rcol=0;
76 		ch=1;
77 		if (match(cstore, "T{")) /* text follows */
78 			/* get_text was originally gettext and was renamed */
79 			table[nlin][icol].col =
80 				(char *)get_text(cstore, nlin, icol,
81 					font[stynum[nlin]][icol],
82 					csize[stynum[nlin]][icol]);
83 		else
84 			{
85 			for(; (ch= *cstore) != '\0' && ch != tab; cstore++)
86 					;
87 			*cstore++ = '\0';
88 			switch(ctype(nlin,icol)) /* numerical or alpha, subcol */
89 				{
90 				case 'n':
91 					table[nlin][icol].rcol =
92 					    (char *)maknew(table[nlin][icol].col);
93 					break;
94 				case 'a':
95 					table[nlin][icol].rcol = table[nlin][icol].col;
96 					table[nlin][icol].col = "";
97 					break;
98 				}
99 			}
100 		while (ctype(nlin,icol+1)== 's') /* spanning */
101 			table[nlin][++icol].col = "";
102 		if (ch == '\0') break;
103 		}
104 	while (++icol <ncol+2)
105 		{
106 		table[nlin][icol].col = "";
107 		table [nlin][icol].rcol=0;
108 		}
109 	while (*cstore != '\0')
110 		 cstore++;
111 	if (cstore-cspace > MAXCHS)
112 		cstore = cspace = chspace();
113 	}
114 last = cstore;
115 permute();
116 if (textflg) untext();
117 return;
118 }
119 
120 int
121 nodata(int il)
122 {
123 int c;
124 for (c=0; c<ncol;c++)
125 	{
126 	switch(ctype(il,c))
127 		{
128 		case 'c': case 'n': case 'r': case 'l': case 's': case 'a':
129 			return(0);
130 		}
131 	}
132 return(1);
133 }
134 
135 int
136 oneh(int lin)
137 {
138 int k, icol;
139 k = ctype(lin,0);
140 for(icol=1; icol<ncol; icol++)
141 	{
142 	if (k != ctype(lin,icol))
143 		return(0);
144 	}
145 return(k);
146 }
147 
148 # define SPAN "\\^"
149 
150 void
151 permute(void)
152 {
153 int irow, jcol, is;
154 char *start, *strig;
155 for(jcol=0; jcol<ncol; jcol++)
156 	{
157 	for(irow=1; irow<nlin; irow++)
158 		{
159 		if (vspand(irow,jcol,0))
160 			{
161 			is = prev(irow);
162 			if (is<0)
163 				error(gettext("Vertical spanning in first row not allowed"));
164 			start = table[is][jcol].col;
165 			strig = table[is][jcol].rcol;
166 			while (irow<nlin &&vspand(irow,jcol,0))
167 				irow++;
168 			table[--irow][jcol].col = start;
169 			table[irow][jcol].rcol = strig;
170 			while (is<irow)
171 				{
172 				table[is][jcol].rcol =0;
173 				table[is][jcol].col= SPAN;
174 				is = next(is);
175 				}
176 			}
177 		}
178 	}
179 }
180 
181 int
182 vspand(int ir, int ij, int ifform)
183 {
184 if (ir<0) return(0);
185 if (ir>=nlin)return(0);
186 if (instead[ir]) return(0);
187 if (ifform==0 && ctype(ir,ij)=='^') return(1);
188 if (table[ir]==0) return(0);
189 if (table[ir][ij].rcol!=0) return(0);
190 if (fullbot[ir]) return(0);
191 return(vspen(table[ir][ij].col));
192 }
193 
194 int
195 vspen(char *s)
196 {
197 if (s==0) return(0);
198 if (!point(s)) return(0);
199 return(match(s, SPAN));
200 }
201