xref: /csrg-svn/contrib/sort/sort.h (revision 62247)
160912Sbostic /*-
2*62247Sbostic  * Copyright (c) 1993
3*62247Sbostic  *	The Regents of the University of California.  All rights reserved.
460912Sbostic  *
560912Sbostic  * This code is derived from software contributed to Berkeley by
660912Sbostic  * Peter McIlroy.
760912Sbostic  *
860912Sbostic  * %sccs.include.redist.c%
960912Sbostic  *
10*62247Sbostic  *	@(#)sort.h	8.1 (Berkeley) 06/06/93
1160912Sbostic  */
1260912Sbostic 
1360912Sbostic #include <sys/param.h>
1460912Sbostic 
1560912Sbostic #include <db.h>
1660912Sbostic #include <err.h>
1760912Sbostic #include <errno.h>
1860912Sbostic #include <fcntl.h>
1960912Sbostic #include <limits.h>
2060912Sbostic #include <stdio.h>
2160912Sbostic #include <stdlib.h>
2260912Sbostic 
2360912Sbostic #define NBINS 256
2460912Sbostic #define MAXMERGE 16
2560912Sbostic 
2660912Sbostic /* values for masks, weights, and other flags. */
2760912Sbostic #define I 1		/* mask out non-printable characters */
2860912Sbostic #define D 2		/* sort alphanumeric characters only */
2960912Sbostic #define N 4		/* Field is a number */
3060912Sbostic #define F 8		/* weight lower and upper case the same */
3160912Sbostic #define R 16		/* Field is reversed with respect to the global weight */
3260912Sbostic #define BI 32		/* ignore blanks in icol */
3360912Sbostic #define BT 64		/* ignore blanks in tcol */
3460912Sbostic 
3560912Sbostic /* masks for delimiters: blanks, fields, and termination. */
3660912Sbostic #define BLANK 1		/* ' ', '\t'; '\n' if -T is invoked */
3760912Sbostic #define FLD_D 2		/* ' ', '\t' default; from -t otherwise */
3860912Sbostic #define REC_D_F 4	/* '\n' default; from -T otherwise */
3960912Sbostic 
4060912Sbostic #define ND 10	/* limit on number of -k options. */
4160912Sbostic 
4260912Sbostic #define min(a, b) ((a) < (b) ? (a) : (b))
4360912Sbostic #define max(a, b) ((a) > (b) ? (a) : (b))
4460912Sbostic 
4560912Sbostic #define	FCLOSE(file) {							\
4660912Sbostic 	if (EOF == fclose(file))					\
4760912Sbostic 		err(2, "%s", file);					\
4860912Sbostic }
4960912Sbostic 
5060912Sbostic #define	EWRITE(ptr, size, n, f) {					\
5160912Sbostic 	if (!fwrite(ptr, size, n, f))					\
5260912Sbostic 		 err(2, NULL);						\
5360912Sbostic }
5460912Sbostic 
5560912Sbostic /* length of record is currently limited to 2^16 - 1 */
5660912Sbostic typedef u_short length_t;
5760912Sbostic 
5860912Sbostic #define SALIGN(n) ((n+1) & ~1)
5960912Sbostic 
6060912Sbostic /* a record is a key/line pair starting at rec.data. It has a total length
6160912Sbostic  * and an offset to the start of the line half of the pair.
6260912Sbostic  */
6360912Sbostic typedef struct recheader {
6460912Sbostic 	length_t length;
6560912Sbostic 	length_t offset;
6660912Sbostic 	u_char data[1];
6760912Sbostic } RECHEADER;
6860912Sbostic 
6960912Sbostic typedef struct trecheader {
7060912Sbostic 	length_t length;
7160912Sbostic 	length_t offset;
7260912Sbostic } TRECHEADER;
7360912Sbostic 
7460912Sbostic /* This is the column as seen by struct field.  It is used by enterfield.
7560912Sbostic  * They are matched with corresponding coldescs during initialization.
7660912Sbostic  */
7760912Sbostic struct column {
7860912Sbostic 	struct coldesc *p;
7960912Sbostic 	int num;
8060912Sbostic 	int indent;
8160912Sbostic };
8260912Sbostic 
8360912Sbostic /* a coldesc has a number and pointers to the beginning and end of the
8460912Sbostic  * corresponding column in the current line.  This is determined in enterkey.
8560912Sbostic  */
8660912Sbostic typedef struct coldesc {
8760912Sbostic 	u_char *start;
8860912Sbostic 	u_char *end;
8960912Sbostic 	int num;
9060912Sbostic } COLDESC;
9160912Sbostic 
9260912Sbostic /* A field has an initial and final column; an omitted final column
9360912Sbostic  * implies the end of the line.  Flags regulate omission of blanks and
9460912Sbostic  * numerical sorts; mask determines which characters are ignored (from -i, -d);
9560912Sbostic  * weights determines the sort weights of a character (from -f, -r).
9660912Sbostic  */
9760912Sbostic struct field {
9860912Sbostic 	struct column icol;
9960912Sbostic 	struct column tcol;
10060912Sbostic 	u_int flags;
10160912Sbostic 	u_char *mask;
10260912Sbostic 	u_char *weights;
10360912Sbostic };
10460912Sbostic 
10560912Sbostic union f_handle {
10660912Sbostic 	int top;
10760912Sbostic 	char **names;
10860912Sbostic };
10960912Sbostic extern int PANIC;	/* maximum depth of fsort before fmerge is called */
11060912Sbostic extern u_char ascii[NBINS], Rascii[NBINS], Ftable[NBINS], RFtable[NBINS];
11160912Sbostic extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS];
11260912Sbostic extern u_char d_mask[NBINS];
11360912Sbostic extern int SINGL_FLD, SEP_FLAG, UNIQUE;
11460912Sbostic extern int REC_D;
11560912Sbostic 
11660912Sbostic #include "extern.h"
117