Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
JSYSTEM::JShell Class Reference

The JShell clas can be used to interact with the shell via I/O streams. More...

#include <JShell.hh>

Inheritance diagram for JSYSTEM::JShell:
JSYSTEM::JProcess JLANG::JFileStream JLANG::JAbstractObjectStatus JLANG::JFileStreamBuffer JLANG::JFileInputStreamBuffer JLANG::JFileOutputStreamBuffer std::vector< char > std::vector< char >

Public Member Functions

 JShell (const std::size_t size=65536)
 Constructor. More...
 
 ~JShell ()
 Destructor. More...
 
const std::string & getPrompt () const
 Get prompt. More...
 
template<class T >
bool get (T &value)
 Get value. More...
 
bool getline (std::string &buffer, const char eol= '\n')
 Get line of text. More...
 
JShellflush (std::ostream &out=null)
 Extracts characters from this shell and flush them to the given output stream until the prompt is read. More...
 
virtual bool getStatus () const override
 Get status of this shell. More...
 
void exit ()
 Exit this shell. 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...
 
 operator bool () const
 Type conversion operator. More...
 
bool operator! () const
 Negated status of this object. More...
 

Static Public Member Functions

static JShellgetInstance ()
 Get reference to unique instance of this class object. More...
 

Protected Types

typedef traits_type::int_type int_type
 
typedef traits_type::int_type int_type
 
typedef std::streamsize streamsize
 

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...
 
virtual int_type underflow () override
 Check underflow. More...
 
bool in_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of input. More...
 
virtual int_type overflow (int_type c) override
 Check overflow. More...
 
virtual int sync () override
 Synchronise buffer. More...
 
bool out_avail (JTimeval timeout=JTimeval::min()) const
 Check availability of output. More...
 
int flush ()
 Flush internal buffer. More...
 

Protected Attributes

std::string prompt
 
int out
 
int err
 
int in
 
JFile in
 
std::size_t memory
 
JFile out
 

Private Member Functions

 JShell (const JShell &)
 
 JShell (JShell &&)
 
JShelloperator= (const JShell &)
 
JShelloperator= (JShell &&)
 

Detailed Description

The JShell clas can be used to interact with the shell via I/O streams.

Definition at line 32 of file JShell.hh.

Member Typedef Documentation

typedef traits_type::int_type JLANG::JFileInputStreamBuffer::int_type
inherited

Definition at line 32 of file JFileStreamBuffer.hh.

typedef traits_type::int_type JLANG::JFileOutputStreamBuffer::int_type
inherited

Definition at line 121 of file JFileStreamBuffer.hh.

typedef std::streamsize JLANG::JFileOutputStreamBuffer::streamsize
inherited

Definition at line 122 of file JFileStreamBuffer.hh.

Constructor & Destructor Documentation

JSYSTEM::JShell::JShell ( const std::size_t  size = 65536)
inline

Constructor.

Parameters
sizesize of internal buffer

Definition at line 47 of file JShell.hh.

47  :
48  JProcess(),
50  {
51  using namespace std;
52 
53  prompt = "_abc321_";
54 
55  static_cast<ostream&>(*this) << "set prompt=" << prompt << endl;
56  static_cast<ostream&>(*this) << "echo hello" << endl;
57 
58  flush();
59  }
int flush()
Flush internal buffer.
JFileStream(const JAbstractFile &in, const JAbstractFile &out, const std::size_t size=65536)
Constructor.
Definition: JFileStream.hh:80
std::string prompt
Definition: JShell.hh:220
JProcess()
Default constructor.
Definition: JProcess.hh:36
JSYSTEM::JShell::~JShell ( )
inline

Destructor.

Definition at line 65 of file JShell.hh.

66  {
67  this->exit();
68  }
void exit()
Exit this shell.
Definition: JShell.hh:211
JSYSTEM::JShell::JShell ( const JShell )
private
JSYSTEM::JShell::JShell ( JShell &&  )
private

Member Function Documentation

static JShell& JSYSTEM::JShell::getInstance ( )
inlinestatic

Get reference to unique instance of this class object.

Returns
reference to this class object

Definition at line 76 of file JShell.hh.

77  {
78  static JShell shell;
79 
80  return shell;
81  }
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:32
const std::string& JSYSTEM::JShell::getPrompt ( ) const
inline

Get prompt.

Returns
prompt

Definition at line 89 of file JShell.hh.

90  {
91  return prompt;
92  }
std::string prompt
Definition: JShell.hh:220
template<class T >
bool JSYSTEM::JShell::get ( T value)
inline

Get value.

Note that

  • in case of success, the input stream is flushed; and
  • in case of failure, the input stream is cleared and flushed.
Parameters
valuevalue
Returns
true if valid; else false

Definition at line 106 of file JShell.hh.

107  {
108  using namespace std;
109 
110  if (*this >> value) {
111 
112  this->flush();
113 
114  return true;
115 
116  } else {
117 
118  static_cast<istream&>(*this).clear();
119 
120  this->flush();
121 
122  return false;
123  }
124  }
int flush()
Flush internal buffer.
bool JSYSTEM::JShell::getline ( std::string &  buffer,
const char  eol = '\n' 
)
inline

Get line of text.

Extracts characters from this shell and stores them into the given buffer until the end of line character is found or the prompt is read.

Parameters
bufferbuffer
eolend of line character
Returns
true if valid; else false

Definition at line 137 of file JShell.hh.

138  {
139  using namespace std;
140 
141  buffer.clear();
142 
143  while (this->getStatus()) {
144 
145  const int c = this->get();
146 
147  if (c == streambuf::traits_type::eof()) {
148 
149  return !buffer.empty();
150 
151  } else if (c == eol) {
152 
153  return true;
154 
155  } else {
156 
157  buffer += (char) c;
158 
159  if (buffer.size() >= prompt.size()) {
160 
161  const size_t pos = buffer.size() - this->prompt.size();
162 
163  if (buffer.substr(pos) == this->prompt) {
164 
165  buffer.erase(pos);
166 
167  return !buffer.empty();
168  }
169  }
170  }
171  }
172 
173  return false;
174  }
virtual bool getStatus() const override
Get status of this shell.
Definition: JShell.hh:201
$WORKDIR ev_configure_dqsimulator txt echo process $DQ_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DQ_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
std::string prompt
Definition: JShell.hh:220
JShell& JSYSTEM::JShell::flush ( std::ostream &  out = null)
inline

Extracts characters from this shell and flush them to the given output stream until the prompt is read.

Parameters
outoutput stream
Returns
this shell

Definition at line 184 of file JShell.hh.

185  {
186  using namespace std;
187 
188  for (string buffer; this->getline(buffer); ) {
189  out << buffer << endl;
190  }
191 
192  return *this;
193  }
bool getline(std::string &buffer, const char eol= '\n')
Get line of text.
Definition: JShell.hh:137
virtual bool JSYSTEM::JShell::getStatus ( ) const
inlineoverridevirtual

Get status of this shell.

Returns
status of this shell

Implements JLANG::JAbstractObjectStatus.

Definition at line 201 of file JShell.hh.

202  {
203  return ((bool) static_cast<const std::ostream&>(*this) &&
204  (bool) static_cast<const std::istream&>(*this));
205  }
void JSYSTEM::JShell::exit ( )
inline

Exit this shell.

Definition at line 211 of file JShell.hh.

212  {
213  using namespace std;
214 
215  static_cast<ostream&>(*this) << "exit" << endl;
216  }
JShell& JSYSTEM::JShell::operator= ( const JShell )
private
JShell& JSYSTEM::JShell::operator= ( JShell &&  )
private
JFileInputStreamBuffer* JSYSTEM::JProcess::getInputStreamBuffer ( const std::size_t  size = 65536)
inlineinherited

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  }
JFileInputStreamBuffer * gOut
Definition: JProcess.hh:207
JFileInputStreamBuffer* JSYSTEM::JProcess::getErrorStreamBuffer ( const std::size_t  size = 65536)
inlineinherited

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  }
JFileInputStreamBuffer * gErr
Definition: JProcess.hh:208
JFileOutputStreamBuffer* JSYSTEM::JProcess::getOutputStreamBuffer ( const std::size_t  size = 65536)
inlineinherited

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  }
JFileOutputStreamBuffer * gIn
Definition: JProcess.hh:209
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 
)
inlineprotectedinherited

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
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:391
$WORKDIR driver txt done cat $WORKDIR driver txt<< EOFprocess ${DATAFILTER}$FILTER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataFilter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAFILTER}-P $PORT</dev/null > &/dev/null &))';process ${DATAWRITER}$WRITER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataWriter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAWRITER}</dev/null > &/dev/null &))';print enterevent ev_init{RC_CMD}event ev_reset{RC_CMD}event ev_init{RC_CMD}event ev_configure{RC_DFLTR%<$WORKDIR/ev_configure_datafilter.txt > RC_DQSIM<$WORKDIR/ev_configure_dqsimulator.txt > RC_DWRT path
virtual int_type JLANG::JFileInputStreamBuffer::underflow ( )
inlineoverridevirtualinherited

Check underflow.

This method reads as many bytes as possible.

Returns
first character if OK; else EOF

Definition at line 60 of file JFileStreamBuffer.hh.

61  {
62  if (gptr() >= egptr()) {
63 
64  char* __begin = this->data();
65 
66  if (eback() == __begin) {
67 
68  // push put back characters
69 
70  memmove(__begin, egptr() - memory, memory);
71 
72  __begin += memory;
73  }
74 
75  const size_t len = in.read(__begin, this->size() - (__begin - this->data()));
76 
77  if (len != 0)
78  setg(this->data(), __begin, __begin + len);
79  else
80  return traits_type::eof();
81  }
82 
83  return *gptr();
84  }
virtual int read(char *buffer, const int length)
Read data from file.
Definition: JFile.hh:80
bool JLANG::JFileInputStreamBuffer::in_avail ( JTimeval  timeout = JTimeval::min()) const
inlineinherited

Check availability of input.

This method returns true if at least one byte can be read without blocking.

Parameters
timeouttimeout
Returns
true if ready to read; else false

Definition at line 94 of file JFileStreamBuffer.hh.

95  {
96  return JFileDescriptorMask(in).in_avail(timeout);
97  }
Auxiliary class for method select.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
virtual int_type JLANG::JFileOutputStreamBuffer::overflow ( int_type  c)
inlineoverridevirtualinherited

Check overflow.

This method writes one byte if possible.

Parameters
ccharacter
Returns
c if OK; else EOF

Definition at line 159 of file JFileStreamBuffer.hh.

160  {
161  if (c != traits_type::eof()) {
162 
163  *pptr() = (char) c;
164 
165  pbump(1);
166 
167  if (flush() == traits_type::eof()) {
168  return traits_type::eof();
169  }
170  }
171 
172  return c;
173  }
int flush()
Flush internal buffer.
$WORKDIR ev_configure_dqsimulator txt echo process $DQ_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DQ_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
virtual int JLANG::JFileOutputStreamBuffer::sync ( )
inlineoverridevirtualinherited

Synchronise buffer.

Definition at line 179 of file JFileStreamBuffer.hh.

180  {
181  if (flush() == traits_type::eof())
182  return -1;
183  else
184  return 0;
185  }
int flush()
Flush internal buffer.
bool JLANG::JFileOutputStreamBuffer::out_avail ( JTimeval  timeout = JTimeval::min()) const
inlineinherited

Check availability of output.

This method returns true if at least one byte can be written without blocking.

Parameters
timeouttimeout
Returns
true if ready to write; else false

Definition at line 195 of file JFileStreamBuffer.hh.

196  {
197  return JFileDescriptorMask(out).out_avail(timeout);
198  }
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
Auxiliary class for method select.
int JLANG::JFileOutputStreamBuffer::flush ( )
inlineprotectedinherited

Flush internal buffer.

Definition at line 204 of file JFileStreamBuffer.hh.

205  {
206  const int len = pptr() - pbase();
207 
208  if (len != 0) {
209 
210  if (out.write(this->data(), len) != len) {
211  return traits_type::eof();
212  }
213 
214  pbump(-len);
215  }
216 
217  return len;
218  }
then usage $script< detector file >< detectorfile > nIf the range of floors is the first detector file is aligned to the second before the comparison nIn this
virtual int write(const char *buffer, const int length)
Write data to file.
Definition: JFile.hh:93
JLANG::JAbstractObjectStatus::operator bool ( ) const
inlineinherited

Type conversion operator.

Returns
status of this object

Definition at line 33 of file JAbstractObjectStatus.hh.

34  {
35  return this->getStatus();
36  }
virtual bool getStatus() const =0
Get status of object.
bool JLANG::JAbstractObjectStatus::operator! ( ) const
inlineinherited

Negated status of this object.

Returns
negated status of this object

Definition at line 44 of file JAbstractObjectStatus.hh.

45  {
46  return !(this->getStatus());
47  }
virtual bool getStatus() const =0
Get status of object.

Member Data Documentation

std::string JSYSTEM::JShell::prompt
protected

Definition at line 220 of file JShell.hh.

int JSYSTEM::JProcess::out
protectedinherited

Definition at line 202 of file JProcess.hh.

int JSYSTEM::JProcess::err
protectedinherited

Definition at line 203 of file JProcess.hh.

int JSYSTEM::JProcess::in
protectedinherited

Definition at line 204 of file JProcess.hh.

JFile JLANG::JFileInputStreamBuffer::in
protectedinherited

Definition at line 100 of file JFileStreamBuffer.hh.

std::size_t JLANG::JFileInputStreamBuffer::memory
protectedinherited

Definition at line 101 of file JFileStreamBuffer.hh.

JFile JLANG::JFileOutputStreamBuffer::out
protectedinherited

Definition at line 221 of file JFileStreamBuffer.hh.


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