Code

Merge branch 'nd/stream-more'
[git.git] / grep.c
diff --git a/grep.c b/grep.c
index bb1856985b673a3a57cf2aaad8c82d5de2abd521..190139cfcd0d61f364a88010fe1dd6e71e26f9e2 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -79,7 +79,7 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
 {
        const char *error;
        int erroffset;
-       int options = 0;
+       int options = PCRE_MULTILINE;
 
        if (opt->ignore_case)
                options |= PCRE_CASELESS;
@@ -168,15 +168,10 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
                p->fixed = 0;
 
        if (p->fixed) {
-               if (opt->regflags & REG_ICASE || p->ignore_case) {
-                       static char trans[256];
-                       int i;
-                       for (i = 0; i < 256; i++)
-                               trans[i] = tolower(i);
-                       p->kws = kwsalloc(trans);
-               } else {
+               if (opt->regflags & REG_ICASE || p->ignore_case)
+                       p->kws = kwsalloc(tolower_trans_tbl);
+               else
                        p->kws = kwsalloc(NULL);
-               }
                kwsincr(p->kws, p->pattern, p->patternlen);
                kwsprep(p->kws);
                return;
@@ -1019,16 +1014,13 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
        }
        opt->last_shown = 0;
 
-       if (grep_source_load(gs) < 0)
-               return 0;
-
        switch (opt->binary) {
        case GREP_BINARY_DEFAULT:
-               if (buffer_is_binary(gs->buf, gs->size))
+               if (grep_source_is_binary(gs))
                        binary_match_only = 1;
                break;
        case GREP_BINARY_NOMATCH:
-               if (buffer_is_binary(gs->buf, gs->size))
+               if (grep_source_is_binary(gs))
                        return 0; /* Assume unmatch */
                break;
        case GREP_BINARY_TEXT:
@@ -1042,6 +1034,9 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
 
        try_lookahead = should_lookahead(opt);
 
+       if (grep_source_load(gs) < 0)
+               return 0;
+
        bol = gs->buf;
        left = gs->size;
        while (left) {
@@ -1350,3 +1345,15 @@ void grep_source_load_driver(struct grep_source *gs)
                gs->driver = userdiff_find_by_name("default");
        grep_attr_unlock();
 }
+
+int grep_source_is_binary(struct grep_source *gs)
+{
+       grep_source_load_driver(gs);
+       if (gs->driver->binary != -1)
+               return gs->driver->binary;
+
+       if (!grep_source_load(gs))
+               return buffer_is_binary(gs->buf, gs->size);
+
+       return 0;
+}