cut.c revision 1.3 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.3 mycroft /*static char sccsid[] = "from: @(#)cut.c 5.4 (Berkeley) 10/30/90";*/
45 1.3 mycroft static char rcsid[] = "$Id: cut.c,v 1.3 1993/08/01 18:16:57 mycroft Exp $";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #include <limits.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <ctype.h>
51 1.1 cgd
52 1.1 cgd int cflag;
53 1.1 cgd char dchar;
54 1.1 cgd int dflag;
55 1.1 cgd int fflag;
56 1.1 cgd int sflag;
57 1.1 cgd
58 1.1 cgd main(argc, argv)
59 1.1 cgd int argc;
60 1.1 cgd char **argv;
61 1.1 cgd {
62 1.1 cgd extern char *optarg;
63 1.1 cgd extern int errno, optind;
64 1.1 cgd FILE *fp;
65 1.1 cgd int ch, (*fcn)(), c_cut(), f_cut();
66 1.1 cgd char *strerror();
67 1.1 cgd
68 1.1 cgd dchar = '\t'; /* default delimiter is \t */
69 1.1 cgd
70 1.1 cgd while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF)
71 1.1 cgd switch(ch) {
72 1.1 cgd case 'c':
73 1.1 cgd fcn = c_cut;
74 1.1 cgd get_list(optarg);
75 1.1 cgd cflag = 1;
76 1.1 cgd break;
77 1.1 cgd case 'd':
78 1.1 cgd dchar = *optarg;
79 1.1 cgd dflag = 1;
80 1.1 cgd break;
81 1.1 cgd case 'f':
82 1.1 cgd get_list(optarg);
83 1.1 cgd fcn = f_cut;
84 1.1 cgd fflag = 1;
85 1.1 cgd break;
86 1.1 cgd case 's':
87 1.1 cgd sflag = 1;
88 1.1 cgd break;
89 1.1 cgd case '?':
90 1.1 cgd default:
91 1.1 cgd usage();
92 1.1 cgd }
93 1.1 cgd argc -= optind;
94 1.1 cgd argv += optind;
95 1.1 cgd
96 1.1 cgd if (fflag) {
97 1.1 cgd if (cflag)
98 1.1 cgd usage();
99 1.1 cgd } else if (!cflag || dflag || sflag)
100 1.1 cgd usage();
101 1.1 cgd
102 1.1 cgd if (*argv)
103 1.1 cgd for (; *argv; ++argv) {
104 1.1 cgd if (!(fp = fopen(*argv, "r"))) {
105 1.1 cgd (void)fprintf(stderr,
106 1.1 cgd "cut: %s: %s\n", *argv, strerror(errno));
107 1.1 cgd exit(1);
108 1.1 cgd }
109 1.1 cgd fcn(fp, *argv);
110 1.1 cgd }
111 1.1 cgd else
112 1.1 cgd fcn(stdin, "stdin");
113 1.1 cgd exit(0);
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd int autostart, autostop, maxval;
117 1.1 cgd
118 1.1 cgd char positions[_POSIX2_LINE_MAX + 1];
119 1.1 cgd
120 1.1 cgd get_list(list)
121 1.1 cgd char *list;
122 1.1 cgd {
123 1.1 cgd register char *pos;
124 1.1 cgd register int setautostart, start, stop;
125 1.1 cgd char *p, *strtok();
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * set a byte in the positions array to indicate if a field or
129 1.1 cgd * column is to be selected; use +1, it's 1-based, not 0-based.
130 1.1 cgd * This parser is less restrictive than the Draft 9 POSIX spec.
131 1.1 cgd * POSIX doesn't allow lists that aren't in increasing order or
132 1.1 cgd * overlapping lists. We also handle "-3-5" although there's no
133 1.1 cgd * real reason too.
134 1.1 cgd */
135 1.1 cgd for (; p = strtok(list, ", \t"); list = NULL) {
136 1.1 cgd setautostart = start = stop = 0;
137 1.1 cgd if (*p == '-') {
138 1.1 cgd ++p;
139 1.1 cgd setautostart = 1;
140 1.1 cgd }
141 1.1 cgd if (isdigit(*p)) {
142 1.1 cgd start = stop = strtol(p, &p, 10);
143 1.1 cgd if (setautostart && start > autostart)
144 1.1 cgd autostart = start;
145 1.1 cgd }
146 1.1 cgd if (*p == '-') {
147 1.1 cgd if (isdigit(p[1]))
148 1.1 cgd stop = strtol(p + 1, &p, 10);
149 1.1 cgd if (*p == '-') {
150 1.1 cgd ++p;
151 1.1 cgd if (!autostop || autostop > stop)
152 1.1 cgd autostop = stop;
153 1.1 cgd }
154 1.1 cgd }
155 1.1 cgd if (*p)
156 1.1 cgd badlist("illegal list value");
157 1.1 cgd if (!stop || !start)
158 1.1 cgd badlist("values may not include zero");
159 1.1 cgd if (stop > _POSIX2_LINE_MAX) {
160 1.1 cgd /* positions used rather than allocate a new buffer */
161 1.1 cgd (void)sprintf(positions, "%d too large (max %d)",
162 1.1 cgd stop, _POSIX2_LINE_MAX);
163 1.1 cgd badlist(positions);
164 1.1 cgd }
165 1.1 cgd if (maxval < stop)
166 1.1 cgd maxval = stop;
167 1.1 cgd for (pos = positions + start; start++ <= stop; *pos++ = 1);
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd /* overlapping ranges */
171 1.1 cgd if (autostop && maxval > autostop)
172 1.1 cgd maxval = autostop;
173 1.1 cgd
174 1.1 cgd /* set autostart */
175 1.1 cgd if (autostart)
176 1.1 cgd memset(positions + 1, '1', autostart);
177 1.1 cgd }
178 1.1 cgd
179 1.1 cgd /* ARGSUSED */
180 1.1 cgd c_cut(fp, fname)
181 1.1 cgd FILE *fp;
182 1.1 cgd char *fname;
183 1.1 cgd {
184 1.1 cgd register int ch, col;
185 1.1 cgd register char *pos;
186 1.1 cgd
187 1.1 cgd for (;;) {
188 1.1 cgd pos = positions + 1;
189 1.1 cgd for (col = maxval; col; --col) {
190 1.1 cgd if ((ch = getc(fp)) == EOF)
191 1.1 cgd return;
192 1.1 cgd if (ch == '\n')
193 1.1 cgd break;
194 1.1 cgd if (*pos++)
195 1.1 cgd putchar(ch);
196 1.1 cgd }
197 1.1 cgd if (ch != '\n')
198 1.1 cgd if (autostop)
199 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n')
200 1.1 cgd putchar(ch);
201 1.1 cgd else
202 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n');
203 1.1 cgd putchar('\n');
204 1.1 cgd }
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd f_cut(fp, fname)
208 1.1 cgd FILE *fp;
209 1.1 cgd char *fname;
210 1.1 cgd {
211 1.1 cgd register int ch, field, isdelim;
212 1.1 cgd register char *pos, *p, sep;
213 1.1 cgd int output;
214 1.1 cgd char lbuf[_POSIX2_LINE_MAX + 1];
215 1.1 cgd
216 1.2 cgd for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp); output = 0) {
217 1.1 cgd for (isdelim = 0, p = lbuf;; ++p) {
218 1.1 cgd if (!(ch = *p)) {
219 1.1 cgd (void)fprintf(stderr,
220 1.1 cgd "cut: %s: line too long.\n", fname);
221 1.1 cgd exit(1);
222 1.1 cgd }
223 1.1 cgd /* this should work if newline is delimiter */
224 1.1 cgd if (ch == sep)
225 1.1 cgd isdelim = 1;
226 1.1 cgd if (ch == '\n') {
227 1.1 cgd if (!isdelim && !sflag)
228 1.1 cgd (void)printf("%s", lbuf);
229 1.1 cgd break;
230 1.1 cgd }
231 1.1 cgd }
232 1.1 cgd if (!isdelim)
233 1.1 cgd continue;
234 1.1 cgd
235 1.1 cgd pos = positions + 1;
236 1.1 cgd for (field = maxval, p = lbuf; field; --field, ++pos) {
237 1.1 cgd if (*pos) {
238 1.1 cgd if (output++)
239 1.1 cgd putchar(sep);
240 1.1 cgd while ((ch = *p++) != '\n' && ch != sep)
241 1.1 cgd putchar(ch);
242 1.1 cgd } else
243 1.1 cgd while ((ch = *p++) != '\n' && ch != sep);
244 1.1 cgd if (ch == '\n')
245 1.1 cgd break;
246 1.1 cgd }
247 1.1 cgd if (ch != '\n')
248 1.1 cgd if (autostop) {
249 1.1 cgd if (output)
250 1.1 cgd putchar(sep);
251 1.1 cgd for (; (ch = *p) != '\n'; ++p)
252 1.1 cgd putchar(ch);
253 1.1 cgd } else
254 1.1 cgd for (; (ch = *p) != '\n'; ++p);
255 1.1 cgd putchar('\n');
256 1.1 cgd }
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd badlist(msg)
260 1.1 cgd char *msg;
261 1.1 cgd {
262 1.1 cgd (void)fprintf(stderr, "cut: [-cf] list: %s.\n", msg);
263 1.1 cgd exit(1);
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd usage()
267 1.1 cgd {
268 1.1 cgd (void)fprintf(stderr,
269 1.1 cgd "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
270 1.1 cgd exit(1);
271 1.1 cgd }
272