Add AC_PROG_GREP check.

Based on feedback from Maciej for Solaris, abstract out calls to grep
and stop using grep -q.