/*******************************************************************\

              Copyright (C) 2004 Joseph Coffland

    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.

            For information regarding this software email
                   jcofflan@users.sourceforge.net

\*******************************************************************/
#ifndef BASICPROCESS_H
#define BASICPROCESS_H

#include <iostream>
#include <string>
#include <list>

#include "BasicPipe.h"

struct ChildPipe {
  BasicPipe pipe;
  int direction;
  int fd;

  ChildPipe(int direction, int fd) : direction(direction), fd(fd) {}
};

class BasicProcess {
  typedef std::list<ChildPipe *> pipes_t;
  pipes_t pipes;

  int pid;
  bool running;
  int returnCode;

public:
  typedef enum {TO_CHILD, FROM_CHILD} dir_t;

  BasicProcess();
  ~BasicProcess();

  void exec(std::list<std::string> &args);
  void exec(char *args);
  void exec(char *argv[]);

  static void parseArgs(char *args, int &argc, char *argv[], int n);

  BasicPipe *getChildPipe(dir_t dir, int childFD = -1);

  int getPID() {return pid;}
  void kill(int sig);
  int wait(int options = 0);

  bool isRunning();
  int getReturnCode() {return returnCode;}
};
#endif // BASICPROCESS_H
