I.e. support `coreutils PROG` in addition to the existing
`coreutils --coreutils-prog=PROG` form. This is more natural and
provides better compat with other implementations.  Note this restricts
one to naming the multi-call binary ending with "coreutils".

* NEWS: Mention the new feature.
* doc/coreutils.texi (coreutils invocation): Mention the new
explicit invocation form.
* src/coreutils.c (main): Accept program name as the second arg.
* tests/misc/coreutils.sh: Add test cases.
---
 NEWS                    |  4 ++++
 doc/coreutils.texi      | 14 +++++++++++---
 src/coreutils.c         | 26 +++++++++++++-------------
 tests/misc/coreutils.sh | 20 ++++++++++++++++++++
 4 files changed, 48 insertions(+), 16 deletions(-)

diff --git a/NEWS b/NEWS
index dc24ae9b9..fd3f34789 100644
--- a/NEWS
+++ b/NEWS
@@ -421,6 +421,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 ** New Features
 
+  The multi-call binary accepts 'coreutils PROGRAM [ARG]...' as a shorthand
+  for 'coreutils --coreutils-prog=PROGRAM [ARG]...', as long as the
+  multi-call binary is named or has a name ending with "coreutils".
+
   'env' and 'printenv' now support the --quoting-style option
   to control the format of printed environment variables.
 
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 98b25fc50..f79d864ae 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -1686,13 +1686,21 @@ you can work around the compatibility problems by 
setting
 
 The @command{coreutils} command invokes an individual utility, either
 implicitly selected by the last component of the name used to invoke
-@command{coreutils}, or explicitly with the
-@option{--coreutils-prog} option.  Synopsis:
+@command{coreutils}, or explicitly with these invocations.
+Synopsis:
 
 @example
-coreutils @option{--coreutils-prog=PROGRAM} @dots{}
+coreutils @var{program} @dots{}
+coreutils @option{--coreutils-prog=@var{program}} @dots{}
 @end example
 
+The first explicit form, e.g. @samp{coreutils ls} requires that
+the multi-call binary is invoked through name ending in @samp{coreutils}.
+
+The second explicit form with the @option{--coreutils-prog=@var{program}} 
option
+gives the additional flexibility of invoking the multi-call binary
+through a name that does not end with @samp{coreutils}.
+
 The @command{coreutils} command is not installed by default, so
 portable scripts should not rely on its existence.
 
diff --git a/src/coreutils.c b/src/coreutils.c
index 26a43ce64..2583f86ac 100644
--- a/src/coreutils.c
+++ b/src/coreutils.c
@@ -61,8 +61,9 @@ usage (int status)
   else
     {
       printf (_("\
-Usage: %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]... \n"),
-              program_name);
+Usage: %s PROGRAM_NAME [PARAMETERS]...\n\
+  or:  %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]...\n"),
+              program_name, program_name);
       fputs (_("\
 Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\
 \n"), stdout);
@@ -82,7 +83,7 @@ Built-in programs:\n\
 
       printf (_("\
 \n\
-Use: '%s --coreutils-prog=PROGRAM_NAME --help' for individual program 
help.\n"),
+Use: '%s PROGRAM_NAME --help' for individual program help.\n"),
               program_name);
       emit_ancillary_info (PROGRAM_NAME);
     }
@@ -136,17 +137,11 @@ main (int argc, char **argv)
   char *prog_name = last_component (argv[0]);
   int optc;
 
-  /* If this program is called directly as "coreutils" or if the value of
-     argv[0] is an unknown tool (which "coreutils" is), we proceed and parse
-     the options.  */
+  /* A built-in selected by argv[0] takes precedence over argv[1].  */
   launch_program (prog_name, argc, argv);
 
-  /* No known program was selected via argv[0].  Try parsing the first
-     argument as --coreutils-prog=PROGRAM to determine the program.  The
-     invocation for this case should be:
-       path/to/coreutils --coreutils-prog=someprog someprog ...
-     The third argument is what the program will see as argv[0].  */
-
+  /* Accept a positional program name only when invoked as the dispatcher.  */
+  bool is_coreutils = str_endswith (prog_name, PROGRAM_NAME);
   size_t nskip = 0;
   if (argc >= 2)
     {
@@ -168,6 +163,11 @@ main (int argc, char **argv)
           else
             arg_name = prog_name;
         }
+      else if (is_coreutils && argv[1][0] != '-')
+        {
+          nskip = 1;
+          arg_name = prog_name = argv[1];
+        }
 
       if (nskip)
         {
@@ -178,7 +178,7 @@ main (int argc, char **argv)
 
   /* Only process options if calling multi-call binary directly,
      otherwise `foo --version` would succeed.  */
-  if (nskip || (prog_name && !str_endswith (prog_name, "coreutils")))
+  if (nskip || !is_coreutils)
     {
       fprintf (stderr, _("%s: unknown program %s\n"),
                PROGRAM_NAME, quote (prog_name));
diff --git a/tests/misc/coreutils.sh b/tests/misc/coreutils.sh
index 73cc177dc..4138cb963 100755
--- a/tests/misc/coreutils.sh
+++ b/tests/misc/coreutils.sh
@@ -27,12 +27,28 @@ echo 'y' > exp &&
 coreutils --coreutils-prog=yes | head -n10 | uniq > out || framework_failure_
 compare exp out || fail=1
 
+# A dispatcher name, including a prefixed one, takes a program operand.
+ln -s blah gcoreutils || framework_failure_
+echo 'two words ls' > exp || framework_failure_
+for cu in coreutils ./gcoreutils; do
+  $cu yes 'two words' ls | head -n1 > out
+  compare exp out || fail=1
+done
+
 # The installed name 'install' selects the ginstall built-in.
 if coreutils --coreutils-prog=ginstall --version > exp 2>/dev/null; then
   coreutils --coreutils-prog=install --version > out || fail=1
   compare exp out || fail=1
+  coreutils install --version > out || fail=1
+  compare exp out || fail=1
 fi
 
+# A link named after a built-in passes even a known program name through.
+ln -s blah yes || framework_failure_
+echo yes > exp || framework_failure_
+./yes yes | head -n1 > out
+compare exp out || fail=1
+
 # Ensure empty arg is rejected
 returns_ 1 coreutils $unset_command || fail=1
 
@@ -41,10 +57,14 @@ echo "coreutils: unknown program 'blah'" > exp || 
framework_failure_
 
 returns_ 1 coreutils --coreutils-prog='blah' --help 2>err || fail=1
 compare exp err || fail=1
+returns_ 1 coreutils blah --help 2>err || fail=1
+compare exp err || fail=1
 
 returns_ 1 ./blah 2>err || fail=1
 compare exp err || fail=1
 returns_ 1 ./blah --version 2>err || fail=1
 compare exp err || fail=1
+returns_ 1 ./blah yes --version 2>err || fail=1
+compare exp err || fail=1
 
 Exit $fail
-- 
2.56.0


Reply via email to