COHERENT manpages

This page displays the COHERENT manpage for cgrep [Pattern search for C source programs].

List of available manpages
Index


cgrep -- Command

Pattern search for C source programs
cgrep [-clnsA] [-r new] expression file ...

cgrep is a string-search utility.  It resembles its cousins grep and egrep,
except that  it is specially designed  to be used with  C source files.  It
checks all  C identifiers against expression and prints  all lines in which
it finds  a match.   cgrep allows  you to search  for a variable  named `i'
without finding  every `if'  and `int' in  your program.  cgrep  defines an
``identifier'' to be  any variable name or C keyword.   expression can be a
regular expression;  if it includes  wildcard characters or  `|'s, you must
``quote  it'' to  protect  it against  being  modified by  the shell.   For
details on the expressions that  cgrep can recognize, see the Lexicon entry
for egrep.

cgrep  tests  names that  include  the `.'  and  `->' operators  against
expression. Thus, to look for ptr->val, type:

    cgrep "ptr->val" x.c

This finds  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 -A option, in which case it will be reported on the line which contains
the  first  token.  This  is  to  simplify MicroEMACS  macros,  as will  be
described below.

To find structure.member, type:

    cgrep "structure\.member"

because `.' in a regular expression matches any character.

Do not include spaces in any  pattern.  Only identifiers and `.' or `->'
between  identifiers  are  included  in  the  tokens checked  for  pattern-
matching.

Command-line Options

cgrep recognizes the following command-line options:

-A Write  all lines  in which  expression is found  into a  temporary file.
   Then, call MicroEMACS 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  -A option  to the cc  command, and  lets you
   build a MicroEMACS script to make systematic changes to the source file.
   To exit  MicroEMACS and prevent cgrep  from searching further, <ctrl-
   U> <ctrl-X> <ctrl-C>.

-c Print all comments in each file. This form takes no expression.

-l List only the names of the files in which expression is found.

-n Prefix each  line in which expression  is found with its  line number in
   the file.

-r Replace all  expression matches with  new.  This option may  not be used
   with any  others, and it  can only match  simple tokens, not  items like
   ptr->val.  When  -r is used and  the input is stdin,  a new file will
   always be created as stdout.

-s Print all strings in each file. This form takes no expression.

Examples

The command

    cgrep tmp *.c

will find the variable name tmp,  but not tmpname, or any occurrence of tmp
in a string or comment.

The script

    cgrep -c < myfile.c | wc -l

count the lines of comments in myfile.c.

The command

    cgrep "x|abc|d" *.c

will  find  x,  ab,  or d.  Note  this  is  a  regular expressions  with  a
surrounding ``^(  )$'' which  is applied  to every identifier.   Thus, reg*
will not match register, but reg.* will.

See Also

commands,
egrep,
grep,
me