Home | History | Annotate | Line # | Download | only in sort
sort.h revision 1.1
      1 /*-
      2  * Copyright (c) 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Peter McIlroy.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)sort.h	8.1 (Berkeley) 6/6/93
     37  */
     38 
     39 #include <sys/param.h>
     40 
     41 #include <db.h>
     42 #include <err.h>
     43 #include <errno.h>
     44 #include <fcntl.h>
     45 #include <limits.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 
     49 #define NBINS 256
     50 #define MAXMERGE 16
     51 
     52 /* values for masks, weights, and other flags. */
     53 #define I 1		/* mask out non-printable characters */
     54 #define D 2		/* sort alphanumeric characters only */
     55 #define N 4		/* Field is a number */
     56 #define F 8		/* weight lower and upper case the same */
     57 #define R 16		/* Field is reversed with respect to the global weight */
     58 #define BI 32		/* ignore blanks in icol */
     59 #define BT 64		/* ignore blanks in tcol */
     60 
     61 /* masks for delimiters: blanks, fields, and termination. */
     62 #define BLANK 1		/* ' ', '\t'; '\n' if -T is invoked */
     63 #define FLD_D 2		/* ' ', '\t' default; from -t otherwise */
     64 #define REC_D_F 4	/* '\n' default; from -T otherwise */
     65 
     66 #define ND 10	/* limit on number of -k options. */
     67 
     68 #define min(a, b) ((a) < (b) ? (a) : (b))
     69 #define max(a, b) ((a) > (b) ? (a) : (b))
     70 
     71 #define	FCLOSE(file) {							\
     72 	if (EOF == fclose(file))					\
     73 		err(2, "%s", file);					\
     74 }
     75 
     76 #define	EWRITE(ptr, size, n, f) {					\
     77 	if (!fwrite(ptr, size, n, f))					\
     78 		 err(2, NULL);						\
     79 }
     80 
     81 /* length of record is currently limited to 2^16 - 1 */
     82 typedef u_short length_t;
     83 
     84 #define SALIGN(n) ((n+1) & ~1)
     85 
     86 /* a record is a key/line pair starting at rec.data. It has a total length
     87  * and an offset to the start of the line half of the pair.
     88  */
     89 typedef struct recheader {
     90 	length_t length;
     91 	length_t offset;
     92 	u_char data[1];
     93 } RECHEADER;
     94 
     95 typedef struct trecheader {
     96 	length_t length;
     97 	length_t offset;
     98 } TRECHEADER;
     99 
    100 /* This is the column as seen by struct field.  It is used by enterfield.
    101  * They are matched with corresponding coldescs during initialization.
    102  */
    103 struct column {
    104 	struct coldesc *p;
    105 	int num;
    106 	int indent;
    107 };
    108 
    109 /* a coldesc has a number and pointers to the beginning and end of the
    110  * corresponding column in the current line.  This is determined in enterkey.
    111  */
    112 typedef struct coldesc {
    113 	u_char *start;
    114 	u_char *end;
    115 	int num;
    116 } COLDESC;
    117 
    118 /* A field has an initial and final column; an omitted final column
    119  * implies the end of the line.  Flags regulate omission of blanks and
    120  * numerical sorts; mask determines which characters are ignored (from -i, -d);
    121  * weights determines the sort weights of a character (from -f, -r).
    122  */
    123 struct field {
    124 	struct column icol;
    125 	struct column tcol;
    126 	u_int flags;
    127 	u_char *mask;
    128 	u_char *weights;
    129 };
    130 
    131 union f_handle {
    132 	int top;
    133 	char **names;
    134 };
    135 extern int PANIC;	/* maximum depth of fsort before fmerge is called */
    136 extern u_char ascii[NBINS], Rascii[NBINS], Ftable[NBINS], RFtable[NBINS];
    137 extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS];
    138 extern u_char d_mask[NBINS];
    139 extern int SINGL_FLD, SEP_FLAG, UNIQUE;
    140 extern int REC_D;
    141 
    142 #include "extern.h"
    143