blob: 4907ed9354fdba9581f828112febab266b7d64bd [file] [log] [blame]
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001/* vi: set sw=4 ts=4: */
2/*
3 * cut implementation for busybox
4 *
5 * Copyright (c) Michael J. Holme
6 *
7 * This version of cut is adapted from Minix cut and was modified
8 * by Erik Andersen <andersee@debian.org> to be used in busybox.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Original copyright notice is retained at the end of this file.
25 */
26
27#include "internal.h"
28#include <sys/types.h>
29#include <ctype.h>
30#include <string.h>
31#include <errno.h>
32#include <stdlib.h>
33#include <stdio.h>
34#define BB_DECLARE_EXTERN
35#define bb_need_help
36#include "messages.c"
37
38#define MAX_FIELD 80 /* Pointers to the beginning of each field
39 * are stored in columns[], if a line holds
40 * more than MAX_FIELD columns the array
41 * boundary is exceed. But unlikely at 80 */
42
43#define MAX_ARGS 32 /* Maximum number of fields following -f or
44 * -c switches */
45int args[MAX_ARGS * 2];
46int num_args;
47
48/* Lots of new defines, should easen maintainance... */
49#define DUMP_STDIN 0 /* define for mode: no options */
50#define OPTIONF 1 /* define for mode: option -f */
51#define OPTIONC 2 /* define for mode: option -c */
52#define OPTIONB 3 /* define for mode: option -b */
53#define NOTSET 0 /* option not selected */
Eric Andersen1386e702000-06-26 12:14:30 +000054#define SET 1 /* option selected */
55#define OPTIONS 1 /*define option -s */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000056/* Defines for the warnings */
57#define DELIMITER_NOT_APPLICABLE 0
58#define OVERRIDING_PREVIOUS_MODE 1
59#define OPTION_NOT_APPLICABLE 2
60#define UNKNOWN_OPTION 3
61#define FILE_NOT_READABLE 4
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000062/* Defines for the fatal errors */
63#define SYNTAX_ERROR 101
64#define POSITION_ERROR 102
65#define LINE_TO_LONG_ERROR 103
66#define RANGE_ERROR 104
67#define MAX_FIELDS_EXEEDED_ERROR 105
68#define MAX_ARGS_EXEEDED_ERROR 106
69
70
71int mode; /* 0 = dump stdin to stdout, 1=-f, 2=-c */
72char delim = '\t'; /* default delimiting character */
73FILE *fd;
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000074char line[BUFSIZ];
75int exit_status;
Eric Andersen1386e702000-06-26 12:14:30 +000076int option = 0; /* for -s option */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000077
78int cut_main(int argc, char **argv);
79void warn(int warn_number, char *option);
80void cuterror(int err);
81void get_args(void);
82void cut(void);
83
84void warn(int warn_number, char *option)
85{
86 static char *warn_msg[] = {
Matt Kraaid537a952000-07-14 01:51:25 +000087 "Option -%s allowed only with -f\n",
88 "-%s overrides earlier option\n",
89 "-%s not allowed in current mode\n",
90 "Cannot open %s\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000091 };
92
Matt Kraaid537a952000-07-14 01:51:25 +000093 errorMsg(warn_msg[warn_number], option);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000094 exit_status = warn_number + 1;
95
96}
97
98void cuterror(int err)
99{
100 static char *err_mes[] = {
Matt Kraaid537a952000-07-14 01:51:25 +0000101 "syntax error\n",
102 "position must be >0\n",
103 "line longer than BUFSIZ\n",
104 "range must not decrease from left to right\n",
105 "MAX_FIELD exceeded\n",
106 "MAX_ARGS exceeded\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000107 };
108
Matt Kraaid537a952000-07-14 01:51:25 +0000109 errorMsg(err_mes[err - 101]);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000110 exit(err);
111}
112
113
114void get_args()
115{
116 int i = 0;
117 int arg_ptr = 0;
118 int flag;
119
120 num_args = 0;
121 do {
122 if (num_args == MAX_ARGS)
123 cuterror(MAX_ARGS_EXEEDED_ERROR);
124 if (!isdigit(line[i]) && line[i] != '-')
125 cuterror(SYNTAX_ERROR);
126
127 args[arg_ptr] = 1;
128 args[arg_ptr + 1] = BUFSIZ;
129 flag = 1;
130
131 while (line[i] != ',' && line[i] != 0) {
132 if (isdigit(line[i])) {
133 args[arg_ptr] = 0;
134 while (isdigit(line[i]))
135 args[arg_ptr] = 10 * args[arg_ptr] + line[i++] - '0';
136 if (!args[arg_ptr])
137 cuterror(POSITION_ERROR);
138 arg_ptr++;
139 }
140 if (line[i] == '-') {
141 arg_ptr |= 1;
142 i++;
143 flag = 0;
144 }
145 }
146 if (flag && arg_ptr & 1)
147 args[arg_ptr] = args[arg_ptr - 1];
148 if (args[num_args * 2] > args[num_args * 2 + 1])
149 cuterror(RANGE_ERROR);
150 num_args++;
151 arg_ptr = num_args * 2;
152 }
153 while (line[i++]);
154}
155
156
157void cut()
158{
159 int i, j, length, maxcol=0;
160 char *columns[MAX_FIELD];
161
162 while (fgets(line, BUFSIZ, fd)) {
Eric Andersen1386e702000-06-26 12:14:30 +0000163 maxcol=0;
164 length = strlen(line) - 1;
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000165 *(line + length) = 0;
166 switch (mode) {
167 case DUMP_STDIN:
168 printf("%s", line);
169 break;
170 case OPTIONF:
171 columns[maxcol++] = line;
172 for (i = 0; i < length; i++) {
173 if (*(line + i) == delim) {
174 *(line + i) = 0;
175 if (maxcol == MAX_FIELD)
176 cuterror(MAX_FIELDS_EXEEDED_ERROR);
177 columns[maxcol] = line + i + 1;
178 maxcol++;
179 }
180 }
181 if (maxcol != 1) {
182 for (i = 0; i < num_args; i++) {
183 for (j = args[i * 2]; j <= args[i * 2 + 1]; j++)
184 if (j <= maxcol) {
Eric Andersen1386e702000-06-26 12:14:30 +0000185
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000186 printf("%s", columns[j - 1]);
Eric Andersen1386e702000-06-26 12:14:30 +0000187
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000188 if (i != num_args - 1 || j != args[i * 2 + 1])
189 putchar(delim);
190 }
191 }
Eric Andersen1386e702000-06-26 12:14:30 +0000192 } else if (option != OPTIONS) {
193 printf("%s",line);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000194 }
195 break;
196 case OPTIONC:
197 for (i = 0; i < num_args; i++) {
198 for (j = args[i * 2];
199 j <= (args[i * 2 + 1] >
200 length ? length : args[i * 2 + 1]); j++)
201 putchar(*(line + j - 1));
202 }
203 }
204 if (maxcol != 1)
205 putchar('\n');
206 }
207}
208
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000209int cut_main(int argc, char **argv)
210{
211 int i = 1;
212 int numberFilenames = 0;
213
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000214 while (i < argc) {
215 if (argv[i][0] == '-') {
216 switch (argv[i++][1]) {
217 case 'd':
218 if (mode == OPTIONC || mode == OPTIONB)
219 warn(DELIMITER_NOT_APPLICABLE, "d");
Eric Andersen1386e702000-06-26 12:14:30 +0000220 if (argc > i)
221 delim = argv[i++][0];
222 else
223 cuterror(SYNTAX_ERROR);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000224 break;
225 case 'f':
226 sprintf(line, "%s", argv[i++]);
227 if (mode == OPTIONC || mode == OPTIONB)
228 warn(OVERRIDING_PREVIOUS_MODE, "f");
229 mode = OPTIONF;
230 break;
231 case 'b':
232 sprintf(line, "%s", argv[i++]);
233 if (mode == OPTIONF || mode == OPTIONC)
234 warn(OVERRIDING_PREVIOUS_MODE, "b");
235 mode = OPTIONB;
236 break;
237 case 'c':
238 sprintf(line, "%s", argv[i++]);
239 if (mode == OPTIONF || mode == OPTIONB)
240 warn(OVERRIDING_PREVIOUS_MODE, "c");
241 mode = OPTIONC;
242 break;
Eric Andersen1386e702000-06-26 12:14:30 +0000243 case 's':
244 option = OPTIONS;
245
246 break;
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000247 case '\0': /* - means: read from stdin */
248 numberFilenames++;
249 break;
250 case 'n': /* needed for Posix, but no effect here */
251 if (mode != OPTIONB)
252 warn(OPTION_NOT_APPLICABLE, "n");
253 break;
254 default:
255 warn(UNKNOWN_OPTION, &(argv[i - 1][1]));
256 }
257 } else {
258 i++;
259 numberFilenames++;
260 }
261 }
262
263/* Here follow the checks, if the selected options are reasonable. */
264 if (mode == OPTIONB) /* since in Minix char := byte */
265 mode = OPTIONC;
Eric Andersen1386e702000-06-26 12:14:30 +0000266
267 if (mode != OPTIONF && option == OPTIONS)
268 warn(DELIMITER_NOT_APPLICABLE,"s");
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000269 get_args();
270 if (numberFilenames != 0) {
271 i = 1;
272 while (i < argc) {
273 if (argv[i][0] == '-') {
274 switch (argv[i][1]) {
275 case 'f':
276 case 'c':
277 case 'b':
278 case 'd':
279 i += 2;
280 break;
281 case 'n':
282 case 'i':
283 case 's':
284 i++;
285 break;
286 case '\0':
287 fd = stdin;
288 i++;
289 cut();
290 break;
291 default:
292 i++;
293 }
294 } else {
295 if ((fd = fopen(argv[i++], "r")) == NULL) {
296 warn(FILE_NOT_READABLE, argv[i - 1]);
297 } else {
298 cut();
299 fclose(fd);
300 }
301 }
302 }
303 } else {
304 fd = stdin;
305 cut();
306 }
307
Eric Andersenb6106152000-06-19 17:25:40 +0000308 return(exit_status);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000309}
310
311/* cut - extract columns from a file or stdin. Author: Michael J. Holme
312 *
313 * Copyright 1989, Michael John Holme, All rights reserved.
314 * This code may be freely distributed, provided that this notice
315 * remains intact.
316 *
317 * V1.1: 6th September 1989
318 *
319 * Bugs, criticisms, etc,
320 * c/o Mark Powell
321 * JANET sq79@uk.ac.liv
322 * ARPA sq79%liv.ac.uk@nsfnet-relay.ac.uk
323 * UUCP ...!mcvax!ukc!liv.ac.uk!sq79
324 *-------------------------------------------------------------------------
325 * Changed for POSIX1003.2/Draft10 conformance
326 * Thomas Brupbacher (tobr@mw.lpc.ethz.ch), September 1990.
327 * Changes:
328 * - separation of error messages ( stderr) and output (stdout).
329 * - support for -b and -n (no effect, -b acts as -c)
330 * - support for -s
331 *-------------------------------------------------------------------------
332 */
333
334/*
335 * Copyright (c) 1987,1997, Prentice Hall
336 * All rights reserved.
337 *
338 * Redistribution and use of the MINIX operating system in source and
339 * binary forms, with or without modification, are permitted provided
340 * that the following conditions are met:
341 *
342 * Redistributions of source code must retain the above copyright
343 * notice, this list of conditions and the following disclaimer.
344 *
345 * Redistributions in binary form must reproduce the above
346 * copyright notice, this list of conditions and the following
347 * disclaimer in the documentation and/or other materials provided
348 * with the distribution.
349 *
350 * Neither the name of Prentice Hall nor the names of the software
351 * authors or contributors may be used to endorse or promote
352 * products derived from this software without specific prior
353 * written permission.
354 *
355 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
356 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
357 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
358 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
359 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
360 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
361 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
362 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
363 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
364 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
365 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
366 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367 *
368 */
369
370