From 41d43d8a53d0a44305005fca02f01bbf5e016800 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 11 Aug 2022 22:11:37 -0500 Subject: [PATCH] Use TOYFLAG_ARGFAIL() value for error_msg(), and add sort -C in passing. --- lib/lib.c | 2 +- tests/sort.test | 4 ++++ toys/posix/nohup.c | 11 ++++------- toys/posix/sort.c | 23 +++++++++++++---------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 770032e6..33470110 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -17,7 +17,7 @@ void verror_msg(char *msg, int err, va_list va) if (err<0 && CFG_TOYBOX_HELP) fprintf(stderr, " (see \"%s --help\")", toys.which->name); if (msg || err) putc('\n', stderr); - if (!toys.exitval) toys.exitval++; + if (!toys.exitval) toys.exitval = (toys.which->flags>>24) ? : 1; } // These functions don't collapse together because of the va_stuff. diff --git a/tests/sort.test b/tests/sort.test index 5fe925e1..3e1e4ee0 100755 --- a/tests/sort.test +++ b/tests/sort.test @@ -104,6 +104,10 @@ toyonly testing "-kx" "sort -k1,1x" "3\na\n0c\n" "" "0c\na\n3\n" # "toy-2.37.tar.gz\ntoy-3.4.tar.gz\ntoy-3.12.tar.gz\ntoy-4.16-rc2.tar.gz\ntoy-4.16.tar.gz\n" "" \ # "toy-3.12.tar.gz\ntoy-2.37.tar.gz\ntoy-3.4.tar.gz\ntoy-4.16-rc2.tar.gz\ntoy-4.16.tar.gz" +testcmd "-c" "-uc |& grep -o [0-9]*" "3\n" "" "a\nb\nb\nc" +testcmd "-C 1" "-C || echo yes" "yes\n" "" "one\ntwo\nthree" +testcmd "-C 2" "-C && echo yes" "yes\n" "" "a\nb\nc\n" + optional SORT_FLOAT # not numbers < NaN < -infinity < numbers < +infinity diff --git a/toys/posix/nohup.c b/toys/posix/nohup.c index 90d374ef..e39b6b60 100644 --- a/toys/posix/nohup.c +++ b/toys/posix/nohup.c @@ -21,17 +21,14 @@ config NOHUP void nohup_main(void) { - toys.exitval = 125; xsignal(SIGHUP, SIG_IGN); if (isatty(1)) { close(1); - if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY, - S_IRUSR|S_IWUSR )) - { + if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY, S_IRUSR|S_IWUSR)) { char *temp = getenv("HOME"); - temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out"); - xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600); + xcreate(temp ? temp = xmprintf("%s/nohup.out", temp) : "nohup.out", + O_CREAT|O_APPEND|O_WRONLY, 0600); free(temp); } } @@ -39,6 +36,6 @@ void nohup_main(void) close(0); xopen_stdio("/dev/null", O_RDONLY); } - toys.exitval = 0; + xexec(toys.optargs); } diff --git a/toys/posix/sort.c b/toys/posix/sort.c index c06b2a5f..ec30c056 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -7,7 +7,7 @@ * Deviations from POSIX: Lots. * We invented -x -USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")"S:T:m" "o:k*t:" "xVbMcszdfirun", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2))) +USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")"S:T:m" "o:k*t:" "xVbMCcszdfirun", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2))) config SORT bool "sort" @@ -21,7 +21,8 @@ config SORT -u Unique lines only -n Numeric order (instead of alphabetical) -b Ignore leading blanks (or trailing blanks in second part of key) - -c Check whether input is sorted + -C Check whether input is sorted + -c Warn if input is unsorted -d Dictionary order (use alphanumeric and whitespace chars only) -f Force uppercase (case insensitive sort) -i Ignore nonprinting characters @@ -282,9 +283,12 @@ static void sort_lines(char **pline, long len) *pline = 0; // handle -c here so we don't allocate more memory than necessary. - if (FLAG(c)) { - if (TT.lines && compare_keys((void *)&TT.lines, &line)>-!!FLAG(u)) - error_exit("%s: Check line %u\n", TT.name, TT.linecount); + if (FLAG(C)||FLAG(c)) { + if (TT.lines && compare_keys((void *)&TT.lines, &line)>-!!FLAG(u)) { + toys.exitval = 1; + if (FLAG(C)) xexit(); + error_exit("%s: Check line %u", TT.name, TT.linecount); + } free(TT.lines); TT.lines = (void *)line; } else { @@ -299,7 +303,7 @@ static void sort_lines(char **pline, long len) static void sort_read(int fd, char *name) { TT.name = name; - do_lines(fd, FLAG(z) ? '\0' : '\n', sort_lines); + do_lines(fd, '\n'*!FLAG(z), sort_lines); } void sort_main(void) @@ -339,10 +343,9 @@ void sort_main(void) flag = 1<<(optlist-temp2+strlen(optlist)-1); // Was it a flag that can apply to a key? - if (!temp2 || flag>FLAG_x || (flag&(FLAG_u|FLAG_c|FLAG_s|FLAG_z))) { - toys.exitval = 2; + if (!temp2 || flag>FLAG_x || (flag&(FLAG_u|FLAG_c|FLAG_s|FLAG_z))) error_exit("Unknown key option."); - } + // b after , means strip _trailing_ space, not leading. if (idx && flag==FLAG_b) flag = FLAG_bb; key->flags |= flag; @@ -362,7 +365,7 @@ void sort_main(void) // The compare (-c) logic was handled in sort_read(), // so if we got here, we're done. - if (FLAG(c)) goto exit_now; + if (FLAG(C)||FLAG(c)) goto exit_now; // Perform the actual sort qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys); -- 2.39.2