diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2007-01-24 09:06:57 +0100 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2007-01-24 09:06:57 +0100 |
commit | a0b2bc8de7c1363f2fe85dd11d28d2b1e68c9d32 (patch) | |
tree | e88361b9764c7e197f7a6ecff2971555ee99c078 /src | |
parent | a0d74a99b786511d037b7fc00038c4cf55151c3a (diff) | |
download | coreutils-a0b2bc8de7c1363f2fe85dd11d28d2b1e68c9d32.tar.xz |
* NEWS: New option sort -C, proposed by XCU ERN 127, which looks
like it will be approved. Also add --check=quiet, --check=silent
as long aliases, and --check=diagnose-first as an alias for -c.
* doc/coreutils.texi (sort invocation): Document this.
Also, mention that sort -c can take at most one file.
* src/sort.c: Implement this.
Include argmatch.h.
(usage): Document the change.
(CHECK_OPTION): New constant.
(long_options): --check now takes an optional argument, and is now
treated differently from 'c'.
(check_args, check_types): New constant arrays.
(check): New arg CHECKONLY, which suppresses diagnostic if -C.
(main): Parse the new options.
* tests/sort/Test.pm (02d, 02d, incompat5, incompat6):
New tests for -C.
Diffstat (limited to 'src')
-rw-r--r-- | src/sort.c | 79 |
1 files changed, 54 insertions, 25 deletions
diff --git a/src/sort.c b/src/sort.c index 693b902f0..f571ecc91 100644 --- a/src/sort.c +++ b/src/sort.c @@ -28,6 +28,7 @@ #include <sys/wait.h> #include <signal.h> #include "system.h" +#include "argmatch.h" #include "error.h" #include "findprog.h" #include "hard-locale.h" @@ -336,7 +337,8 @@ Ordering options:\n\ fputs (_("\ Other options:\n\ \n\ - -c, --check check whether input is sorted; do not sort\n\ + -c, --check, --check=diagnose-first check for sorted input; do not sort\n\ + -C, --check=quiet, --check=silent like -c, but do not report first bad line\n\ -k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)\n\ -m, --merge merge already sorted files; do not sort\n\ -o, --output=FILE write result to FILE instead of standard output\n\ @@ -385,15 +387,16 @@ native byte values.\n\ non-character as a pseudo short option, starting with CHAR_MAX + 1. */ enum { - RANDOM_SOURCE_OPTION = CHAR_MAX + 1 + CHECK_OPTION = CHAR_MAX + 1, + RANDOM_SOURCE_OPTION }; -static char const short_options[] = "-bcdfgik:mMno:rRsS:t:T:uy:z"; +static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z"; static struct option const long_options[] = { {"ignore-leading-blanks", no_argument, NULL, 'b'}, - {"check", no_argument, NULL, 'c'}, + {"check", optional_argument, NULL, CHECK_OPTION}, {"dictionary-order", no_argument, NULL, 'd'}, {"ignore-case", no_argument, NULL, 'f'}, {"general-numeric-sort", no_argument, NULL, 'g'}, @@ -417,6 +420,16 @@ static struct option const long_options[] = {NULL, 0, NULL, 0}, }; +static char const *const check_args[] = +{ + "quiet", "silent", "diagnose-first", NULL +}; +static char const check_types[] = +{ + 'C', 'C', 'c' +}; +ARGMATCH_VERIFY (check_args, check_types); + /* The set of signals that are caught. */ static sigset_t caught_signals; @@ -1917,13 +1930,13 @@ compare (const struct line *a, const struct line *b) return reverse ? -diff : diff; } -/* Check that the lines read from FILE_NAME come in order. Print a - diagnostic (FILE_NAME, line number, contents of line) to stderr and return - false if they are not in order. Otherwise, print no diagnostic - and return true. */ +/* Check that the lines read from FILE_NAME come in order. Return + true if they are in order. If CHECKONLY == 'c', also print a + diagnostic (FILE_NAME, line number, contents of line) to stderr if + they are not in order. */ static bool -check (char const *file_name) +check (char const *file_name, char checkonly) { FILE *fp = xfopen (file_name, "r"); struct buffer buf; /* Input buffer. */ @@ -1949,15 +1962,19 @@ check (char const *file_name) { found_disorder: { - struct line const *disorder_line = line - 1; - uintmax_t disorder_line_number = - buffer_linelim (&buf) - disorder_line + line_number; - char hr_buf[INT_BUFSIZE_BOUND (uintmax_t)]; - fprintf (stderr, _("%s: %s:%s: disorder: "), - program_name, file_name, - umaxtostr (disorder_line_number, hr_buf)); - write_bytes (disorder_line->text, disorder_line->length, stderr, - _("standard error")); + if (checkonly == 'c') + { + struct line const *disorder_line = line - 1; + uintmax_t disorder_line_number = + buffer_linelim (&buf) - disorder_line + line_number; + char hr_buf[INT_BUFSIZE_BOUND (uintmax_t)]; + fprintf (stderr, _("%s: %s:%s: disorder: "), + program_name, file_name, + umaxtostr (disorder_line_number, hr_buf)); + write_bytes (disorder_line->text, disorder_line->length, + stderr, _("standard error")); + } + ordered = false; break; } @@ -2728,7 +2745,7 @@ main (int argc, char **argv) struct keyfield gkey; char const *s; int c = 0; - bool checkonly = false; + char checkonly = 0; bool mergeonly = false; char *random_source = NULL; bool need_random = false; @@ -2920,8 +2937,16 @@ main (int argc, char **argv) } break; + case CHECK_OPTION: + c = (optarg + ? XARGMATCH ("--check", optarg, check_args, check_types) + : 'c'); + /* Fall through. */ case 'c': - checkonly = true; + case 'C': + if (checkonly && checkonly != c) + incompatible_options ("cC"); + checkonly = c; break; case 'k': @@ -3128,15 +3153,19 @@ main (int argc, char **argv) if (checkonly) { if (nfiles > 1) - error (SORT_FAILURE, 0, _("extra operand %s not allowed with -c"), - quote (files[1])); + error (SORT_FAILURE, 0, _("extra operand %s not allowed with -%c"), + quote (files[1]), checkonly); if (outfile) - incompatible_options ("co"); + { + static char opts[] = {0, 'o', 0}; + opts[0] = checkonly; + incompatible_options (opts); + } - /* POSIX requires that sort return 1 IFF invoked with -c and the + /* POSIX requires that sort return 1 IFF invoked with -c or -C and the input is not properly sorted. */ - exit (check (files[0]) ? EXIT_SUCCESS : SORT_OUT_OF_ORDER); + exit (check (files[0], checkonly) ? EXIT_SUCCESS : SORT_OUT_OF_ORDER); } if (mergeonly) |