cut.c revision 1.20 1 1.20 christos /* $NetBSD: cut.c,v 1.20 2006/04/25 19:34:42 christos 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.11 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
38 1.11 lukem The Regents of the University of California. All rights reserved.\n");
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.20 christos __RCSID("$NetBSD: cut.c,v 1.20 2006/04/25 19:34:42 christos 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.1 cgd
58 1.1 cgd int cflag;
59 1.1 cgd char dchar;
60 1.1 cgd int dflag;
61 1.1 cgd int fflag;
62 1.1 cgd int sflag;
63 1.1 cgd
64 1.17 xtraeme void c_cut(FILE *, const char *);
65 1.17 xtraeme void f_cut(FILE *, const char *);
66 1.17 xtraeme void get_list(char *);
67 1.17 xtraeme void usage(void);
68 1.8 glass
69 1.5 jtc int
70 1.17 xtraeme main(int argc, char *argv[])
71 1.1 cgd {
72 1.1 cgd FILE *fp;
73 1.17 xtraeme void (*fcn)(FILE *, const char *);
74 1.8 glass int ch;
75 1.6 jtc
76 1.11 lukem fcn = NULL;
77 1.6 jtc setlocale (LC_ALL, "");
78 1.1 cgd
79 1.1 cgd dchar = '\t'; /* default delimiter is \t */
80 1.1 cgd
81 1.4 jtc /* Since we don't support multi-byte characters, the -c and -b
82 1.4 jtc options are equivalent, and the -n option is meaningless. */
83 1.10 mrg while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
84 1.1 cgd switch(ch) {
85 1.4 jtc case 'b':
86 1.1 cgd case 'c':
87 1.1 cgd fcn = c_cut;
88 1.1 cgd get_list(optarg);
89 1.1 cgd cflag = 1;
90 1.1 cgd break;
91 1.1 cgd case 'd':
92 1.1 cgd dchar = *optarg;
93 1.1 cgd dflag = 1;
94 1.1 cgd break;
95 1.1 cgd case 'f':
96 1.1 cgd get_list(optarg);
97 1.1 cgd fcn = f_cut;
98 1.1 cgd fflag = 1;
99 1.1 cgd break;
100 1.1 cgd case 's':
101 1.1 cgd sflag = 1;
102 1.4 jtc break;
103 1.4 jtc case 'n':
104 1.1 cgd break;
105 1.1 cgd case '?':
106 1.1 cgd default:
107 1.1 cgd usage();
108 1.1 cgd }
109 1.1 cgd argc -= optind;
110 1.1 cgd argv += optind;
111 1.1 cgd
112 1.1 cgd if (fflag) {
113 1.1 cgd if (cflag)
114 1.1 cgd usage();
115 1.1 cgd } else if (!cflag || dflag || sflag)
116 1.1 cgd usage();
117 1.1 cgd
118 1.1 cgd if (*argv)
119 1.1 cgd for (; *argv; ++argv) {
120 1.8 glass if (!(fp = fopen(*argv, "r")))
121 1.6 jtc err(1, "%s", *argv);
122 1.1 cgd fcn(fp, *argv);
123 1.8 glass (void)fclose(fp);
124 1.1 cgd }
125 1.1 cgd else
126 1.1 cgd fcn(stdin, "stdin");
127 1.1 cgd exit(0);
128 1.1 cgd }
129 1.1 cgd
130 1.1 cgd int autostart, autostop, maxval;
131 1.1 cgd
132 1.1 cgd char positions[_POSIX2_LINE_MAX + 1];
133 1.1 cgd
134 1.8 glass void
135 1.17 xtraeme get_list(char *list)
136 1.1 cgd {
137 1.11 lukem int setautostart, start, stop;
138 1.11 lukem char *pos;
139 1.6 jtc char *p;
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * set a byte in the positions array to indicate if a field or
143 1.1 cgd * column is to be selected; use +1, it's 1-based, not 0-based.
144 1.1 cgd * This parser is less restrictive than the Draft 9 POSIX spec.
145 1.1 cgd * POSIX doesn't allow lists that aren't in increasing order or
146 1.1 cgd * overlapping lists. We also handle "-3-5" although there's no
147 1.1 cgd * real reason too.
148 1.1 cgd */
149 1.11 lukem for (; (p = strtok(list, ", \t")) != NULL; list = NULL) {
150 1.1 cgd setautostart = start = stop = 0;
151 1.1 cgd if (*p == '-') {
152 1.1 cgd ++p;
153 1.1 cgd setautostart = 1;
154 1.1 cgd }
155 1.13 christos if (isdigit((unsigned char)*p)) {
156 1.1 cgd start = stop = strtol(p, &p, 10);
157 1.1 cgd if (setautostart && start > autostart)
158 1.1 cgd autostart = start;
159 1.1 cgd }
160 1.1 cgd if (*p == '-') {
161 1.13 christos if (isdigit((unsigned char)p[1]))
162 1.1 cgd stop = strtol(p + 1, &p, 10);
163 1.1 cgd if (*p == '-') {
164 1.1 cgd ++p;
165 1.1 cgd if (!autostop || autostop > stop)
166 1.1 cgd autostop = stop;
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd if (*p)
170 1.15 itojun errx(1, "[-cf] list: illegal list value");
171 1.1 cgd if (!stop || !start)
172 1.15 itojun errx(1, "[-cf] list: values may not include zero");
173 1.8 glass if (stop > _POSIX2_LINE_MAX)
174 1.15 itojun errx(1, "[-cf] list: %d too large (max %d)",
175 1.1 cgd stop, _POSIX2_LINE_MAX);
176 1.1 cgd if (maxval < stop)
177 1.1 cgd maxval = stop;
178 1.1 cgd for (pos = positions + start; start++ <= stop; *pos++ = 1);
179 1.1 cgd }
180 1.1 cgd
181 1.1 cgd /* overlapping ranges */
182 1.1 cgd if (autostop && maxval > autostop)
183 1.1 cgd maxval = autostop;
184 1.1 cgd
185 1.1 cgd /* set autostart */
186 1.1 cgd if (autostart)
187 1.1 cgd memset(positions + 1, '1', autostart);
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /* ARGSUSED */
191 1.8 glass void
192 1.17 xtraeme c_cut(FILE *fp, const char *fname)
193 1.1 cgd {
194 1.11 lukem int ch, col;
195 1.11 lukem char *pos;
196 1.1 cgd
197 1.11 lukem ch = 0;
198 1.1 cgd for (;;) {
199 1.1 cgd pos = positions + 1;
200 1.1 cgd for (col = maxval; col; --col) {
201 1.1 cgd if ((ch = getc(fp)) == EOF)
202 1.1 cgd return;
203 1.1 cgd if (ch == '\n')
204 1.1 cgd break;
205 1.1 cgd if (*pos++)
206 1.8 glass (void)putchar(ch);
207 1.1 cgd }
208 1.12 ross if (ch != '\n') {
209 1.1 cgd if (autostop)
210 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n')
211 1.8 glass (void)putchar(ch);
212 1.1 cgd else
213 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n');
214 1.12 ross }
215 1.8 glass (void)putchar('\n');
216 1.1 cgd }
217 1.1 cgd }
218 1.1 cgd
219 1.8 glass void
220 1.17 xtraeme f_cut(FILE *fp, const char *fname)
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.18 yamt for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len));) {
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.18 yamt 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.8 glass void
286 1.17 xtraeme usage(void)
287 1.1 cgd {
288 1.14 wiz (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