meillo@14: /* $OpenBSD: cut.c,v 1.13 2008/06/27 08:02:13 sobrado Exp $ */ meillo@14: /* $NetBSD: cut.c,v 1.9 1995/09/02 05:59:23 jtc Exp $ */ meillo@14: 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: * 3. 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 char copyright[] = meillo@14: "@(#) Copyright (c) 1989, 1993\n\ meillo@14: The Regents of the University of California. All rights reserved.\n"; meillo@14: #endif /* not lint */ meillo@14: meillo@14: #ifndef lint meillo@14: #if 0 meillo@14: static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95"; meillo@14: #endif meillo@14: static char rcsid[] = "$OpenBSD: cut.c,v 1.13 2008/06/27 08:02:13 sobrado Exp $"; meillo@14: #endif /* not lint */ 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: meillo@14: int cflag; meillo@14: char dchar; meillo@14: int dflag; meillo@14: int fflag; meillo@14: int sflag; meillo@14: meillo@14: void c_cut(FILE *, char *); meillo@14: void f_cut(FILE *, char *); meillo@14: void get_list(char *); meillo@14: void usage(void); meillo@14: meillo@14: int meillo@14: main(int argc, char *argv[]) meillo@14: { meillo@14: FILE *fp; meillo@14: void (*fcn)(FILE *, char *); meillo@14: int ch; meillo@14: meillo@14: setlocale (LC_ALL, ""); meillo@14: meillo@14: dchar = '\t'; /* default delimiter is \t */ meillo@14: meillo@14: /* Since we don't support multi-byte characters, the -c and -b meillo@14: options are equivalent, and the -n option is meaningless. */ meillo@14: while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) meillo@14: switch(ch) { meillo@14: case 'b': meillo@14: case 'c': meillo@14: fcn = c_cut; meillo@14: get_list(optarg); meillo@14: cflag = 1; meillo@14: break; meillo@14: case 'd': meillo@14: dchar = *optarg; meillo@14: dflag = 1; meillo@14: break; meillo@14: case 'f': meillo@14: get_list(optarg); meillo@14: fcn = f_cut; meillo@14: fflag = 1; meillo@14: break; meillo@14: case 's': meillo@14: sflag = 1; meillo@14: break; meillo@14: case 'n': 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 (cflag) meillo@14: usage(); meillo@14: } else if (!cflag || dflag || sflag) meillo@14: usage(); meillo@14: meillo@14: if (*argv) meillo@14: for (; *argv; ++argv) { meillo@14: if (!(fp = fopen(*argv, "r"))) meillo@14: err(1, "%s", *argv); meillo@14: fcn(fp, *argv); meillo@14: (void)fclose(fp); meillo@14: } meillo@14: else meillo@14: fcn(stdin, "stdin"); meillo@14: exit(0); meillo@14: } meillo@14: meillo@14: int autostart, autostop, maxval; meillo@14: meillo@14: char positions[_POSIX2_LINE_MAX + 1]; meillo@14: meillo@14: void meillo@14: get_list(char *list) meillo@14: { meillo@14: int 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: * This parser is less restrictive than the Draft 9 POSIX spec. meillo@14: * POSIX doesn't allow lists that aren't in increasing order or meillo@14: * overlapping lists. We also handle "-3-5" although there's no meillo@14: * real reason too. meillo@14: */ meillo@14: while ((p = strsep(&list, ", \t"))) { meillo@14: setautostart = start = stop = 0; meillo@14: if (*p == '-') { meillo@14: ++p; meillo@14: setautostart = 1; meillo@14: } meillo@14: if (isdigit(*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(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, "[-cf] list: illegal list value"); meillo@14: if (!stop || !start) meillo@14: errx(1, "[-cf] list: values may not include zero"); meillo@14: if (stop > _POSIX2_LINE_MAX) meillo@14: errx(1, "[-cf] list: %d too large (max %d)", meillo@14: stop, _POSIX2_LINE_MAX); meillo@14: if (maxval < stop) meillo@14: maxval = stop; meillo@14: for (pos = positions + start; start++ <= stop; *pos++ = 1) meillo@14: ; meillo@14: } meillo@14: meillo@14: /* overlapping ranges */ meillo@14: if (autostop && maxval > autostop) meillo@14: maxval = autostop; meillo@14: meillo@14: /* set autostart */ meillo@14: if (autostart) meillo@14: memset(positions + 1, '1', autostart); meillo@14: } meillo@14: meillo@14: /* ARGSUSED */ meillo@14: void meillo@14: c_cut(FILE *fp, char *fname) meillo@14: { meillo@14: int ch, col; meillo@14: char *pos; meillo@14: 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; 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: } meillo@14: (void)putchar('\n'); meillo@14: } meillo@14: } meillo@14: meillo@14: void meillo@14: f_cut(FILE *fp, char *fname) meillo@14: { meillo@14: int ch, field, isdelim; meillo@14: char *pos, *p, sep; meillo@14: int output; meillo@14: size_t len; meillo@14: char *lbuf, *tbuf; meillo@14: meillo@14: for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len));) { meillo@14: output = 0; meillo@14: if (lbuf[len - 1] != '\n') { meillo@14: /* no newline at the end of the last line so add one */ meillo@14: if ((tbuf = (char *)malloc(len + 1)) == NULL) meillo@14: err(1, NULL); meillo@14: memcpy(tbuf, lbuf, len); meillo@14: tbuf[len] = '\n'; meillo@14: lbuf = tbuf; meillo@14: } meillo@14: for (isdelim = 0, p = lbuf;; ++p) { meillo@14: ch = *p; meillo@14: /* this should work if newline is delimiter */ meillo@14: if (ch == sep) meillo@14: isdelim = 1; meillo@14: if (ch == '\n') { meillo@14: if (!isdelim && !sflag) meillo@14: (void)fwrite(lbuf, len, 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) { meillo@14: if (output++) meillo@14: (void)putchar(sep); meillo@14: while ((ch = *p++) != '\n' && ch != sep) meillo@14: (void)putchar(ch); meillo@14: } else meillo@14: while ((ch = *p++) != '\n' && ch != sep) 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: (void)putchar(sep); 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: } meillo@14: (void)putchar('\n'); meillo@14: } meillo@14: if (tbuf) meillo@14: free(tbuf); meillo@14: } meillo@14: meillo@14: void meillo@14: usage(void) meillo@14: { meillo@14: (void)fprintf(stderr, meillo@14: "usage: cut -b list [-n] [file ...]\n" meillo@14: " cut -c list [file ...]\n" meillo@14: " cut -f list [-s] [-d delim] [file ...]\n"); meillo@14: exit(1); meillo@14: }