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