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