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