#!/bin/sh

# predefs: derived from what-defs.  Added cc/gcc distinction, path searches.
# tested on SunOS 4.1.x, Solaris 2.x

# what-defs: prints names of predefined cpp macros
# tested on Sun3, Sun4, SGI, HP, Stellar, Convex, DECstation, Moto 1147

# stupid, stupid Ultrix! I have a "which" function that I cannot use, because
# the Ultrix /bin/sh does not understand shell functions! (try /bin/sh5!)
# also, the Ultrix "test" command does not understand the -x flag!

TMP=/tmp/predef.$$.c
export TMP

if locatable cc
then
	echo 'predefines for cc (heuristic, but usually close):'
	path="`echo $PATH | sed -e 's/:/ /g'`"
	cc=`for dir in $path ; do
	    if [ -r $dir/cc ] ; then
		echo $dir/cc
		exit
	    fi
	done`
	export cc
	cpp=`for dir in $path /usr/ccs/lib ; do
	    if [ -r $dir/cpp ] ; then
		echo $dir/cpp
		exit
	    fi
	done`
	export cpp

	strings -a -2 $cc $cpp | \
	sed -e 's/^-D//' |
	sort -u |
	sed -n '/^[a-zA-Z_][a-zA-Z0-9_]*$/s//#ifdef &\
	"%&"\
#endif/p' > $TMP

	if `echo $cc | awk ' { print $1 }'` -E $TMP | \
		grep '"%.*"' | \
		sed 's/.*"%\(.*\)"/\1/'
	then
		:
		rm -f $TMP
	else
		:
	fi
fi

if locatable gcc
then
	echo 'main(){}' > $TMP
	echo 'predefines for gcc (apparently a subset):'
	gcc -E -dM $TMP
	echo 'predefines for gcc -ansi (apparently a subset):'
	gcc -ansi -E -dM $TMP
	echo 'predefines for gcc -traditional (apparently a subset):'
	gcc -traditional -E -dM $TMP
	rm -f $TMP
fi