cut: simplify OPT_ names, eliminate one variable

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2024-12-10 01:38:31 +01:00
parent f020414413
commit 73e9d25d75

View file

@ -65,14 +65,14 @@ typedef struct { int rm_eo, rm_so; } regmatch_t;
/* option vars */ /* option vars */
#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n" #define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
#define CUT_OPT_BYTE_FLGS (1 << 0) #define OPT_BYTE (1 << 0)
#define CUT_OPT_CHAR_FLGS (1 << 1) #define OPT_CHAR (1 << 1)
#define CUT_OPT_FIELDS_FLGS (1 << 2) #define OPT_FIELDS (1 << 2)
#define CUT_OPT_DELIM_FLGS (1 << 3) #define OPT_DELIM (1 << 3)
#define CUT_OPT_ODELIM_FLGS (1 << 4) #define OPT_ODELIM (1 << 4)
#define CUT_OPT_SUPPRESS_FLGS (1 << 5) #define OPT_SUPPRESS (1 << 5)
#define CUT_OPT_NOSORT_FLGS (1 << 6) #define OPT_NOSORT (1 << 6)
#define CUT_OPT_REGEX_FLGS ((1 << 7) * ENABLE_FEATURE_CUT_REGEX) #define OPT_REGEX ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
struct cut_list { struct cut_list {
int startpos; int startpos;
@ -88,12 +88,14 @@ static int cmpfunc(const void *a, const void *b)
static void cut_file(FILE *file, const char *delim, const char *odelim, static void cut_file(FILE *file, const char *delim, const char *odelim,
const struct cut_list *cut_lists, unsigned nlists) const struct cut_list *cut_lists, unsigned nlists)
{ {
#define opt_REGEX (option_mask32 & OPT_REGEX)
char *line; char *line;
unsigned linenum = 0; /* keep these zero-based to be consistent */ unsigned linenum = 0; /* keep these zero-based to be consistent */
regex_t reg; regex_t reg;
int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS; int spos;
if (shoe) xregcomp(&reg, delim, REG_EXTENDED); if (opt_REGEX)
xregcomp(&reg, delim, REG_EXTENDED);
/* go through every line in the file */ /* go through every line in the file */
while ((line = xmalloc_fgetline(file)) != NULL) { while ((line = xmalloc_fgetline(file)) != NULL) {
@ -105,7 +107,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
unsigned cl_pos = 0; unsigned cl_pos = 0;
/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) { if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
/* print the chars specified in each cut list */ /* print the chars specified in each cut list */
for (; cl_pos < nlists; cl_pos++) { for (; cl_pos < nlists; cl_pos++) {
for (spos = cut_lists[cl_pos].startpos; spos < linelen;) { 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,
/* Blank line? Check -s (later check for -s does not catch empty lines) */ /* Blank line? Check -s (later check for -s does not catch empty lines) */
if (linelen == 0) { if (linelen == 0) {
if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) if (option_mask32 & OPT_SUPPRESS)
goto next_line; goto next_line;
} }
@ -164,22 +166,22 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
if (end == linelen || dcount > cut_lists[cl_pos].endpos) { if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
if (++cl_pos >= nlists) if (++cl_pos >= nlists)
break; break;
if (option_mask32 & CUT_OPT_NOSORT_FLGS) if (option_mask32 & OPT_NOSORT)
start = dcount = uu = 0; start = dcount = uu = 0;
end = 0; end = 0;
} }
/* End of current line? */ /* End of current line? */
if (uu == linelen) { if (uu == linelen) {
/* If we've seen no delimiters, check -s */ /* If we've seen no delimiters, check -s */
if (!cl_pos && !dcount && !shoe) { if (!cl_pos && !dcount && !opt_REGEX) {
if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) if (option_mask32 & OPT_SUPPRESS)
goto next_line; goto next_line;
} else if (dcount < cut_lists[cl_pos].startpos) } else if (dcount < cut_lists[cl_pos].startpos)
start = linelen; start = linelen;
end = linelen; end = linelen;
} else { } else {
/* Find next delimiter */ /* Find next delimiter */
if (shoe) { if (opt_REGEX) {
regmatch_t rr = {-1, -1}; regmatch_t rr = {-1, -1};
if (!regexec(&reg, line + uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) { 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,
continue; continue;
} }
} }
if (end != start || !shoe) if (end != start || !opt_REGEX)
printf("%s%.*s", out++ ? odelim : "", end - start, line + start); printf("%s%.*s", out++ ? odelim : "", end - start, line + start);
start = uu; start = uu;
if (!dcount) if (!dcount)
@ -215,6 +217,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
free(printed); free(printed);
free(orig_line); free(orig_line);
} }
#undef opt_REGEX
} }
int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@ -235,23 +238,23 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
&sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt) &sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt)
); );
if (!delim || !*delim) if (!delim || !*delim)
delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t"; delim = (opt & OPT_REGEX) ? "[[:space:]]+" : "\t";
if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim; if (!odelim) odelim = (opt & OPT_REGEX) ? " " : delim;
// argc -= optind; // argc -= optind;
argv += optind; argv += optind;
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS))) if (!(opt & (OPT_BYTE | OPT_CHAR | OPT_FIELDS | OPT_REGEX)))
bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields"); bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
/* non-field (char or byte) cutting has some special handling */ /* non-field (char or byte) cutting has some special handling */
if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) { if (!(opt & (OPT_FIELDS|OPT_REGEX))) {
static const char _op_on_field[] ALIGN1 = " only when operating on fields"; static const char _op_on_field[] ALIGN1 = " only when operating on fields";
if (opt & CUT_OPT_SUPPRESS_FLGS) { if (opt & OPT_SUPPRESS) {
bb_error_msg_and_die bb_error_msg_and_die
("suppressing non-delimited lines makes sense%s", _op_on_field); ("suppressing non-delimited lines makes sense%s", _op_on_field);
} }
if (opt & CUT_OPT_DELIM_FLGS) { if (opt & OPT_DELIM) {
bb_error_msg_and_die bb_error_msg_and_die
("a delimiter may be specified%s", _op_on_field); ("a delimiter may be specified%s", _op_on_field);
} }
@ -313,7 +316,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
/* now that the lists are parsed, we need to sort them to make life /* now that the lists are parsed, we need to sort them to make life
* easier on us when it comes time to print the chars / fields / lines * easier on us when it comes time to print the chars / fields / lines
*/ */
if (!(opt & CUT_OPT_NOSORT_FLGS)) if (!(opt & OPT_NOSORT))
qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc); qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
} }