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