--- nfo/perl/libs/shortcuts.pm 2003/04/04 17:31:59 1.8 +++ nfo/perl/libs/shortcuts.pm 2003/05/13 05:36:24 1.9 @@ -1,7 +1,12 @@ ## --------------------------------------------------------------------------- -## $Id: shortcuts.pm,v 1.8 2003/04/04 17:31:59 joko Exp $ +## $Id: shortcuts.pm,v 1.9 2003/05/13 05:36:24 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: shortcuts.pm,v $ +## Revision 1.9 2003/05/13 05:36:24 joko +## heavy modifications to run_cmd +## + sub get_executable +## + sub get_executable_wrapper +## ## Revision 1.8 2003/04/04 17:31:59 joko ## + sub make_guid ## @@ -49,7 +54,7 @@ use Data::Dumper; use POSIX qw( strftime ); #use IPC::Run qw( run timeout ); -use IPC::Run qw( start pump finish timeout ) ; +use IPC::Run qw( start pump finish timeout run ) ; use Carp; @@ -68,12 +73,38 @@ return strftime("%Y-%m-%d", localtime); } +sub get_executable { + my $cmd = shift; + # FIXME: detect type of program and run with proper application/interpreter + # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!? + # => better use absolute path-names only?! + my $application = ''; + if ($cmd =~ m/\w+\.pl\s*.*/) { + $application = 'perl'; + } + return $application; +} + +sub get_executable_wrapper { + my $cmd = shift; + my $application = ''; + # Required to adapt to IPC::Run on win32. + if (RUNNING_IN_HELL()) { + #$application = 'cmd.exe /C'; + $application = 'cmd.exe /C'; + } + return $application; +} + + sub run_cmd { my $cmd = shift; my $caption = shift; my $options = shift; #$cmd = 'perl ' . $cmd; + #print Dumper($options); + # report - header my $sep = "-" x 60; print $sep, "\n"; @@ -88,14 +119,32 @@ $cmd =~ m/(.+?)\s/; my $executable = $1; +=pod # for unix: check if executable is in local directory, if so - prefix with './' if (!RUNNING_IN_HELL()) { #if ($cmd !~ m/\//) { if (-e $executable) { - $cmd = "./$cmd"; } } - +=cut + + # new of 2003-05-07: basedir option to be prepended to command string + my $basedir = $options->{BASEDIR}; + my $use_path = $options->{USE_PATH}; + + # for all systems: check existance of files - use basedir if given, try current directory otherwise + if ($basedir) { + -e "$basedir/$executable" or die("$basedir/$executable does not exist."); + $basedir .= '/'; + } elsif ($use_path) { + $basedir = ""; + } else { + -e $executable or die("$executable does not exist."); + #$basedir = "."; + $basedir .= './'; + } + $cmd = "$basedir$cmd"; + # V1 - backticks or qq{} #`$cmd`; #qq{$cmd}; @@ -105,42 +154,43 @@ # V3 - using IPC::Run (optional) if ($options->{async}) { - # FIXME: detect type of program and run with proper application/interpreter - # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here! - # => better use absolute path-names only?! - my $application = ''; - if (RUNNING_IN_HELL()) { - $application = 'cmd.exe /C'; - } - - if ($cmd =~ m/\w+\.pl\s*.*/) { - $application = 'perl'; - } - + my $application = get_executable_wrapper($cmd); $cmd = "$application $cmd" if $application; - print "IPC::Run: $cmd", "\n"; + print "run_cmd: IPC::Run: $cmd", "\n"; #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; my @cmd = split(' ', $cmd); my $in; my $out; my $err; - start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'."); + #print "IPC::Run: $cmd", "\n"; + #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); + run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); #$IPC::Run::Timer::timeout = 2000; #start $cmd or die("IPC::Run could not start '$cmd'."); } else { + if (!$use_path) { + my $application = get_executable($cmd); + $cmd = "$application $cmd" if $application; + } + print "run_cmd: system('$cmd').", "\n"; system($cmd); } - print "ready.", "\n"; + print "run_cmd: ready.", "\n"; } sub run_cmds { + my $options = {}; + if (ref $_[$#_] eq 'HASH') { + #print "YAI", "\n"; + $options = pop @_; + } foreach (@_) { - run_cmd($_); + run_cmd($_, '', $options); } }