xref: /minix3/usr.bin/lorder/lorder.sh (revision d71cc7b9f698d5e0edd9d57b3d9bd9975670f144)
1*d71cc7b9SLionel Sambuc#!/bin/sh -
2*d71cc7b9SLionel Sambuc#	$NetBSD: lorder.sh,v 1.15 2007/01/14 16:29:35 apb Exp $
3*d71cc7b9SLionel Sambuc#
4*d71cc7b9SLionel Sambuc# Copyright (c) 1990, 1993
5*d71cc7b9SLionel Sambuc#	The Regents of the University of California.  All rights reserved.
6*d71cc7b9SLionel Sambuc#
7*d71cc7b9SLionel Sambuc# Redistribution and use in source and binary forms, with or without
8*d71cc7b9SLionel Sambuc# modification, are permitted provided that the following conditions
9*d71cc7b9SLionel Sambuc# are met:
10*d71cc7b9SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
11*d71cc7b9SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
12*d71cc7b9SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
13*d71cc7b9SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
14*d71cc7b9SLionel Sambuc#    documentation and/or other materials provided with the distribution.
15*d71cc7b9SLionel Sambuc# 3. All advertising materials mentioning features or use of this software
16*d71cc7b9SLionel Sambuc#    must display the following acknowledgement:
17*d71cc7b9SLionel Sambuc#	This product includes software developed by the University of
18*d71cc7b9SLionel Sambuc#	California, Berkeley and its contributors.
19*d71cc7b9SLionel Sambuc# 4. Neither the name of the University nor the names of its contributors
20*d71cc7b9SLionel Sambuc#    may be used to endorse or promote products derived from this software
21*d71cc7b9SLionel Sambuc#    without specific prior written permission.
22*d71cc7b9SLionel Sambuc#
23*d71cc7b9SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*d71cc7b9SLionel Sambuc# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*d71cc7b9SLionel Sambuc# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*d71cc7b9SLionel Sambuc# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*d71cc7b9SLionel Sambuc# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*d71cc7b9SLionel Sambuc# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*d71cc7b9SLionel Sambuc# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*d71cc7b9SLionel Sambuc# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*d71cc7b9SLionel Sambuc# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*d71cc7b9SLionel Sambuc# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*d71cc7b9SLionel Sambuc# SUCH DAMAGE.
34*d71cc7b9SLionel Sambuc#
35*d71cc7b9SLionel Sambuc#	@(#)lorder.sh	8.1 (Berkeley) 6/6/93
36*d71cc7b9SLionel Sambuc#
37*d71cc7b9SLionel Sambuc
38*d71cc7b9SLionel Sambuc# If the user has set ${NM} then we use it, otherwise we use 'nm'.
39*d71cc7b9SLionel Sambuc# Similarly for JOIN, MKTEMP, SED, and SORT.
40*d71cc7b9SLionel Sambuc#
41*d71cc7b9SLionel Sambuc# For each of these, we try to find the command in the user's path, and
42*d71cc7b9SLionel Sambuc# if that fails we try to find it in the default path.  If we can't
43*d71cc7b9SLionel Sambuc# find it, we punt.  Once we find it, we canonicalize its name and set
44*d71cc7b9SLionel Sambuc# the path to the default path so that other commands we use are picked
45*d71cc7b9SLionel Sambuc# properly.
46*d71cc7b9SLionel Sambuc
47*d71cc7b9SLionel Sambuc: ${JOIN:=join}
48*d71cc7b9SLionel Sambuc: ${MKTEMP:=mktemp}
49*d71cc7b9SLionel Sambuc: ${NM:=nm}
50*d71cc7b9SLionel Sambuc: ${SED:=sed}
51*d71cc7b9SLionel Sambuc: ${SORT:=sort}
52*d71cc7b9SLionel Sambucfor var in JOIN MKTEMP NM SED SORT ; do
53*d71cc7b9SLionel Sambuc	if ! eval type "\$${var}" >/dev/null 2>&1 ; then
54*d71cc7b9SLionel Sambuc		PATH=/bin:/usr/bin
55*d71cc7b9SLionel Sambuc		export PATH
56*d71cc7b9SLionel Sambuc		if ! eval type "\$${var}" > /dev/null 2>&1; then
57*d71cc7b9SLionel Sambuc			eval echo "lorder: \$${var}: not found" >&2
58*d71cc7b9SLionel Sambuc			exit 1
59*d71cc7b9SLionel Sambuc		fi
60*d71cc7b9SLionel Sambuc	fi
61*d71cc7b9SLionel Sambuc	cmd='set $(eval type "\$${var}") ; eval echo \$$#'
62*d71cc7b9SLionel Sambuc	eval "${var}=\$(eval \${cmd})"
63*d71cc7b9SLionel Sambucdone
64*d71cc7b9SLionel Sambuc
65*d71cc7b9SLionel Sambuc# only one argument is a special case, just output the name twice
66*d71cc7b9SLionel Sambuccase $# in
67*d71cc7b9SLionel Sambuc	0)
68*d71cc7b9SLionel Sambuc		echo "usage: lorder file ..." >&2;
69*d71cc7b9SLionel Sambuc		exit ;;
70*d71cc7b9SLionel Sambuc	1)
71*d71cc7b9SLionel Sambuc		echo $1 $1;
72*d71cc7b9SLionel Sambuc		exit ;;
73*d71cc7b9SLionel Sambucesac
74*d71cc7b9SLionel Sambuc
75*d71cc7b9SLionel Sambuc# temporary files
76*d71cc7b9SLionel SambucN=$("${MKTEMP}" /tmp/_nm_.XXXXXX) || exit 1
77*d71cc7b9SLionel SambucR=$("${MKTEMP}" /tmp/_reference_.XXXXXX) || exit 1
78*d71cc7b9SLionel SambucS=$("${MKTEMP}" /tmp/_symbol_.XXXXXX) || exit 1
79*d71cc7b9SLionel Sambuc
80*d71cc7b9SLionel Sambuc# remove temporary files on exit
81*d71cc7b9SLionel Sambuctrap "rm -f $N $R $S; exit 0" 0
82*d71cc7b9SLionel Sambuctrap "rm -f $N $R $S; exit 1" HUP INT QUIT PIPE TERM 2>/dev/null || \
83*d71cc7b9SLionel Sambuc	trap "rm -f $N $R $S; exit 1" 1 2 3 13 15
84*d71cc7b9SLionel Sambuc
85*d71cc7b9SLionel Sambuc# if the line ends in a colon, assume it's the first occurrence of a new
86*d71cc7b9SLionel Sambuc# object file.  Echo it twice, just to make sure it gets into the output.
87*d71cc7b9SLionel Sambuc#
88*d71cc7b9SLionel Sambuc# if the line has " T " or " D " it's a globally defined symbol, put it
89*d71cc7b9SLionel Sambuc# into the symbol file.
90*d71cc7b9SLionel Sambuc#
91*d71cc7b9SLionel Sambuc# if the line has " U " it's a globally undefined symbol, put it into
92*d71cc7b9SLionel Sambuc# the reference file.
93*d71cc7b9SLionel Sambuc(for file in $* ; do echo $file":" ; done ; $NM -go $*) >$N
94*d71cc7b9SLionel Sambuc"${SED}" -ne '/:$/{s/://;s/.*/& &/;p;}' <$N
95*d71cc7b9SLionel Sambuc"${SED}" -ne 's/:.* [TDGR] / /p' <$N >$S
96*d71cc7b9SLionel Sambuc"${SED}" -ne 's/:.* U / /p' <$N >$R
97*d71cc7b9SLionel Sambuc
98*d71cc7b9SLionel Sambuc# sort symbols and references on the second field (the symbol)
99*d71cc7b9SLionel Sambuc# join on that field, and print out the file names.
100*d71cc7b9SLionel Sambuc"${SORT}" -k2 $R -o $R
101*d71cc7b9SLionel Sambuc"${SORT}" -k2 $S -o $S
102*d71cc7b9SLionel Sambuc"${JOIN}" -j 2 -o 1.1,2.1 $R $S
103