xref: /freebsd/usr.bin/cut/cut.c (revision f457179a13d8bb203bcc381a2f1e8ce72a7f7d40)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
38fa146c53SArchie Cobbs static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
41fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
42d8a3fbd5SWill Andrews static const char rcsid[] =
43d8a3fbd5SWill Andrews   "$FreeBSD$";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <ctype.h>
47812bff99SPhilippe Charnier #include <err.h>
489b50d902SRodney W. Grimes #include <limits.h>
49d51c6625SEivind Eklund #include <locale.h>
509b50d902SRodney W. Grimes #include <stdio.h>
519b50d902SRodney W. Grimes #include <stdlib.h>
529b50d902SRodney W. Grimes #include <string.h>
53df3f5d9dSPeter Wemm #include <unistd.h>
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes int	cflag;
569b50d902SRodney W. Grimes char	dchar;
579b50d902SRodney W. Grimes int	dflag;
589b50d902SRodney W. Grimes int	fflag;
599b50d902SRodney W. Grimes int	sflag;
609b50d902SRodney W. Grimes 
6199557a79SWill Andrews void	c_cut (FILE *, const char *);
6299557a79SWill Andrews void	f_cut (FILE *, const char *);
6399557a79SWill Andrews void	get_list (char *);
6499557a79SWill Andrews int	main (int, char **);
65a8522a9bSTim J. Robbins void	needpos(size_t);
6699557a79SWill Andrews static 	void usage (void);
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes int
699b50d902SRodney W. Grimes main(argc, argv)
709b50d902SRodney W. Grimes 	int argc;
719b50d902SRodney W. Grimes 	char *argv[];
729b50d902SRodney W. Grimes {
739b50d902SRodney W. Grimes 	FILE *fp;
7499557a79SWill Andrews 	void (*fcn) (FILE *, const char *) = NULL;
75a6ea32c3STim J. Robbins 	int ch, rval;
769b50d902SRodney W. Grimes 
772c39ae65SEivind Eklund 	fcn = NULL;
78d51c6625SEivind Eklund 	setlocale (LC_ALL, "");
79d51c6625SEivind Eklund 
809b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
819b50d902SRodney W. Grimes 
82d51c6625SEivind Eklund 	/* Since we don't support multi-byte characters, the -c and -b
83d51c6625SEivind Eklund 	   options are equivalent, and the -n option is meaningless. */
845183fb53SEivind Eklund 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
859b50d902SRodney W. Grimes 		switch(ch) {
86d51c6625SEivind Eklund 		case 'b':
879b50d902SRodney W. Grimes 		case 'c':
889b50d902SRodney W. Grimes 			fcn = c_cut;
899b50d902SRodney W. Grimes 			get_list(optarg);
909b50d902SRodney W. Grimes 			cflag = 1;
919b50d902SRodney W. Grimes 			break;
929b50d902SRodney W. Grimes 		case 'd':
939b50d902SRodney W. Grimes 			dchar = *optarg;
949b50d902SRodney W. Grimes 			dflag = 1;
959b50d902SRodney W. Grimes 			break;
969b50d902SRodney W. Grimes 		case 'f':
979b50d902SRodney W. Grimes 			get_list(optarg);
989b50d902SRodney W. Grimes 			fcn = f_cut;
999b50d902SRodney W. Grimes 			fflag = 1;
1009b50d902SRodney W. Grimes 			break;
1019b50d902SRodney W. Grimes 		case 's':
1029b50d902SRodney W. Grimes 			sflag = 1;
1039b50d902SRodney W. Grimes 			break;
104d51c6625SEivind Eklund 		case 'n':
105d51c6625SEivind Eklund 			break;
1069b50d902SRodney W. Grimes 		case '?':
1079b50d902SRodney W. Grimes 		default:
1089b50d902SRodney W. Grimes 			usage();
1099b50d902SRodney W. Grimes 		}
1109b50d902SRodney W. Grimes 	argc -= optind;
1119b50d902SRodney W. Grimes 	argv += optind;
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes 	if (fflag) {
1149b50d902SRodney W. Grimes 		if (cflag)
1159b50d902SRodney W. Grimes 			usage();
1169b50d902SRodney W. Grimes 	} else if (!cflag || dflag || sflag)
1179b50d902SRodney W. Grimes 		usage();
1189b50d902SRodney W. Grimes 
119a6ea32c3STim J. Robbins 	rval = 0;
1209b50d902SRodney W. Grimes 	if (*argv)
1219b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
122204c78a1STim J. Robbins 			if (strcmp(*argv, "-") == 0)
123204c78a1STim J. Robbins 				fcn(stdin, "stdin");
124204c78a1STim J. Robbins 			else {
125a6ea32c3STim J. Robbins 				if (!(fp = fopen(*argv, "r"))) {
126a6ea32c3STim J. Robbins 					warn("%s", *argv);
127a6ea32c3STim J. Robbins 					rval = 1;
128a6ea32c3STim J. Robbins 					continue;
129a6ea32c3STim J. Robbins 				}
1309b50d902SRodney W. Grimes 				fcn(fp, *argv);
1319b50d902SRodney W. Grimes 				(void)fclose(fp);
1329b50d902SRodney W. Grimes 			}
133204c78a1STim J. Robbins 		}
1349b50d902SRodney W. Grimes 	else
1359b50d902SRodney W. Grimes 		fcn(stdin, "stdin");
136a6ea32c3STim J. Robbins 	exit(rval);
1379b50d902SRodney W. Grimes }
1389b50d902SRodney W. Grimes 
139d8a3fbd5SWill Andrews size_t autostart, autostop, maxval;
1409b50d902SRodney W. Grimes 
141a8522a9bSTim J. Robbins char *positions;
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes void
1449b50d902SRodney W. Grimes get_list(list)
1459b50d902SRodney W. Grimes 	char *list;
1469b50d902SRodney W. Grimes {
147d8a3fbd5SWill Andrews 	size_t setautostart, start, stop;
1482c39ae65SEivind Eklund 	char *pos;
1499b50d902SRodney W. Grimes 	char *p;
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes 	/*
1529b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1539b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1549b50d902SRodney W. Grimes 	 * This parser is less restrictive than the Draft 9 POSIX spec.
1559b50d902SRodney W. Grimes 	 * POSIX doesn't allow lists that aren't in increasing order or
1569b50d902SRodney W. Grimes 	 * overlapping lists.  We also handle "-3-5" although there's no
1579b50d902SRodney W. Grimes 	 * real reason too.
1589b50d902SRodney W. Grimes 	 */
1592c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1609b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1619b50d902SRodney W. Grimes 		if (*p == '-') {
1629b50d902SRodney W. Grimes 			++p;
1639b50d902SRodney W. Grimes 			setautostart = 1;
1649b50d902SRodney W. Grimes 		}
1652c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1669b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1679b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1689b50d902SRodney W. Grimes 				autostart = start;
1699b50d902SRodney W. Grimes 		}
1709b50d902SRodney W. Grimes 		if (*p == '-') {
1712c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1729b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1739b50d902SRodney W. Grimes 			if (*p == '-') {
1749b50d902SRodney W. Grimes 				++p;
1759b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1769b50d902SRodney W. Grimes 					autostop = stop;
1779b50d902SRodney W. Grimes 			}
1789b50d902SRodney W. Grimes 		}
1799b50d902SRodney W. Grimes 		if (*p)
180812bff99SPhilippe Charnier 			errx(1, "[-cf] list: illegal list value");
1819b50d902SRodney W. Grimes 		if (!stop || !start)
182812bff99SPhilippe Charnier 			errx(1, "[-cf] list: values may not include zero");
183a8522a9bSTim J. Robbins 		if (maxval < stop) {
1849b50d902SRodney W. Grimes 			maxval = stop;
185a8522a9bSTim J. Robbins 			needpos(maxval + 1);
186a8522a9bSTim J. Robbins 		}
1879b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
1889b50d902SRodney W. Grimes 	}
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 	/* overlapping ranges */
191a8522a9bSTim J. Robbins 	if (autostop && maxval > autostop) {
1929b50d902SRodney W. Grimes 		maxval = autostop;
193a8522a9bSTim J. Robbins 		needpos(maxval + 1);
194a8522a9bSTim J. Robbins 	}
1959b50d902SRodney W. Grimes 
1969b50d902SRodney W. Grimes 	/* set autostart */
1979b50d902SRodney W. Grimes 	if (autostart)
1989b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
1999b50d902SRodney W. Grimes }
2009b50d902SRodney W. Grimes 
201a8522a9bSTim J. Robbins void
202a8522a9bSTim J. Robbins needpos(size_t n)
203a8522a9bSTim J. Robbins {
204a8522a9bSTim J. Robbins 	static size_t npos;
205f457179aSTim J. Robbins 	size_t oldnpos;
206a8522a9bSTim J. Robbins 
207a8522a9bSTim J. Robbins 	/* Grow the positions array to at least the specified size. */
208a8522a9bSTim J. Robbins 	if (n > npos) {
209f457179aSTim J. Robbins 		oldnpos = npos;
210a8522a9bSTim J. Robbins 		if (npos == 0)
211a8522a9bSTim J. Robbins 			npos = n;
212a8522a9bSTim J. Robbins 		while (n > npos)
213a8522a9bSTim J. Robbins 			npos *= 2;
214a8522a9bSTim J. Robbins 		if ((positions = realloc(positions, npos)) == NULL)
215a8522a9bSTim J. Robbins 			err(1, "realloc");
216f457179aSTim J. Robbins 		memset((char *)positions + oldnpos, 0, npos - oldnpos);
217a8522a9bSTim J. Robbins 	}
218a8522a9bSTim J. Robbins }
219a8522a9bSTim J. Robbins 
2209b50d902SRodney W. Grimes /* ARGSUSED */
2219b50d902SRodney W. Grimes void
2229b50d902SRodney W. Grimes c_cut(fp, fname)
2239b50d902SRodney W. Grimes 	FILE *fp;
224d8a3fbd5SWill Andrews 	const char *fname;
2259b50d902SRodney W. Grimes {
2262c39ae65SEivind Eklund 	int ch, col;
2272c39ae65SEivind Eklund 	char *pos;
228d8a3fbd5SWill Andrews 	fname = NULL;
2299b50d902SRodney W. Grimes 
2302c39ae65SEivind Eklund 	ch = 0;
2319b50d902SRodney W. Grimes 	for (;;) {
2329b50d902SRodney W. Grimes 		pos = positions + 1;
2339b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
2349b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF)
2359b50d902SRodney W. Grimes 				return;
2369b50d902SRodney W. Grimes 			if (ch == '\n')
2379b50d902SRodney W. Grimes 				break;
2389b50d902SRodney W. Grimes 			if (*pos++)
2399b50d902SRodney W. Grimes 				(void)putchar(ch);
2409b50d902SRodney W. Grimes 		}
2412c39ae65SEivind Eklund 		if (ch != '\n') {
2429b50d902SRodney W. Grimes 			if (autostop)
2439b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n')
2449b50d902SRodney W. Grimes 					(void)putchar(ch);
2459b50d902SRodney W. Grimes 			else
2469b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n');
2472c39ae65SEivind Eklund 		}
2489b50d902SRodney W. Grimes 		(void)putchar('\n');
2499b50d902SRodney W. Grimes 	}
2509b50d902SRodney W. Grimes }
2519b50d902SRodney W. Grimes 
2529b50d902SRodney W. Grimes void
2539b50d902SRodney W. Grimes f_cut(fp, fname)
2549b50d902SRodney W. Grimes 	FILE *fp;
25541ff7633SDima Dorfman 	const char *fname __unused;
2569b50d902SRodney W. Grimes {
2572c39ae65SEivind Eklund 	int ch, field, isdelim;
2582c39ae65SEivind Eklund 	char *pos, *p, sep;
2599b50d902SRodney W. Grimes 	int output;
2601928e20eSDima Dorfman 	char *lbuf, *mlbuf = NULL;
2611928e20eSDima Dorfman 	size_t lbuflen;
2629b50d902SRodney W. Grimes 
2631928e20eSDima Dorfman 	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
2641928e20eSDima Dorfman 		/* Assert EOL has a newline. */
2651928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
2661928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
2671928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
2681928e20eSDima Dorfman 			if (mlbuf == NULL)
2691928e20eSDima Dorfman 				err(1, "malloc");
2701928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
2711928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
2721928e20eSDima Dorfman 			lbuf = mlbuf;
2731928e20eSDima Dorfman 		}
274eaf92380SAndrey A. Chernov 		output = 0;
2759b50d902SRodney W. Grimes 		for (isdelim = 0, p = lbuf;; ++p) {
2761928e20eSDima Dorfman 			ch = *p;
2779b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
2789b50d902SRodney W. Grimes 			if (ch == sep)
2799b50d902SRodney W. Grimes 				isdelim = 1;
2809b50d902SRodney W. Grimes 			if (ch == '\n') {
2819b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
2821928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
2839b50d902SRodney W. Grimes 				break;
2849b50d902SRodney W. Grimes 			}
2859b50d902SRodney W. Grimes 		}
2869b50d902SRodney W. Grimes 		if (!isdelim)
2879b50d902SRodney W. Grimes 			continue;
2889b50d902SRodney W. Grimes 
2899b50d902SRodney W. Grimes 		pos = positions + 1;
2909b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
2919b50d902SRodney W. Grimes 			if (*pos) {
2929b50d902SRodney W. Grimes 				if (output++)
2939b50d902SRodney W. Grimes 					(void)putchar(sep);
2949b50d902SRodney W. Grimes 				while ((ch = *p++) != '\n' && ch != sep)
2959b50d902SRodney W. Grimes 					(void)putchar(ch);
2962c39ae65SEivind Eklund 			} else {
2972c39ae65SEivind Eklund 				while ((ch = *p++) != '\n' && ch != sep)
2982c39ae65SEivind Eklund 					continue;
2992c39ae65SEivind Eklund 			}
3009b50d902SRodney W. Grimes 			if (ch == '\n')
3019b50d902SRodney W. Grimes 				break;
3029b50d902SRodney W. Grimes 		}
3032c39ae65SEivind Eklund 		if (ch != '\n') {
3049b50d902SRodney W. Grimes 			if (autostop) {
3059b50d902SRodney W. Grimes 				if (output)
3069b50d902SRodney W. Grimes 					(void)putchar(sep);
3079b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
3089b50d902SRodney W. Grimes 					(void)putchar(ch);
3099b50d902SRodney W. Grimes 			} else
3109b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
3112c39ae65SEivind Eklund 		}
3129b50d902SRodney W. Grimes 		(void)putchar('\n');
3139b50d902SRodney W. Grimes 	}
3141928e20eSDima Dorfman 	if (mlbuf != NULL)
3151928e20eSDima Dorfman 		free(mlbuf);
3169b50d902SRodney W. Grimes }
3179b50d902SRodney W. Grimes 
318812bff99SPhilippe Charnier static void
3199b50d902SRodney W. Grimes usage()
3209b50d902SRodney W. Grimes {
321d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
322d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
323d51c6625SEivind Eklund 		"       cut -c list [file ...]",
324812bff99SPhilippe Charnier 		"       cut -f list [-s] [-d delim] [file ...]");
3259b50d902SRodney W. Grimes 	exit(1);
3269b50d902SRodney W. Grimes }
327