FreeNOS
Public Member Functions | Private Member Functions | Private Attributes
Shell Class Reference

System command shell interpreter. More...

#include <Shell.h>

Inheritance diagram for Shell:
POSIXApplication Application

Public Member Functions

 Shell (int argc, char **argv)
 Constructor. More...
 
virtual ~Shell ()
 Destructor. More...
 
virtual Result exec ()
 Execute the application. More...
 
ShellCommandgetCommand (const char *name)
 Get shell command. More...
 
HashTable< String, ShellCommand * > & getCommands ()
 Get all shell commands. More...
 
void registerCommand (ShellCommand *command)
 Register a new ShellCommand. More...
 
int executeInput (const Size argc, const char **argv, const bool background)
 Executes the given input. More...
 
int executeInput (char *cmdline)
 Executes the given input. More...
 
- Public Member Functions inherited from POSIXApplication
 POSIXApplication (int argc, char **argv)
 Class constructor. More...
 
virtual ~POSIXApplication ()
 Class destructor. More...
 
- Public Member Functions inherited from Application
 Application (int argc, char **argv)
 Class constructor. More...
 
virtual ~Application ()
 Class destructor. More...
 
virtual int run ()
 Run the application. More...
 

Private Member Functions

Result runInteractive ()
 Executes the Shell by entering an infinite loop. More...
 
char * getInput () const
 Fetch a command text from standard input. More...
 
void prompt () const
 Output a prompt. More...
 
Size parse (char *cmdline, char **argv, Size maxArgv, bool *background)
 Parses an input string into separate pieces. More...
 

Private Attributes

HashTable< String, ShellCommand * > m_commands
 All known ShellCommands. More...
 

Additional Inherited Members

- Public Types inherited from Application
enum  Result {
  Success, NotFound, IOError, InvalidArgument,
  ShowUsage, TimedOut, OutOfMemory
}
 Result codes. More...
 
- Protected Member Functions inherited from POSIXApplication
virtual Result output (const char *string) const
 Print text to output. More...
 
int runProgram (const char *path, const char **argv)
 Runs an external program. More...
 
- Protected Member Functions inherited from Application
virtual Result initialize ()
 Initialize the application. More...
 
virtual Result output (String &string) const
 Print string to output. More...
 
ArgumentParserparser ()
 Get program arguments parser. More...
 
const ArgumentParserparser () const
 Get constant program arguments parser. More...
 
const ArgumentContainerarguments () const
 Get program arguments. More...
 
void setVersion (const String &version)
 Set program version. More...
 
- Protected Attributes inherited from Application
int m_argc
 Input argument count. More...
 
char ** m_argv
 Input argument values. More...
 

Detailed Description

System command shell interpreter.

Definition at line 33 of file Shell.h.

Constructor & Destructor Documentation

◆ Shell()

Shell::Shell ( int  argc,
char **  argv 
)

Constructor.

Parameters
argcArgument count
argvArgument values

Definition at line 40 of file Shell.cpp.

References Application::parser(), registerCommand(), ArgumentParser::registerPositional(), and ArgumentParser::setDescription().

◆ ~Shell()

Shell::~Shell ( )
virtual

Destructor.

Definition at line 54 of file Shell.cpp.

References HashIterator< K, V >::hasCurrent(), and m_commands.

Member Function Documentation

◆ exec()

Shell::Result Shell::exec ( )
virtual

◆ executeInput() [1/2]

int Shell::executeInput ( char *  cmdline)

Executes the given input.

Parameters
cmdlineFull commandline input to execute.
Returns
Exit status of the command.

Definition at line 220 of file Shell.cpp.

References executeInput(), EXIT_SUCCESS, MAX_ARGV, parse(), and strlen().

◆ executeInput() [2/2]

int Shell::executeInput ( const Size  argc,
const char **  argv,
const bool  background 
)

Executes the given input.

Parameters
argcArgument count
argvArgument values
backgroundTrue to run program without waiting for termination
Returns
Exit status of the command.

Definition at line 161 of file Shell.cpp.

References DEBUG, errno, ERROR, ShellCommand::execute(), EXIT_FAILURE, EXIT_SUCCESS, getCommand(), ShellCommand::getMinimumParams(), ShellCommand::getName(), POSIXApplication::runProgram(), snprintf(), strerror(), and waitpid().

Referenced by exec(), TimeCommand::execute(), executeInput(), and runInteractive().

◆ getCommand()

ShellCommand * Shell::getCommand ( const char *  name)

Get shell command.

Parameters
nameShellCommand name.
Returns
ShellCommand object pointer.

Definition at line 317 of file Shell.cpp.

References HashTable< K, V >::get(), m_commands, and ZERO.

Referenced by executeInput().

◆ getCommands()

HashTable< String, ShellCommand * > & Shell::getCommands ( )

Get all shell commands.

Returns
HashTable with ShellCommands

Definition at line 312 of file Shell.cpp.

References m_commands.

Referenced by HelpCommand::execute().

◆ getInput()

char * Shell::getInput ( ) const
private

Fetch a command text from standard input.

Returns
Pointer to a command text.

Definition at line 239 of file Shell.cpp.

References NULL, printf(), read(), and ZERO.

Referenced by runInteractive().

◆ parse()

Size Shell::parse ( char *  cmdline,
char **  argv,
Size  maxArgv,
bool *  background 
)
private

Parses an input string into separate pieces.

Parameters
cmdlineCommand input string.
argvArgument list buffer.
maxArgvMaximum number of entries in argv.
backgroundTrue if process must run in the background.
Returns
Number of parsed arguments.

Definition at line 327 of file Shell.cpp.

References ZERO.

Referenced by executeInput().

◆ prompt()

void Shell::prompt ( ) const
private

Output a prompt.

Definition at line 289 of file Shell.cpp.

References BLUE, getcwd(), gethostname(), GREEN, NULL, printf(), and WHITE.

Referenced by runInteractive().

◆ registerCommand()

void Shell::registerCommand ( ShellCommand command)

Register a new ShellCommand.

Parameters
commandShellCommand object pointer.

Definition at line 322 of file Shell.cpp.

References ShellCommand::getName(), HashTable< K, V >::insert(), and m_commands.

Referenced by Shell().

◆ runInteractive()

Shell::Result Shell::runInteractive ( )
private

Executes the Shell by entering an infinite loop.

Returns
Result code

Definition at line 135 of file Shell.cpp.

References executeInput(), getInput(), prompt(), strlen(), and Application::Success.

Referenced by exec().

Field Documentation

◆ m_commands

HashTable<String, ShellCommand *> Shell::m_commands
private

All known ShellCommands.

Definition at line 131 of file Shell.h.

Referenced by getCommand(), getCommands(), registerCommand(), and ~Shell().


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