xref: /netbsd-src/usr.bin/sort/init.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: init.c,v 1.5 2001/02/19 20:50:17 jdolecek 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.5 2001/02/19 20:50:17 jdolecek 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 static const char *setcolumn __P((const char *, struct field *, int));
51 int setfield __P((const 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  * masks of ignored characters.  Alltable is 256 ones.
59  */
60 static u_char alltable[NBINS], dtable[NBINS], itable[NBINS];
61 
62 /*
63  * clist (list of columns which correspond to one or more icol or tcol)
64  * is in increasing order of columns.
65  * Fields are kept in increasing order of fields.
66  */
67 
68 /*
69  * keep clist in order--inserts a column in a sorted array
70  */
71 static void
72 insertcol(field)
73 	struct field *field;
74 {
75 	int i;
76 	for (i = 0; i < ncols; i++)
77 		if (field->icol.num <= clist[i].num)
78 			break;
79 	if (field->icol.num != clist[i].num) {
80 		memmove(clist+i+1, clist+i, sizeof(COLDESC)*(ncols-i));
81 		clist[i].num = field->icol.num;
82 		ncols++;
83 	}
84 	if (field->tcol.num && field->tcol.num != field->icol.num) {
85 		for (i = 0; i < ncols; i++)
86 			if (field->tcol.num <= clist[i].num)
87 				break;
88 		if (field->tcol.num != clist[i].num) {
89 			memmove(clist+i+1, clist+i,sizeof(COLDESC)*(ncols-i));
90 			clist[i].num = field->tcol.num;
91 			ncols++;
92 		}
93 	}
94 }
95 
96 /*
97  * matches fields with the appropriate columns--n^2 but who cares?
98  */
99 void
100 fldreset(fldtab)
101 	struct field *fldtab;
102 {
103 	int i;
104 	fldtab[0].tcol.p = clist+ncols-1;
105 	for (++fldtab; fldtab->icol.num; ++fldtab) {
106 		for (i = 0; fldtab->icol.num != clist[i].num; i++)
107 			;
108 		fldtab->icol.p = clist + i;
109 		if (!fldtab->tcol.num)
110 			continue;
111 		for (i = 0; fldtab->tcol.num != clist[i].num; i++)
112 			;
113 		fldtab->tcol.p = clist + i;
114 	}
115 }
116 
117 /*
118  * interprets a column in a -k field
119  */
120 static const char *
121 setcolumn(pos, cur_fld, gflag)
122 	const char *pos;
123 	struct field *cur_fld;
124 	int gflag;
125 {
126 	struct column *col;
127 	int tmp;
128 	col = cur_fld->icol.num ? (&(*cur_fld).tcol) : (&(*cur_fld).icol);
129 	pos += sscanf(pos, "%d", &(col->num));
130 	while (isdigit(*pos))
131 		pos++;
132 	if (col->num <= 0 && !(col->num == 0 && col == &(cur_fld->tcol)))
133 		errx(2, "field numbers must be positive");
134 	if (*pos == '.') {
135 		if (!col->num)
136 			errx(2, "cannot indent end of line");
137 		pos += sscanf(++pos, "%d", &(col->indent));
138 		while (isdigit(*pos))
139 			pos++;
140 		if (&cur_fld->icol == col)
141 			col->indent--;
142 		if (col->indent < 0)
143 			errx(2, "illegal offset");
144 	}
145 	if (optval(*pos, cur_fld->tcol.num))
146 		while ((tmp = optval(*pos, cur_fld->tcol.num))) {
147 			cur_fld->flags |= tmp;
148 			pos++;
149 	}
150 	if (cur_fld->icol.num == 0)
151 		cur_fld->icol.num = 1;
152 	return (pos);
153 }
154 
155 int
156 setfield(pos, cur_fld, gflag)
157 	const char *pos;
158 	struct field *cur_fld;
159 	int gflag;
160 {
161 	static int nfields = 0;
162 	int tmp;
163 
164 	if (++nfields == ND)
165 		errx(2, "too many sort keys. (Limit is %d)", ND-1);
166 
167 	cur_fld->weights = ascii;
168 	cur_fld->mask = alltable;
169 
170 	pos = setcolumn(pos, cur_fld, gflag);
171 	if (*pos == '\0')			/* key extends to EOL. */
172 		cur_fld->tcol.num = 0;
173 	else {
174 		if (*pos != ',')
175 			errx(2, "illegal field descriptor");
176 		setcolumn((++pos), cur_fld, gflag);
177 	}
178 	if (!cur_fld->flags)
179 		cur_fld->flags = gflag;
180 	tmp = cur_fld->flags;
181 
182 	/*
183 	 * Assign appropriate mask table and weight table.
184 	 * If the global weights are reversed, the local field
185 	 * must be "re-reversed".
186 	 */
187 	if (((tmp & R) ^ (gflag & R)) && (tmp & F))
188 		cur_fld->weights = RFtable;
189 	else if (tmp & F)
190 		cur_fld->weights = Ftable;
191 	else if ((tmp & R) ^ (gflag & R))
192 		cur_fld->weights = Rascii;
193 
194 	if (tmp & I)
195 		cur_fld->mask = itable;
196 	else if (tmp & D)
197 		cur_fld->mask = dtable;
198 
199 	cur_fld->flags |= (gflag & (BI | BT));
200 	if (!cur_fld->tcol.indent)	/* BT has no meaning at end of field */
201 		cur_fld->flags &= ~BT;
202 
203 	if (cur_fld->tcol.num && !(!(cur_fld->flags & BI)
204 	    && cur_fld->flags & BT) && (cur_fld->tcol.num <= cur_fld->icol.num
205 	    && cur_fld->tcol.indent < cur_fld->icol.indent))
206 		errx(2, "fields out of order");
207 	insertcol(cur_fld);
208 	return (cur_fld->tcol.num);
209 }
210 
211 int
212 optval(desc, tcolflag)
213 	int desc, tcolflag;
214 {
215 	switch(desc) {
216 		case 'b':
217 			if (!tcolflag)
218 				return (BI);
219 			else
220 				return (BT);
221 		case 'd': return (D);
222 		case 'f': return (F);
223 		case 'i': return (I);
224 		case 'n': return (N);
225 		case 'r': return (R);
226 		default:  return (0);
227 	}
228 }
229 
230 void
231 fixit(argc, argv)
232 	int *argc;
233 	char **argv;
234 {
235 	int i, j, v, w, x;
236 	static char vbuf[ND*20], *vpos, *tpos;
237 	vpos = vbuf;
238 
239 	for (i = 1; i < *argc; i++) {
240 		if (argv[i][0] == '+') {
241 			tpos = argv[i]+1;
242 			argv[i] = vpos;
243 			vpos += sprintf(vpos, "-k");
244 			tpos += sscanf(tpos, "%d", &v);
245 			while (isdigit(*tpos))
246 				tpos++;
247 			vpos += sprintf(vpos, "%d", v+1);
248 			if (*tpos == '.') {
249 				tpos += sscanf(++tpos, "%d", &x);
250 				vpos += sprintf(vpos, ".%d", x+1);
251 			}
252 			while (*tpos)
253 				*vpos++ = *tpos++;
254 			vpos += sprintf(vpos, ",");
255 			if (argv[i+1] &&
256 			    argv[i+1][0] == '-' && isdigit(argv[i+1][1])) {
257 				tpos = argv[i+1] + 1;
258 				tpos += sscanf(tpos, "%d", &w);
259 				while (isdigit(*tpos))
260 					tpos++;
261 				x = 0;
262 				if (*tpos == '.') {
263 					tpos += sscanf(++tpos, "%d", &x);
264 					while (isdigit(*tpos))
265 						tpos++;
266 				}
267 				if (x) {
268 					vpos += sprintf(vpos, "%d", w+1);
269 					vpos += sprintf(vpos, ".%d", x);
270 				} else
271 					vpos += sprintf(vpos, "%d", w);
272 				while (*tpos)
273 					*vpos++ = *tpos++;
274 				for (j= i+1; j < *argc; j++)
275 					argv[j] = argv[j+1];
276 				*argc -= 1;
277 			}
278 		}
279 	}
280 }
281 
282 /*
283  * ascii, Rascii, Ftable, and RFtable map
284  * REC_D -> REC_D;  {not REC_D} -> {not REC_D}.
285  * gweights maps REC_D -> (0 or 255); {not REC_D} -> {not gweights[REC_D]}.
286  * Note: when sorting in forward order, to encode character zero in a key,
287  * use \001\001; character 1 becomes \001\002.  In this case, character 0
288  * is reserved for the field delimiter.  Analagously for -r (fld_d = 255).
289  * Note: this is only good for ASCII sorting.  For different LC 's,
290  * all bets are off.  See also num_init in number.c
291  */
292 void
293 settables(gflags)
294 	int gflags;
295 {
296 	u_char *wts;
297 	int i, incr;
298 	for (i=0; i < 256; i++) {
299 		ascii[i] = i;
300 		if (i > REC_D && i < 255 - REC_D+1)
301 			Rascii[i] = 255 - i + 1;
302 		else
303 			Rascii[i] = 255 - i;
304 		if (islower(i)) {
305 			Ftable[i] = Ftable[toupper(i)];
306 			RFtable[i] = RFtable[toupper(i)];
307 		} else if (REC_D>= 'A' && REC_D < 'Z' && i < 'a' && i > REC_D) {
308 			Ftable[i] = i + 1;
309 			RFtable[i] = Rascii[i] - 1;
310 		} else {
311 			Ftable[i] = i;
312 			RFtable[i] = Rascii[i];
313 		}
314 		alltable[i] = 1;
315 
316 		if (i == '\n' || isprint(i))
317 			itable[i] = 1;
318 		else
319 			itable[i] = 0;
320 
321 		if (i == '\n' || i == '\t' || i == ' ' || isalnum(i))
322 			dtable[i] = 1;
323 		else
324 			dtable[i] = 0;
325 	}
326 
327 	Rascii[REC_D] = RFtable[REC_D] = REC_D;
328 	if (isupper(REC_D))
329 		Ftable[tolower(REC_D)]++;
330 
331 	if ((gflags & R) && !((gflags & F) && SINGL_FLD))
332 		wts = Rascii;
333 	else if (!((gflags & F) && SINGL_FLD))
334 		wts = ascii;
335 	else if (gflags & R)
336 		wts = RFtable;
337 	else
338 		wts = Ftable;
339 
340 	memmove(gweights, wts, sizeof(gweights));
341 	incr = (gflags & R) ? -1 : 1;
342 	for (i = 0; i < REC_D; i++)
343 		gweights[i] += incr;
344 	gweights[REC_D] = ((gflags & R) ? 255 : 0);
345 	if (SINGL_FLD && (gflags & F)) {
346 		for (i = 0; i < REC_D; i++) {
347 			ascii[i] += incr;
348 			Rascii[i] += incr;
349 		}
350 		ascii[REC_D] = Rascii[REC_D] = gweights[REC_D];
351 	}
352 }
353