CommandInterpreter Class Reference

Back to the index.

Public Member Functions | Static Public Member Functions | List of all members
CommandInterpreter Class Reference

An interactive command interpreter, which run Commands. More...

#include <CommandInterpreter.h>

Inheritance diagram for CommandInterpreter:
UnitTestable

Public Member Functions

 CommandInterpreter (GXemul *owner)
 Constructs a CommandInterpreter. More...
 
bool AddKey (stringchar key)
 Adds a character (keypress) to the current command buffer. More...
 
void ClearCurrentCommandBuffer ()
 Clears the current command buffer. More...
 
void ReshowCurrentCommandBuffer ()
 Re-displays the current command buffer. More...
 
bool RunCommand (const string &command, bool *pSuccess=NULL)
 Runs a command, given as a string. More...
 
const string & GetCurrentCommandBuffer () const
 Retrieves the current command buffer. More...
 
void AddCommand (refcount_ptr< Command > command)
 Adds a new Command to the command interpreter. More...
 
const CommandsGetCommands () const
 Gets a collection of all commands. More...
 
int AddLineToCommandHistory (const string &command)
 Adds a command line to the command history. More...
 
string GetHistoryLine (int nStepsBack) const
 Retrieves a line from the command history. More...
 

Static Public Member Functions

static void RunUnitTests (int &nSucceeded, int &nFailures)
 
- Static Public Member Functions inherited from UnitTestable
static void RunUnitTests (int &nSucceeded, int &nFailures)
 Runs unit test cases. More...
 

Detailed Description

An interactive command interpreter, which run Commands.

A command interpreter can execute commands in the form of complete strings, or it can be given one character (keypress) at a time to build up a command. When given individual keypresses, the command interpreter echoes back what is to be printed. It also supports TAB completion.

Definition at line 48 of file CommandInterpreter.h.

Constructor & Destructor Documentation

◆ CommandInterpreter()

CommandInterpreter::CommandInterpreter ( GXemul owner)

Constructs a CommandInterpreter.

Parameters
ownerthe GXemul instance that owns the CommandInterpreter

Definition at line 38 of file CommandInterpreter.cc.

Member Function Documentation

◆ AddCommand()

void CommandInterpreter::AddCommand ( refcount_ptr< Command command)

Adds a new Command to the command interpreter.

Parameters
commandA reference counter pointer to the Command.

Definition at line 57 of file CommandInterpreter.cc.

References Command::GetCommandName().

Referenced by GetCurrentCommandBuffer().

◆ AddKey()

bool CommandInterpreter::AddKey ( stringchar  key)

Adds a character (keypress) to the current command buffer.

Most normal keys are added at the end of the buffer. Some exceptions are:

  • nul char: does not change the input line buffer, but forces it to be visually redrawn/updated
  • backspace: removes the last character (if any)
  • tab: attempts TAB completion of the last word
  • newline or cr: calls RunCommand, and then clears the current buffer
  • escape: special handling
Parameters
keythe character/key to add
Returns
true if this was a complete command line (i.e. the key was a newline), false otherwise

Definition at line 381 of file CommandInterpreter.cc.

References AddLineToCommandHistory(), ClearCurrentCommandBuffer(), GetHistoryLine(), GXemul::GetUI(), UI::InputLineDone(), ReshowCurrentCommandBuffer(), RunCommand(), and UI::ShowDebugMessage().

Referenced by GetCurrentCommandBuffer().

◆ AddLineToCommandHistory()

int CommandInterpreter::AddLineToCommandHistory ( const string &  command)

Adds a command line to the command history.

If the command is empty, or the same as the last command in the command history, it is ignored.

The command history buffer only holds a small fixed number of entries.

Parameters
commandThe command line to add to the command history.
Returns
The next insert position in the command history. Useful for unit testing purposes; should otherwise be ignored.

Definition at line 69 of file CommandInterpreter.cc.

Referenced by AddKey(), and GetCurrentCommandBuffer().

◆ ClearCurrentCommandBuffer()

void CommandInterpreter::ClearCurrentCommandBuffer ( )

Clears the current command buffer.

Definition at line 654 of file CommandInterpreter.cc.

Referenced by AddKey(), and ConsoleUI_SIGINT_Handler().

◆ GetCommands()

const Commands & CommandInterpreter::GetCommands ( ) const

Gets a collection of all commands.

Returns
A const reference to the collection of commands.

Definition at line 63 of file CommandInterpreter.cc.

Referenced by HelpCommand::Execute(), and GetCurrentCommandBuffer().

◆ GetCurrentCommandBuffer()

const string & CommandInterpreter::GetCurrentCommandBuffer ( ) const

Retrieves the current command buffer.

Returns
a string representing the current command buffer

Definition at line 1013 of file CommandInterpreter.cc.

References AddCommand(), AddKey(), AddLineToCommandHistory(), UnitTest::Assert(), GXemul::GetCommandInterpreter(), GetCommands(), GetCurrentCommandBuffer(), GetHistoryLine(), RunCommand(), UNITTEST, and UNITTESTS.

Referenced by GetCurrentCommandBuffer().

◆ GetHistoryLine()

string CommandInterpreter::GetHistoryLine ( int  nStepsBack) const

Retrieves a line from the command history.

Parameters
nStepsBackThe number of steps back into the history. 0 means return an empty line, 1 means the last history line, 2 means the second last, and so on.
Returns
The line from the history, or an empty string if nStepsBack was zero.

Definition at line 88 of file CommandInterpreter.cc.

References Component::GetMethodNames(), GXemul::GetRootComponent(), GXemul::GetUI(), Component::GetVariableNames(), refcount_ptr< T >::IsNULL(), and UI::ShowDebugMessage().

Referenced by AddKey(), and GetCurrentCommandBuffer().

◆ ReshowCurrentCommandBuffer()

void CommandInterpreter::ReshowCurrentCommandBuffer ( )

Re-displays the current command buffer.

Useful e.g. when running in text console mode, and the user has pressed CTRL-Z. When returning to GXemul, the current command buffer can be showed again by calling this function.

Definition at line 637 of file CommandInterpreter.cc.

References GXemul::GetUI(), and UI::RedisplayInputLine().

Referenced by AddKey().

◆ RunCommand()

bool CommandInterpreter::RunCommand ( const string &  command,
bool *  pSuccess = NULL 
)

Runs a command, given as a string.

The return value from the function is true if the command was run. However, the command may run and fail, and that's what pSuccess is for.

Parameters
commandthe command to run
pSuccessa pointer to a bool, which (if pSuccess is non-NULL) will be set to whether the command succeeded or not.
Returns
true if the command was run, false if the command was not known

Definition at line 957 of file CommandInterpreter.cc.

Referenced by AddKey(), DYNTRANS_INSTR(), DummyComponent::GetAttribute(), GetCurrentCommandBuffer(), CopyComponentCommand::GetLongDescription(), MoveComponentCommand::GetLongDescription(), BackwardStepCommand::GetLongDescription(), GXemul::Reset(), MainbusComponent::WriteData(), and CPUComponent::WriteData().

◆ RunUnitTests()

static void CommandInterpreter::RunUnitTests ( int &  nSucceeded,
int &  nFailures 
)
static

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

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13