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