
# UNISRC_ID: @(#)entrypts.sh	26.1	84/07/03  
# Script to create a sorted list of entrypoints (defined, external,
# code or data) from library archives.

# Status and error messages go to stderr, leaving stdout for results.


	usage="usage: $0 [-dlv] libraries..."

# Check arguments:

	set -- `getopt dlv $*`
	if [ $? != 0 ]			# bad options.
	then
		echo $usage 1>&2
		exit 1
	fi

	dflag=CODE			# default = code, not data.
	lflag=0				# default = sort all, not by library.
	vflag=0				# default = not verbose.

	for arg in $*
	do
		case $arg in
		-d)	dflag=DATA;;	# select data.
		-l)	lflag=1;;	# sort by library.
		-v)	vflag=1;;	# verbose.
		--)	break;;
		esac
		shift
	done
	shift

	if [ $# -lt 1 ]			# missing arguments.
	then
		echo $usage 1>&2
		exit 1
	fi

# Initialize:

	if [ $dflag = "CODE" ]
	then	str1="LABEL";	str2="SYSTEM";	str3="FUNC"
	else	str1="PTR";	str2="COMM";	str3="DATA"
	fi

	tab="	"

	temp=/tmp/entrypoints
	rm -f $temp
	trap "rm -f $temp; exit" 0 1 2 3
	set -e				# exit on any error.

# Print title to stdout:

	echo "${tab}LIBRARY $dflag ENTRYPOINTS\c"
	if [ $lflag = 1 ]
	then
		echo ", GROUPED BY LIBRARY\c"
	fi
	echo "\n"

# Do each library, listing the names to stderr when started:

	for lib in $*
	do
		if [ $vflag = 1 ]
		then
			echo $lib 1>&2
		fi

# Set up values for sed:

		if [ $lflag = 0 ]
		then	# will append $lib to lines:
			pre="$tab"		# indent data once only.
			app1="$tab$lib"		# for long names.
			app2="$tab$tab$lib"	# for short names.
		else	# echo $lib only once:
			echo "$tab$lib:"
			pre="$tab$tab"		# indent data twice.
			app1=""
			app2=""
		fi

# The sed does the following:
#   converts each $str1 and $str2 to $str3;
#   removes all lines that don't contain $str3;
#   removes all lines that start with "U" (undefined);
#   removes any trailing blanks or tabs;
#   removes all but the last token (the entrypoint name);
#   appends the file name (if any);
#   prepends the indentation (if any);
#   saves the results in $temp.

		nm -g $lib |
		sed	-e "s/$str1/$str3/"		\
			-e "s/$str2/$str3/"		\
			-e "/$str3/!d"			\
			-e "/^ *U/d"			\
			-e "s/[ $tab]*$//"		\
			-e "s/.* //"			\
			-e "/......../s%$%$app1%"	\
			-e "/$tab/!s%$%$app2%"		\
			-e "s/^/$pre/"			>>$temp
		
# If by library, dump the data now:

		if [ $lflag = 1 ]
		then
			sort $temp
			rm -f $temp
		fi

	done # with one library.

# If not by library, sort and print the total list:

	if [ $lflag = 0 ]
	then
		if [ $vflag = 1 ]
		then
			echo "sorting results..." 1>&2
		fi
		sort $temp
	fi
