Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 1 | /* 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 */ |
| 45 | int args[MAX_ARGS * 2]; |
| 46 | int 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 Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 54 | #define SET 1 /* option selected */ |
| 55 | #define OPTIONS 1 /*define option -s */ |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 56 | /* 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 Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 62 | /* 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 | |
| 71 | int mode; /* 0 = dump stdin to stdout, 1=-f, 2=-c */ |
| 72 | char delim = '\t'; /* default delimiting character */ |
| 73 | FILE *fd; |
| 74 | char *name; |
| 75 | char line[BUFSIZ]; |
| 76 | int exit_status; |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 77 | int option = 0; /* for -s option */ |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 78 | |
| 79 | int cut_main(int argc, char **argv); |
| 80 | void warn(int warn_number, char *option); |
| 81 | void cuterror(int err); |
| 82 | void get_args(void); |
| 83 | void cut(void); |
| 84 | |
| 85 | void warn(int warn_number, char *option) |
| 86 | { |
| 87 | static char *warn_msg[] = { |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 88 | "%s: Option -%s allowed only with -f\n", |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 89 | "%s: -%s overrides earlier option\n", |
| 90 | "%s: -%s not allowed in current mode\n", |
| 91 | "%s: Cannot open %s\n" |
| 92 | }; |
| 93 | |
| 94 | fprintf(stderr, warn_msg[warn_number], name, option); |
| 95 | exit_status = warn_number + 1; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | void cuterror(int err) |
| 100 | { |
| 101 | static char *err_mes[] = { |
| 102 | "%s: syntax error\n", |
| 103 | "%s: position must be >0\n", |
| 104 | "%s: line longer than BUFSIZ\n", |
| 105 | "%s: range must not decrease from left to right\n", |
| 106 | "%s: MAX_FIELD exceeded\n", |
| 107 | "%s: MAX_ARGS exceeded\n" |
| 108 | }; |
| 109 | |
| 110 | fprintf(stderr, err_mes[err - 101], name); |
| 111 | exit(err); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void get_args() |
| 116 | { |
| 117 | int i = 0; |
| 118 | int arg_ptr = 0; |
| 119 | int flag; |
| 120 | |
| 121 | num_args = 0; |
| 122 | do { |
| 123 | if (num_args == MAX_ARGS) |
| 124 | cuterror(MAX_ARGS_EXEEDED_ERROR); |
| 125 | if (!isdigit(line[i]) && line[i] != '-') |
| 126 | cuterror(SYNTAX_ERROR); |
| 127 | |
| 128 | args[arg_ptr] = 1; |
| 129 | args[arg_ptr + 1] = BUFSIZ; |
| 130 | flag = 1; |
| 131 | |
| 132 | while (line[i] != ',' && line[i] != 0) { |
| 133 | if (isdigit(line[i])) { |
| 134 | args[arg_ptr] = 0; |
| 135 | while (isdigit(line[i])) |
| 136 | args[arg_ptr] = 10 * args[arg_ptr] + line[i++] - '0'; |
| 137 | if (!args[arg_ptr]) |
| 138 | cuterror(POSITION_ERROR); |
| 139 | arg_ptr++; |
| 140 | } |
| 141 | if (line[i] == '-') { |
| 142 | arg_ptr |= 1; |
| 143 | i++; |
| 144 | flag = 0; |
| 145 | } |
| 146 | } |
| 147 | if (flag && arg_ptr & 1) |
| 148 | args[arg_ptr] = args[arg_ptr - 1]; |
| 149 | if (args[num_args * 2] > args[num_args * 2 + 1]) |
| 150 | cuterror(RANGE_ERROR); |
| 151 | num_args++; |
| 152 | arg_ptr = num_args * 2; |
| 153 | } |
| 154 | while (line[i++]); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void cut() |
| 159 | { |
| 160 | int i, j, length, maxcol=0; |
| 161 | char *columns[MAX_FIELD]; |
| 162 | |
| 163 | while (fgets(line, BUFSIZ, fd)) { |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 164 | maxcol=0; |
| 165 | length = strlen(line) - 1; |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 166 | *(line + length) = 0; |
| 167 | switch (mode) { |
| 168 | case DUMP_STDIN: |
| 169 | printf("%s", line); |
| 170 | break; |
| 171 | case OPTIONF: |
| 172 | columns[maxcol++] = line; |
| 173 | for (i = 0; i < length; i++) { |
| 174 | if (*(line + i) == delim) { |
| 175 | *(line + i) = 0; |
| 176 | if (maxcol == MAX_FIELD) |
| 177 | cuterror(MAX_FIELDS_EXEEDED_ERROR); |
| 178 | columns[maxcol] = line + i + 1; |
| 179 | maxcol++; |
| 180 | } |
| 181 | } |
| 182 | if (maxcol != 1) { |
| 183 | for (i = 0; i < num_args; i++) { |
| 184 | for (j = args[i * 2]; j <= args[i * 2 + 1]; j++) |
| 185 | if (j <= maxcol) { |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 186 | |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 187 | printf("%s", columns[j - 1]); |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 188 | |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 189 | if (i != num_args - 1 || j != args[i * 2 + 1]) |
| 190 | putchar(delim); |
| 191 | } |
| 192 | } |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 193 | } else if (option != OPTIONS) { |
| 194 | printf("%s",line); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 195 | } |
| 196 | break; |
| 197 | case OPTIONC: |
| 198 | for (i = 0; i < num_args; i++) { |
| 199 | for (j = args[i * 2]; |
| 200 | j <= (args[i * 2 + 1] > |
| 201 | length ? length : args[i * 2 + 1]); j++) |
| 202 | putchar(*(line + j - 1)); |
| 203 | } |
| 204 | } |
| 205 | if (maxcol != 1) |
| 206 | putchar('\n'); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | int cut_main(int argc, char **argv) |
| 212 | { |
| 213 | int i = 1; |
| 214 | int numberFilenames = 0; |
| 215 | |
| 216 | name = argv[0]; |
| 217 | |
| 218 | if (argc == 1 || strcmp(argv[1], dash_dash_help)==0) |
| 219 | usage( "cut [OPTION]... [FILE]...\n" |
| 220 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 221 | "\nPrints selected fields from each input FILE to standard output.\n\n" |
| 222 | "Options:\n" |
| 223 | "\t-b LIST\tOutput only bytes from LIST\n" |
| 224 | "\t-c LIST\tOutput only characters from LIST\n" |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 225 | "\t-d CHAR\tUse CHAR instead of tab as the field delimiter\n" |
| 226 | "\t-s\tOnly output Lines if the include DELIM\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 227 | "\t-f N\tPrint only these fields\n" |
| 228 | "\t-n\tIgnored\n" |
| 229 | #endif |
| 230 | ); |
| 231 | |
| 232 | while (i < argc) { |
| 233 | if (argv[i][0] == '-') { |
| 234 | switch (argv[i++][1]) { |
| 235 | case 'd': |
| 236 | if (mode == OPTIONC || mode == OPTIONB) |
| 237 | warn(DELIMITER_NOT_APPLICABLE, "d"); |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 238 | if (argc > i) |
| 239 | delim = argv[i++][0]; |
| 240 | else |
| 241 | cuterror(SYNTAX_ERROR); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 242 | break; |
| 243 | case 'f': |
| 244 | sprintf(line, "%s", argv[i++]); |
| 245 | if (mode == OPTIONC || mode == OPTIONB) |
| 246 | warn(OVERRIDING_PREVIOUS_MODE, "f"); |
| 247 | mode = OPTIONF; |
| 248 | break; |
| 249 | case 'b': |
| 250 | sprintf(line, "%s", argv[i++]); |
| 251 | if (mode == OPTIONF || mode == OPTIONC) |
| 252 | warn(OVERRIDING_PREVIOUS_MODE, "b"); |
| 253 | mode = OPTIONB; |
| 254 | break; |
| 255 | case 'c': |
| 256 | sprintf(line, "%s", argv[i++]); |
| 257 | if (mode == OPTIONF || mode == OPTIONB) |
| 258 | warn(OVERRIDING_PREVIOUS_MODE, "c"); |
| 259 | mode = OPTIONC; |
| 260 | break; |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 261 | case 's': |
| 262 | option = OPTIONS; |
| 263 | |
| 264 | break; |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 265 | case '\0': /* - means: read from stdin */ |
| 266 | numberFilenames++; |
| 267 | break; |
| 268 | case 'n': /* needed for Posix, but no effect here */ |
| 269 | if (mode != OPTIONB) |
| 270 | warn(OPTION_NOT_APPLICABLE, "n"); |
| 271 | break; |
| 272 | default: |
| 273 | warn(UNKNOWN_OPTION, &(argv[i - 1][1])); |
| 274 | } |
| 275 | } else { |
| 276 | i++; |
| 277 | numberFilenames++; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Here follow the checks, if the selected options are reasonable. */ |
| 282 | if (mode == OPTIONB) /* since in Minix char := byte */ |
| 283 | mode = OPTIONC; |
Eric Andersen | 1386e70 | 2000-06-26 12:14:30 +0000 | [diff] [blame] | 284 | |
| 285 | if (mode != OPTIONF && option == OPTIONS) |
| 286 | warn(DELIMITER_NOT_APPLICABLE,"s"); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 287 | get_args(); |
| 288 | if (numberFilenames != 0) { |
| 289 | i = 1; |
| 290 | while (i < argc) { |
| 291 | if (argv[i][0] == '-') { |
| 292 | switch (argv[i][1]) { |
| 293 | case 'f': |
| 294 | case 'c': |
| 295 | case 'b': |
| 296 | case 'd': |
| 297 | i += 2; |
| 298 | break; |
| 299 | case 'n': |
| 300 | case 'i': |
| 301 | case 's': |
| 302 | i++; |
| 303 | break; |
| 304 | case '\0': |
| 305 | fd = stdin; |
| 306 | i++; |
| 307 | cut(); |
| 308 | break; |
| 309 | default: |
| 310 | i++; |
| 311 | } |
| 312 | } else { |
| 313 | if ((fd = fopen(argv[i++], "r")) == NULL) { |
| 314 | warn(FILE_NOT_READABLE, argv[i - 1]); |
| 315 | } else { |
| 316 | cut(); |
| 317 | fclose(fd); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } else { |
| 322 | fd = stdin; |
| 323 | cut(); |
| 324 | } |
| 325 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 326 | return(exit_status); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* cut - extract columns from a file or stdin. Author: Michael J. Holme |
| 330 | * |
| 331 | * Copyright 1989, Michael John Holme, All rights reserved. |
| 332 | * This code may be freely distributed, provided that this notice |
| 333 | * remains intact. |
| 334 | * |
| 335 | * V1.1: 6th September 1989 |
| 336 | * |
| 337 | * Bugs, criticisms, etc, |
| 338 | * c/o Mark Powell |
| 339 | * JANET sq79@uk.ac.liv |
| 340 | * ARPA sq79%liv.ac.uk@nsfnet-relay.ac.uk |
| 341 | * UUCP ...!mcvax!ukc!liv.ac.uk!sq79 |
| 342 | *------------------------------------------------------------------------- |
| 343 | * Changed for POSIX1003.2/Draft10 conformance |
| 344 | * Thomas Brupbacher (tobr@mw.lpc.ethz.ch), September 1990. |
| 345 | * Changes: |
| 346 | * - separation of error messages ( stderr) and output (stdout). |
| 347 | * - support for -b and -n (no effect, -b acts as -c) |
| 348 | * - support for -s |
| 349 | *------------------------------------------------------------------------- |
| 350 | */ |
| 351 | |
| 352 | /* |
| 353 | * Copyright (c) 1987,1997, Prentice Hall |
| 354 | * All rights reserved. |
| 355 | * |
| 356 | * Redistribution and use of the MINIX operating system in source and |
| 357 | * binary forms, with or without modification, are permitted provided |
| 358 | * that the following conditions are met: |
| 359 | * |
| 360 | * Redistributions of source code must retain the above copyright |
| 361 | * notice, this list of conditions and the following disclaimer. |
| 362 | * |
| 363 | * Redistributions in binary form must reproduce the above |
| 364 | * copyright notice, this list of conditions and the following |
| 365 | * disclaimer in the documentation and/or other materials provided |
| 366 | * with the distribution. |
| 367 | * |
| 368 | * Neither the name of Prentice Hall nor the names of the software |
| 369 | * authors or contributors may be used to endorse or promote |
| 370 | * products derived from this software without specific prior |
| 371 | * written permission. |
| 372 | * |
| 373 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND |
| 374 | * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 375 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 376 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 377 | * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE |
| 378 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 379 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 380 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 381 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 382 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 383 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 384 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 385 | * |
| 386 | */ |
| 387 | |
| 388 | |