meillo@14: /* $NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz 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: #include meillo@14: #ifndef lint meillo@14: __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ meillo@14: The Regents of the University of California. All rights reserved."); 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: __RCSID("$NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz 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: #include meillo@14: #include meillo@14: #include meillo@14: meillo@14: static int bflag; meillo@14: static int cflag; meillo@14: static char dchar; meillo@14: static int dflag; meillo@14: static int fflag; meillo@14: static int sflag; meillo@14: meillo@14: static void b_cut(FILE *, const char *); meillo@14: static void c_cut(FILE *, const char *); meillo@14: static void f_cut(FILE *, const char *); meillo@14: static void get_list(char *); meillo@14: static void usage(void) __dead; meillo@14: meillo@14: int meillo@14: main(int argc, char *argv[]) meillo@14: { meillo@14: FILE *fp; meillo@14: void (*fcn)(FILE *, const char *); meillo@14: int ch, rval; meillo@14: meillo@14: fcn = NULL; meillo@14: (void)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: fcn = b_cut; meillo@14: get_list(optarg); meillo@14: bflag = 1; meillo@14: break; 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 || bflag) meillo@14: usage(); meillo@14: } else if ((!cflag && !bflag) || dflag || sflag) meillo@14: usage(); meillo@14: else if (bflag && cflag) meillo@14: usage(); meillo@14: meillo@14: rval = 0; meillo@14: if (*argv) meillo@14: for (; *argv; ++argv) { meillo@14: if (strcmp(*argv, "-") == 0) meillo@14: fcn(stdin, "stdin"); meillo@14: else { meillo@14: if ((fp = fopen(*argv, "r"))) { meillo@14: fcn(fp, *argv); meillo@14: (void)fclose(fp); meillo@14: } else { meillo@14: rval = 1; meillo@14: warn("%s", *argv); meillo@14: } meillo@14: } meillo@14: } meillo@14: else meillo@14: fcn(stdin, "stdin"); meillo@14: return(rval); meillo@14: } meillo@14: meillo@14: static size_t autostart, autostop, maxval; meillo@14: meillo@14: static char *positions = NULL; meillo@14: static size_t numpositions = 0; meillo@14: #define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */ 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: if (positions == NULL) { meillo@14: numpositions = ALLOC_CHUNK; meillo@14: positions = ecalloc(numpositions, sizeof(*positions)); meillo@14: } 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 to. meillo@14: */ meillo@14: for (; (p = strtok(list, ", \t")) != NULL; 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((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 (stop + 1 > numpositions) { meillo@14: size_t newsize; meillo@14: newsize = roundup(stop + 1, ALLOC_CHUNK); meillo@14: positions = erealloc(positions, newsize); meillo@14: (void)memset(positions + numpositions, 0, meillo@14: newsize - numpositions); meillo@14: numpositions = newsize; meillo@14: } meillo@14: if (maxval < stop) meillo@14: maxval = stop; meillo@14: for (pos = positions + start; start++ <= stop; pos++) meillo@14: *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: (void)memset(positions + 1, '1', autostart); meillo@14: } meillo@14: meillo@14: static void meillo@14: /*ARGSUSED*/ meillo@14: f_cut(FILE *fp, const char *fname __unused) 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)) != NULL;) { 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: (void)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: continue; 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: (void)putchar('\n'); meillo@14: if (tbuf) { meillo@14: free(tbuf); meillo@14: tbuf = NULL; meillo@14: } meillo@14: } meillo@14: if (tbuf) meillo@14: free(tbuf); meillo@14: } meillo@14: meillo@14: static void meillo@14: usage(void) meillo@14: { meillo@14: (void)fprintf(stderr, "usage:\tcut -b list [-n] [file ...]\n" meillo@14: "\tcut -c list [file ...]\n" meillo@14: "\tcut -f list [-d string] [-s] [file ...]\n"); meillo@14: exit(1); meillo@14: } meillo@14: meillo@14: /* make b_put(): */ meillo@14: #define CUT_BYTE 1 meillo@14: #include "x_cut.c" meillo@14: #undef CUT_BYTE meillo@14: meillo@14: /* make c_put(): */ meillo@14: #define CUT_BYTE 0 meillo@14: #include "x_cut.c" meillo@14: #undef CUT_BYTE