/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef CARDWORDS_CHILDPROCESS_HH
#define CARDWORDS_CHILDPROCESS_HH

// The class CardWords_ChildProcess forks and execs a child process.
// It creates a pipe and dupes the pipe to stdin and stdout of the
// child process.
// The parent process can ask for the file
// descriptors, and the state of the child. The destructor sends
// sigterm to the child and waits for it to exit.
#include "cardwords_iostream.hh"
#include "cardwords_pipe.hh"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <unistd.h> // for sleep()

class CardWords_ChildProcess {
public:
  // The constructor does not fork and exec, it only gets the
  // name of the child program:
  CardWords_ChildProcess (const string & progname);

  // add a command line argument:
  void add_argument(const string & arg);

  // fork and exec:
  void start (bool with_pipes = true);
  
  bool is_running(void);

  int send_signal(int);

  //  returns the status bits from waitpid(..,int *status,..):
  int get_exit_status (void);

  bool exited_abnormally (void);

  // Destructor sends the term sig and waits for the child to finish.
  ~CardWords_ChildProcess();

  // catch death of childs:
  static void sigchldhandler (int);

  // The communication handles:
  int get_talk_fd (void) const;
  int get_listen_fd (void) const;
  
private:
  pid_t pid;
  volatile int status;

  CardWords_Pipe talkPipe;
  CardWords_Pipe listenPipe;

  volatile bool running;
  bool wait_called;

  // The argument vector:
  vector<const char *> argv; // The char *'s are needed for the execv call.

  // The strings that were copied:
  vector<string> strings;
  
  // The map of child processes, needed by sigchldhandler:
  static map<pid_t, CardWords_ChildProcess *> process_map;
};

inline bool
CardWords_ChildProcess::is_running(void)
{
  return running;
}

//  returns the status bits from waitpid(..,int *status,..):
inline int
CardWords_ChildProcess::get_exit_status (void)
{
  return status;
}

inline bool
CardWords_ChildProcess::exited_abnormally (void)
{
  if (is_running())
    return false;

  return WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0;
}

#ifndef IMPLIKATION
#define IMPLIKATION(a,b) (!(a) || (b))
#endif

// Destructor sends the term sig and waits for the child to finish.
inline
CardWords_ChildProcess::~CardWords_ChildProcess()
{
  if (is_running()) {
    send_signal(SIGTERM);

    while (is_running()) {
      // FIXME: logfile logging needed
      sleep(1);
    }
  }
}

// The communication handles:
inline int CardWords_ChildProcess::get_talk_fd (void) const
{
  return talkPipe.get_talkFD();
}

inline int CardWords_ChildProcess::get_listen_fd (void) const
{
  return listenPipe.get_listenFD();
}
#endif
