#! /usr/bin/sh
#	LORDER
#
# @(#) 92453-07 command s800.sgs lorder A.10.44 950918

#
trap "rm -f /var/tmp/$$sym?ef; exit" 0 1 2 13 15

STDIN="FALSE" 

case $# in
0)      STDIN="TRUE";;
1)      case $1 in
	*.o)    set $1 $1
	esac
esac

name1=""
name2=""

if [ "$STDIN" = "TRUE" ]
then
	read name1
	read name2
        if [ "$name2" = "" ]
        then
                case "$name1" in
                *.o) list="$name1 $name1";;
                esac
                echo "$list"
        else
                list="$name1 $name2"
                echo "$list"
                while read name1
                do
                        echo "$name1"
                done
        fi
else
        echo "$*"
fi | 

#       This sed script removes instances of "--".  Below we add a -- in front
#	of the argument we pass to nm.

sed -e 's/^--[ 	]//g'						\
    -e 's/[ 	]--[ 	]/ /g' 					\
    -e 's/[ 	]--$//'  					|

#	The following sed script is commented here.
#	The egrep pipe insures that we only have lines
#	that contain file names and the external
#	declarations associated with each file.
#	The first two parts of the sed script put the pattern
#	(in this case the file name) into the hold space
#	and creates the "filename filename" lines and
#	writes them out. The first part is for .o files,
#	the second is for .o's in archives.
#	The next 2 sections of code are exactly alike but
#	they handle different external symbols, namely the
#	symbols that are defined in the file or symbols
#	that are referenced but not defined in this file.
#	A line containing the symbol (from the pattern space) and 
#	the file it is referenced in (from the hold space) is
#	put into the pattern space.
#	If it's defined it is written out to the symbol definition
#	(symdef) file, otherwise it was referenced but not declared
#	in this file so it is written out to the symbol referenced
#	(symref) file.
#

xargs nm -e -- | egrep "\\.o:|\\.o]:|\\|extern|\\|undef" |
	sed '
	/\.o:$/{
		s/://
		s/^.* //
		h
		s/.*/& &/
		p
		d
	}
	/\.o]:$/{
		s/]://
		s/^.*\[//
		h
		s/.*/& &/
		p
		d
	}
	/|extern/{
		s/ *|.*//
		G
		s/\n/ /
		w '/var/tmp/$$symdef'
		d
	}
	s/ *|.*//
	G
	s/\n/ /
	w '/var/tmp/$$symref'
	d
'
sort -o /var/tmp/$$symdef /var/tmp/$$symdef
sort -o /var/tmp/$$symref /var/tmp/$$symref
join /var/tmp/$$symref /var/tmp/$$symdef | sed 's/[^ ]* *//'
