11590Srgrimes#!/bin/sh -
21590Srgrimes#
31590Srgrimes# Copyright (c) 1990, 1993
41590Srgrimes#	The Regents of the University of California.  All rights reserved.
51590Srgrimes#
61590Srgrimes# Redistribution and use in source and binary forms, with or without
71590Srgrimes# modification, are permitted provided that the following conditions
81590Srgrimes# are met:
91590Srgrimes# 1. Redistributions of source code must retain the above copyright
101590Srgrimes#    notice, this list of conditions and the following disclaimer.
111590Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
121590Srgrimes#    notice, this list of conditions and the following disclaimer in the
131590Srgrimes#    documentation and/or other materials provided with the distribution.
141590Srgrimes# 4. Neither the name of the University nor the names of its contributors
151590Srgrimes#    may be used to endorse or promote products derived from this software
161590Srgrimes#    without specific prior written permission.
171590Srgrimes#
181590Srgrimes# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191590Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201590Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211590Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221590Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231590Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241590Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251590Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261590Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271590Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281590Srgrimes# SUCH DAMAGE.
291590Srgrimes#
301590Srgrimes#	@(#)lorder.sh	8.1 (Berkeley) 6/6/93
311590Srgrimes#
3260671Shoek# $FreeBSD$
3360671Shoek#
341590Srgrimes
351590Srgrimes# only one argument is a special case, just output the name twice
361590Srgrimescase $# in
371590Srgrimes	0)
381590Srgrimes		echo "usage: lorder file ...";
391590Srgrimes		exit ;;
401590Srgrimes	1)
411590Srgrimes		echo $1 $1;
421590Srgrimes		exit ;;
431590Srgrimesesac
441590Srgrimes
451590Srgrimes# temporary files
4660671ShoekR=$(mktemp -t _reference_)
4760671ShoekS=$(mktemp -t _symbol_)
4873882SdfrNM=${NM:-nm}
491590Srgrimes
501590Srgrimes# remove temporary files on HUP, INT, QUIT, PIPE, TERM
51148060Srutrap "rm -f $R $S $T; exit 1" 1 2 3 13 15
521590Srgrimes
5338322Sjb# make sure all the files get into the output
5438322Sjbfor i in $*; do
5538322Sjb	echo $i $i
5638322Sjbdone
5738322Sjb
58148060Sru# if the line has " [TDW] " it's a globally defined symbol, put it
591590Srgrimes# into the symbol file.
601590Srgrimes#
611590Srgrimes# if the line has " U " it's a globally undefined symbol, put it into
621590Srgrimes# the reference file.
6373882Sdfr${NM} -go $* | sed "
64148060Sru	/ [TDW] / {
65148060Sru		s/:.* [TDW] / /
661590Srgrimes		w $S
671590Srgrimes		d
681590Srgrimes	}
691590Srgrimes	/ U / {
701590Srgrimes		s/:.* U / /
711590Srgrimes		w $R
721590Srgrimes	}
731590Srgrimes	d
741590Srgrimes"
751590Srgrimes
76148060Sru# eliminate references that can be resolved by the same library.
77148060Sruif [ $(expr "$*" : '.*\.a[[:>:]]') -ne 0 ]; then
78148060Sru	sort -u -o $S $S
79148060Sru	sort -u -o $R $R
80148060Sru	T=$(mktemp -t _temp_)
81148060Sru	comm -23 $R $S >$T
82148060Sru	mv $T $R
83148060Srufi
84148060Sru
85148060Sru# sort references and symbols on the second field (the symbol),
861590Srgrimes# join on that field, and print out the file names.
87115647Stjrsort -k 2 -o $R $R
88115647Stjrsort -k 2 -o $S $S
891590Srgrimesjoin -j 2 -o 1.1 2.1 $R $S
901590Srgrimesrm -f $R $S
91