1#! /bin/sh
2# mkh - pull headers out of C source
3PATH=/bin:/usr/bin ; export PATH
4
5# egrep pattern to pick out marked lines
6egrep='^ =([ 	]|$)'
7
8# Sed program to process marked lines into lines for the header file.
9# The markers have already been removed.  Two things are done here:  removal
10# of backslashed newlines, and some fudging of comments.  The first is done
11# because -o needs to have prototypes on one line to strip them down.
12# Getting comments into the output is tricky; we turn C++-style // comments
13# into /* */ comments, after altering any existing */'s to avoid trouble.
14peel='	/\\$/N
15	/\\\n[ 	]*/s///g
16	/\/\//s;\*/;* /;g
17	/\/\//s;//\(.*\);/*\1 */;'
18
19for a
20do
21	case "$a" in
22	-o)	# old (pre-function-prototype) compiler
23		# add code to comment out argument lists
24		peel="$peel
25			"'/^\([^#\/][^\/]*[a-zA-Z0-9_)]\)(\(.*\))/s;;\1(/*\2*/);'
26		shift
27		;;
28	-b)	# funny Berkeley __P macro
29		peel="$peel
30			"'/^\([^#\/][^\/]*[a-zA-Z0-9_)]\)(\(.*\))/s;;\1 __P((\2));'
31		shift
32		;;
33	-s)	# compiler doesn't like `static foo();'
34		# add code to get rid of the `static'
35		peel="$peel
36			"'/^static[ 	][^\/]*[a-zA-Z0-9_)](.*)/s;static.;;'
37		shift
38		;;
39	-p)	# private declarations
40		egrep='^ ==([ 	]|$)'
41		shift
42		;;
43	-i)	# wrap in #ifndef, argument is name
44		ifndef="$2"
45		shift ; shift
46		;;
47	*)	break
48		;;
49	esac
50done
51
52if test " $ifndef" != " "
53then
54	echo "#ifndef $ifndef"
55	echo "#define	$ifndef	/* never again */"
56fi
57echo "/* ========= begin header generated by $0 ========= */"
58echo '#ifdef __cplusplus'
59echo 'extern "C" {'
60echo '#endif'
61for f
62do
63	echo
64	echo "/* === $f === */"
65	egrep "$egrep" $f | sed 's/^ ==*[ 	]//;s/^ ==*$//' | sed "$peel"
66	echo
67done
68echo '#ifdef __cplusplus'
69echo '}'
70echo '#endif'
71echo "/* ========= end header generated by $0 ========= */"
72if test " $ifndef" != " "
73then
74	echo "#endif"
75fi
76exit 0
77