From 747e296ff656813340c9355d98b0a13cba8473bc Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 4 Dec 2018 21:29:51 -0600 Subject: Add FLAG(x) macro, expanding to (toys.optflags & FLAG_##x) --- toys.h | 2 ++ toys/posix/sed.c | 19 +++++++++---------- toys/posix/sort.c | 17 ++++++++--------- toys/posix/strings.c | 5 ++--- toys/posix/tail.c | 11 +++++------ toys/posix/tee.c | 4 ++-- toys/posix/time.c | 2 +- toys/posix/touch.c | 13 ++++++------- toys/posix/ulimit.c | 14 +++++++------- toys/posix/uniq.c | 13 ++++++------- toys/posix/uuencode.c | 2 +- toys/posix/wc.c | 4 ++-- toys/posix/who.c | 2 +- toys/posix/xargs.c | 2 +- 14 files changed, 53 insertions(+), 57 deletions(-) diff --git a/toys.h b/toys.h index 5e71e3fe..4083725a 100644 --- a/toys.h +++ b/toys.h @@ -119,6 +119,8 @@ extern char toybuf[4096], libbuf[4096]; extern char **environ; +#define FLAG(x) (toys.optflags&FLAG_##x) + #define GLOBALS(...) #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array)) #define TAGGED_ARRAY(X, ...) {__VA_ARGS__} diff --git a/toys/posix/sed.c b/toys/posix/sed.c index eadac885..03179ca2 100644 --- a/toys/posix/sed.c +++ b/toys/posix/sed.c @@ -260,7 +260,7 @@ static void sed_line(char **pline, long plen) int eol = 0, tea = 0; // Ignore EOF for all files before last unless -i - if (!pline && !(toys.optflags&FLAG_i)) return; + if (!pline && !FLAG(i)) return; // Grab next line for deferred processing (EOF detection: we get a NULL // pline at EOF to flush last line). Note that only end of _last_ input @@ -600,7 +600,7 @@ writenow: command = command->next; } - if (line && !(toys.optflags & FLAG_n)) emit(line, len, eol); + if (line && !FLAG(n)) emit(line, len, eol); done: if (dlist_terminate(append)) while (append) { @@ -627,10 +627,9 @@ done: // Callback called on each input file static void do_sed_file(int fd, char *name) { - int i = toys.optflags & FLAG_i; char *tmp; - if (i) { + if (FLAG(i)) { struct sedcmd *command; if (!fd) return error_msg("-i on stdin"); @@ -640,7 +639,7 @@ static void do_sed_file(int fd, char *name) command->hit = 0; } do_lines(fd, sed_line); - if (i) { + if (FLAG(i)) { replace_tempfile(-1, TT.fdout, &tmp); TT.fdout = 1; TT.nextline = 0; @@ -780,7 +779,7 @@ static void parse_pattern(char **pline, long len) if (!(s = unescape_delimited_string(&line, 0))) goto error; if (!*s) command->rmatch[i] = 0; else { - xregcomp((void *)reg, s, (toys.optflags & FLAG_r)*REG_EXTENDED); + xregcomp((void *)reg, s, FLAG(r)*REG_EXTENDED); command->rmatch[i] = reg-toybuf; reg += sizeof(regex_t); } @@ -874,7 +873,7 @@ resume_s: // allocating the space was done by extend_string() above if (!*TT.remember) command->arg1 = 0; else xregcomp((void *)(command->arg1 + (char *)command), TT.remember, - ((toys.optflags & FLAG_r)*REG_EXTENDED)|((command->sflags&1)*REG_ICASE)); + (FLAG(r)*REG_EXTENDED)|((command->sflags&1)*REG_ICASE)); free(TT.remember); TT.remember = 0; if (*line == 'w') { @@ -995,13 +994,13 @@ void sed_main(void) // Lie to autoconf when it asks stupid questions, so configure regexes // that look for "GNU sed version %f" greater than some old buggy number // don't fail us for not matching their narrow expectations. - if (toys.optflags & FLAG_version) { + if (FLAG(version)) { xprintf("This is not GNU sed version 9.0\n"); return; } // Handling our own --version means we handle our own --help too. - if (toys.optflags&FLAG_help) help_exit(0); + if (FLAG(help)) help_exit(0); // Parse pattern into commands. @@ -1027,7 +1026,7 @@ void sed_main(void) loopfiles_rw(args, O_RDONLY|WARN_ONLY, 0, do_sed_file); // Provide EOF flush at end of cumulative input for non-i mode. - if (!(toys.optflags & FLAG_i)) { + if (!FLAG(i)) { toys.optflags |= FLAG_i; sed_line(0, 0); } diff --git a/toys/posix/sort.c b/toys/posix/sort.c index c33b8ff0..2f4d49a0 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -250,7 +250,7 @@ static int compare_keys(const void *xarg, const void *yarg) // Perform fallback sort if necessary (always case insensitive, no -f, // the point is to get a stable order even for -f sorts) - if (!retval && !(toys.optflags&FLAG_s)) { + if (!retval && !FLAG(s)) { flags = toys.optflags; retval = strcmp(xx, yy); } @@ -264,14 +264,13 @@ static void sort_read(int fd, char *name) // Read each line from file, appending to a big array. for (;;) { - char * line = (toys.optflags&FLAG_z) - ? get_rawline(fd, NULL, 0) : get_line(fd); + char * line = FLAG(z) ? get_rawline(fd, NULL, 0) : get_line(fd); if (!line) break; // handle -c here so we don't allocate more memory than necessary. - if (toys.optflags&FLAG_c) { - int j = (toys.optflags&FLAG_u) ? -1 : 0; + if (FLAG(c)) { + int j = FLAG(u) ? -1 : 0; if (TT.lines && compare_keys((void *)&TT.lines, &line)>j) error_exit("%s: Check line %d\n", name, TT.linecount); @@ -343,7 +342,7 @@ void sort_main(void) } // global b flag strips both leading and trailing spaces - if (toys.optflags&FLAG_b) toys.optflags |= FLAG_bb; + if (FLAG(b)) toys.optflags |= FLAG_bb; // If no keys, perform alphabetic sort over the whole line. if (!TT.key_list) add_key()->range[0] = 1; @@ -353,13 +352,13 @@ void sort_main(void) // The compare (-c) logic was handled in sort_read(), // so if we got here, we're done. - if (toys.optflags&FLAG_c) goto exit_now; + if (FLAG(c)) goto exit_now; // Perform the actual sort qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys); // handle unique (-u) - if (toys.optflags&FLAG_u) { + if (FLAG(u)) { int jdx; for (jdx=0, idx=1; idxtv_nsec = 0; // List of search types - if (toys.optflags & FLAG_d) { + if (FLAG(d)) { format = (char *[]){"%Y-%m-%dT%T", "%Y-%m-%d %T", 0}; date = TT.d; } else { @@ -67,7 +67,7 @@ void touch_main(void) } while (*format) { - if (toys.optflags&FLAG_t) { + if (FLAG(t)) { s = strchr(date, '.'); if ((s ? s-date : strlen(date)) != strlen(*format)) { format++; @@ -83,7 +83,7 @@ void touch_main(void) // parse nanoseconds if (s && *s=='.' && isdigit(s[1])) { s++; - if (toys.optflags&FLAG_t) + if (FLAG(t)) if (1 == sscanf(s, "%2u%n", &(tm.tm_sec), &len)) s += len; if (1 == sscanf(s, "%lu%n", &ts->tv_nsec, &len)) { s += len; @@ -122,9 +122,8 @@ void touch_main(void) if (!futimens(1, ts)) continue; } else { // cheat: FLAG_h is rightmost flag, so its value is 1 - if (!utimensat(AT_FDCWD, s, ts, - (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) continue; - if (toys.optflags & FLAG_c) continue; + if (!utimensat(AT_FDCWD, s, ts, FLAG(h)*AT_SYMLINK_NOFOLLOW)) continue; + if (FLAG(c)) continue; if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) { close(fd); if (toys.optflags) ss--; diff --git a/toys/posix/ulimit.c b/toys/posix/ulimit.c index 4fa01618..da7f2ead 100644 --- a/toys/posix/ulimit.c +++ b/toys/posix/ulimit.c @@ -68,10 +68,10 @@ void ulimit_main(void) RLIMIT_CPU, RLIMIT_NPROC, RLIMIT_AS}; if (!(toys.optflags&(FLAG_H-1))) toys.optflags |= FLAG_f; - if ((toys.optflags&(FLAG_a|FLAG_p)) && toys.optc) error_exit("can't set -ap"); + if ((FLAG(a)||FLAG(p)) && toys.optc) error_exit("can't set -ap"); // Fetch data - if (!(toys.optflags&FLAG_P)) TT.P = getppid(); + if (!FLAG(P)) TT.P = getppid(); for (i=0; i= 1) infile = xfopen(toys.optargs[0], "r"); if (toys.optc >= 2) outfile = xfopen(toys.optargs[1], "w"); - if (toys.optflags & FLAG_z) eol = 0; + if (FLAG(z)) eol = 0; // If first line can't be read if (getdelim(&prevline, &prevsize, eol, infile) < 0) return; @@ -84,9 +84,8 @@ void uniq_main(void) } if (!TT.w) - diff = !(toys.optflags & FLAG_i) ? strcmp(t1, t2) : strcasecmp(t1, t2); - else diff = !(toys.optflags & FLAG_i) ? strncmp(t1, t2, TT.w) - : strncasecmp(t1, t2, TT.w); + diff = !FLAG(i) ? strcmp(t1, t2) : strcasecmp(t1, t2); + else diff = !FLAG(i) ? strncmp(t1, t2, TT.w) : strncasecmp(t1, t2, TT.w); if (!diff) TT.repeats++; else { diff --git a/toys/posix/uuencode.c b/toys/posix/uuencode.c index f0e5be12..44e38bcb 100644 --- a/toys/posix/uuencode.c +++ b/toys/posix/uuencode.c @@ -24,7 +24,7 @@ void uuencode_main(void) { char *name = toys.optargs[toys.optc-1], buf[(76/4)*3]; - int i, m = toys.optflags & FLAG_m, fd = 0; + int i, m = FLAG(m), fd = 0; if (toys.optc > 1) fd = xopenro(toys.optargs[0]); diff --git a/toys/posix/wc.c b/toys/posix/wc.c index 5d362e7d..910e4690 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -42,7 +42,7 @@ static void show_lengths(unsigned long *lengths, char *name) // And, yes, folks have test scripts that rely on all this nonsense :-( // Note: sufficiently modern versions of coreutils wc will use the smallest // column width necessary to have all columns be equal width rather than 0. - if (!(toys.optc==0 && (toys.optflags & (toys.optflags-1))==0) && toys.optc!=1) + if (!(!toys.optc && !(toys.optflags & (toys.optflags-1))) && toys.optc!=1) space = 7; for (i = 0; i<4; i++) { @@ -82,7 +82,7 @@ static void do_wc(int fd, char *name) for (pos = 0; posut_type == USER_PROCESS) { + if (FLAG(a) || entry->ut_type == USER_PROCESS) { time_t time; int time_size; char *times; diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c index aaa0ac79..d25a2e3f 100644 --- a/toys/posix/xargs.c +++ b/toys/posix/xargs.c @@ -120,7 +120,7 @@ void xargs_main(void) posix_max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes() - 2048; if (!TT.s || TT.s > posix_max_bytes) TT.s = posix_max_bytes; - if (!(toys.optflags & FLAG_0)) TT.delim = '\n'; + if (!FLAG(0)) TT.delim = '\n'; // If no optargs, call echo. if (!toys.optc) { -- cgit v1.2.3