#!/usr/bin/perl
use strict;
use warnings;
package Commands;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
use Exporter;
our $VERSION=1.0;
our @ISA = qw(Exporter);
our @EXPORT = qw(%cmd_tmpl eval_cmd);
our @EXPORT_OK = qw();
our sub eval_cmd ($);
####################################################################
####################################################################
## Command Hash template
our %cmd_tmpl =
(
'backup' => 'token token > token',
'list' => 'token -le token'
);
## Evaluate command , accepts Command , Client, and Arguments
our sub eval_cmd ($)
{
my $array_ref = shift;
my $cmd = @{$array_ref}[0];
my $client = @{$array_ref}[1];
my @args;
if ( defined @{$array_ref}[2] )
{
@args = split(/ /,@{$array_ref}[2]);
}
die "No such command: $cmd!" unless exists $cmd_tmpl{lc($cmd)}; ## convert command to lc internally
my $cmd_value = $cmd_tmpl{$cmd};
## update command , replaces first 'token' encountered.
$cmd_value =~ s/token/$cmd/;
## update client , replaces first 'token' encountered.
$cmd_value =~ s/token/$client/;
## update arguments , replaces first 'token' encountered.
for(my $i = 0; $i <=$#args; $i++)
{
$cmd_value =~ s/token/$args[$i]/;
}
if ($cmd_value =~ m/token/)
{
die "Not enough aruments on [@{$array_ref}]";
}
return $cmd_value;
}
####################################################################
####################################################################
1;
####
#/usr/bin/perl
use strict;
use warnings;
use Commands 1.0;
sub Get_Client();
my @clients;
die "Error. Usage \'perl Command_parser.pl -Noclient | [-Command Client1 Arguments -Command Client2 Arguments...] \'\n $!" unless $#ARGV >= 0;
####################################################################
####################################################################
## If a client was not provided at the command line, get one
if (lc($ARGV[0]) eq "-noclient")
{
Get_Client();
}
else
{
my @current_command;
for(my $i = 0; $i <=$#ARGV; $i++)
{
if ( $ARGV[$i] =~ m/^\-/ && $i != 0)
{
# eval current command , then wipe it and keep processing
my $command_value = eval_cmd(\@current_command);
print "Command is :$command_value\n";
##system($command_value);
@current_command = ();
push @current_command , $ARGV[$i];
$current_command[0] =~ s/\-//g;
}
elsif ( $i == $#ARGV )
{
# eval last command
push @current_command , $ARGV[$i];
my $command_value = eval_cmd(\@current_command);
print "Command is :$command_value\n";
##system($command_value);
}
else
{
push @current_command , $ARGV[$i];
if ($i == 0)
{
$current_command[0] =~ s/\-//g;
}
}
}
}
####################################################################
####################################################################
## No client was provided at the command line, fill one in
sub Get_Client()
{
my @current_command = ('backup','mars','file1.txt');
my $cmd_value = eval_cmd(\@current_command); # here magic happens!
##security_check($cmd);
##system("$cmd");
}
##
##
C:\monks\command_template>perl Command_parser.pl -backup client1 myfile1.txt -backup client2 myfile2.txt -list client3
Command is :backup client1 > myfile1.txt
Command is :backup client2 > myfile2.txt
Command is :list -le client3