exec.pl 1.46 KB
Newer Older
1
# Copyright 2012 - 2013, Steve Rader
2
# Copyright 2013 - 2018, Scott Kostyshak
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

sub task_exec {
  my ($cmd) = @_;
  my $es = 0;
  my $result = '';
  &audit("TASK EXEC $task $cmd 2>&1");
  open(IN,"echo -e \"yes\\n\" | $task $cmd 2>&1 |");
  while(<IN>) {
    chop;
    $_ =~ s/\x1b.*?m//g; # decolorize
    if ( $_ =~ /^\w+ override:/ ) { next; }
    $result .= "$_ ";
  }
  close(IN);
  if ( $! ) {
    $es = 1;
    &audit("FAILED \"$task $cmd\" error closing short pipe");
  }
  if ( $? != 0 ) {
    $es = $?;
    &audit("FAILED \"$task $cmd\" returned exit status $?");
  }
  return ($es,$result);
}

#------------------------------------------------------------------

30 31 32 33 34 35
sub exited_successfully {
  my $status = shift || 0;
  return 1  if  WIFEXITED($status) and WEXITSTATUS($status)==0;
  return undef;
}

36 37 38 39 40 41 42 43 44 45 46 47 48
sub shell_exec {
  my ($cmd,$mode) = @_;
  endwin();
  if ( $clear ne 'NOT_FOUND' ) { system("$clear"); }
  if ( $audit ) {
    print "$_[0]\r\n";
  }
  if ( ! fork() ) {
    &audit("EXEC $cmd");
    exec($cmd);
    exit();
  }
  wait();
49 50 51 52 53 54 55 56
  my $success = &exited_successfully($?);
  # two reasons to wait:
  # - an error occurred
  # - $mode is wait and the user didn't specify nowait in config file
  if ( not $success or ( $mode eq 'wait' and not $nowait ) or $mode eq 'forcewait' ) {
    if (not $success) {
      print "Error while executing command `$cmd'\n";
    }
57 58 59 60 61 62 63 64 65
    print "Press return to continue.\r\n";
    <STDIN>;
  }
  &init_curses('refresh');
  &draw_screen();
}

return 1;