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. All advertising materials mentioning features or use of this software meillo@14: * must display the following acknowledgement: meillo@14: * This product includes software developed by the University of meillo@14: * California, Berkeley and its contributors. 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 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: static char sccsid[] = "@(#)cut.c 8.1 (Berkeley) 6/6/93"; 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: 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 __P((FILE *, char *)); meillo@14: void err __P((const char *, ...)); meillo@14: void f_cut __P((FILE *, char *)); meillo@14: void get_list __P((char *)); meillo@14: void usage __P((void)); meillo@14: meillo@14: int meillo@14: main(argc, argv) meillo@14: int argc; meillo@14: char *argv[]; meillo@14: { meillo@14: FILE *fp; meillo@14: void (*fcn) __P((FILE *, char *)); meillo@14: int ch; 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: err("%s: %s\n", *argv, strerror(errno)); 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(list) meillo@14: char *list; meillo@14: { meillo@14: register int setautostart, start, stop; meillo@14: register 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: 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: err("[-cf] list: illegal list value\n"); meillo@14: if (!stop || !start) meillo@14: err("[-cf] list: values may not include zero\n"); meillo@14: if (stop > _POSIX2_LINE_MAX) meillo@14: err("[-cf] list: %d too large (max %d)\n", 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: /* 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(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: (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: (void)putchar('\n'); meillo@14: } meillo@14: } meillo@14: meillo@14: void 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[_POSIX2_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: err("%s: line too long.\n", fname); 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: (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: 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: (void)putchar('\n'); meillo@14: } meillo@14: } meillo@14: meillo@14: void 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: } meillo@14: meillo@14: #if __STDC__ meillo@14: #include meillo@14: #else meillo@14: #include meillo@14: #endif meillo@14: meillo@14: void meillo@14: #if __STDC__ meillo@14: err(const char *fmt, ...) meillo@14: #else meillo@14: err(fmt, va_alist) meillo@14: char *fmt; meillo@14: va_dcl meillo@14: #endif meillo@14: { meillo@14: va_list ap; meillo@14: #if __STDC__ meillo@14: va_start(ap, fmt); meillo@14: #else meillo@14: va_start(ap); meillo@14: #endif meillo@14: (void)fprintf(stderr, "cut: "); meillo@14: (void)vfprintf(stderr, fmt, ap); meillo@14: va_end(ap); meillo@14: (void)fprintf(stderr, "\n"); meillo@14: exit(1); meillo@14: /* NOTREACHED */ meillo@14: }