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