xref: /csrg-svn/old/tbl/t5.c (revision 11032)
1 /*	t5.c	4.1	83/02/12	*/
2 
3  /* t5.c: read data for table */
4 # include "t..c"
5 gettbl()
6 {
7 int icol, ch;
8 cstore=cspace= chspace();
9 textflg=0;
10 for (nlin=nslin=0; gets1(cstore); nlin++)
11 	{
12 	stynum[nlin]=nslin;
13 	if (prefix(".TE", cstore))
14 		{
15 		leftover=0;
16 		break;
17 		}
18 	if (prefix(".TC", cstore) || prefix(".T&", cstore))
19 		{
20 		readspec();
21 		nslin++;
22 		}
23 	if (nlin>=MAXLIN)
24 		{
25 		leftover=cstore;
26 		break;
27 		}
28 	fullbot[nlin]=0;
29 	if (cstore[0] == '.' && !isdigit(cstore[1]))
30 		{
31 		instead[nlin] = cstore;
32 		while (*cstore++);
33 		continue;
34 		}
35 	else instead[nlin] = 0;
36 	if (nodata(nlin))
37 		{
38 		if (ch = oneh(nlin))
39 			fullbot[nlin]= ch;
40 		nlin++;
41 		nslin++;
42 		instead[nlin]=fullbot[nlin]=0;
43 		}
44 	table[nlin] = alocv((ncol+2)*sizeof(table[0][0]));
45 	if (cstore[1]==0)
46 	switch(cstore[0])
47 		{
48 		case '_': fullbot[nlin]= '-'; continue;
49 		case '=': fullbot[nlin]= '='; continue;
50 		}
51 	stynum[nlin] = nslin;
52 	nslin = min(nslin+1, nclin-1);
53 	for (icol = 0; icol <ncol; icol++)
54 		{
55 		table[nlin][icol].col = cstore;
56 		table[nlin][icol].rcol=0;
57 		ch=1;
58 		if (match(cstore, "T{")) /* text follows */
59 			table[nlin][icol].col =
60 				gettext(cstore, nlin, icol,
61 					font[stynum[nlin]][icol],
62 					csize[stynum[nlin]][icol]);
63 		else
64 			{
65 			for(; (ch= *cstore) != '\0' && ch != tab; cstore++)
66 					;
67 			*cstore++ = '\0';
68 			switch(ctype(nlin,icol)) /* numerical or alpha, subcol */
69 				{
70 				case 'n':
71 					table[nlin][icol].rcol = maknew(table[nlin][icol].col);
72 					break;
73 				case 'a':
74 					table[nlin][icol].rcol = table[nlin][icol].col;
75 					table[nlin][icol].col = "";
76 					break;
77 				}
78 			}
79 		while (ctype(nlin,icol+1)== 's') /* spanning */
80 			table[nlin][++icol].col = "";
81 		if (ch == '\0') break;
82 		}
83 	while (++icol <ncol+2)
84 		{
85 		table[nlin][icol].col = "";
86 		table [nlin][icol].rcol=0;
87 		}
88 	while (*cstore != '\0')
89 		 cstore++;
90 	if (cstore-cspace > MAXCHS)
91 		cstore = cspace = chspace();
92 	}
93 last = cstore;
94 permute();
95 if (textflg) untext();
96 return;
97 }
98 nodata(il)
99 {
100 int c;
101 for (c=0; c<ncol;c++)
102 	{
103 	switch(ctype(il,c))
104 		{
105 		case 'c': case 'n': case 'r': case 'l': case 's': case 'a':
106 			return(0);
107 		}
108 	}
109 return(1);
110 }
111 oneh(lin)
112 {
113 int k, icol;
114 k = ctype(lin,0);
115 for(icol=1; icol<ncol; icol++)
116 	{
117 	if (k != ctype(lin,icol))
118 		return(0);
119 	}
120 return(k);
121 }
122 # define SPAN "\\^"
123 permute()
124 {
125 int irow, jcol, is;
126 char *start, *strig;
127 for(jcol=0; jcol<ncol; jcol++)
128 	{
129 	for(irow=1; irow<nlin; irow++)
130 		{
131 		if (vspand(irow,jcol,0))
132 			{
133 			is = prev(irow);
134 			if (is<0)
135 				error("Vertical spanning in first row not allowed");
136 			start = table[is][jcol].col;
137 			strig = table[is][jcol].rcol;
138 			while (irow<nlin &&vspand(irow,jcol,0))
139 				irow++;
140 			table[--irow][jcol].col = start;
141 			table[irow][jcol].rcol = strig;
142 			while (is<irow)
143 				{
144 				table[is][jcol].rcol =0;
145 				table[is][jcol].col= SPAN;
146 				is = next(is);
147 				}
148 			}
149 		}
150 	}
151 }
152 vspand(ir,ij,ifform)
153 {
154 if (ir<0) return(0);
155 if (ir>=nlin)return(0);
156 if (instead[ir]) return(0);
157 if (ifform==0 && ctype(ir,ij)=='^') return(1);
158 if (table[ir][ij].rcol!=0) return(0);
159 if (fullbot[ir]) return(0);
160 return(vspen(table[ir][ij].col));
161 }
162 vspen(s)
163 	char *s;
164 {
165 if (s==0) return(0);
166 if (!point(s)) return(0);
167 return(match(s, SPAN));
168 }
169