1 |
dpavlin |
97 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
use lib 'lib'; |
7 |
|
|
|
8 |
|
|
use Term::Shelly; |
9 |
|
|
use CWMP::Store; |
10 |
|
|
use DBM::Deep; |
11 |
|
|
use Data::Dump qw/dump/; |
12 |
|
|
use Getopt::Long; |
13 |
|
|
|
14 |
|
|
my $sh = Term::Shelly->new(); |
15 |
|
|
|
16 |
|
|
my $debug = 0; |
17 |
|
|
my $store_path = 'state.db'; |
18 |
|
|
|
19 |
|
|
GetOptions( |
20 |
|
|
'debug+' => \$debug, |
21 |
|
|
'store-path=s' => \$store_path, |
22 |
|
|
); |
23 |
|
|
|
24 |
|
|
our $store = CWMP::Store->new({ |
25 |
|
|
debug => $debug, |
26 |
|
|
path => $store_path, |
27 |
|
|
}); |
28 |
|
|
|
29 |
|
|
$sh->out( |
30 |
|
|
"You can issue commenads in form using tab-complete: |
31 |
|
|
|
32 |
|
|
CPE_Serial ( parametar [ [=] value ] | command ) |
33 |
|
|
" |
34 |
|
|
); |
35 |
|
|
|
36 |
|
|
$sh->{"completion_function"} = \&completer; |
37 |
|
|
$sh->{"readline_callback"} = \&command; |
38 |
|
|
|
39 |
|
|
my @history = ( 'exit' ); |
40 |
|
|
my $pos = $#history; |
41 |
|
|
$sh->{'mappings'}->{'up-history'} = [ sub { |
42 |
|
|
my $self = shift; |
43 |
|
|
if ( $pos >= 0 ) { |
44 |
|
|
$self->{'input_line'} = $history[ $pos ]; |
45 |
|
|
$pos--; |
46 |
|
|
$self->{'input_position'} = length( $self->{'input_line'} ); |
47 |
|
|
$self->fix_inputline; |
48 |
|
|
} |
49 |
|
|
} ]; |
50 |
|
|
$sh->{'mappings'}->{'down-history'} = [ sub { |
51 |
|
|
my $self = shift; |
52 |
|
|
my $line = ''; |
53 |
|
|
if ( $pos < $#history ) { |
54 |
|
|
$pos++; |
55 |
|
|
$line = $history[ $pos ]; |
56 |
|
|
} |
57 |
|
|
$self->{'input_line'} = $line; |
58 |
|
|
$self->{'input_position'} = length( $self->{'input_line'} ); |
59 |
|
|
$self->fix_inputline; |
60 |
|
|
} ]; |
61 |
|
|
|
62 |
dpavlin |
99 |
$sh->prompt( '> ' ); |
63 |
dpavlin |
97 |
|
64 |
|
|
while (1) { |
65 |
|
|
$sh->do_one_loop(); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
sub completer { |
69 |
|
|
my ($line, $bword, $pos, $curword) = @_; |
70 |
|
|
|
71 |
|
|
$sh->out( "line: '$line' [ $bword - $pos ] -> '$curword'" ); |
72 |
|
|
|
73 |
|
|
my @matches; |
74 |
|
|
|
75 |
dpavlin |
99 |
# do we have list (part) of CPE? |
76 |
|
|
if ( $line =~ /^(\w+)\s*$/ ) { |
77 |
dpavlin |
97 |
@matches = sort grep { /^\Q$curword\E/ } $store->known_CPE; |
78 |
|
|
$sh->out( "CPE available: ", join(",", @matches ) ); |
79 |
dpavlin |
99 |
} elsif ( $line =~ |
80 |
dpavlin |
97 |
|
81 |
|
|
return @matches; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
sub command { |
85 |
|
|
my ( $line ) = @_; |
86 |
|
|
|
87 |
|
|
return unless ( $line && $line ne '' ); |
88 |
|
|
|
89 |
|
|
# just CPE uid |
90 |
|
|
if ( $line =~ /^(\w+)\s*$/ ) { |
91 |
|
|
if ( main->can( $1 ) ) { |
92 |
|
|
$sh->out( "# execute command $1" ); |
93 |
|
|
eval " \&$1( \$line ) "; |
94 |
|
|
} elsif ( my $state = $store->db->get('state')->get( $1 ) ) { |
95 |
|
|
$sh->out( join(" ", keys %{ $state }) ); |
96 |
|
|
push @history, $line; |
97 |
|
|
$pos = $#history; |
98 |
|
|
} else { |
99 |
|
|
$sh->out( "$line: CPE not found" ); |
100 |
|
|
} |
101 |
|
|
} else { |
102 |
|
|
$sh->out( "$line: command not recognized" ); |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
sub history { |
107 |
|
|
$sh->out( "history: ", dump ( @history ) ); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sub exit { |
111 |
|
|
CORE::exit(); |
112 |
|
|
} |