1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/* -*- c++ -*-
*
* (C) Copyright Pigeon Point Systems. 2011
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTConsoleLITY or FITNESS FOR A PARTICULAR PURPOSE. This
* file and program are licensed under a BSD style license. See
* the Copying file included with the OpenHPI distribution for
* full licensing terms.
*
* Author(s):
* Anton Pak <anton.pak@pigeonpoint.com>
*/
#ifndef CONSOLE_H_FB2B5DD5_4E7D_49F5_9397_C2FEC21B4010
#define CONSOLE_H_FB2B5DD5_4E7D_49F5_9397_C2FEC21B4010
#include <stddef.h>
#include <list>
#include <string>
#include <vector>
#include "object.h"
#include "server.h"
namespace TA {
/**************************************************************
* class cConsoleCmd
*************************************************************/
class cConsole;
struct cConsoleCmd
{
typedef std::vector<std::string> Args;
typedef void ( cConsole::*CmdHandler)( const Args& args );
explicit cConsoleCmd( const std::string& _name,
const std::string& _usage,
const std::string& _info,
CmdHandler _cmd_handler,
size_t _nargs )
: name( _name ),
usage( _usage ),
info( _info ),
cmd_handler( _cmd_handler ),
nargs( _nargs )
{
// empty
}
std::string name;
std::string usage;
std::string info;
CmdHandler cmd_handler;
size_t nargs;
};
/**************************************************************
* class cConsole
*************************************************************/
class cHandler;
typedef std::list<std::string> ObjectPath;
class cConsole : private cServer
{
public:
explicit cConsole( cHandler& handler, uint16_t port, cObject& root );
virtual ~cConsole();
bool Init();
void Send( const char * data, size_t len ) const;
void Send( const char * str ) const;
void Send( const std::string& str ) const;
private:
cConsole( const cConsole& );
cConsole& operator =( const cConsole& );
private: // cServer virtual functions
virtual void WelcomeUser() const;
virtual void ProcessUserLine( const std::vector<char>& line,
bool& quit );
private: // Console commands
void CmdHelp( const cConsoleCmd::Args& args );
void CmdQuit( const cConsoleCmd::Args& args );
void CmdLs( const cConsoleCmd::Args& args );
void CmdCd( const cConsoleCmd::Args& args );
void CmdNew( const cConsoleCmd::Args& args );
void CmdRm( const cConsoleCmd::Args& args );
void CmdSet( const cConsoleCmd::Args& args );
private:
void SendOK( const std::string& msg );
void SendERR( const std::string& msg );
void SendCurrentPath() const;
cObject * GetObject( const ObjectPath& path ) const;
cObject * TestAndGetCurrentObject();
void MakeNewPath( ObjectPath& path, const std::string& path_str ) const;
private: // data
cHandler& m_handler;
std::vector<cConsoleCmd > m_cmds;
bool m_quit;
ObjectPath m_path;
cObject& m_root;
};
}; // namespace TA
#endif // CONSOLE_H_FB2B5DD5_4E7D_49F5_9397_C2FEC21B4010
|