forked from vjohansen/emacs-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incgrep.pl
executable file
·116 lines (99 loc) · 3.05 KB
/
incgrep.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
#! /usr/local/bin/perl -sw
use strict;
use Cwd;
######################################################################
### CONFIG
my $verbose = 0;
my $not_found_count = 0;
my @g_include_directories =
(".", "/usr/include", "/usr/include/linux",
"/usr/include/QtCore",
"/usr/include/Qt",
"/usr/lib/gcc/x86_64-redhat-linux/4.4.4/include/",
"/home/vj/src/easyviz/install/include", "/home/vj/src/easyviz/evncserver2/common"
# "c:/tools/milbin/boost_v1.30.2"
);
######################################################################
my $filename = shift @ARGV or die "filename needed as first argument\n";
my $grepword = shift @ARGV or die "need word to search for (is text selected?)";
my %seen;
if ($ENV{EMACS})
{
print ' Hi-lock: ((":[ ]*class[ ]+\\\\([^ ]+\\\\)" (1 (quote font-lock-type-face) t)))'."\n";
print ' Hi-lock: ((":[ ]*\\\\(//.*\\\\)" (1 (quote font-lock-comment-face) t)))'."\n";
}
cpp_grep($filename, 0, ".");
if ($not_found_count > 0) {print "\n$not_found_count header files not found\n"; }
# Recursively grep for in filename in @g_include_directories
# args: filename, function call level, current directory
# The latter two args are by the recursion, set them to 0 and "." initially
sub cpp_grep
{
my ($filename, $level, $cwd) = @_;
my ($fh, $line);
if (not open $fh, "<", $filename)
{
print "$filename not found";
return;
}
while ($line = <$fh>)
{
if ($line =~ m/^\s* \# \s* include \s* ([<\"]) ([^>\"]+) ([>\"])/x)
{
my $filename = $2;
if ($filename =~ m/\b(list|string|vector|list|map)\b[^.]/)
{
print "SKIP $filename\n" if $verbose >= 1;
next;
}
print " " x $level, "* $filename\n" if $verbose >= 2;
my @include_files = find_include_files($filename, $cwd);
for my $include_file (@include_files)
{
if ($seen{$include_file})
{
print "already seen $include_file\n" if $verbose >= 3;
next; # skip it
}
$seen{$include_file}++;
my $output = `grep -n \"$grepword\" $include_file`;
if ($output) {
my $line_number = 0;
if ($output =~ m/^(\d+)/)
{
$line_number = $1;
}
print "\n$include_file:$line_number: \n";
print $output;
}
my $dir = $include_file;
$dir =~ s/\/[^\/]+$//;
cpp_grep($include_file, $level+1, $dir);
}
if (not @include_files)
{
$not_found_count++;
print "# $filename not found\n" if $verbose;
}
}
}
close $fh;
}
sub find_include_files
{
my ($filename, $cwd) = @_;
print "FIND $filename in $cwd\n" if $cwd and $verbose >= 3;
my @result;
for my $include_directory (@g_include_directories)
{
my $fn = "${include_directory}/$filename";
push @result, $fn if -e $fn;
}
# Not in include path. maybe it is next to the parent include
if (not @result and $cwd and -e "$cwd/$filename")
{
push @result, "$cwd/$filename";
print "LOCAL $cwd / $filename" if $verbose >= 2;
}
return @result;
}