1#!/bin/sh
2# $Id: listused.sh,v 1.5 2003/07/05 19:02:41 tom Exp $
3# A very simple script to list entrypoints that are used by either a test
4# program, or within the libraries.  This relies on the output format of 'nm',
5# and assumes that the libraries are configured with TRACE defined, and using
6# these options:
7#	--disable-macros
8#	--enable-widec
9# Static libraries are used, to provide some filtering based on internal usage
10# of the different symbols.
11
12# keep the sorting independent of locale:
13if test "${LANGUAGE+set}"    = set; then LANGUAGE=C;    export LANGUAGE;    fi
14if test "${LANG+set}"        = set; then LANG=C;        export LANG;        fi
15if test "${LC_ALL+set}"      = set; then LC_ALL=C;      export LC_ALL;      fi
16if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
17if test "${LC_CTYPE+set}"    = set; then LC_CTYPE=C;    export LC_CTYPE;    fi
18if test "${LC_COLLATE+set}"  = set; then LC_COLLATE=C;  export LC_COLLATE;  fi
19
20NM_OPTS=
21
22if test ! -d ../objects ; then
23	echo "? need objects to run this script"
24	exit 1
25elif test ! -d ../lib ; then
26	echo "? need libraries to run this script"
27	exit 1
28fi
29
30PROGS=
31for name in `(echo "test:";sort modules; echo "progs:";sort ../progs/modules) |sed -e 's/[ 	].*//' -e '/^[#@]/d'`
32do
33	case $name in
34	*:)
35		PROGS="$PROGS $name"
36		;;
37	*)
38		NAME=../objects/${name}.o
39		if test -f $NAME
40		then
41			PROGS="$PROGS $NAME"
42		fi
43		;;
44	esac
45done
46
47# For each library -
48for lib in ../lib/*.a
49do
50	LIB=`basename $lib .a`
51	case $LIB in
52	*_*|*+*)
53		continue
54		;;
55	esac
56
57	tmp=`echo $LIB|sed -e 's/w$//'`
58	echo
59	echo "${tmp}:"
60	echo $tmp |sed -e 's/./-/g'
61	# Construct a list of public externals provided by the library.
62	WANT=`nm $NM_OPTS $lib |\
63		sed	-e 's/^[^ ]*//' \
64			-e 's/^ *//' \
65			-e '/^[ a-z] /d' \
66			-e '/:$/d' \
67			-e '/^$/d' \
68			-e '/^U /d' \
69			-e 's/^[A-Z] //' \
70			-e '/^_/d' |\
71		sort -u`
72	# List programs which use that external.
73	for name in $WANT
74	do
75		HAVE=
76		tags=
77		last=
78		for prog in $PROGS
79		do
80			case $prog in
81			*:)
82				tags=$prog
83				;;
84			*)
85				TEST=`nm $NM_OPTS $prog |\
86					sed	-e 's/^[^ ]*//' \
87						-e 's/^ *//' \
88						-e '/^[ a-z] /d' \
89						-e '/:$/d' \
90						-e '/^$/d' \
91						-e 's/^[A-Z] //' \
92						-e '/^_/d' \
93						-e 's/^'${name}'$/_/' \
94						-e '/^[^_]/d'`
95				if test -n "$TEST"
96				then
97					have=`basename $prog .o`
98					if test -n "$HAVE"
99					then
100						if test "$last" = "$tags"
101						then
102							HAVE="$HAVE $have"
103						else
104							HAVE="$HAVE $tags $have"
105						fi
106					else
107						HAVE="$tags $have"
108					fi
109					last="$tags"
110				fi
111				;;
112			esac
113		done
114		# if we did not find a program using it directly, see if it
115		# is used within a library.
116		if test -z "$HAVE"
117		then
118			for tmp in ../lib/*.a
119			do 
120				case $tmp in
121				*_*|*+*)
122					continue
123					;;
124				esac
125				TEST=`nm $NM_OPTS $tmp |\
126					sed	-e 's/^[^ ]*//' \
127						-e 's/^ *//' \
128						-e '/^[ a-z] /d' \
129						-e '/:$/d' \
130						-e '/^$/d' \
131						-e '/^[A-TV-Z] /d' \
132						-e 's/^[A-Z] //' \
133						-e '/^_/d' \
134						-e 's/^'${name}'$/_/' \
135						-e '/^[^_]/d'`
136				if test -n "$TEST"
137				then
138					tmp=`basename $tmp .a |sed -e 's/w$//'`
139					HAVE=`echo $tmp | sed -e 's/lib/lib: /'`
140					break
141				fi
142			done
143		fi
144		test -z "$HAVE" && HAVE="-"
145		lenn=`expr 39 - length $name`
146		lenn=`expr $lenn / 8`
147		tabs=
148		while test $lenn != 0
149		do
150			tabs="${tabs}	"
151			lenn=`expr $lenn - 1`
152		done
153		echo "${name}${tabs}${HAVE}"
154	done
155done
156