xref: /freebsd/usr.bin/cut/cut.c (revision a6ea32c3c5709e4b914a69fee3a7a8070ac14bc6)
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 **);
6599557a79SWill Andrews static 	void usage (void);
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes int
689b50d902SRodney W. Grimes main(argc, argv)
699b50d902SRodney W. Grimes 	int argc;
709b50d902SRodney W. Grimes 	char *argv[];
719b50d902SRodney W. Grimes {
729b50d902SRodney W. Grimes 	FILE *fp;
7399557a79SWill Andrews 	void (*fcn) (FILE *, const char *) = NULL;
74a6ea32c3STim J. Robbins 	int ch, rval;
759b50d902SRodney W. Grimes 
762c39ae65SEivind Eklund 	fcn = NULL;
77d51c6625SEivind Eklund 	setlocale (LC_ALL, "");
78d51c6625SEivind Eklund 
799b50d902SRodney W. Grimes 	dchar = '\t';			/* default delimiter is \t */
809b50d902SRodney W. Grimes 
81d51c6625SEivind Eklund 	/* Since we don't support multi-byte characters, the -c and -b
82d51c6625SEivind Eklund 	   options are equivalent, and the -n option is meaningless. */
835183fb53SEivind Eklund 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
849b50d902SRodney W. Grimes 		switch(ch) {
85d51c6625SEivind Eklund 		case 'b':
869b50d902SRodney W. Grimes 		case 'c':
879b50d902SRodney W. Grimes 			fcn = c_cut;
889b50d902SRodney W. Grimes 			get_list(optarg);
899b50d902SRodney W. Grimes 			cflag = 1;
909b50d902SRodney W. Grimes 			break;
919b50d902SRodney W. Grimes 		case 'd':
929b50d902SRodney W. Grimes 			dchar = *optarg;
939b50d902SRodney W. Grimes 			dflag = 1;
949b50d902SRodney W. Grimes 			break;
959b50d902SRodney W. Grimes 		case 'f':
969b50d902SRodney W. Grimes 			get_list(optarg);
979b50d902SRodney W. Grimes 			fcn = f_cut;
989b50d902SRodney W. Grimes 			fflag = 1;
999b50d902SRodney W. Grimes 			break;
1009b50d902SRodney W. Grimes 		case 's':
1019b50d902SRodney W. Grimes 			sflag = 1;
1029b50d902SRodney W. Grimes 			break;
103d51c6625SEivind Eklund 		case 'n':
104d51c6625SEivind Eklund 			break;
1059b50d902SRodney W. Grimes 		case '?':
1069b50d902SRodney W. Grimes 		default:
1079b50d902SRodney W. Grimes 			usage();
1089b50d902SRodney W. Grimes 		}
1099b50d902SRodney W. Grimes 	argc -= optind;
1109b50d902SRodney W. Grimes 	argv += optind;
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	if (fflag) {
1139b50d902SRodney W. Grimes 		if (cflag)
1149b50d902SRodney W. Grimes 			usage();
1159b50d902SRodney W. Grimes 	} else if (!cflag || dflag || sflag)
1169b50d902SRodney W. Grimes 		usage();
1179b50d902SRodney W. Grimes 
118a6ea32c3STim J. Robbins 	rval = 0;
1199b50d902SRodney W. Grimes 	if (*argv)
1209b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
121a6ea32c3STim J. Robbins 			if (!(fp = fopen(*argv, "r"))) {
122a6ea32c3STim J. Robbins 				warn("%s", *argv);
123a6ea32c3STim J. Robbins 				rval = 1;
124a6ea32c3STim J. Robbins 				continue;
125a6ea32c3STim J. Robbins 			}
1269b50d902SRodney W. Grimes 			fcn(fp, *argv);
1279b50d902SRodney W. Grimes 			(void)fclose(fp);
1289b50d902SRodney W. Grimes 		}
1299b50d902SRodney W. Grimes 	else
1309b50d902SRodney W. Grimes 		fcn(stdin, "stdin");
131a6ea32c3STim J. Robbins 	exit(rval);
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes 
134d8a3fbd5SWill Andrews size_t autostart, autostop, maxval;
1359b50d902SRodney W. Grimes 
1369b50d902SRodney W. Grimes char positions[_POSIX2_LINE_MAX + 1];
1379b50d902SRodney W. Grimes 
1389b50d902SRodney W. Grimes void
1399b50d902SRodney W. Grimes get_list(list)
1409b50d902SRodney W. Grimes 	char *list;
1419b50d902SRodney W. Grimes {
142d8a3fbd5SWill Andrews 	size_t setautostart, start, stop;
1432c39ae65SEivind Eklund 	char *pos;
1449b50d902SRodney W. Grimes 	char *p;
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 	/*
1479b50d902SRodney W. Grimes 	 * set a byte in the positions array to indicate if a field or
1489b50d902SRodney W. Grimes 	 * column is to be selected; use +1, it's 1-based, not 0-based.
1499b50d902SRodney W. Grimes 	 * This parser is less restrictive than the Draft 9 POSIX spec.
1509b50d902SRodney W. Grimes 	 * POSIX doesn't allow lists that aren't in increasing order or
1519b50d902SRodney W. Grimes 	 * overlapping lists.  We also handle "-3-5" although there's no
1529b50d902SRodney W. Grimes 	 * real reason too.
1539b50d902SRodney W. Grimes 	 */
1542c39ae65SEivind Eklund 	for (; (p = strsep(&list, ", \t")) != NULL;) {
1559b50d902SRodney W. Grimes 		setautostart = start = stop = 0;
1569b50d902SRodney W. Grimes 		if (*p == '-') {
1579b50d902SRodney W. Grimes 			++p;
1589b50d902SRodney W. Grimes 			setautostart = 1;
1599b50d902SRodney W. Grimes 		}
1602c39ae65SEivind Eklund 		if (isdigit((unsigned char)*p)) {
1619b50d902SRodney W. Grimes 			start = stop = strtol(p, &p, 10);
1629b50d902SRodney W. Grimes 			if (setautostart && start > autostart)
1639b50d902SRodney W. Grimes 				autostart = start;
1649b50d902SRodney W. Grimes 		}
1659b50d902SRodney W. Grimes 		if (*p == '-') {
1662c39ae65SEivind Eklund 			if (isdigit((unsigned char)p[1]))
1679b50d902SRodney W. Grimes 				stop = strtol(p + 1, &p, 10);
1689b50d902SRodney W. Grimes 			if (*p == '-') {
1699b50d902SRodney W. Grimes 				++p;
1709b50d902SRodney W. Grimes 				if (!autostop || autostop > stop)
1719b50d902SRodney W. Grimes 					autostop = stop;
1729b50d902SRodney W. Grimes 			}
1739b50d902SRodney W. Grimes 		}
1749b50d902SRodney W. Grimes 		if (*p)
175812bff99SPhilippe Charnier 			errx(1, "[-cf] list: illegal list value");
1769b50d902SRodney W. Grimes 		if (!stop || !start)
177812bff99SPhilippe Charnier 			errx(1, "[-cf] list: values may not include zero");
1789b50d902SRodney W. Grimes 		if (stop > _POSIX2_LINE_MAX)
17941ff7633SDima Dorfman 			errx(1, "[-cf] list: %ld too large (max %d)",
18041ff7633SDima Dorfman 			    (long)stop, _POSIX2_LINE_MAX);
1819b50d902SRodney W. Grimes 		if (maxval < stop)
1829b50d902SRodney W. Grimes 			maxval = stop;
1839b50d902SRodney W. Grimes 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
1849b50d902SRodney W. Grimes 	}
1859b50d902SRodney W. Grimes 
1869b50d902SRodney W. Grimes 	/* overlapping ranges */
1879b50d902SRodney W. Grimes 	if (autostop && maxval > autostop)
1889b50d902SRodney W. Grimes 		maxval = autostop;
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 	/* set autostart */
1919b50d902SRodney W. Grimes 	if (autostart)
1929b50d902SRodney W. Grimes 		memset(positions + 1, '1', autostart);
1939b50d902SRodney W. Grimes }
1949b50d902SRodney W. Grimes 
1959b50d902SRodney W. Grimes /* ARGSUSED */
1969b50d902SRodney W. Grimes void
1979b50d902SRodney W. Grimes c_cut(fp, fname)
1989b50d902SRodney W. Grimes 	FILE *fp;
199d8a3fbd5SWill Andrews 	const char *fname;
2009b50d902SRodney W. Grimes {
2012c39ae65SEivind Eklund 	int ch, col;
2022c39ae65SEivind Eklund 	char *pos;
203d8a3fbd5SWill Andrews 	fname = NULL;
2049b50d902SRodney W. Grimes 
2052c39ae65SEivind Eklund 	ch = 0;
2069b50d902SRodney W. Grimes 	for (;;) {
2079b50d902SRodney W. Grimes 		pos = positions + 1;
2089b50d902SRodney W. Grimes 		for (col = maxval; col; --col) {
2099b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF)
2109b50d902SRodney W. Grimes 				return;
2119b50d902SRodney W. Grimes 			if (ch == '\n')
2129b50d902SRodney W. Grimes 				break;
2139b50d902SRodney W. Grimes 			if (*pos++)
2149b50d902SRodney W. Grimes 				(void)putchar(ch);
2159b50d902SRodney W. Grimes 		}
2162c39ae65SEivind Eklund 		if (ch != '\n') {
2179b50d902SRodney W. Grimes 			if (autostop)
2189b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n')
2199b50d902SRodney W. Grimes 					(void)putchar(ch);
2209b50d902SRodney W. Grimes 			else
2219b50d902SRodney W. Grimes 				while ((ch = getc(fp)) != EOF && ch != '\n');
2222c39ae65SEivind Eklund 		}
2239b50d902SRodney W. Grimes 		(void)putchar('\n');
2249b50d902SRodney W. Grimes 	}
2259b50d902SRodney W. Grimes }
2269b50d902SRodney W. Grimes 
2279b50d902SRodney W. Grimes void
2289b50d902SRodney W. Grimes f_cut(fp, fname)
2299b50d902SRodney W. Grimes 	FILE *fp;
23041ff7633SDima Dorfman 	const char *fname __unused;
2319b50d902SRodney W. Grimes {
2322c39ae65SEivind Eklund 	int ch, field, isdelim;
2332c39ae65SEivind Eklund 	char *pos, *p, sep;
2349b50d902SRodney W. Grimes 	int output;
2351928e20eSDima Dorfman 	char *lbuf, *mlbuf = NULL;
2361928e20eSDima Dorfman 	size_t lbuflen;
2379b50d902SRodney W. Grimes 
2381928e20eSDima Dorfman 	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
2391928e20eSDima Dorfman 		/* Assert EOL has a newline. */
2401928e20eSDima Dorfman 		if (*(lbuf + lbuflen - 1) != '\n') {
2411928e20eSDima Dorfman 			/* Can't have > 1 line with no trailing newline. */
2421928e20eSDima Dorfman 			mlbuf = malloc(lbuflen + 1);
2431928e20eSDima Dorfman 			if (mlbuf == NULL)
2441928e20eSDima Dorfman 				err(1, "malloc");
2451928e20eSDima Dorfman 			memcpy(mlbuf, lbuf, lbuflen);
2461928e20eSDima Dorfman 			*(mlbuf + lbuflen) = '\n';
2471928e20eSDima Dorfman 			lbuf = mlbuf;
2481928e20eSDima Dorfman 		}
249eaf92380SAndrey A. Chernov 		output = 0;
2509b50d902SRodney W. Grimes 		for (isdelim = 0, p = lbuf;; ++p) {
2511928e20eSDima Dorfman 			ch = *p;
2529b50d902SRodney W. Grimes 			/* this should work if newline is delimiter */
2539b50d902SRodney W. Grimes 			if (ch == sep)
2549b50d902SRodney W. Grimes 				isdelim = 1;
2559b50d902SRodney W. Grimes 			if (ch == '\n') {
2569b50d902SRodney W. Grimes 				if (!isdelim && !sflag)
2571928e20eSDima Dorfman 					(void)fwrite(lbuf, lbuflen, 1, stdout);
2589b50d902SRodney W. Grimes 				break;
2599b50d902SRodney W. Grimes 			}
2609b50d902SRodney W. Grimes 		}
2619b50d902SRodney W. Grimes 		if (!isdelim)
2629b50d902SRodney W. Grimes 			continue;
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 		pos = positions + 1;
2659b50d902SRodney W. Grimes 		for (field = maxval, p = lbuf; field; --field, ++pos) {
2669b50d902SRodney W. Grimes 			if (*pos) {
2679b50d902SRodney W. Grimes 				if (output++)
2689b50d902SRodney W. Grimes 					(void)putchar(sep);
2699b50d902SRodney W. Grimes 				while ((ch = *p++) != '\n' && ch != sep)
2709b50d902SRodney W. Grimes 					(void)putchar(ch);
2712c39ae65SEivind Eklund 			} else {
2722c39ae65SEivind Eklund 				while ((ch = *p++) != '\n' && ch != sep)
2732c39ae65SEivind Eklund 					continue;
2742c39ae65SEivind Eklund 			}
2759b50d902SRodney W. Grimes 			if (ch == '\n')
2769b50d902SRodney W. Grimes 				break;
2779b50d902SRodney W. Grimes 		}
2782c39ae65SEivind Eklund 		if (ch != '\n') {
2799b50d902SRodney W. Grimes 			if (autostop) {
2809b50d902SRodney W. Grimes 				if (output)
2819b50d902SRodney W. Grimes 					(void)putchar(sep);
2829b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p)
2839b50d902SRodney W. Grimes 					(void)putchar(ch);
2849b50d902SRodney W. Grimes 			} else
2859b50d902SRodney W. Grimes 				for (; (ch = *p) != '\n'; ++p);
2862c39ae65SEivind Eklund 		}
2879b50d902SRodney W. Grimes 		(void)putchar('\n');
2889b50d902SRodney W. Grimes 	}
2891928e20eSDima Dorfman 	if (mlbuf != NULL)
2901928e20eSDima Dorfman 		free(mlbuf);
2919b50d902SRodney W. Grimes }
2929b50d902SRodney W. Grimes 
293812bff99SPhilippe Charnier static void
2949b50d902SRodney W. Grimes usage()
2959b50d902SRodney W. Grimes {
296d51c6625SEivind Eklund 	(void)fprintf(stderr, "%s\n%s\n%s\n",
297d51c6625SEivind Eklund 		"usage: cut -b list [-n] [file ...]",
298d51c6625SEivind Eklund 		"       cut -c list [file ...]",
299812bff99SPhilippe Charnier 		"       cut -f list [-s] [-d delim] [file ...]");
3009b50d902SRodney W. Grimes 	exit(1);
3019b50d902SRodney W. Grimes }
302