#!/bin/csh -f
# list all *.[ch] file names which contains the pattern given as argv[1].
#   -v show the lines too.
#   -e co and invoke editor on files with matching pattern.

if ($#argv == 0) goto usage;
if ("$argv[1]" == "-help") goto usage;

if ("$argv[1]" == "-v") then
    shift
    set pattern = "$argv[1]"
    onintr cleanup
    set tmp = /tmp/findrefs.$$
    set cmd = "grep "\""$pattern"\"" {} > $tmp"
    find . -name '*.[ch]' -exec sh -c "$cmd" \; -print -exec cat $tmp \;
    cleanup:
    rm -f $tmp

else if ("$argv[1]" == "-e") then
    shift
    set pattern = "$argv[1]"
    onintr cleanup1
    set tmp = /tmp/findrefs.$$
    find . -name '*.[ch]' \
    	-exec grep -q "$pattern" {} \; \
	\( -perm -200 -o -exec co -l {} \; \) \
	-print \
	> $tmp
    if (-s $tmp) then
	vi "+/$pattern" `cat $tmp`
    else
	echo No matches.
    endif
    cleanup1:
    rm -f $tmp

else
    find . -name '*.[ch]' -exec grep -l "$argv[1]" {} \;

endif

exit 0

usage:
echo "$0:t"":" "[-ev] <pattern>"
echo "  -e: co and invoke editor on files with matching pattern"
echo "  -v: show the lines themselves too";
exit 1

# For RCS Only -- Do Not Edit
# @(#) $RCSfile: findrefs,v $ $Date: 2003/04/15 20:47:55 $ $Revision: 1.1.1.1 $ $Name:  $
