.TH cgrep "" "" Command
.PC "Pattern search for C source programs"
\fBcgrep [\-clnsA] [\-r \fInew\fB] \fIexpression\fR \fIfile\fB ...\fR
.PP
.HS
.SH Options:
.IC \fB\-A\fR
Build \fIerror list\fR for interactive
editing using \*(ME, like \fB\-A\fR
option to the \fBcc\fR command.
.IC \fB\-c\fR
Print all C comments
.IC \fB\-l\fR
Return file where \fIexpression\fR found
.IC \fB\-n\fR
Prefix each line containing \fIexpressions\fR
with its number in its source file
.IC \fB\-r\fR
Replaces each \fIexpression\fR with \fInew\fR
.IC \fB\-s\fR
Print all C strings
.HE
.B cgrep
is a string-search utility.
It resembles its cousins
.B grep
and
.BR egrep ,
except that it is specially designed to be used with C source files.
It checks all C identifiers against
.I expression
and prints all lines in which it finds a match.
.B cgrep
allows you to search for a variable named `i' without finding every
`if' and `int' in your program.
.B cgrep
defines an ``identifier'' to be any variable name or C keyword.
.I expression
can be a regular expression; if it includes wildcard characters or `|'s, you
must \*(QLquote it\*(QR to protect it against being modified by the shell.
For details on the expressions that
.B cgrep
can recognize, see the Lexicon entry for
.BR egrep .
.PP
.B cgrep
tests names that include the `.' and `->' operators against
.IR expression .
Thus, to look for
.BR ptr->val ,
type:
.DM
	cgrep "ptr->val" x.c
.DE
.PP
This finds
.B ptr->val
even if it contains spaces, comments, or is spread across lines.
If it is spread across lines, it will be reported
on the line that contains the last token.
The only exception is if you include the
.B \-A
option, in which case it will be reported on the line which contains
the first token.
This is to simplify \*(ME macros, as will be described below.
.PP
To find
.BR structure.member ,
type:
.DM
	cgrep "structure\e.member"
.DE
.PP
because `.' in a regular expression matches any character.
.PP
Do not include spaces in any pattern.
Only identifiers and `.' or `->' between identifiers
are included in the tokens checked for pattern-matching.
.SH "Command-line Options"
.B cgrep
recognizes the following command-line options:
.IP \fB\-A\fR 0.3i
Write all lines in which
.I expression
is found into a temporary file.
Then, call \*(ME with its error option to process the source file,
with the contents of the temporary file serving as an ``error'' list.
This option resembles the \fB\-A\fR option to the \fBcc\fR command, and
lets you build a \*(ME script to make systematic changes to the
source file.
To exit \*(ME and prevent
.B cgrep
from searching further, \fB<ctrl-U> <ctrl-X> <ctrl-C>\fR.
.IP \fB\-c\fR
Print all comments in each
.IR file .
This form takes no expression.
.IP \fB\-l\fR
List only the names of the files in which
.I expression
is found.
.IP \fB\-n\fR
Prefix each line in which
.I expression
is found with its line number in the file.
.IP \fB\-r\fR
Replace all expression matches with \fInew\fR.
This option may not be used with any others,
and it can only match simple tokens, not items like \fBptr->val\fR.
When \fB\-r\fR is used and the input is
.BR stdin ,
a new file will always be created as
.BR stdout .
.IP \fB\-s\fR
Print all strings in each
.IR file .
This form takes no expression.
.SH Examples
The command
.DM
	cgrep tmp *.c
.DE
.PP
will find the variable name
.BR tmp ,
but not
.BR tmpname ,
or any occurrence of
.B tmp
in a string or comment.
.PP
The script
.DM
	cgrep -c < myfile.c | wc -l
.DE
.PP
count the lines of comments in
.BR myfile.c .
.PP
The command
.DM
	cgrep "x|abc|d" *.c
.DE
.PP
will find
.BR x ,
.BR ab ,
or
.BR d .
Note this is a regular expressions with a surrounding ``^( )$''
which is applied to every identifier.
Thus,
.B reg*
will not match
.BR register ,
but
.B reg.*
will.
.SH "See Also"
.Xr "commands," commands
.Xr "egrep," egrep
.Xr "grep," grep
.Xr "me" me
