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