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