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 USE_XARGS(NEWTOY(xargs, "^I:E:L#ptxrn#<1s#0", TOYFLAG_USR|TOYFLAG_BIN))
8 
9 config XARGS
10   bool "xargs"
11   default y
12   help
13     usage: xargs [-ptxr0] [-s NUM] [-n NUM] [-L NUM] [-E STR] COMMAND...
14 
15     Run command line one or more times, appending arguments from stdin.
16 
17     If command exits with 255, don't launch another even if arguments remain.
18 
19     -s	Size in bytes per command line
20     -n	Max number of arguments per command
21     -0	Each argument is NULL terminated, no whitespace or quote processing
22     #-p	Prompt for y/n from tty before running each command
23     #-t	Trace, print command line to stderr
24     #-x	Exit if can't fit everything in one command
25     #-r	Don't run command with empty input
26     #-L	Max number of lines of input per command
27     -E	stop at line matching string
28 
29 config XARGS_PEDANTIC
30   bool "TODO xargs pedantic posix compatability"
31   default n
32   depends on XARGS
33   help
34     This version supports insane posix whitespace handling rendered obsolete
35     by -0 mode.
36 */
37 
38 #define FOR_xargs
39 #include "toys.h"
40 
GLOBALS(long max_bytes;long max_entries;long L;char * eofstr;char * I;long entries,bytes;char delim;)41 GLOBALS(
42   long max_bytes;
43   long max_entries;
44   long L;
45   char *eofstr;
46   char *I;
47 
48   long entries, bytes;
49   char delim;
50 )
51 
52 // If out==NULL count TT.bytes and TT.entries, stopping at max.
53 // Otherwise, fill out out[]
54 
55 // Returning NULL means need more data.
56 // Returning char * means hit data limits, start of data left over
57 // Returning 1 means hit data limits, but consumed all data
58 // Returning 2 means hit -E eofstr
59 
60 static char *handle_entries(char *data, char **entry)
61 {
62   if (TT.delim) {
63     char *s = data;
64 
65     // Chop up whitespace delimited string into args
66     while (*s) {
67       char *save;
68 
69       while (isspace(*s)) {
70         if (entry) *s = 0;
71         s++;
72       }
73 
74       if (TT.max_entries && TT.entries >= TT.max_entries)
75         return *s ? s : (char *)1;
76 
77       if (!*s) break;
78       save = s;
79 
80       for (;;) {
81         if (++TT.bytes >= TT.max_bytes && TT.max_bytes) return save;
82         if (!*s || isspace(*s)) break;
83         s++;
84       }
85       if (TT.eofstr) {
86         int len = s-save;
87         if (len == strlen(TT.eofstr) && !strncmp(save, TT.eofstr, len))
88           return (char *)2;
89       }
90       if (entry) entry[TT.entries] = save;
91       ++TT.entries;
92     }
93 
94   // -0 support
95   } else {
96     TT.bytes += strlen(data)+1;
97     if (TT.max_bytes && TT.bytes >= TT.max_bytes) return data;
98     if (TT.max_entries && TT.entries >= TT.max_entries)
99       return (char *)1;
100     if (entry) entry[TT.entries] = data;
101     TT.entries++;
102   }
103 
104   return NULL;
105 }
106 
xargs_main(void)107 void xargs_main(void)
108 {
109   struct double_list *dlist = NULL, *dtemp;
110   int entries, bytes, done = 0, status;
111   char *data = NULL, **out;
112 
113   if (!(toys.optflags & FLAG_0)) TT.delim = '\n';
114 
115   // If no optargs, call echo.
116   if (!toys.optc) {
117     free(toys.optargs);
118     *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
119     toys.optc = 1;
120   }
121 
122   for (entries = 0, bytes = -1; entries < toys.optc; entries++, bytes++)
123     bytes += strlen(toys.optargs[entries]);
124 
125   // Loop through exec chunks.
126   while (data || !done) {
127     TT.entries = 0;
128     TT.bytes = bytes;
129 
130     // Loop reading input
131     for (;;) {
132 
133       // Read line
134       if (!data) {
135         ssize_t l = 0;
136         l = getdelim(&data, (size_t *)&l, TT.delim, stdin);
137 
138         if (l<0) {
139           data = 0;
140           done++;
141           break;
142         }
143       }
144       dlist_add(&dlist, data);
145 
146       // Count data used
147       data = handle_entries(data, NULL);
148       if (!data) continue;
149       if (data == (char *)2) done++;
150       if ((long)data <= 2) data = 0;
151       else data = xstrdup(data);
152 
153       break;
154     }
155 
156     // Accumulate cally thing
157 
158     if (data && !TT.entries) error_exit("argument too long");
159     out = xzalloc((entries+TT.entries+1)*sizeof(char *));
160 
161     // Fill out command line to exec
162     memcpy(out, toys.optargs, entries*sizeof(char *));
163     TT.entries = 0;
164     TT.bytes = bytes;
165     if (dlist) dlist->prev->next = 0;
166     for (dtemp = dlist; dtemp; dtemp = dtemp->next)
167       handle_entries(dtemp->data, out+entries);
168 
169     pid_t pid=xfork();
170     if (!pid) {
171       xclose(0);
172       open("/dev/null", O_RDONLY);
173       xexec(out);
174     }
175     waitpid(pid, &status, 0);
176     status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
177 
178     // Abritrary number of execs, can't just leak memory each time...
179     while (dlist) {
180       struct double_list *dtemp = dlist->next;
181 
182       free(dlist->data);
183       free(dlist);
184       dlist = dtemp;
185     }
186     free(out);
187   }
188 }
189