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