cut.c revision 1.1 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.1 cgd static char sccsid[] = "@(#)cut.c 5.4 (Berkeley) 10/30/90";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #include <limits.h>
48 1.1 cgd #include <stdio.h>
49 1.1 cgd #include <ctype.h>
50 1.1 cgd
51 1.1 cgd int cflag;
52 1.1 cgd char dchar;
53 1.1 cgd int dflag;
54 1.1 cgd int fflag;
55 1.1 cgd int sflag;
56 1.1 cgd
57 1.1 cgd main(argc, argv)
58 1.1 cgd int argc;
59 1.1 cgd char **argv;
60 1.1 cgd {
61 1.1 cgd extern char *optarg;
62 1.1 cgd extern int errno, optind;
63 1.1 cgd FILE *fp;
64 1.1 cgd int ch, (*fcn)(), c_cut(), f_cut();
65 1.1 cgd char *strerror();
66 1.1 cgd
67 1.1 cgd dchar = '\t'; /* default delimiter is \t */
68 1.1 cgd
69 1.1 cgd while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF)
70 1.1 cgd switch(ch) {
71 1.1 cgd case 'c':
72 1.1 cgd fcn = c_cut;
73 1.1 cgd get_list(optarg);
74 1.1 cgd cflag = 1;
75 1.1 cgd break;
76 1.1 cgd case 'd':
77 1.1 cgd dchar = *optarg;
78 1.1 cgd dflag = 1;
79 1.1 cgd break;
80 1.1 cgd case 'f':
81 1.1 cgd get_list(optarg);
82 1.1 cgd fcn = f_cut;
83 1.1 cgd fflag = 1;
84 1.1 cgd break;
85 1.1 cgd case 's':
86 1.1 cgd sflag = 1;
87 1.1 cgd break;
88 1.1 cgd case '?':
89 1.1 cgd default:
90 1.1 cgd usage();
91 1.1 cgd }
92 1.1 cgd argc -= optind;
93 1.1 cgd argv += optind;
94 1.1 cgd
95 1.1 cgd if (fflag) {
96 1.1 cgd if (cflag)
97 1.1 cgd usage();
98 1.1 cgd } else if (!cflag || dflag || sflag)
99 1.1 cgd usage();
100 1.1 cgd
101 1.1 cgd if (*argv)
102 1.1 cgd for (; *argv; ++argv) {
103 1.1 cgd if (!(fp = fopen(*argv, "r"))) {
104 1.1 cgd (void)fprintf(stderr,
105 1.1 cgd "cut: %s: %s\n", *argv, strerror(errno));
106 1.1 cgd exit(1);
107 1.1 cgd }
108 1.1 cgd fcn(fp, *argv);
109 1.1 cgd }
110 1.1 cgd else
111 1.1 cgd fcn(stdin, "stdin");
112 1.1 cgd exit(0);
113 1.1 cgd }
114 1.1 cgd
115 1.1 cgd int autostart, autostop, maxval;
116 1.1 cgd
117 1.1 cgd char positions[_POSIX2_LINE_MAX + 1];
118 1.1 cgd
119 1.1 cgd get_list(list)
120 1.1 cgd char *list;
121 1.1 cgd {
122 1.1 cgd register char *pos;
123 1.1 cgd register int setautostart, start, stop;
124 1.1 cgd char *p, *strtok();
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * set a byte in the positions array to indicate if a field or
128 1.1 cgd * column is to be selected; use +1, it's 1-based, not 0-based.
129 1.1 cgd * This parser is less restrictive than the Draft 9 POSIX spec.
130 1.1 cgd * POSIX doesn't allow lists that aren't in increasing order or
131 1.1 cgd * overlapping lists. We also handle "-3-5" although there's no
132 1.1 cgd * real reason too.
133 1.1 cgd */
134 1.1 cgd for (; p = strtok(list, ", \t"); list = NULL) {
135 1.1 cgd setautostart = start = stop = 0;
136 1.1 cgd if (*p == '-') {
137 1.1 cgd ++p;
138 1.1 cgd setautostart = 1;
139 1.1 cgd }
140 1.1 cgd if (isdigit(*p)) {
141 1.1 cgd start = stop = strtol(p, &p, 10);
142 1.1 cgd if (setautostart && start > autostart)
143 1.1 cgd autostart = start;
144 1.1 cgd }
145 1.1 cgd if (*p == '-') {
146 1.1 cgd if (isdigit(p[1]))
147 1.1 cgd stop = strtol(p + 1, &p, 10);
148 1.1 cgd if (*p == '-') {
149 1.1 cgd ++p;
150 1.1 cgd if (!autostop || autostop > stop)
151 1.1 cgd autostop = stop;
152 1.1 cgd }
153 1.1 cgd }
154 1.1 cgd if (*p)
155 1.1 cgd badlist("illegal list value");
156 1.1 cgd if (!stop || !start)
157 1.1 cgd badlist("values may not include zero");
158 1.1 cgd if (stop > _POSIX2_LINE_MAX) {
159 1.1 cgd /* positions used rather than allocate a new buffer */
160 1.1 cgd (void)sprintf(positions, "%d too large (max %d)",
161 1.1 cgd stop, _POSIX2_LINE_MAX);
162 1.1 cgd badlist(positions);
163 1.1 cgd }
164 1.1 cgd if (maxval < stop)
165 1.1 cgd maxval = stop;
166 1.1 cgd for (pos = positions + start; start++ <= stop; *pos++ = 1);
167 1.1 cgd }
168 1.1 cgd
169 1.1 cgd /* overlapping ranges */
170 1.1 cgd if (autostop && maxval > autostop)
171 1.1 cgd maxval = autostop;
172 1.1 cgd
173 1.1 cgd /* set autostart */
174 1.1 cgd if (autostart)
175 1.1 cgd memset(positions + 1, '1', autostart);
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd /* ARGSUSED */
179 1.1 cgd c_cut(fp, fname)
180 1.1 cgd FILE *fp;
181 1.1 cgd char *fname;
182 1.1 cgd {
183 1.1 cgd register int ch, col;
184 1.1 cgd register char *pos;
185 1.1 cgd
186 1.1 cgd for (;;) {
187 1.1 cgd pos = positions + 1;
188 1.1 cgd for (col = maxval; col; --col) {
189 1.1 cgd if ((ch = getc(fp)) == EOF)
190 1.1 cgd return;
191 1.1 cgd if (ch == '\n')
192 1.1 cgd break;
193 1.1 cgd if (*pos++)
194 1.1 cgd putchar(ch);
195 1.1 cgd }
196 1.1 cgd if (ch != '\n')
197 1.1 cgd if (autostop)
198 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n')
199 1.1 cgd putchar(ch);
200 1.1 cgd else
201 1.1 cgd while ((ch = getc(fp)) != EOF && ch != '\n');
202 1.1 cgd putchar('\n');
203 1.1 cgd }
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd f_cut(fp, fname)
207 1.1 cgd FILE *fp;
208 1.1 cgd char *fname;
209 1.1 cgd {
210 1.1 cgd register int ch, field, isdelim;
211 1.1 cgd register char *pos, *p, sep;
212 1.1 cgd int output;
213 1.1 cgd char lbuf[_POSIX2_LINE_MAX + 1];
214 1.1 cgd
215 1.1 cgd for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp);) {
216 1.1 cgd for (isdelim = 0, p = lbuf;; ++p) {
217 1.1 cgd if (!(ch = *p)) {
218 1.1 cgd (void)fprintf(stderr,
219 1.1 cgd "cut: %s: line too long.\n", fname);
220 1.1 cgd exit(1);
221 1.1 cgd }
222 1.1 cgd /* this should work if newline is delimiter */
223 1.1 cgd if (ch == sep)
224 1.1 cgd isdelim = 1;
225 1.1 cgd if (ch == '\n') {
226 1.1 cgd if (!isdelim && !sflag)
227 1.1 cgd (void)printf("%s", lbuf);
228 1.1 cgd break;
229 1.1 cgd }
230 1.1 cgd }
231 1.1 cgd if (!isdelim)
232 1.1 cgd continue;
233 1.1 cgd
234 1.1 cgd pos = positions + 1;
235 1.1 cgd for (field = maxval, p = lbuf; field; --field, ++pos) {
236 1.1 cgd if (*pos) {
237 1.1 cgd if (output++)
238 1.1 cgd putchar(sep);
239 1.1 cgd while ((ch = *p++) != '\n' && ch != sep)
240 1.1 cgd putchar(ch);
241 1.1 cgd } else
242 1.1 cgd while ((ch = *p++) != '\n' && ch != sep);
243 1.1 cgd if (ch == '\n')
244 1.1 cgd break;
245 1.1 cgd }
246 1.1 cgd if (ch != '\n')
247 1.1 cgd if (autostop) {
248 1.1 cgd if (output)
249 1.1 cgd putchar(sep);
250 1.1 cgd for (; (ch = *p) != '\n'; ++p)
251 1.1 cgd putchar(ch);
252 1.1 cgd } else
253 1.1 cgd for (; (ch = *p) != '\n'; ++p);
254 1.1 cgd putchar('\n');
255 1.1 cgd }
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd badlist(msg)
259 1.1 cgd char *msg;
260 1.1 cgd {
261 1.1 cgd (void)fprintf(stderr, "cut: [-cf] list: %s.\n", msg);
262 1.1 cgd exit(1);
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd usage()
266 1.1 cgd {
267 1.1 cgd (void)fprintf(stderr,
268 1.1 cgd "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
269 1.1 cgd exit(1);
270 1.1 cgd }
271