#!/usr/bin/perl use strict; use warnings; sub help { my $cmdname = $0; $cmdname =~ s:.*/::; print "$cmdname - list the BibTeX keys defined in bib files.\n"; print "Input is read from stdin or from named files.\n"; print "The keys are sorted lexicographically, with each key listed once even if\n"; print "it is defined more than once.\n"; print "If a key is defined more than once, a message is printed to STDERR.\n"; print " -help Show this help.\n"; } my $arg; my $numFiles = 0; my $sep = "\n"; my $v = ""; my $k = ""; my %keysDefined = (); my $count = 0; foreach $arg (@ARGV) { if ($arg =~ /^-/) { if ($arg =~ /^-+help$/) { &help; exit 0; } else { print "Unexpected argument \"$arg\".\n"; exit 0; } } else { if (open(HIN, $arg)) { &bibFile(\*HIN, $arg); close HIN; ++$numFiles; } else { print STDERR "Can't open input file \"$arg\".\n"; exit -1; } } } if (0 == $numFiles) { &bibFile(\*STDIN, ""); } if (scalar %keysDefined) { foreach $k (sort keysDefined %keysDefined) { if ($count++) { print "$sep"; } print "$k"; } print "\n"; } sub bibFile { my $IN = $_[0]; my $fname = $_[1]; my $lineno = 0; while (<$IN>) { ++$lineno; chomp; if (/^@[a-zA-Z]+[({]([-0-9a-zA-Z+]+),$/) { s/^@[a-zA-Z]+[({]([-0-9a-zA-Z+]+),$/$1/; #print "$_\n"; my $key = $_; if (defined $keysDefined{$key}) { print STDERR "($fname:$lineno) '$key' multiply defined ($keysDefined{$key}).\n"; } $keysDefined{$_} = "$fname:$lineno"; } } }