-
Notifications
You must be signed in to change notification settings - Fork 27
/
aux2bib.in
80 lines (71 loc) · 2 KB
/
aux2bib.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!@PERL@
# auxtobib takes the name of a main .aux file (as generated by LaTeX)
# as argument. It extracts all the citations contained in the aux
# file, recursively descending into included aux files (as generated
# by the LaTeX \include directive), and uses this list to extract a
# BibTeX file containing only the cited references, including the
# needed string abbreviations and crossreferences. The names of the
# BibTeX files are taken from the main aux file, and are searched in
# exactly the same way as done by bibtex.
#
# Needs bib2bib (see http://www.lri.fr/~filliatr/bibtex2html)
# and kpsewhich (comes with the kpathsea library).
#
# Ralf Treinen <[email protected]>.
# Published under the Gnu General Public Licence, version 2.
# process arguments
if ( $#ARGV != 0 ) {&errorexit("Usage: aux2bib <aux-file>");}
$mainauxfile = $ARGV[0];
sub errorexit {
print STDERR "aux2bib: @_[0]\n";
exit;
}
# process the aux files
sub dofile {
local($filename, $handle) = @_;
$handle++;
open($handle,$filename) || &errorexit("Cannot open file $filename");
while (<$handle>){
if (/\\\@input\{(.*)\}/) {
&dofile($1,$handle);
next;
}
if (/\\citation\{(.*)\}/) {
foreach $subkey (split(/,/,$1)) {
$keys{$subkey} = 0;
}
next;
}
if (/\\bibdata\{(.*)\}/) {
@bibfiles = split(/,/,$1);
}
}
close $handle;
}
&dofile($mainauxfile,'handle00');
# build the expression for the search condition
$condfile="/tmp/aux2bib$$";
open(COND,"> $condfile") || &errorexit("Cannot open temp file $condfile");
foreach $key (keys %keys){
print COND "\$key=\"$key\" or ";
}
print COND "1=2\n";
close COND;
# get the string of path names of the bib files
$bibfilesbib = "";
foreach $bibfile (@bibfiles) {
$bibfilesbib .= "$bibfile "
}
$bibfilestring = "";
open(CMD,"kpsewhich -format bib $bibfilesbib|");
while (<CMD>){
chop;
$bibfilestring .= "$_ ";
}
close(CMD);
# call bib2bib
open(CMD,"bib2bib -c \"`cat $condfile`\" $bibfilestring|");
while (<CMD>){print;}
close(CMD);
# cleanup
unlink($condfile);