aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-12-10 01:38:31 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2024-12-10 01:38:31 +0100
commit73e9d25d7503896e94b5c00093a77b33d1a17a0d (patch)
treef1a0c6898677a9441fed25dc9d2b573d1eb79066
parentf02041441344389b05d10fe6ba8759b6670b8e10 (diff)
downloadbusybox-w32-73e9d25d7503896e94b5c00093a77b33d1a17a0d.tar.gz
busybox-w32-73e9d25d7503896e94b5c00093a77b33d1a17a0d.tar.bz2
busybox-w32-73e9d25d7503896e94b5c00093a77b33d1a17a0d.zip
cut: simplify OPT_ names, eliminate one variable
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/cut.c51
1 files changed, 27 insertions, 24 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index f68bbbad5..48f3656b4 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -65,14 +65,14 @@ typedef struct { int rm_eo, rm_so; } regmatch_t;
65 65
66/* option vars */ 66/* option vars */
67#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n" 67#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
68#define CUT_OPT_BYTE_FLGS (1 << 0) 68#define OPT_BYTE (1 << 0)
69#define CUT_OPT_CHAR_FLGS (1 << 1) 69#define OPT_CHAR (1 << 1)
70#define CUT_OPT_FIELDS_FLGS (1 << 2) 70#define OPT_FIELDS (1 << 2)
71#define CUT_OPT_DELIM_FLGS (1 << 3) 71#define OPT_DELIM (1 << 3)
72#define CUT_OPT_ODELIM_FLGS (1 << 4) 72#define OPT_ODELIM (1 << 4)
73#define CUT_OPT_SUPPRESS_FLGS (1 << 5) 73#define OPT_SUPPRESS (1 << 5)
74#define CUT_OPT_NOSORT_FLGS (1 << 6) 74#define OPT_NOSORT (1 << 6)
75#define CUT_OPT_REGEX_FLGS ((1 << 7) * ENABLE_FEATURE_CUT_REGEX) 75#define OPT_REGEX ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
76 76
77struct cut_list { 77struct cut_list {
78 int startpos; 78 int startpos;
@@ -88,12 +88,14 @@ static int cmpfunc(const void *a, const void *b)
88static void cut_file(FILE *file, const char *delim, const char *odelim, 88static void cut_file(FILE *file, const char *delim, const char *odelim,
89 const struct cut_list *cut_lists, unsigned nlists) 89 const struct cut_list *cut_lists, unsigned nlists)
90{ 90{
91#define opt_REGEX (option_mask32 & OPT_REGEX)
91 char *line; 92 char *line;
92 unsigned linenum = 0; /* keep these zero-based to be consistent */ 93 unsigned linenum = 0; /* keep these zero-based to be consistent */
93 regex_t reg; 94 regex_t reg;
94 int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS; 95 int spos;
95 96
96 if (shoe) xregcomp(&reg, delim, REG_EXTENDED); 97 if (opt_REGEX)
98 xregcomp(&reg, delim, REG_EXTENDED);
97 99
98 /* go through every line in the file */ 100 /* go through every line in the file */
99 while ((line = xmalloc_fgetline(file)) != NULL) { 101 while ((line = xmalloc_fgetline(file)) != NULL) {
@@ -105,7 +107,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
105 unsigned cl_pos = 0; 107 unsigned cl_pos = 0;
106 108
107 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ 109 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
108 if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) { 110 if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
109 /* print the chars specified in each cut list */ 111 /* print the chars specified in each cut list */
110 for (; cl_pos < nlists; cl_pos++) { 112 for (; cl_pos < nlists; cl_pos++) {
111 for (spos = cut_lists[cl_pos].startpos; spos < linelen;) { 113 for (spos = cut_lists[cl_pos].startpos; spos < linelen;) {
@@ -154,7 +156,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
154 156
155 /* Blank line? Check -s (later check for -s does not catch empty lines) */ 157 /* Blank line? Check -s (later check for -s does not catch empty lines) */
156 if (linelen == 0) { 158 if (linelen == 0) {
157 if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) 159 if (option_mask32 & OPT_SUPPRESS)
158 goto next_line; 160 goto next_line;
159 } 161 }
160 162
@@ -164,22 +166,22 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
164 if (end == linelen || dcount > cut_lists[cl_pos].endpos) { 166 if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
165 if (++cl_pos >= nlists) 167 if (++cl_pos >= nlists)
166 break; 168 break;
167 if (option_mask32 & CUT_OPT_NOSORT_FLGS) 169 if (option_mask32 & OPT_NOSORT)
168 start = dcount = uu = 0; 170 start = dcount = uu = 0;
169 end = 0; 171 end = 0;
170 } 172 }
171 /* End of current line? */ 173 /* End of current line? */
172 if (uu == linelen) { 174 if (uu == linelen) {
173 /* If we've seen no delimiters, check -s */ 175 /* If we've seen no delimiters, check -s */
174 if (!cl_pos && !dcount && !shoe) { 176 if (!cl_pos && !dcount && !opt_REGEX) {
175 if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) 177 if (option_mask32 & OPT_SUPPRESS)
176 goto next_line; 178 goto next_line;
177 } else if (dcount < cut_lists[cl_pos].startpos) 179 } else if (dcount < cut_lists[cl_pos].startpos)
178 start = linelen; 180 start = linelen;
179 end = linelen; 181 end = linelen;
180 } else { 182 } else {
181 /* Find next delimiter */ 183 /* Find next delimiter */
182 if (shoe) { 184 if (opt_REGEX) {
183 regmatch_t rr = {-1, -1}; 185 regmatch_t rr = {-1, -1};
184 186
185 if (!regexec(&reg, line + uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) { 187 if (!regexec(&reg, line + uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
@@ -201,7 +203,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
201 continue; 203 continue;
202 } 204 }
203 } 205 }
204 if (end != start || !shoe) 206 if (end != start || !opt_REGEX)
205 printf("%s%.*s", out++ ? odelim : "", end - start, line + start); 207 printf("%s%.*s", out++ ? odelim : "", end - start, line + start);
206 start = uu; 208 start = uu;
207 if (!dcount) 209 if (!dcount)
@@ -215,6 +217,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
215 free(printed); 217 free(printed);
216 free(orig_line); 218 free(orig_line);
217 } 219 }
220#undef opt_REGEX
218} 221}
219 222
220int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 223int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -235,23 +238,23 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
235 &sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt) 238 &sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt)
236 ); 239 );
237 if (!delim || !*delim) 240 if (!delim || !*delim)
238 delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t"; 241 delim = (opt & OPT_REGEX) ? "[[:space:]]+" : "\t";
239 if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim; 242 if (!odelim) odelim = (opt & OPT_REGEX) ? " " : delim;
240 243
241// argc -= optind; 244// argc -= optind;
242 argv += optind; 245 argv += optind;
243 if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS))) 246 if (!(opt & (OPT_BYTE | OPT_CHAR | OPT_FIELDS | OPT_REGEX)))
244 bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields"); 247 bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
245 248
246 /* non-field (char or byte) cutting has some special handling */ 249 /* non-field (char or byte) cutting has some special handling */
247 if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) { 250 if (!(opt & (OPT_FIELDS|OPT_REGEX))) {
248 static const char _op_on_field[] ALIGN1 = " only when operating on fields"; 251 static const char _op_on_field[] ALIGN1 = " only when operating on fields";
249 252
250 if (opt & CUT_OPT_SUPPRESS_FLGS) { 253 if (opt & OPT_SUPPRESS) {
251 bb_error_msg_and_die 254 bb_error_msg_and_die
252 ("suppressing non-delimited lines makes sense%s", _op_on_field); 255 ("suppressing non-delimited lines makes sense%s", _op_on_field);
253 } 256 }
254 if (opt & CUT_OPT_DELIM_FLGS) { 257 if (opt & OPT_DELIM) {
255 bb_error_msg_and_die 258 bb_error_msg_and_die
256 ("a delimiter may be specified%s", _op_on_field); 259 ("a delimiter may be specified%s", _op_on_field);
257 } 260 }
@@ -313,7 +316,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
313 /* now that the lists are parsed, we need to sort them to make life 316 /* now that the lists are parsed, we need to sort them to make life
314 * easier on us when it comes time to print the chars / fields / lines 317 * easier on us when it comes time to print the chars / fields / lines
315 */ 318 */
316 if (!(opt & CUT_OPT_NOSORT_FLGS)) 319 if (!(opt & OPT_NOSORT))
317 qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc); 320 qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
318 } 321 }
319 322