From owner-p4-projects@FreeBSD.ORG Wed Jul 23 08:42:27 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 30ACB106567E; Wed, 23 Jul 2008 08:42:27 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E88161065679 for ; Wed, 23 Jul 2008 08:42:26 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id E76938FC20 for ; Wed, 23 Jul 2008 08:42:26 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m6N8gQYL045719 for ; Wed, 23 Jul 2008 08:42:26 GMT (envelope-from gabor@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.2/8.14.1/Submit) id m6N8gQfb045717 for perforce@freebsd.org; Wed, 23 Jul 2008 08:42:26 GMT (envelope-from gabor@freebsd.org) Date: Wed, 23 Jul 2008 08:42:26 GMT Message-Id: <200807230842.m6N8gQfb045717@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to gabor@freebsd.org using -f From: Gabor Kovesdan To: Perforce Change Reviews Cc: Subject: PERFORCE change 145682 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jul 2008 08:42:27 -0000 http://perforce.freebsd.org/chv.cgi?CH=145682 Change 145682 by gabor@gabor_server on 2008/07/23 08:41:57 - Add comments to the code Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/file.c#22 edit .. //depot/projects/soc2008/gabor_textproc/grep/grep.c#70 edit .. //depot/projects/soc2008/gabor_textproc/grep/util.c#66 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/file.c#22 (text+ko) ==== @@ -50,13 +50,14 @@ #include "grep.h" -static char fname[MAXPATHLEN]; +static char fname[MAXPATHLEN]; /* file name */ + +/* Some global variable for the buffering and reading. */ static char *lnbuf; static size_t lnbuflen; static char binbuf[BUFSIZ * 4]; static int binbufsiz; char *binbufptr; - static int bzerr; #define iswbinary(ch) (!iswspace((ch)) && iswcntrl((ch)) && (ch != L'\b') && (ch != L'\0')) @@ -123,7 +124,9 @@ const char *src = binbuf; mbstate_t mbs; + /* Fill in the buffer if it is empty. */ if (binbufptr == NULL) { + /* Only pre-read to the buffer if we need the binary check. */ if (binbehave != BINFILE_TEXT) { for (; i < (BUFSIZ * sizeof(wint_t)); i++) { if (grep_feof(f)) @@ -134,6 +137,8 @@ binbufsiz = i; binbufptr = binbuf; + /* Convert at most (BUFSIZ * sizeof(wint_t)) characters or + (BUFSIZ - 1) bytes to wide character string. */ size = mbsnrtowcs(wbinbuf, &src, BUFSIZ * sizeof(wint_t), BUFSIZ - 1, &mbs); f->binary = 0; for (; size > 0; size--) @@ -148,6 +153,7 @@ } } + /* Read a line whether from the buffer or from the file itself. */ for (i = 0; ; i++) { if (binbufptr == &binbuf[binbufsiz]) { if (grep_feof(f)) @@ -166,7 +172,7 @@ lnbuf[i] = ch; } if (grep_feof(f) && (i == 0)) - return NULL; + return (NULL); *len = i; return (lnbuf); } ==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#70 (text+ko) ==== @@ -85,7 +85,8 @@ int cflags; int eflags = REG_STARTEND; -int matchall; /* shortcut */ +/* Shortcut for matching all cases like empty regex */ +int matchall; /* Searching patterns */ int patterns, pattern_sz; @@ -113,6 +114,7 @@ int iflag; /* -i: ignore case */ int lflag; /* -l: only show names of files with matches */ int mflag; /* -m x: stop reading the files after x matches */ +unsigned long long mcount; /* count for -m */ int nflag; /* -n: show line numbers in front of matching lines */ int oflag; /* -o: print only matching part */ int qflag; /* -q: quiet mode (don't output anything) */ @@ -126,15 +128,13 @@ int inclflag; /* --include */ char *label; /* --label */ char *color; /* --color */ -unsigned long long mcount; /* count for -m */ +int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */ +int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */ +int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */ +int devbehave = DEV_GREP; /* -D: handling of devices */ +int dirbehave = DIR_GREP; /* -dRr: handling of directories */ +int linkbehave = LINK_GREP; /* -OpS: handling of symlinks */ -int grepbehave = GREP_BASIC; -int binbehave = BINFILE_BIN; -int filebehave = FILE_STDIO; -int devbehave = DEV_GREP; -int dirbehave = DIR_GREP; -int linkbehave = LINK_GREP; - enum { BIN_OPT = CHAR_MAX + 1, COLOR_OPT, @@ -226,11 +226,13 @@ static void add_pattern(char *pat, size_t len) { + + /* Check if we can do a shortcut */ if (len == 0 || matchall) { matchall = 1; return; } - + /* Increase size if necessary */ if (patterns == pattern_sz) { pattern_sz *= 2; pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern)); @@ -268,6 +270,8 @@ static void add_epattern(char *pat, size_t len) { + + /* Increase size if necessary */ if (epatterns == epattern_sz) { epattern_sz *= 2; epattern = grep_realloc(epattern, ++epattern_sz * sizeof(*epattern)); @@ -312,6 +316,9 @@ catalog = catopen("grep", NL_CAT_LOCALE); #endif + /* Check what is the program name of the binary. In this + way we can have all the funcionalities in one binary + without the need of scripting and using ugly hacks. */ switch (__progname[0]) { case 'e': grepbehave = GREP_EXTENDED; @@ -549,9 +556,11 @@ argc -= optind; argv += optind; + /* Fail if we don't have any pattern */ if (argc == 0 && needpattern) usage(); + /* Process patterns from command line */ if (argc != 0 && needpattern) { add_pattern(*argv, strlen(*argv)); --argc; @@ -580,6 +589,7 @@ usage(); } if (grepbehave != GREP_PERL) { + /* Compile regexes with regcomp() */ r_pattern = grep_calloc(patterns, sizeof(*r_pattern)); for (i = 0; i < patterns; ++i) { c = regcomp(&r_pattern[i], pattern[i], cflags); @@ -591,6 +601,7 @@ } } else { #ifdef WITH_PCRE + /* Compile Perl regexes with pcre_compile() */ perl_pattern = grep_calloc(patterns, sizeof(perl_pattern)); for (i = 0; i < patterns; ++i) { char **err_msg = NULL; @@ -624,6 +635,8 @@ catclose(catalog); #endif + /* Find out the correct return value according to the + results and the command line option. */ if (c) { if (notfound && qflag) exit(0); ==== //depot/projects/soc2008/gabor_textproc/grep/util.c#66 (text+ko) ==== @@ -91,10 +91,12 @@ case FTS_D: break; case FTS_DC: + /* Print a warning if there is a recursive directory loop */ warnx("warning: %s: recursive directory loop", p->fts_path); break; default: + /* Check for file exclusion/inclusion */ if (exclflag) { ok = 1; for (i = 0; i < epatterns; ++i) @@ -146,6 +148,7 @@ f = grep_stdin_open(); } else { if (!stat(fn, &sb)) { + /* Check if we need to process the file */ s = sb.st_mode & S_IFMT; if (s == S_IFDIR && dirbehave == DIR_SKIP) return (0); @@ -180,17 +183,19 @@ --ln.len; ln.line_no++; + /* Return if we need to skip a binary file */ if (f->binary && binbehave == BINFILE_SKIP) { grep_close(f); return (0); } - + /* Process the file line-by-line */ if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) { enqueue(&ln); linesqueued++; } c += t; + /* Count the matches if we have a match limit */ if (mflag) { mcount -= t; if (mcount <= 0) @@ -240,10 +245,12 @@ if (!matchall) { t = vflag ? REG_NOMATCH : 0; + /* Loop to process the whole line */ while (st <= l->len) { pmatch.rm_so = st; pmatch.rm_eo = l->len; + /* Loop to compare with all the patterns */ for (i = 0; i < patterns; i++) { if (grepbehave != GREP_PERL) { r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags); @@ -260,9 +267,11 @@ } if (r == REG_NOMATCH && t == 0) continue; + /* Check for full match */ if (r == 0 && xflag) if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len) r = REG_NOMATCH; + /* Check for whole word match */ if (r == 0 && wflag && pmatch.rm_so != 0 && pmatch.rm_eo != l->len) { wchar_t *wbegin; wint_t wend; @@ -297,6 +306,7 @@ if (c && binbehave == BINFILE_BIN && nottext) return (c); /* Binary file */ + /* Dealing with the context */ if ((tail || (c && !vflag)) && !cflag && !qflag) { if (c) { if (!first && !prev && !tail && Aflag) @@ -394,6 +404,7 @@ } if (n) putchar(sep); + /* --color and -o */ if ((oflag || color) && m > 0) { for (i = 0; i < m; i++) { if (!oflag)