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