busybox/coreutils/cut.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * cut.c - minimalist version of cut
   4 *
   5 * Copyright (C) 1999,2000,2001 by Lineo, inc.
   6 * Written by Mark Whitley <markw@codepoet.org>
   7 * debloated by Bernhard Reutner-Fischer
   8 *
   9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10 */
  11//config:config CUT
  12//config:       bool "cut (5.8 kb)"
  13//config:       default y
  14//config:       help
  15//config:       cut is used to print selected parts of lines from
  16//config:       each file to stdout.
  17//config:
  18//config:config FEATURE_CUT_REGEX
  19//config:       bool "cut -F"
  20//config:       default y
  21//config:       depends on CUT
  22//config:       help
  23//config:       Allow regex based delimiters.
  24
  25//applet:IF_CUT(APPLET_NOEXEC(cut, cut, BB_DIR_USR_BIN, BB_SUID_DROP, cut))
  26
  27//kbuild:lib-$(CONFIG_CUT) += cut.o
  28
  29//usage:#define cut_trivial_usage
  30//usage:       "[OPTIONS] [FILE]..."
  31//usage:#define cut_full_usage "\n\n"
  32//usage:       "Print selected fields from FILEs to stdout\n"
  33//usage:     "\n        -b LIST Output only bytes from LIST"
  34//usage:     "\n        -c LIST Output only characters from LIST"
  35//usage:     "\n        -d SEP  Field delimiter for input (default -f TAB, -F run of whitespace)"
  36//usage:     "\n        -O SEP  Field delimeter for output (default = -d for -f, one space for -F)"
  37//usage:     "\n        -D      Don't sort/collate sections or match -fF lines without delimeter"
  38//usage:     "\n        -f LIST Print only these fields (-d is single char)"
  39//usage:     IF_FEATURE_CUT_REGEX(
  40//usage:     "\n        -F LIST Print only these fields (-d is regex)"
  41//usage:     )
  42//usage:     "\n        -s      Output only lines containing delimiter"
  43//usage:     "\n        -n      Ignored"
  44//(manpage:-n   with -b: don't split multibyte characters)
  45//usage:
  46//usage:#define cut_example_usage
  47//usage:       "$ echo \"Hello world\" | cut -f 1 -d ' '\n"
  48//usage:       "Hello\n"
  49//usage:       "$ echo \"Hello world\" | cut -f 2 -d ' '\n"
  50//usage:       "world\n"
  51
  52#include "libbb.h"
  53
  54#if ENABLE_FEATURE_CUT_REGEX
  55#include "xregex.h"
  56#else
  57#define regex_t int
  58typedef struct { int rm_eo, rm_so; } regmatch_t;
  59#define xregcomp(x, ...) *(x) = 0
  60#define regexec(...)     0
  61#endif
  62
  63/* This is a NOEXEC applet. Be very careful! */
  64
  65
  66/* option vars */
  67#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
  68#define CUT_OPT_BYTE_FLGS     (1 << 0)
  69#define CUT_OPT_CHAR_FLGS     (1 << 1)
  70#define CUT_OPT_FIELDS_FLGS   (1 << 2)
  71#define CUT_OPT_DELIM_FLGS    (1 << 3)
  72#define CUT_OPT_ODELIM_FLGS   (1 << 4)
  73#define CUT_OPT_SUPPRESS_FLGS (1 << 5)
  74#define CUT_OPT_NOSORT_FLGS   (1 << 6)
  75#define CUT_OPT_REGEX_FLGS    ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
  76
  77struct cut_list {
  78        int startpos;
  79        int endpos;
  80};
  81
  82static int cmpfunc(const void *a, const void *b)
  83{
  84        return (((struct cut_list *) a)->startpos -
  85                        ((struct cut_list *) b)->startpos);
  86}
  87
  88static void cut_file(FILE *file, const char *delim, const char *odelim,
  89                const struct cut_list *cut_lists, unsigned nlists)
  90{
  91        char *line;
  92        unsigned linenum = 0;   /* keep these zero-based to be consistent */
  93        regex_t reg;
  94        int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS;
  95
  96        if (shoe) xregcomp(&reg, delim, REG_EXTENDED);
  97
  98        /* go through every line in the file */
  99        while ((line = xmalloc_fgetline(file)) != NULL) {
 100
 101                /* set up a list so we can keep track of what's been printed */
 102                int linelen = strlen(line);
 103                char *printed = xzalloc(linelen + 1);
 104                char *orig_line = line;
 105                unsigned cl_pos = 0;
 106
 107                /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
 108                if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
 109                        /* print the chars specified in each cut list */
 110                        for (; cl_pos < nlists; cl_pos++) {
 111                                for (spos = cut_lists[cl_pos].startpos; spos < linelen;) {
 112                                        if (!printed[spos]) {
 113                                                printed[spos] = 'X';
 114                                                putchar(line[spos]);
 115                                        }
 116                                        if (++spos > cut_lists[cl_pos].endpos) {
 117                                                break;
 118                                        }
 119                                }
 120                        }
 121                } else if (*delim == '\n') {    /* cut by lines */
 122                        spos = cut_lists[cl_pos].startpos;
 123
 124                        /* get out if we have no more lists to process or if the lines
 125                         * are lower than what we're interested in */
 126                        if (((int)linenum < spos) || (cl_pos >= nlists))
 127                                goto next_line;
 128
 129                        /* if the line we're looking for is lower than the one we were
 130                         * passed, it means we displayed it already, so move on */
 131                        while (spos < (int)linenum) {
 132                                spos++;
 133                                /* go to the next list if we're at the end of this one */
 134                                if (spos > cut_lists[cl_pos].endpos) {
 135                                        cl_pos++;
 136                                        /* get out if there's no more lists to process */
 137                                        if (cl_pos >= nlists)
 138                                                goto next_line;
 139                                        spos = cut_lists[cl_pos].startpos;
 140                                        /* get out if the current line is lower than the one
 141                                         * we just became interested in */
 142                                        if ((int)linenum < spos)
 143                                                goto next_line;
 144                                }
 145                        }
 146
 147                        /* If we made it here, it means we've found the line we're
 148                         * looking for, so print it */
 149                        puts(line);
 150                        goto next_line;
 151                } else {                /* cut by fields */
 152                        unsigned uu = 0, start = 0, end = 0, out = 0;
 153                        int dcount = 0;
 154
 155                        /* Loop through bytes, finding next delimiter */
 156                        for (;;) {
 157                                /* End of current range? */
 158                                if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
 159                                        if (++cl_pos >= nlists) break;
 160                                        if (option_mask32 & CUT_OPT_NOSORT_FLGS)
 161                                                start = dcount = uu = 0;
 162                                        end = 0;
 163                                }
 164                                /* End of current line? */
 165                                if (uu == linelen) {
 166                                        /* If we've seen no delimiters, check -s */
 167                                        if (!cl_pos && !dcount && !shoe) {
 168                                                if (option_mask32 & CUT_OPT_SUPPRESS_FLGS)
 169                                                        goto next_line;
 170                                        } else if (dcount<cut_lists[cl_pos].startpos)
 171                                                start = linelen;
 172                                        end = linelen;
 173                                } else {
 174                                        /* Find next delimiter */
 175                                        if (shoe) {
 176                                                regmatch_t rr = {-1, -1};
 177
 178                                                if (!regexec(&reg, line+uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
 179                                                        end = uu + rr.rm_so;
 180                                                        uu += rr.rm_eo;
 181                                                } else {
 182                                                        uu = linelen;
 183                                                        continue;
 184                                                }
 185                                        } else if (line[end = uu++] != *delim)
 186                                                continue;
 187
 188                                        /* Got delimiter. Loop if not yet within range. */
 189                                        if (dcount++ < cut_lists[cl_pos].startpos) {
 190                                                start = uu;
 191                                                continue;
 192                                        }
 193                                }
 194                                if (end != start || !shoe)
 195                                        printf("%s%.*s", out++ ? odelim : "", end-start, line + start);
 196                                start = uu;
 197                                if (!dcount)
 198                                        break;
 199                        }
 200                }
 201                /* if we printed anything, finish with newline */
 202                putchar('\n');
 203 next_line:
 204                linenum++;
 205                free(printed);
 206                free(orig_line);
 207        }
 208}
 209
 210int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 211int cut_main(int argc UNUSED_PARAM, char **argv)
 212{
 213        /* growable array holding a series of lists */
 214        struct cut_list *cut_lists = NULL;
 215        unsigned nlists = 0;    /* number of elements in above list */
 216        char *sopt, *ltok;
 217        const char *delim = NULL;
 218        const char *odelim = NULL;
 219        unsigned opt;
 220
 221#define ARG "bcf"IF_FEATURE_CUT_REGEX("F")
 222        opt = getopt32(argv, "^"
 223                        OPT_STR  // = "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
 224                        "\0" "b--"ARG":c--"ARG":f--"ARG IF_FEATURE_CUT_REGEX("F--"ARG),
 225                        &sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt)
 226        );
 227        if (!delim || !*delim)
 228                delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t";
 229        if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim;
 230
 231//      argc -= optind;
 232        argv += optind;
 233        if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS)))
 234                bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
 235
 236        /*  non-field (char or byte) cutting has some special handling */
 237        if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) {
 238                static const char _op_on_field[] ALIGN1 = " only when operating on fields";
 239
 240                if (opt & CUT_OPT_SUPPRESS_FLGS) {
 241                        bb_error_msg_and_die
 242                                ("suppressing non-delimited lines makes sense%s", _op_on_field);
 243                }
 244                if (opt & CUT_OPT_DELIM_FLGS) {
 245                        bb_error_msg_and_die
 246                                ("a delimiter may be specified%s", _op_on_field);
 247                }
 248        }
 249
 250        /*
 251         * parse list and put values into startpos and endpos.
 252         * valid list formats: N, N-, N-M, -M
 253         * more than one list can be separated by commas
 254         */
 255        {
 256                char *ntok;
 257                int s = 0, e = 0;
 258
 259                /* take apart the lists, one by one (they are separated with commas) */
 260                while ((ltok = strsep(&sopt, ",")) != NULL) {
 261
 262                        /* it's actually legal to pass an empty list */
 263                        if (!ltok[0])
 264                                continue;
 265
 266                        /* get the start pos */
 267                        ntok = strsep(&ltok, "-");
 268                        if (!ntok[0]) {
 269                                s = 0;
 270                        } else {
 271                                s = xatoi_positive(ntok);
 272                                /* account for the fact that arrays are zero based, while
 273                                 * the user expects the first char on the line to be char #1 */
 274                                if (s != 0)
 275                                        s--;
 276                        }
 277
 278                        /* get the end pos */
 279                        if (ltok == NULL) {
 280                                e = s;
 281                        } else if (!ltok[0]) {
 282                                e = INT_MAX;
 283                        } else {
 284                                e = xatoi_positive(ltok);
 285                                /* if the user specified and end position of 0,
 286                                 * that means "til the end of the line" */
 287                                if (!*ltok)
 288                                        e = INT_MAX;
 289                                else if (e < s)
 290                                        bb_error_msg_and_die("%d<%d", e, s);
 291                                e--;    /* again, arrays are zero based, lines are 1 based */
 292                        }
 293
 294                        /* add the new list */
 295                        cut_lists = xrealloc_vector(cut_lists, 4, nlists);
 296                        /* NB: startpos is always >= 0 */
 297                        cut_lists[nlists].startpos = s;
 298                        cut_lists[nlists].endpos = e;
 299                        nlists++;
 300                }
 301
 302                /* make sure we got some cut positions out of all that */
 303                if (nlists == 0)
 304                        bb_simple_error_msg_and_die("missing list of positions");
 305
 306                /* now that the lists are parsed, we need to sort them to make life
 307                 * easier on us when it comes time to print the chars / fields / lines
 308                 */
 309                if (!(opt & CUT_OPT_NOSORT_FLGS))
 310                        qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
 311        }
 312
 313        {
 314                int retval = EXIT_SUCCESS;
 315
 316                if (!*argv)
 317                        *--argv = (char *)"-";
 318
 319                do {
 320                        FILE *file = fopen_or_warn_stdin(*argv);
 321                        if (!file) {
 322                                retval = EXIT_FAILURE;
 323                                continue;
 324                        }
 325                        cut_file(file, delim, odelim, cut_lists, nlists);
 326                        fclose_if_not_stdin(file);
 327                } while (*++argv);
 328
 329                if (ENABLE_FEATURE_CLEAN_UP)
 330                        free(cut_lists);
 331                fflush_stdout_and_exit(retval);
 332        }
 333}
 334