use
vars
qw($VERSION @ISA)
;
$VERSION
=
do
{
my
@r
= (
q$Revision: 1.29 $
=~ /\d+/g);
sprintf
"%d."
.
"%02d"
x
$#r
,
@r
};
$|=1;
@ISA
=
qw(Perlbug::Base)
;
my
%OPTS
= (
"h"
=>
''
,
"t"
=> 0,
'd'
=> 0,
'p'
=>
''
,
'u'
=>
''
,
);
sub
new {
my
$proto
=
shift
;
my
$class
=
ref
(
$proto
) ||
$proto
;
my
$self
= Perlbug::Base->new(
@_
);
$self
->cgi(
'no_debug'
);
$self
->check_user(
$ENV
{
'LOGNAME'
} ||
$Perlbug::User
||
'generic'
);
$self
->{
'attr'
}{
'history'
} = {};
$self
->{
'attr'
}{
'pending_ids'
} = [];
$self
->{
'attr'
}{
'pending_type'
} =
't'
;
$self
->{
'attr'
}{
'lines'
} = 25;
$self
->current({
'context'
,
'text'
});
$self
->{
'_opts'
} = \
%OPTS
;
bless
(
$self
,
$class
);
}
sub
opt {
my
$self
=
shift
;
my
$key
=
shift
||
''
;
my
@args
=
ref
(
$self
->{
'_opts'
}{
$key
}) eq
'ARRAY'
? @{
$self
->{
'_opts'
}{
$key
}} :
$self
->{
'_opts'
}{
$key
};
return
@args
;
}
sub
cmd {
my
$self
=
shift
;
$Perlbug::User
=
getpwuid
($<);
my
@data
=
getpwnam
(
$Perlbug::User
);
my
$flag
=
$self
->isadmin ?
'*'
:
''
;
my
$prompt
=
' > '
;
my
$cnt
= 1;
my
$i_total
= 0;
my
$help
=
qq|h = help, quit = quit\n|
;
print
qq|
Perlbug Database Cmd Interface $Perlbug::VERSION ($data[0]$flag -> $data[6]):
------------------------------
$help
1 $prompt|
;
READ:
while
(<>) {
chomp
(
my
$in
=
$_
);
$in
=~ s/^\s*//o;
if
(
$in
=~ /^\!\s*(\d+)\s*$/o) {
(
$in
) =
$self
->history($1);
}
elsif
(
$in
=~ /.+/o) {
}
else
{
print
"Please input a command: \n$cnt $prompt"
;
next
READ;
}
$self
->history(
$cnt
,
$in
);
last
READ
if
$in
=~ /^(quit|
exit
)$/io;
$i_total
+=
my
$i_num
=
$self
->process(
$in
);
print
"$cnt $prompt"
;
$cnt
++;
}
print
"Bye bye!\n"
;
return
$i_total
;
}
sub
process {
my
$self
=
shift
;
my
$line
=
shift
;
my
$h_cmds
=
$self
->parse_input(
"-$line"
);
my
@res
= ();
if
(
ref
(
$h_cmds
) ne
'HASH'
) {
$res
[0] =
"Command line($line) parse failure($h_cmds) - try 'h'\n"
;
}
else
{
@res
=
$self
->process_commands(
$h_cmds
);
if
(!((
scalar
(
@res
) >= 1))) {
$res
[0] =
"Command($line) failed to produce any results(@res) - try 'h'\n"
;
}
}
@res
=
$self
->scroll(
@res
);
return
@res
;
}
sub
input2args {
my
$self
=
shift
;
my
$cmd
=
shift
;
my
$arg
=
shift
||
''
;
my
$ret
=
$self
->SUPER::input2args(
$cmd
,
$arg
);
my
$wanted
=
$self
->return_type(
$cmd
);
if
(
$wanted
eq
'HASH'
) {
$$ret
{
'sourceaddr'
} ||=
$self
->isadmin.
'@'
.
$self
->
system
(
'hostname'
);
if
(
$cmd
eq
'G'
) {
(
$$ret
{
'name'
}) = $1
if
(
$$ret
{
'opts'
} =~ /^(\w+)/o);
(
$$ret
{
'description'
}) = $1
if
(
$$ret
{
'body'
} =~ /(.+)/mso);
}
elsif
(
$cmd
eq
'U'
) {
my
@args
=
split
(/\s+/,
$arg
);
$ret
= {
'userid'
=>
$args
[0],
'password'
=>
$args
[1],
'name'
=>
$args
[2],
'address'
=>
$args
[3],
'match_address'
=>
$args
[4],
};
}
}
return
$ret
;
}
sub
scroll {
my
$self
=
shift
;
my
$i_num
=
my
@data
=
@_
;
my
$i_max
=
$self
->{
'attr'
}{
'lines'
} || 35;
my
$compl
=
join
(
"\n"
,
@data
);
my
$i_cnt
= (
$compl
=~
tr
/\n/\n/);
if
(!(
$i_cnt
>
$i_max
)) {
print
@data
,
"\n"
;
}
else
{
print
"Showing $i_cnt data lines in $i_max line chunks\n"
;
my
$i_chunk
= 0;
CHUNK:
foreach
my
$chunk
(
@data
) {
next
CHUNK
unless
$chunk
;
$i_chunk
++;
my
@stuff
=
map
{
"$_\n"
}
split
(
"\n"
,
$chunk
);
print
"$i_chunk of "
.
@data
.
" - press ENTER or any other key to cancel\n"
;
DATA:
while
(
@stuff
) {
my
$res
= <>;
chomp
$res
;
last
CHUNK
if
$res
=~ /.+/o;
print
splice
(
@stuff
, 0,
$i_max
),
"\n"
;
last
DATA
unless
@stuff
;
print
scalar
(
@stuff
).
" lines remaining ...\n"
;
}
}
}
$self
->debug(2,
"items($i_num)"
)
if
$Perlbug::DEBUG
;
return
@data
;
}
sub
history {
my
$self
=
shift
;
my
$indx
=
shift
;
my
$cmd
=
shift
||
''
;
my
@hist
= ();
if
(
$indx
=~ /^\s*\d+\s*$/o) {
if
(
$cmd
=~ /^(.+)$/o) {
$self
->{
'attr'
}{
'history'
}{
$indx
} =
$cmd
;
}
@hist
= (
$self
->{
'attr'
}{
'history'
}{
$indx
});
}
elsif
(
$indx
eq
'keys'
) {
@hist
=
keys
%{
$self
->{
'attr'
}{
'history'
}};
}
else
{
@hist
=
values
%{
$self
->{
'attr'
}{
'history'
}};
}
return
@hist
;
}
sub
spec {
my
$self
=
shift
;
my
(
$dynamic
) =
$self
->SUPER::spec;
my
$perldoc
=
join
(
''
,
$self
->
read
(
'spec'
));
return
$dynamic
.
$perldoc
;
}
sub
doh {
my
$self
=
shift
;
my
$args
=
shift
;
my
@args
= (
ref
(
$args
) eq
'HASH'
) ? %{
$args
} : ();
my
$res
=
$self
->SUPER::doh({
'!'
=>
'shell escape - repeat third command (!3)'
,
'H'
=>
'History listing, use exclamation mark to repeat cmd: (!3)'
,
@args
});
return
$res
;
}
sub
doH {
my
$self
=
shift
;
my
$h_args
=
shift
;
my
$history
=
''
;
my
@keys
=
$self
->history(
'keys'
);
foreach
my
$key
(
sort
{
$a
<=>
$b
}
@keys
) {
next
unless
$key
=~ /.+/o;
my
(
$cmd
) =
$self
->history(
$key
);
$history
.=
sprintf
(
'%-6d'
,
$key
).
"$cmd\n"
;
}
return
$history
;
}
1;