-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaultgrep.pl
executable file
·272 lines (264 loc) · 6.07 KB
/
vaultgrep.pl
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#! /usr/bin/env perl
#
# Grep Crawl vault files for some string and output the match preceded by
# vault name. Don't search things like SUBST lines or the MAP.
#
use File::Find;
my $myname = $0;
$myname =~ s,.*/,,;
$myname =~ s/(.)\..*$/$1/;
# probably pointless extensibility...
my @path = ($ENV{HOME} . '/Sources/crawl/crawl-ref/source/dat/des');
my @sfx = ('des');
my %sel = (monster => [qr/^MONS:/, qr/^KMONS:/]
,item => [qr/^ITEM:/, qr/^KITEM:/]
,feature => [qr/^FEAT:/, qr/^KFEAT:/]
);
my %extra = (property => [qr/^PROP:/, qr/^KPROP:/, qr/^TAGS:/, qr/^LTAGS:/]
,branch => [qr/^PLACE:/]
);
my (%which, %also);
my $err = 0;
my $and = 1;
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg eq '--all') {
%which = map {$_ => 1} keys %sel;
}
elsif ($arg =~ /^--(\w+)$/ and exists $sel{$1}) {
$which{$1} = 1;
}
elsif ($arg =~ /^--no-(\w+)$/ and exists $sel{$1}) {
if (!keys %which) {
%which = map {$_ => 1} keys %sel;
}
delete $which{$1};
}
elsif ($arg =~ /^--(\w+)$/ and exists $extra{$1}) {
push @{$also{$1}}, shift @ARGV;
}
elsif ($arg =~ /^--(\w+)=(.*)$/ and exists $extra{$1}) {
push @{$also{$1}}, $2;
}
# @@@ should perhaps accept --any|--all, except see above...
elsif ($arg eq '--and' or $arg eq '-a') {
$and = 1;
}
elsif ($arg eq '--or' or $arg eq '-o') {
$and = 0;
}
elsif ($arg eq '--help' or $arg eq '-h') {
$err = 1;
last;
}
# @@@ do this via mapping somehow instead of breaking abstraction :/
elsif ($arg =~ /^-b(.*)$/) {
push @{$also{branch}}, $1;
}
elsif ($arg eq '-b') {
push @{$also{branch}}, shift @ARGV;
}
elsif ($arg eq '-m') {
$which{monster} = 1;
}
elsif ($arg eq '-i') {
$which{item} = 1;
}
elsif ($arg eq '-f') {
$which{feature} = 1;
}
elsif ($arg eq '--') {
last;
}
elsif ($arg =~ /^-/) {
print STDERR "$myname: unknown switch $arg\n";
$err = 1;
}
else {
unshift @ARGV, $arg;
last;
}
}
if ($err or !@ARGV) {
print STDERR "usage: $myname [--and|--or] [--all";
for (keys %sel) {
print STDERR "|--[no-]$_";
}
print STDERR "] [";
$err = 0;
for (keys %extra) {
$err and print STDERR '|';
print STDERR "--$_=pattern";
$err = 1;
}
print STDERR "] pattern...\n";
exit 1;
}
keys %which or %which = map {$_ => 1} keys %sel;
find(sub {vgrep(clean($File::Find::dir, @path), $_)}
,@path
);
###############################################################################
sub clean {
my ($dir, @pfx) = @_;
# @@@ after allowing for multiple paths, we make it useless...
for my $pfx (@pfx) {
$dir =~ s,^$pfx($|/),, and return $dir;
}
return $dir;
}
sub vgrep {
my ($dir, $name) = @_;
-f $_ or return;
my $ok = 0;
for my $sfx (@sfx) {
if (/\.$sfx$/i) {
$ok = 1;
last;
}
}
$ok or return;
# it's presumably a .des file; munch it
open($f, $_) or return;
my $ln;
my $map = 0;
my $lua = 0;
my $cur = undef;
my $lno = 0;
my $doing = -1;
my $dd = undef;
my $ldd = undef;
while (defined ($ln = <$f>)) {
$lno++;
chomp $ln;
$ln =~ /^\s*($|#)/ and next;
while ($ln =~ s/\\$//) {
my $l2 = <$f>;
unless (defined $l2) {
print STDERR "$dir/$_:$lno: warning: end of file in continued line\n";
$l2 = '';
}
$lno++;
chomp $l2;
$l2 =~ s/^\s+//;
$ln .= $l2;
}
if (defined $cur and !$map and !$lua and $ln =~ /^MAP$/) {
$map = 1;
next;
}
elsif (!$map and !$lua and $ln =~ /^:/) {
# one-liner lua
next;
}
elsif (!$map and !$lua and $ln =~ /^(?:lua\s*)?\{\{$/) {
$lua = 1;
next;
}
elsif ($lua and $ln =~ /^\}\}$/) {
$lua = 0;
next;
}
elsif ($map and $ln =~ /^ENDMAP$/) {
$cur = undef;
$map = 0;
next;
}
elsif ($map or $lua) {
next;
}
elsif ($ln =~ /^NAME:\s*(\S+)\s*$/) {
# @@@ serial vaults don't have maps in the main vaults
# @@@ check default depth vs. branch here to set $doing!
# @@@@ except that's wrong if it sets DEPTH:
# if (defined $cur) {
# print STDERR "$dir/$_:$lno: warning: already in $cur: $ln\n";
# }
$cur = $1;
$doing = -1;
$ldd = undef;
next;
}
# this is allowed outside of any definition
elsif (!defined $cur and $ln =~ /^default-depth:\s*(.*)$/) {
$dd = $1;
next;
}
elsif (!defined $cur) {
print STDERR "$dir/$_:$lno: warning: not in a definition: $ln\n";
next;
}
elsif ($ln =~ /^DEPTH:\s*(.*)$/) {
$ldd = $1;
}
else {
# look for extras matches
$ok = 0;
my $rok = 0;
for my $extra (keys %also) {
next if $extra eq 'branch'; # @@@@@@@@@@
# does this line match a selector?
for my $kw (@{$extra{$extra}}) {
if ($ln =~ $kw) {
$rok = 1;
for my $pat (@{$also{$extra}}) {
if ($ln =~ /$pat/) {
$ok = 1;
last;
}
}
$ok or $doing = 0;
last;
}
}
}
# if we matched any extra keyword then it can't be a section keyword
$rok and next;
# is section enabled?
for my $sect (keys %which) {
# does the line match a selector?
for my $kw (@{$sel{$sect}}) {
if ($ln =~ $kw) {
$ok = 1;
last;
}
}
$ok or next;
# figure out if we are in a selected branch
# @@@ and pray DEPTH: doesn't occur *after* MONS etc.
if ($doing == -1) {
if (!exists $also{branch}) {
$doing = 1;
}
elsif (defined $dd or defined $ldd) {
defined $ldd or $ldd = $dd;
$doing = 0;
for my $pat (map {split(',', $_)} @{$also{branch}}) {
if ($ldd =~ /(?:^|,\s*)$pat(?:,|:|$)/i) {
$doing = 1;
}
}
}
}
$doing or next;
# try matching against all the patterns.
# @@@ AND / OR expressions?
# @@@ for that matter, and/or sections... right now always OR
$ok = $and ? @ARGV : 0;
for my $pat (@ARGV) {
# @@@ might want to delete prefixes for those keywords that have them
if ($ln =~ /$pat/) {
if ($and) {
$ok--;
} else {
$ok = 1;
}
}
}
if (($and and !$ok) or (!$and and $ok)) {
print "$dir/$_:${lno}: [$cur] $ln\n";
}
}
}
}
}