1*0a6a1f1dSLionel Sambuc /* $NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $ */
2d091a404SThomas Cort
3d091a404SThomas Cort /*
4d091a404SThomas Cort * Copyright (c) 1989, 1993
5d091a404SThomas Cort * The Regents of the University of California. All rights reserved.
6d091a404SThomas Cort *
7d091a404SThomas Cort * This code is derived from software contributed to Berkeley by
8d091a404SThomas Cort * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
9d091a404SThomas Cort *
10d091a404SThomas Cort * Redistribution and use in source and binary forms, with or without
11d091a404SThomas Cort * modification, are permitted provided that the following conditions
12d091a404SThomas Cort * are met:
13d091a404SThomas Cort * 1. Redistributions of source code must retain the above copyright
14d091a404SThomas Cort * notice, this list of conditions and the following disclaimer.
15d091a404SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
16d091a404SThomas Cort * notice, this list of conditions and the following disclaimer in the
17d091a404SThomas Cort * documentation and/or other materials provided with the distribution.
18d091a404SThomas Cort * 3. Neither the name of the University nor the names of its contributors
19d091a404SThomas Cort * may be used to endorse or promote products derived from this software
20d091a404SThomas Cort * without specific prior written permission.
21d091a404SThomas Cort *
22d091a404SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23d091a404SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24d091a404SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25d091a404SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26d091a404SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27d091a404SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28d091a404SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29d091a404SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30d091a404SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31d091a404SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d091a404SThomas Cort * SUCH DAMAGE.
33d091a404SThomas Cort */
34d091a404SThomas Cort
35d091a404SThomas Cort #include <sys/cdefs.h>
36d091a404SThomas Cort #ifndef lint
37d091a404SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38d091a404SThomas Cort The Regents of the University of California. All rights reserved.");
39d091a404SThomas Cort #endif /* not lint */
40d091a404SThomas Cort
41d091a404SThomas Cort #ifndef lint
42d091a404SThomas Cort #if 0
43d091a404SThomas Cort static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
44d091a404SThomas Cort #endif
45*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cut.c,v 1.29 2014/02/03 20:22:19 wiz Exp $");
46d091a404SThomas Cort #endif /* not lint */
47d091a404SThomas Cort
48d091a404SThomas Cort #include <ctype.h>
49d091a404SThomas Cort #include <err.h>
50d091a404SThomas Cort #include <errno.h>
51d091a404SThomas Cort #include <limits.h>
52d091a404SThomas Cort #include <locale.h>
53d091a404SThomas Cort #include <stdio.h>
54d091a404SThomas Cort #include <stdlib.h>
55d091a404SThomas Cort #include <string.h>
56d091a404SThomas Cort #include <unistd.h>
57d091a404SThomas Cort #include <util.h>
58d091a404SThomas Cort #include <wchar.h>
59d091a404SThomas Cort #include <sys/param.h>
60d091a404SThomas Cort
61d091a404SThomas Cort static int bflag;
62d091a404SThomas Cort static int cflag;
63d091a404SThomas Cort static char dchar;
64d091a404SThomas Cort static int dflag;
65d091a404SThomas Cort static int fflag;
66d091a404SThomas Cort static int sflag;
67d091a404SThomas Cort
68d091a404SThomas Cort static void b_cut(FILE *, const char *);
69d091a404SThomas Cort static void c_cut(FILE *, const char *);
70d091a404SThomas Cort static void f_cut(FILE *, const char *);
71d091a404SThomas Cort static void get_list(char *);
72d091a404SThomas Cort static void usage(void) __dead;
73d091a404SThomas Cort
74d091a404SThomas Cort int
main(int argc,char * argv[])75d091a404SThomas Cort main(int argc, char *argv[])
76d091a404SThomas Cort {
77d091a404SThomas Cort FILE *fp;
78d091a404SThomas Cort void (*fcn)(FILE *, const char *);
79*0a6a1f1dSLionel Sambuc int ch, rval;
80d091a404SThomas Cort
81d091a404SThomas Cort fcn = NULL;
82d091a404SThomas Cort (void)setlocale(LC_ALL, "");
83d091a404SThomas Cort
84d091a404SThomas Cort dchar = '\t'; /* default delimiter is \t */
85d091a404SThomas Cort
86d091a404SThomas Cort /* Since we don't support multi-byte characters, the -c and -b
87d091a404SThomas Cort options are equivalent, and the -n option is meaningless. */
88d091a404SThomas Cort while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
89d091a404SThomas Cort switch(ch) {
90d091a404SThomas Cort case 'b':
91d091a404SThomas Cort fcn = b_cut;
92d091a404SThomas Cort get_list(optarg);
93d091a404SThomas Cort bflag = 1;
94d091a404SThomas Cort break;
95d091a404SThomas Cort case 'c':
96d091a404SThomas Cort fcn = c_cut;
97d091a404SThomas Cort get_list(optarg);
98d091a404SThomas Cort cflag = 1;
99d091a404SThomas Cort break;
100d091a404SThomas Cort case 'd':
101d091a404SThomas Cort dchar = *optarg;
102d091a404SThomas Cort dflag = 1;
103d091a404SThomas Cort break;
104d091a404SThomas Cort case 'f':
105d091a404SThomas Cort get_list(optarg);
106d091a404SThomas Cort fcn = f_cut;
107d091a404SThomas Cort fflag = 1;
108d091a404SThomas Cort break;
109d091a404SThomas Cort case 's':
110d091a404SThomas Cort sflag = 1;
111d091a404SThomas Cort break;
112d091a404SThomas Cort case 'n':
113d091a404SThomas Cort break;
114d091a404SThomas Cort case '?':
115d091a404SThomas Cort default:
116d091a404SThomas Cort usage();
117d091a404SThomas Cort }
118d091a404SThomas Cort argc -= optind;
119d091a404SThomas Cort argv += optind;
120d091a404SThomas Cort
121d091a404SThomas Cort if (fflag) {
122d091a404SThomas Cort if (cflag || bflag)
123d091a404SThomas Cort usage();
124d091a404SThomas Cort } else if ((!cflag && !bflag) || dflag || sflag)
125d091a404SThomas Cort usage();
126d091a404SThomas Cort else if (bflag && cflag)
127d091a404SThomas Cort usage();
128d091a404SThomas Cort
129*0a6a1f1dSLionel Sambuc rval = 0;
130d091a404SThomas Cort if (*argv)
131d091a404SThomas Cort for (; *argv; ++argv) {
132d091a404SThomas Cort if (strcmp(*argv, "-") == 0)
133d091a404SThomas Cort fcn(stdin, "stdin");
134d091a404SThomas Cort else {
135*0a6a1f1dSLionel Sambuc if ((fp = fopen(*argv, "r"))) {
136d091a404SThomas Cort fcn(fp, *argv);
137d091a404SThomas Cort (void)fclose(fp);
138*0a6a1f1dSLionel Sambuc } else {
139*0a6a1f1dSLionel Sambuc rval = 1;
140*0a6a1f1dSLionel Sambuc warn("%s", *argv);
141*0a6a1f1dSLionel Sambuc }
142d091a404SThomas Cort }
143d091a404SThomas Cort }
144d091a404SThomas Cort else
145d091a404SThomas Cort fcn(stdin, "stdin");
146*0a6a1f1dSLionel Sambuc return(rval);
147d091a404SThomas Cort }
148d091a404SThomas Cort
149d091a404SThomas Cort static size_t autostart, autostop, maxval;
150d091a404SThomas Cort
151d091a404SThomas Cort static char *positions = NULL;
152d091a404SThomas Cort static size_t numpositions = 0;
153d091a404SThomas Cort #define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */
154d091a404SThomas Cort
155d091a404SThomas Cort static void
get_list(char * list)156d091a404SThomas Cort get_list(char *list)
157d091a404SThomas Cort {
158d091a404SThomas Cort size_t setautostart, start, stop;
159d091a404SThomas Cort char *pos;
160d091a404SThomas Cort char *p;
161d091a404SThomas Cort
162d091a404SThomas Cort if (positions == NULL) {
163d091a404SThomas Cort numpositions = ALLOC_CHUNK;
164d091a404SThomas Cort positions = ecalloc(numpositions, sizeof(*positions));
165d091a404SThomas Cort }
166d091a404SThomas Cort
167d091a404SThomas Cort /*
168d091a404SThomas Cort * set a byte in the positions array to indicate if a field or
169d091a404SThomas Cort * column is to be selected; use +1, it's 1-based, not 0-based.
170d091a404SThomas Cort * This parser is less restrictive than the Draft 9 POSIX spec.
171d091a404SThomas Cort * POSIX doesn't allow lists that aren't in increasing order or
172d091a404SThomas Cort * overlapping lists. We also handle "-3-5" although there's no
173d091a404SThomas Cort * real reason to.
174d091a404SThomas Cort */
175d091a404SThomas Cort for (; (p = strtok(list, ", \t")) != NULL; list = NULL) {
176d091a404SThomas Cort setautostart = start = stop = 0;
177d091a404SThomas Cort if (*p == '-') {
178d091a404SThomas Cort ++p;
179d091a404SThomas Cort setautostart = 1;
180d091a404SThomas Cort }
181d091a404SThomas Cort if (isdigit((unsigned char)*p)) {
182d091a404SThomas Cort start = stop = strtol(p, &p, 10);
183d091a404SThomas Cort if (setautostart && start > autostart)
184d091a404SThomas Cort autostart = start;
185d091a404SThomas Cort }
186d091a404SThomas Cort if (*p == '-') {
187d091a404SThomas Cort if (isdigit((unsigned char)p[1]))
188d091a404SThomas Cort stop = strtol(p + 1, &p, 10);
189d091a404SThomas Cort if (*p == '-') {
190d091a404SThomas Cort ++p;
191d091a404SThomas Cort if (!autostop || autostop > stop)
192d091a404SThomas Cort autostop = stop;
193d091a404SThomas Cort }
194d091a404SThomas Cort }
195d091a404SThomas Cort if (*p)
196d091a404SThomas Cort errx(1, "[-bcf] list: illegal list value");
197d091a404SThomas Cort if (!stop || !start)
198d091a404SThomas Cort errx(1, "[-bcf] list: values may not include zero");
199d091a404SThomas Cort if (stop + 1 > numpositions) {
200d091a404SThomas Cort size_t newsize;
201d091a404SThomas Cort newsize = roundup(stop + 1, ALLOC_CHUNK);
202d091a404SThomas Cort positions = erealloc(positions, newsize);
203d091a404SThomas Cort (void)memset(positions + numpositions, 0,
204d091a404SThomas Cort newsize - numpositions);
205d091a404SThomas Cort numpositions = newsize;
206d091a404SThomas Cort }
207d091a404SThomas Cort if (maxval < stop)
208d091a404SThomas Cort maxval = stop;
209d091a404SThomas Cort for (pos = positions + start; start++ <= stop; pos++)
210d091a404SThomas Cort *pos = 1;
211d091a404SThomas Cort }
212d091a404SThomas Cort
213d091a404SThomas Cort /* overlapping ranges */
214d091a404SThomas Cort if (autostop && maxval > autostop)
215d091a404SThomas Cort maxval = autostop;
216d091a404SThomas Cort
217d091a404SThomas Cort /* set autostart */
218d091a404SThomas Cort if (autostart)
219d091a404SThomas Cort (void)memset(positions + 1, '1', autostart);
220d091a404SThomas Cort }
221d091a404SThomas Cort
222d091a404SThomas Cort static void
223d091a404SThomas Cort /*ARGSUSED*/
f_cut(FILE * fp,const char * fname __unused)224d091a404SThomas Cort f_cut(FILE *fp, const char *fname __unused)
225d091a404SThomas Cort {
226d091a404SThomas Cort int ch, field, isdelim;
227d091a404SThomas Cort char *pos, *p, sep;
228d091a404SThomas Cort int output;
229d091a404SThomas Cort size_t len;
230d091a404SThomas Cort char *lbuf, *tbuf;
231d091a404SThomas Cort
232d091a404SThomas Cort for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len)) != NULL;) {
233d091a404SThomas Cort output = 0;
234d091a404SThomas Cort if (lbuf[len - 1] != '\n') {
235d091a404SThomas Cort /* no newline at the end of the last line so add one */
236d091a404SThomas Cort if ((tbuf = (char *)malloc(len + 1)) == NULL)
237d091a404SThomas Cort err(1, NULL);
238d091a404SThomas Cort (void)memcpy(tbuf, lbuf, len);
239d091a404SThomas Cort tbuf[len++] = '\n';
240d091a404SThomas Cort lbuf = tbuf;
241d091a404SThomas Cort }
242d091a404SThomas Cort for (isdelim = 0, p = lbuf;; ++p) {
243d091a404SThomas Cort ch = *p;
244d091a404SThomas Cort /* this should work if newline is delimiter */
245d091a404SThomas Cort if (ch == sep)
246d091a404SThomas Cort isdelim = 1;
247d091a404SThomas Cort if (ch == '\n') {
248d091a404SThomas Cort if (!isdelim && !sflag)
249d091a404SThomas Cort (void)fwrite(lbuf, len, 1, stdout);
250d091a404SThomas Cort break;
251d091a404SThomas Cort }
252d091a404SThomas Cort }
253d091a404SThomas Cort if (!isdelim)
254d091a404SThomas Cort continue;
255d091a404SThomas Cort
256d091a404SThomas Cort pos = positions + 1;
257d091a404SThomas Cort for (field = maxval, p = lbuf; field; --field, ++pos) {
258d091a404SThomas Cort if (*pos) {
259d091a404SThomas Cort if (output++)
260d091a404SThomas Cort (void)putchar(sep);
261d091a404SThomas Cort while ((ch = *p++) != '\n' && ch != sep)
262d091a404SThomas Cort (void)putchar(ch);
263d091a404SThomas Cort } else {
264d091a404SThomas Cort while ((ch = *p++) != '\n' && ch != sep)
265d091a404SThomas Cort continue;
266d091a404SThomas Cort }
267d091a404SThomas Cort if (ch == '\n')
268d091a404SThomas Cort break;
269d091a404SThomas Cort }
270d091a404SThomas Cort if (ch != '\n') {
271d091a404SThomas Cort if (autostop) {
272d091a404SThomas Cort if (output)
273d091a404SThomas Cort (void)putchar(sep);
274d091a404SThomas Cort for (; (ch = *p) != '\n'; ++p)
275d091a404SThomas Cort (void)putchar(ch);
276d091a404SThomas Cort } else
277d091a404SThomas Cort for (; (ch = *p) != '\n'; ++p);
278d091a404SThomas Cort }
279d091a404SThomas Cort (void)putchar('\n');
280d091a404SThomas Cort if (tbuf) {
281d091a404SThomas Cort free(tbuf);
282d091a404SThomas Cort tbuf = NULL;
283d091a404SThomas Cort }
284d091a404SThomas Cort }
285d091a404SThomas Cort if (tbuf)
286d091a404SThomas Cort free(tbuf);
287d091a404SThomas Cort }
288d091a404SThomas Cort
289d091a404SThomas Cort static void
usage(void)290d091a404SThomas Cort usage(void)
291d091a404SThomas Cort {
292d091a404SThomas Cort (void)fprintf(stderr, "usage:\tcut -b list [-n] [file ...]\n"
293d091a404SThomas Cort "\tcut -c list [file ...]\n"
294d091a404SThomas Cort "\tcut -f list [-d string] [-s] [file ...]\n");
295d091a404SThomas Cort exit(1);
296d091a404SThomas Cort }
297d091a404SThomas Cort
298d091a404SThomas Cort /* make b_put(): */
299d091a404SThomas Cort #define CUT_BYTE 1
300d091a404SThomas Cort #include "x_cut.c"
301d091a404SThomas Cort #undef CUT_BYTE
302d091a404SThomas Cort
303d091a404SThomas Cort /* make c_put(): */
304d091a404SThomas Cort #define CUT_BYTE 0
305d091a404SThomas Cort #include "x_cut.c"
306d091a404SThomas Cort #undef CUT_BYTE
307