meillo@14: /* meillo@14: * Copyright (c) 1989, 1993 meillo@14: * The Regents of the University of California. All rights reserved. meillo@14: * meillo@14: * This code is derived from software contributed to Berkeley by meillo@14: * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. meillo@14: * meillo@14: * Redistribution and use in source and binary forms, with or without meillo@14: * modification, are permitted provided that the following conditions meillo@14: * are met: meillo@14: * 1. Redistributions of source code must retain the above copyright meillo@14: * notice, this list of conditions and the following disclaimer. meillo@14: * 2. Redistributions in binary form must reproduce the above copyright meillo@14: * notice, this list of conditions and the following disclaimer in the meillo@14: * documentation and/or other materials provided with the distribution. meillo@14: * 4. Neither the name of the University nor the names of its contributors meillo@14: * may be used to endorse or promote products derived from this software meillo@14: * without specific prior written permission. meillo@14: * meillo@14: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND meillo@14: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE meillo@14: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE meillo@14: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE meillo@14: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL meillo@14: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS meillo@14: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) meillo@14: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT meillo@14: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY meillo@14: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF meillo@14: * SUCH DAMAGE. meillo@14: */ meillo@14: meillo@14: #ifndef lint meillo@14: static const char copyright[] = meillo@14: "@(#) Copyright (c) 1989, 1993\n\ meillo@14: The Regents of the University of California. All rights reserved.\n"; meillo@14: static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; meillo@14: #endif /* not lint */ meillo@14: #include meillo@14: __FBSDID("$FreeBSD$"); meillo@14: meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: #include meillo@14: meillo@14: static int bflag; meillo@14: static int cflag; meillo@14: static wchar_t dchar; meillo@14: static char dcharmb[MB_LEN_MAX + 1]; meillo@14: static int dflag; meillo@14: static int fflag; meillo@14: static int nflag; meillo@14: static int sflag; meillo@14: static int wflag; meillo@14: meillo@14: static size_t autostart, autostop, maxval; meillo@14: static char * positions; meillo@14: meillo@14: static int b_cut(FILE *, const char *); meillo@14: static int b_n_cut(FILE *, const char *); meillo@14: static int c_cut(FILE *, const char *); meillo@14: static int f_cut(FILE *, const char *); meillo@14: static void get_list(char *); meillo@14: static int is_delim(wchar_t); meillo@14: static void needpos(size_t); meillo@14: static void usage(void); meillo@14: meillo@14: int meillo@14: main(int argc, char *argv[]) meillo@14: { meillo@14: FILE *fp; meillo@14: int (*fcn)(FILE *, const char *); meillo@14: int ch, rval; meillo@14: size_t n; meillo@14: meillo@14: setlocale(LC_ALL, ""); meillo@14: meillo@14: fcn = NULL; meillo@14: dchar = '\t'; /* default delimiter is \t */ meillo@14: strcpy(dcharmb, "\t"); meillo@14: meillo@14: while ((ch = getopt(argc, argv, "b:c:d:f:snw")) != -1) meillo@14: switch(ch) { meillo@14: case 'b': meillo@14: get_list(optarg); meillo@14: bflag = 1; meillo@14: break; meillo@14: case 'c': meillo@14: get_list(optarg); meillo@14: cflag = 1; meillo@14: break; meillo@14: case 'd': meillo@14: n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL); meillo@14: if (dchar == '\0' || n != strlen(optarg)) meillo@14: errx(1, "bad delimiter"); meillo@14: strcpy(dcharmb, optarg); meillo@14: dflag = 1; meillo@14: break; meillo@14: case 'f': meillo@14: get_list(optarg); meillo@14: fflag = 1; meillo@14: break; meillo@14: case 's': meillo@14: sflag = 1; meillo@14: break; meillo@14: case 'n': meillo@14: nflag = 1; meillo@14: break; meillo@14: case 'w': meillo@14: wflag = 1; meillo@14: break; meillo@14: case '?': meillo@14: default: meillo@14: usage(); meillo@14: } meillo@14: argc -= optind; meillo@14: argv += optind; meillo@14: meillo@14: if (fflag) { meillo@14: if (bflag || cflag || nflag || (wflag && dflag)) meillo@14: usage(); meillo@14: } else if (!(bflag || cflag) || dflag || sflag || wflag) meillo@14: usage(); meillo@14: else if (!bflag && nflag) meillo@14: usage(); meillo@14: meillo@14: if (fflag) meillo@14: fcn = f_cut; meillo@14: else if (cflag) meillo@14: fcn = MB_CUR_MAX > 1 ? c_cut : b_cut; meillo@14: else if (bflag) meillo@14: fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut; meillo@14: meillo@14: rval = 0; meillo@14: if (*argv) meillo@14: for (; *argv; ++argv) { meillo@14: if (strcmp(*argv, "-") == 0) meillo@14: rval |= fcn(stdin, "stdin"); meillo@14: else { meillo@14: if (!(fp = fopen(*argv, "r"))) { meillo@14: warn("%s", *argv); meillo@14: rval = 1; meillo@14: continue; meillo@14: } meillo@14: fcn(fp, *argv); meillo@14: (void)fclose(fp); meillo@14: } meillo@14: } meillo@14: else meillo@14: rval = fcn(stdin, "stdin"); meillo@14: exit(rval); meillo@14: } meillo@14: meillo@14: static void meillo@14: get_list(char *list) meillo@14: { meillo@14: size_t setautostart, start, stop; meillo@14: char *pos; meillo@14: char *p; meillo@14: meillo@14: /* meillo@14: * set a byte in the positions array to indicate if a field or meillo@14: * column is to be selected; use +1, it's 1-based, not 0-based. meillo@14: * Numbers and number ranges may be overlapping, repeated, and in meillo@14: * any order. We handle "-3-5" although there's no real reason to. meillo@14: */ meillo@14: for (; (p = strsep(&list, ", \t")) != NULL;) { meillo@14: setautostart = start = stop = 0; meillo@14: if (*p == '-') { meillo@14: ++p; meillo@14: setautostart = 1; meillo@14: } meillo@14: if (isdigit((unsigned char)*p)) { meillo@14: start = stop = strtol(p, &p, 10); meillo@14: if (setautostart && start > autostart) meillo@14: autostart = start; meillo@14: } meillo@14: if (*p == '-') { meillo@14: if (isdigit((unsigned char)p[1])) meillo@14: stop = strtol(p + 1, &p, 10); meillo@14: if (*p == '-') { meillo@14: ++p; meillo@14: if (!autostop || autostop > stop) meillo@14: autostop = stop; meillo@14: } meillo@14: } meillo@14: if (*p) meillo@14: errx(1, "[-bcf] list: illegal list value"); meillo@14: if (!stop || !start) meillo@14: errx(1, "[-bcf] list: values may not include zero"); meillo@14: if (maxval < stop) { meillo@14: maxval = stop; meillo@14: needpos(maxval + 1); meillo@14: } meillo@14: for (pos = positions + start; start++ <= stop; *pos++ = 1); meillo@14: } meillo@14: meillo@14: /* overlapping ranges */ meillo@14: if (autostop && maxval > autostop) { meillo@14: maxval = autostop; meillo@14: needpos(maxval + 1); meillo@14: } meillo@14: meillo@14: /* set autostart */ meillo@14: if (autostart) meillo@14: memset(positions + 1, '1', autostart); meillo@14: } meillo@14: meillo@14: static void meillo@14: needpos(size_t n) meillo@14: { meillo@14: static size_t npos; meillo@14: size_t oldnpos; meillo@14: meillo@14: /* Grow the positions array to at least the specified size. */ meillo@14: if (n > npos) { meillo@14: oldnpos = npos; meillo@14: if (npos == 0) meillo@14: npos = n; meillo@14: while (n > npos) meillo@14: npos *= 2; meillo@14: if ((positions = realloc(positions, npos)) == NULL) meillo@14: err(1, "realloc"); meillo@14: memset((char *)positions + oldnpos, 0, npos - oldnpos); meillo@14: } meillo@14: } meillo@14: meillo@14: static int meillo@14: b_cut(FILE *fp, const char *fname __unused) meillo@14: { meillo@14: int ch, col; meillo@14: char *pos; meillo@14: meillo@14: ch = 0; meillo@14: for (;;) { meillo@14: pos = positions + 1; meillo@14: for (col = maxval; col; --col) { meillo@14: if ((ch = getc(fp)) == EOF) meillo@14: return (0); meillo@14: if (ch == '\n') meillo@14: break; meillo@14: if (*pos++) meillo@14: (void)putchar(ch); meillo@14: } meillo@14: if (ch != '\n') { meillo@14: if (autostop) meillo@14: while ((ch = getc(fp)) != EOF && ch != '\n') meillo@14: (void)putchar(ch); meillo@14: else meillo@14: while ((ch = getc(fp)) != EOF && ch != '\n'); meillo@14: } meillo@14: (void)putchar('\n'); meillo@14: } meillo@14: return (0); meillo@14: } meillo@14: meillo@14: /* meillo@14: * Cut based on byte positions, taking care not to split multibyte characters. meillo@14: * Although this function also handles the case where -n is not specified, meillo@14: * b_cut() ought to be much faster. meillo@14: */ meillo@14: static int meillo@14: b_n_cut(FILE *fp, const char *fname) meillo@14: { meillo@14: size_t col, i, lbuflen; meillo@14: char *lbuf; meillo@14: int canwrite, clen, warned; meillo@14: mbstate_t mbs; meillo@14: meillo@14: memset(&mbs, 0, sizeof(mbs)); meillo@14: warned = 0; meillo@14: while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { meillo@14: for (col = 0; lbuflen > 0; col += clen) { meillo@14: if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) { meillo@14: if (!warned) { meillo@14: warn("%s", fname); meillo@14: warned = 1; meillo@14: } meillo@14: memset(&mbs, 0, sizeof(mbs)); meillo@14: clen = 1; meillo@14: } meillo@14: if (clen == 0 || *lbuf == '\n') meillo@14: break; meillo@14: if (col < maxval && !positions[1 + col]) { meillo@14: /* meillo@14: * Print the character if (1) after an initial meillo@14: * segment of un-selected bytes, the rest of meillo@14: * it is selected, and (2) the last byte is meillo@14: * selected. meillo@14: */ meillo@14: i = col; meillo@14: while (i < col + clen && i < maxval && meillo@14: !positions[1 + i]) meillo@14: i++; meillo@14: canwrite = i < col + clen; meillo@14: for (; i < col + clen && i < maxval; i++) meillo@14: canwrite &= positions[1 + i]; meillo@14: if (canwrite) meillo@14: fwrite(lbuf, 1, clen, stdout); meillo@14: } else { meillo@14: /* meillo@14: * Print the character if all of it has meillo@14: * been selected. meillo@14: */ meillo@14: canwrite = 1; meillo@14: for (i = col; i < col + clen; i++) meillo@14: if ((i >= maxval && !autostop) || meillo@14: (i < maxval && !positions[1 + i])) { meillo@14: canwrite = 0; meillo@14: break; meillo@14: } meillo@14: if (canwrite) meillo@14: fwrite(lbuf, 1, clen, stdout); meillo@14: } meillo@14: lbuf += clen; meillo@14: lbuflen -= clen; meillo@14: } meillo@14: if (lbuflen > 0) meillo@14: putchar('\n'); meillo@14: } meillo@14: return (warned); meillo@14: } meillo@14: meillo@14: static int meillo@14: c_cut(FILE *fp, const char *fname) meillo@14: { meillo@14: wint_t ch; meillo@14: int col; meillo@14: char *pos; meillo@14: meillo@14: ch = 0; meillo@14: for (;;) { meillo@14: pos = positions + 1; meillo@14: for (col = maxval; col; --col) { meillo@14: if ((ch = getwc(fp)) == WEOF) meillo@14: goto out; meillo@14: if (ch == '\n') meillo@14: break; meillo@14: if (*pos++) meillo@14: (void)putwchar(ch); meillo@14: } meillo@14: if (ch != '\n') { meillo@14: if (autostop) meillo@14: while ((ch = getwc(fp)) != WEOF && ch != '\n') meillo@14: (void)putwchar(ch); meillo@14: else meillo@14: while ((ch = getwc(fp)) != WEOF && ch != '\n'); meillo@14: } meillo@14: (void)putwchar('\n'); meillo@14: } meillo@14: out: meillo@14: if (ferror(fp)) { meillo@14: warn("%s", fname); meillo@14: return (1); meillo@14: } meillo@14: return (0); meillo@14: } meillo@14: meillo@14: static int meillo@14: is_delim(wchar_t ch) meillo@14: { meillo@14: if (wflag) { meillo@14: if (ch == ' ' || ch == '\t') meillo@14: return 1; meillo@14: } else { meillo@14: if (ch == dchar) meillo@14: return 1; meillo@14: } meillo@14: return 0; meillo@14: } meillo@14: meillo@14: static int meillo@14: f_cut(FILE *fp, const char *fname) meillo@14: { meillo@14: wchar_t ch; meillo@14: int field, i, isdelim; meillo@14: char *pos, *p; meillo@14: int output; meillo@14: char *lbuf, *mlbuf; meillo@14: size_t clen, lbuflen, reallen; meillo@14: meillo@14: mlbuf = NULL; meillo@14: while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { meillo@14: reallen = lbuflen; meillo@14: /* Assert EOL has a newline. */ meillo@14: if (*(lbuf + lbuflen - 1) != '\n') { meillo@14: /* Can't have > 1 line with no trailing newline. */ meillo@14: mlbuf = malloc(lbuflen + 1); meillo@14: if (mlbuf == NULL) meillo@14: err(1, "malloc"); meillo@14: memcpy(mlbuf, lbuf, lbuflen); meillo@14: *(mlbuf + lbuflen) = '\n'; meillo@14: lbuf = mlbuf; meillo@14: reallen++; meillo@14: } meillo@14: output = 0; meillo@14: for (isdelim = 0, p = lbuf;; p += clen) { meillo@14: clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL); meillo@14: if (clen == (size_t)-1 || clen == (size_t)-2) { meillo@14: warnc(EILSEQ, "%s", fname); meillo@14: free(mlbuf); meillo@14: return (1); meillo@14: } meillo@14: if (clen == 0) meillo@14: clen = 1; meillo@14: /* this should work if newline is delimiter */ meillo@14: if (is_delim(ch)) meillo@14: isdelim = 1; meillo@14: if (ch == '\n') { meillo@14: if (!isdelim && !sflag) meillo@14: (void)fwrite(lbuf, lbuflen, 1, stdout); meillo@14: break; meillo@14: } meillo@14: } meillo@14: if (!isdelim) meillo@14: continue; meillo@14: meillo@14: pos = positions + 1; meillo@14: for (field = maxval, p = lbuf; field; --field, ++pos) { meillo@14: if (*pos && output++) meillo@14: for (i = 0; dcharmb[i] != '\0'; i++) meillo@14: putchar(dcharmb[i]); meillo@14: for (;;) { meillo@14: clen = mbrtowc(&ch, p, lbuf + reallen - p, meillo@14: NULL); meillo@14: if (clen == (size_t)-1 || clen == (size_t)-2) { meillo@14: warnc(EILSEQ, "%s", fname); meillo@14: free(mlbuf); meillo@14: return (1); meillo@14: } meillo@14: if (clen == 0) meillo@14: clen = 1; meillo@14: p += clen; meillo@14: if (ch == '\n' || is_delim(ch)) { meillo@14: /* compress whitespace */ meillo@14: if (wflag && ch != '\n') meillo@14: while (is_delim(*p)) meillo@14: p++; meillo@14: break; meillo@14: } meillo@14: if (*pos) meillo@14: for (i = 0; i < (int)clen; i++) meillo@14: putchar(p[i - clen]); meillo@14: } meillo@14: if (ch == '\n') meillo@14: break; meillo@14: } meillo@14: if (ch != '\n') { meillo@14: if (autostop) { meillo@14: if (output) meillo@14: for (i = 0; dcharmb[i] != '\0'; i++) meillo@14: putchar(dcharmb[i]); meillo@14: for (; (ch = *p) != '\n'; ++p) meillo@14: (void)putchar(ch); meillo@14: } else meillo@14: for (; (ch = *p) != '\n'; ++p); meillo@14: } meillo@14: (void)putchar('\n'); meillo@14: } meillo@14: free(mlbuf); meillo@14: return (0); meillo@14: } meillo@14: meillo@14: static void meillo@14: usage(void) meillo@14: { meillo@14: (void)fprintf(stderr, "%s\n%s\n%s\n", meillo@14: "usage: cut -b list [-n] [file ...]", meillo@14: " cut -c list [file ...]", meillo@14: " cut -f list [-s] [-w | -d delim] [file ...]"); meillo@14: exit(1); meillo@14: }