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