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