1*60912Sbostic /*- 2*60912Sbostic * Copyright (c) 1993 The Regents of the University of California. 3*60912Sbostic * All rights reserved. 4*60912Sbostic * 5*60912Sbostic * This code is derived from software contributed to Berkeley by 6*60912Sbostic * Peter McIlroy. 7*60912Sbostic * 8*60912Sbostic * %sccs.include.redist.c% 9*60912Sbostic * 10*60912Sbostic * @(#)sort.h 5.1 (Berkeley) 06/01/93 11*60912Sbostic */ 12*60912Sbostic 13*60912Sbostic #include <sys/param.h> 14*60912Sbostic 15*60912Sbostic #include <db.h> 16*60912Sbostic #include <err.h> 17*60912Sbostic #include <errno.h> 18*60912Sbostic #include <fcntl.h> 19*60912Sbostic #include <limits.h> 20*60912Sbostic #include <stdio.h> 21*60912Sbostic #include <stdlib.h> 22*60912Sbostic 23*60912Sbostic #define NBINS 256 24*60912Sbostic #define MAXMERGE 16 25*60912Sbostic 26*60912Sbostic /* values for masks, weights, and other flags. */ 27*60912Sbostic #define I 1 /* mask out non-printable characters */ 28*60912Sbostic #define D 2 /* sort alphanumeric characters only */ 29*60912Sbostic #define N 4 /* Field is a number */ 30*60912Sbostic #define F 8 /* weight lower and upper case the same */ 31*60912Sbostic #define R 16 /* Field is reversed with respect to the global weight */ 32*60912Sbostic #define BI 32 /* ignore blanks in icol */ 33*60912Sbostic #define BT 64 /* ignore blanks in tcol */ 34*60912Sbostic 35*60912Sbostic /* masks for delimiters: blanks, fields, and termination. */ 36*60912Sbostic #define BLANK 1 /* ' ', '\t'; '\n' if -T is invoked */ 37*60912Sbostic #define FLD_D 2 /* ' ', '\t' default; from -t otherwise */ 38*60912Sbostic #define REC_D_F 4 /* '\n' default; from -T otherwise */ 39*60912Sbostic 40*60912Sbostic #define ND 10 /* limit on number of -k options. */ 41*60912Sbostic 42*60912Sbostic #define min(a, b) ((a) < (b) ? (a) : (b)) 43*60912Sbostic #define max(a, b) ((a) > (b) ? (a) : (b)) 44*60912Sbostic 45*60912Sbostic #define FCLOSE(file) { \ 46*60912Sbostic if (EOF == fclose(file)) \ 47*60912Sbostic err(2, "%s", file); \ 48*60912Sbostic } 49*60912Sbostic 50*60912Sbostic #define EWRITE(ptr, size, n, f) { \ 51*60912Sbostic if (!fwrite(ptr, size, n, f)) \ 52*60912Sbostic err(2, NULL); \ 53*60912Sbostic } 54*60912Sbostic 55*60912Sbostic /* length of record is currently limited to 2^16 - 1 */ 56*60912Sbostic typedef u_short length_t; 57*60912Sbostic 58*60912Sbostic #define SALIGN(n) ((n+1) & ~1) 59*60912Sbostic 60*60912Sbostic /* a record is a key/line pair starting at rec.data. It has a total length 61*60912Sbostic * and an offset to the start of the line half of the pair. 62*60912Sbostic */ 63*60912Sbostic typedef struct recheader { 64*60912Sbostic length_t length; 65*60912Sbostic length_t offset; 66*60912Sbostic u_char data[1]; 67*60912Sbostic } RECHEADER; 68*60912Sbostic 69*60912Sbostic typedef struct trecheader { 70*60912Sbostic length_t length; 71*60912Sbostic length_t offset; 72*60912Sbostic } TRECHEADER; 73*60912Sbostic 74*60912Sbostic /* This is the column as seen by struct field. It is used by enterfield. 75*60912Sbostic * They are matched with corresponding coldescs during initialization. 76*60912Sbostic */ 77*60912Sbostic struct column { 78*60912Sbostic struct coldesc *p; 79*60912Sbostic int num; 80*60912Sbostic int indent; 81*60912Sbostic }; 82*60912Sbostic 83*60912Sbostic /* a coldesc has a number and pointers to the beginning and end of the 84*60912Sbostic * corresponding column in the current line. This is determined in enterkey. 85*60912Sbostic */ 86*60912Sbostic typedef struct coldesc { 87*60912Sbostic u_char *start; 88*60912Sbostic u_char *end; 89*60912Sbostic int num; 90*60912Sbostic } COLDESC; 91*60912Sbostic 92*60912Sbostic /* A field has an initial and final column; an omitted final column 93*60912Sbostic * implies the end of the line. Flags regulate omission of blanks and 94*60912Sbostic * numerical sorts; mask determines which characters are ignored (from -i, -d); 95*60912Sbostic * weights determines the sort weights of a character (from -f, -r). 96*60912Sbostic */ 97*60912Sbostic struct field { 98*60912Sbostic struct column icol; 99*60912Sbostic struct column tcol; 100*60912Sbostic u_int flags; 101*60912Sbostic u_char *mask; 102*60912Sbostic u_char *weights; 103*60912Sbostic }; 104*60912Sbostic 105*60912Sbostic union f_handle { 106*60912Sbostic int top; 107*60912Sbostic char **names; 108*60912Sbostic }; 109*60912Sbostic extern int PANIC; /* maximum depth of fsort before fmerge is called */ 110*60912Sbostic extern u_char ascii[NBINS], Rascii[NBINS], Ftable[NBINS], RFtable[NBINS]; 111*60912Sbostic extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS]; 112*60912Sbostic extern u_char d_mask[NBINS]; 113*60912Sbostic extern int SINGL_FLD, SEP_FLAG, UNIQUE; 114*60912Sbostic extern int REC_D; 115*60912Sbostic 116*60912Sbostic #include "extern.h" 117