Go to the documentation of this file.
70 if (positionals.
count() > 0)
73 for (
Size i = 0; i < positionals.
count(); i++)
75 const char *file = *(positionals[i]->getValue());
78 if (
stat(file, &st) != 0)
92 contents =
new char[st.
st_size + 1];
105 String contentString(contents);
124 "Entering interactive Shell. Type 'help' for the command list.\r\n"
149 if (!cmdStr ||
strlen(cmdStr) == 0)
167 for (
Size i = 0; i < argc; i++)
169 DEBUG(
"argv[" << i <<
"] = " << argv[i]);
173 if (argv[0][0] ==
'#')
191 else if (argv[0][0] !=
'/' &&
snprintf(tmp,
sizeof(tmp),
"/bin/%s", argv[0]) &&
214 return cmd->
execute(argc - 1, argv + 1);
236 return executeInput(argc, (
const char **)argv, background);
241 static char line[1024];
244 const bool echo =
false;
246 const bool echo =
true;
250 while (total <
sizeof(line) - 1)
256 return (
char *)
NULL;
275 if (echo)
printf(
"\b \b");
280 if (echo)
printf(
"%c", line[total]);
291 char host[128], cwd[128];
333 for (argc = 0; argc < maxArgv && *cmdline; argc++)
335 while (*cmdline && *cmdline ==
' ')
344 argv[argc] = cmdline;
346 while (*cmdline && *cmdline !=
' ')
349 if (*cmdline) *cmdline++ =
ZERO;
int gethostname(char *name, size_t namelen)
Get name of current host.
virtual ~Shell()
Destructor.
#define EXIT_FAILURE
Unsuccessful termination.
off_t st_size
For regular files, the file size in bytes.
virtual Result exec()
Execute the application.
HashTable< String, ShellCommand * > m_commands
All known ShellCommands.
The <sys/stat.h> header shall define the stat structure.
Measure the execution time of a program.
size_t strlen(const char *str)
Calculate the length of a string.
C int errno
The lvalue errno is used by many functions to return error values.
FILE * fopen(const char *filename, const char *mode)
Open a stream.
void prompt() const
Output a prompt.
char * getcwd(char *buf, size_t size)
Get the pathname of the current working directory.
Builtin command for the Shell.
Iterate through a HashTable.
POSIX-compatible application.
Prints the help info for all known ShellCommands.
const ArgumentContainer & arguments() const
Get program arguments.
void setDescription(const String &desc)
Set program description.
const char * getName() const
Get command name.
int executeInput(const Size argc, const char **argv, const bool background)
Executes the given input.
ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
void registerCommand(ShellCommand *command)
Register a new ShellCommand.
List< String > split(const char delimiter) const
Split the String into parts separated by a delimiter.
#define DEBUG(msg)
Output a debug message to standard output.
int printf(const char *format,...)
Output a formatted string to standard output.
virtual bool hasCurrent() const
Check if there is a current item on the List.
Shell(int argc, char **argv)
Constructor.
virtual const V * get(const K &key) const
Returns the first value for the given key.
int runProgram(const char *path, const char **argv)
Runs an external program.
virtual Size count() const
Returns the number of items inside the Vector.
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
HashTable< String, ShellCommand * > & getCommands()
Get all shell commands.
#define NULL
NULL means zero.
unsigned int Size
Any sane size indicator cannot go negative.
Size parse(char *cmdline, char **argv, Size maxArgv, bool *background)
Parses an input string into separate pieces.
Change the current working directory.
Result runInteractive()
Executes the Shell by entering an infinite loop.
char * getInput() const
Fetch a command text from standard input.
int stat(const char *path, struct stat *buf)
Get file status.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
#define MAX_ARGV
Maximum number of supported command arguments.
#define EXIT_SUCCESS
Successful termination.
ArgumentParser & parser()
Get program arguments parser.
#define ERROR(msg)
Output an error message.
ShellCommand * getCommand(const char *name)
Get shell command.
Size getMinimumParams() const
Get the minimum number of parameters required.
virtual int execute(const Size nparams, const char **params)=0
Executes the command.
slong ssize_t
Used for a count of bytes or an error indication.
virtual bool hasCurrent() const
Check if there is a current item.
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream)
Binary input.
A structure containing information about a file.
pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
int fclose(FILE *stream)
Close a stream.
int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
virtual bool insert(const K &key, const V &value)
Inserts the given item to the HashTable.
Change the standard Input/Output of the shell.
const Vector< Argument * > & getPositionals() const
Get positional arguments.