xref: /netbsd-src/usr.bin/lorder/lorder.sh (revision a291dbbc012c5a2db90f811d16ef4b8e977fe7a8)
161f28255Scgd#!/bin/sh -
2*a291dbbcSapb#	$NetBSD: lorder.sh,v 1.15 2007/01/14 16:29:35 apb Exp $
36d298651Sjtc#
4d5252c47Sjtc# Copyright (c) 1990, 1993
5d5252c47Sjtc#	The Regents of the University of California.  All rights reserved.
661f28255Scgd#
761f28255Scgd# Redistribution and use in source and binary forms, with or without
861f28255Scgd# modification, are permitted provided that the following conditions
961f28255Scgd# are met:
1061f28255Scgd# 1. Redistributions of source code must retain the above copyright
1161f28255Scgd#    notice, this list of conditions and the following disclaimer.
1261f28255Scgd# 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd#    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd#    documentation and/or other materials provided with the distribution.
1561f28255Scgd# 3. All advertising materials mentioning features or use of this software
1661f28255Scgd#    must display the following acknowledgement:
1761f28255Scgd#	This product includes software developed by the University of
1861f28255Scgd#	California, Berkeley and its contributors.
1961f28255Scgd# 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd#    may be used to endorse or promote products derived from this software
2161f28255Scgd#    without specific prior written permission.
2261f28255Scgd#
2361f28255Scgd# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd# SUCH DAMAGE.
3461f28255Scgd#
35d5252c47Sjtc#	@(#)lorder.sh	8.1 (Berkeley) 6/6/93
3661f28255Scgd#
3761f28255Scgd
38e765fcaeScgd# If the user has set ${NM} then we use it, otherwise we use 'nm'.
39*a291dbbcSapb# Similarly for JOIN, MKTEMP, SED, and SORT.
40*a291dbbcSapb#
41*a291dbbcSapb# For each of these, we try to find the command in the user's path, and
42*a291dbbcSapb# if that fails we try to find it in the default path.  If we can't
43*a291dbbcSapb# find it, we punt.  Once we find it, we canonicalize its name and set
44*a291dbbcSapb# the path to the default path so that other commands we use are picked
45*a291dbbcSapb# properly.
46e765fcaeScgd
47*a291dbbcSapb: ${JOIN:=join}
48*a291dbbcSapb: ${MKTEMP:=mktemp}
49*a291dbbcSapb: ${NM:=nm}
50*a291dbbcSapb: ${SED:=sed}
51*a291dbbcSapb: ${SORT:=sort}
52*a291dbbcSapbfor var in JOIN MKTEMP NM SED SORT ; do
53*a291dbbcSapb	if ! eval type "\$${var}" >/dev/null 2>&1 ; then
54e765fcaeScgd		PATH=/bin:/usr/bin
55e765fcaeScgd		export PATH
56*a291dbbcSapb		if ! eval type "\$${var}" > /dev/null 2>&1; then
57*a291dbbcSapb			eval echo "lorder: \$${var}: not found" >&2
58e765fcaeScgd			exit 1
59e765fcaeScgd		fi
60e765fcaeScgd	fi
61*a291dbbcSapb	cmd='set $(eval type "\$${var}") ; eval echo \$$#'
62*a291dbbcSapb	eval "${var}=\$(eval \${cmd})"
63*a291dbbcSapbdone
64fece31c9Sthorpej
6561f28255Scgd# only one argument is a special case, just output the name twice
6661f28255Scgdcase $# in
6761f28255Scgd	0)
68d62839b8Sbjh21		echo "usage: lorder file ..." >&2;
6961f28255Scgd		exit ;;
7061f28255Scgd	1)
7161f28255Scgd		echo $1 $1;
7261f28255Scgd		exit ;;
7361f28255Scgdesac
7461f28255Scgd
7561f28255Scgd# temporary files
76*a291dbbcSapbN=$("${MKTEMP}" /tmp/_nm_.XXXXXX) || exit 1
77*a291dbbcSapbR=$("${MKTEMP}" /tmp/_reference_.XXXXXX) || exit 1
78*a291dbbcSapbS=$("${MKTEMP}" /tmp/_symbol_.XXXXXX) || exit 1
7961f28255Scgd
8079c6c68dSlukem# remove temporary files on exit
8133333aa3Stvtrap "rm -f $N $R $S; exit 0" 0
8233333aa3Stvtrap "rm -f $N $R $S; exit 1" HUP INT QUIT PIPE TERM 2>/dev/null || \
8333333aa3Stv	trap "rm -f $N $R $S; exit 1" 1 2 3 13 15
8461f28255Scgd
8561f28255Scgd# if the line ends in a colon, assume it's the first occurrence of a new
8661f28255Scgd# object file.  Echo it twice, just to make sure it gets into the output.
8761f28255Scgd#
8861f28255Scgd# if the line has " T " or " D " it's a globally defined symbol, put it
8961f28255Scgd# into the symbol file.
9061f28255Scgd#
9161f28255Scgd# if the line has " U " it's a globally undefined symbol, put it into
9261f28255Scgd# the reference file.
93e9109c51Smycroft(for file in $* ; do echo $file":" ; done ; $NM -go $*) >$N
94*a291dbbcSapb"${SED}" -ne '/:$/{s/://;s/.*/& &/;p;}' <$N
95*a291dbbcSapb"${SED}" -ne 's/:.* [TDGR] / /p' <$N >$S
96*a291dbbcSapb"${SED}" -ne 's/:.* U / /p' <$N >$R
9761f28255Scgd
980b5ed2adSchristos# sort symbols and references on the second field (the symbol)
9961f28255Scgd# join on that field, and print out the file names.
100*a291dbbcSapb"${SORT}" -k2 $R -o $R
101*a291dbbcSapb"${SORT}" -k2 $S -o $S
102*a291dbbcSapb"${JOIN}" -j 2 -o 1.1,2.1 $R $S
103