* NEWS: Agument the new feature.
* doc/coreutils.texi (multi-call invocation): Document --list.
* src/coreutils.c (list_programs): A new helper to output
the built-in programs witha space or newline separator.
Also wrap the --help (spaced separated) output at 80 chars.

Link: https://github.com/coreutils/coreutils/issues/223
---
 NEWS               |  1 +
 doc/coreutils.texi | 10 ++++++-
 src/coreutils.c    | 68 ++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 67 insertions(+), 12 deletions(-)

diff --git a/NEWS b/NEWS
index fd3f34789..8e7098784 100644
--- a/NEWS
+++ b/NEWS
@@ -424,6 +424,7 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   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".
+  'coreutils --list' lists the built-in programs, one per line.
 
   '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 f79d864ae..18eb4cafe 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -1694,13 +1694,21 @@ 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 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 program accepts the following options.  Also see @ref{Common options}.
+
+@table @samp
+@optItem{coreutils,--list,}
+The @option{--list} option prints the names of the supported programs,
+one per line.
+@end table
+
 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 2583f86ac..10f852a8c 100644
--- a/src/coreutils.c
+++ b/src/coreutils.c
@@ -45,13 +45,46 @@
 #define AUTHORS \
   proper_name ("Alex Deymo")
 
+enum { LIST_OPTION = CHAR_MAX + 1 };
+
 static struct option const long_options[] =
 {
+  {"list", no_argument, NULL, LIST_OPTION},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
 };
 
+/* Print the program names separated by SEP, wrapping at 80 columns for ' '.  
*/
+static void
+list_programs (char sep)
+{
+  static char const names[] =
+#ifdef SINGLE_BINARY
+# define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) prog_name_str "\0"
+# include "coreutils.h"
+# undef SINGLE_BINARY_PROGRAM
+#endif
+    "";
+
+  size_t width = 0;
+  for (char const *p = names; *p; )
+    {
+      size_t len = strlen (p);
+      if (sep == ' ' && 80 < width + 1 + len)
+        {
+          putchar ('\n');
+          width = 0;
+        }
+      if (sep == ' ' || p != names)
+        putchar (sep);
+      fputs (p, stdout);
+      width += len + 1;
+      p += len + 1;
+    }
+  if (*names)
+    putchar ('\n');
+}
 
 void
 usage (int status)
@@ -62,23 +95,22 @@ usage (int status)
     {
       printf (_("\
 Usage: %s PROGRAM_NAME [PARAMETERS]...\n\
-  or:  %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]...\n"),
-              program_name, program_name);
+  or:  %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]...\n\
+  or:  %s OPTION\n"),
+              program_name, program_name, program_name);
       fputs (_("\
 Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\
 \n"), stdout);
+      oputs (_("\
+      --list\n\
+         list supported programs, one per line\n\
+"));
       oputs (HELP_OPTION_DESCRIPTION);
       oputs (VERSION_OPTION_DESCRIPTION);
 
 #ifdef SINGLE_BINARY
-      char const *prog_name_list =
-# define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) " " prog_name_str
-# include "coreutils.h"
-# undef SINGLE_BINARY_PROGRAM
-      ;
-      printf ("\n\
-Built-in programs:\n\
-%s\n", prog_name_list);
+      fputs ("\nSupported programs:\n", stdout);
+      list_programs (' ');
 #endif
 
       printf (_("\
@@ -193,13 +225,27 @@ main (int argc, char **argv)
   textdomain (PACKAGE);
   atexit (close_stdout);
 
-  if ((optc = getopt_long (argc, argv, "", long_options, NULL)) != -1)
+  bool list = false;
+  while ((optc = getopt_long (argc, argv, "", long_options, NULL)) != -1)
     switch (optc)
       {
+      case LIST_OPTION:
+        list = true;
+        break;
+
       case_GETOPT_HELP_CHAR;
 
       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+
+      default:
+        usage (EXIT_FAILURE);
       }
 
+  if (list)
+    {
+      list_programs ('\n');
+      return EXIT_SUCCESS;
+    }
+
   usage (EXIT_FAILURE);
 }
-- 
2.56.0


Reply via email to