# HG changeset patch # User Rob Landley # Date 1327930832 21600 # Node ID e9f6e7f25854b65d69bd99e63d9fd28c26da6ca7 # Parent 2cbbd4c5eaaae316062e22aa13482bed924b9de9 More work on xargs: bugfix and tests. diff -r 2cbbd4c5eaaa -r e9f6e7f25854 scripts/test/xargs.test --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/test/xargs.test Mon Jan 30 07:40:32 2012 -0600 @@ -0,0 +1,35 @@ +#!/bin/bash + +[ -f testing.sh ] && . testing.sh + +#testing "name" "command" "result" "infile" "stdin" + +testing "xargs" "xargs && echo yes" "hello\nyes\n" "" "hello" +testing "xargs spaces" "xargs" \ + "one two three four\n" "" "one two\tthree \nfour\n\n" + +testing "xargs -n 0" "xargs -n 0 2>/dev/null || echo ok" "ok\n" \ + "" "one \ntwo\n three" +testing "xargs -n 2" "xargs -n 2" "one two\nthree\n" "" "one \ntwo\n three" +testing "xargs -n exact match" "xargs -n 3" "one two three\n" "" "one two three" +testing "xargs2" "xargs -n2" "one two\nthree four\nfive\n" "" \ + "one two three four five" +testing "xargs -s too long" "xargs -s 9 echo 2>/dev/null || echo ok" \ + "one\ntwo\nok\n" "" "one two three" +testing "xargs -s 13" "xargs -s 13 echo" "one two\nthree\n" "" "one \ntwo\n three" +testing "xargs -s 12" "xargs -s 12 echo" "one\ntwo\nthree\n" "" "one \ntwo\n three" + +touch one two three +testing "xargs command -opt" "xargs -n2 ls -1" "one\ntwo\nthree\n" "" \ + "one two three" +rm one two three + +exit + +testing "xargs -n exact match" +testing "xargs -s exact match" +testing "xargs -s 0" +testing "xargs -s impossible" + +# xargs command_not_found - returns 127 +# xargs false - returns 1 diff -r 2cbbd4c5eaaa -r e9f6e7f25854 toys/xargs.c --- a/toys/xargs.c Sun Jan 29 13:54:13 2012 -0600 +++ b/toys/xargs.c Mon Jan 30 07:40:32 2012 -0600 @@ -6,7 +6,7 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/xargs.html -USE_XARGS(NEWTOY(xargs, "n#s#0", TOYFLAG_USR|TOYFLAG_BIN)) +USE_XARGS(NEWTOY(xargs, "^I:E:L#ptxrn#<1s#0", TOYFLAG_USR|TOYFLAG_BIN)) config XARGS bool "xargs" @@ -16,9 +16,17 @@ Run command line one or more times, appending arguments from stdin. + If command exits with 255, don't launch another even if arguments remain. + -s Size in bytes per command line -n Max number of arguments per command -0 Each argument is NULL terminated, no whitespace or quote processing + #-p Prompt for y/n from tty before running each command + #-t Trace, print command line to stderr + #-x Exit if can't fit everything in one command + #-r Don't run command with empty input + #-L Max number of lines of input per command + #-E stop at line matching string */ #include "toys.h" @@ -64,7 +72,7 @@ } if (TT.entries >= TT.max_entries && TT.max_entries) - return *s ? (char *)1: s; + return *s ? s : (char *)1; if (!*s) break; save = s; @@ -106,10 +114,11 @@ bytes += strlen(toys.optargs[entries]); // Loop through exec chunks. - while (!done) { + while (data || !done) { + char **out; + TT.entries = 0; TT.bytes = bytes; - char **out; // Loop reading input for (;;) {