-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextract_env_vars.pl
191 lines (163 loc) · 5.35 KB
/
extract_env_vars.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
#!/usr/bin/perl
#########################################################################
#
# The Contents of this file are made available subject to the terms of
# the Sun Industry Standards Source License Version 1.2
#
# Sun Microsystems Inc., March, 2001
#
#
# Sun Industry Standards Source License Version 1.2
# =================================================
# The contents of this file are subject to the Sun Industry Standards
# Source License Version 1.2 (the "License"); You may not use this file
# except in compliance with the License. You may obtain a copy of the
# License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
# Software provided under this License is provided on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
# See the License for the specific provisions governing your rights and
# obligations concerning the Software.
#
# The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
# Copyright: 2001 by Sun Microsystems, Inc.
#
# All Rights Reserved.
#
#########################################################################
#
# This script recursively searches all .c files under the current
# directory for uses of setenv() or getenv(). Whenever such a function
# call is found, an attempt is made to determine the environment variable
# used. If the argument to the function call matches the pattern
# "[A-Z0-9_]+", the argument is assumed to be an env var. If the argument
# matches the pattern [A-Z0-9_]+, the argument is assumed to be a macro.
# The script will then recursively search all .c and .h files under the
# current directory for a macro definition. If the -a switch is given,
# anything not matching either of the above patterns is assumed to be a
# variable expression and is not resolved any further. If the -g switch is
# given, only getenv() calls are included. If the -s switch is given, only
# setenv() calls are included.
# The output of this script is a list of the names of the env vars found,
# one per line, followed the names of the files in which they where found,
# indented and one per line. In the case of macros, the name takes the
# form:
# macro(resolved_value)
# In the case of variable expressions, the name takes the form:
# (variable_expression)
#
# Syntax:
# Usage: extract_env_vars.pl [-a] [-g|-s]
#
#########################################################################
my %list, %macros, $do_all, $do_gets, $do_sets, $arg;
sub usage {
print "Usage: extract_env_vars.pl [-a] [-g|-s]\n";
exit (1);
}
sub help {
print "extract_env_vars.pl [-a] [-g|-s]\n";
print "\t-a\tinclude variable expressions\n";
print "\t-g\tinclude only getenv() calls\n";
print "\t-s\tinclude only setenv() calls\n";
exit (0);
}
$do_all = 0;
$do_gets = 1;
$do_sets = 1;
while (@ARGV > 0) {
$arg = shift (@ARGV);
if ($arg eq "-help") {
help();
}
elsif ($arg eq "-a") {
$do_all = 1;
}
elsif ($arg eq "-s") {
$do_sets = 1;
$do_gets = 0;
}
elsif ($arg eq "-g") {
$do_gets = 1;
$do_sets = 0;
}
else {
usage ();
}
}
open (GREP, '/usr/bin/csh -c "/usr/bin/grep etenv {*.c,*/*.c,*/*/*.c,*/*/*/*.c}" |');
my $file;
while (<GREP>) {
if (/^(.+?):.*(sge_|[^a-zA-Z_])([gs])etenv\s*\(\s*(.+?)\s*\)/) {
$file = $1;
$arg = $4;
# print "$3: $4 in $1\n";
if ($do_gets && ($3 eq "g")) {
# With quotes is an env var.
if ($arg =~ /^"([A-Z0-9_]+)"$/) {
push (@{$list{$1}}, $file);
}
# Without quotes is a macro.
elsif ($arg =~ /^([A-Z0-9_]+)$/) {
push (@{$macros{$1}}, $file);
}
# Lower case is a variable expression.
elsif ($do_all) {
if (index ($arg, "(") != -1) {
$arg = "$arg)";
}
push (@{$list{"($arg)"}}, $file);
}
}
elsif ($do_sets && ($3 eq "s")) {
# With quotes is an env var.
if ($arg =~ /^"([A-Z0-9_]+?)"\s*,.+$/) {
push (@{$list{$1}}, $file);
}
# Without quotes is a macro.
elsif ($arg =~ /^([A-Z0-9_]+?)\s*,.+$/) {
push (@{$macros{$1}}, $file);
}
# Lower case is a variable expression.
elsif ($do_all && ($arg =~ /^(.+)\s*,.+$/)) {
$arg = $1;
if (index ($arg, "(") != -1) {
$arg = "$arg)";
}
push (@{$list{"($arg)"}}, $file);
}
}
}
}
close GREP;
#exit;
my $var, $found;
# Resolve macros
foreach $var (keys (%macros)) {
open (GREP, "/usr/bin/csh -c \"/usr/bin/grep $var {*.c,*/*.c,*/*/*.c,*/*/*/*.c,*.h,*/*.h,*/*/*.h,*/*/*/*.h} | /usr/bin/grep #define\" |");
$found = 0;
while (<GREP>) {
if (/#define\s+"?$var"?\s+(.*?\S)\s*(\/\*|\r?\n)/) {
push (@{$list{"$var($1)"}}, @{$macros{$var}});
$found = 1;
}
}
close GREP;
if (!$found) {
push (@{$list{"$var(?)"}}, @{$macros{$var}});;
}
}
my $file, $last_file;
foreach $var (sort (keys (%list))) {
print "$var:\n";
$last_file = "";
foreach $file (sort (@{$list{$var}})) {
if ($file ne $last_file) {
print " $file\n";
$last_file = $file;
}
}
}