blob: 7009e74cfa1b7ec5970a08f43f9f8bf34fedeadb [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00002/*
Mark Whitley807f0fd2000-08-02 18:30:11 +00003 * cut.c - minimalist version of cut
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00004 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Written by Mark Whitley <markw@codepoet.org>
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00007 * debloated by Bernhard Reutner-Fischer
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000010 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config:config CUT
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "cut (5.8 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config: default y
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: cut is used to print selected parts of lines from
16//config: each file to stdout.
Rob Landley0068ce22021-07-20 16:02:31 +020017//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 Vlasenkoaf3f4202016-11-23 14:46:56 +010024
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 Andersen7ab9c7e2000-05-12 19:41:47 +000028
Pere Orga34425382011-03-31 14:43:25 +020029//usage:#define cut_trivial_usage
30//usage: "[OPTIONS] [FILE]..."
31//usage:#define cut_full_usage "\n\n"
Denys Vlasenkoba9f9c22021-04-14 15:15:45 +020032//usage: "Print selected fields from FILEs to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020033//usage: "\n -b LIST Output only bytes from LIST"
34//usage: "\n -c LIST Output only characters from LIST"
Rob Landley0068ce22021-07-20 16:02:31 +020035//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 Vlasenkoba9f9c22021-04-14 15:15:45 +020042//usage: "\n -s Output only lines containing delimiter"
Pere Orga34425382011-03-31 14:43:25 +020043//usage: "\n -n Ignored"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020044//(manpage:-n with -b: don't split multibyte characters)
Pere Orga34425382011-03-31 14:43:25 +020045//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 Vlasenkob6adbf12007-05-26 19:00:18 +000052#include "libbb.h"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000053
Rob Landley0068ce22021-07-20 16:02:31 +020054#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
Denis Vlasenko99912ca2007-04-10 15:43:37 +000063/* This is a NOEXEC applet. Be very careful! */
64
65
Mark Whitleyb6967632001-05-18 23:04:51 +000066/* option vars */
Rob Landley0068ce22021-07-20 16:02:31 +020067#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
Denis Vlasenko3831c912007-11-23 07:26:15 +000068#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 Landley0068ce22021-07-20 16:02:31 +020072#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 Whitleyb6967632001-05-18 23:04:51 +000076
77struct cut_list {
78 int startpos;
79 int endpos;
80};
81
Mark Whitleyb6967632001-05-18 23:04:51 +000082static int cmpfunc(const void *a, const void *b)
83{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000084 return (((struct cut_list *) a)->startpos -
85 ((struct cut_list *) b)->startpos);
Mark Whitleyb6967632001-05-18 23:04:51 +000086}
87
Rob Landley0068ce22021-07-20 16:02:31 +020088static void cut_file(FILE *file, const char *delim, const char *odelim,
89 const struct cut_list *cut_lists, unsigned nlists)
Mark Whitley807f0fd2000-08-02 18:30:11 +000090{
Denis Vlasenko9e3a5402008-07-22 10:34:46 +000091 char *line;
92 unsigned linenum = 0; /* keep these zero-based to be consistent */
Rob Landley0068ce22021-07-20 16:02:31 +020093 regex_t reg;
94 int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS;
95
96 if (shoe) xregcomp(&reg, delim, REG_EXTENDED);
Mark Whitley807f0fd2000-08-02 18:30:11 +000097
98 /* go through every line in the file */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000099 while ((line = xmalloc_fgetline(file)) != NULL) {
Mark Whitleyb6967632001-05-18 23:04:51 +0000100
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101 /* set up a list so we can keep track of what's been printed */
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000102 int linelen = strlen(line);
Denis Vlasenko8334db12008-08-15 21:20:23 +0000103 char *printed = xzalloc(linelen + 1);
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000104 char *orig_line = line;
105 unsigned cl_pos = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000106
Mark Whitleyb6967632001-05-18 23:04:51 +0000107 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
Denis Vlasenko372686b2006-10-12 22:42:33 +0000108 if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000109 /* print the chars specified in each cut list */
110 for (; cl_pos < nlists; cl_pos++) {
Rob Landley0068ce22021-07-20 16:02:31 +0200111 for (spos = cut_lists[cl_pos].startpos; spos < linelen;) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000112 if (!printed[spos]) {
113 printed[spos] = 'X';
114 putchar(line[spos]);
115 }
Rob Landley0068ce22021-07-20 16:02:31 +0200116 if (++spos > cut_lists[cl_pos].endpos) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000117 break;
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000118 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000119 }
120 }
Rob Landley0068ce22021-07-20 16:02:31 +0200121 } else if (*delim == '\n') { /* cut by lines */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000122 spos = cut_lists[cl_pos].startpos;
Mark Whitleyb6967632001-05-18 23:04:51 +0000123
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000124 /* get out if we have no more lists to process or if the lines
125 * are lower than what we're interested in */
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000126 if (((int)linenum < spos) || (cl_pos >= nlists))
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000127 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 Vlasenko77ad97f2008-05-13 02:27:31 +0000131 while (spos < (int)linenum) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000132 spos++;
133 /* go to the next list if we're at the end of this one */
Rob Landley0068ce22021-07-20 16:02:31 +0200134 if (spos > cut_lists[cl_pos].endpos) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000135 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 Vlasenko77ad97f2008-05-13 02:27:31 +0000142 if ((int)linenum < spos)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000143 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 Landley0068ce22021-07-20 16:02:31 +0200152 unsigned uu = 0, start = 0, end = 0, out = 0;
153 int dcount = 0;
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200154
Rob Landley0068ce22021-07-20 16:02:31 +0200155 /* 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-Fischer73561cc2006-08-28 23:31:54 +0000177
Rob Landley0068ce22021-07-20 16:02:31 +0200178 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;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000187
Rob Landley0068ce22021-07-20 16:02:31 +0200188 /* Got delimiter. Loop if not yet within range. */
189 if (dcount++ < cut_lists[cl_pos].startpos) {
190 start = uu;
191 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000192 }
Rob Landley0068ce22021-07-20 16:02:31 +0200193 }
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-Fischer73561cc2006-08-28 23:31:54 +0000199 }
Mark Whitley807f0fd2000-08-02 18:30:11 +0000200 }
Rob Landley0068ce22021-07-20 16:02:31 +0200201 /* if we printed anything, finish with newline */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000202 putchar('\n');
Denis Vlasenko372686b2006-10-12 22:42:33 +0000203 next_line:
Mark Whitleyb6967632001-05-18 23:04:51 +0000204 linenum++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000205 free(printed);
206 free(orig_line);
Mark Whitley807f0fd2000-08-02 18:30:11 +0000207 }
208}
209
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000210int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000211int cut_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley807f0fd2000-08-02 18:30:11 +0000212{
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000213 /* 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-Fischer73561cc2006-08-28 23:31:54 +0000216 char *sopt, *ltok;
Rob Landley0068ce22021-07-20 16:02:31 +0200217 const char *delim = NULL;
218 const char *odelim = NULL;
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000219 unsigned opt;
Mark Whitley807f0fd2000-08-02 18:30:11 +0000220
Rob Landley0068ce22021-07-20 16:02:31 +0200221#define ARG "bcf"IF_FEATURE_CUT_REGEX("F")
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200222 opt = getopt32(argv, "^"
Rob Landley0068ce22021-07-20 16:02:31 +0200223 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 Vlasenko22542ec2017-08-08 21:55:02 +0200226 );
Rob Landley0068ce22021-07-20 16:02:31 +0200227 if (!delim || !*delim)
228 delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t";
229 if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim;
230
Denis Vlasenko9f739442006-12-16 23:49:13 +0000231// argc -= optind;
232 argv += optind;
Rob Landley0068ce22021-07-20 16:02:31 +0200233 if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS)))
James Byrne69374872019-07-02 11:35:03 +0200234 bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000235
Mark Whitleyb6967632001-05-18 23:04:51 +0000236 /* non-field (char or byte) cutting has some special handling */
Rob Landley0068ce22021-07-20 16:02:31 +0200237 if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) {
Denis Vlasenko3831c912007-11-23 07:26:15 +0000238 static const char _op_on_field[] ALIGN1 = " only when operating on fields";
239
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000240 if (opt & CUT_OPT_SUPPRESS_FLGS) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000241 bb_error_msg_and_die
Rob Landley0068ce22021-07-20 16:02:31 +0200242 ("suppressing non-delimited lines makes sense%s", _op_on_field);
Mark Whitleyb6967632001-05-18 23:04:51 +0000243 }
Rob Landley0068ce22021-07-20 16:02:31 +0200244 if (opt & CUT_OPT_DELIM_FLGS) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000245 bb_error_msg_and_die
246 ("a delimiter may be specified%s", _op_on_field);
Mark Whitleyb6967632001-05-18 23:04:51 +0000247 }
Mark Whitley807f0fd2000-08-02 18:30:11 +0000248 }
249
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000250 /*
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 Vlasenko62a90cd2008-03-17 09:07:36 +0000259 /* take apart the lists, one by one (they are separated with commas) */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000260 while ((ltok = strsep(&sopt, ",")) != NULL) {
261
262 /* it's actually legal to pass an empty list */
Denis Vlasenko3831c912007-11-23 07:26:15 +0000263 if (!ltok[0])
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 continue;
265
266 /* get the start pos */
267 ntok = strsep(&ltok, "-");
Denis Vlasenko3831c912007-11-23 07:26:15 +0000268 if (!ntok[0]) {
Rob Landley0068ce22021-07-20 16:02:31 +0200269 s = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000270 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +0200271 s = xatoi_positive(ntok);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000272 /* 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 Vlasenko3831c912007-11-23 07:26:15 +0000279 if (ltok == NULL) {
Rob Landley0068ce22021-07-20 16:02:31 +0200280 e = s;
Denis Vlasenko3831c912007-11-23 07:26:15 +0000281 } else if (!ltok[0]) {
Rob Landley0068ce22021-07-20 16:02:31 +0200282 e = INT_MAX;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000283 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +0200284 e = xatoi_positive(ltok);
Denis Vlasenko9e3a5402008-07-22 10:34:46 +0000285 /* if the user specified and end position of 0,
286 * that means "til the end of the line" */
Rob Landley0068ce22021-07-20 16:02:31 +0200287 if (!*ltok)
288 e = INT_MAX;
289 else if (e < s)
290 bb_error_msg_and_die("%d<%d", e, s);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000291 e--; /* again, arrays are zero based, lines are 1 based */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000292 }
293
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000294 /* add the new list */
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000295 cut_lists = xrealloc_vector(cut_lists, 4, nlists);
Rob Landley0068ce22021-07-20 16:02:31 +0200296 /* NB: startpos is always >= 0 */
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000297 cut_lists[nlists].startpos = s;
298 cut_lists[nlists].endpos = e;
299 nlists++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000300 }
301
302 /* make sure we got some cut positions out of all that */
303 if (nlists == 0)
James Byrne69374872019-07-02 11:35:03 +0200304 bb_simple_error_msg_and_die("missing list of positions");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305
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 Landley0068ce22021-07-20 16:02:31 +0200309 if (!(opt & CUT_OPT_NOSORT_FLGS))
310 qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000311 }
312
Denis Vlasenko3831c912007-11-23 07:26:15 +0000313 {
314 int retval = EXIT_SUCCESS;
Denis Vlasenko3831c912007-11-23 07:26:15 +0000315
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000316 if (!*argv)
317 *--argv = (char *)"-";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000318
Denis Vlasenko9f739442006-12-16 23:49:13 +0000319 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000320 FILE *file = fopen_or_warn_stdin(*argv);
Denis Vlasenko3831c912007-11-23 07:26:15 +0000321 if (!file) {
322 retval = EXIT_FAILURE;
323 continue;
Mark Whitley807f0fd2000-08-02 18:30:11 +0000324 }
Rob Landley0068ce22021-07-20 16:02:31 +0200325 cut_file(file, delim, odelim, cut_lists, nlists);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000326 fclose_if_not_stdin(file);
Denis Vlasenko9f739442006-12-16 23:49:13 +0000327 } while (*++argv);
Denis Vlasenko3831c912007-11-23 07:26:15 +0000328
329 if (ENABLE_FEATURE_CLEAN_UP)
330 free(cut_lists);
331 fflush_stdout_and_exit(retval);
Mark Whitley807f0fd2000-08-02 18:30:11 +0000332 }
Mark Whitley807f0fd2000-08-02 18:30:11 +0000333}