/*{{{  #includes*/
#ifdef CONFIG_H
#   include "config.h"
#endif

#include <sys/types.h>
#include <sys/wait.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include <local/bool.h>

#define SHELL_C
#define I_GETMSG_C
#define I_PROMPT_C

#include "origami.h"
#include "../h/envvar_str.h"
/*}}}  */

/*{{{  waitfor(procid): wait for process and give exit-status back (error -1)*/
/*{{{  maybe define the exit-status macros*/
#ifndef WIFSTOPPED                            /* child stopped ?             */
#   define WIFSTOPPED ((i&255)==127)
#endif
#ifndef WIFEXITED                             /* chield exited ?             */
#   define WIFEXITED  ((i&255)==0)
#endif
#ifndef WEXITSTATUS                           /* get exit-code               */
#   define WEXITSTATUS (i>>8&255)
#endif
/*}}}  */
int waitfor(pid_t pid)
{ int i;
  pid_t j;

  while (pid!=(j=wait(&i)) || WIFSTOPPED(i)) if (-1==j) return(-1);
  return(WIFEXITED(i) ? WEXITSTATUS(i) : -1);
}
/*}}}  */
/*{{{  fork and call $SHELL [cmd], return exit-status to father*/
int subshell(char *cmd)
{
  pid_t child;

  if ((child=fork())==0)
  /*{{{  child*/
  {
    execl(getenv(SHELL), getenv(SHELL), cmd ? "-c" : (char*)0, cmd, (char*)0);
    exit(127);
  }
  /*}}}  */
  else if (child>0)
  /*{{{  wait for child*/
  return(waitfor(child));
  /*}}}  */
  else
  /*{{{  fork failed*/
  message(get_msg(M_FORKFAIL),TRUE);
  return(-1);
  /*}}}  */
}
/*}}}  */
