/* ----------------------------------------------------------------------
   Execute the given mailcap command

  Args: cmd           -- the command to execute
	image_file    -- the file the data is in
	needsterminal -- does this command want to take over the terminal?
  ----*/
void
exec_mailcap_cmd(cmd, image_file, needsterminal)
char *cmd;
char *image_file;
int   needsterminal;
{
    char   *command = NULL,
	   *result_file = NULL,
	   *p;
    char  **r_file_h;
    PIPE_S *syspipe;
    int     mode;

    p = command = (char *)fs_get((32 + strlen(cmd) + (2*strlen(image_file)))
			     * sizeof(char));
    if(!needsterminal)  /* put in background if it doesn't need terminal */
      *p++ = '(';
    sprintf(p, "%s ; rm -f %s", cmd, image_file);
    p += strlen(p);
    if(!needsterminal){
	*p++ = ')';
	*p++ = ' ';
	*p++ = '&';
    }
    *p++ = '\n';
    *p   = '\0';
    dprint(9, (debugfile, "exec_mailcap_cmd: command=%s\n", command));

    if(needsterminal)
      r_file_h = NULL;
    else{
	result_file = temp_nam(NULL, "pine_cmd");
	r_file_h    = &result_file;
    }

    mode = PIPE_SYS | (needsterminal ? PIPE_NOPIPE : 0);
    if(syspipe = open_system_pipe(command, r_file_h, mode)){
	(void)close_system_pipe(&syspipe, mode);
	if(needsterminal)
	  q_status_message(0, 0, 4, "VIEWER command completed");
	else
	  display_system_pipe_output(result_file, "VIEWER");
    }
    else
      q_status_message1(0, 1, 4, "Cannot spawn command : %s", cmd);

    fs_give((void **)&command);
    if(result_file)
      fs_give((void **)&result_file);
}


/* ----------------------------------------------------------------------
   Execute the given mailcap test= cmd

  Args: cmd -- command to execute
  Returns exit status
  
  ----*/
int
exec_mailcap_test_cmd(cmd)
    char *cmd;
{
    return(system(cmd));
}


