xref: /netbsd-src/usr.bin/sort/fields.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: fields.c,v 1.4 2000/10/17 15:13:40 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 /* Subroutines to generate sort keys. */
40 
41 #include "sort.h"
42 
43 #ifndef lint
44 __RCSID("$NetBSD: fields.c,v 1.4 2000/10/17 15:13:40 jdolecek Exp $");
45 __SCCSID("@(#)fields.c	8.1 (Berkeley) 6/6/93");
46 #endif /* not lint */
47 
48 #define blancmange(ptr) {					\
49 	if (BLANK & d_mask[*(ptr)])				\
50 		while (BLANK & d_mask[*(++(ptr))]);		\
51 }
52 
53 #define NEXTCOL(pos) {						\
54 	if (!SEP_FLAG)						\
55 		while (BLANK & l_d_mask[*(++pos)]);		\
56 	while (!((FLD_D | REC_D_F) & l_d_mask[*++pos]));	\
57 }
58 
59 extern u_char *enterfield __P((u_char *, u_char *, struct field *, int));
60 
61 extern u_char *number __P((u_char *, u_char *, u_char *, u_char *, int));
62 
63 extern struct coldesc clist[(ND+1)*2];
64 extern int ncols;
65 
66 #define DECIMAL '.'
67 #define OFFSET 128
68 
69 u_char TENS[10];	/* TENS[0] = REC_D <= 128 ? 130 - '0' : 2 -'0'... */
70 u_char NEGTENS[10];	/* NEGTENS[0] = REC_D <= 128 ? 126 + '0' : 252 +'0' */
71 u_char *OFF_TENS, *OFF_NTENS;	/* TENS - '0', NEGTENS - '0' */
72 u_char fnum[NBINS], rnum[NBINS];
73 
74 /*
75  * constructs sort key with leading recheader, followed by the key,
76  * followed by the original line.
77  */
78 length_t
79 enterkey(keybuf, line, size, fieldtable)
80 	struct recheader *keybuf;	/* pointer to start of key */
81 	DBT *line;
82 	int size;
83 	struct field fieldtable[];
84 {
85 	int i;
86 	u_char *l_d_mask;
87 	u_char *lineend, *pos;
88 	u_char *endkey, *keypos;
89 	struct coldesc *clpos;
90 	int col = 1;
91 	struct field *ftpos;
92 	l_d_mask = d_mask;
93 	pos = (u_char *) line->data - 1;
94 	lineend = (u_char *) line->data + line->size-1;
95 				/* don't include rec_delimiter */
96 	keypos = keybuf->data;
97 
98 	for (i = 0, clpos = clist; i < ncols; i++, clpos++) {
99 		for (; (col < clpos->num) && (pos < lineend); col++)
100 			{ NEXTCOL(pos); }
101 		if (pos >= lineend)
102 			break;
103 		clpos->start = SEP_FLAG ? pos + 1 : pos;
104 		NEXTCOL(pos);
105 		clpos->end = pos;
106 		col++;
107 		if (pos >= lineend) {
108 			clpos->end = lineend;
109 			++i;
110 			break;
111 		}
112 	}
113 	for (; i <= ncols; i++)
114 		clist[i].start = clist[i].end = lineend;
115 	if (clist[0].start < (u_char *) line->data)
116 		++clist[0].start;
117 	endkey = (u_char *) keybuf + size - line->size;
118 	for (ftpos = fieldtable + 1; ftpos->icol.num; ftpos++)
119 		if ((keypos = enterfield(keypos, endkey, ftpos,
120 		    fieldtable->flags)) == NULL)
121 			return (1);
122 
123 	if (UNIQUE)
124 		*(keypos-1) = REC_D;
125 	keybuf->offset = keypos - keybuf->data;
126 	keybuf->length = keybuf->offset + line->size;
127 	if (keybuf->length + sizeof(TRECHEADER) > size)
128 		return (1);		/* line too long for buffer */
129 	memcpy(keybuf->data + keybuf->offset, line->data, line->size);
130 	return (0);
131 }
132 
133 /*
134  * constructs a field (as defined by -k) within a key
135  */
136 u_char *
137 enterfield(tablepos, endkey, cur_fld, gflags)
138 	struct field *cur_fld;
139 	u_char *tablepos, *endkey;
140 	int gflags;
141 {
142 	u_char *start, *end, *lineend, *mask, *lweight;
143 	struct column icol, tcol;
144 	u_int flags;
145 	u_int Rflag;
146 	icol = cur_fld->icol;
147 	tcol = cur_fld->tcol;
148 	flags = cur_fld->flags;
149 	start = icol.p->start;
150 	lineend = clist[ncols].end;
151 	if (flags & BI)
152 		blancmange(start);
153 	start += icol.indent;
154 	start = min(start, lineend);
155 	if (!tcol.num)
156 		end = lineend;
157 	else {
158 		if (tcol.indent) {
159 			end = tcol.p->start;
160 			if (flags & BT) blancmange(end);
161 			end += tcol.indent;
162 			end = min(end, lineend);
163 		} else
164 			end = tcol.p->end;
165 	}
166 	if (flags & N) {
167 		Rflag = (gflags & R ) ^ (flags & R) ? 1 : 0;
168 		tablepos = number(tablepos, endkey, start, end, Rflag);
169 		return (tablepos);
170 	}
171 	mask = alltable;
172 	mask = cur_fld->mask;
173 	lweight = cur_fld->weights;
174 	for (; start < end; start++)
175 		if (mask[*start]) {
176 			if (*start <= 1) {
177 				if (tablepos+2 >= endkey)
178 					return (NULL);
179 				*tablepos++ = lweight[1];
180 				*tablepos++ = lweight[*start ? 2 : 1];
181 			} else {
182 				*tablepos++ = lweight[*start];
183 				if (tablepos == endkey)
184 				return (NULL);
185 			}
186 		}
187 	*tablepos++ = lweight[0];
188 	return (tablepos == endkey ? NULL : tablepos);
189 }
190 
191 /* Uses the first bin to assign sign, expsign, 0, and the first
192  * 61 out of the exponent ( (254 - 3 origins - 4 over/underflows)/4 = 61 ).
193  *   When sorting in forward order:
194  * use (0-99) -> (130->240) for sorting the mantissa if REC_D <=128;
195  * else use (0-99)->(2-102).
196  * If the exponent is >=61, use another byte for each additional 253
197  * in the exponent. Cutoff is at 567.
198  * To avoid confusing the exponent and the mantissa, use a field delimiter
199  * if the exponent is exactly 61, 61+252, etc--this is ok, since it's the
200  * only time a field delimiter can come in that position.
201  * Reverse order is done analagously.
202 */
203 
204 u_char *
205 number(pos, bufend, line, lineend, Rflag)
206 	u_char *line, *pos, *bufend, *lineend;
207 	int Rflag;
208 {
209 	int or_sign, parity = 0;
210 	int expincr = 1, exponent = -1;
211 	int bite, expsign = 1, sign = 1;
212 	u_char lastvalue, *nonzero, *tline, *C_TENS;
213 	u_char *nweights;
214 
215 	if (Rflag)
216 		nweights = rnum;
217 	else
218 		nweights = fnum;
219 	if (pos > bufend - 8)
220 		return (NULL);
221 	/* or_sign sets the sort direction:
222 	 *	(-r: +/-)(sign: +/-)(expsign: +/-) */
223 	or_sign = sign ^ expsign ^ Rflag;
224 	blancmange(line);
225 	if (*line == '-') {	/* set the sign */
226 		or_sign ^= 1;
227 		sign = 0;
228 		line++;
229 	}
230 	/* eat initial zeroes */
231 	for (; *line == '0' && line < lineend; line++);
232 	/* calculate exponents < 0 */
233 	if (*line == DECIMAL) {
234 		exponent = 1;
235 		while (*++line == '0' && line < lineend)
236 			exponent++;
237 		expincr = 0;
238 		expsign = 0;
239 	}
240 	/* next character better be a digit */
241 	if (*line < '1' || *line > '9' || line >= lineend) {
242 		*pos++ = nweights[127];
243 		return (pos);
244 	}
245 	if (expincr) {
246 		for (tline = line-1; *++tline >= '0' &&
247 		    *tline <= '9' && tline < lineend;)
248 			exponent++;
249 	}
250 	if (exponent > 567) {
251 		*pos++ = nweights[sign ? (expsign ? 254 : 128)
252 					: (expsign ? 0 : 126)];
253 		warnx("exponent out of bounds");
254 		return (pos);
255 	}
256 	bite = min(exponent, 61);
257 	*pos++ = nweights[(sign) ? (expsign ? 189+bite : 189-bite)
258 				: (expsign ? 64-bite : 64+bite)];
259 	if (bite >= 61) {
260 		do {
261 			exponent -= bite;
262 			bite = min(exponent, 254);
263 			*pos++ = nweights[or_sign ? 254-bite : bite];
264 		} while (bite == 254);
265 	}
266 	C_TENS = or_sign ? OFF_NTENS : OFF_TENS;
267 	for (; line < lineend; line++) {
268 		if (*line >= '0' && *line <= '9') {
269 			if (parity) {
270 				*pos++ = C_TENS[lastvalue] + (or_sign ? - *line
271 						: *line);
272 				if (pos == bufend)
273 					return (NULL);
274 				if (*line != '0' || lastvalue != '0')
275 					nonzero = pos;
276 			} else
277 				lastvalue = *line;
278 			parity ^= 1;
279 		} else if(*line == DECIMAL) {
280 			if(!expincr)	/* a decimal already occurred once */
281 				break;
282 			expincr = 0;
283 		} else
284 			break;
285 	}
286 	if (parity && lastvalue != '0') {
287 		*pos++ = or_sign ? OFF_NTENS[lastvalue] - '0' :
288 					OFF_TENS[lastvalue] + '0';
289 	} else
290 		pos = nonzero;
291 	if (pos > bufend-1)
292 		return (NULL);
293 	*pos++ = or_sign ? nweights[254] : nweights[0];
294 	return (pos);
295 }
296 
297 /* This forces a gap around the record delimiter
298  * Thus fnum has vaues over (0,254) -> ((0,REC_D-1),(REC_D+1,255));
299  * rnum over (0,254) -> (255,REC_D+1),(REC_D-1,0))
300 */
301 void
302 num_init()
303 {
304 	int i;
305 	TENS[0] = REC_D <=128 ? 130 - '0' : 2 - '0';
306 	NEGTENS[0] = REC_D <=128 ? 126 + '0' : 254 + '0';
307 	OFF_TENS = TENS - '0';
308 	OFF_NTENS = NEGTENS - '0';
309 	for (i = 1; i < 10; i++) {
310 		TENS[i] = TENS[i-1] + 10;
311 		NEGTENS[i] = NEGTENS[i-1] - 10;
312 	}
313 	for (i = 0; i < REC_D; i++) {
314 		fnum[i] = i;
315 		rnum[255-i] = i;
316 	}
317 	for (i = REC_D; i <255; i++) {
318 		fnum[i] = i+1;
319 		rnum[255-i] = i-1;
320 	}
321 }
322