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