line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LEOCHARRE::CLI; |
2
|
4
|
|
|
4
|
|
80239
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
137
|
|
3
|
4
|
|
|
4
|
|
22
|
use Carp; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
421
|
|
4
|
4
|
|
|
4
|
|
23
|
use Cwd; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
480
|
|
5
|
4
|
|
|
4
|
|
20
|
use vars qw($VERSION); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
9433
|
|
6
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)/g; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$main::DEBUG = 0; |
9
|
|
|
|
|
|
|
$main::USAGE = 0; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub main::DEBUG : lvalue { |
12
|
2
|
|
|
2
|
|
31
|
$main::DEBUG; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub main::debug { |
16
|
1
|
50
|
|
1
|
|
6
|
$main::DEBUG or return 1; |
17
|
1
|
|
|
|
|
3
|
my $msg = shift; |
18
|
1
|
|
|
|
|
26
|
print STDERR " $0, $msg\n"; |
19
|
1
|
|
|
|
|
8
|
return 1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# BEGIN USER |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub main::whoami { |
35
|
4
|
100
|
|
4
|
|
547
|
unless (defined $::WHOAMI){ |
36
|
2
|
|
|
|
|
1664
|
require File::Which; |
37
|
2
|
50
|
|
|
|
1997
|
if (my $wb = File::Which::which('whoami')){ |
38
|
2
|
|
|
|
|
35755
|
my $whoami = `$wb`; |
39
|
2
|
|
|
|
|
70
|
chomp $whoami; |
40
|
2
|
|
|
|
|
67
|
$::WHOAMI = $whoami; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
0
|
|
|
|
|
0
|
warn("whoami bin path not found."); |
44
|
0
|
|
|
|
|
0
|
$::WHOAMI = 0; |
45
|
0
|
|
|
|
|
0
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
4
|
50
|
|
|
|
153
|
$::WHOAMI or return; |
49
|
|
|
|
|
|
|
|
50
|
4
|
|
|
|
|
123
|
return $::WHOAMI; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub main::force_root { |
54
|
0
|
0
|
0
|
0
|
|
0
|
main::running_as_root() or print "$0, only root can use this." and exit; |
55
|
0
|
|
|
|
|
0
|
return 1; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub main::running_as_root { |
59
|
1
|
50
|
|
1
|
|
15580
|
my $whoami = main::whoami() or return 0; |
60
|
1
|
50
|
|
|
|
8
|
$whoami eq 'root' or return 0; |
61
|
1
|
|
|
|
|
8
|
return 1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub main::get_uid { |
65
|
2
|
|
|
2
|
|
596
|
my $name = shift; |
66
|
2
|
100
|
|
|
|
8
|
main::user_exists($name) or return; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
6
|
require Linux::usermod; |
69
|
1
|
|
|
|
|
7
|
my $user = Linux::usermod->new($name); |
70
|
1
|
|
|
|
|
835
|
my $id = $user->get('uid'); |
71
|
1
|
50
|
|
|
|
26
|
$id=~/^\d+$/ or return; |
72
|
1
|
|
|
|
|
11
|
return $id; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub main::user_exists { |
76
|
4
|
|
|
4
|
|
18
|
my $name = shift; |
77
|
4
|
|
|
|
|
1451
|
require Linux::usermod; |
78
|
4
|
|
|
|
|
12644
|
my %u = Linux::usermod->users; |
79
|
4
|
100
|
|
|
|
1072
|
$u{$name} or return 0; |
80
|
3
|
|
|
|
|
25
|
return 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub main::get_gid { |
84
|
1
|
|
|
1
|
|
283
|
my $name = shift; |
85
|
1
|
50
|
|
|
|
3
|
main::user_exists($name) or return; |
86
|
1
|
|
|
|
|
6
|
require Linux::usermod; |
87
|
1
|
|
|
|
|
6
|
my $g = Linux::usermod->new($name,1); |
88
|
1
|
|
|
|
|
862
|
my $id = $g->get('gid'); |
89
|
1
|
50
|
|
|
|
18
|
$id=~/^\d+$/ or return; |
90
|
1
|
|
|
|
|
6
|
return $id; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# END USER |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub main::get_mode { |
101
|
1
|
|
|
1
|
|
310
|
my $abs = shift; |
102
|
1
|
|
|
|
|
1015
|
require File::chmod; |
103
|
1
|
50
|
|
|
|
8798
|
my $mod = File::chmod::getmod($abs) or return; |
104
|
1
|
|
|
|
|
53
|
return $mod; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# BEGIN GOPTS AND ARGS |
117
|
|
|
|
|
|
|
sub main::gopts { |
118
|
0
|
|
|
0
|
|
0
|
my $opts = shift; |
119
|
0
|
|
0
|
|
|
0
|
$opts||=''; |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
0
|
if($opts=~s/v\:?|h\:?|d\:?//sg){ |
122
|
0
|
0
|
|
|
|
0
|
print STDERR("$0, options changed") if ::DEBUG; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
$opts.='vhd'; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
my $o = {}; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
require Getopt::Std; |
130
|
0
|
|
|
|
|
0
|
Getopt::Std::getopts($opts, $o); |
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
0
|
if($o->{v}){ |
133
|
0
|
0
|
|
|
|
0
|
if (defined $::VERSION){ |
134
|
0
|
|
|
|
|
0
|
print $::VERSION; |
135
|
0
|
|
|
|
|
0
|
exit; |
136
|
|
|
|
|
|
|
} |
137
|
0
|
|
|
|
|
0
|
print STDERR "$0 has no version\n"; |
138
|
0
|
|
|
|
|
0
|
exit; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
0
|
if ($o->{d}){ |
142
|
0
|
|
|
|
|
0
|
$main::DEBUG = 1; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
0
|
0
|
|
|
|
0
|
if($o->{h}){ |
147
|
0
|
|
|
|
|
0
|
main::man() |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
0
|
return $o; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub main::argv_aspaths { |
157
|
0
|
|
|
0
|
|
0
|
my @argv; |
158
|
0
|
0
|
|
|
|
0
|
scalar @ARGV or return; |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
0
|
for(@ARGV){ |
161
|
0
|
0
|
0
|
|
|
0
|
my $abs = Cwd::abs_path($_) or warn("$0, Does not resolve: $_, skipped.") and next; |
162
|
0
|
0
|
0
|
|
|
0
|
-e $abs or warn("$0, Does not exist: $_, skipped.") and next; |
163
|
0
|
|
|
|
|
0
|
push @argv, $abs; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
0
|
scalar @argv or return; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
0
|
return \@argv; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub main::argv_aspaths_strict { |
172
|
0
|
|
|
0
|
|
0
|
my @argv; |
173
|
0
|
0
|
|
|
|
0
|
scalar @ARGV or return; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
0
|
for(@ARGV){ |
176
|
0
|
0
|
0
|
|
|
0
|
my $abs = Cwd::abs_path($_) or warn("Does not resolve: $_.") and return; |
177
|
0
|
0
|
0
|
|
|
0
|
-e $abs or warn("Is not on disk: $_.") and return; |
178
|
0
|
|
|
|
|
0
|
push @argv, $abs; |
179
|
|
|
|
|
|
|
} |
180
|
0
|
0
|
|
|
|
0
|
scalar @argv or return; |
181
|
0
|
|
|
|
|
0
|
return \@argv; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub main::argv_aspaths_loose { |
185
|
0
|
|
|
0
|
|
0
|
my @argv; |
186
|
0
|
0
|
|
|
|
0
|
scalar @ARGV or return; |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
0
|
for(@ARGV){ |
189
|
0
|
0
|
0
|
|
|
0
|
my $abs = Cwd::abs_path($_) or warn("$0, Does not resolve: $_, skipped.") and next; |
190
|
0
|
|
|
|
|
0
|
push @argv, $abs; |
191
|
|
|
|
|
|
|
} |
192
|
0
|
0
|
|
|
|
0
|
scalar @argv or return; |
193
|
0
|
|
|
|
|
0
|
return \@argv; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub main::yn { |
197
|
0
|
|
0
|
0
|
|
0
|
my $question = shift; $question ||='Your answer? '; |
|
0
|
|
|
|
|
0
|
|
198
|
0
|
|
|
|
|
0
|
my $val = undef; |
199
|
0
|
|
|
|
|
0
|
until (defined $val){ |
200
|
0
|
|
|
|
|
0
|
print "$question (y/n): "; |
201
|
0
|
|
|
|
|
0
|
$val = ; |
202
|
0
|
|
|
|
|
0
|
chomp $val; |
203
|
0
|
0
|
|
|
|
0
|
if ($val eq 'y'){ $val = 1; } |
|
0
|
0
|
|
|
|
0
|
|
204
|
0
|
|
|
|
|
0
|
elsif ($val eq 'n'){ $val = 0;} |
205
|
0
|
|
|
|
|
0
|
else { $val = undef; } |
206
|
|
|
|
|
|
|
} |
207
|
0
|
|
|
|
|
0
|
return $val; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# END GOPTS AND ARGS |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# BEIGN HELP , USAGE, MAN ETC |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub main::man { |
233
|
|
|
|
|
|
|
|
234
|
0
|
0
|
|
0
|
|
0
|
if( defined $main::usage ){ |
|
|
0
|
|
|
|
|
|
235
|
0
|
|
|
|
|
0
|
my $output = $main::usage; |
236
|
0
|
|
|
|
|
0
|
print STDERR "$output\n"; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
elsif( defined &main::usage ){ |
240
|
0
|
|
|
|
|
0
|
my $output = main::usage(); |
241
|
0
|
|
|
|
|
0
|
print STDERR "$output\n"; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
else { |
245
|
0
|
|
|
|
|
0
|
my $name = main::_scriptname(); |
246
|
0
|
|
|
|
|
0
|
print `man $name`; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
0
|
exit; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# END HELP |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub main::_scriptname{ |
262
|
1
|
50
|
|
1
|
|
6
|
my $name = $0 or return; |
263
|
1
|
|
|
|
|
8
|
$name=~s/^.+\///; |
264
|
1
|
|
|
|
|
5
|
return $name; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub main::_scriptname_only{ |
268
|
4
|
50
|
|
4
|
|
34
|
my $name = $0 or return; |
269
|
4
|
|
|
|
|
64
|
$name=~s/^.+\///; |
270
|
4
|
|
|
|
|
20
|
$name=~s/\.\w{1,}$//; |
271
|
4
|
|
|
|
|
32
|
return $name; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub main::config { |
275
|
3
|
|
|
3
|
|
47861
|
my $abs_conf = shift; |
276
|
|
|
|
|
|
|
|
277
|
3
|
|
66
|
|
|
27
|
$abs_conf ||= main::suggest_abs_conf(); |
278
|
|
|
|
|
|
|
|
279
|
3
|
50
|
0
|
|
|
25
|
$abs_conf |
280
|
|
|
|
|
|
|
or warn("Cannot determine abs_conf automatically and no arg passed") |
281
|
|
|
|
|
|
|
and return; |
282
|
|
|
|
|
|
|
|
283
|
3
|
100
|
50
|
|
|
225
|
-f $abs_conf |
284
|
|
|
|
|
|
|
or warn("$0, [$abs_conf] does not exist.") |
285
|
|
|
|
|
|
|
and return; |
286
|
|
|
|
|
|
|
|
287
|
2
|
|
|
|
|
3497
|
require YAML; |
288
|
2
|
|
|
|
|
29222
|
return (YAML::LoadFile($abs_conf)); |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub main::suggest_abs_conf { |
292
|
3
|
50
|
0
|
3
|
|
29
|
$ENV{HOME} or warn("ENV HOME not set") and return; |
293
|
3
|
|
|
|
|
40
|
return ( $ENV{HOME}.'/'. main::_scriptname_only().'.conf'); |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
sub main::suggest_abs_log { |
297
|
1
|
50
|
0
|
1
|
|
762
|
$ENV{HOME} or warn("ENV HOME not set") and return; |
298
|
1
|
|
|
|
|
5
|
return ( $ENV{HOME}.'/'. main::_scriptname_only().'.log'); |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
sub main::mktmpdir { |
302
|
1
|
|
|
1
|
|
71
|
my $d = '/tmp/tmp_'.time().( int rand(2000000) ); |
303
|
1
|
50
|
0
|
|
|
90
|
return undef and warn("$0, $d exists") if -d $d; |
304
|
1
|
50
|
|
|
|
137
|
mkdir $d or die("$0, cannot make $d, $!"); |
305
|
1
|
|
|
|
|
11
|
return $d; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
# BEGIN OPERATING SYSTEM AND WITCH |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
sub main::os_is_win { |
316
|
2
|
|
|
2
|
|
13
|
for(qw(dos os2 mswin32)){ |
317
|
6
|
50
|
|
|
|
51
|
$^O=~/^$_/i or next; |
318
|
0
|
|
|
|
|
0
|
return 1; |
319
|
|
|
|
|
|
|
} |
320
|
2
|
|
|
|
|
5
|
return 0; |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
sub witch { |
324
|
0
|
|
|
0
|
1
|
|
my $bin = shift; |
325
|
0
|
|
|
|
|
|
require File::Which; |
326
|
0
|
0
|
|
|
|
|
my $binpath = File::Which::which($bin) |
327
|
|
|
|
|
|
|
or dye("Can't find $bin, is it installed?"); |
328
|
0
|
|
|
|
|
|
return $binpath; |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
|
333
|
0
|
0
|
|
0
|
1
|
|
sub say { print STDERR (+shift)."\n" and return 1; } |
334
|
0
|
0
|
|
0
|
1
|
|
sub dye { say("$0, ".(+shift)) and exit 1; } |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
*::witch = \&witch; |
337
|
|
|
|
|
|
|
*::say = \&say; |
338
|
|
|
|
|
|
|
*::dye = \&dye; |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# END OPERATING SYSTEM AND WITCH |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
1; |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
__END__ |