cut.c revision 97237
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  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
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41static const char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
42static const char rcsid[] =
43  "$FreeBSD: head/usr.bin/cut/cut.c 97237 2002-05-24 09:56:18Z tjr $";
44#endif /* not lint */
45
46#include <ctype.h>
47#include <err.h>
48#include <limits.h>
49#include <locale.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55int	cflag;
56char	dchar;
57int	dflag;
58int	fflag;
59int	sflag;
60
61void	c_cut (FILE *, const char *);
62void	f_cut (FILE *, const char *);
63void	get_list (char *);
64int	main (int, char **);
65void	needpos(size_t);
66static 	void usage (void);
67
68int
69main(argc, argv)
70	int argc;
71	char *argv[];
72{
73	FILE *fp;
74	void (*fcn) (FILE *, const char *) = NULL;
75	int ch, rval;
76
77	fcn = NULL;
78	setlocale (LC_ALL, "");
79
80	dchar = '\t';			/* default delimiter is \t */
81
82	/* Since we don't support multi-byte characters, the -c and -b
83	   options are equivalent, and the -n option is meaningless. */
84	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
85		switch(ch) {
86		case 'b':
87		case 'c':
88			fcn = c_cut;
89			get_list(optarg);
90			cflag = 1;
91			break;
92		case 'd':
93			dchar = *optarg;
94			dflag = 1;
95			break;
96		case 'f':
97			get_list(optarg);
98			fcn = f_cut;
99			fflag = 1;
100			break;
101		case 's':
102			sflag = 1;
103			break;
104		case 'n':
105			break;
106		case '?':
107		default:
108			usage();
109		}
110	argc -= optind;
111	argv += optind;
112
113	if (fflag) {
114		if (cflag)
115			usage();
116	} else if (!cflag || dflag || sflag)
117		usage();
118
119	rval = 0;
120	if (*argv)
121		for (; *argv; ++argv) {
122			if (strcmp(*argv, "-") == 0)
123				fcn(stdin, "stdin");
124			else {
125				if (!(fp = fopen(*argv, "r"))) {
126					warn("%s", *argv);
127					rval = 1;
128					continue;
129				}
130				fcn(fp, *argv);
131				(void)fclose(fp);
132			}
133		}
134	else
135		fcn(stdin, "stdin");
136	exit(rval);
137}
138
139size_t autostart, autostop, maxval;
140
141char *positions;
142
143void
144get_list(list)
145	char *list;
146{
147	size_t setautostart, start, stop;
148	char *pos;
149	char *p;
150
151	/*
152	 * set a byte in the positions array to indicate if a field or
153	 * column is to be selected; use +1, it's 1-based, not 0-based.
154	 * This parser is less restrictive than the Draft 9 POSIX spec.
155	 * POSIX doesn't allow lists that aren't in increasing order or
156	 * overlapping lists.  We also handle "-3-5" although there's no
157	 * real reason too.
158	 */
159	for (; (p = strsep(&list, ", \t")) != NULL;) {
160		setautostart = start = stop = 0;
161		if (*p == '-') {
162			++p;
163			setautostart = 1;
164		}
165		if (isdigit((unsigned char)*p)) {
166			start = stop = strtol(p, &p, 10);
167			if (setautostart && start > autostart)
168				autostart = start;
169		}
170		if (*p == '-') {
171			if (isdigit((unsigned char)p[1]))
172				stop = strtol(p + 1, &p, 10);
173			if (*p == '-') {
174				++p;
175				if (!autostop || autostop > stop)
176					autostop = stop;
177			}
178		}
179		if (*p)
180			errx(1, "[-cf] list: illegal list value");
181		if (!stop || !start)
182			errx(1, "[-cf] list: values may not include zero");
183		if (maxval < stop) {
184			maxval = stop;
185			needpos(maxval + 1);
186		}
187		for (pos = positions + start; start++ <= stop; *pos++ = 1);
188	}
189
190	/* overlapping ranges */
191	if (autostop && maxval > autostop) {
192		maxval = autostop;
193		needpos(maxval + 1);
194	}
195
196	/* set autostart */
197	if (autostart)
198		memset(positions + 1, '1', autostart);
199}
200
201void
202needpos(size_t n)
203{
204	static size_t npos;
205
206	/* Grow the positions array to at least the specified size. */
207	if (n > npos) {
208		if (npos == 0)
209			npos = n;
210		while (n > npos)
211			npos *= 2;
212		if ((positions = realloc(positions, npos)) == NULL)
213			err(1, "realloc");
214	}
215}
216
217/* ARGSUSED */
218void
219c_cut(fp, fname)
220	FILE *fp;
221	const char *fname;
222{
223	int ch, col;
224	char *pos;
225	fname = NULL;
226
227	ch = 0;
228	for (;;) {
229		pos = positions + 1;
230		for (col = maxval; col; --col) {
231			if ((ch = getc(fp)) == EOF)
232				return;
233			if (ch == '\n')
234				break;
235			if (*pos++)
236				(void)putchar(ch);
237		}
238		if (ch != '\n') {
239			if (autostop)
240				while ((ch = getc(fp)) != EOF && ch != '\n')
241					(void)putchar(ch);
242			else
243				while ((ch = getc(fp)) != EOF && ch != '\n');
244		}
245		(void)putchar('\n');
246	}
247}
248
249void
250f_cut(fp, fname)
251	FILE *fp;
252	const char *fname __unused;
253{
254	int ch, field, isdelim;
255	char *pos, *p, sep;
256	int output;
257	char *lbuf, *mlbuf = NULL;
258	size_t lbuflen;
259
260	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
261		/* Assert EOL has a newline. */
262		if (*(lbuf + lbuflen - 1) != '\n') {
263			/* Can't have > 1 line with no trailing newline. */
264			mlbuf = malloc(lbuflen + 1);
265			if (mlbuf == NULL)
266				err(1, "malloc");
267			memcpy(mlbuf, lbuf, lbuflen);
268			*(mlbuf + lbuflen) = '\n';
269			lbuf = mlbuf;
270		}
271		output = 0;
272		for (isdelim = 0, p = lbuf;; ++p) {
273			ch = *p;
274			/* this should work if newline is delimiter */
275			if (ch == sep)
276				isdelim = 1;
277			if (ch == '\n') {
278				if (!isdelim && !sflag)
279					(void)fwrite(lbuf, lbuflen, 1, stdout);
280				break;
281			}
282		}
283		if (!isdelim)
284			continue;
285
286		pos = positions + 1;
287		for (field = maxval, p = lbuf; field; --field, ++pos) {
288			if (*pos) {
289				if (output++)
290					(void)putchar(sep);
291				while ((ch = *p++) != '\n' && ch != sep)
292					(void)putchar(ch);
293			} else {
294				while ((ch = *p++) != '\n' && ch != sep)
295					continue;
296			}
297			if (ch == '\n')
298				break;
299		}
300		if (ch != '\n') {
301			if (autostop) {
302				if (output)
303					(void)putchar(sep);
304				for (; (ch = *p) != '\n'; ++p)
305					(void)putchar(ch);
306			} else
307				for (; (ch = *p) != '\n'; ++p);
308		}
309		(void)putchar('\n');
310	}
311	if (mlbuf != NULL)
312		free(mlbuf);
313}
314
315static void
316usage()
317{
318	(void)fprintf(stderr, "%s\n%s\n%s\n",
319		"usage: cut -b list [-n] [file ...]",
320		"       cut -c list [file ...]",
321		"       cut -f list [-s] [-d delim] [file ...]");
322	exit(1);
323}
324