162856Sdcs#! /bin/sh
262856Sdcs# mkh - pull headers out of C source
362856Sdcs# $FreeBSD$
462856SdcsPATH=/bin:/usr/bin ; export PATH
562856Sdcs
662856Sdcs# egrep pattern to pick out marked lines
762856Sdcsegrep='^ =([ 	]|$)'
862856Sdcs
962856Sdcs# Sed program to process marked lines into lines for the header file.
1062856Sdcs# The markers have already been removed.  Two things are done here:  removal
1162856Sdcs# of backslashed newlines, and some fudging of comments.  The first is done
1262856Sdcs# because -o needs to have prototypes on one line to strip them down.
1362856Sdcs# Getting comments into the output is tricky; we turn C++-style // comments
1462856Sdcs# into /* */ comments, after altering any existing */'s to avoid trouble.
1562856Sdcspeel='	/\\$/N
1662856Sdcs	/\\\n[ 	]*/s///g
1762856Sdcs	/\/\//s;\*/;* /;g
1862856Sdcs	/\/\//s;//\(.*\);/*\1 */;'
1962856Sdcs
2062856Sdcsfor a
2162856Sdcsdo
2262856Sdcs	case "$a" in
2362856Sdcs	-o)	# old (pre-function-prototype) compiler
2462856Sdcs		# add code to comment out argument lists
2562856Sdcs		peel="$peel
2662856Sdcs			"'/^\([^#\/][^\/]*[a-zA-Z0-9_)]\)(\(.*\))/s;;\1(/*\2*/);'
2762856Sdcs		shift
2862856Sdcs		;;
2962856Sdcs	-b)	# funny Berkeley __P macro
3062856Sdcs		peel="$peel
3192971Sobrien			"'/^\([^#\/][^\/]*[a-zA-Z0-9_)]\)(\(.*\))/s;;\1 __P((\2));'
3262856Sdcs		shift
3362856Sdcs		;;
3462856Sdcs	-s)	# compiler doesn't like `static foo();'
3562856Sdcs		# add code to get rid of the `static'
3662856Sdcs		peel="$peel
3762856Sdcs			"'/^static[ 	][^\/]*[a-zA-Z0-9_)](.*)/s;static.;;'
3862856Sdcs		shift
3962856Sdcs		;;
4062856Sdcs	-p)	# private declarations
4162856Sdcs		egrep='^ ==([ 	]|$)'
4262856Sdcs		shift
4362856Sdcs		;;
4462856Sdcs	-i)	# wrap in #ifndef, argument is name
4562856Sdcs		ifndef="$2"
4662856Sdcs		shift ; shift
4762856Sdcs		;;
4862856Sdcs	*)	break
4962856Sdcs		;;
5062856Sdcs	esac
5162856Sdcsdone
5262856Sdcs
5362856Sdcsif test " $ifndef" != " "
5462856Sdcsthen
5562856Sdcs	echo "#ifndef $ifndef"
5662856Sdcs	echo "#define	$ifndef	/* never again */"
5762856Sdcsfi
5862856Sdcsecho "/* ========= begin header generated by $0 ========= */"
5962856Sdcsecho '#ifdef __cplusplus'
6062856Sdcsecho 'extern "C" {'
6162856Sdcsecho '#endif'
6262856Sdcsfor f
6362856Sdcsdo
6462856Sdcs	echo
6562856Sdcs	echo "/* === $f === */"
6662856Sdcs	egrep "$egrep" $f | sed 's/^ ==*[ 	]//;s/^ ==*$//' | sed "$peel"
6762856Sdcs	echo
6862856Sdcsdone
6962856Sdcsecho '#ifdef __cplusplus'
7062856Sdcsecho '}'
7162856Sdcsecho '#endif'
7262856Sdcsecho "/* ========= end header generated by $0 ========= */"
7362856Sdcsif test " $ifndef" != " "
7462856Sdcsthen
7562856Sdcs	echo "#endif"
7662856Sdcsfi
7762856Sdcsexit 0
78