123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #define dbg_msg(...) ((void)0)
- #ifdef TEST
- # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
- # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
- # endif
- # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
- # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
- # endif
- # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
- # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
- # endif
- # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
- # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
- # endif
- #endif
- struct globals {
- char **args;
- #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
- char **argv;
- const char *repl_str;
- char eol_ch;
- #endif
- const char *eof_str;
- int idx;
- #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
- int running_procs;
- int max_procs;
- #endif
- smalluint xargs_exitcode;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- G.eof_str = NULL; \
- G.idx = 0; \
- IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
- IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
- G.xargs_exitcode = 0; \
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
- } while (0)
- static int xargs_exec(void)
- {
- int status;
- #if !ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
- status = spawn_and_wait(G.args);
- #else
- if (G.max_procs == 1) {
- status = spawn_and_wait(G.args);
- } else {
- pid_t pid;
- int wstat;
- again:
- if (G.running_procs >= G.max_procs)
- pid = safe_waitpid(-1, &wstat, 0);
- else
- pid = wait_any_nohang(&wstat);
- if (pid > 0) {
-
- if (G.running_procs != 0)
- G.running_procs--;
- status = WIFSIGNALED(wstat)
- ? 0x180 + WTERMSIG(wstat)
- : WEXITSTATUS(wstat);
- if (status > 0 && status < 255) {
-
- G.xargs_exitcode = 123;
- status = 0;
- }
- if (status == 0)
- goto again;
-
- } else if (G.max_procs != 0) {
-
- status = spawn(G.args);
-
- if (status > 0) {
- G.running_procs++;
- status = 0;
- }
-
- } else {
-
- status = 0;
- }
- }
- #endif
-
- if (status < 0) {
- bb_simple_perror_msg(G.args[0]);
- status = (errno == ENOENT) ? 127 : 126;
- }
- else if (status >= 0x180) {
- bb_error_msg("'%s' terminated by signal %d",
- G.args[0], status - 0x180);
- status = 125;
- }
- else if (status != 0) {
- if (status == 255) {
- bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
- return 124;
- }
-
- G.xargs_exitcode = 123;
- status = 0;
- }
- if (status != 0)
- G.xargs_exitcode = status;
- return status;
- }
- #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
- static void store_param(char *s)
- {
-
- if (!(G.idx & 0xff)) {
-
- G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
- }
- G.args[G.idx++] = s;
- }
- #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
- static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
- {
- #define NORM 0
- #define QUOTE 1
- #define BACKSLASH 2
- #define SPACE 4
- char q = '\0';
- char state = NORM;
- char *s = buf;
- char *p = s + strlen(buf);
- buf += n_max_chars;
-
- while (1) {
- int c = getchar();
- if (c == EOF) {
- if (p != s)
- goto close_word;
- goto ret;
- }
- if (state == BACKSLASH) {
- state = NORM;
- goto set;
- }
- if (state == QUOTE) {
- if (c != q)
- goto set;
- q = '\0';
- state = NORM;
- } else {
- if (ISSPACE(c)) {
- if (p != s) {
- close_word:
- state = SPACE;
- c = '\0';
- goto set;
- }
- } else {
- if (c == '\\') {
- state = BACKSLASH;
- } else if (c == '\'' || c == '"') {
- q = c;
- state = QUOTE;
- } else {
- set:
- *p++ = c;
- }
- }
- }
- if (state == SPACE) {
- if (q) {
- bb_error_msg_and_die("unmatched %s quote",
- q == '\'' ? "single" : "double");
- }
-
- if (G.eof_str) {
- if (strcmp(s, G.eof_str) == 0) {
- while (getchar() != EOF)
- continue;
- p = s;
- goto ret;
- }
- }
- store_param(s);
- dbg_msg("args[]:'%s'", s);
- s = p;
- n_max_arg--;
- if (n_max_arg == 0) {
- goto ret;
- }
- state = NORM;
- }
- if (p == buf) {
- goto ret;
- }
- }
- ret:
- *p = '\0';
-
- dbg_msg("return:'%s'", s);
- return s;
- }
- #else
- static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
- {
- char *s = buf;
- char *p = s + strlen(buf);
- buf += n_max_chars;
- while (1) {
- int c = getchar();
- if (c == EOF) {
- if (p == s)
- goto ret;
- }
- if (c == EOF || ISSPACE(c)) {
- if (p == s)
- continue;
- c = EOF;
- }
- *p++ = (c == EOF ? '\0' : c);
- if (c == EOF) {
-
- if (G.eof_str) {
- if (strcmp(s, G.eof_str) == 0) {
- while (getchar() != EOF)
- continue;
- p = s;
- goto ret;
- }
- }
- store_param(s);
- dbg_msg("args[]:'%s'", s);
- s = p;
- n_max_arg--;
- if (n_max_arg == 0) {
- goto ret;
- }
- }
- if (p == buf) {
- goto ret;
- }
- }
- ret:
- *p = '\0';
-
- dbg_msg("return:'%s'", s);
- return s;
- }
- #endif
- #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
- static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
- {
- char *s = buf;
- char *p = s + strlen(buf);
- buf += n_max_chars;
- while (1) {
- int c = getchar();
- if (c == EOF) {
- if (p == s)
- goto ret;
- c = '\0';
- }
- *p++ = c;
- if (c == '\0') {
-
- store_param(s);
- dbg_msg("args[]:'%s'", s);
- s = p;
- n_max_arg--;
- if (n_max_arg == 0) {
- goto ret;
- }
- }
- if (p == buf) {
- goto ret;
- }
- }
- ret:
- *p = '\0';
-
- dbg_msg("return:'%s'", s);
- return s;
- }
- #endif
- #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
- static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
- {
- int i;
- char *end, *p;
-
- for (i = 0; G.args && G.args[i]; i++)
- if (G.args[i] != G.argv[i])
- free(G.args[i]);
- end = buf + n_max_chars;
- p = buf;
- while (1) {
- int c = getchar();
- if (c == EOF || c == G.eol_ch) {
- if (p == buf)
- goto ret;
- c = '\0';
- }
- *p++ = c;
- if (c == '\0') {
- i = 0;
- while (G.argv[i]) {
- char *arg = G.argv[i];
- int count = count_strstr(arg, G.repl_str);
- if (count != 0)
- arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
- store_param(arg);
- dbg_msg("args[]:'%s'", arg);
- i++;
- }
- p = buf;
- goto ret;
- }
- if (p == end) {
- goto ret;
- }
- }
- ret:
- *p = '\0';
-
- dbg_msg("return:'%s'", buf);
- return buf;
- }
- #endif
- #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
- static int xargs_ask_confirmation(void)
- {
- FILE *tty_stream;
- int c, savec;
- tty_stream = xfopen_for_read(CURRENT_TTY);
- fputs(" ?...", stderr);
- fflush_all();
- c = savec = getc(tty_stream);
- while (c != EOF && c != '\n')
- c = getc(tty_stream);
- fclose(tty_stream);
- return (savec == 'y' || savec == 'Y');
- }
- #else
- # define xargs_ask_confirmation() 1
- #endif
- enum {
- OPTBIT_VERBOSE = 0,
- OPTBIT_NO_EMPTY,
- OPTBIT_UPTO_NUMBER,
- OPTBIT_UPTO_SIZE,
- OPTBIT_EOF_STRING,
- OPTBIT_EOF_STRING1,
- IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
- IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
- IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
- IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
- IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
- OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
- OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
- OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
- OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
- OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING ,
- OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1,
- OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
- OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
- OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
- OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
- OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
- };
- #define OPTION_STR "+trn:s:e::E:" \
- IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
- IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
- IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
- IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") \
- IF_FEATURE_XARGS_SUPPORT_PARALLEL( "P:+") \
- IF_FEATURE_XARGS_SUPPORT_ARGS_FILE( "a:")
- int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int xargs_main(int argc UNUSED_PARAM, char **argv)
- {
- int initial_idx;
- int i;
- char *max_args;
- char *max_chars;
- char *buf;
- unsigned opt;
- int n_max_chars;
- int n_max_arg;
- #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
- || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
- char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
- #else
- #define read_args process_stdin
- #endif
- IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
- INIT_G();
- opt = getopt32long(argv, OPTION_STR,
- "no-run-if-empty\0" No_argument "r",
- &max_args, &max_chars, &G.eof_str, &G.eof_str
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
- IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
- IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
- );
- #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
- if (G.max_procs <= 0)
- G.max_procs = 100;
- #endif
- #if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
- if (opt_a)
- xmove_fd(xopen(opt_a, O_RDONLY), 0);
- #endif
-
- if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
- G.eof_str = NULL;
- if (opt & OPT_ZEROTERM) {
- IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
- }
- argv += optind;
-
- if (!argv[0]) {
-
- *--argv = (char*)"echo";
-
- }
-
- n_max_chars = bb_arg_max();
- if (n_max_chars > 32 * 1024)
- n_max_chars = 32 * 1024;
-
- n_max_chars -= 2048;
- if (opt & OPT_UPTO_SIZE) {
- n_max_chars = xatou_range(max_chars, 1, INT_MAX);
- }
-
- {
- size_t n_chars = 0;
- for (i = 0; argv[i]; i++) {
- n_chars += strlen(argv[i]) + 1;
- }
- n_max_chars -= n_chars;
- }
-
- if (n_max_chars <= 0) {
- bb_error_msg_and_die("can't fit single argument within argument list size limit");
- }
- buf = xzalloc(n_max_chars + 1);
- n_max_arg = n_max_chars;
- if (opt & OPT_UPTO_NUMBER) {
- n_max_arg = xatou_range(max_args, 1, INT_MAX);
-
-
- }
- #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
- if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
-
- G.args = NULL;
- G.argv = argv;
- read_args = process_stdin_with_replace;
-
-
- opt |= OPT_NO_EMPTY;
- } else
- #endif
- {
-
- for (i = 0; argv[i]; i++)
- store_param(argv[i]);
- }
- initial_idx = G.idx;
- while (1) {
- char *rem;
- G.idx = initial_idx;
- rem = read_args(n_max_chars, n_max_arg, buf);
- store_param(NULL);
- if (!G.args[initial_idx]) {
- if (*rem != '\0')
- bb_error_msg_and_die("argument line too long");
- if (opt & OPT_NO_EMPTY)
- break;
- }
- opt |= OPT_NO_EMPTY;
- if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
- const char *fmt = " %s" + 1;
- char **args = G.args;
- for (i = 0; args[i]; i++) {
- fprintf(stderr, fmt, args[i]);
- fmt = " %s";
- }
- if (!(opt & OPT_INTERACTIVE))
- bb_putchar_stderr('\n');
- }
- if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
- if (xargs_exec() != 0)
- break;
- }
- overlapping_strcpy(buf, rem);
- }
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(G.args);
- free(buf);
- }
- #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
- G.max_procs = 0;
- xargs_exec();
- #endif
- return G.xargs_exitcode;
- }
- #ifdef TEST
- const char *applet_name = "debug stuff usage";
- void bb_show_usage(void)
- {
- fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
- applet_name);
- exit(EXIT_FAILURE);
- }
- int main(int argc, char **argv)
- {
- return xargs_main(argc, argv);
- }
- #endif
|