xref: /freebsd/usr.bin/cut/cut.c (revision 953a3198a35204535cc9d450f04da982a4fea59b)
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; fgets(lbuf, sizeof(lbuf), fp);) {
222 		output = 0;
223 		for (isdelim = 0, p = lbuf;; ++p) {
224 			if (!(ch = *p))
225 				err("%s: line too long.\n", fname);
226 			/* this should work if newline is delimiter */
227 			if (ch == sep)
228 				isdelim = 1;
229 			if (ch == '\n') {
230 				if (!isdelim && !sflag)
231 					(void)printf("%s", lbuf);
232 				break;
233 			}
234 		}
235 		if (!isdelim)
236 			continue;
237 
238 		pos = positions + 1;
239 		for (field = maxval, p = lbuf; field; --field, ++pos) {
240 			if (*pos) {
241 				if (output++)
242 					(void)putchar(sep);
243 				while ((ch = *p++) != '\n' && ch != sep)
244 					(void)putchar(ch);
245 			} else
246 				while ((ch = *p++) != '\n' && ch != sep);
247 			if (ch == '\n')
248 				break;
249 		}
250 		if (ch != '\n')
251 			if (autostop) {
252 				if (output)
253 					(void)putchar(sep);
254 				for (; (ch = *p) != '\n'; ++p)
255 					(void)putchar(ch);
256 			} else
257 				for (; (ch = *p) != '\n'; ++p);
258 		(void)putchar('\n');
259 	}
260 }
261 
262 void
263 usage()
264 {
265 	(void)fprintf(stderr,
266 "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
267 	exit(1);
268 }
269 
270 #if __STDC__
271 #include <stdarg.h>
272 #else
273 #include <varargs.h>
274 #endif
275 
276 void
277 #if __STDC__
278 err(const char *fmt, ...)
279 #else
280 err(fmt, va_alist)
281 	char *fmt;
282         va_dcl
283 #endif
284 {
285 	va_list ap;
286 #if __STDC__
287 	va_start(ap, fmt);
288 #else
289 	va_start(ap);
290 #endif
291 	(void)fprintf(stderr, "cut: ");
292 	(void)vfprintf(stderr, fmt, ap);
293 	va_end(ap);
294 	(void)fprintf(stderr, "\n");
295 	exit(1);
296 	/* NOTREACHED */
297 }
298