init.c revision 1.1 1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Peter McIlroy.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40
41 #include "sort.h"
42
43 #include <ctype.h>
44 #include <string.h>
45
46 extern struct coldesc clist[(ND+1)*2];
47 extern int ncols;
48 u_char gweights[NBINS];
49
50 /*
51 * clist (list of columns which correspond to one or more icol or tcol)
52 * is in increasing order of columns.
53 * Fields are kept in increasing order of fields.
54 */
55
56 /*
57 * keep clist in order--inserts a column in a sorted array
58 */
59 static void
60 insertcol(field)
61 struct field *field;
62 {
63 int i;
64 for (i = 0; i < ncols; i++)
65 if (field->icol.num <= clist[i].num)
66 break;
67 if (field->icol.num != clist[i].num) {
68 memmove(clist+i+1, clist+i, sizeof(COLDESC)*(ncols-i));
69 clist[i].num = field->icol.num;
70 ncols++;
71 }
72 if (field->tcol.num && field->tcol.num != field->icol.num) {
73 for (i = 0; i < ncols; i++)
74 if (field->tcol.num <= clist[i].num)
75 break;
76 if (field->tcol.num != clist[i].num) {
77 memmove(clist+i+1, clist+i,sizeof(COLDESC)*(ncols-i));
78 clist[i].num = field->tcol.num;
79 ncols++;
80 }
81 }
82 }
83
84 /*
85 * matches fields with the appropriate columns--n^2 but who cares?
86 */
87 void
88 fldreset(fldtab)
89 struct field *fldtab;
90 {
91 int i;
92 fldtab[0].tcol.p = clist+ncols-1;
93 for (++fldtab; fldtab->icol.num; ++fldtab) {
94 for (i = 0; fldtab->icol.num != clist[i].num; i++);
95 fldtab->icol.p = clist + i;
96 if (!fldtab->tcol.num)
97 continue;
98 for (i = 0; fldtab->tcol.num != clist[i].num; i++);
99 fldtab->tcol.p = clist + i;
100 }
101 }
102
103 /*
104 * interprets a column in a -k field
105 */
106 char *
107 setcolumn(pos, cur_fld, gflag)
108 char *pos;
109 struct field *cur_fld;
110 int gflag;
111 {
112 struct column *col;
113 int tmp;
114 col = cur_fld->icol.num ? (&(*cur_fld).tcol) : (&(*cur_fld).icol);
115 pos += sscanf(pos, "%d", &(col->num));
116 while (isdigit(*pos))
117 pos++;
118 if (col->num <= 0 && !(col->num == 0 && col == &(cur_fld->tcol)))
119 errx(2, "field numbers must be positive");
120 if (*pos == '.') {
121 if (!col->num)
122 errx(2, "cannot indent end of line");
123 pos += sscanf(++pos, "%d", &(col->indent));
124 while (isdigit(*pos))
125 pos++;
126 if (&cur_fld->icol == col)
127 col->indent--;
128 if (col->indent < 0)
129 errx(2, "illegal offset");
130 }
131 if (optval(*pos, cur_fld->tcol.num))
132 while (tmp = optval(*pos, cur_fld->tcol.num)) {
133 cur_fld->flags |= tmp;
134 pos++;
135 }
136 if (cur_fld->icol.num == 0)
137 cur_fld->icol.num = 1;
138 return (pos);
139 }
140
141 int
142 setfield(pos, cur_fld, gflag)
143 char *pos;
144 struct field *cur_fld;
145 int gflag;
146 {
147 static int nfields = 0;
148 int tmp;
149 char *setcolumn();
150 if (++nfields == ND)
151 errx(2, "too many sort keys. (Limit is %d)", ND-1);
152 cur_fld->weights = ascii;
153 cur_fld->mask = alltable;
154 pos = setcolumn(pos, cur_fld, gflag);
155 if (*pos == '\0') /* key extends to EOL. */
156 cur_fld->tcol.num = 0;
157 else {
158 if (*pos != ',')
159 errx(2, "illegal field descriptor");
160 setcolumn((++pos), cur_fld, gflag);
161 }
162 if (!cur_fld->flags)
163 cur_fld->flags = gflag;
164 tmp = cur_fld->flags;
165
166 /*
167 * Assign appropriate mask table and weight table.
168 * If the global weights are reversed, the local field
169 * must be "re-reversed".
170 */
171 if (((tmp & R) ^ (gflag & R)) && tmp & F)
172 cur_fld->weights = RFtable;
173 else if (tmp & F)
174 cur_fld->weights = Ftable;
175 else if ((tmp & R) ^ (gflag & R))
176 cur_fld->weights = Rascii;
177 if (tmp & I)
178 cur_fld->mask = itable;
179 else if (tmp & D)
180 cur_fld->mask = dtable;
181 cur_fld->flags |= (gflag & (BI | BT));
182 if (!cur_fld->tcol.indent) /* BT has no meaning at end of field */
183 cur_fld->flags &= (D|F|I|N|R|BI);
184 if (cur_fld->tcol.num && !(!(cur_fld->flags & BI)
185 && cur_fld->flags & BT) && (cur_fld->tcol.num <= cur_fld->icol.num
186 && cur_fld->tcol.indent < cur_fld->icol.indent))
187 errx(2, "fields out of order");
188 insertcol(cur_fld);
189 return (cur_fld->tcol.num);
190 }
191
192 int
193 optval(desc, tcolflag)
194 int desc, tcolflag;
195 {
196 switch(desc) {
197 case 'b':
198 if (!tcolflag)
199 return(BI);
200 else
201 return(BT);
202 case 'd': return(D);
203 case 'f': return(F);
204 case 'i': return(I);
205 case 'n': return(N);
206 case 'r': return(R);
207 default: return(0);
208 }
209 }
210
211 void
212 fixit(argc, argv)
213 int *argc;
214 char **argv;
215 {
216 int i, j, v, w, x;
217 static char vbuf[ND*20], *vpos, *tpos;
218 vpos = vbuf;
219
220 for (i = 1; i < *argc; i++) {
221 if (argv[i][0] == '+') {
222 tpos = argv[i]+1;
223 argv[i] = vpos;
224 vpos += sprintf(vpos, "-k");
225 tpos += sscanf(tpos, "%d", &v);
226 while (isdigit(*tpos))
227 tpos++;
228 vpos += sprintf(vpos, "%d", v+1);
229 if (*tpos == '.') {
230 tpos += sscanf(++tpos, "%d", &x);
231 vpos += sprintf(vpos, ".%d", x+1);
232 }
233 while (*tpos)
234 *vpos++ = *tpos++;
235 vpos += sprintf(vpos, ",");
236 if (argv[i+1] &&
237 argv[i+1][0] == '-' && isdigit(argv[i+1][1])) {
238 tpos = argv[i+1] + 1;
239 tpos += sscanf(tpos, "%d", &w);
240 while (isdigit(*tpos))
241 tpos++;
242 x = 0;
243 if (*tpos == '.') {
244 tpos += sscanf(++tpos, "%d", &x);
245 while (isdigit(*tpos))
246 *tpos++;
247 }
248 if (x) {
249 vpos += sprintf(vpos, "%d", w+1);
250 vpos += sprintf(vpos, ".%d", x);
251 } else
252 vpos += sprintf(vpos, "%d", w);
253 while (*tpos)
254 *vpos++ = *tpos++;
255 for (j= i+1; j < *argc; j++)
256 argv[j] = argv[j+1];
257 *argc -= 1;
258 }
259 }
260 }
261 }
262
263 /*
264 * ascii, Rascii, Ftable, and RFtable map
265 * REC_D -> REC_D; {not REC_D} -> {not REC_D}.
266 * gweights maps REC_D -> (0 or 255); {not REC_D} -> {not gweights[REC_D]}.
267 * Note: when sorting in forward order, to encode character zero in a key,
268 * use \001\001; character 1 becomes \001\002. In this case, character 0
269 * is reserved for the field delimiter. Analagously for -r (fld_d = 255).
270 * Note: this is only good for ASCII sorting. For different LC 's,
271 * all bets are off. See also num_init in number.c
272 */
273 void
274 settables(gflags)
275 int gflags;
276 {
277 u_char *wts;
278 int i, incr;
279 for (i=0; i < 256; i++) {
280 ascii[i] = i;
281 if (i > REC_D && i < 255 - REC_D+1)
282 Rascii[i] = 255 - i + 1;
283 else
284 Rascii[i] = 255 - i;
285 if (islower(i)) {
286 Ftable[i] = Ftable[i- ('a' -'A')];
287 RFtable[i] = RFtable[i - ('a' - 'A')];
288 } else if (REC_D>= 'A' && REC_D < 'Z' && i < 'a' && i > REC_D) {
289 Ftable[i] = i + 1;
290 RFtable[i] = Rascii[i] - 1;
291 } else {
292 Ftable[i] = i;
293 RFtable[i] = Rascii[i];
294 }
295 alltable[i] = 1;
296 if (i == '\n' || isprint(i))
297 itable[i] = 1;
298 else itable[i] = 0;
299 if (i == '\n' || i == '\t' || i == ' ' || isalnum(i))
300 dtable[i] = 1;
301 else dtable[i] = 0;
302 }
303 Rascii[REC_D] = RFtable[REC_D] = REC_D;
304 if (REC_D >= 'A' && REC_D < 'Z')
305 ++Ftable[REC_D + ('a' - 'A')];
306 if (gflags & R && (!(gflags & F) || !SINGL_FLD))
307 wts = Rascii;
308 else if (!(gflags & F) || !SINGL_FLD)
309 wts = ascii;
310 else if (gflags & R)
311 wts = RFtable;
312 else
313 wts = Ftable;
314 memmove(gweights, wts, sizeof(gweights));
315 incr = (gflags & R) ? -1 : 1;
316 for (i = 0; i < REC_D; i++)
317 gweights[i] += incr;
318 gweights[REC_D] = ((gflags & R) ? 255 : 0);
319 if (SINGL_FLD && gflags & F) {
320 for (i = 0; i < REC_D; i++) {
321 ascii[i] += incr;
322 Rascii[i] += incr;
323 }
324 ascii[REC_D] = Rascii[REC_D] = gweights[REC_D];
325 }
326 }
327