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