xref: /csrg-svn/contrib/sort/init.c (revision 62247)
160907Sbostic /*-
2*62247Sbostic  * Copyright (c) 1993
3*62247Sbostic  *	The Regents of the University of California.  All rights reserved.
460907Sbostic  *
560907Sbostic  * This code is derived from software contributed to Berkeley by
660907Sbostic  * Peter McIlroy.
760907Sbostic  *
860907Sbostic  * %sccs.include.redist.c%
960907Sbostic  */
1060907Sbostic 
1160907Sbostic #ifndef lint
12*62247Sbostic static char sccsid[] = "@(#)init.c	8.1 (Berkeley) 06/06/93";
1360907Sbostic #endif /* not lint */
1460907Sbostic 
1560907Sbostic #include "sort.h"
1660907Sbostic 
1760907Sbostic #include <ctype.h>
1860907Sbostic #include <string.h>
1960907Sbostic 
2060907Sbostic extern struct coldesc clist[(ND+1)*2];
2160907Sbostic extern int ncols;
2260907Sbostic u_char gweights[NBINS];
2360907Sbostic 
2460907Sbostic /*
2560907Sbostic  * clist (list of columns which correspond to one or more icol or tcol)
2660907Sbostic  * is in increasing order of columns.
2760907Sbostic  * Fields are kept in increasing order of fields.
2860907Sbostic  */
2960907Sbostic 
3060907Sbostic /*
3160907Sbostic  * keep clist in order--inserts a column in a sorted array
3260907Sbostic  */
3360907Sbostic static void
insertcol(field)3460907Sbostic insertcol(field)
3560907Sbostic 	struct field *field;
3660907Sbostic {
3760907Sbostic 	int i;
3860907Sbostic 	for (i = 0; i < ncols; i++)
3960907Sbostic 		if (field->icol.num <= clist[i].num)
4060907Sbostic 			break;
4160907Sbostic 	if (field->icol.num != clist[i].num) {
4260907Sbostic 		memmove(clist+i+1, clist+i, sizeof(COLDESC)*(ncols-i));
4360907Sbostic 		clist[i].num = field->icol.num;
4460907Sbostic 		ncols++;
4560907Sbostic 	}
4660907Sbostic 	if (field->tcol.num && field->tcol.num != field->icol.num) {
4760907Sbostic 		for (i = 0; i < ncols; i++)
4860907Sbostic 			if (field->tcol.num <= clist[i].num)
4960907Sbostic 				break;
5060907Sbostic 		if (field->tcol.num != clist[i].num) {
5160907Sbostic 			memmove(clist+i+1, clist+i,sizeof(COLDESC)*(ncols-i));
5260907Sbostic 			clist[i].num = field->tcol.num;
5360907Sbostic 			ncols++;
5460907Sbostic 		}
5560907Sbostic 	}
5660907Sbostic }
5760907Sbostic 
5860907Sbostic /*
5960907Sbostic  * matches fields with the appropriate columns--n^2 but who cares?
6060907Sbostic  */
6160907Sbostic void
fldreset(fldtab)6260907Sbostic fldreset(fldtab)
6360907Sbostic 	struct field *fldtab;
6460907Sbostic {
6560907Sbostic 	int i;
6660907Sbostic 	fldtab[0].tcol.p = clist+ncols-1;
6760907Sbostic 	for (++fldtab; fldtab->icol.num; ++fldtab) {
6860907Sbostic 		for (i = 0; fldtab->icol.num != clist[i].num; i++);
6960907Sbostic 		fldtab->icol.p = clist + i;
7060907Sbostic 		if (!fldtab->tcol.num)
7160907Sbostic 			continue;
7260907Sbostic 		for (i = 0; fldtab->tcol.num != clist[i].num; i++);
7360907Sbostic 		fldtab->tcol.p = clist + i;
7460907Sbostic 	}
7560907Sbostic }
7660907Sbostic 
7760907Sbostic /*
7860907Sbostic  * interprets a column in a -k field
7960907Sbostic  */
8060907Sbostic char *
setcolumn(pos,cur_fld,gflag)8160907Sbostic setcolumn(pos, cur_fld, gflag)
8260907Sbostic 	char *pos;
8360907Sbostic 	struct field *cur_fld;
8460907Sbostic 	int gflag;
8560907Sbostic {
8660907Sbostic 	struct column *col;
8760907Sbostic 	int tmp;
8860907Sbostic 	col = cur_fld->icol.num ? (&(*cur_fld).tcol) : (&(*cur_fld).icol);
8960907Sbostic 	pos += sscanf(pos, "%d", &(col->num));
9060907Sbostic 	while (isdigit(*pos))
9160907Sbostic 		pos++;
9260907Sbostic 	if (col->num <= 0 && !(col->num == 0 && col == &(cur_fld->tcol)))
9360907Sbostic 		errx(2, "field numbers must be positive");
9460907Sbostic 	if (*pos == '.') {
9560907Sbostic 		if (!col->num)
9660907Sbostic 			errx(2, "cannot indent end of line");
9760907Sbostic 		pos += sscanf(++pos, "%d", &(col->indent));
9860907Sbostic 		while (isdigit(*pos))
9960907Sbostic 			pos++;
10060907Sbostic 		if (&cur_fld->icol == col)
10160907Sbostic 			col->indent--;
10260907Sbostic 		if (col->indent < 0)
10360907Sbostic 			errx(2, "illegal offset");
10460907Sbostic 	}
10560907Sbostic 	if (optval(*pos, cur_fld->tcol.num))
10660907Sbostic 		while (tmp = optval(*pos, cur_fld->tcol.num)) {
10760907Sbostic 			cur_fld->flags |= tmp;
10860907Sbostic 			pos++;
10960907Sbostic 	}
11060907Sbostic 	if (cur_fld->icol.num == 0)
11160907Sbostic 		cur_fld->icol.num = 1;
11260907Sbostic 	return (pos);
11360907Sbostic }
11460907Sbostic 
11560907Sbostic int
setfield(pos,cur_fld,gflag)11660907Sbostic setfield(pos, cur_fld, gflag)
11760907Sbostic 	char *pos;
11860907Sbostic 	struct field *cur_fld;
11960907Sbostic 	int gflag;
12060907Sbostic {
12160907Sbostic 	static int nfields = 0;
12260907Sbostic 	int tmp;
12360907Sbostic 	char *setcolumn();
12460907Sbostic 	if (++nfields == ND)
12560907Sbostic 		errx(2, "too many sort keys. (Limit is %d)", ND-1);
12660907Sbostic 	cur_fld->weights = ascii;
12760907Sbostic 	cur_fld->mask = alltable;
12860907Sbostic 	pos = setcolumn(pos, cur_fld, gflag);
12960907Sbostic 	if (*pos == '\0')			/* key extends to EOL. */
13060907Sbostic 		cur_fld->tcol.num = 0;
13160907Sbostic 	else {
13260907Sbostic 		if (*pos != ',')
13360907Sbostic 			errx(2, "illegal field descriptor");
13460907Sbostic 		setcolumn((++pos), cur_fld, gflag);
13560907Sbostic 	}
13660907Sbostic 	if (!cur_fld->flags)
13760907Sbostic 		cur_fld->flags = gflag;
13860907Sbostic 	tmp = cur_fld->flags;
13960907Sbostic 
14060907Sbostic 	/*
14160907Sbostic 	 * Assign appropriate mask table and weight table.
14260907Sbostic 	 * If the global weights are reversed, the local field
14360907Sbostic 	 * must be "re-reversed".
14460907Sbostic 	 */
14560907Sbostic 	if (((tmp & R) ^ (gflag & R)) && tmp & F)
14660907Sbostic 		cur_fld->weights = RFtable;
14760907Sbostic 	else if (tmp & F)
14860907Sbostic 		cur_fld->weights = Ftable;
14960907Sbostic 	else if ((tmp & R) ^ (gflag & R))
15060907Sbostic 		cur_fld->weights = Rascii;
15160907Sbostic 	if (tmp & I)
15260907Sbostic 		cur_fld->mask = itable;
15360907Sbostic 	else if (tmp & D)
15460907Sbostic 		cur_fld->mask = dtable;
15560907Sbostic 	cur_fld->flags |= (gflag & (BI | BT));
15660907Sbostic 	if (!cur_fld->tcol.indent)	/* BT has no meaning at end of field */
15760907Sbostic 		cur_fld->flags &= (D|F|I|N|R|BI);
15860907Sbostic 	if (cur_fld->tcol.num && !(!(cur_fld->flags & BI)
15960907Sbostic 	    && cur_fld->flags & BT) && (cur_fld->tcol.num <= cur_fld->icol.num
16060907Sbostic 	    && cur_fld->tcol.indent < cur_fld->icol.indent))
16160907Sbostic 		errx(2, "fields out of order");
16260907Sbostic 	insertcol(cur_fld);
16360907Sbostic 	return (cur_fld->tcol.num);
16460907Sbostic }
16560907Sbostic 
16660907Sbostic int
optval(desc,tcolflag)16760907Sbostic optval(desc, tcolflag)
16860907Sbostic 	int desc, tcolflag;
16960907Sbostic {
17060907Sbostic 	switch(desc) {
17160907Sbostic 		case 'b':
17260907Sbostic 			if (!tcolflag)
17360907Sbostic 				return(BI);
17460907Sbostic 			else
17560907Sbostic 				return(BT);
17660907Sbostic 		case 'd': return(D);
17760907Sbostic 		case 'f': return(F);
17860907Sbostic 		case 'i': return(I);
17960907Sbostic 		case 'n': return(N);
18060907Sbostic 		case 'r': return(R);
18160907Sbostic 		default:  return(0);
18260907Sbostic 	}
18360907Sbostic }
18460907Sbostic 
18560907Sbostic void
fixit(argc,argv)18660907Sbostic fixit(argc, argv)
18760907Sbostic 	int *argc;
18860907Sbostic 	char **argv;
18960907Sbostic {
19060907Sbostic 	int i, j, v, w, x;
19160907Sbostic 	static char vbuf[ND*20], *vpos, *tpos;
19260907Sbostic 	vpos = vbuf;
19360907Sbostic 
19460907Sbostic 	for (i = 1; i < *argc; i++) {
19560907Sbostic 		if (argv[i][0] == '+') {
19660907Sbostic 			tpos = argv[i]+1;
19760907Sbostic 			argv[i] = vpos;
19860907Sbostic 			vpos += sprintf(vpos, "-k");
19960907Sbostic 			tpos += sscanf(tpos, "%d", &v);
20060907Sbostic 			while (isdigit(*tpos))
20160907Sbostic 				tpos++;
20260907Sbostic 			vpos += sprintf(vpos, "%d", v+1);
20360907Sbostic 			if (*tpos == '.') {
20460907Sbostic 				tpos += sscanf(++tpos, "%d", &x);
20560907Sbostic 				vpos += sprintf(vpos, ".%d", x+1);
20660907Sbostic 			}
20760907Sbostic 			while (*tpos)
20860907Sbostic 				*vpos++ = *tpos++;
20960907Sbostic 			vpos += sprintf(vpos, ",");
21060907Sbostic 			if (argv[i+1] &&
21160907Sbostic 			    argv[i+1][0] == '-' && isdigit(argv[i+1][1])) {
21260907Sbostic 				tpos = argv[i+1] + 1;
21360907Sbostic 				tpos += sscanf(tpos, "%d", &w);
21460907Sbostic 				while (isdigit(*tpos))
21560907Sbostic 					tpos++;
21660907Sbostic 				x = 0;
21760907Sbostic 				if (*tpos == '.') {
21860907Sbostic 					tpos += sscanf(++tpos, "%d", &x);
21960907Sbostic 					while (isdigit(*tpos))
22060907Sbostic 						*tpos++;
22160907Sbostic 				}
22260907Sbostic 				if (x) {
22360907Sbostic 					vpos += sprintf(vpos, "%d", w+1);
22460907Sbostic 					vpos += sprintf(vpos, ".%d", x);
22560907Sbostic 				} else
22660907Sbostic 					vpos += sprintf(vpos, "%d", w);
22760907Sbostic 				while (*tpos)
22860907Sbostic 					*vpos++ = *tpos++;
22960907Sbostic 				for (j= i+1; j < *argc; j++)
23060907Sbostic 					argv[j] = argv[j+1];
23160907Sbostic 				*argc -= 1;
23260907Sbostic 			}
23360907Sbostic 		}
23460907Sbostic 	}
23560907Sbostic }
23660907Sbostic 
23760907Sbostic /*
23860907Sbostic  * ascii, Rascii, Ftable, and RFtable map
23960907Sbostic  * REC_D -> REC_D;  {not REC_D} -> {not REC_D}.
24060907Sbostic  * gweights maps REC_D -> (0 or 255); {not REC_D} -> {not gweights[REC_D]}.
24160907Sbostic  * Note: when sorting in forward order, to encode character zero in a key,
24260907Sbostic  * use \001\001; character 1 becomes \001\002.  In this case, character 0
24360907Sbostic  * is reserved for the field delimiter.  Analagously for -r (fld_d = 255).
24460907Sbostic  * Note: this is only good for ASCII sorting.  For different LC 's,
24560907Sbostic  * all bets are off.  See also num_init in number.c
24660907Sbostic  */
24760907Sbostic void
settables(gflags)24860907Sbostic settables(gflags)
24960907Sbostic 	int gflags;
25060907Sbostic {
25160907Sbostic 	u_char *wts;
25260907Sbostic 	int i, incr;
25360907Sbostic 	for (i=0; i < 256; i++) {
25460907Sbostic 		ascii[i] = i;
25560907Sbostic 		if (i > REC_D && i < 255 - REC_D+1)
25660907Sbostic 			Rascii[i] = 255 - i + 1;
25760907Sbostic 		else
25860907Sbostic 			Rascii[i] = 255 - i;
25960907Sbostic 		if (islower(i)) {
26060907Sbostic 			Ftable[i] = Ftable[i- ('a' -'A')];
26160907Sbostic 			RFtable[i] = RFtable[i - ('a' - 'A')];
26260907Sbostic 		} else if (REC_D>= 'A' && REC_D < 'Z' && i < 'a' && i > REC_D) {
26360907Sbostic 			Ftable[i] = i + 1;
26460907Sbostic 			RFtable[i] = Rascii[i] - 1;
26560907Sbostic 		} else {
26660907Sbostic 			Ftable[i] = i;
26760907Sbostic 			RFtable[i] = Rascii[i];
26860907Sbostic 		}
26960907Sbostic 		alltable[i] = 1;
27060907Sbostic 		if (i == '\n' || isprint(i))
27160907Sbostic 			itable[i] = 1;
27260907Sbostic 		else itable[i] = 0;
27360907Sbostic 		if (i == '\n' || i == '\t' || i == ' ' || isalnum(i))
27460907Sbostic 			dtable[i] = 1;
27560907Sbostic 		else dtable[i] = 0;
27660907Sbostic 	}
27760907Sbostic 	Rascii[REC_D] = RFtable[REC_D] = REC_D;
27860907Sbostic 	if (REC_D >= 'A' && REC_D < 'Z')
27960907Sbostic 		++Ftable[REC_D + ('a' - 'A')];
28060907Sbostic 	if (gflags & R && (!(gflags & F) || !SINGL_FLD))
28160907Sbostic 		wts = Rascii;
28260907Sbostic 	else if (!(gflags & F) || !SINGL_FLD)
28360907Sbostic 		wts = ascii;
28460907Sbostic 	else if (gflags & R)
28560907Sbostic 		wts = RFtable;
28660907Sbostic 	else
28760907Sbostic 		wts = Ftable;
28860907Sbostic 	memmove(gweights, wts, sizeof(gweights));
28960907Sbostic 	incr = (gflags & R) ? -1 : 1;
29060907Sbostic 	for (i = 0; i < REC_D; i++)
29160907Sbostic 		gweights[i] += incr;
29260907Sbostic 	gweights[REC_D] = ((gflags & R) ? 255 : 0);
29360907Sbostic 	if (SINGL_FLD && gflags & F) {
29460907Sbostic 		for (i = 0; i < REC_D; i++) {
29560907Sbostic 			ascii[i] += incr;
29660907Sbostic 			Rascii[i] += incr;
29760907Sbostic 		}
29860907Sbostic 		ascii[REC_D] = Rascii[REC_D] = gweights[REC_D];
29960907Sbostic 	}
30060907Sbostic }
301