From owner-p4-projects@FreeBSD.ORG Thu Jun 5 21:46:20 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 74D5F1065674; Thu, 5 Jun 2008 21:46:20 +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 30AF01065672 for ; Thu, 5 Jun 2008 21:46:20 +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 1ECAF8FC0A for ; Thu, 5 Jun 2008 21:46:20 +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 m55LkJPY089443 for ; Thu, 5 Jun 2008 21:46:19 GMT (envelope-from gabor@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id m55LkJkE089441 for perforce@freebsd.org; Thu, 5 Jun 2008 21:46:19 GMT (envelope-from gabor@freebsd.org) Date: Thu, 5 Jun 2008 21:46:19 GMT Message-Id: <200806052146.m55LkJkE089441@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 142987 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: Thu, 05 Jun 2008 21:46:20 -0000 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;