#!/bin/perl
eval "exec /usr/local/bin/perl -S $0 $*"
    if $running_under_some_shell;

  #          $Id: mksymdb,v 1.4 1992/08/18 18:37:54 joel Exp root $
  # ======================================================================== #
  #                                                                          #
  #   Copyright (c)       1992 by Techne Resarch                             #
  #   Copyright (c)       1992 by Joel Rosi-Schwartz                         #
  #   All Rights Reserved                                                    #
  #                                                                          #
  #   Full Copyright notice at bottom of file.                               #
  #                                                                          #
  #  mksymdb and its sister program locsym attemts to handle the common      #
  #  progamming problem of finding which library or libraries contains       #
  #  the definition of a given symbol.  mksymdb needs only be run once to    #
  #  set up the dbm database (or, of course, if you change your libraries).  #
  #  After that to find a given function or extern definition is a quick     #
  #  look-up using locdb.                                                    #
  #                                                                          #
  #  This code is for sure not portable across systems at this point.  I've  #
  #  looked at Larry Wall's Configrue program to get an idea of what needs   #
  #  to be done to make it portable and only understand half of it (maybe;-) #
  #  Please send me any code towards this end and I will try to maintain     #
  #  this such that as many systems as possible work without kluges.         #
  #                                                                          #
  #  The sole purpose of the copyright is so I don't get sued and my soul ;-)#
  #  intention is that it should be as helpful and useful to as many folks   #
  #  as possible, without anyone trying to hoard it or claim it as their     #
  #  own.  Use it in peace and good health.                                  #
  #                                                                          #
  #  Suggestions, advice, bug reports and flames to:                         #
  #                                                                          #
  #        Joel Rosi-Schwartz                                                #
  #        Techne Research                                                   #
  #        The United Kingdom                                                #
  #        E-mail: joel@abigale.UUPC                                         #
  #        Or: uknet.ac.uk!pyrltd.uucp!abigale.uucp!joel                     #
  #        Phone: +44 734 730.260                                            #
  #        Fax: +44 734 730.272                                              #
  #                                                                          #
  # ======================================================================== #

#
# How much noise do we want ;-)
#
$verbose = 0;
$quiet = 0;
$debug = 0;

#
# The root directories of all archives we will parse.
#
@libroots = (
	'/',
#	'/lib',
#	'/usr/lib',
#	'/usr/local/lib',
	);

#
# The "nm" command which will create quickly parseable output.
#
$nmcmd = 'nm -p';

#
# Our symbols database.
#
$symdb = '/usr/local/etc/symdb';
%SYMDB = ();

#
# Save the sysmdb iff it exists, JIC.
#
if ( -e "${symdb}.pag") {
	print STDERR "${symdb}.pag exists. Saving in ${symdb}.pag-\n"
		unless $quiet;
	rename ("${symdb}.pag", "${symdb}.pag-") ||
		die "Could not rename ${symdb}.pag: $!\n";
}

if ( -e "${symdb}.dir") {
	print STDERR "${symdb}.dir exists. Saving in ${symdb}.dir-\n"
		unless $quiet;
	rename ("${symdb}.dir", "${symdb}.dir-") ||
		die "Could not rename ${symdb}.dir: $!\n";
}

#
# Open up a new one.
#
dbmopen (%SYMDB, $symdb, 0666);

#
# Gather up all of the ".a" files to parse.
# Note that the regex in find is "lib*.a", since I explicity
# want to avoid the Xenix [SML] libs.
#
print STDERR "Locating all archives, please be patient ..." unless $quiet;
#@archives = `find @libroots -type f -name "lib*.a" -print`;
@archives = `find @libroots -type f '(' -name "lib*.a"  -o -name "lib*.so" ')' -print`;

print STDERR " done\n" unless $quiet;

#
# Okay, lets take a look.
# We open each archive in turn and add the archive name to
# the list for each externally visible symbol.
#
#  The database tends to get very big fast. Instead of listing
#  each library in full for every subroutine, we use a bit
#  of indirection.  Each library is listed once and keyed.  Then
#  we just list the short key in the subroutine record.  A fast
#  lookup will give us back the library name.  The keys are numbers
#  so the is no chance of a conflict with a real function name.
#
$indir = 0;

foreach $archive (@archives) {
	$indir++;
	%seen = ();		# to weed out duplicate symbols
	chop $archive;
	$SYMDB{$indir} = $archive;
	print STDERR "Processing $archive\n" unless $quiet;
	open (NMOUT, "$nmcmd $archive|");

SYMBOL:
	while (<NMOUT>) {
		$symbol = '';
		chop;
		if (/^.*\s+[ATDS]\s+([^_.].*)/o) { $symbol = $1 ;}
		next unless $symbol;
		# weed out file names from shared libs
		next SYMBOL if $symbol =~ m=^/shlib/=o;
		# only one per archive, thank you
		next SYMBOL if $seen{$symbol}++;
		$SYMDB{$symbol} .= " $indir";
		print STDERR "$symbol: $SYMDB{$symbol}\n" if $verbose;
	}
}

#
# And say bye
#
dbmclose (SYMDB);
print STDERR "Processing complete\n" unless $quiet;
exit 0;

  #############################################################################
  #                                                                           #
  #  Copyright (c) 1992 Techne Research                                       #
  #  Copyright (c) 1992 Joel Rosi-Schwartz                                    #
  #  All rights reserved.                                                     #
  #                                                                           #
  #  Redistribution and use in source and binary forms, with or without       #
  #  modification, are permitted provided that the following conditions       #
  #  are met:                                                                 #
  #  1. Redistributions of source code must retain the above copyright        #
  #     notice, this list of conditions and the following disclaimer.         #
  #  2. Redistributions in binary form must reproduce the above copyright     #
  #     notice, this list of conditions and the following disclaimer in the   #
  #     documentation and/or other materials provided with the distribution.  #
  #  3. All advertising materials mentioning features or use of this software #
  #     must display the following acknowledgement:                           #
  #       This product includes software developed by the Techne Research     #
  #  4. The name of Techne Research may not be used to endorse or promote     #
  #     products derived from this software without specific prior written    #
  #     permission.                                                           #
  #                                                                           #
  #  THIS SOFTWARE IS PROVIDED BY THE TECHNE RESEARCH (UK) ``AS IS'' AND      #
  #  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    #
  #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR       #
  #  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COMDISCO SYSTEMS INC BE       #
  #  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR      #
  #  CONSEQUENTIAL #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT     #
  #  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR       #
  #  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,    #
  #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE     #
  #  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,        #
  #  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                       #
  #                                                                           #
  # This copyright notice derived from material copyrighted by the Regents    #
  # of the University of California.                                          #
  #                                                                           #
  #############################################################################

#
