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