: mktags -- make tags files that work regardless of the directory

PATH=/bin:/usr/bin; export PATH

ctags=/usr/local/bin/ctags
if [ ! -x $ctags ]
then
	ctags=/usr/lbin/ctags
	if [ ! -x $ctags ]
	then
		echo "mktags: can't locate public domain ctags!!!"
		exit 1
	fi
fi

/bin/rm -f tags

# let ctags do most of the hard work (public domain ctags will do typedefs too)

$ctags -t *.[ch] */*.[ch] 2>/dev/null
mv tags tags1
$ctags -t ../iopm/sys/*.h 2>/dev/null
cat tags1 >> tags
/bin/rm tags1

# use ed to truncate patterns and undo ctags's Mmain convention

echo '
1,$s/(.*$/(/
/^Mmain/s/Mm/m/
w
q' | ed tags >> /dev/null 2>&1

# now use egrep and sed to include extra typedef, struct, and union stuff

t='	'				# a tab
wt='[ 	]*'				# match any white space
nwt='[^ 	]*'			# match all until white space
file="\([^:]*\)"			# match (and remember) filename
id='\([A-Z_a-z][0-9A-Z_a-z]*\)'		# match (and remember) identifier
nid='[^0-9A-Z_a-z]*'			# match until an identifier

p1="\|struct|s|^$file:\(${wt}typedef${wt}struct${wt}\)${id}.*$|\3$t\1$t/^\2\3|p"
p2="\|union|s|^$file:\(${wt}typedef${wt}union${wt}\)${id}.*$|\3$t\1$t/^\2\3|p"
p3="\|}|s|^$file:\(${wt}}${nid}\)${id}.*$|\3$t\1$t/^\2\3|p"
p4="\|struct|s|^$file:\(${wt}struct${wt}\)${id}\(${wt}{\).*$|\3$t\1$t/^\2\3\4|p"
p5="\|union|s|^$file:\(${wt}union${wt}\)${id}\(${wt}{\).*$|\3$t\1$t/^\2\3\4|p"

egrep -f :egrep.pats *.h */*.h ../iopm/sys/*.h | \
sed -n -e "$p1" -e "$p2" -e "$p3" -e "$p4" -e "$p5" >> tags

# now more egrep and sed for the code in the ml directory

p1="\|:|s|^$file:\([A-Z_a-z][0-9%A-Z_a-z]*\):.*$|\2$t\1$t/^\2:|p"

egrep : */*.s ../iopm/ml/*.s | sed -n -e "$p1" >> tags

sort -u +0 -1 tags -o tags		# sort 'em
