line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bash::History::Read; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2015-11-07'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.04'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3588
|
use 5.014; |
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
6
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
947
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw(each_hist); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(parse_bash_history_file); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _doit { |
15
|
1
|
|
|
1
|
|
3
|
my ($which, $code, $fh0) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $call_code = sub { |
18
|
5
|
|
|
5
|
|
11
|
my ($ts, $content) = @_; |
19
|
5
|
|
|
|
|
8
|
package main { |
20
|
5
|
|
|
|
|
11
|
local $_ = $content; |
21
|
5
|
|
|
|
|
8
|
local $main::TS = $ts; |
22
|
5
|
|
|
|
|
10
|
local $main::PRINT = 1; |
23
|
5
|
|
|
|
|
10
|
$code->(); |
24
|
5
|
100
|
|
|
|
16
|
if (!defined($main::TS)) { |
25
|
3
|
|
|
|
|
6
|
undef $ts; |
26
|
|
|
|
|
|
|
} |
27
|
5
|
50
|
|
|
|
22
|
if ($main::PRINT) { |
28
|
0
|
0
|
|
|
|
0
|
print "#$ts\n" if defined $ts; |
29
|
0
|
|
|
|
|
0
|
print; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
1
|
|
|
|
|
7
|
}; |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
|
|
2
|
my $fh; |
35
|
1
|
50
|
|
|
|
7
|
if ($which eq 'each_hist') { |
36
|
0
|
|
|
|
|
0
|
$fh = \*ARGV; |
37
|
|
|
|
|
|
|
} else { |
38
|
1
|
|
|
|
|
3
|
$fh = $fh0; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
2
|
my $ts; |
42
|
1
|
|
|
|
|
45
|
while (defined(my $line = <$fh>)) { |
43
|
7
|
100
|
|
|
|
33
|
if ($line =~ /\A#(\d+)$/) { |
44
|
2
|
|
|
|
|
14
|
$ts = $1; |
45
|
|
|
|
|
|
|
} else { |
46
|
5
|
|
|
|
|
12
|
$call_code->($ts, $line); |
47
|
5
|
|
|
|
|
34
|
undef $ts; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub each_hist(&) { |
53
|
0
|
|
|
0
|
1
|
0
|
my $code = shift; |
54
|
0
|
|
|
|
|
0
|
_doit('each_hist', $code); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub parse_bash_history_file { |
58
|
1
|
|
|
1
|
1
|
11
|
my ($path) = @_; |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
0
|
|
|
6
|
$path //= $ENV{HISTFILE} // "$ENV{HOME}/.bash_history"; |
|
|
|
33
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
114
|
open my($fh), "<", $path or die "Can't open bash history file '$path': $!"; |
63
|
1
|
|
|
|
|
3
|
my $res = []; |
64
|
|
|
|
|
|
|
_doit('parse_bash_history_file', |
65
|
|
|
|
|
|
|
sub { |
66
|
5
|
|
|
5
|
|
17
|
push @$res, [$main::TS, $_]; |
67
|
5
|
|
|
|
|
10
|
$main::PRINT = 0; |
68
|
|
|
|
|
|
|
}, |
69
|
1
|
|
|
|
|
12
|
$fh, |
70
|
|
|
|
|
|
|
); |
71
|
1
|
|
|
|
|
29
|
$res; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
# ABSTRACT: Utility to read bash history file entries |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
__END__ |