From bfea9c150229db14316262c1c05198c7caeb9538 Mon Sep 17 00:00:00 2001 From: James Youngman Date: Sat, 16 Jul 2016 15:10:32 +0100 Subject: [PATCH] xargs: add new option --interactive-default-yes, modifying --interactive. To: address@hidden * xargs/xargs.c: Add a new static boolean interactive_default_yes which is set when the --interactive-default-yes option is in effect. (longopts): add --interactive-default-yes. (main): Set interactive_default_yes when --interactive-default-yes is used. (xargs_do_exec): Pass interactive_default_yes to print_args. (print_args): Accept just RETURN as being a positive response when interactive_default_yes is in effect. Also specify /dev/tty in the error message when we fail to read from it, instead of incorrectly stating that we failed to read from stdin. (usage): Mention --interactive-default-yes. * xargs/xargs.1: Document the new option --interactive-default-yes. * doc/find.texi (Querying): Document --interactive-default-yes. (xargs options): Likewise. * NEWS: Mention this improvement. --- NEWS | 5 +++++ doc/find.texi | 16 ++++++++++++++++ xargs/xargs.1 | 13 ++++++++++++- xargs/xargs.c | 31 +++++++++++++++++++++---------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index bccb19a..251a527 100644 --- a/NEWS +++ b/NEWS @@ -35,6 +35,11 @@ of the - yet more portable - '( -type l -o -type d )'. find now diagnoses failures returned by readdir(). This bug was inherent in the use of FTS. +The new option --interactive-default-yes for xargs modifies the way +that xargs behaves when the user's response is just to press the +return key. Without this option (and before this change) the command +is not run. With this option, the command is run. + ** Bug Fixes #48180: find -noop (an internal option not intended to be exposed to the user) diff --git a/doc/find.texi b/doc/find.texi index b3c5d10..546ef00 100644 --- a/doc/find.texi +++ b/doc/find.texi @@ -2772,6 +2772,15 @@ from the terminal. Only run the command line if the response starts with @samp{y} or @samp{Y}. Implies @samp{-t}. @end table address@hidden @code address@hidden --interactive-default-yes +Normally a response @samp{y} or @samp{Y} is required for address@hidden to run the command. When address@hidden is specified, just pressing return +has the same effect as pressing @samp{y} then return. +This option does not imply @samp{--interactive}. address@hidden table + @node Delete Files @section Delete Files @@ -3729,6 +3738,13 @@ terminate arguments; instead, the input is split at newlines only. If Implies @samp{-x} and @samp{-l 1}. The @samp{-i} option is deprecated in favour of the @samp{-I} option. address@hidden --interactive-default-yes +Normally a response @samp{y} or @samp{Y} is required for address@hidden to run the command. When address@hidden is specified, just pressing return +has the same effect as pressing @samp{y} then return. Does not imply address@hidden + @item -L @var{max-lines} @itemx address@hidden@address@hidden @itemx address@hidden@address@hidden diff --git a/xargs/xargs.1 b/xargs/xargs.1 index 6837786..024dfd1 100644 --- a/xargs/xargs.1 +++ b/xargs/xargs.1 @@ -25,6 +25,7 @@ xargs \- build and execute command lines from standard input [\fB\-\-max\-procs\fR=\fImax-procs\fR] [\fB\-\-process\-slot\-var\fR=\fIname\fR] [\fB\-\-interactive\fR] +[\fB\-\-interactive-default-yes\fR] [\fB\-\-verbose\fR] [\fB\-\-exit\fR] [\fB\-\-no\-run\-if\-empty\fR] @@ -178,6 +179,16 @@ This option is deprecated; use .B \-I instead. .TP +.PD +.B \-\-interactive\-default\-yes +When the +.B \-\-interactive +option is in effect and the user simply +presses return in response to the prompt, behave as if they had +responded positively. If you want the prompting behaviour, you +will also need to specify +.BR \-\-interactive . +.TP .BI \-L " max-lines" Use at most \fImax-lines\fR nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the @@ -255,7 +266,7 @@ otherwise use separate resources). .B \-p, \-\-interactive Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts -with `y' or `Y'. Implies +with `y' or `Y' (though see also \-\-interactive\-default\-yes). Implies .BR -t . .TP .PD diff --git a/xargs/xargs.c b/xargs/xargs.c index 5cf8c13..2fd2904 100644 --- a/xargs/xargs.c +++ b/xargs/xargs.c @@ -158,6 +158,10 @@ static bool print_command = false; /* Option -t */ execute the command if the user responds affirmatively. */ static bool query_before_executing = false; +/* If true, if the user just types RETURN in response to a query from -p, + * behave as if they had responded 'y' (instead of 'n'). */ +static bool interactive_default_yes = false; + /* The delimiter for input arguments. This is only consulted if the * -0 or -d option had been given. */ @@ -173,7 +177,8 @@ static char* slot_var_name = NULL; enum LongOptionIdentifier { - PROCESS_SLOT_VAR = CHAR_MAX+1 + PROCESS_SLOT_VAR = CHAR_MAX+1, + INTERACTIVE_DEFAULT_YES_OPT }; static struct option const longopts[] = @@ -186,6 +191,7 @@ static struct option const longopts[] = {"max-lines", optional_argument, NULL, 'l'}, {"max-args", required_argument, NULL, 'n'}, {"interactive", no_argument, NULL, 'p'}, + {"interactive-default-yes", no_argument, NULL, INTERACTIVE_DEFAULT_YES_OPT}, {"no-run-if-empty", no_argument, NULL, 'r'}, {"max-chars", required_argument, NULL, 's'}, {"verbose", no_argument, NULL, 't'}, @@ -215,7 +221,7 @@ enum ClientStatusValues { static int read_line (void); static int read_string (void); -static bool print_args (bool ask); +static bool print_args (bool ask, bool default_yes); /* static void do_exec (void); */ static int xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv); static void exec_if_possible (void); @@ -613,6 +619,10 @@ main (int argc, char **argv) print_command = true; break; + case INTERACTIVE_DEFAULT_YES_OPT: + interactive_default_yes = true; + break; + case 'r': always_run_command = 0; break; @@ -1079,7 +1089,7 @@ read_string (void) otherwise, return false. */ static bool -print_args (bool ask) +print_args (bool ask, bool default_yes) { size_t i; @@ -1093,13 +1103,13 @@ print_args (bool ask) { static FILE *tty_stream; int c, savec; - + const char tty_dev[] = "/dev/tty"; if (!tty_stream) { - tty_stream = fopen_cloexec_for_read_only ("/dev/tty"); + tty_stream = fopen_cloexec_for_read_only (tty_dev); if (!tty_stream) error (EXIT_FAILURE, errno, - _("failed to open /dev/tty for reading")); + _("failed to open %s for reading"), tty_dev); } fputs ("?...", stderr); if (fflush (stderr) != 0) @@ -1109,8 +1119,8 @@ print_args (bool ask) while (c != EOF && c != '\n') c = getc (tty_stream); if (EOF == c) - error (EXIT_FAILURE, errno, _("Failed to read from stdin")); - if (savec == 'y' || savec == 'Y') + error (EXIT_FAILURE, errno, _("Failed to read from %s"), tty_dev); + if (savec == 'y' || savec == 'Y' || (default_yes && savec == '\n')) return true; } else @@ -1233,10 +1243,10 @@ xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char * } } - if (!query_before_executing || print_args (true)) + if (!query_before_executing || print_args (true, interactive_default_yes)) { if (!query_before_executing && print_command) - print_args (false); + print_args (false, interactive_default_yes); /* Before forking, reap any already-exited child. We do this so that we don't leave unreaped children around while we build a @@ -1686,6 +1696,7 @@ usage (int status) HTL (_(" --show-limits show limits on command-line length\n")); HTL (_(" -t, --verbose print commands before executing them\n")); HTL (_(" -x, --exit exit if the size (see -s) is exceeded\n")); + HTL (_(" --interactive-default-yes assume return means 'y' with --interactive\n")); HTL (_(" --help display this help and exit\n")); HTL (_(" --version output version information and exit\n\n")); -- 2.1.4