#!/bin/sh
# incgrep --- search all header files in known include directories for pattern
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1996-01-29

# Copyright (C) 1995, 1996 Noah S. Friedman

# $Id: incgrep,v 2.3 1996/03/03 22:54:01 friedman Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# This script works better if you have GCC version 2 and GNU `find'.

# Code:

cc=${CC-gcc}

includes='
  /usr/local/gnu/include
  /usr/local/include
  /usr/include
'

if ($cc -v) > /dev/null 2>&1; then
  version=`gcc -v 2>&1 \
            | sed -ne '/gcc version/!d
                       s/gcc version  *//
                       s/^2\..*/2/
                       p
                      '`
  case "$version" in
    2 )
      ccdir=`$cc -print-libgcc-file-name | sed -e 's/\/[^/]*$//'`
      cpp=$ccdir/cpp

      includes=`$cpp -v 2>&1 < /dev/null \
                  | sed -ne '/#include <...> search starts here:/!d
                             n
                             :l
                             /^End of search list./!{
                               p
                               n
                               b l
                             }'`
     ;;
  esac
fi

if (find --version) > /dev/null 2>&1 ; then
   follow=-follow
else
   follow=
fi

find $includes $follow -name '*.h' -print 2> /dev/null \
 | xargs grep ${1+"$@"}

# incgrep ends here.
