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
.Op Fl 0pt
.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 L Ar number
.Oo
@ -104,7 +107,11 @@ Execute
.Ar utility
for each input line, replacing one or more occurences of
.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
with the entire line of input.
The resulting arguments after replacement is done will not be allowed to grow
@ -191,6 +198,10 @@ A response of
.Ql y
causes the command to be executed, any other response causes it to be
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
Set the maximum number of bytes for the command line length provided to
.Ar utility .

View File

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