Make -J a proper flag internal to the code (rather than just check for

use of replstr and lack of Iflag), and add -R, which when given with
-I controls the number of arguments on which replacement will be done.

Some people happen to think it's idiotic to limit to 5 arguments, so
let the user override it if they like.
This commit is contained in:
Juli Mallett 2002-05-02 02:42:34 +00:00
parent 569687d02f
commit b50a72861d
2 changed files with 30 additions and 10 deletions

View File

@ -47,7 +47,10 @@
.Nm .Nm
.Op Fl 0pt .Op Fl 0pt
.Op Fl E Ar eofstr .Op Fl E Ar eofstr
.Op Fl I Ar replstr .Oo
.Fl I Ar replstr
.Op Fl R Ar replacements
.Oc
.Op Fl J Ar replstr .Op Fl J Ar replstr
.Op Fl L Ar number .Op Fl L Ar number
.Oo .Oo
@ -104,7 +107,11 @@ Execute
.Ar utility .Ar utility
for each input line, replacing one or more occurences of for each input line, replacing one or more occurences of
.Ar replstr .Ar replstr
in up to 5 arguments to in up to
.Ar replacements
(or 5 if no
.Op R
flag is specified) arguments to
.Ar utility .Ar utility
with the entire line of input. with the entire line of input.
The resulting arguments after replacement is done will not be allowed to grow The resulting arguments after replacement is done will not be allowed to grow
@ -191,6 +198,10 @@ A response of
.Ql y .Ql y
causes the command to be executed, any other response causes it to be causes the command to be executed, any other response causes it to be
skipped. skipped.
.It Fl R Ar replacements
This option specifies the maximum number of arguments that
.Op I
will do replacement in.
.It Fl s Ar size .It Fl s Ar size
Set the maximum number of bytes for the command line length provided to Set the maximum number of bytes for the command line length provided to
.Ar utility . .Ar utility .

View File

@ -76,8 +76,8 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
long arg_max; long arg_max;
int ch, cnt, count, Iflag, indouble, insingle, jfound, lflag; int ch, cnt, count, Iflag, indouble, insingle, Jflag, jfound, lflag;
int nargs, nflag, nline, wasquoted, foundeof, xflag; int nargs, nflag, nline, Rflag, wasquoted, foundeof, xflag;
size_t linelen; size_t linelen;
const char *eofstr; const char *eofstr;
char **av, **avj, **bxp, **ep, **exp, **xp; char **av, **avj, **bxp, **ep, **exp, **xp;
@ -86,7 +86,8 @@ main(int argc, char **argv)
ep = environ; ep = environ;
inpline = replstr = NULL; inpline = replstr = NULL;
eofstr = ""; eofstr = "";
cnt = count = Iflag = jfound = lflag = nflag = xflag = wasquoted = 0; cnt = count = Iflag = Jflag = jfound = lflag = nflag = Rflag = xflag =
wasquoted = 0;
/* /*
* POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that
@ -109,7 +110,7 @@ main(int argc, char **argv)
/* 1 byte for each '\0' */ /* 1 byte for each '\0' */
nline -= strlen(*ep++) + 1 + sizeof(*ep); nline -= strlen(*ep++) + 1 + sizeof(*ep);
} }
while ((ch = getopt(argc, argv, "0E:I:J:L:n:ps:tx")) != -1) while ((ch = getopt(argc, argv, "0E:I:J:L:n:pR:s:tx")) != -1)
switch(ch) { switch(ch) {
case 'E': case 'E':
eofstr = optarg; eofstr = optarg;
@ -117,9 +118,11 @@ main(int argc, char **argv)
case 'I': case 'I':
Iflag = 1; Iflag = 1;
lflag = 1; lflag = 1;
Rflag = 5;
replstr = optarg; replstr = optarg;
break; break;
case 'J': case 'J':
Jflag = 1;
replstr = optarg; replstr = optarg;
break; break;
case 'L': case 'L':
@ -133,6 +136,12 @@ main(int argc, char **argv)
case 'p': case 'p':
pflag = 1; pflag = 1;
break; break;
case 'R':
if (!Iflag)
usage();
if ((Rflag = atoi(optarg)) <= 0)
errx(1, "illegal number of replacements");
break;
case 's': case 's':
nline = atoi(optarg); nline = atoi(optarg);
break; break;
@ -177,7 +186,7 @@ main(int argc, char **argv)
cnt = strlen((*bxp++ = echo)); cnt = strlen((*bxp++ = echo));
else { else {
do { do {
if (!Iflag && replstr && strcmp(*argv, replstr) == 0) { if (Jflag && strcmp(*argv, replstr) == 0) {
jfound = 1; jfound = 1;
argv++; argv++;
for (avj = argv; *avj; avj++) for (avj = argv; *avj; avj++)
@ -289,7 +298,7 @@ arg2:
if (tmp == NULL) if (tmp == NULL)
err(1, "malloc"); err(1, "malloc");
tmp2 = tmp; tmp2 = tmp;
repls = 5; repls = Rflag;
for (avj = av, iter = argc; iter; avj++, iter--) { for (avj = av, iter = argc; iter; avj++, iter--) {
*tmp = *avj; *tmp = *avj;
if (avj != av && repls > 0 && if (avj != av && repls > 0 &&
@ -424,7 +433,7 @@ static void
usage(void) usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"usage: xargs [-0pt] [-E eofstr] [-I replstr] [-J replstr] [-L number]\n" "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
" [-n number [-x] [-s size] [utility [argument ...]]\n"); " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");
exit(1); exit(1);
} }