meillo@14: /* meillo@14: * Copyright (c) 1989 The Regents of the University of California. meillo@14: * 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 are permitted provided meillo@14: * that: (1) source distributions retain this entire copyright notice and meillo@14: * comment, and (2) distributions including binaries display the following meillo@14: * acknowledgement: ``This product includes software developed by the meillo@14: * University of California, Berkeley and its contributors'' in the meillo@14: * documentation or other materials provided with the distribution and in meillo@14: * all advertising materials mentioning features or use of this software. meillo@14: * Neither the name of the University nor the names of its contributors may meillo@14: * be used to endorse or promote products derived from this software without meillo@14: * specific prior written permission. meillo@14: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED meillo@14: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF meillo@14: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. meillo@14: */ meillo@14: meillo@14: #ifndef lint meillo@14: char copyright[] = meillo@14: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ meillo@14: All rights reserved.\n"; meillo@14: #endif /* not lint */ meillo@14: meillo@14: #ifndef lint meillo@14: static char sccsid[] = "@(#)cut.c 5.3 (Berkeley) 6/24/90"; meillo@14: #endif /* not lint */ meillo@14: 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: main(argc, argv) meillo@14: int argc; meillo@14: char **argv; meillo@14: { meillo@14: extern char *optarg; meillo@14: extern int errno, optind; meillo@14: FILE *fp; meillo@14: int ch, (*fcn)(), c_cut(), f_cut(); meillo@14: char *strerror(); meillo@14: meillo@14: dchar = '\t'; /* default delimiter is \t */ meillo@14: meillo@14: while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF) meillo@14: switch(ch) { 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 '?': 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: (void)fprintf(stderr, meillo@14: "cut: %s: %s\n", *argv, strerror(errno)); meillo@14: exit(1); meillo@14: } meillo@14: fcn(fp, *argv); 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[_BSD_LINE_MAX + 1]; meillo@14: meillo@14: get_list(list) meillo@14: char *list; meillo@14: { meillo@14: register char *pos; meillo@14: register int setautostart, start, stop; meillo@14: char *p, *strtok(); 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: for (; p = strtok(list, ", \t"); list = NULL) { 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: badlist("illegal list value"); meillo@14: if (!stop || !start) meillo@14: badlist("values may not include zero"); meillo@14: if (stop > _BSD_LINE_MAX) { meillo@14: /* positions used rather than allocate a new buffer */ meillo@14: (void)sprintf(positions, "%d too large (max %d)", meillo@14: stop, _BSD_LINE_MAX); meillo@14: badlist(positions); meillo@14: } 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: /* 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: c_cut(fp, fname) meillo@14: FILE *fp; meillo@14: char *fname; meillo@14: { meillo@14: register int ch, col; meillo@14: register 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: putchar(ch); meillo@14: } meillo@14: if (ch != '\n') meillo@14: if (autostop) meillo@14: while ((ch = getc(fp)) != EOF && ch != '\n') meillo@14: putchar(ch); meillo@14: else meillo@14: while ((ch = getc(fp)) != EOF && ch != '\n'); meillo@14: putchar('\n'); meillo@14: } meillo@14: } meillo@14: meillo@14: f_cut(fp, fname) meillo@14: FILE *fp; meillo@14: char *fname; meillo@14: { meillo@14: register int ch, field, isdelim; meillo@14: register char *pos, *p, sep; meillo@14: int output; meillo@14: char lbuf[_BSD_LINE_MAX + 1]; meillo@14: meillo@14: for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp);) { meillo@14: for (isdelim = 0, p = lbuf;; ++p) { meillo@14: if (!(ch = *p)) { meillo@14: (void)fprintf(stderr, meillo@14: "cut: %s: line too long.\n", fname); meillo@14: exit(1); meillo@14: } 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)printf("%s", lbuf); 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: putchar(sep); meillo@14: while ((ch = *p++) != '\n' && ch != sep) meillo@14: putchar(ch); meillo@14: } else meillo@14: while ((ch = *p++) != '\n' && ch != sep); 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: putchar(sep); meillo@14: for (; (ch = *p) != '\n'; ++p) meillo@14: putchar(ch); meillo@14: } else meillo@14: for (; (ch = *p) != '\n'; ++p); meillo@14: putchar('\n'); meillo@14: } meillo@14: } meillo@14: meillo@14: badlist(msg) meillo@14: char *msg; meillo@14: { meillo@14: (void)fprintf(stderr, "cut: [-cf] list: %s.\n", msg); meillo@14: exit(1); meillo@14: } meillo@14: meillo@14: usage() meillo@14: { meillo@14: (void)fprintf(stderr, meillo@14: "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n"); meillo@14: exit(1); meillo@14: }