-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcdirs
executable file
·88 lines (84 loc) · 2.24 KB
/
srcdirs
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
#!/usr/bin/env perl
#=======================================================================
# List directories containing source files
#=======================================================================
use strict;
use Getopt::Long;
use Cwd qw(getcwd);
(my $myname = $0) =~ s!.*/!!;
my @args = @ARGV;
my($mode);
my(@typelists);
my($var) = 'TOP';
my @exclude;
GetOptions('g|grep' => sub {$mode = 'grep'},
't|types=s' => \@typelists,
'e|etags' => sub {$mode = 'etags'},
'v|var=s' => \$var,
'x|exclude=s' => \@exclude)
and @ARGV <= 1
or die "Usage: $0 [--grep|--etags] [--types=ext1,ext2,...] [--var=NAME] [--exclude=path]... [dir]\n";
my $top = (@ARGV ? shift : '.');
@typelists = ('h,c,hpp,cpp') unless @typelists;
$top = getcwd if $top eq '.';
$top =~ s!/$!!;
my $toplen = length($top);
my @match;
foreach my $type (map {split /,/, $_} @typelists) {
push @match, '-o' if @match;
push @match, '-name', "*.$type";
}
my @cmd = ('find',$top, '(', @match, ')', '-print');
my(%patterns,%dirs);
open(my $find, '-|', @cmd)
or die "Cannot open pipe from find: $!\n";
while (<$find>) {
chomp(my $path = $_);
my($dir,$ext) = $path =~ m!(.*/).*?(\..*)!;
die "Bad path $dir\n" unless substr($dir,0,$toplen+1) eq "$top/";
my $reldir = substr($dir,$toplen+1);
next if &exclude("/$reldir");
$dirs{$dir} = 1;
my $pat = "$reldir*$ext";
$patterns{$pat} = 1;
}
close($find);
if ($mode eq 'grep') {
print "#!/bin/sh\n";
print "# Generated by: $myname @args\n";
print "$var=$top\n";
print "exec grep \"\$\@\" \\\n";
foreach my $pat (sort keys %patterns) {
print " \$$var/$pat \\\n";
}
print "\n";
} elsif ($mode eq 'etags') {
print "#!/bin/sh\n";
print "$var=$top\n";
print "exec etags \\\n";
foreach my $pat (sort keys %patterns) {
print " \$$var/$pat \\\n";
}
print "\n";
} else {
foreach my $dir (sort keys %dirs) {
print "$dir\n";
}
}
sub exclude {
my($path) = @_;
foreach my $excl (@exclude) {
my $skip;
if ($excl =~ m!^/!) {
# Explicitly at top level
$skip = 1 if $path =~ m!^\Q$excl\E/!;
} else {
$skip = 2 if $path =~ m!\Q$excl\E/!;
}
if ($skip) {
# print STDERR "Excluding $path\n";
return 1;
}
}
return;
}