: 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

DIRLIST='cpu dbug dpm40 dsdb head io iopm local m68k ml os pm20 rwi tty'

for dir in $DIRLIST
do
	FILELIST="$FILELIST $dir/*.[ch]"
done

rm -f tags */tags

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

ctags=/usr/local/bin/ctags
$ctags -t $FILELIST 2>/dev/null

# 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 [hd][es][ad][db]/*.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 : ml/*.s | sed -n -e "$p1" >> tags

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

# process tags with sed to do the relative pathnames for subdirectories

tb='[	]*'				# match any tabs
ntb='[^	]*'				# match all until a tab

sed -e "s/^\($ntb\)$tb\($ntb\)$tb\(.*\)$/\1$t..\/\2$t\3/" tags > cpu/tags
for dir in $DIRLIST
do
	if [ "$dir" != "cpu" ]
	then
		ln cpu/tags $dir/tags
	fi
done
