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