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