ecord that (for tidying up of
temporary files and so on).  Unsuprisingly, this works better with
L<wait_bg_cmd> than with L<do_cmd>, but we do have a handler for
SIGCHLD and we can usually get this from L<do_cmd> as well.  There is
a small race condition there, though, I think -- it gets the PID of
the most recent command which has exited when system() returns -- so
don't rely totally on this!

=cut

sub pid {
  my $self=shift;
  return $self->{pid};
}

sub set_pid {
  my $self=shift;
  my $pid=shift;
  $self->{pid}=$pid;
}

=head3 errstr

  _error($rv->errstr("The ANSYS CFX Solver"));

Returns a string which can be used in an error report by the caller,
if an error condition has occurred.  It will always return a string
describing how the process exited, so it is not in itself an indicator
of an error condition.

The argument, if any, is a descriptive name which is used instead of
the executable name.

If the C<term_error> information is set on this object, some lines
from this information will also be included in the error message.  See
the C<do_termerr_cmd> call in L<CFX5::Commands> for more details.

=cut

sub errstr {
  my $self=shift;
  my $desc=shift || $self->cmd_desc() || $self->cmd() || "Command";

  my $outinfo='';
  # we restrict this to the top 10 and bottom 10 lines if we need to
  if (my @outinfo = $self->term_error()) {
    if (scalar(@outinfo) > 20) {
      splice(@outinfo,10,(scalar(@outinfo) - 20),'...');
    }
    $outinfo=join("\n  ",
		  "\n\nIt gave the following output:",
		  @outinfo)."\n";
  }

  my $killederr='';
  if (defined $self->killederr()) {
    $killederr = " " . $self->killederr() . "  It";
  }

  unless ($self->ran()) {
    # usually this means it couldn't start ... but sometimes, it's a
    # "valid" return code, at least on NT.  Grrr.
    my $msg="$desc$killederr could not be started";
    if (defined $self->actual_rv()) {
      $msg .= ", or exited with return code " . $self->actual_rv();
    }
    if (defined $self->runerr()) {
      return "$msg: " . $self->runerr() . "." . $outinfo;
    } else {
      return "$msg." . $outinfo;
    }
  }
  if ($self->signal()) {
    return ("$desc$killederr was interrupted by signal " . 
	    $self->sig_name() . " (" . $self->signal() . ")" .
	    ($self->core() ? ' and dumped core' : '') . $outinfo);
  }
  # if it ran, and didn't have signal death, it gave a return value?
  if (defined $self->rv()) {
    return "$desc$killederr exited with return code " . $self->rv(). ". " . $outinfo;
  }
  # no, not even that.  If we have a killed error, return that, as
  # otherwise it would look funny ...
  if ($killederr) {
    return "$desc" . $self->killederr() . "  $outinfo";
  }
  # so give up.
  return "$desc aborted for unknown reasons.  $outinfo";
}

=head3 cmd_desc

If a textual description of the command is available, this can be used
to store and retrieve it, for use in error messages etc.

=cut

sub cmd_desc {
  my $self=shift;
  return $self->{cmd_desc};
}

=head3 set_cmd_desc

Sets the C<cmd_desc> string.

=cut

sub set_cmd_desc {
  my $self=shift;
  $self->{cmd_desc}=shift;
}

=head3 runerr

This item is only relevant when the C<ran> item is FALSE.  It contains
the string to give as an error message indicating why the command
failed to run (which may be something like 'unknown error' if it is
not known).  You should use this instead of C<$!>, which is not
reliable for this purpose.

=cut

sub runerr {
  my $self=shift;
  return $self->{runerr};
}

=head3 set_runerr

Sets the C<runerr> string.  This is useful when a descriptive error
from the system cannot be easily encoded in $?, for example on Win32.

=cut

sub set_runerr {
  my $self=shift;
  $self->{runerr}=shift;
}

=head3 killederr

This item contains, if relevant, a string describing why a process was
terminated.  It is used by timeout code to explain that a process was
explicitly timed out.

=cut

sub killederr {
  my $self=shift;
  return $self->{killederr};
}

=head3 set_killederr

Sets the C<killederr> string.

=cut

sub set_killederr {
  my $self=shift;
  $self->{killederr}=shift;
}

=head3 shell_rv

Returns a shell-style numeric return value.  This is either the actual
return value, the signal number +128 if the process was killed by a
signal, or 255 if the process could not run.

=cut

sub shell_rv {
  my $self=shift;

  unless ($self->ran()) {
    return 255;
  } elsif ($self->signal()) {
    return $self->signal() + 128;
  } else {
    return $self->rv();
  }
}


=head2 ADDITIONAL INFORMATION

Objects of this type have the capability to store additional
information about the result of a command, which can be interrogated
by users of the object when necessary.  So far, we have only a single
field, storing the output of the command, for use if it was run with
the get_cmd_output call.

=head3 cmd_output

Returns the command output from the command which produced this
result.  If called in a list context, returns a list of CR-separated
lines; in a scalar context, returns all the lines joined together as a
large string.

=cut

sub cmd_output {
  my $self=shift;
  if (wantarray) {
    if (defined $self->{cmd_output}) {
      return @{$self->{cmd_output}};
    } else {
      return ();
    }
  } else {
    if (defined $self->{cmd_output}) {
      return join("\n", @{$self->{cmd_output}});
    } else {
      return undef;
    }
  }
}

=head3 set_cmd_output

The counterpart of the C<cmd_output> call, this sets the field
containing the output.  The arguments should be the list of lines in
the output.

=cut

sub set_cmd_output {
  my $self=shift;
  # we might as well try to split it, it may or may not contain
  # embedded newlines
  $self->{cmd_output}=[@_];
}

=head3 term_error

Returns the output from the command which produced this result.  This
behaves largely like C<cmd_output>, but if any text is recorded in
this slot it will be included in any error messages which are produced
by this object.  See the C<do_termerr_cmd> call in L<CFX5::Commands>
for more details.

If called in a list context, returns a list of CR-separated lines; in
a scalar context, returns all the lines joined together as a large
string.

=cut

sub term_error {
  my $self=shift;
  if (wantarray) {
    if (defined $self->{term_error}) {
      return @{$self->{term_error}};
    } else {
      return ();
    }
  } else {
    if (defined $self->{term_error}) {
      return join("\n", @{$self->{term_error}});
    } else {
      return undef;
    }
  }
}

=head3 set_term_error

The counterpart of the C<term_error> call, this sets the field
containing the output.  The arguments should be the list of lines in
the output.

=cut

sub set_term_error {
  my $self=shift;
  $self->{term_error}=[@_];
}

1;
