sort: add --sort=... option.
authorAndreas Schwab <schwab@suse.de>
Wed, 20 Feb 2008 07:36:56 +0000 (08:36 +0100)
committerJim Meyering <meyering@redhat.com>
Wed, 20 Feb 2008 07:41:29 +0000 (08:41 +0100)
* src/sort.c (SORT_OPTION): New enum.
(sort_args, sort_types): Define.
(usage, long_options, main): New option --sort.
* tests/sort/Test.pm: Test it.
* doc/coreutils.texi (sort invocation): Document --sort option.
* NEWS: Mention this.

NEWS
doc/coreutils.texi
src/sort.c
tests/sort/Test.pm

diff --git a/NEWS b/NEWS
index 233e514..5a5a0a0 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   join now verifies that the inputs are in sorted order.  This check can
   be turned off with the --nocheck-order option.
 
+  sort accepts the new option --sort=WORD, where WORD can be one of
+  general-numeric, month, numeric or random.  These are equivalent to the
+  options --general-numeric-sort/-g, --month-sort/-M, --numeric-sort/-n
+  and --random-sort/-R, resp.
+
 ** Improvements
 
   ls --color no longer outputs unnecessary escape sequences
index e8ccb4b..040a1de 100644 (file)
@@ -3536,8 +3536,10 @@ The @env{LC_CTYPE} locale determines character types.
 
 @item -g
 @itemx --general-numeric-sort
+@itemx --sort=general-numeric
 @opindex -g
 @opindex --general-numeric-sort
+@opindex --sort
 @cindex general numeric sort
 @vindex LC_NUMERIC
 Sort numerically, using the standard C function @code{strtod} to convert
@@ -3580,8 +3582,10 @@ This option has no effect if the stronger @option{--dictionary-order}
 
 @item -M
 @itemx --month-sort
+@itemx --sort=month
 @opindex -M
 @opindex --month-sort
+@opindex --sort
 @cindex months, sorting by
 @vindex LC_TIME
 An initial string, consisting of any amount of blanks, followed
@@ -3594,8 +3598,10 @@ can change this.
 
 @item -n
 @itemx --numeric-sort
+@itemx --sort=numeric
 @opindex -n
 @opindex --numeric-sort
+@opindex --sort
 @cindex numeric sort
 @vindex LC_NUMERIC
 Sort numerically.  The number begins each line and consists
@@ -3623,8 +3629,10 @@ appear earlier in the output instead of later.
 
 @item -R
 @itemx --random-sort
+@itemx --sort=random
 @opindex -R
 @opindex --random-sort
+@opindex --sort
 @cindex random sort
 Sort by hashing the input keys and then sorting the hash values.
 Choose the hash function at random, ensuring that it is free of
index 1183fc5..8b2eec5 100644 (file)
@@ -1,5 +1,5 @@
 /* sort - sort lines of text (with all kinds of options).
-   Copyright (C) 1988, 1991-2007 Free Software Foundation, Inc.
+   Copyright (C) 1988, 1991-2008 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -329,6 +329,9 @@ Ordering options:\n\
   -n, --numeric-sort          compare according to string numerical value\n\
   -R, --random-sort           sort by random hash of keys\n\
       --random-source=FILE    get random bytes from FILE (default /dev/urandom)\n\
+      --sort=WORD             sort according to WORD:\n\
+                                general-numeric -g, month -M, numeric -n,\n\
+                                random -R\n\
   -r, --reverse               reverse the result of comparisons\n\
 \n\
 "), stdout);
@@ -391,7 +394,8 @@ enum
 {
   CHECK_OPTION = CHAR_MAX + 1,
   COMPRESS_PROGRAM_OPTION,
-  RANDOM_SOURCE_OPTION
+  RANDOM_SOURCE_OPTION,
+  SORT_OPTION
 };
 
 static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z";
@@ -411,6 +415,7 @@ static struct option const long_options[] =
   {"numeric-sort", no_argument, NULL, 'n'},
   {"random-sort", no_argument, NULL, 'R'},
   {"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
+  {"sort", required_argument, NULL, SORT_OPTION},
   {"output", required_argument, NULL, 'o'},
   {"reverse", no_argument, NULL, 'r'},
   {"stable", no_argument, NULL, 's'},
@@ -434,6 +439,16 @@ static char const check_types[] =
 };
 ARGMATCH_VERIFY (check_args, check_types);
 
+static char const *const sort_args[] =
+{
+  "general-numeric", "month", "numeric", "random", NULL
+};
+static char const sort_types[] =
+{
+  'g', 'M', 'n', 'R'
+};
+ARGMATCH_VERIFY (sort_args, sort_types);
+
 /* The set of signals that are caught.  */
 static sigset_t caught_signals;
 
@@ -2902,6 +2917,9 @@ main (int argc, char **argv)
            files[nfiles++] = optarg;
          break;
 
+       case SORT_OPTION:
+         c = XARGMATCH ("--sort", optarg, sort_args, sort_types);
+         /* Fall through. */
        case 'b':
        case 'd':
        case 'f':
index e4d98be..0462973 100644 (file)
@@ -274,6 +274,7 @@ my @tv = (
 ["incompat4", '-c -o /dev/null', '', '', 2],
 ["incompat5", '-C -o /dev/null', '', '', 2],
 ["incompat6", '-cC', '', '', 2],
+["incompat7", '--sort=random -n', '', '', 2],
 
 # -t '\0' is accepted, as of coreutils-5.0.91
 ['nul-tab', "-k2,2 -t '\\0'", "a\0z\01\nb\0y\02\n", "b\0y\02\na\0z\01\n", 0],
@@ -289,6 +290,9 @@ my @tv = (
 # Exercise the code that enlarges the line buffer.  See the thread here:
 # http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/11006
 ['realloc-buf', '-S1', 'a'x4000 ."\n", 'a'x4000 ."\n", 0],
+
+["sort-numeric", '--sort=numeric', ".01\n0\n", "0\n.01\n", 0],
+["sort-gennum", '--sort=general-numeric', "1e2\n2e1\n", "2e1\n1e2\n", 0],
 );
 
 sub test_vector