-
Notifications
You must be signed in to change notification settings - Fork 112
/
srclines
executable file
·63 lines (49 loc) · 1.56 KB
/
srclines
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
#!/usr/bin/env perl
use strict;
use warnings;
for my $file (@ARGV) {
my ($ext) = ($file =~ /\.(\w+)$/);
if ($ext) {
$ext = lc($ext);
}
open my $in, $file or
die "Cannot open $file for reading: $!\n";
my $src = do { local $/; <$in> };
if (!$ext) {
if ($src =~ /\A\#!(.*)/) {
my $shebang = $1;
if ($shebang =~ /\b(?:sh|bash)\b/) {
$ext = 'sh';
} elsif ($shebang =~ /\b(?:perl)\b/) {
$ext = 'pl';
}
} else {
die "no file extension nor shebang line found in file $file\n";
}
}
close $in;
if ($ext eq 'lua') {
$src =~ s/^\s*--.*//gm;
$src =~ s/^\s*\n//gm;
} elsif ($ext =~ /^p[l6]$/ || $ext eq 'fan' || $ext =~ /^pm6?$/ || $ext eq 'edge' || $ext eq 'sh' || $ext eq 'pgx') {
$src =~ s/\#\`\(.*?\)//gms;
$src =~ s/^\s*\#.*//gm;
$src =~ s/^\s*\n//gm;
$src =~ s/^=\w+.*?^=cut\s*$//gms;
$src =~ s/^__(?:DATA|END)__\n.*//gms;
} elsif ($ext =~ /^(?:[ch]|cc|cpp|y|dasc)$/) {
$src =~ s{^\#[ \t]*if[ \t]+(?:0|\(?(?:DDEBUG|NGX_DEBUG)\)?)[ \t]*\n.*?\n\#endif\b.*?\n}{}gms;
$src =~ s{/\*.*?\*/}{}gs;
$src =~ s{//[^\n]*}{}gs;
$src =~ s{^\s*dd\(".*\n}{}gm;
if ($ext eq 'dasc') {
$src =~ s/^[ \t]*\|[ \t]*$//mgs;
}
$src =~ s/^\s*\n//gm;
} else {
$src =~ s/^\s*\#\s.*//gm;
$src =~ s/^\s*\n//gm;
warn "WARNING: unknown recognized extension: $ext\n";
}
print $src;
}