1 /* $OpenBSD: sort.h,v 1.7 2007/08/21 20:29:25 millert 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 #define MAXMERGE 16 50 51 /* values for masks, weights, and other flags. */ 52 #define I 1 /* mask out non-printable characters */ 53 #define D 2 /* sort alphanumeric characters only */ 54 #define N 4 /* Field is a number */ 55 #define F 8 /* weight lower and upper case the same */ 56 #define R 16 /* Field is reversed with respect to the global weight */ 57 #define BI 32 /* ignore blanks in icol */ 58 #define BT 64 /* ignore blanks in tcol */ 59 60 /* masks for delimiters: blanks, fields, and termination. */ 61 #define BLANK 1 /* ' ', '\t'; '\n' if -T is invoked */ 62 #define FLD_D 2 /* ' ', '\t' default; from -t otherwise */ 63 #define REC_D_F 4 /* '\n' default; from -T otherwise */ 64 65 #define min(a, b) ((a) < (b) ? (a) : (b)) 66 #define max(a, b) ((a) > (b) ? (a) : (b)) 67 68 #define FCLOSE(file) { \ 69 if (EOF == fclose(file)) \ 70 err(2, "fclose"); \ 71 } 72 73 #define EWRITE(ptr, size, n, f) { \ 74 if (!fwrite(ptr, size, n, f)) \ 75 err(2, "fwrite"); \ 76 } 77 78 /* length of record is currently limited to maximum string length (size_t) */ 79 typedef size_t length_t; 80 81 #define SALIGN(n) ((n+(sizeof(length_t)-1)) & ~(sizeof(length_t)-1)) 82 83 /* a record is a key/line pair starting at rec.data. It has a total length 84 * and an offset to the start of the line half of the pair. 85 */ 86 typedef struct recheader { 87 length_t length; 88 length_t offset; 89 u_char data[1]; 90 } RECHEADER; 91 92 typedef struct trecheader { 93 length_t length; 94 length_t offset; 95 } TRECHEADER; 96 97 /* This is the column as seen by struct field. It is used by enterfield. 98 * They are matched with corresponding coldescs during initialization. 99 */ 100 struct column { 101 struct coldesc *p; 102 int num; 103 int indent; 104 }; 105 106 /* a coldesc has a number and pointers to the beginning and end of the 107 * corresponding column in the current line. This is determined in enterkey. 108 */ 109 typedef struct coldesc { 110 u_char *start; 111 u_char *end; 112 int num; 113 } COLDESC; 114 115 /* A field has an initial and final column; an omitted final column 116 * implies the end of the line. Flags regulate omission of blanks and 117 * numerical sorts; mask determines which characters are ignored (from -i, -d); 118 * weights determines the sort weights of a character (from -f, -r). 119 */ 120 struct field { 121 struct column icol; 122 struct column tcol; 123 u_int flags; 124 u_char *mask; 125 u_char *weights; 126 }; 127 128 union f_handle { 129 int top; 130 char **names; 131 }; 132 extern int PANIC; /* maximum depth of fsort before fmerge is called */ 133 extern u_char ascii[NBINS], Rascii[NBINS], Ftable[NBINS], RFtable[NBINS]; 134 extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS]; 135 extern u_char d_mask[NBINS]; 136 extern int SINGL_FLD, SEP_FLAG, UNIQUE, STABLE; 137 extern int REC_D; 138 extern char *tmpdir; 139 extern int ND; /* limit on number of -k options. */ 140 141 #include "extern.h" 142