/* This file is part of cardwords
   (c) 1998 1999 2000 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_pipe.hh"
#include <vector>
#include <set>
#include <map>
#include <string>
namespace CardWords {

class ChildProcess {
public:
  // The constructor does not fork and exec, it only gets the
  // name of the child program:
  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.
  ~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;

  Pipe talkPipe;
  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, ChildProcess *> process_map;
};
}
#endif

