aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-08-12 03:08:56 -0500
committerRob Landley <rob@landley.net>2013-08-12 03:08:56 -0500
commit31f07104445bc0c0cbc942d86ee9d31d08c344d8 (patch)
treefc125c2a0ecee8a63861b9f0671de0674a3314da /toys
parentdd2d23930241a30a8eb4f0fc9d70bc86c4a6cb6e (diff)
downloadtoybox-31f07104445bc0c0cbc942d86ee9d31d08c344d8.tar.gz
More grep work: name "(standard input)" correctly, make multiple -e work, regex with embedded newline, multiple regex without -E.
Diffstat (limited to 'toys')
-rw-r--r--toys/pending/grep.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/toys/pending/grep.c b/toys/pending/grep.c
index 2d772651..215c6d24 100644
--- a/toys/pending/grep.c
+++ b/toys/pending/grep.c
@@ -55,6 +55,8 @@ static void do_grep(int fd, char *name)
long offset = 0;
int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
+ if (!fd) name = "(standard input)";
+
if (!file) {
perror_msg("%s", name);
return;
@@ -164,40 +166,49 @@ static void do_grep(int fd, char *name)
static void parse_regex(void)
{
- struct arg_list *al;
+ struct arg_list *al, *new, *list = NULL;
long len = 0;
char *s, *ss;
// Add all -f lines to -e list. (Yes, this is leaking allocation context for
// exit to free. Not supporting nofork for this command any time soon.)
- for (al = TT.f; al; al = al->next) {
- s = ss = xreadfile(al->arg);
+ al = TT.f ? TT.f : TT.e;
+ while (al) {
+ if (TT.f) s = ss = xreadfile(al->arg);
+ else s = ss = al->arg;
while (ss && *s) {
ss = strchr(s, '\n');
- if (ss) *ss = 0;
- al = xmalloc(sizeof(struct arg_list));
- al->next = TT.e;
- al->arg = s;
- TT.e = al;
+ if (ss) *(ss++) = 0;
+ new = xmalloc(sizeof(struct arg_list));
+ new->next = list;
+ new->arg = s;
+ list = new;
s = ss;
}
+ al = al->next;
+ if (!al && TT.f) {
+ TT.f = 0;
+ al = TT.e;
+ }
}
+ TT.e = list;
if (!(toys.optflags & FLAG_F)) {
int w = toys.optflags & FLAG_w;
- // Convert strings to one big regex string.
- for (al = TT.e; al; al = al->next) len += strlen(al->arg)+1;
+ // Convert strings to one big regex
if (w) len = 36;
+ for (al = TT.e; al; al = al->next) len += strlen(al->arg)+1;
TT.regstr = s = xmalloc(len);
if (w) s = stpcpy(s, "(^|[^_[:alnum:]])(");
for (al = TT.e; al; al = al->next) {
s = stpcpy(s, al->arg);
+ if (!(toys.optflags & FLAG_E)) *(s++) = '\\';
*(s++) = '|';
}
- *(--s) = 0;
+ *(s-=(1+!(toys.optflags & FLAG_E))) = 0;
if (w) strcpy(s, ")($|[^_[:alnum:]])");
w = regcomp((regex_t *)toybuf, TT.regstr,