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