pipe.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2000-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Fri Jan 14 17:33:24 2000
00011 // commit: $Id: pipe.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/rutz/pipe.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED
00036 #define GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED
00037 
00038 #include "rutz/stdiobuf.h"
00039 
00040 #include <cstdio>
00041 #include <istream>
00042 #include <sys/types.h>
00043 #include <sys/wait.h>
00044 #include <unistd.h>
00045 
00046 namespace rutz
00047 {
00048   class shell_pipe;
00049   class pipe_fds;
00050   class child_process;
00051   class exec_pipe;
00052   class bidir_pipe;
00053 }
00054 
00056 class rutz::shell_pipe
00057 {
00058 public:
00059   shell_pipe(const char* command, const char* mode);
00060 
00061   ~shell_pipe();
00062 
00063   std::iostream& stream() { return m_stream; }
00064 
00065   int close();
00066 
00067   bool is_closed() const { return (m_file == 0) || !m_stream.is_open(); }
00068 
00069   int exit_status() const { return m_exit_status; }
00070 
00071 private:
00072   shell_pipe(const shell_pipe&);
00073   shell_pipe& operator=(const shell_pipe&);
00074 
00075   FILE*             m_file;
00076   rutz::stdiostream m_stream;
00077   int               m_exit_status;
00078 };
00079 
00081 class rutz::pipe_fds
00082 {
00083 public:
00085 
00086   pipe_fds();
00087 
00089   ~pipe_fds() throw();
00090 
00091   int reader() const throw() { return m_fds[0]; }
00092   int writer() const throw() { return m_fds[1]; }
00093 
00094   void close_reader() throw() { if (m_fds[0] >= 0) close(m_fds[0]); m_fds[0] = -1; }
00095   void close_writer() throw() { if (m_fds[1] >= 0) close(m_fds[1]); m_fds[1] = -1; }
00096 
00097 private:
00098   pipe_fds(const pipe_fds&);
00099   pipe_fds& operator=(const pipe_fds&);
00100 
00101   int m_fds[2]; // reading == m_fds[0], writing == m_fds[1]
00102 };
00103 
00105 class rutz::child_process
00106 {
00107 public:
00109 
00110   child_process();
00111 
00113   ~child_process() throw();
00114 
00116   bool in_parent() const throw() { return m_pid != 0; }
00117 
00119   int wait() throw();
00120 
00121 private:
00122   child_process(const child_process&);
00123   child_process& operator=(const child_process&);
00124 
00125   int m_child_status;
00126   pid_t m_pid;
00127 };
00128 
00129 
00131 class rutz::exec_pipe
00132 {
00133 private:
00134   void init(char* const* argv);
00135 
00136 public:
00138 
00141   exec_pipe(const char* m, char* const* argv);
00142 
00144 
00147   exec_pipe(const char* m, const char* argv0, ...);
00148 
00150   ~exec_pipe() throw();
00151 
00153   std::iostream& stream() throw();
00154 
00156   void close();
00157 
00159   int exit_status() throw();
00160 
00161 private:
00162   bool               m_parent_is_reader;
00163   pipe_fds           m_fds;
00164   child_process      m_child;
00165   rutz::stdiostream* m_stream;
00166 };
00167 
00168 
00170 class rutz::bidir_pipe
00171 {
00172 public:
00174   bidir_pipe();
00175 
00177 
00178   bidir_pipe(char* const* argv);
00179 
00181 
00187   bidir_pipe(const char* argv0, ...);
00188 
00190   ~bidir_pipe() throw();
00191 
00193 
00210   void block_child_sigint();
00211 
00213 
00214   void init(char* const* argv);
00215 
00217 
00218   void init(const char* argv0, ...);
00219 
00221   std::iostream& in_stream() throw();
00222 
00224   std::iostream& out_stream() throw();
00225 
00227   void close_in();
00228 
00230   void close_out();
00231 
00233   int exit_status() throw();
00234 
00235 private:
00236   bidir_pipe(const bidir_pipe&); // not implemented
00237   bidir_pipe& operator=(const bidir_pipe&); // not implemented
00238 
00239   pipe_fds           m_in_pipe;
00240   pipe_fds           m_out_pipe;
00241   child_process      m_child;
00242   rutz::stdiostream* m_in_stream;
00243   rutz::stdiostream* m_out_stream;
00244   bool               m_block_child_sigint;
00245 };
00246 
00247 static const char __attribute__((used)) vcid_groovx_rutz_pipe_h_utc20050626084019[] = "$Id: pipe.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00248 #endif // !GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.