Jpp  debug
the software that should make you happy
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
JSYSTEM::JProcess Class Reference

Streaming of input and output from Linux command. More...

#include <JProcess.hh>

Inheritance diagram for JSYSTEM::JProcess:
JSYSTEM::JShell

Public Member Functions

 JProcess ()
 Default constructor. More...
 
 JProcess (const std::string &command)
 Constructor. More...
 
 ~JProcess ()
 Destructor. More...
 
JFileInputStreamBuffergetInputStreamBuffer (const std::size_t size=65536)
 Get pointer to input stream buffer connected to the normal output stream. More...
 
JFileInputStreamBuffergetErrorStreamBuffer (const std::size_t size=65536)
 Get pointer to input stream buffer connected to the error output stream. More...
 
JFileOutputStreamBuffergetOutputStreamBuffer (const std::size_t size=65536)
 Get pointer to output stream buffer connected to the normal input stream. More...
 

Protected Member Functions

void execute (const char *path, const char *arg0, const char *arg1=(char *) NULL, const char *arg2=(char *) NULL, const char *arg3=(char *) NULL, const char *arg4=(char *) NULL, const char *arg5=(char *) NULL, const char *arg6=(char *) NULL, const char *arg7=(char *) NULL, const char *arg8=(char *) NULL, const char *arg9=(char *) NULL)
 Execute command. More...
 

Protected Attributes

int out
 
int err
 
int in
 

Private Member Functions

 JProcess (const JProcess &)
 
 JProcess (JProcess &&)
 
JProcessoperator= (const JProcess &)
 
JProcessoperator= (JProcess &&)
 

Private Attributes

JFileInputStreamBuffergOut
 
JFileInputStreamBuffergErr
 
JFileOutputStreamBuffergIn
 

Detailed Description

Streaming of input and output from Linux command.

Definition at line 29 of file JProcess.hh.

Constructor & Destructor Documentation

◆ JProcess() [1/4]

JSYSTEM::JProcess::JProcess ( )
inline

Default constructor.

This constructor starts an interactive shell.

Definition at line 36 of file JProcess.hh.

36  :
37  out (-1),
38  err (-1),
39  in (-1),
40  gOut(NULL),
41  gErr(NULL),
42  gIn (NULL)
43  {
44  execute("/bin/tcsh", "tcsh", "-i", "-f");
45  }
JFileOutputStreamBuffer * gIn
Definition: JProcess.hh:209
void execute(const char *path, const char *arg0, const char *arg1=(char *) NULL, const char *arg2=(char *) NULL, const char *arg3=(char *) NULL, const char *arg4=(char *) NULL, const char *arg5=(char *) NULL, const char *arg6=(char *) NULL, const char *arg7=(char *) NULL, const char *arg8=(char *) NULL, const char *arg9=(char *) NULL)
Execute command.
Definition: JProcess.hh:143
JFileInputStreamBuffer * gErr
Definition: JProcess.hh:208
JFileInputStreamBuffer * gOut
Definition: JProcess.hh:207

◆ JProcess() [2/4]

JSYSTEM::JProcess::JProcess ( const std::string &  command)
inline

Constructor.

Parameters
commandLinux command

Definition at line 53 of file JProcess.hh.

53  :
54  out (-1),
55  err (-1),
56  in (-1),
57  gOut(NULL),
58  gErr(NULL),
59  gIn (NULL)
60  {
61  execute("/bin/tcsh", "tcsh", "-f", "-c", command.c_str());
62  }

◆ ~JProcess()

JSYSTEM::JProcess::~JProcess ( )
inline

Destructor.

Definition at line 68 of file JProcess.hh.

69  {
70  close(err);
71  close(out);
72  close(in);
73 
74  if (gOut != NULL) { delete gOut; }
75  if (gErr != NULL) { delete gErr; }
76  if (gIn != NULL) { delete gIn; }
77  }
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:391

◆ JProcess() [3/4]

JSYSTEM::JProcess::JProcess ( const JProcess )
private

◆ JProcess() [4/4]

JSYSTEM::JProcess::JProcess ( JProcess &&  )
private

Member Function Documentation

◆ getInputStreamBuffer()

JFileInputStreamBuffer* JSYSTEM::JProcess::getInputStreamBuffer ( const std::size_t  size = 65536)
inline

Get pointer to input stream buffer connected to the normal output stream.

Parameters
sizesize of internal buffers
Returns
pointer input stream buffer

Definition at line 86 of file JProcess.hh.

87  {
88  if (gOut == NULL) {
89  gOut = new JFileInputStreamBuffer(out, size);
90  }
91 
92  return gOut;
93  }

◆ getErrorStreamBuffer()

JFileInputStreamBuffer* JSYSTEM::JProcess::getErrorStreamBuffer ( const std::size_t  size = 65536)
inline

Get pointer to input stream buffer connected to the error output stream.

Parameters
sizesize of internal buffers
Returns
pointer to input stream buffer

Definition at line 102 of file JProcess.hh.

103  {
104  if (gErr == NULL) {
105  gErr = new JFileInputStreamBuffer(err, size);
106  }
107 
108  return gErr;
109  }

◆ getOutputStreamBuffer()

JFileOutputStreamBuffer* JSYSTEM::JProcess::getOutputStreamBuffer ( const std::size_t  size = 65536)
inline

Get pointer to output stream buffer connected to the normal input stream.

Parameters
sizesize of internal buffers
Returns
pointer to output stream buffer

Definition at line 118 of file JProcess.hh.

119  {
120  if (gIn == NULL) {
121  gIn = new JFileOutputStreamBuffer(in, size);
122  }
123 
124  return gIn;
125  }

◆ execute()

void JSYSTEM::JProcess::execute ( const char *  path,
const char *  arg0,
const char *  arg1 = (char*) NULL,
const char *  arg2 = (char*) NULL,
const char *  arg3 = (char*) NULL,
const char *  arg4 = (char*) NULL,
const char *  arg5 = (char*) NULL,
const char *  arg6 = (char*) NULL,
const char *  arg7 = (char*) NULL,
const char *  arg8 = (char*) NULL,
const char *  arg9 = (char*) NULL 
)
inlineprotected

Execute command.

Parameters
pathpath
arg0comma separted argument list
arg1comma separted argument list
arg2comma separted argument list
arg3comma separted argument list
arg4comma separted argument list
arg5comma separted argument list
arg6comma separted argument list
arg7comma separted argument list
arg8comma separted argument list
arg9comma separted argument list

Definition at line 143 of file JProcess.hh.

154  {
155  int pipe_stdin [2];
156  int pipe_stdout[2];
157  int pipe_stderr[2];
158 
159  if (pipe(pipe_stdin)) { THROW(JPipeOpenException, "Error at pipe of standard input."); }
160  if (pipe(pipe_stdout)) { THROW(JPipeOpenException, "Error at pipe of standard output."); }
161  if (pipe(pipe_stderr)) { THROW(JPipeOpenException, "Error at pipe of standard error."); }
162 
163  const pid_t pid = fork();
164 
165  if (pid < 0)
166 
167  // error
168 
169  THROW(JForkException, "JProcess::fork() failed");
170 
171  else if (pid == 0) {
172 
173  // child
174 
175  close(pipe_stdin [1]);
176  if (dup2 (pipe_stdin [0], STDIN_FILENO) == -1) { JSystemException("dup2-stdin"); }
177 
178  close(pipe_stdout[0]);
179  if (dup2 (pipe_stdout[1], STDOUT_FILENO) == -1) { JSystemException("dup2-stdout"); }
180 
181  close(pipe_stdout[0]);
182  if (dup2 (pipe_stdout[1], STDERR_FILENO) == -1) { JSystemException("dup2-stderr"); }
183 
184  execl(path, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, (char*) NULL);
185 
186  THROW(JSystemException, path);
187 
188  } else {
189 
190  // parent
191 
192  in = pipe_stdin [1];
193  out = pipe_stdout[0];
194  err = pipe_stderr[0];
195 
196  close(pipe_stdin [0]);
197  close(pipe_stdout[1]);
198  close(pipe_stderr[1]);
199  }
200  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712

◆ operator=() [1/2]

JProcess& JSYSTEM::JProcess::operator= ( const JProcess )
private

◆ operator=() [2/2]

JProcess& JSYSTEM::JProcess::operator= ( JProcess &&  )
private

Member Data Documentation

◆ out

int JSYSTEM::JProcess::out
protected

Definition at line 202 of file JProcess.hh.

◆ err

int JSYSTEM::JProcess::err
protected

Definition at line 203 of file JProcess.hh.

◆ in

int JSYSTEM::JProcess::in
protected

Definition at line 204 of file JProcess.hh.

◆ gOut

JFileInputStreamBuffer* JSYSTEM::JProcess::gOut
private

Definition at line 207 of file JProcess.hh.

◆ gErr

JFileInputStreamBuffer* JSYSTEM::JProcess::gErr
private

Definition at line 208 of file JProcess.hh.

◆ gIn

JFileOutputStreamBuffer* JSYSTEM::JProcess::gIn
private

Definition at line 209 of file JProcess.hh.


The documentation for this class was generated from the following file: