"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 2 | /* |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 3 | * cut.c - minimalist version of cut |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Written by Mark Whitley <markw@codepoet.org> |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * debloated by Bernhard Reutner-Fischer |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config:config CUT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "cut (5.8 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: cut is used to print selected parts of lines from |
| 16 | //config: each file to stdout. |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 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 |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 28 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 29 | //usage:#define cut_trivial_usage |
| 30 | //usage: "[OPTIONS] [FILE]..." |
| 31 | //usage:#define cut_full_usage "\n\n" |
Denys Vlasenko | ba9f9c2 | 2021-04-14 15:15:45 +0200 | [diff] [blame] | 32 | //usage: "Print selected fields from FILEs to stdout\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 33 | //usage: "\n -b LIST Output only bytes from LIST" |
| 34 | //usage: "\n -c LIST Output only characters from LIST" |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 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: ) |
Denys Vlasenko | ba9f9c2 | 2021-04-14 15:15:45 +0200 | [diff] [blame] | 42 | //usage: "\n -s Output only lines containing delimiter" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 43 | //usage: "\n -n Ignored" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 44 | //(manpage:-n with -b: don't split multibyte characters) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 52 | #include "libbb.h" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 53 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 54 | #if ENABLE_FEATURE_CUT_REGEX |
| 55 | #include "xregex.h" |
| 56 | #else |
| 57 | #define regex_t int |
| 58 | typedef struct { int rm_eo, rm_so; } regmatch_t; |
| 59 | #define xregcomp(x, ...) *(x) = 0 |
| 60 | #define regexec(...) 0 |
| 61 | #endif |
| 62 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 63 | /* This is a NOEXEC applet. Be very careful! */ |
| 64 | |
| 65 | |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 66 | /* option vars */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 67 | #define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n" |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 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) |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 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) |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 76 | |
| 77 | struct cut_list { |
| 78 | int startpos; |
| 79 | int endpos; |
| 80 | }; |
| 81 | |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 82 | static int cmpfunc(const void *a, const void *b) |
| 83 | { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 84 | return (((struct cut_list *) a)->startpos - |
| 85 | ((struct cut_list *) b)->startpos); |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 88 | static void cut_file(FILE *file, const char *delim, const char *odelim, |
| 89 | const struct cut_list *cut_lists, unsigned nlists) |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 90 | { |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 91 | char *line; |
| 92 | unsigned linenum = 0; /* keep these zero-based to be consistent */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 93 | regex_t reg; |
| 94 | int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS; |
| 95 | |
| 96 | if (shoe) xregcomp(®, delim, REG_EXTENDED); |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 97 | |
| 98 | /* go through every line in the file */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 99 | while ((line = xmalloc_fgetline(file)) != NULL) { |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 100 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 101 | /* set up a list so we can keep track of what's been printed */ |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 102 | int linelen = strlen(line); |
Denis Vlasenko | 8334db1 | 2008-08-15 21:20:23 +0000 | [diff] [blame] | 103 | char *printed = xzalloc(linelen + 1); |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 104 | char *orig_line = line; |
| 105 | unsigned cl_pos = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 106 | |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 107 | /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 108 | if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 109 | /* print the chars specified in each cut list */ |
| 110 | for (; cl_pos < nlists; cl_pos++) { |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 111 | for (spos = cut_lists[cl_pos].startpos; spos < linelen;) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 112 | if (!printed[spos]) { |
| 113 | printed[spos] = 'X'; |
| 114 | putchar(line[spos]); |
| 115 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 116 | if (++spos > cut_lists[cl_pos].endpos) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 117 | break; |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 118 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 121 | } else if (*delim == '\n') { /* cut by lines */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 122 | spos = cut_lists[cl_pos].startpos; |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 123 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 124 | /* get out if we have no more lists to process or if the lines |
| 125 | * are lower than what we're interested in */ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 126 | if (((int)linenum < spos) || (cl_pos >= nlists)) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 131 | while (spos < (int)linenum) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 132 | spos++; |
| 133 | /* go to the next list if we're at the end of this one */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 134 | if (spos > cut_lists[cl_pos].endpos) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 142 | if ((int)linenum < spos) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 152 | unsigned uu = 0, start = 0, end = 0, out = 0; |
| 153 | int dcount = 0; |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 154 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 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}; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 177 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 178 | if (!regexec(®, 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; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 187 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 188 | /* Got delimiter. Loop if not yet within range. */ |
| 189 | if (dcount++ < cut_lists[cl_pos].startpos) { |
| 190 | start = uu; |
| 191 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 192 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 193 | } |
| 194 | if (end != start || !shoe) |
| 195 | printf("%s%.*s", out++ ? odelim : "", end-start, line + start); |
| 196 | start = uu; |
| 197 | if (!dcount) |
| 198 | break; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 199 | } |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 200 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 201 | /* if we printed anything, finish with newline */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 202 | putchar('\n'); |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 203 | next_line: |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 204 | linenum++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 205 | free(printed); |
| 206 | free(orig_line); |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 210 | int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 211 | int cut_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 212 | { |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 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 */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 216 | char *sopt, *ltok; |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 217 | const char *delim = NULL; |
| 218 | const char *odelim = NULL; |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 219 | unsigned opt; |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 220 | |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 221 | #define ARG "bcf"IF_FEATURE_CUT_REGEX("F") |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 222 | opt = getopt32(argv, "^" |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 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) |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 226 | ); |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 227 | if (!delim || !*delim) |
| 228 | delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t"; |
| 229 | if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim; |
| 230 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 231 | // argc -= optind; |
| 232 | argv += optind; |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 233 | if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS))) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 234 | bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 235 | |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 236 | /* non-field (char or byte) cutting has some special handling */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 237 | if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) { |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 238 | static const char _op_on_field[] ALIGN1 = " only when operating on fields"; |
| 239 | |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 240 | if (opt & CUT_OPT_SUPPRESS_FLGS) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 241 | bb_error_msg_and_die |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 242 | ("suppressing non-delimited lines makes sense%s", _op_on_field); |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 243 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 244 | if (opt & CUT_OPT_DELIM_FLGS) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 245 | bb_error_msg_and_die |
| 246 | ("a delimiter may be specified%s", _op_on_field); |
Mark Whitley | b696763 | 2001-05-18 23:04:51 +0000 | [diff] [blame] | 247 | } |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 259 | /* take apart the lists, one by one (they are separated with commas) */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 260 | while ((ltok = strsep(&sopt, ",")) != NULL) { |
| 261 | |
| 262 | /* it's actually legal to pass an empty list */ |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 263 | if (!ltok[0]) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 264 | continue; |
| 265 | |
| 266 | /* get the start pos */ |
| 267 | ntok = strsep(<ok, "-"); |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 268 | if (!ntok[0]) { |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 269 | s = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 270 | } else { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 271 | s = xatoi_positive(ntok); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 279 | if (ltok == NULL) { |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 280 | e = s; |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 281 | } else if (!ltok[0]) { |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 282 | e = INT_MAX; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 283 | } else { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 284 | e = xatoi_positive(ltok); |
Denis Vlasenko | 9e3a540 | 2008-07-22 10:34:46 +0000 | [diff] [blame] | 285 | /* if the user specified and end position of 0, |
| 286 | * that means "til the end of the line" */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 287 | if (!*ltok) |
| 288 | e = INT_MAX; |
| 289 | else if (e < s) |
| 290 | bb_error_msg_and_die("%d<%d", e, s); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 291 | e--; /* again, arrays are zero based, lines are 1 based */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 294 | /* add the new list */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 295 | cut_lists = xrealloc_vector(cut_lists, 4, nlists); |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 296 | /* NB: startpos is always >= 0 */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 297 | cut_lists[nlists].startpos = s; |
| 298 | cut_lists[nlists].endpos = e; |
| 299 | nlists++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* make sure we got some cut positions out of all that */ |
| 303 | if (nlists == 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 304 | bb_simple_error_msg_and_die("missing list of positions"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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 | */ |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 309 | if (!(opt & CUT_OPT_NOSORT_FLGS)) |
| 310 | qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 313 | { |
| 314 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 315 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 316 | if (!*argv) |
| 317 | *--argv = (char *)"-"; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 318 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 319 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 320 | FILE *file = fopen_or_warn_stdin(*argv); |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 321 | if (!file) { |
| 322 | retval = EXIT_FAILURE; |
| 323 | continue; |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 324 | } |
Rob Landley | 0068ce2 | 2021-07-20 16:02:31 +0200 | [diff] [blame] | 325 | cut_file(file, delim, odelim, cut_lists, nlists); |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 326 | fclose_if_not_stdin(file); |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 327 | } while (*++argv); |
Denis Vlasenko | 3831c91 | 2007-11-23 07:26:15 +0000 | [diff] [blame] | 328 | |
| 329 | if (ENABLE_FEATURE_CLEAN_UP) |
| 330 | free(cut_lists); |
| 331 | fflush_stdout_and_exit(retval); |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 332 | } |
Mark Whitley | 807f0fd | 2000-08-02 18:30:11 +0000 | [diff] [blame] | 333 | } |