Index: src/regexp.c ================================================================== --- src/regexp.c +++ src/regexp.c @@ -736,24 +736,56 @@ return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 0, re_sql_func, 0, 0); } /* -** Run a "grep" over a single file +** Run a "grep" over a single file read from disk. */ -static void grep(ReCompiled *pRe, const char *zFile, FILE *in){ +static void grep_file(ReCompiled *pRe, const char *zFile, FILE *in){ int ln = 0; int n; char zLine[2000]; while( fgets(zLine, sizeof(zLine), in) ){ ln++; n = (int)strlen(zLine); while( n && (zLine[n-1]=='\n' || zLine[n-1]=='\r') ) n--; if( re_match(pRe, (const unsigned char*)zLine, n) ){ - printf("%s:%d:%.*s\n", zFile, ln, n, zLine); + fossil_print("%s:%d:%.*s\n", zFile, ln, n, zLine); + } + } +} + +/* +** Flags for grep_buffer() +*/ +#define GREP_EXISTS 0x001 /* If any match, print only the name and stop */ + +/* +** Run a "grep" over a text file +*/ +static int grep_buffer( + ReCompiled *pRe, + const char *zName, + const char *z, + u32 flags +){ + int i, j, n, ln, cnt; + for(i=j=ln=cnt=0; z[i]; i=j+1){ + for(j=i; z[j] && z[j]!='\n'; j++){} + n = j - i; + if( z[j]=='\n' ) j++; + ln++; + if( re_match(pRe, (const unsigned char*)(z+i), j-i) ){ + cnt++; + if( flags & GREP_EXISTS ){ + fossil_print("%s\n", zName); + break; + } + fossil_print("%s:%d:%.*s\n", zName, ln, n, z+i); } } + return cnt; } /* ** COMMAND: test-grep ** @@ -774,20 +806,77 @@ usage("REGEXP [FILE...]"); } zErr = re_compile(&pRe, g.argv[2], ignoreCase); if( zErr ) fossil_fatal("%s", zErr); if( g.argc==3 ){ - grep(pRe, "-", stdin); + grep_file(pRe, "-", stdin); }else{ int i; for(i=3; i