>From 2f5c7eb053c6ab09fb9f622dcd0aec94c82db2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draig=20Brady?= Date: Sat, 5 May 2018 16:28:41 -0700 Subject: [PATCH] env: further tweaks to -S support Adjust error messages and --help. Diagnose common case where options passed to subsequent command. Add test case for that common use case. Remove use of @exdent from texinfo. Add Assaf to AUTHORS for env. Add `static` to two global variables. --- AUTHORS | 2 +- doc/coreutils.texi | 2 +- src/env.c | 30 +++++++++++++++--------------- tests/misc/env-S.pl | 7 ++++++- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/AUTHORS b/AUTHORS index 93d547b..de27076 100644 --- a/AUTHORS +++ b/AUTHORS @@ -26,7 +26,7 @@ dircolors: H. Peter Anvin dirname: David MacKenzie, Jim Meyering du: Torbjorn Granlund, David MacKenzie, Paul Eggert, Jim Meyering echo: Brian Fox, Chet Ramey -env: Richard Mlynarik, David MacKenzie +env: Richard Mlynarik, David MacKenzie, Assaf Gordon expand: David MacKenzie expr: Mike Parker, James Youngman, Paul Eggert factor: Paul Rubin, Torbjörn Granlund, Niels Möller diff --git a/doc/coreutils.texi b/doc/coreutils.texi index ec140d5..544937e 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -16903,7 +16903,7 @@ env -[v]S'[@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c env @end example -@exdent @command{env} is commonly used on first line of scripts (shebang line): +@command{env} is commonly used on first line of scripts (shebang line): @example #!/usr/bin/env @var{command} #!/usr/bin/env -[v]S[@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c diff --git a/src/env.c b/src/env.c index 74f50fd..97b6d6b 100644 --- a/src/env.c +++ b/src/env.c @@ -33,12 +33,13 @@ #define AUTHORS \ proper_name ("Richard Mlynarik"), \ - proper_name ("David MacKenzie") + proper_name ("David MacKenzie"), \ + proper_name ("Assaf Gordon") /* array of envvars to unset. */ static const char** usvars; -size_t usvars_alloc; -size_t usvars_used; +static size_t usvars_alloc; +static size_t usvars_used; /* Annotate the output with extra info to aid the user. */ static bool dev_debug; @@ -70,10 +71,7 @@ usage (int status) else { printf (_("\ -Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"), - program_name); - printf (_("\ - or: %s -[v]S'[OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]'\n"), +Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"), program_name); fputs (_("\ Set each NAME to VALUE in the environment and run COMMAND.\n\ @@ -90,8 +88,8 @@ Set each NAME to VALUE in the environment and run COMMAND.\n\ -C, --chdir=DIR change working directory to DIR\n\ "), stdout); fputs (_("\ - -S, --split-string=S process and split S into separate arguments\n\ - used to pass multiple arguments on shebang lines\n\ + -S, --split-string=S process and split S into separate arguments;\n\ + used to pass multiple arguments on shebang lines\n\ -v, --debug print verbose information for each processing step\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); @@ -571,13 +569,11 @@ main (int argc, char **argv) case '-': /* Space,tab,dash are undocumented options. Attempt to detect incorrect shebang usage with extraneous space, e.g.: - #!env -v -S'cat -n' - In which case argv[1] == "-v -S'cat -n'" k. - see: https://lists.gnu.org/r/coreutils/2018-04/msg00020.html */ + #!/usr/bin/env -i command + In which case argv[1] == "-i command". */ + error (0, 0, _("invalid option -- '%c'"), optc); if (argc == 3) - error (0, 0, _("possible incorrect usage of env in a script")); - else - error (0, 0, _("invalid option -- '%c'"), optc); + error (0, 0, _("use -[v]S to pass options in shebang lines")); usage (EXIT_CANCELED); case_GETOPT_HELP_CHAR; @@ -659,5 +655,9 @@ main (int argc, char **argv) int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE; error (0, errno, "%s", quote (argv[optind])); + + if (argc == 3 && exit_status == EXIT_ENOENT && strchr (argv[optind], ' ')) + error (0, 0, _("use -[v]S to pass options in shebang lines")); + return exit_status; } diff --git a/tests/misc/env-S.pl b/tests/misc/env-S.pl index bafd746..d3bfc46 100755 --- a/tests/misc/env-S.pl +++ b/tests/misc/env-S.pl @@ -226,8 +226,13 @@ my @Tests = # argv[1] = '-v -S cat -n' # argv[2] = './xxx' ['err_sp5', q['-v -S cat -n' ./xxx], {EXIT=>125}, - {ERR=>"env: possible incorrect usage of env in a script\n" . + {ERR=>"env: invalid option -- ' '\n" . + "env: use -[v]S to pass options in shebang lines\n" . "Try 'env --help' for more information.\n"}], + + ['err_sp6', q['cat -n' ./xxx], {EXIT=>127}, + {ERR=>"env: 'cat -n': No such file or directory\n" . + "env: use -[v]S to pass options in shebang lines\n"}], ); # Append a newline to end of each expected 'OUT' string. -- 2.9.3