1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
64
65
66
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;
93 regex_t reg;
94 int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS;
95
96 if (shoe) xregcomp(®, delim, REG_EXTENDED);
97
98
99 while ((line = xmalloc_fgetline(file)) != NULL) {
100
101
102 int linelen = strlen(line);
103 char *printed = xzalloc(linelen + 1);
104 char *orig_line = line;
105 unsigned cl_pos = 0;
106
107
108 if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
109
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') {
122 spos = cut_lists[cl_pos].startpos;
123
124
125
126 if (((int)linenum < spos) || (cl_pos >= nlists))
127 goto next_line;
128
129
130
131 while (spos < (int)linenum) {
132 spos++;
133
134 if (spos > cut_lists[cl_pos].endpos) {
135 cl_pos++;
136
137 if (cl_pos >= nlists)
138 goto next_line;
139 spos = cut_lists[cl_pos].startpos;
140
141
142 if ((int)linenum < spos)
143 goto next_line;
144 }
145 }
146
147
148
149 puts(line);
150 goto next_line;
151 } else {
152 unsigned uu = 0, start = 0, end = 0, out = 0;
153 int dcount = 0;
154
155
156 for (;;) {
157
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
165 if (uu == linelen) {
166
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
175 if (shoe) {
176 regmatch_t rr = {-1, -1};
177
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;
187
188
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
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
214 struct cut_list *cut_lists = NULL;
215 unsigned nlists = 0;
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
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
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
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
252
253
254
255 {
256 char *ntok;
257 int s = 0, e = 0;
258
259
260 while ((ltok = strsep(&sopt, ",")) != NULL) {
261
262
263 if (!ltok[0])
264 continue;
265
266
267 ntok = strsep(<ok, "-");
268 if (!ntok[0]) {
269 s = 0;
270 } else {
271 s = xatoi_positive(ntok);
272
273
274 if (s != 0)
275 s--;
276 }
277
278
279 if (ltok == NULL) {
280 e = s;
281 } else if (!ltok[0]) {
282 e = INT_MAX;
283 } else {
284 e = xatoi_positive(ltok);
285
286
287 if (!*ltok)
288 e = INT_MAX;
289 else if (e < s)
290 bb_error_msg_and_die("%d<%d", e, s);
291 e--;
292 }
293
294
295 cut_lists = xrealloc_vector(cut_lists, 4, nlists);
296
297 cut_lists[nlists].startpos = s;
298 cut_lists[nlists].endpos = e;
299 nlists++;
300 }
301
302
303 if (nlists == 0)
304 bb_simple_error_msg_and_die("missing list of positions");
305
306
307
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