xref: /freebsd/usr.bin/lorder/lorder.sh (revision aedb37dc49319a7cd1faf34f312a8a9fc01e611d)
19b50d902SRodney W. Grimes#!/bin/sh -
29b50d902SRodney W. Grimes#
38a16b7a1SPedro F. Giffuni# SPDX-License-Identifier: BSD-3-Clause
48a16b7a1SPedro F. Giffuni#
59b50d902SRodney W. Grimes# Copyright (c) 1990, 1993
69b50d902SRodney W. Grimes#	The Regents of the University of California.  All rights reserved.
79b50d902SRodney W. Grimes#
89b50d902SRodney W. Grimes# Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes# modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes# are met:
119b50d902SRodney W. Grimes# 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes#    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes# 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes#    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes#    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh# 3. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes#    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes#    without specific prior written permission.
199b50d902SRodney W. Grimes#
209b50d902SRodney W. Grimes# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes# SUCH DAMAGE.
319b50d902SRodney W. Grimes
325c7b986cSDag-Erling Smørgravexport LC_CTYPE=C
335c7b986cSDag-Erling Smørgravexport LC_COLLATE=C
345c7b986cSDag-Erling Smørgravset -e
359b50d902SRodney W. Grimes
365c7b986cSDag-Erling Smørgravusage() {
375c7b986cSDag-Erling Smørgrav	echo "usage: lorder file ..." >&2
385c7b986cSDag-Erling Smørgrav	exit 1
395c7b986cSDag-Erling Smørgrav}
405c7b986cSDag-Erling Smørgrav
415c7b986cSDag-Erling Smørgravwhile getopts "" opt ; do
425c7b986cSDag-Erling Smørgrav	case $opt in
435c7b986cSDag-Erling Smørgrav	*)
445c7b986cSDag-Erling Smørgrav		usage
455c7b986cSDag-Erling Smørgrav		;;
465c7b986cSDag-Erling Smørgrav	esac
475c7b986cSDag-Erling Smørgravdone
485c7b986cSDag-Erling Smørgravshift $(($OPTIND - 1))
495c7b986cSDag-Erling Smørgravif [ $# -eq 0 ] ; then
505c7b986cSDag-Erling Smørgrav	usage
515c7b986cSDag-Erling Smørgravfi
525c7b986cSDag-Erling Smørgrav
535c7b986cSDag-Erling Smørgrav#
545c7b986cSDag-Erling Smørgrav# Create temporary files.
555c7b986cSDag-Erling Smørgrav#
565c7b986cSDag-Erling SmørgravN=$(mktemp -t _nm_)
57b75cda59STim VanderhoekR=$(mktemp -t _reference_)
58b75cda59STim VanderhoekS=$(mktemp -t _symbol_)
595c7b986cSDag-Erling SmørgravT=$(mktemp -t _temp_)
607172dc3bSDoug RabsonNM=${NM:-nm}
619b50d902SRodney W. Grimes
629b50d902SRodney W. Grimes#
635c7b986cSDag-Erling Smørgrav# Remove temporary files on termination.
645c7b986cSDag-Erling Smørgrav#
655c7b986cSDag-Erling Smørgravtrap "rm -f $N $R $S $T" EXIT 1 2 3 13 15
665c7b986cSDag-Erling Smørgrav
675c7b986cSDag-Erling Smørgrav#
685c7b986cSDag-Erling Smørgrav# A line matching " [RTDW] " indicates that the input defines a symbol
695c7b986cSDag-Erling Smørgrav# with external linkage; put it in the symbol file.
705c7b986cSDag-Erling Smørgrav#
715c7b986cSDag-Erling Smørgrav# A line matching " U " indicates that the input references an
725c7b986cSDag-Erling Smørgrav# undefined symbol; put it in the reference file.
735c7b986cSDag-Erling Smørgrav#
745c7b986cSDag-Erling Smørgrav${NM} ${NMFLAGS} -go "$@" >$N
755c7b986cSDag-Erling Smørgravsed -e "
76e1d6d6f9SEd Maste	/ [RTDW] / {
77e1d6d6f9SEd Maste		s/:.* [RTDW] / /
789b50d902SRodney W. Grimes		w $S
799b50d902SRodney W. Grimes		d
809b50d902SRodney W. Grimes	}
819b50d902SRodney W. Grimes	/ U / {
829b50d902SRodney W. Grimes		s/:.* U / /
839b50d902SRodney W. Grimes		w $R
849b50d902SRodney W. Grimes	}
859b50d902SRodney W. Grimes	d
865c7b986cSDag-Erling Smørgrav" <$N
879b50d902SRodney W. Grimes
885c7b986cSDag-Erling Smørgrav#
895c7b986cSDag-Erling Smørgrav# Elide entries representing a reference to a symbol from within the
905c7b986cSDag-Erling Smørgrav# library that defines it.
915c7b986cSDag-Erling Smørgrav#
92e46be7ffSRuslan Ermilovsort -u -o $S $S
93e46be7ffSRuslan Ermilovsort -u -o $R $R
94e46be7ffSRuslan Ermilovcomm -23 $R $S >$T
95e46be7ffSRuslan Ermilovmv $T $R
96e46be7ffSRuslan Ermilov
975c7b986cSDag-Erling Smørgrav#
985c7b986cSDag-Erling Smørgrav# Make sure that all inputs get into the output.
995c7b986cSDag-Erling Smørgrav#
1005c7b986cSDag-Erling Smørgravfor i ; do
1015c7b986cSDag-Erling Smørgrav	echo "$i" "$i"
1025c7b986cSDag-Erling Smørgravdone
1035c7b986cSDag-Erling Smørgrav
1045c7b986cSDag-Erling Smørgrav#
1055c7b986cSDag-Erling Smørgrav# Sort references and symbols on the second field (the symbol), join
1065c7b986cSDag-Erling Smørgrav# on that field, and print out the file names.
1075c7b986cSDag-Erling Smørgrav#
108ebe5d44dSTim J. Robbinssort -k 2 -o $R $R
109ebe5d44dSTim J. Robbinssort -k 2 -o $S $S
110*aedb37dcSDag-Erling Smørgravjoin -j 2 -o 1.1 -o 2.1 $R $S
111