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