1 /* xargs.c - Run command with arguments taken from stdin.
2  *
3  * Copyright 2011 Rob Landley <rob@landley.net>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/xargs.html
6  *
7  * TODO: Rich's whitespace objection, env size isn't fixed anymore.
8  * TODO: -I	Insert mode
9  * TODO: -L	Max number of lines of input per command
10  * TODO: -x	Exit if can't fit everything in one command
11 
12 USE_XARGS(NEWTOY(xargs, "^E:P#<0=1optrn#<1(max-args)s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
13 
14 config XARGS
15   bool "xargs"
16   default y
17   help
18     usage: xargs [-0prt] [-snE STR] COMMAND...
19 
20     Run command line one or more times, appending arguments from stdin.
21 
22     If COMMAND exits with 255, don't launch another even if arguments remain.
23 
24     -0	Each argument is NULL terminated, no whitespace or quote processing
25     -E	Stop at line matching string
26     -n	Max number of arguments per command
27     -o	Open tty for COMMAND's stdin (default /dev/null)
28     -p	Prompt for y/n from tty before running each command
29     -P	Parallel processes (default 1)
30     -r	Don't run with empty input (otherwise always run command once)
31     -s	Size in bytes per command line
32     -t	Trace, print command line to stderr
33 */
34 
35 #define FOR_xargs
36 #include "toys.h"
37 
GLOBALS(long s,n,P;char * E;long entries,bytes,np;char delim;FILE * tty;)38 GLOBALS(
39   long s, n, P;
40   char *E;
41 
42   long entries, bytes, np;
43   char delim;
44   FILE *tty;
45 )
46 
47 // If !entry count TT.bytes and TT.entries, stopping at max.
48 // Otherwise, fill out entry[].
49 
50 // Returning NULL means need more data.
51 // Returning char * means hit data limits, start of data left over
52 // Returning 1 means hit data limits, but consumed all data
53 // Returning 2 means hit -E STR
54 
55 static char *handle_entries(char *data, char **entry)
56 {
57   if (TT.delim) {
58     char *save, *ss, *s;
59 
60     // Chop up whitespace delimited string into args
61     for (s = data; *s; TT.entries++) {
62       while (isspace(*s)) s++;
63       if (TT.n && TT.entries >= TT.n) return *s ? s : (char *)1;
64       if (!*s) break;
65       save = ss = s;
66 
67       // Specifying -s can cause "argument too long" errors.
68       if (!FLAG(s)) TT.bytes += sizeof(void *)+1;
69       for (;;) {
70         if (++TT.bytes >= TT.s) return save;
71         if (!*s || isspace(*s)) break;
72         s++;
73       }
74       if (TT.E && strstart(&ss, TT.E) && ss == s) return (char *)2;
75       if (entry) {
76         entry[TT.entries] = save;
77         if (*s) *s++ = 0;
78       }
79     }
80 
81   // -0 support
82   } else {
83     long bytes = TT.bytes+sizeof(char *)+strlen(data)+1;
84 
85     if (bytes >= TT.s || (TT.n && TT.entries >= TT.n)) return data;
86     TT.bytes = bytes;
87     if (entry) entry[TT.entries] = data;
88     TT.entries++;
89   }
90 
91   return 0;
92 }
93 
94 // Handle SIGUSR1 and SIGUSR2 for -P
signal_P(int sig)95 static void signal_P(int sig)
96 {
97   if (sig == SIGUSR2 && TT.P>1) TT.P--;
98   else TT.P++;
99 }
100 
xargs_main(void)101 void xargs_main(void)
102 {
103   struct double_list *dlist = 0, *dtemp;
104   int entries, bytes, done = 0, status;
105   char *data = 0, **out = 0;
106   pid_t pid = 0;
107 
108   xsignal_flags(SIGUSR1, signal_P, SA_RESTART);
109   xsignal_flags(SIGUSR2, signal_P, SA_RESTART);
110 
111   // POSIX requires that we never hit the ARG_MAX limit, even if we try to
112   // with -s. POSIX also says we have to reserve 2048 bytes "to guarantee
113   // that the invoked utility has room to modify its environment variables
114   // and command line arguments and still be able to invoke another utility",
115   // though obviously that's not really something you can guarantee.
116   if (!FLAG(s)) TT.s = sysconf(_SC_ARG_MAX) - environ_bytes() - 4096;
117 
118   TT.delim = '\n'*!FLAG(0);
119 
120   // If no optargs, call echo.
121   if (!toys.optc) {
122     free(toys.optargs);
123     *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
124     toys.optc = 1;
125   }
126 
127   // count entries
128   for (entries = 0, bytes = -1; entries < toys.optc; entries++)
129     bytes += strlen(toys.optargs[entries])+1+sizeof(char *)*!FLAG(s);
130   if (bytes >= TT.s) error_exit("command too long");
131 
132   // Loop through exec chunks.
133   while (data || !done) {
134     TT.entries = 0;
135     TT.bytes = bytes;
136 
137     // Loop reading input
138     for (;;) {
139 
140       // Read line
141       if (!data) {
142         size_t l = 0;
143 
144         if (getdelim(&data, &l, TT.delim, stdin)<0) {
145           data = 0;
146           done++;
147           break;
148         }
149       }
150       dlist_add(&dlist, data);
151       // Count data used
152       if (!(data = handle_entries(data, 0))) continue;
153       if (data == (char *)2) done++;
154       if ((unsigned long)data <= 2) data = 0;
155       else data = xstrdup(data);
156 
157       break;
158     }
159 
160     if (!TT.entries) {
161       if (data) error_exit("argument too long");
162       if (pid || FLAG(r)) goto reap_children;
163     }
164 
165     // Fill out command line to exec
166     out = xzalloc((entries+TT.entries+1)*sizeof(char *));
167     memcpy(out, toys.optargs, entries*sizeof(char *));
168     TT.entries = 0;
169     TT.bytes = bytes;
170     if (dlist) dlist->prev->next = 0;
171     for (dtemp = dlist; dtemp; dtemp = dtemp->next)
172       handle_entries(dtemp->data, out+entries);
173 
174     if (FLAG(p) || FLAG(t)) {
175       int i;
176 
177       for (i = 0; out[i]; ++i) fprintf(stderr, "%s ", out[i]);
178       if (FLAG(p)) {
179         fprintf(stderr, "?");
180         if (!TT.tty) TT.tty = xfopen("/dev/tty", "re");
181         if (!fyesno(TT.tty, 0)) goto reap_children;
182       } else fprintf(stderr, "\n");
183     }
184 
185     if (!(pid = XVFORK())) {
186       close(0);
187       xopen_stdio(FLAG(o) ? "/dev/tty" : "/dev/null", O_RDONLY);
188       xexec(out);
189     }
190     TT.np++;
191 
192 reap_children:
193     while (TT.np) {
194       int xv = (TT.np == TT.P) || (!data && done);
195 
196       if (1>(xv = waitpid(-1, &status, WNOHANG*!xv))) break;
197       TT.np--;
198       xv = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+128;
199       if (xv == 255) {
200         error_msg("%s: exited with status 255; aborting", *out);
201         toys.exitval = 124;
202         break;
203       } else if ((xv|1)==127) toys.exitval = xv;
204       else if (xv>127) xv = 125;
205       else if (xv) toys.exitval = 123;
206     }
207 
208     // Abritrary number of execs, can't just leak memory each time...
209     llist_traverse(dlist, llist_free_double);
210     dlist = 0;
211     free(out);
212     out = 0;
213   }
214   while (TT.np && -1 != wait(&status)) TT.np--;
215   if (TT.tty) fclose(TT.tty);
216 }
217