cut.c revision 131194
1/* 2 * Copyright (c) 1989, 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 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 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 37#ifndef lint 38static const char copyright[] = 39"@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 42#endif /* not lint */ 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: head/usr.bin/cut/cut.c 131194 2004-06-27 14:55:07Z tjr $"); 45 46#include <ctype.h> 47#include <err.h> 48#include <limits.h> 49#include <locale.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <string.h> 53#include <unistd.h> 54#include <wchar.h> 55 56int bflag; 57int cflag; 58char dchar; 59int dflag; 60int fflag; 61int nflag; 62int sflag; 63 64int b_cut(FILE *, const char *); 65int b_n_cut(FILE *, const char *); 66int c_cut(FILE *, const char *); 67int f_cut(FILE *, const char *); 68void get_list(char *); 69void needpos(size_t); 70static void usage(void); 71 72int 73main(int argc, char *argv[]) 74{ 75 FILE *fp; 76 int (*fcn)(FILE *, const char *); 77 int ch, rval; 78 79 setlocale(LC_ALL, ""); 80 81 fcn = NULL; 82 dchar = '\t'; /* default delimiter is \t */ 83 84 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) 85 switch(ch) { 86 case 'b': 87 get_list(optarg); 88 bflag = 1; 89 break; 90 case 'c': 91 get_list(optarg); 92 cflag = 1; 93 break; 94 case 'd': 95 dchar = *optarg; 96 dflag = 1; 97 break; 98 case 'f': 99 get_list(optarg); 100 fflag = 1; 101 break; 102 case 's': 103 sflag = 1; 104 break; 105 case 'n': 106 nflag = 1; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 if (fflag) { 116 if (bflag || cflag || nflag) 117 usage(); 118 } else if (!(bflag || cflag) || dflag || sflag) 119 usage(); 120 else if (!bflag && nflag) 121 usage(); 122 123 if (fflag) 124 fcn = f_cut; 125 else if (cflag) 126 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut; 127 else if (bflag) 128 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut; 129 130 rval = 0; 131 if (*argv) 132 for (; *argv; ++argv) { 133 if (strcmp(*argv, "-") == 0) 134 rval |= fcn(stdin, "stdin"); 135 else { 136 if (!(fp = fopen(*argv, "r"))) { 137 warn("%s", *argv); 138 rval = 1; 139 continue; 140 } 141 fcn(fp, *argv); 142 (void)fclose(fp); 143 } 144 } 145 else 146 rval = fcn(stdin, "stdin"); 147 exit(rval); 148} 149 150size_t autostart, autostop, maxval; 151 152char *positions; 153 154void 155get_list(char *list) 156{ 157 size_t setautostart, start, stop; 158 char *pos; 159 char *p; 160 161 /* 162 * set a byte in the positions array to indicate if a field or 163 * column is to be selected; use +1, it's 1-based, not 0-based. 164 * This parser is less restrictive than the Draft 9 POSIX spec. 165 * POSIX doesn't allow lists that aren't in increasing order or 166 * overlapping lists. We also handle "-3-5" although there's no 167 * real reason too. 168 */ 169 for (; (p = strsep(&list, ", \t")) != NULL;) { 170 setautostart = start = stop = 0; 171 if (*p == '-') { 172 ++p; 173 setautostart = 1; 174 } 175 if (isdigit((unsigned char)*p)) { 176 start = stop = strtol(p, &p, 10); 177 if (setautostart && start > autostart) 178 autostart = start; 179 } 180 if (*p == '-') { 181 if (isdigit((unsigned char)p[1])) 182 stop = strtol(p + 1, &p, 10); 183 if (*p == '-') { 184 ++p; 185 if (!autostop || autostop > stop) 186 autostop = stop; 187 } 188 } 189 if (*p) 190 errx(1, "[-cf] list: illegal list value"); 191 if (!stop || !start) 192 errx(1, "[-cf] list: values may not include zero"); 193 if (maxval < stop) { 194 maxval = stop; 195 needpos(maxval + 1); 196 } 197 for (pos = positions + start; start++ <= stop; *pos++ = 1); 198 } 199 200 /* overlapping ranges */ 201 if (autostop && maxval > autostop) { 202 maxval = autostop; 203 needpos(maxval + 1); 204 } 205 206 /* set autostart */ 207 if (autostart) 208 memset(positions + 1, '1', autostart); 209} 210 211void 212needpos(size_t n) 213{ 214 static size_t npos; 215 size_t oldnpos; 216 217 /* Grow the positions array to at least the specified size. */ 218 if (n > npos) { 219 oldnpos = npos; 220 if (npos == 0) 221 npos = n; 222 while (n > npos) 223 npos *= 2; 224 if ((positions = realloc(positions, npos)) == NULL) 225 err(1, "realloc"); 226 memset((char *)positions + oldnpos, 0, npos - oldnpos); 227 } 228} 229 230int 231b_cut(FILE *fp, const char *fname) 232{ 233 int ch, col; 234 char *pos; 235 236 ch = 0; 237 for (;;) { 238 pos = positions + 1; 239 for (col = maxval; col; --col) { 240 if ((ch = getc(fp)) == EOF) 241 return (0); 242 if (ch == '\n') 243 break; 244 if (*pos++) 245 (void)putchar(ch); 246 } 247 if (ch != '\n') { 248 if (autostop) 249 while ((ch = getc(fp)) != EOF && ch != '\n') 250 (void)putchar(ch); 251 else 252 while ((ch = getc(fp)) != EOF && ch != '\n'); 253 } 254 (void)putchar('\n'); 255 } 256 return (0); 257} 258 259/* 260 * Cut based on byte positions, taking care not to split multibyte characters. 261 * Although this function also handles the case where -n is not specified, 262 * b_cut() ought to be much faster. 263 */ 264int 265b_n_cut(FILE *fp, const char *fname) 266{ 267 size_t col, i, lbuflen; 268 char *lbuf; 269 int canwrite, clen, warned; 270 mbstate_t mbs; 271 272 memset(&mbs, 0, sizeof(mbs)); 273 warned = 0; 274 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { 275 for (col = 0; lbuflen > 0; col += clen) { 276 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) { 277 if (!warned) { 278 warn("%s", fname); 279 warned = 1; 280 } 281 memset(&mbs, 0, sizeof(mbs)); 282 clen = 1; 283 } 284 if (clen == 0 || *lbuf == '\n') 285 break; 286 if (col < maxval && !positions[1 + col]) { 287 /* 288 * Print the character if (1) after an initial 289 * segment of un-selected bytes, the rest of 290 * it is selected, and (2) the last byte is 291 * selected. 292 */ 293 i = col; 294 while (i < col + clen && i < maxval && 295 !positions[1 + i]) 296 i++; 297 canwrite = i < col + clen; 298 for (; i < col + clen && i < maxval; i++) 299 canwrite &= positions[1 + i]; 300 if (canwrite) 301 fwrite(lbuf, 1, clen, stdout); 302 } else { 303 /* 304 * Print the character if all of it has 305 * been selected. 306 */ 307 canwrite = 1; 308 for (i = col; i < col + clen; i++) 309 if ((i >= maxval && !autostop) || 310 (i < maxval && !positions[1 + i])) { 311 canwrite = 0; 312 break; 313 } 314 if (canwrite) 315 fwrite(lbuf, 1, clen, stdout); 316 } 317 lbuf += clen; 318 lbuflen -= clen; 319 } 320 if (lbuflen > 0) 321 putchar('\n'); 322 } 323 return (warned); 324} 325 326int 327c_cut(FILE *fp, const char *fname) 328{ 329 wint_t ch; 330 int col; 331 char *pos; 332 333 ch = 0; 334 for (;;) { 335 pos = positions + 1; 336 for (col = maxval; col; --col) { 337 if ((ch = getwc(fp)) == WEOF) 338 goto out; 339 if (ch == '\n') 340 break; 341 if (*pos++) 342 (void)putwchar(ch); 343 } 344 if (ch != '\n') { 345 if (autostop) 346 while ((ch = getwc(fp)) != WEOF && ch != '\n') 347 (void)putwchar(ch); 348 else 349 while ((ch = getwc(fp)) != WEOF && ch != '\n'); 350 } 351 (void)putwchar('\n'); 352 } 353out: 354 if (ferror(fp)) { 355 warn("%s", fname); 356 return (1); 357 } 358 return (0); 359} 360 361int 362f_cut(FILE *fp, const char *fname __unused) 363{ 364 int ch, field, isdelim; 365 char *pos, *p, sep; 366 int output; 367 char *lbuf, *mlbuf; 368 size_t lbuflen; 369 370 mlbuf = NULL; 371 for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) { 372 /* Assert EOL has a newline. */ 373 if (*(lbuf + lbuflen - 1) != '\n') { 374 /* Can't have > 1 line with no trailing newline. */ 375 mlbuf = malloc(lbuflen + 1); 376 if (mlbuf == NULL) 377 err(1, "malloc"); 378 memcpy(mlbuf, lbuf, lbuflen); 379 *(mlbuf + lbuflen) = '\n'; 380 lbuf = mlbuf; 381 } 382 output = 0; 383 for (isdelim = 0, p = lbuf;; ++p) { 384 ch = *p; 385 /* this should work if newline is delimiter */ 386 if (ch == sep) 387 isdelim = 1; 388 if (ch == '\n') { 389 if (!isdelim && !sflag) 390 (void)fwrite(lbuf, lbuflen, 1, stdout); 391 break; 392 } 393 } 394 if (!isdelim) 395 continue; 396 397 pos = positions + 1; 398 for (field = maxval, p = lbuf; field; --field, ++pos) { 399 if (*pos) { 400 if (output++) 401 (void)putchar(sep); 402 while ((ch = *p++) != '\n' && ch != sep) 403 (void)putchar(ch); 404 } else { 405 while ((ch = *p++) != '\n' && ch != sep) 406 continue; 407 } 408 if (ch == '\n') 409 break; 410 } 411 if (ch != '\n') { 412 if (autostop) { 413 if (output) 414 (void)putchar(sep); 415 for (; (ch = *p) != '\n'; ++p) 416 (void)putchar(ch); 417 } else 418 for (; (ch = *p) != '\n'; ++p); 419 } 420 (void)putchar('\n'); 421 } 422 if (mlbuf != NULL) 423 free(mlbuf); 424 return (0); 425} 426 427static void 428usage(void) 429{ 430 (void)fprintf(stderr, "%s\n%s\n%s\n", 431 "usage: cut -b list [-n] [file ...]", 432 " cut -c list [file ...]", 433 " cut -f list [-s] [-d delim] [file ...]"); 434 exit(1); 435} 436