I quite like the idea. Here's an example implementation of a flag that could be used in a shell function/alias, if that's your thing. Comments?
On Sun, Apr 17, 2016 at 9:12 PM, Bernhard Voelker <m...@bernhard-voelker.de> wrote: > On 04/10/2016 04:26 PM, 積丹尼 Dan Jacobson wrote: > >>>>>> "BV" == Bernhard Voelker <m...@bernhard-voelker.de> writes: > > BV> Well, there are many possibilities, but there is already a way to > > BV> to do it in xargs. So what would you want to achieve by saving a > RET? > > BV> I don't think it's about the wearing of the ENTER key on your > keyboard? > > > > Thank you. If there is a way to do > > $ seq 99|sed s/^/URL_/|xargs -pn 9 $BROWSER > > so I can browse 9 items at a time > > with just hitting RET instead of y RET, please tell me. > > I'd personally go with some shell loop like: > > seq 99|sed s/^/URL_/|xargs -n9 \ > | while read urls; do \ > read -p "$urls:" </dev/tty; \ > $BROWSER $urls; \ > done > > Of course, as the URLs may contain some special characters, you'd have > to care about proper quoting or escaping/unescaping additionally. > > Nevertheless, xargs isn't primarily a tool to get and evaluate > the user's approval to execute arbitrary tools; and I don't think > it should support more ways of evaluating such approval. > > Have a nice day, > Berny > > -- -- This email is intended solely for the use of its addressee, sender, and any readers of a mailing list archive in which it happens to appear. If you have received this email in error, please say or type three times, "I believe in the utility of email disclaimers," and then reply to the author correcting any spellings (and, optionally, any incorrect spellings), accompanying these with humorous jests about the author's parentage. If you are not the addressee, you are nevertheless permitted to both copy and forward this email since without such permissions email systems are unable to transmit email to anybody, intended recipient or not. To those still reading by this point, the author would like to apologise for being unable to maintain a consistent level of humour throughout this disclaimer. Contents may settle during transit. Do not feed the animals.
From bfea9c150229db14316262c1c05198c7caeb9538 Mon Sep 17 00:00:00 2001 From: James Youngman <j...@gnu.org> Date: Sat, 16 Jul 2016 15:10:32 +0100 Subject: [PATCH] xargs: add new option --interactive-default-yes, modifying --interactive. To: findutils-patc...@gnu.org * 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 +@table @code +@item --interactive-default-yes +Normally a response @samp{y} or @samp{Y} is required for +@samp{--interactive} to run the command. When +@samp{--interactive-default-yes} is specified, just pressing return +has the same effect as pressing @samp{y} then return. +This option does not imply @samp{--interactive}. +@end 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. +@item --interactive-default-yes +Normally a response @samp{y} or @samp{Y} is required for +@samp{--interactive} to run the command. When +@samp{--interactive-default-yes} is specified, just pressing return +has the same effect as pressing @samp{y} then return. Does not imply +@samp{--interactive}. + @item -L @var{max-lines} @itemx --max-lines@r{[}=@var{max-lines}@r{]} @itemx -l@r{[}@var{max-lines}@r{]} 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