-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymscan.pl
executable file
·360 lines (318 loc) · 7.21 KB
/
symscan.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
use File::Basename;
use Getopt::Long;
use IPC::Open2;
use Time::HiRes;
use Encode;
###### config {{{
my $REPO_VER = 2;
my $repofile = 'tags.repo.gz';
my $TAGSCAN_PAT = $ENV{TAGSCAN_PAT} || '*';
my $TAGSCAN_IGNORE_PAT = "*[.-]min.js";
# '*.c;*.cpp;*.h;*.hpp;*.cc;*.mak;*.cs;*.java;*.s;*.pl;*.py';
my $IGNORE_PAT = $ENV{IGNORE_PAT} || '*.o;*.obj;*.d;.*;node_modules';
my $MSWIN_ENC = 'gbk';
#}}}
###### globals {{{
my $IS_MSWIN = $^O eq 'MSWin32';
my $IS_MSYS = $^O eq 'msys';
my $sep = $IS_MSWIN? '\\': '/';
my $CWD = mygetcwd();
my $PROG = getAbsPath($0);
my $outfile;
my $symcnt = 0;
my $filecnt = 0;
my $updFilecnt = 0;
my ($tagScanRE, $tagIgnoreRE, $ignoreRE);
#}}}
###### toolkit {{{
sub mtime # ($file)
{
my @a = stat($_[0]);
return $a[9];
}
# e.g. "*.c;tags;.*" -> /\.c$|^tags$|^\./i OR /^(.*?\.c|tags|\..*?)$/i
sub patToRE # ($pat)
{
local $_ = shift;
# remove wrong quotes
s/^["'](.*)["']$/$1/;
s/\s//g;
# escape
s/([.|()^\$])/\\$1/g;
# remove empty
s/;+/|/g;
s/;$//;
# replace '*'
s/\*/.*?/g;
return qr/^($_)$/i;
}
#}}}
###### function {{{
sub mygetcwd
{
local $_ = getcwd();
if ($IS_MSYS) {
s+/(\w)/+$1:/+; # /d/prog -> d:/prog
}
s/\//\\/g if $IS_MSWIN;
$_;
}
# require: $CWD, $IS_MSWIN, $sep
sub getAbsPath # ($path)
{
local $_ = shift;
if ($_ eq '.') {
return $CWD;
}
s/^\.[\/\\]//; # ./symscan.pl -> symscan.pl
unless (/^[\/\\]/ || (($IS_MSWIN || $IS_MSYS) && /^\w:/))
{
$_ = $CWD . $sep . $_;
}
$_;
}
# getSym(undef) means the end.
my $ctag_out;
sub getSym # ($file)
{
my ($file) = @_;
my $cnt = 0;
if (!defined $ctag_out) {
return if !defined $file;
my $CTAGS = dirname($PROG) . $sep . 'stags';
$CTAGS .= '.exe' if $IS_MSWIN || $IS_MSYS;
die "*** $CTAGS does not exist or cannot run!" unless -f $CTAGS;
print "=== use ctags: $CTAGS\n" if $ENV{DEBUG};
# --format=99: my own format
# -u: don't sorting
open2(\*I, $ctag_out, "\"$CTAGS\" -x --format=99 --c++-kinds=+px -u --filter=yes --filter-terminator=") or die "fail to open stags!\n";
}
if (!defined $file) {
print $ctag_out ".\n";
close $ctag_out;
close I;
undef $ctag_out;
return;
}
if ($IS_MSYS) {
Encode::from_to($file, 'utf-8', $MSWIN_ENC);
}
print $ctag_out "$file\n";
while (<I>) {
chop; # remvoe "\n"
chop if /\s$/o; # remove "\r"
last unless /\S/o;
print O $_, "\n";
++ $cnt;
}
return $cnt;
}
# require: $REPO
sub loadRepo # ($f)
{
my ($f) = @_;
$f = "gzip -dc $f |";
open IN, $f or die "cannot open file $f\n";
my @repos = ();
my $repo;
my ($curdir, $curfileobj);
my $ismeta = 0;
while (<IN>) {
if (/^!(\w+)\s+(.*)/o) {
if (! $ismeta) {
$repo = {
meta => {},
files => {},
};
undef $curdir;
undef $curfileobj;
push @repos, $repo;
$ismeta = 1;
}
$repo->{meta}{$1} = $2;
}
elsif (/^\t(d|f)\t([^\t]+)\t(\d+)/o) {
$ismeta = 0;
my ($type, $name, $mtime) = ($1, $2, $3);
if ($type eq 'f') {
my $fileobj = {mtime => $mtime, content => $_};
!$curdir && die "*** bad repo!";
$name = $curdir . $sep . $name;
$repo->{files}{$name} = $fileobj;
$curfileobj = $fileobj;
}
else {
$curdir = $name;
}
}
else {
$curfileobj->{content} .= $_;
}
}
close IN;
@repos;
}
# require: O
sub handleDir # ($name, $dirname)
{
my ($name, $dirname) = @_;
$dirname =~ s/\//\\/g if $IS_MSWIN;
my $fname = $name eq '.'? $dirname: $dirname . $sep . $name;
my $mtime1 = mtime($name);
if ($IS_MSWIN) {
Encode::from_to($fname, $MSWIN_ENC, 'utf-8');
}
print O "\td\t$fname\t$mtime1\n";
}
# require: O, $filecnt, $symcnt
sub handleFile # ($name, $dirname, $repo)
{
my ($name, $dirname, $repo) = @_;
$dirname =~ s/\//\\/g if $IS_MSWIN;
my $forUpdate = defined $repo;
my $fname = $dirname . $sep . $name;
my $mtime1 = mtime($name);
my $fileobj;
if ($repo && exists $repo->{files}{$fname}) {
$fileobj = $repo->{files}{$fname};
}
++ $filecnt;
if (!(defined $fileobj) || $mtime1 > $fileobj->{mtime}) {
my $name1 = $name;
if ($IS_MSWIN) {
Encode::from_to($name1, $MSWIN_ENC, 'utf-8');
}
print O "\tf\t$name1\t$mtime1\n";
++ $updFilecnt if $forUpdate;
print "### scan $fname\n" if $ENV{DEBUG};
if ((!defined $tagScanRE || $name =~ /$tagScanRE/)
&& (!defined $tagIgnoreRE || $name !~ /$tagIgnoreRE/)) {
my $f = mygetcwd() . $sep . $name;
$symcnt += getSym($f);
}
}
else { # update
print O $fileobj->{content};
}
if ($filecnt % 100 == 0) {
if (! $forUpdate) {
print "$filecnt files, $symcnt symbols ...\r";
}
else {
print "$filecnt files ($updFilecnt updates with $symcnt symbols) ...\r";
}
}
}
#}}}
###### main routine {{{
$| = 1;
# @ARGV=("$ENV{SBO_BASE}/Source/Infrastructure", "$ENV{SBO_BASE}/Source/Client");
if (@ARGV == 0) {
print "Usage: symscan.pl {folder(s)|repo-file(s)} [-o {outfile=tags.repo.gz}]\n";
exit;
}
GetOptions("o=s", \$outfile);
for (@ARGV) {
s/$sep{2,}/$sep/g;
s/$sep+$//;
$_ = getAbsPath($_);
unless (-d $_ || -f $_) {
print STDERR "*** cannot find repo-file or folder $_\n";
exit;
}
}
if ($ARGV[0] =~ /\.gz$/) {
$repofile = $ARGV[0];
}
if (defined $outfile) {
$outfile .= '.repo.gz' if $outfile !~ /\.gz/i;
}
else {
$outfile = $repofile;
}
### process each repo-file update or folder scan
my $fdopened;
my %scanedRoot; # avoid the same root to be scaned repeatly
for (@ARGV) {
my $forUpdate;
### repo-file in memory (for update)
my @repos; # elem: {\%meta, \%files}
# meta: $key => $value
# files: $filename => {name, mtime, content}
if (-d) {
@repos = ({
meta => {
ROOT => $_,
}
});
}
elsif (-f) {
print "=== load repo-file $_...\n";
@repos = loadRepo($_);
$forUpdate = 1;
}
# NOTE: !!! ENSURE O is opened after loading first repo (as outfile may be the same as the first repo)
if (!$fdopened)
{
open O, "| gzip > $outfile";
binmode O; # avoid "\r" on mswindows
$fdopened = 1;
}
for (@repos) {
my $repo = $_;
my $root = $repo->{meta}{ROOT};
next if exists $scanedRoot{$root};
$scanedRoot{$root} = 1;
print O "!ROOT $root\n";
print O "!REPO_VER ", ($_->{meta}{REPO_VER} || $REPO_VER), "\n";
print O "!LAST_UPDATE ", time(), "\n";
print O "!TAGSCAN_PAT ", $TAGSCAN_PAT, "\n";
print O "!IGNORE_PAT ", $IGNORE_PAT, "\n";
$ignoreRE = patToRE($IGNORE_PAT);
$tagScanRE = patToRE($TAGSCAN_PAT);
$tagIgnoreRE = patToRE($TAGSCAN_IGNORE_PAT);
my $T0 = Time::HiRes::time();
chdir $root;
($filecnt, $updFilecnt, $symcnt) = (0,0,0);
print "=== scan folder $root...\n";
# NOTE: ENSURE files are processed before sub-dirs
find({
preprocess => sub {
my @r;
for (@_) {
next if $_ eq '.' or $_ eq '..';
if (!defined $ignoreRE || !/$ignoreRE/) {
if (-f)
{
handleFile($_, $File::Find::name, $forUpdate? $repo: undef);
}
else {
push @r, $_;
}
}
}
@r;
},
wanted => sub {
handleDir($_, $File::Find::dir);
},
}, '.');
my $t = Time::HiRes::time()-$T0;
if (! $forUpdate) {
printf "$filecnt files, $symcnt symbols are saved to repository $outfile in %.3lfs.\n", $t;
}
else {
printf "$filecnt files ($updFilecnt updates with $symcnt symbols) in repository $outfile in %.3lfs.\n", $t;
}
}
}
chdir $CWD;
getSym(undef); # close the sub-process
close O;
#}}}
# vim: set foldmethod=marker :