summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/bib2xhtml/gen-bst
blob: 38e77cedaeaedca0163f62a75ac20d460e37dde3 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/local/bin/perl -w
#
# $Id: gen-bst 1.4 2004/04/07 12:59:49 dds Exp $
#

$DEFS = "html-btxbst.doc";

sub cpp ($@);

&cpp('a',  'ALPHA');
&cpp('aa', 'ALPHA', 'ABSTRACT');
&cpp('n',  'NAMED');
&cpp('na', 'NAMED', 'ABSTRACT');
&cpp('u',  'UNSRT');
&cpp('ua', 'UNSRT', 'ABSTRACT');
&cpp('nr',   'NAMED',             'REVERSEDATE');
&cpp('ar',   'ALPHA',             'REVERSEDATE');
&cpp('ara',  'ALPHA', 'ABSTRACT', 'REVERSEDATE');
&cpp('nra',  'NAMED', 'ABSTRACT', 'REVERSEDATE');
&cpp('acr',  'ALPHA',             'REVERSEDATE', 'SORTBYYEAR');
&cpp('acra', 'ALPHA', 'ABSTRACT', 'REVERSEDATE', 'SORTBYYEAR');
&cpp('ncr',  'NAMED',             'REVERSEDATE', 'SORTBYYEAR');
&cpp('ncra', 'NAMED', 'ABSTRACT', 'REVERSEDATE', 'SORTBYYEAR');
&cpp('ac',   'ALPHA',             'SORTBYYEAR');
&cpp('aca',  'ALPHA', 'ABSTRACT', 'SORTBYYEAR');
&cpp('nc',   'NAMED',             'SORTBYYEAR');
&cpp('nca',  'NAMED', 'ABSTRACT', 'SORTBYYEAR');

# ----------------------------------------------------------------------------
# cpp function does just enough cpp parsing to process the $DEFS file.
sub cpp ($@) {
    my $suffix = shift;
    my @defs = @_;

    my $word;
    my $value;
    my %defs;
    my $skipping = 0;
    my $blank = 0;
    my $outfile = "html-${suffix}.bst";

    print STDERR "creating $outfile...\n";

    $defs{'HTML'} = 1;
    foreach $word (@defs) {
#print "define $word 1\n";
	$defs{$word} = 1;
    }

    open(FILE, "<$DEFS") || die "error opening $DEFS for read: $!\n";
    open(OUTFILE, ">$outfile") || die "error opening $outfile for write: $!\n";

line:
    while (<FILE>) {
	next line if (m/^%/);

	if (m/^#/) {
	    if (m/^#\s*ifdef\s+(\w+)/) {
		$word = $1;
#print "ifdef $word: " . (exists($defs{$word}) ? "1" : "0") . "\n";
		if ($skipping) {
		    $skipping++;
		    next line;
		}
		if (!exists($defs{$word})) {
		    $skipping++;
		}
		next line;
	    }
	    if (m/^#\s*ifndef\s+(\w+)/) {
		$word = $1;
		if ($skipping) {
		    $skipping++;
		    next line;
		}
		if (exists($defs{$word})) {
		    $skipping++;
		}
#print "ifndef $word: " . (exists($defs{$word}) ? "0" : "1") . "\n";
		next line;
	    }
	    if (m/^#\s*if\s+(\!?)\s*(\w+)/) {
		$negate = ($1 ne '');
		$word = $2;
		if ($skipping) {
		    $skipping++;
		    next line;
		}
		if (($negate && $defs{$word}) || (!$negate && !$defs{$word})) {
		    $skipping++;
		}
#print "if $word: (". $defs{$word} . ") " . ($defs{$word} != 0 ? "1" : "0") . "\n";
		next line;
	    }
	    if (m/^#\s*else\b/) {
		if ($skipping == 1) {
		    $skipping--;
		} elsif ($skipping == 0) {
		    $skipping++;
		}
#print "else\n";
		next line;
	    }
	    if (m/^#\s*endif\b/) {
#print "endif\n";
		if ($skipping > 0) {
		    $skipping--;
		}
		next line;
	    }
	    if (m/^#\s*define\s+(\w+)\s+(\S+)?/) {
		next line if $skipping;
		$word = $1;
		$value = $2;
#print "define $word = $value\n";
		$defs{$word} = (defined($value) ? $value : 1);
		next line;
	    }
	}

	if (m/^$/) {
	    next line if ($blank);
	    $blank = 1;
	} else {
	    $blank = 0;
	}

	print OUTFILE if (!$skipping);
    }

    die "#ifdef/#endif mismatch!\n" if ($skipping != 0);

    close(FILE);
    close(OUTFILE);
}