#!/bin/sh

# @(#) findcpp 1.4 Delta: 91/05/23 17:26:59 Extraction 94/11/21 11:18:40 @(#)
#
# defining a version of strings so that we are not dependent of the system
#

cd /tmp
cat - <<\!EOF! >c$$.c
#include <stdio.h>

#define n(x) (sizeof(x)/sizeof(*(x)))

static char bfr[3]; /* size of determines minimum interesting string */

main()
{
	register char *bp = bfr;
	register int  chr;

	bfr[n(bfr)-1] = 0xff;
	while((chr = getc(stdin)) != EOF) {
		if((chr >= 'A' && chr <= 'I') ||
		   (chr >= 'J' && chr <= 'R') ||
		   (chr >= 'S' && chr <= 'Z') ||
		   (chr >= 'a' && chr <= 'i') ||
		   (chr >= 'j' && chr <= 'r') ||
		   (chr >= 's' && chr <= 'z') ||
		   (chr >= '0' && chr <= '9') ||
		   chr == '_' ||
		   chr == '-') {
			if(bp != (bfr+n(bfr)-1)) {
				*(bp++) = chr;
			} else {
				if(*bp) {
					*bp = '\0';
					fputs(bfr,stdout);
				}
				putc(chr,stdout);
			}
		} else {
			if(bp != bfr) {
				if(!bfr[n(bfr)-1]) {
					putc('\n',stdout);
				}
				bp = bfr;
				bfr[n(bfr)-1] = 0xff;
			}
		}
	}
	if(!bfr[n(bfr)-1]) {
		putc('\n',stdout);
	}
	
	exit(0);
}
!EOF!
cc c$$.c -o c$$

(
#
# Check the strings found in the C preprocessor (cpp)
#

PROG=/lib/cpp
(
echo 'main(){'
for s in `./c$$ <$PROG | sort | uniq | grep -v "^[0-9]" | \
          sed -e 's/^-D//' -e 's/-//g'`
do
	echo "#ifdef $s"
	echo '  printf("%s%c","'$s'",10);'
	echo '#endif'
done
echo 'exit(0);}'
)>p$$.c
cc p$$.c -o p$$
./p$$

#
# Check the strings found in the C compiler controller (cc)
#

P="`echo $PATH | sed -e 's/^:/.:/' -e 's/:$/:./' -e 's/::/:.:/g' -e 's/:/ /g'`"
for d in $P
do
	p=$d/cc
	if [ -f $p -a -x $p ]
	then
		PROG=$p
		break
	fi
done
rm -f ./p$$.c
(
echo 'main(){'
for s in `./c$$ <$PROG | sort | uniq | grep -v "^[0-9]" | \
          sed -e 's/^-D//' -e 's/-//g'`
do
	echo "#ifdef $s"
	echo '  printf("%s%c","'$s'",10);'
	echo '#endif'
done
echo 'exit(0);}'
)>p$$.c
cc p$$.c -o p$$
./p$$

) | sort | uniq

#
# Cleanup our trash
#
rm [cp]$$ [cp]$$.c

