54using std::ostringstream;
59#ifdef HAVE_LIBREADLINE
60# if defined(HAVE_READLINE_READLINE_H)
61# include <readline/readline.h>
62# elif defined(HAVE_READLINE_H)
66 char *readline(
const char *);
74#ifdef HAVE_READLINE_HISTORY
75# if defined(HAVE_READLINE_HISTORY_H)
76# include <readline/history.h>
77# elif defined(HAVE_HISTORY_H)
81 int add_history(
const char *);
82 int write_history(
const char *);
83 int read_history(
const char *);
90#define SIZE_COMMUNICATION_BUFFER (4096*4096)
92#include "BESXMLInterface.h"
93#include "BESStopWatch.h"
97#include "StandAloneClient.h"
98#include "CmdTranslation.h"
100StandAloneClient::~StandAloneClient()
102 if (_strmCreated && _strm) {
130 if (_strmCreated && _strm) {
138 _strmCreated = created;
154 string suppress =
"suppress";
155 if (cmd.compare(0, suppress.length(), suppress) == 0) {
160 string output =
"output to";
161 if (cmd.compare(0, output.length(), output) == 0) {
162 string subcmd = cmd.substr(output.length() + 1);
163 string screen =
"screen";
164 if (subcmd.compare(0, screen.length(), screen) == 0) {
169 string file = subcmd.substr(0, subcmd.length() - 1);
170 auto *fstrm =
new ofstream(file.c_str(), ios::app);
173 cerr <<
"Unable to set client output to file " << file << endl;
183 string load =
"load";
184 if (cmd.compare(0, load.length(), load) == 0) {
185 string file = cmd.substr(load.length() + 1, cmd.length() - load.length() - 2);
186 ifstream fstrm(file.c_str());
188 cerr <<
"Unable to load commands from file " << file <<
": file does not exist or failed to open file"
198 cerr <<
"Improper client command " << cmd << endl;
213void StandAloneClient::executeCommand(
const string & cmd,
int repeat)
215 string client =
"client";
216 if (cmd.compare(0, client.length(), client) == 0) {
220 if (repeat < 1) repeat = 1;
221 for (
int i = 0; i < repeat; i++) {
222 ostringstream *show_stream =
nullptr;
223 if (CmdTranslation::is_show()) {
224 show_stream =
new ostringstream;
227 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand sending: " << cmd << endl );
247 status = interface->finish(status);
250 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - executed successfully" << endl);
254 BESDEBUG(
"standalone",
"StandAloneClient::executeCommand - error occurred" << endl);
256 case BES_INTERNAL_FATAL_ERROR: {
257 cerr <<
"Status not OK, dispatcher returned value " << status << endl;
261 case BES_INTERNAL_ERROR:
262 case BES_SYNTAX_USER_ERROR:
263 case BES_FORBIDDEN_ERROR:
264 case BES_NOT_FOUND_ERROR:
274 *(_strm) << show_stream->str() << endl;
276 show_stream =
nullptr;
302 _isInteractive =
true;
303 if (repeat < 1) repeat = 1;
305 CmdTranslation::set_show(
false);
307 string doc = CmdTranslation::translate(cmd_list);
309 executeCommand(doc, repeat);
313 CmdTranslation::set_show(
false);
314 _isInteractive =
false;
317 CmdTranslation::set_show(
false);
318 _isInteractive =
false;
341 _isInteractive =
false;
342 if (repeat < 1) repeat = 1;
343 for (
int i = 0; i < repeat; i++) {
345 istrm.seekg(0, ios::beg);
348 while (getline(istrm, line)) {
351 this->executeCommand(cmd, 1);
372 _isInteractive =
true;
374 cout << endl << endl <<
"Type 'exit' to exit the command line client and 'help' or '?' "
375 <<
"to display the help screen" << endl << endl;
380 size_t len = this->readLine(message);
381 if ( message ==
"exit" || message ==
"exit;") {
384 else if (message ==
"help" || message ==
"help;" || message ==
"?") {
387 else if (message.length() > 6 && message.substr(0, 6) ==
"client") {
388 this->executeCommand(message, 1);
390 else if (len != 0 && !message.empty()) {
391 CmdTranslation::set_show(
false);
393 string doc = CmdTranslation::translate(message);
395 this->executeCommand(doc, 1);
399 CmdTranslation::set_show(
false);
400 _isInteractive =
false;
403 CmdTranslation::set_show(
false);
406 _isInteractive =
false;
414size_t StandAloneClient::readLine(
string & msg)
417 char *buf = (
char *)
nullptr;
418 buf = ::readline(
"BESClient> ");
421#ifdef HAVE_READLINE_HISTORY
424 if (len > SIZE_COMMUNICATION_BUFFER) {
425 cerr << __FILE__ << __LINE__ <<
426 ": incoming data buffer exceeds maximum capacity with lenght " << len << endl;
445 buf = (
char *)
nullptr;
452void StandAloneClient::displayHelp()
456 cout <<
"BES Command Line Client Help" << endl;
458 cout <<
"Client commands available:" << endl;
459 cout <<
" exit - exit the command line interface" << endl;
460 cout <<
" help - display this help screen" << endl;
461 cout <<
" client suppress; - suppress output from the server" << endl;
462 cout <<
" client output to screen; - display server output to the screen" << endl;
463 cout <<
" client output to <file>; - display server output to specified file" << endl;
465 cout <<
"Any commands beginning with 'client' must end with a semicolon" << endl;
467 cout <<
"To display the list of commands available from the server " <<
"please type the command 'show help;'"
481 strm << BESIndent::LMarg <<
"StandAloneClient::dump - (" << (
void *)
this <<
")" << endl;
483 strm << BESIndent::LMarg <<
"stream: " << (
void *) _strm << endl;
484 strm << BESIndent::LMarg <<
"stream created? " << _strmCreated << endl;
485 BESIndent::UnIndent();
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Base exception class for the BES with basic string message.
virtual int execute_request(const std::string &from)
The entry point for command execution; called by BESServerHandler::execute()
virtual bool start(std::string name)
Entry point into BES using xml document requests.
void executeCommands(const std::string &cmd_list, int repeat)
Send the command(s) specified to the BES server after wrapping in request document.
void interact()
An interactive BES client that takes BES requests on the command line.
void executeClientCommand(const std::string &cmd)
Executes a client side command.
virtual void dump(std::ostream &strm) const
dumps information about this object
void setOutput(std::ostream *strm, bool created)
Set the output stream for responses from the BES server.