cut.c revision 131226
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";
42#endif /* not lint */
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/usr.bin/cut/cut.c 131226 2004-06-28 06:05:36Z tjr $");
45
46#include <ctype.h>
47#include <err.h>
48#include <errno.h>
49#include <limits.h>
50#include <locale.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55#include <wchar.h>
56
57int	bflag;
58int	cflag;
59wchar_t	dchar;
60char	dcharmb[MB_LEN_MAX + 1];
61int	dflag;
62int	fflag;
63int	nflag;
64int	sflag;
65
66size_t	autostart, autostop, maxval;
67char *	positions;
68
69int	b_cut(FILE *, const char *);
70int	b_n_cut(FILE *, const char *);
71int	c_cut(FILE *, const char *);
72int	f_cut(FILE *, const char *);
73void	get_list(char *);
74void	needpos(size_t);
75static 	void usage(void);
76
77int
78main(int argc, char *argv[])
79{
80	FILE *fp;
81	int (*fcn)(FILE *, const char *);
82	int ch, rval;
83	size_t n;
84
85	setlocale(LC_ALL, "");
86
87	fcn = NULL;
88	dchar = '\t';			/* default delimiter is \t */
89	strcpy(dcharmb, "\t");
90
91	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
92		switch(ch) {
93		case 'b':
94			get_list(optarg);
95			bflag = 1;
96			break;
97		case 'c':
98			get_list(optarg);
99			cflag = 1;
100			break;
101		case 'd':
102			n = mbrtowc(&dchar, optarg, MB_LEN_MAX, NULL);
103			if (dchar == '\0' || n != strlen(optarg))
104				errx(1, "bad delimiter");
105			strcpy(dcharmb, optarg);
106			dflag = 1;
107			break;
108		case 'f':
109			get_list(optarg);
110			fflag = 1;
111			break;
112		case 's':
113			sflag = 1;
114			break;
115		case 'n':
116			nflag = 1;
117			break;
118		case '?':
119		default:
120			usage();
121		}
122	argc -= optind;
123	argv += optind;
124
125	if (fflag) {
126		if (bflag || cflag || nflag)
127			usage();
128	} else if (!(bflag || cflag) || dflag || sflag)
129		usage();
130	else if (!bflag && nflag)
131		usage();
132
133	if (fflag)
134		fcn = f_cut;
135	else if (cflag)
136		fcn = MB_CUR_MAX > 1 ? c_cut : b_cut;
137	else if (bflag)
138		fcn = nflag && MB_CUR_MAX > 1 ? b_n_cut : b_cut;
139
140	rval = 0;
141	if (*argv)
142		for (; *argv; ++argv) {
143			if (strcmp(*argv, "-") == 0)
144				rval |= fcn(stdin, "stdin");
145			else {
146				if (!(fp = fopen(*argv, "r"))) {
147					warn("%s", *argv);
148					rval = 1;
149					continue;
150				}
151				fcn(fp, *argv);
152				(void)fclose(fp);
153			}
154		}
155	else
156		rval = fcn(stdin, "stdin");
157	exit(rval);
158}
159
160void
161get_list(char *list)
162{
163	size_t setautostart, start, stop;
164	char *pos;
165	char *p;
166
167	/*
168	 * set a byte in the positions array to indicate if a field or
169	 * column is to be selected; use +1, it's 1-based, not 0-based.
170	 * Numbers and number ranges may be overlapping, repeated, and in
171	 * any order. We handle "-3-5" although there's no real reason too.
172	 */
173	for (; (p = strsep(&list, ", \t")) != NULL;) {
174		setautostart = start = stop = 0;
175		if (*p == '-') {
176			++p;
177			setautostart = 1;
178		}
179		if (isdigit((unsigned char)*p)) {
180			start = stop = strtol(p, &p, 10);
181			if (setautostart && start > autostart)
182				autostart = start;
183		}
184		if (*p == '-') {
185			if (isdigit((unsigned char)p[1]))
186				stop = strtol(p + 1, &p, 10);
187			if (*p == '-') {
188				++p;
189				if (!autostop || autostop > stop)
190					autostop = stop;
191			}
192		}
193		if (*p)
194			errx(1, "[-cf] list: illegal list value");
195		if (!stop || !start)
196			errx(1, "[-cf] list: values may not include zero");
197		if (maxval < stop) {
198			maxval = stop;
199			needpos(maxval + 1);
200		}
201		for (pos = positions + start; start++ <= stop; *pos++ = 1);
202	}
203
204	/* overlapping ranges */
205	if (autostop && maxval > autostop) {
206		maxval = autostop;
207		needpos(maxval + 1);
208	}
209
210	/* set autostart */
211	if (autostart)
212		memset(positions + 1, '1', autostart);
213}
214
215void
216needpos(size_t n)
217{
218	static size_t npos;
219	size_t oldnpos;
220
221	/* Grow the positions array to at least the specified size. */
222	if (n > npos) {
223		oldnpos = npos;
224		if (npos == 0)
225			npos = n;
226		while (n > npos)
227			npos *= 2;
228		if ((positions = realloc(positions, npos)) == NULL)
229			err(1, "realloc");
230		memset((char *)positions + oldnpos, 0, npos - oldnpos);
231	}
232}
233
234int
235b_cut(FILE *fp, const char *fname __unused)
236{
237	int ch, col;
238	char *pos;
239
240	ch = 0;
241	for (;;) {
242		pos = positions + 1;
243		for (col = maxval; col; --col) {
244			if ((ch = getc(fp)) == EOF)
245				return (0);
246			if (ch == '\n')
247				break;
248			if (*pos++)
249				(void)putchar(ch);
250		}
251		if (ch != '\n') {
252			if (autostop)
253				while ((ch = getc(fp)) != EOF && ch != '\n')
254					(void)putchar(ch);
255			else
256				while ((ch = getc(fp)) != EOF && ch != '\n');
257		}
258		(void)putchar('\n');
259	}
260	return (0);
261}
262
263/*
264 * Cut based on byte positions, taking care not to split multibyte characters.
265 * Although this function also handles the case where -n is not specified,
266 * b_cut() ought to be much faster.
267 */
268int
269b_n_cut(FILE *fp, const char *fname)
270{
271	size_t col, i, lbuflen;
272	char *lbuf;
273	int canwrite, clen, warned;
274	mbstate_t mbs;
275
276	memset(&mbs, 0, sizeof(mbs));
277	warned = 0;
278	while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
279		for (col = 0; lbuflen > 0; col += clen) {
280			if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
281				if (!warned) {
282					warn("%s", fname);
283					warned = 1;
284				}
285				memset(&mbs, 0, sizeof(mbs));
286				clen = 1;
287			}
288			if (clen == 0 || *lbuf == '\n')
289				break;
290			if (col < maxval && !positions[1 + col]) {
291				/*
292				 * Print the character if (1) after an initial
293				 * segment of un-selected bytes, the rest of
294				 * it is selected, and (2) the last byte is
295				 * selected.
296				 */
297				i = col;
298				while (i < col + clen && i < maxval &&
299				    !positions[1 + i])
300					i++;
301				canwrite = i < col + clen;
302				for (; i < col + clen && i < maxval; i++)
303					canwrite &= positions[1 + i];
304				if (canwrite)
305					fwrite(lbuf, 1, clen, stdout);
306			} else {
307				/*
308				 * Print the character if all of it has
309				 * been selected.
310				 */
311				canwrite = 1;
312				for (i = col; i < col + clen; i++)
313					if ((i >= maxval && !autostop) ||
314					    (i < maxval && !positions[1 + i])) {
315						canwrite = 0;
316						break;
317					}
318				if (canwrite)
319					fwrite(lbuf, 1, clen, stdout);
320			}
321			lbuf += clen;
322			lbuflen -= clen;
323		}
324		if (lbuflen > 0)
325			putchar('\n');
326	}
327	return (warned);
328}
329
330int
331c_cut(FILE *fp, const char *fname)
332{
333	wint_t ch;
334	int col;
335	char *pos;
336
337	ch = 0;
338	for (;;) {
339		pos = positions + 1;
340		for (col = maxval; col; --col) {
341			if ((ch = getwc(fp)) == WEOF)
342				goto out;
343			if (ch == '\n')
344				break;
345			if (*pos++)
346				(void)putwchar(ch);
347		}
348		if (ch != '\n') {
349			if (autostop)
350				while ((ch = getwc(fp)) != WEOF && ch != '\n')
351					(void)putwchar(ch);
352			else
353				while ((ch = getwc(fp)) != WEOF && ch != '\n');
354		}
355		(void)putwchar('\n');
356	}
357out:
358	if (ferror(fp)) {
359		warn("%s", fname);
360		return (1);
361	}
362	return (0);
363}
364
365int
366f_cut(FILE *fp, const char *fname)
367{
368	wchar_t ch;
369	int field, i, isdelim;
370	char *pos, *p;
371	wchar_t sep;
372	int output;
373	char *lbuf, *mlbuf;
374	size_t clen, lbuflen;
375
376	mlbuf = NULL;
377	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
378		/* Assert EOL has a newline. */
379		if (*(lbuf + lbuflen - 1) != '\n') {
380			/* Can't have > 1 line with no trailing newline. */
381			mlbuf = malloc(lbuflen + 1);
382			if (mlbuf == NULL)
383				err(1, "malloc");
384			memcpy(mlbuf, lbuf, lbuflen);
385			*(mlbuf + lbuflen) = '\n';
386			lbuf = mlbuf;
387		}
388		output = 0;
389		for (isdelim = 0, p = lbuf;; p += clen) {
390			clen = mbrtowc(&ch, p, lbuf + lbuflen - p, NULL);
391			if (clen == (size_t)-1 || clen == (size_t)-2) {
392				warnc(EILSEQ, "%s", fname);
393				free(mlbuf);
394				return (1);
395			}
396			if (clen == 0)
397				clen = 1;
398			/* this should work if newline is delimiter */
399			if (ch == sep)
400				isdelim = 1;
401			if (ch == '\n') {
402				if (!isdelim && !sflag)
403					(void)fwrite(lbuf, lbuflen, 1, stdout);
404				break;
405			}
406		}
407		if (!isdelim)
408			continue;
409
410		pos = positions + 1;
411		for (field = maxval, p = lbuf; field; --field, ++pos) {
412			if (*pos && output++)
413				for (i = 0; dcharmb[i] != '\0'; i++)
414					putchar(dcharmb[i]);
415			for (;;) {
416				clen = mbrtowc(&ch, p, lbuf + lbuflen - p,
417				    NULL);
418				if (clen == (size_t)-1 || clen == (size_t)-2) {
419					warnc(EILSEQ, "%s", fname);
420					free(mlbuf);
421					return (1);
422				}
423				if (clen == 0)
424					clen = 1;
425				p += clen;
426				if (ch == '\n' || ch == sep)
427					break;
428				if (*pos)
429					for (i = 0; i < (int)clen; i++)
430						putchar(p[i - clen]);
431			}
432			if (ch == '\n')
433				break;
434		}
435		if (ch != '\n') {
436			if (autostop) {
437				if (output)
438					for (i = 0; dcharmb[i] != '\0'; i++)
439						putchar(dcharmb[i]);
440				for (; (ch = *p) != '\n'; ++p)
441					(void)putchar(ch);
442			} else
443				for (; (ch = *p) != '\n'; ++p);
444		}
445		(void)putchar('\n');
446	}
447	free(mlbuf);
448	return (0);
449}
450
451static void
452usage(void)
453{
454	(void)fprintf(stderr, "%s\n%s\n%s\n",
455		"usage: cut -b list [-n] [file ...]",
456		"       cut -c list [file ...]",
457		"       cut -f list [-s] [-d delim] [file ...]");
458	exit(1);
459}
460