xref: /freebsd/usr.bin/lorder/lorder.sh (revision 783d3ff6)
1#!/bin/sh -
2#
3# SPDX-License-Identifier: BSD-3-Clause
4#
5# Copyright (c) 1990, 1993
6#	The Regents of the University of California.  All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. Neither the name of the University nor the names of its contributors
17#    may be used to endorse or promote products derived from this software
18#    without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31
32export LC_CTYPE=C
33export LC_COLLATE=C
34set -e
35
36usage() {
37	echo "usage: lorder file ..." >&2
38	exit 1
39}
40
41while getopts "" opt ; do
42	case $opt in
43	*)
44		usage
45		;;
46	esac
47done
48shift $(($OPTIND - 1))
49if [ $# -eq 0 ] ; then
50	usage
51fi
52
53#
54# Create temporary files.
55#
56N=$(mktemp -t _nm_)
57R=$(mktemp -t _reference_)
58S=$(mktemp -t _symbol_)
59T=$(mktemp -t _temp_)
60NM=${NM:-nm}
61
62#
63# Remove temporary files on termination.
64#
65trap "rm -f $N $R $S $T" EXIT 1 2 3 13 15
66
67#
68# A line matching " [RTDW] " indicates that the input defines a symbol
69# with external linkage; put it in the symbol file.
70#
71# A line matching " U " indicates that the input references an
72# undefined symbol; put it in the reference file.
73#
74${NM} ${NMFLAGS} -go "$@" >$N
75sed -e "
76	/ [RTDW] / {
77		s/:.* [RTDW] / /
78		w $S
79		d
80	}
81	/ U / {
82		s/:.* U / /
83		w $R
84	}
85	d
86" <$N
87
88#
89# Elide entries representing a reference to a symbol from within the
90# library that defines it.
91#
92sort -u -o $S $S
93sort -u -o $R $R
94comm -23 $R $S >$T
95mv $T $R
96
97#
98# Make sure that all inputs get into the output.
99#
100for i ; do
101	echo "$i" "$i"
102done
103
104#
105# Sort references and symbols on the second field (the symbol), join
106# on that field, and print out the file names.
107#
108sort -k 2 -o $R $R
109sort -k 2 -o $S $S
110join -j 2 -o 1.1 -o 2.1 $R $S
111