xref: /freebsd/usr.bin/cut/cut.c (revision 2c39ae659cbfbbf953ba18f8452e3db91edaeb47)
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";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
44fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
459b50d902SRodney W. Grimes #endif /* not lint */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <ctype.h>
48812bff99SPhilippe Charnier #include <err.h>
499b50d902SRodney W. Grimes #include <errno.h>
509b50d902SRodney W. Grimes #include <limits.h>
51d51c6625SEivind Eklund #include <locale.h>
529b50d902SRodney W. Grimes #include <stdio.h>
539b50d902SRodney W. Grimes #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
55df3f5d9dSPeter Wemm #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes int	cflag;
589b50d902SRodney W. Grimes char	dchar;
599b50d902SRodney W. Grimes int	dflag;
609b50d902SRodney W. Grimes int	fflag;
619b50d902SRodney W. Grimes int	sflag;
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes void	c_cut __P((FILE *, char *));
649b50d902SRodney W. Grimes void	f_cut __P((FILE *, char *));
659b50d902SRodney W. Grimes void	get_list __P((char *));
662c39ae65SEivind Eklund int	main __P((int, char **));
67812bff99SPhilippe Charnier static 	void usage __P((void));
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes int
709b50d902SRodney W. Grimes main(argc, argv)
719b50d902SRodney W. Grimes 	int argc;
729b50d902SRodney W. Grimes 	char *argv[];
739b50d902SRodney W. Grimes {
749b50d902SRodney W. Grimes 	FILE *fp;
75fa146c53SArchie Cobbs 	void (*fcn) __P((FILE *, char *)) = NULL;
769b50d902SRodney W. Grimes 	int ch;
779b50d902SRodney W. Grimes 
782c39ae65SEivind Eklund 	fcn = NULL;
79d51c6625SEivind Eklund 	setlocale (LC_ALL, "");
80d51c6625SEivind Eklund 
819b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
829b50d902SRodney W. Grimes 
83d51c6625SEivind Eklund 	/* Since we don't support multi-byte characters, the -c and -b
84d51c6625SEivind Eklund 	   options are equivalent, and the -n option is meaningless. */
855183fb53SEivind Eklund 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
869b50d902SRodney W. Grimes 		switch(ch) {
87d51c6625SEivind Eklund 		case 'b':
889b50d902SRodney W. Grimes 		case 'c':
899b50d902SRodney W. Grimes 			fcn = c_cut;
909b50d902SRodney W. Grimes 			get_list(optarg);
919b50d902SRodney W. Grimes 			cflag = 1;
929b50d902SRodney W. Grimes 			break;
939b50d902SRodney W. Grimes 		case 'd':
949b50d902SRodney W. Grimes 			dchar = *optarg;
959b50d902SRodney W. Grimes 			dflag = 1;
969b50d902SRodney W. Grimes 			break;
979b50d902SRodney W. Grimes 		case 'f':
989b50d902SRodney W. Grimes 			get_list(optarg);
999b50d902SRodney W. Grimes 			fcn = f_cut;
1009b50d902SRodney W. Grimes 			fflag = 1;
1019b50d902SRodney W. Grimes 			break;
1029b50d902SRodney W. Grimes 		case 's':
1039b50d902SRodney W. Grimes 			sflag = 1;
1049b50d902SRodney W. Grimes 			break;
105d51c6625SEivind Eklund 		case 'n':
106d51c6625SEivind Eklund 			break;
1079b50d902SRodney W. Grimes 		case '?':
1089b50d902SRodney W. Grimes 		default:
1099b50d902SRodney W. Grimes 			usage();
1109b50d902SRodney W. Grimes 		}
1119b50d902SRodney W. Grimes 	argc -= optind;
1129b50d902SRodney W. Grimes 	argv += optind;
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes 	if (fflag) {
1159b50d902SRodney W. Grimes 		if (cflag)
1169b50d902SRodney W. Grimes 			usage();
1179b50d902SRodney W. Grimes 	} else if (!cflag || dflag || sflag)
1189b50d902SRodney W. Grimes 		usage();
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes 	if (*argv)
1219b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
1229b50d902SRodney W. Grimes 			if (!(fp = fopen(*argv, "r")))
123812bff99SPhilippe Charnier 				err(1, "%s", *argv);
1249b50d902SRodney W. Grimes 			fcn(fp, *argv);
1259b50d902SRodney W. Grimes 			(void)fclose(fp);
1269b50d902SRodney W. Grimes 		}
1279b50d902SRodney W. Grimes 	else
1289b50d902SRodney W. Grimes 		fcn(stdin, "stdin");
1299b50d902SRodney W. Grimes 	exit(0);
1309b50d902SRodney W. Grimes }
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes int autostart, autostop, maxval;
1339b50d902SRodney W. Grimes 
1349b50d902SRodney W. Grimes char positions[_POSIX2_LINE_MAX + 1];
1359b50d902SRodney W. Grimes 
1369b50d902SRodney W. Grimes void
1379b50d902SRodney W. Grimes get_list(list)
1389b50d902SRodney W. Grimes 	char *list;
1399b50d902SRodney W. Grimes {
1402c39ae65SEivind Eklund 	int setautostart, start, stop;
1412c39ae65SEivind Eklund 	char *pos;
1429b50d902SRodney W. Grimes 	char *p;
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes 	/*
1459b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1469b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1479b50d902SRodney W. Grimes 	 * This parser is less restrictive than the Draft 9 POSIX spec.
1489b50d902SRodney W. Grimes 	 * POSIX doesn't allow lists that aren't in increasing order or
1499b50d902SRodney W. Grimes 	 * overlapping lists.  We also handle "-3-5" although there's no
1509b50d902SRodney W. Grimes 	 * real reason too.
1519b50d902SRodney W. Grimes 	 */
1522c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1539b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1549b50d902SRodney W. Grimes 		if (*p == '-') {
1559b50d902SRodney W. Grimes 			++p;
1569b50d902SRodney W. Grimes 			setautostart = 1;
1579b50d902SRodney W. Grimes 		}
1582c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1599b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1609b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1619b50d902SRodney W. Grimes 				autostart = start;
1629b50d902SRodney W. Grimes 		}
1639b50d902SRodney W. Grimes 		if (*p == '-') {
1642c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1659b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1669b50d902SRodney W. Grimes 			if (*p == '-') {
1679b50d902SRodney W. Grimes 				++p;
1689b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1699b50d902SRodney W. Grimes 					autostop = stop;
1709b50d902SRodney W. Grimes 			}
1719b50d902SRodney W. Grimes 		}
1729b50d902SRodney W. Grimes 		if (*p)
173812bff99SPhilippe Charnier 			errx(1, "[-cf] list: illegal list value");
1749b50d902SRodney W. Grimes 		if (!stop || !start)
175812bff99SPhilippe Charnier 			errx(1, "[-cf] list: values may not include zero");
1769b50d902SRodney W. Grimes 		if (stop > _POSIX2_LINE_MAX)
177812bff99SPhilippe Charnier 			errx(1, "[-cf] list: %d too large (max %d)",
1789b50d902SRodney W. Grimes 			    stop, _POSIX2_LINE_MAX);
1799b50d902SRodney W. Grimes 		if (maxval < stop)
1809b50d902SRodney W. Grimes 			maxval = stop;
1819b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
1829b50d902SRodney W. Grimes 	}
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 	/* overlapping ranges */
1859b50d902SRodney W. Grimes 	if (autostop && maxval > autostop)
1869b50d902SRodney W. Grimes 		maxval = autostop;
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 	/* set autostart */
1899b50d902SRodney W. Grimes 	if (autostart)
1909b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
1919b50d902SRodney W. Grimes }
1929b50d902SRodney W. Grimes 
1939b50d902SRodney W. Grimes /* ARGSUSED */
1949b50d902SRodney W. Grimes void
1959b50d902SRodney W. Grimes c_cut(fp, fname)
1969b50d902SRodney W. Grimes 	FILE *fp;
1979b50d902SRodney W. Grimes 	char *fname;
1989b50d902SRodney W. Grimes {
1992c39ae65SEivind Eklund 	int ch, col;
2002c39ae65SEivind Eklund 	char *pos;
2019b50d902SRodney W. Grimes 
2022c39ae65SEivind Eklund 	ch = 0;
2039b50d902SRodney W. Grimes 	for (;;) {
2049b50d902SRodney W. Grimes 		pos = positions + 1;
2059b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
2069b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF)
2079b50d902SRodney W. Grimes 				return;
2089b50d902SRodney W. Grimes 			if (ch == '\n')
2099b50d902SRodney W. Grimes 				break;
2109b50d902SRodney W. Grimes 			if (*pos++)
2119b50d902SRodney W. Grimes 				(void)putchar(ch);
2129b50d902SRodney W. Grimes 		}
2132c39ae65SEivind Eklund 		if (ch != '\n') {
2149b50d902SRodney W. Grimes 			if (autostop)
2159b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n')
2169b50d902SRodney W. Grimes 					(void)putchar(ch);
2179b50d902SRodney W. Grimes 			else
2189b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n');
2192c39ae65SEivind Eklund 		}
2209b50d902SRodney W. Grimes 		(void)putchar('\n');
2219b50d902SRodney W. Grimes 	}
2229b50d902SRodney W. Grimes }
2239b50d902SRodney W. Grimes 
2249b50d902SRodney W. Grimes void
2259b50d902SRodney W. Grimes f_cut(fp, fname)
2269b50d902SRodney W. Grimes 	FILE *fp;
2279b50d902SRodney W. Grimes 	char *fname;
2289b50d902SRodney W. Grimes {
2292c39ae65SEivind Eklund 	int ch, field, isdelim;
2302c39ae65SEivind Eklund 	char *pos, *p, sep;
2319b50d902SRodney W. Grimes 	int output;
2329b50d902SRodney W. Grimes 	char lbuf[_POSIX2_LINE_MAX + 1];
2339b50d902SRodney W. Grimes 
234eaf92380SAndrey A. Chernov 	for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) {
235eaf92380SAndrey A. Chernov 		output = 0;
2369b50d902SRodney W. Grimes 		for (isdelim = 0, p = lbuf;; ++p) {
2379b50d902SRodney W. Grimes 			if (!(ch = *p))
2385183fb53SEivind Eklund 				errx(1, "%s: line too long.", fname);
2399b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
2409b50d902SRodney W. Grimes 			if (ch == sep)
2419b50d902SRodney W. Grimes 				isdelim = 1;
2429b50d902SRodney W. Grimes 			if (ch == '\n') {
2439b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
2449b50d902SRodney W. Grimes 					(void)printf("%s", lbuf);
2459b50d902SRodney W. Grimes 				break;
2469b50d902SRodney W. Grimes 			}
2479b50d902SRodney W. Grimes 		}
2489b50d902SRodney W. Grimes 		if (!isdelim)
2499b50d902SRodney W. Grimes 			continue;
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 		pos = positions + 1;
2529b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
2539b50d902SRodney W. Grimes 			if (*pos) {
2549b50d902SRodney W. Grimes 				if (output++)
2559b50d902SRodney W. Grimes 					(void)putchar(sep);
2569b50d902SRodney W. Grimes 				while ((ch = *p++) != '\n' && ch != sep)
2579b50d902SRodney W. Grimes 					(void)putchar(ch);
2582c39ae65SEivind Eklund 			} else {
2592c39ae65SEivind Eklund 				while ((ch = *p++) != '\n' && ch != sep)
2602c39ae65SEivind Eklund 					continue;
2612c39ae65SEivind Eklund 			}
2629b50d902SRodney W. Grimes 			if (ch == '\n')
2639b50d902SRodney W. Grimes 				break;
2649b50d902SRodney W. Grimes 		}
2652c39ae65SEivind Eklund 		if (ch != '\n') {
2669b50d902SRodney W. Grimes 			if (autostop) {
2679b50d902SRodney W. Grimes 				if (output)
2689b50d902SRodney W. Grimes 					(void)putchar(sep);
2699b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
2709b50d902SRodney W. Grimes 					(void)putchar(ch);
2719b50d902SRodney W. Grimes 			} else
2729b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
2732c39ae65SEivind Eklund 		}
2749b50d902SRodney W. Grimes 		(void)putchar('\n');
2759b50d902SRodney W. Grimes 	}
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
278812bff99SPhilippe Charnier static void
2799b50d902SRodney W. Grimes usage()
2809b50d902SRodney W. Grimes {
281d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
282d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
283d51c6625SEivind Eklund 		"       cut -c list [file ...]",
284812bff99SPhilippe Charnier 		"       cut -f list [-s] [-d delim] [file ...]");
2859b50d902SRodney W. Grimes 	exit(1);
2869b50d902SRodney W. Grimes }
287