1 /* $NetBSD: init.c,v 1.7 2002/12/24 13:20:25 jdolecek 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include "sort.h" 40 41 #ifndef lint 42 __RCSID("$NetBSD: init.c,v 1.7 2002/12/24 13:20:25 jdolecek Exp $"); 43 __SCCSID("@(#)init.c 8.1 (Berkeley) 6/6/93"); 44 #endif /* not lint */ 45 46 #include <ctype.h> 47 #include <string.h> 48 49 static void insertcol __P((struct field *)); 50 static const char *setcolumn __P((const char *, struct field *, int)); 51 int setfield __P((const char *, struct field *, int)); 52 53 u_char gweights[NBINS]; 54 55 /* 56 * masks of ignored characters. Alltable is 256 ones. 57 */ 58 static u_char alltable[NBINS], dtable[NBINS], itable[NBINS]; 59 60 /* 61 * clist (list of columns which correspond to one or more icol or tcol) 62 * is in increasing order of columns. 63 * Fields are kept in increasing order of fields. 64 */ 65 66 /* 67 * keep clist in order--inserts a column in a sorted array 68 */ 69 static void 70 insertcol(field) 71 struct field *field; 72 { 73 int i; 74 for (i = 0; i < ncols; i++) 75 if (field->icol.num <= clist[i].num) 76 break; 77 if (field->icol.num != clist[i].num) { 78 memmove(clist+i+1, clist+i, sizeof(COLDESC)*(ncols-i)); 79 clist[i].num = field->icol.num; 80 ncols++; 81 } 82 if (field->tcol.num && field->tcol.num != field->icol.num) { 83 for (i = 0; i < ncols; i++) 84 if (field->tcol.num <= clist[i].num) 85 break; 86 if (field->tcol.num != clist[i].num) { 87 memmove(clist+i+1, clist+i,sizeof(COLDESC)*(ncols-i)); 88 clist[i].num = field->tcol.num; 89 ncols++; 90 } 91 } 92 } 93 94 /* 95 * matches fields with the appropriate columns--n^2 but who cares? 96 */ 97 void 98 fldreset(fldtab) 99 struct field *fldtab; 100 { 101 int i; 102 fldtab[0].tcol.p = clist+ncols-1; 103 for (++fldtab; fldtab->icol.num; ++fldtab) { 104 for (i = 0; fldtab->icol.num != clist[i].num; i++) 105 ; 106 fldtab->icol.p = clist + i; 107 if (!fldtab->tcol.num) 108 continue; 109 for (i = 0; fldtab->tcol.num != clist[i].num; i++) 110 ; 111 fldtab->tcol.p = clist + i; 112 } 113 } 114 115 /* 116 * interprets a column in a -k field 117 */ 118 static const char * 119 setcolumn(pos, cur_fld, gflag) 120 const char *pos; 121 struct field *cur_fld; 122 int gflag; 123 { 124 struct column *col; 125 int tmp; 126 col = cur_fld->icol.num ? (&(*cur_fld).tcol) : (&(*cur_fld).icol); 127 pos += sscanf(pos, "%d", &(col->num)); 128 while (isdigit(*pos)) 129 pos++; 130 if (col->num <= 0 && !(col->num == 0 && col == &(cur_fld->tcol))) 131 errx(2, "field numbers must be positive"); 132 if (*pos == '.') { 133 if (!col->num) 134 errx(2, "cannot indent end of line"); 135 ++pos; 136 pos += sscanf(pos, "%d", &(col->indent)); 137 while (isdigit(*pos)) 138 pos++; 139 if (&cur_fld->icol == col) 140 col->indent--; 141 if (col->indent < 0) 142 errx(2, "illegal offset"); 143 } 144 if (optval(*pos, cur_fld->tcol.num)) 145 while ((tmp = optval(*pos, cur_fld->tcol.num))) { 146 cur_fld->flags |= tmp; 147 pos++; 148 } 149 if (cur_fld->icol.num == 0) 150 cur_fld->icol.num = 1; 151 return (pos); 152 } 153 154 int 155 setfield(pos, cur_fld, gflag) 156 const char *pos; 157 struct field *cur_fld; 158 int gflag; 159 { 160 static int nfields = 0; 161 int tmp; 162 163 if (++nfields == ND) 164 errx(2, "too many sort keys. (Limit is %d)", ND-1); 165 166 cur_fld->weights = ascii; 167 cur_fld->mask = alltable; 168 169 pos = setcolumn(pos, cur_fld, gflag); 170 if (*pos == '\0') /* key extends to EOL. */ 171 cur_fld->tcol.num = 0; 172 else { 173 if (*pos != ',') 174 errx(2, "illegal field descriptor"); 175 setcolumn((++pos), cur_fld, gflag); 176 } 177 if (!cur_fld->flags) 178 cur_fld->flags = gflag; 179 tmp = cur_fld->flags; 180 181 /* 182 * Assign appropriate mask table and weight table. 183 * If the global weights are reversed, the local field 184 * must be "re-reversed". 185 */ 186 if (((tmp & R) ^ (gflag & R)) && (tmp & F)) 187 cur_fld->weights = RFtable; 188 else if (tmp & F) 189 cur_fld->weights = Ftable; 190 else if ((tmp & R) ^ (gflag & R)) 191 cur_fld->weights = Rascii; 192 193 if (tmp & I) 194 cur_fld->mask = itable; 195 else if (tmp & D) 196 cur_fld->mask = dtable; 197 198 cur_fld->flags |= (gflag & (BI | BT)); 199 if (!cur_fld->tcol.indent) /* BT has no meaning at end of field */ 200 cur_fld->flags &= ~BT; 201 202 if (cur_fld->tcol.num && !(!(cur_fld->flags & BI) 203 && cur_fld->flags & BT) && (cur_fld->tcol.num <= cur_fld->icol.num 204 && cur_fld->tcol.indent < cur_fld->icol.indent)) 205 errx(2, "fields out of order"); 206 insertcol(cur_fld); 207 return (cur_fld->tcol.num); 208 } 209 210 int 211 optval(desc, tcolflag) 212 int desc, tcolflag; 213 { 214 switch(desc) { 215 case 'b': 216 if (!tcolflag) 217 return (BI); 218 else 219 return (BT); 220 case 'd': return (D); 221 case 'f': return (F); 222 case 'i': return (I); 223 case 'n': return (N); 224 case 'r': return (R); 225 default: return (0); 226 } 227 } 228 229 void 230 fixit(argc, argv) 231 int *argc; 232 char **argv; 233 { 234 int i, j, v, w, x; 235 static char vbuf[ND*20], *vpos, *tpos; 236 vpos = vbuf; 237 238 for (i = 1; i < *argc; i++) { 239 if (argv[i][0] == '+') { 240 tpos = argv[i]+1; 241 argv[i] = vpos; 242 vpos += sprintf(vpos, "-k"); 243 tpos += sscanf(tpos, "%d", &v); 244 while (isdigit(*tpos)) 245 tpos++; 246 vpos += sprintf(vpos, "%d", v+1); 247 if (*tpos == '.') { 248 ++tpos; 249 tpos += sscanf(tpos, "%d", &x); 250 vpos += sprintf(vpos, ".%d", x+1); 251 } 252 while (*tpos) 253 *vpos++ = *tpos++; 254 vpos += sprintf(vpos, ","); 255 if (argv[i+1] && 256 argv[i+1][0] == '-' && isdigit(argv[i+1][1])) { 257 tpos = argv[i+1] + 1; 258 tpos += sscanf(tpos, "%d", &w); 259 while (isdigit(*tpos)) 260 tpos++; 261 x = 0; 262 if (*tpos == '.') { 263 ++tpos; 264 tpos += sscanf(tpos, "%d", &x); 265 while (isdigit(*tpos)) 266 tpos++; 267 } 268 if (x) { 269 vpos += sprintf(vpos, "%d", w+1); 270 vpos += sprintf(vpos, ".%d", x); 271 } else 272 vpos += sprintf(vpos, "%d", w); 273 while (*tpos) 274 *vpos++ = *tpos++; 275 for (j= i+1; j < *argc; j++) 276 argv[j] = argv[j+1]; 277 *argc -= 1; 278 } 279 } 280 } 281 } 282 283 /* 284 * ascii, Rascii, Ftable, and RFtable map 285 * REC_D -> REC_D; {not REC_D} -> {not REC_D}. 286 * gweights maps REC_D -> (0 or 255); {not REC_D} -> {not gweights[REC_D]}. 287 * Note: when sorting in forward order, to encode character zero in a key, 288 * use \001\001; character 1 becomes \001\002. In this case, character 0 289 * is reserved for the field delimiter. Analagously for -r (fld_d = 255). 290 * Note: this is only good for ASCII sorting. For different LC 's, 291 * all bets are off. See also num_init in number.c 292 */ 293 void 294 settables(gflags) 295 int gflags; 296 { 297 u_char *wts; 298 int i, incr; 299 for (i=0; i < 256; i++) { 300 ascii[i] = i; 301 if (i > REC_D && i < 255 - REC_D+1) 302 Rascii[i] = 255 - i + 1; 303 else 304 Rascii[i] = 255 - i; 305 if (islower(i)) { 306 Ftable[i] = Ftable[toupper(i)]; 307 RFtable[i] = RFtable[toupper(i)]; 308 } else if (REC_D>= 'A' && REC_D < 'Z' && i < 'a' && i > REC_D) { 309 Ftable[i] = i + 1; 310 RFtable[i] = Rascii[i] - 1; 311 } else { 312 Ftable[i] = i; 313 RFtable[i] = Rascii[i]; 314 } 315 alltable[i] = 1; 316 317 if (i == '\n' || isprint(i)) 318 itable[i] = 1; 319 else 320 itable[i] = 0; 321 322 if (i == '\n' || i == '\t' || i == ' ' || isalnum(i)) 323 dtable[i] = 1; 324 else 325 dtable[i] = 0; 326 } 327 328 Rascii[REC_D] = RFtable[REC_D] = REC_D; 329 if (isupper(REC_D)) 330 Ftable[tolower(REC_D)]++; 331 332 if ((gflags & R) && !((gflags & F) && SINGL_FLD)) 333 wts = Rascii; 334 else if (!((gflags & F) && SINGL_FLD)) 335 wts = ascii; 336 else if (gflags & R) 337 wts = RFtable; 338 else 339 wts = Ftable; 340 341 memmove(gweights, wts, sizeof(gweights)); 342 incr = (gflags & R) ? -1 : 1; 343 for (i = 0; i < REC_D; i++) 344 gweights[i] += incr; 345 gweights[REC_D] = ((gflags & R) ? 255 : 0); 346 if (SINGL_FLD && (gflags & F)) { 347 for (i = 0; i < REC_D; i++) { 348 ascii[i] += incr; 349 Rascii[i] += incr; 350 } 351 ascii[REC_D] = Rascii[REC_D] = gweights[REC_D]; 352 } 353 } 354