Date: Thu, 5 Jun 2008 21:46:19 GMT From: Gabor Kovesdan <gabor@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 142987 for review Message-ID: <200806052146.m55LkJkE089441@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=142987 Change 142987 by gabor@gabor_server on 2008/06/05 21:45:48 - Implement -o / --only-matching Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/grep.1#5 edit .. //depot/projects/soc2008/gabor_textproc/grep/grep.c#12 edit .. //depot/projects/soc2008/gabor_textproc/grep/grep.h#10 edit .. //depot/projects/soc2008/gabor_textproc/grep/util.c#12 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/grep.1#5 (text+ko) ==== @@ -29,7 +29,7 @@ .\" .\" @(#)grep.1 8.3 (Berkeley) 4/18/94 .\" -.Dd May 7, 2008 +.Dd 5 Jun, 2008 .Dt GREP 1 .Os .Sh NAME @@ -39,7 +39,7 @@ .Sh SYNOPSIS .Nm grep .Bk -words -.Op Fl abcDEFGHhIiLlnoPqRSsUVvwxZ +.Op Fl abcDEFGHhIiLlnOoPqRSsUVvwxZ .Op Fl A Ar num .Op Fl B Ar num .Op Fl C Ns Op Ar num @@ -251,12 +251,14 @@ specified. .It Fl Fl null Prints a zero-byte after the file name. -.It Fl o +.It Fl O If .Fl R is specified, follow symbolic links only if they were explicitly listed on the command line. The default is not to follow symbolic links. +.It Fl o, Fl Fl only-matching +Prints only the matching part of the lines. .\" XXX: UNIMPLEMENTED / INCOMPATIBLE .It Fl P .\"If .\".Fl R ==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#12 (text+ko) ==== @@ -84,6 +84,7 @@ int iflag; /* -i: ignore case */ int lflag; /* -l: only show names of files with matches */ 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) */ int sflag; /* -s: silent mode (ignore errors) */ int vflag; /* -v: only show non-matching lines */ @@ -120,14 +121,14 @@ usage(void) { fprintf(stderr, - "usage: %s [-abcDEFGHhIiLlnoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n" + "usage: %s [-abcDEFGHhIiLlnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n" "\t[-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n" "\t[--label] [--line-buffered] [--null] [pattern] [file ...]\n" , __progname); exit(2); } -static char *optstr = "0123456789A:B:CD:EFGHILPSRUVZabce:f:hilnoqrsuvwxy"; +static char *optstr = "0123456789A:B:CD:EFGHILOPSRUVZabce:f:hilnoqrsuvwxy"; struct option long_options[] = { @@ -164,8 +165,7 @@ /* XXX: UNIMPLEMENTED {"max-count", required_argument, NULL, 'm'}, */ {"line-number", no_argument, NULL, 'n'}, -/* XXX: UNIMPLEMENTED / INCOMPATIBLE - {"only-matching", no_argument, NULL, 'o'}, */ + {"only-matching", no_argument, NULL, 'o'}, {"quiet", no_argument, NULL, 'q'}, {"silent", no_argument, NULL, 'q'}, {"recursive", no_argument, NULL, 'r'}, @@ -384,6 +384,9 @@ case 'O': Oflag++; break; + case 'o': + oflag++; + break; case 'P': Pflag++; break; ==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#10 (text+ko) ==== @@ -62,7 +62,7 @@ /* Command line flags */ extern int Aflag, Bflag, Dflag, Eflag, Fflag, Gflag, Hflag, Lflag, Pflag, Sflag, Rflag, Zflag, - bflag, cflag, hflag, iflag, lflag, nflag, Oflag, qflag, sflag, + bflag, cflag, hflag, iflag, lflag, nflag, Oflag, oflag, qflag, sflag, vflag, wflag, xflag, nullflag; extern char *label; ==== //depot/projects/soc2008/gabor_textproc/grep/util.c#12 (text+ko) ==== @@ -215,6 +215,15 @@ if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len) r = REG_NOMATCH; } + if (r == 0 && oflag) { + char *tmp; + if ((tmp = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char))) == NULL) + errx(2, NULL); + strncpy(tmp, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1); + tmp[pmatch.rm_eo - pmatch.rm_so] = '\0'; + l->dat = tmp; + l->len = strlen(l->dat); + } if (r == 0) { c++; break;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200806052146.m55LkJkE089441>