decompiler
1.0.0
|
A generic console mode interface and command executor. More...
#include <interface.hh>
Public Member Functions | |
IfaceStatus (const string &prmpt, ostream &os, int4 mxhist=10) | |
Constructor. More... | |
virtual | ~IfaceStatus (void) |
Destructor. | |
void | setErrorIsDone (bool val) |
Set if processing should terminate on an error. | |
void | pushScript (const string &filename, const string &newprompt) |
Push a new file on the script stack. More... | |
virtual void | pushScript (istream *iptr, const string &newprompt) |
Provide a new input stream to execute, with an associated command prompt. More... | |
virtual void | popScript (void) |
Return to processing the parent stream. More... | |
virtual void | reset (void) |
Pop any existing script streams and return to processing from the base stream. | |
int4 | getNumInputStreamSize (void) const |
Get depth of script nesting. | |
void | writePrompt (void) |
Write the current command prompt to the current output stream. | |
void | registerCom (IfaceCommand *fptr, const char *nm1, const char *nm2=(const char *) 0, const char *nm3=(const char *) 0, const char *nm4=(const char *) 0, const char *nm5=(const char *) 0) |
Register a command with this interface. More... | |
IfaceData * | getData (const string &nm) const |
Get data associated with a IfaceCommand module. More... | |
bool | runCommand (void) |
Run the next command. More... | |
void | getHistory (string &line, int4 i) const |
Get the i-th command line from history. More... | |
int4 | getHistorySize (void) const |
Get the number of command lines in history. | |
virtual bool | isStreamFinished (void) const =0 |
Return true if the current stream is finished. | |
bool | isInError (void) const |
Return true if the last command failed. | |
void | evaluateError (void) |
Adjust which stream to process based on last error. | |
Static Public Member Functions | |
static void | wordsToString (string &res, const vector< string > &list) |
Concatenate tokens. More... | |
Public Attributes | |
bool | done |
Set to true (by a command) to indicate processing is finished. | |
ostream * | optr |
Where to put command line output. | |
ostream * | fileoptr |
Where to put bulk output. | |
Protected Member Functions | |
int4 | expandCom (vector< string > &expand, istream &s, vector< IfaceCommand *>::const_iterator &first, vector< IfaceCommand *>::const_iterator &last) |
Expand tokens from the given input stream to a full command. More... | |
Protected Attributes | |
bool | inerror |
Set to true if last command did not succeed. | |
vector< IfaceCommand * > | comlist |
List of registered commands. | |
map< string, IfaceData * > | datamap |
Data associated with particular modules. | |
Private Member Functions | |
void | restrictCom (vector< IfaceCommand *>::const_iterator &first, vector< IfaceCommand *>::const_iterator &last, vector< string > &input) |
Restrict range of possible commands given a list of command line tokens. More... | |
virtual void | readLine (string &line)=0 |
Read the next command line. More... | |
void | saveHistory (const string &line) |
Store the given command line into history. More... | |
Private Attributes | |
vector< string > | promptstack |
Stack of command prompts corresponding to script nesting level. | |
vector< uint4 > | flagstack |
Stack of flag state corresponding to script nesting level. | |
string | prompt |
The current command prompt. | |
int4 | maxhistory |
Maximum number of command lines to store in history. | |
int4 | curhistory |
Most recent history. | |
vector< string > | history |
History of commands executed through this interface. | |
bool | sorted |
Set to true if commands are sorted. | |
bool | errorisdone |
Set to true if any error terminates the process. | |
A generic console mode interface and command executor.
Input is provided one command line at a time by providing calling readLine(). Output goes to a provided ostream, optr. Output to a separate bulk stream can be enabled by setting fileoptr.
A derived IfaceCommand is attached to a command string via registerCom() i.e. stat.registerCom(new IfcQuit(),"quit"); stat.registerCom(new IfcOpenfileAppend(),"openfile","append"); stat.mainloop(); Command line processing is started with mainloop(), which prints a command prompt, allows command line editing, including command completion and history, and executes the corresponding IfaceComman::execute() callback. Command words only have to match enough to disambiguate it from other commands. A Custom history size and command prompt can be passed to the constructor. Applications should inherit from base class IfaceStatus in order to
ghidra::IfaceStatus::IfaceStatus | ( | const string & | prmpt, |
ostream & | os, | ||
int4 | mxhist = 10 |
||
) |
Constructor.
prmpt | is the base command line prompt |
os | is the base stream to write output to |
mxhist | is the maximum number of lines to store in history |
|
protected |
Expand tokens from the given input stream to a full command.
A range of possible commands is returned. Processing of the stream stops as soon as at least one complete command is recognized. Tokens partially matching a command are expanded to the full command and passed back.
expand | will hold the list of expanded tokens |
s | is the input stream tokens are read from |
first | will hold the beginning of the matching range of commands |
last | will hold the end of the matching range of commands |
Referenced by ghidra::IfaceTerm::doCompletion().
IfaceData * ghidra::IfaceStatus::getData | ( | const string & | nm | ) | const |
Get data associated with a IfaceCommand module.
Commands (IfaceCommand) are associated with a particular module that has a formal name and a data object associated with it. This method retrieves the module specific data object by name.
nm | is the name of the module |
Referenced by ghidra::IfcExecuteTestCommand::execute(), and ghidra::FunctionTestCollection::FunctionTestCollection().
void ghidra::IfaceStatus::getHistory | ( | string & | line, |
int4 | i | ||
) | const |
Get the i-th command line from history.
A command line is selected by specifying how many steps in time to go back through the list of successful command lines.
line | will hold the selected command line from history |
i | is the number of steps back to go |
Referenced by ghidra::IfaceTerm::readLine().
|
virtual |
Return to processing the parent stream.
The current input stream, as established by a script, is popped from the stack, along with its command prompt, and processing continues with the previous stream.
Reimplemented in ghidra::IfaceTerm.
Referenced by ghidra::IfcExecuteTestCommand::execute(), and ghidra::IfaceTerm::popScript().
void ghidra::IfaceStatus::pushScript | ( | const string & | filename, |
const string & | newprompt | ||
) |
Push a new file on the script stack.
Attempt to open the file, and if we succeed put the open stream onto the script stack.
filename | is the name of the script file |
newprompt | is the command line prompt to associate with the file |
Referenced by ghidra::IfcSource::execute(), and ghidra::IfaceTerm::pushScript().
|
virtual |
Provide a new input stream to execute, with an associated command prompt.
The new stream is added to a stack and becomes the primary source for parsing new commands. Once commands from the stream are exhausted, parsing will resume in the previous stream.
iptr | is the new input stream |
newprompt | is the command line prompt to associate with the new stream |
Reimplemented in ghidra::IfaceTerm.
|
privatepure virtual |
Read the next command line.
line | is filled in with the next command to execute |
Implemented in ghidra::ConsoleCommands, and ghidra::IfaceTerm.
void ghidra::IfaceStatus::registerCom | ( | IfaceCommand * | fptr, |
const char * | nm1, | ||
const char * | nm2 = (const char *)0 , |
||
const char * | nm3 = (const char *)0 , |
||
const char * | nm4 = (const char *)0 , |
||
const char * | nm5 = (const char *)0 |
||
) |
Register a command with this interface.
A command object is associated with one or more tokens on the command line. A string containing up to 5 tokens can be associated with the command.
fptr | is the IfaceCommand object |
nm1 | is the first token representing the command |
nm2 | is the second token (or null) |
nm3 | is the third token (or null) |
nm4 | is the fourth token (or null) |
nm5 | is the fifth token (or null) |
References ghidra::IfaceCommand::addWord(), ghidra::IfaceCommand::createData(), ghidra::IfaceCommand::getModule(), and ghidra::IfaceCommand::setData().
Referenced by ghidra::IfaceDecompCapability::registerCommands().
|
private |
Restrict range of possible commands given a list of command line tokens.
Given a set of tokens partially describing a command, provide the most narrow range of IfaceCommand objects that could be referred to.
first | will hold an iterator to the first command in the range |
last | will hold an iterator (one after) the last command in the range |
input | is the list of command tokens to match on |
References ghidra::IfaceCommand::addWord(), ghidra::IfaceCommand::addWords(), ghidra::compare_ifacecommand(), and ghidra::IfaceCommand::removeWord().
bool ghidra::IfaceStatus::runCommand | ( | void | ) |
Run the next command.
A single command line is read (via readLine) and executed. If the command is successfully executed, the command line is committed to history and true is returned.
References ghidra::compare_ifacecommand().
Referenced by ghidra::IfcExecuteTestCommand::execute().
|
private |
Store the given command line into history.
The line is saved in a circular history buffer
line | is the command line to save |
|
static |
Concatenate tokens.
Concatenate a list of tokens into a single string, separated by a space character.
Referenced by ghidra::IfaceCommand::commandString(), and ghidra::IfaceTerm::doCompletion().