Home | History | Annotate | Line # | Download | only in cut
cut.c revision 1.3
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 char copyright[] =
     39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     40  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /*static char sccsid[] = "from: @(#)cut.c	5.4 (Berkeley) 10/30/90";*/
     45 static char rcsid[] = "$Id: cut.c,v 1.3 1993/08/01 18:16:57 mycroft Exp $";
     46 #endif /* not lint */
     47 
     48 #include <limits.h>
     49 #include <stdio.h>
     50 #include <ctype.h>
     51 
     52 int	cflag;
     53 char	dchar;
     54 int	dflag;
     55 int	fflag;
     56 int	sflag;
     57 
     58 main(argc, argv)
     59 	int argc;
     60 	char **argv;
     61 {
     62 	extern char *optarg;
     63 	extern int errno, optind;
     64 	FILE *fp;
     65 	int ch, (*fcn)(), c_cut(), f_cut();
     66 	char *strerror();
     67 
     68 	dchar = '\t';			/* default delimiter is \t */
     69 
     70 	while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF)
     71 		switch(ch) {
     72 		case 'c':
     73 			fcn = c_cut;
     74 			get_list(optarg);
     75 			cflag = 1;
     76 			break;
     77 		case 'd':
     78 			dchar = *optarg;
     79 			dflag = 1;
     80 			break;
     81 		case 'f':
     82 			get_list(optarg);
     83 			fcn = f_cut;
     84 			fflag = 1;
     85 			break;
     86 		case 's':
     87 			sflag = 1;
     88 			break;
     89 		case '?':
     90 		default:
     91 			usage();
     92 		}
     93 	argc -= optind;
     94 	argv += optind;
     95 
     96 	if (fflag) {
     97 		if (cflag)
     98 			usage();
     99 	} else if (!cflag || dflag || sflag)
    100 		usage();
    101 
    102 	if (*argv)
    103 		for (; *argv; ++argv) {
    104 			if (!(fp = fopen(*argv, "r"))) {
    105 				(void)fprintf(stderr,
    106 				    "cut: %s: %s\n", *argv, strerror(errno));
    107 				exit(1);
    108 			}
    109 			fcn(fp, *argv);
    110 		}
    111 	else
    112 		fcn(stdin, "stdin");
    113 	exit(0);
    114 }
    115 
    116 int autostart, autostop, maxval;
    117 
    118 char positions[_POSIX2_LINE_MAX + 1];
    119 
    120 get_list(list)
    121 	char *list;
    122 {
    123 	register char *pos;
    124 	register int setautostart, start, stop;
    125 	char *p, *strtok();
    126 
    127 	/*
    128 	 * set a byte in the positions array to indicate if a field or
    129 	 * column is to be selected; use +1, it's 1-based, not 0-based.
    130 	 * This parser is less restrictive than the Draft 9 POSIX spec.
    131 	 * POSIX doesn't allow lists that aren't in increasing order or
    132 	 * overlapping lists.  We also handle "-3-5" although there's no
    133 	 * real reason too.
    134 	 */
    135 	for (; p = strtok(list, ", \t"); list = NULL) {
    136 		setautostart = start = stop = 0;
    137 		if (*p == '-') {
    138 			++p;
    139 			setautostart = 1;
    140 		}
    141 		if (isdigit(*p)) {
    142 			start = stop = strtol(p, &p, 10);
    143 			if (setautostart && start > autostart)
    144 				autostart = start;
    145 		}
    146 		if (*p == '-') {
    147 			if (isdigit(p[1]))
    148 				stop = strtol(p + 1, &p, 10);
    149 			if (*p == '-') {
    150 				++p;
    151 				if (!autostop || autostop > stop)
    152 					autostop = stop;
    153 			}
    154 		}
    155 		if (*p)
    156 			badlist("illegal list value");
    157 		if (!stop || !start)
    158 			badlist("values may not include zero");
    159 		if (stop > _POSIX2_LINE_MAX) {
    160 			/* positions used rather than allocate a new buffer */
    161 			(void)sprintf(positions, "%d too large (max %d)",
    162 			    stop, _POSIX2_LINE_MAX);
    163 			badlist(positions);
    164 		}
    165 		if (maxval < stop)
    166 			maxval = stop;
    167 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
    168 	}
    169 
    170 	/* overlapping ranges */
    171 	if (autostop && maxval > autostop)
    172 		maxval = autostop;
    173 
    174 	/* set autostart */
    175 	if (autostart)
    176 		memset(positions + 1, '1', autostart);
    177 }
    178 
    179 /* ARGSUSED */
    180 c_cut(fp, fname)
    181 	FILE *fp;
    182 	char *fname;
    183 {
    184 	register int ch, col;
    185 	register char *pos;
    186 
    187 	for (;;) {
    188 		pos = positions + 1;
    189 		for (col = maxval; col; --col) {
    190 			if ((ch = getc(fp)) == EOF)
    191 				return;
    192 			if (ch == '\n')
    193 				break;
    194 			if (*pos++)
    195 				putchar(ch);
    196 		}
    197 		if (ch != '\n')
    198 			if (autostop)
    199 				while ((ch = getc(fp)) != EOF && ch != '\n')
    200 					putchar(ch);
    201 			else
    202 				while ((ch = getc(fp)) != EOF && ch != '\n');
    203 		putchar('\n');
    204 	}
    205 }
    206 
    207 f_cut(fp, fname)
    208 	FILE *fp;
    209 	char *fname;
    210 {
    211 	register int ch, field, isdelim;
    212 	register char *pos, *p, sep;
    213 	int output;
    214 	char lbuf[_POSIX2_LINE_MAX + 1];
    215 
    216 	for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp); output = 0) {
    217 		for (isdelim = 0, p = lbuf;; ++p) {
    218 			if (!(ch = *p)) {
    219 				(void)fprintf(stderr,
    220 				    "cut: %s: line too long.\n", fname);
    221 				exit(1);
    222 			}
    223 			/* this should work if newline is delimiter */
    224 			if (ch == sep)
    225 				isdelim = 1;
    226 			if (ch == '\n') {
    227 				if (!isdelim && !sflag)
    228 					(void)printf("%s", lbuf);
    229 				break;
    230 			}
    231 		}
    232 		if (!isdelim)
    233 			continue;
    234 
    235 		pos = positions + 1;
    236 		for (field = maxval, p = lbuf; field; --field, ++pos) {
    237 			if (*pos) {
    238 				if (output++)
    239 					putchar(sep);
    240 				while ((ch = *p++) != '\n' && ch != sep)
    241 					putchar(ch);
    242 			} else
    243 				while ((ch = *p++) != '\n' && ch != sep);
    244 			if (ch == '\n')
    245 				break;
    246 		}
    247 		if (ch != '\n')
    248 			if (autostop) {
    249 				if (output)
    250 					putchar(sep);
    251 				for (; (ch = *p) != '\n'; ++p)
    252 					putchar(ch);
    253 			} else
    254 				for (; (ch = *p) != '\n'; ++p);
    255 		putchar('\n');
    256 	}
    257 }
    258 
    259 badlist(msg)
    260 	char *msg;
    261 {
    262 	(void)fprintf(stderr, "cut: [-cf] list: %s.\n", msg);
    263 	exit(1);
    264 }
    265 
    266 usage()
    267 {
    268 	(void)fprintf(stderr,
    269 "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
    270 	exit(1);
    271 }
    272