1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 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 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 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 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1989, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; 40 #endif /* not lint */ 41 #include <sys/cdefs.h> 42 #include <ctype.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <limits.h> 46 #include <locale.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <wchar.h> 52 53 static int bflag; 54 static int cflag; 55 static wchar_t dchar; 56 static char dcharmb[MB_LEN_MAX + 1]; 57 static int dflag; 58 static int fflag; 59 static int nflag; 60 static int sflag; 61 static int wflag; 62 63 static size_t autostart, autostop, maxval; 64 static char * positions; 65 66 static int b_cut(FILE *, const char *); 67 static int b_n_cut(FILE *, const char *); 68 static int c_cut(FILE *, const char *); 69 static int f_cut(FILE *, const char *); 70 static void get_list(char *); 71 static int is_delim(wchar_t); 72 static void needpos(size_t); 73 static void usage(void); 74 75 int 76 main(int argc, char *argv[]) 77 { 78 FILE *fp; 79 int (*fcn)(FILE *, const char *); 80 int ch, rval; 81 size_t n; 82 83 setlocale(LC_ALL, ""); 84 85 fcn = NULL; 86 dchar = '\t'; /* default delimiter is \t */ 87 strcpy(dcharmb, "\t"); 88 89 while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1) 90 switch(ch) { 91 case 'b': 92 get_list(optarg); 93 bflag = 1; 94 break; 95 case 'c': 96 get_list(optarg); 97 cflag = 1; 98 break; 99 case 'd': 100 n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL); 101 if (dchar == '\0' || n != strlen(optarg)) 102 errx(1, "bad delimiter"); 103 strcpy(dcharmb, optarg); 104 dflag = 1; 105 break; 106 case 'f': 107 get_list(optarg); 108 fflag = 1; 109 break; 110 case 's': 111 sflag = 1; 112 break; 113 case 'n': 114 nflag = 1; 115 break; 116 case 'w': 117 wflag = 1; 118 break; 119 case '?': 120 default: 121 usage(); 122 } 123 argc -= optind; 124 argv += optind; 125 126 if (fflag) { 127 if (bflag || cflag || nflag || (wflag && dflag)) 128 usage(); 129 } else if (!(bflag || cflag) || dflag || sflag || wflag) 130 usage(); 131 else if (!bflag && nflag) 132 usage(); 133 134 if (fflag) 135 fcn = f_cut; 136 else if (cflag) 137 fcn = MB_CUR_MAX > 1 ? c_cut : b_cut; 138 else if (bflag) 139 fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut; 140 141 rval = 0; 142 if (*argv) 143 for (; *argv; ++argv) { 144 if (strcmp(*argv, "-") == 0) 145 rval |= fcn(stdin, "stdin"); 146 else { 147 if (!(fp = fopen(*argv, "r"))) { 148 warn("%s", *argv); 149 rval = 1; 150 continue; 151 } 152 fcn(fp, *argv); 153 (void)fclose(fp); 154 } 155 } 156 else 157 rval = fcn(stdin, "stdin"); 158 exit(rval); 159 } 160 161 static void 162 get_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 to. 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, "[-bcf] list: illegal list value"); 196 if (!stop || !start) 197 errx(1, "[-bcf] 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 /* reversed range with autostart */ 212 if (maxval < autostart) { 213 maxval = autostart; 214 needpos(maxval + 1); 215 } 216 217 /* set autostart */ 218 if (autostart) 219 memset(positions + 1, '1', autostart); 220 } 221 222 static void 223 needpos(size_t n) 224 { 225 static size_t npos; 226 size_t oldnpos; 227 228 /* Grow the positions array to at least the specified size. */ 229 if (n > npos) { 230 oldnpos = npos; 231 if (npos == 0) 232 npos = n; 233 while (n > npos) 234 npos *= 2; 235 if ((positions = realloc(positions, npos)) == NULL) 236 err(1, "realloc"); 237 memset((char *)positions + oldnpos, 0, npos - oldnpos); 238 } 239 } 240 241 static int 242 b_cut(FILE *fp, const char *fname __unused) 243 { 244 int ch, col; 245 char *pos; 246 247 ch = 0; 248 for (;;) { 249 pos = positions + 1; 250 for (col = maxval; col; --col) { 251 if ((ch = getc(fp)) == EOF) 252 return (0); 253 if (ch == '\n') 254 break; 255 if (*pos++) 256 (void)putchar(ch); 257 } 258 if (ch != '\n') { 259 if (autostop) 260 while ((ch = getc(fp)) != EOF && ch != '\n') 261 (void)putchar(ch); 262 else 263 while ((ch = getc(fp)) != EOF && ch != '\n'); 264 } 265 (void)putchar('\n'); 266 } 267 return (0); 268 } 269 270 /* 271 * Cut based on byte positions, taking care not to split multibyte characters. 272 * Although this function also handles the case where -n is not specified, 273 * b_cut() ought to be much faster. 274 */ 275 static int 276 b_n_cut(FILE *fp, const char *fname) 277 { 278 size_t col, i, lbuflen; 279 char *lbuf; 280 int canwrite, clen, warned; 281 mbstate_t mbs; 282 283 memset(&mbs, 0, sizeof(mbs)); 284 warned = 0; 285 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { 286 for (col = 0; lbuflen > 0; col += clen) { 287 if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) { 288 if (!warned) { 289 warn("%s", fname); 290 warned = 1; 291 } 292 memset(&mbs, 0, sizeof(mbs)); 293 clen = 1; 294 } 295 if (clen == 0 || *lbuf == '\n') 296 break; 297 if (col < maxval && !positions[1 + col]) { 298 /* 299 * Print the character if (1) after an initial 300 * segment of un-selected bytes, the rest of 301 * it is selected, and (2) the last byte is 302 * selected. 303 */ 304 i = col; 305 while (i < col + clen && i < maxval && 306 !positions[1 + i]) 307 i++; 308 canwrite = i < col + clen; 309 for (; i < col + clen && i < maxval; i++) 310 canwrite &= positions[1 + i]; 311 if (canwrite) 312 fwrite(lbuf, 1, clen, stdout); 313 } else { 314 /* 315 * Print the character if all of it has 316 * been selected. 317 */ 318 canwrite = 1; 319 for (i = col; i < col + clen; i++) 320 if ((i >= maxval && !autostop) || 321 (i < maxval && !positions[1 + i])) { 322 canwrite = 0; 323 break; 324 } 325 if (canwrite) 326 fwrite(lbuf, 1, clen, stdout); 327 } 328 lbuf += clen; 329 lbuflen -= clen; 330 } 331 if (lbuflen > 0) 332 putchar('\n'); 333 } 334 return (warned); 335 } 336 337 static int 338 c_cut(FILE *fp, const char *fname) 339 { 340 wint_t ch; 341 int col; 342 char *pos; 343 344 ch = 0; 345 for (;;) { 346 pos = positions + 1; 347 for (col = maxval; col; --col) { 348 if ((ch = getwc(fp)) == WEOF) 349 goto out; 350 if (ch == '\n') 351 break; 352 if (*pos++) 353 (void)putwchar(ch); 354 } 355 if (ch != '\n') { 356 if (autostop) 357 while ((ch = getwc(fp)) != WEOF && ch != '\n') 358 (void)putwchar(ch); 359 else 360 while ((ch = getwc(fp)) != WEOF && ch != '\n'); 361 } 362 (void)putwchar('\n'); 363 } 364 out: 365 if (ferror(fp)) { 366 warn("%s", fname); 367 return (1); 368 } 369 return (0); 370 } 371 372 static int 373 is_delim(wchar_t ch) 374 { 375 if (wflag) { 376 if (ch == ' ' || ch == '\t') 377 return 1; 378 } else { 379 if (ch == dchar) 380 return 1; 381 } 382 return 0; 383 } 384 385 static int 386 f_cut(FILE *fp, const char *fname) 387 { 388 wchar_t ch; 389 int field, i, isdelim; 390 char *pos, *p; 391 int output; 392 char *lbuf, *mlbuf; 393 size_t clen, lbuflen, reallen; 394 395 mlbuf = NULL; 396 while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { 397 reallen = lbuflen; 398 /* Assert EOL has a newline. */ 399 if (*(lbuf + lbuflen - 1) != '\n') { 400 /* Can't have > 1 line with no trailing newline. */ 401 mlbuf = malloc(lbuflen + 1); 402 if (mlbuf == NULL) 403 err(1, "malloc"); 404 memcpy(mlbuf, lbuf, lbuflen); 405 *(mlbuf + lbuflen) = '\n'; 406 lbuf = mlbuf; 407 reallen++; 408 } 409 output = 0; 410 for (isdelim = 0, p = lbuf;; p += clen) { 411 clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL); 412 if (clen == (size_t)-1 || clen == (size_t)-2) { 413 warnc(EILSEQ, "%s", fname); 414 free(mlbuf); 415 return (1); 416 } 417 if (clen == 0) 418 clen = 1; 419 /* this should work if newline is delimiter */ 420 if (is_delim(ch)) 421 isdelim = 1; 422 if (ch == '\n') { 423 if (!isdelim && !sflag) 424 (void)fwrite(lbuf, lbuflen, 1, stdout); 425 break; 426 } 427 } 428 if (!isdelim) 429 continue; 430 431 pos = positions + 1; 432 for (field = maxval, p = lbuf; field; --field, ++pos) { 433 if (*pos && output++) 434 for (i = 0; dcharmb[i] != '\0'; i++) 435 putchar(dcharmb[i]); 436 for (;;) { 437 clen = mbrtowc(&ch, p, lbuf + reallen - p, 438 NULL); 439 if (clen == (size_t)-1 || clen == (size_t)-2) { 440 warnc(EILSEQ, "%s", fname); 441 free(mlbuf); 442 return (1); 443 } 444 if (clen == 0) 445 clen = 1; 446 p += clen; 447 if (ch == '\n' || is_delim(ch)) { 448 /* compress whitespace */ 449 if (wflag && ch != '\n') 450 while (is_delim(*p)) 451 p++; 452 break; 453 } 454 if (*pos) 455 for (i = 0; i < (int)clen; i++) 456 putchar(p[i - clen]); 457 } 458 if (ch == '\n') 459 break; 460 } 461 if (ch != '\n') { 462 if (autostop) { 463 if (output) 464 for (i = 0; dcharmb[i] != '\0'; i++) 465 putchar(dcharmb[i]); 466 for (; (ch = *p) != '\n'; ++p) 467 (void)putchar(ch); 468 } else 469 for (; (ch = *p) != '\n'; ++p); 470 } 471 (void)putchar('\n'); 472 } 473 free(mlbuf); 474 return (0); 475 } 476 477 static void 478 usage(void) 479 { 480 (void)fprintf(stderr, "%s\n%s\n%s\n", 481 "usage: cut -b list [-n] [file ...]", 482 " cut -c list [file ...]", 483 " cut -f list [-s] [-w | -d delim] [file ...]"); 484 exit(1); 485 } 486