Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
 ~JShell ()
 Destructor.
 
const std::string & getPrompt () const
 Get prompt.
 
template<class T >
bool get (T &value)
 Get value.
 
bool getline (std::string &buffer, const char eol='\n')
 Get line of text.
 
JShellflush (std::ostream &out=null)
 Extracts characters from this shell and flush them to the given output stream until the prompt is read.
 
virtual bool getStatus () const override
 Get status of this shell.
 
void exit ()
 Exit this shell.
 
JFileInputStreamBuffergetInputStreamBuffer (const std::size_t size=65536)
 Get pointer to input stream buffer connected to the normal output stream.
 
JFileInputStreamBuffergetErrorStreamBuffer (const std::size_t size=65536)
 Get pointer to input stream buffer connected to the error output stream.
 
JFileOutputStreamBuffergetOutputStreamBuffer (const std::size_t size=65536)
 Get pointer to output stream buffer connected to the normal input stream.
 
 operator bool () const
 Type conversion operator.
 
bool operator! () const
 Negated status of this object.
 

Static Public Member Functions

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

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

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 &&)
 

Private Attributes

JFileInputStreamBuffergOut
 
JFileInputStreamBuffergErr
 
JFileOutputStreamBuffergIn
 

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

◆ int_type [1/2]

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

Definition at line 32 of file JFileStreamBuffer.hh.

◆ int_type [2/2]

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

Definition at line 121 of file JFileStreamBuffer.hh.

◆ streamsize

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

Definition at line 122 of file JFileStreamBuffer.hh.

Constructor & Destructor Documentation

◆ JShell() [1/3]

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.
JProcess()
Default constructor.
Definition JProcess.hh:36
std::string prompt
Definition JShell.hh:220

◆ ~JShell()

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

◆ JShell() [2/3]

JSYSTEM::JShell::JShell ( const JShell & )
private

◆ JShell() [3/3]

JSYSTEM::JShell::JShell ( JShell && )
private

Member Function Documentation

◆ getInstance()

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 }
JShell(const std::size_t size=65536)
Constructor.
Definition JShell.hh:47

◆ getPrompt()

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 }

◆ get()

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 }

◆ getline()

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 }
bool get(T &value)
Get value.
Definition JShell.hh:106
virtual bool getStatus() const override
Get status of this shell.
Definition JShell.hh:201

◆ flush() [1/2]

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

◆ getStatus()

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 }

◆ exit()

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 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ getInputStreamBuffer()

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

◆ getErrorStreamBuffer()

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

◆ getOutputStreamBuffer()

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

◆ 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 )
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.
void close(std::istream *pf)
Close file.

◆ underflow()

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

◆ in_avail()

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 }

◆ overflow()

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 }

◆ sync()

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 }

◆ out_avail()

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 }

◆ flush() [2/2]

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 }
virtual int write(const char *buffer, const int length)
Write data to file.
Definition JFile.hh:93

◆ operator bool()

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.

◆ operator!()

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 }

Member Data Documentation

◆ prompt

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

Definition at line 220 of file JShell.hh.

◆ out [1/2]

int JSYSTEM::JProcess::out
protectedinherited

Definition at line 202 of file JProcess.hh.

◆ err

int JSYSTEM::JProcess::err
protectedinherited

Definition at line 203 of file JProcess.hh.

◆ in [1/2]

int JSYSTEM::JProcess::in
protectedinherited

Definition at line 204 of file JProcess.hh.

◆ gOut

JFileInputStreamBuffer* JSYSTEM::JProcess::gOut
privateinherited

Definition at line 207 of file JProcess.hh.

◆ gErr

JFileInputStreamBuffer* JSYSTEM::JProcess::gErr
privateinherited

Definition at line 208 of file JProcess.hh.

◆ gIn

JFileOutputStreamBuffer* JSYSTEM::JProcess::gIn
privateinherited

Definition at line 209 of file JProcess.hh.

◆ in [2/2]

JFile JLANG::JFileInputStreamBuffer::in
protectedinherited

Definition at line 100 of file JFileStreamBuffer.hh.

◆ memory

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

Definition at line 101 of file JFileStreamBuffer.hh.

◆ out [2/2]

JFile JLANG::JFileOutputStreamBuffer::out
protectedinherited

Definition at line 221 of file JFileStreamBuffer.hh.


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