cut.c revision 1.25 1 1.25 lukem /* $NetBSD: cut.c,v 1.25 2008/07/21 14:19:22 lukem 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.25 lukem __RCSID("$NetBSD: cut.c,v 1.25 2008/07/21 14:19:22 lukem 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.8 glass int ch;
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.4 jtc /* 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.1 cgd if (*argv)
130 1.1 cgd for (; *argv; ++argv) {
131 1.21 jnemeth if (strcmp(*argv, "-") == 0)
132 1.21 jnemeth fcn(stdin, "stdin");
133 1.21 jnemeth else {
134 1.21 jnemeth if ((fp = fopen(*argv, "r")) == NULL)
135 1.21 jnemeth err(1, "%s", *argv);
136 1.21 jnemeth fcn(fp, *argv);
137 1.21 jnemeth (void)fclose(fp);
138 1.21 jnemeth }
139 1.1 cgd }
140 1.1 cgd else
141 1.1 cgd fcn(stdin, "stdin");
142 1.23 christos return 0;
143 1.1 cgd }
144 1.1 cgd
145 1.23 christos static size_t autostart, autostop, maxval;
146 1.1 cgd
147 1.23 christos static char *positions = NULL;
148 1.23 christos static size_t numpositions = 0;
149 1.23 christos #define ALLOC_CHUNK _POSIX2_LINE_MAX /* malloc granularity */
150 1.1 cgd
151 1.23 christos static void
152 1.17 xtraeme get_list(char *list)
153 1.1 cgd {
154 1.23 christos size_t setautostart, start, stop;
155 1.11 lukem char *pos;
156 1.6 jtc char *p;
157 1.1 cgd
158 1.23 christos if (positions == NULL) {
159 1.23 christos numpositions = ALLOC_CHUNK;
160 1.23 christos positions = ecalloc(numpositions, sizeof(*positions));
161 1.23 christos }
162 1.23 christos
163 1.1 cgd /*
164 1.1 cgd * set a byte in the positions array to indicate if a field or
165 1.1 cgd * column is to be selected; use +1, it's 1-based, not 0-based.
166 1.1 cgd * This parser is less restrictive than the Draft 9 POSIX spec.
167 1.1 cgd * POSIX doesn't allow lists that aren't in increasing order or
168 1.1 cgd * overlapping lists. We also handle "-3-5" although there's no
169 1.1 cgd * real reason too.
170 1.1 cgd */
171 1.11 lukem for (; (p = strtok(list, ", \t")) != NULL; list = NULL) {
172 1.1 cgd setautostart = start = stop = 0;
173 1.1 cgd if (*p == '-') {
174 1.1 cgd ++p;
175 1.1 cgd setautostart = 1;
176 1.1 cgd }
177 1.13 christos if (isdigit((unsigned char)*p)) {
178 1.1 cgd start = stop = strtol(p, &p, 10);
179 1.1 cgd if (setautostart && start > autostart)
180 1.1 cgd autostart = start;
181 1.1 cgd }
182 1.1 cgd if (*p == '-') {
183 1.13 christos if (isdigit((unsigned char)p[1]))
184 1.1 cgd stop = strtol(p + 1, &p, 10);
185 1.1 cgd if (*p == '-') {
186 1.1 cgd ++p;
187 1.1 cgd if (!autostop || autostop > stop)
188 1.1 cgd autostop = stop;
189 1.1 cgd }
190 1.1 cgd }
191 1.1 cgd if (*p)
192 1.15 itojun errx(1, "[-cf] list: illegal list value");
193 1.1 cgd if (!stop || !start)
194 1.15 itojun errx(1, "[-cf] list: values may not include zero");
195 1.23 christos if (stop + 1 > numpositions) {
196 1.23 christos size_t newsize;
197 1.23 christos newsize = roundup(stop + 1, ALLOC_CHUNK);
198 1.23 christos positions = erealloc(positions, newsize);
199 1.23 christos (void)memset(positions + numpositions, 0,
200 1.23 christos newsize - numpositions);
201 1.23 christos numpositions = newsize;
202 1.23 christos }
203 1.1 cgd if (maxval < stop)
204 1.1 cgd maxval = stop;
205 1.23 christos for (pos = positions + start; start++ <= stop; pos++)
206 1.23 christos *pos = 1;
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /* overlapping ranges */
210 1.1 cgd if (autostop && maxval > autostop)
211 1.1 cgd maxval = autostop;
212 1.1 cgd
213 1.1 cgd /* set autostart */
214 1.1 cgd if (autostart)
215 1.23 christos (void)memset(positions + 1, '1', autostart);
216 1.1 cgd }
217 1.1 cgd
218 1.23 christos static void
219 1.23 christos /*ARGSUSED*/
220 1.23 christos f_cut(FILE *fp, const char *fname __unused)
221 1.1 cgd {
222 1.11 lukem int ch, field, isdelim;
223 1.11 lukem char *pos, *p, sep;
224 1.1 cgd int output;
225 1.18 yamt size_t len;
226 1.18 yamt char *lbuf, *tbuf;
227 1.1 cgd
228 1.23 christos for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len)) != NULL;) {
229 1.7 mycroft output = 0;
230 1.18 yamt if (lbuf[len - 1] != '\n') {
231 1.18 yamt /* no newline at the end of the last line so add one */
232 1.18 yamt if ((tbuf = (char *)malloc(len + 1)) == NULL)
233 1.18 yamt err(1, NULL);
234 1.23 christos (void)memcpy(tbuf, lbuf, len);
235 1.19 dsl tbuf[len++] = '\n';
236 1.18 yamt lbuf = tbuf;
237 1.18 yamt }
238 1.1 cgd for (isdelim = 0, p = lbuf;; ++p) {
239 1.18 yamt ch = *p;
240 1.1 cgd /* this should work if newline is delimiter */
241 1.1 cgd if (ch == sep)
242 1.1 cgd isdelim = 1;
243 1.1 cgd if (ch == '\n') {
244 1.1 cgd if (!isdelim && !sflag)
245 1.18 yamt (void)fwrite(lbuf, len, 1, stdout);
246 1.1 cgd break;
247 1.1 cgd }
248 1.1 cgd }
249 1.1 cgd if (!isdelim)
250 1.1 cgd continue;
251 1.1 cgd
252 1.1 cgd pos = positions + 1;
253 1.1 cgd for (field = maxval, p = lbuf; field; --field, ++pos) {
254 1.1 cgd if (*pos) {
255 1.1 cgd if (output++)
256 1.8 glass (void)putchar(sep);
257 1.1 cgd while ((ch = *p++) != '\n' && ch != sep)
258 1.8 glass (void)putchar(ch);
259 1.12 ross } else {
260 1.12 ross while ((ch = *p++) != '\n' && ch != sep)
261 1.12 ross continue;
262 1.12 ross }
263 1.1 cgd if (ch == '\n')
264 1.1 cgd break;
265 1.1 cgd }
266 1.12 ross if (ch != '\n') {
267 1.1 cgd if (autostop) {
268 1.1 cgd if (output)
269 1.8 glass (void)putchar(sep);
270 1.1 cgd for (; (ch = *p) != '\n'; ++p)
271 1.8 glass (void)putchar(ch);
272 1.1 cgd } else
273 1.1 cgd for (; (ch = *p) != '\n'; ++p);
274 1.12 ross }
275 1.8 glass (void)putchar('\n');
276 1.19 dsl if (tbuf) {
277 1.19 dsl free(tbuf);
278 1.19 dsl tbuf = NULL;
279 1.19 dsl }
280 1.1 cgd }
281 1.20 christos if (tbuf)
282 1.20 christos free(tbuf);
283 1.1 cgd }
284 1.1 cgd
285 1.23 christos static void
286 1.17 xtraeme usage(void)
287 1.1 cgd {
288 1.23 christos (void)fprintf(stderr, "Usage:\tcut -b list [-n] [file ...]\n"
289 1.14 wiz "\tcut -c list [file1 ...]\n"
290 1.14 wiz "\tcut -f list [-d delim] [-s] [file ...]\n");
291 1.1 cgd exit(1);
292 1.1 cgd }
293 1.22 hubertf
294 1.22 hubertf /* make b_put(): */
295 1.22 hubertf #define CUT_BYTE 1
296 1.22 hubertf #include "x_cut.c"
297 1.22 hubertf #undef CUT_BYTE
298 1.22 hubertf
299 1.22 hubertf /* make c_put(): */
300 1.22 hubertf #define CUT_BYTE 0
301 1.22 hubertf #include "x_cut.c"
302 1.22 hubertf #undef CUT_BYTE
303