]> asedeno.scripts.mit.edu Git - git.git/blobdiff - grep.c
Teach Solaris that _XOPEN_SOURCE=600 really menas XPG6
[git.git] / grep.c
diff --git a/grep.c b/grep.c
index f3a27d7d6e141bd8813b978edbe33d16aa764fb4..a649f063cf28baa5b0ddce72d7d6e4d9fca6f360 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -72,6 +72,8 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
        struct grep_expr *x;
 
        p = *list;
+       if (!p)
+               return NULL;
        switch (p->token) {
        case GREP_PATTERN: /* atom */
        case GREP_PATTERN_HEAD:
@@ -84,8 +86,6 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
        case GREP_OPEN_PAREN:
                *list = p->next;
                x = compile_pattern_or(list);
-               if (!x)
-                       return NULL;
                if (!*list || (*list)->token != GREP_CLOSE_PAREN)
                        die("unmatched parenthesis");
                *list = (*list)->next;
@@ -101,6 +101,8 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
        struct grep_expr *x;
 
        p = *list;
+       if (!p)
+               return NULL;
        switch (p->token) {
        case GREP_NOT:
                if (!p->next)
@@ -303,6 +305,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 {
        int hit = 0;
        int saved_ch = 0;
+       const char *start = bol;
 
        if ((p->token != GREP_PATTERN) &&
            ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
@@ -363,6 +366,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
        }
        if (p->token == GREP_PATTERN_HEAD && saved_ch)
                *eol = saved_ch;
+       if (hit) {
+               pmatch[0].rm_so += bol - start;
+               pmatch[0].rm_eo += bol - start;
+       }
        return hit;
 }
 
@@ -372,6 +379,8 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
        int h = 0;
        regmatch_t match;
 
+       if (!x)
+               die("Not a valid grep expression");
        switch (x->node) {
        case GREP_NODE_ATOM:
                h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);