/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * 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
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_procstream_hh
#define _uds_procstream_hh


// includes

#include <sys/types.h>
#include <fstream>


namespace uds
{

/** represents a stream to a child process
	\author Julian v. Bock */
class ProcStream : public fstream
{
public:
	/** default constructor */
	ProcStream();
	
	/** \param cmd command to be executed
		\param t if this flag is set to true, the child process gets a
			SIGTERM, if it didn't exit and the stream is closed.
		\param env environment */
	explicit
	ProcStream( const char* cmd, bool t = true, const char** env = 0 );
	
	/** destructor */
	virtual
	~ProcStream();
	
	/** forks a new process, executes a program and establishes a two-way
		communications channel.
		\param cmd command to be executed
		\param t if this flag is set to true, the child process gets a
			SIGTERM, if it didn't exit and the stream is closed.
		\param env environment */
	virtual void
	Open( const char* cmd, bool t = true, const char** env = 0 );
	
	/** waits for the child process to terminate */
	virtual void
	Close();
	
private:
	/// socket to child process
	int		sock;
	/// pid of child process
	pid_t	cpid;
	/// kill flag
	bool	term;
};

}

#endif // _uds_procstream_hh
