A utility class for generic shell operations. More...
#include <shell.h>
Data Structures | |
class | charopt |
Character option for shell parsing. More... | |
class | errormap |
A class to redefine error messages. More... | |
class | flagopt |
Flag option for shell parsing. More... | |
class | iobuf |
Process pipe with I/O buffering. More... | |
class | numericopt |
Numeric option for shell parsing. More... | |
class | Option |
A base class used to create parsable shell options. More... | |
class | pipeio |
A class to control a process that is piped. More... | |
class | stringopt |
Text option for shell parsing. More... | |
Public Types | |
enum | errmsg_t { NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE } |
Error table index. | |
typedef iobuf | io_t |
Buffered i/o type for pipes and stdio. | |
typedef int | pid_t |
Standard type of process id for shell class. | |
typedef pipeio * | pipe_t |
Convenience low level pipe object type. | |
enum | pmode_t { RD = IOBuffer::BUF_RD, WR = IOBuffer::BUF_WR, RDWR = IOBuffer::BUF_RDWR } |
Pipe I/O mode. | |
Public Member Functions | |
unsigned | argc (void) |
Get saved internal argc count for items. | |
char ** | argv (void) |
Get saved internal argv count for items in this shell object. | |
char * | argv0 () |
Get program name (argv0). | |
int | detach (char *path, char **argv, char **env=((void *) 0), fd_t *stdio=((void *) 0)) |
Create a detached process. | |
char ** | getargv (char **argv) |
Get the argument list by parsing options, and return the remaining file arguments. | |
char * | getargv0 (char **argv) |
Parse and extract the argv0 filename alone. | |
char * | getenv (char *name, char *value=((void *) 0)) |
Get an environment variable. | |
unsigned | operator() (void) |
Return argc count. | |
char * | operator[] (unsigned offset) |
Return parser argv element. | |
void | parse (int argc, char **argv) |
Parse the command line arguments using the option table. | |
char ** | parse (char *string) |
Parse a string as a series of arguments for use in exec calls. | |
shell (size_t pagesize=0) | |
Construct an empty shell parser argument list. | |
shell (int argc, char **argv, size_t pagesize=0) | |
Construct a shell argument list from existing arguments. | |
shell (char *string, size_t pagesize=0) | |
Construct a shell argument list by parsing a simple command string. | |
Static Public Member Functions | |
static int | cancel (shell::pipe_t pointer) |
Cancel a child pipe. | |
static int | cancel (shell::pid_t pid) |
Cancel a child process. | |
static unsigned | count (char **argv) |
Get argc count for an existing array. | |
static void | errexit (int exitcode, char *format=((void *) 0),...) |
Print error message and exit. | |
static void | errmsg (errmsg_t id, char *text) |
This is used to set internationalized error messages for the shell parser. | |
static char * | errmsg (errmsg_t id) |
This can be used to get internationalized error messages. | |
static fd_t | error (void) |
static void | help (void) |
Display shell options. | |
static fd_t | input (void) |
static fd_t | output (void) |
static char ** | parse (shell &args, char *string) |
Parse shell arguments directly into a shell object. | |
static size_t | printf (pipe_t pipe, char *format,...) |
Print to a pipe object. | |
static size_t | printf (char *format,...) |
Print to standard output. | |
static size_t | read (pipe_t pipe, String &string) |
static size_t | read (String &string) |
static size_t | readln (pipe_t pipe, char *buffer, size_t size) |
Read a line from a pipe object. | |
static size_t | readln (char *address, size_t size) |
static shell::pipe_t | spawn (char *path, char **argv, pmode_t mode, size_t size=512, char **env=((void *) 0)) |
Spawn a child pipe. | |
static shell::pid_t | spawn (char *path, char **argv, char **env=((void *) 0), fd_t *stdio=((void *) 0)) |
Spawn a child process. | |
static int | system (char *command, char **env=((void *) 0)) |
A shell system call. | |
static int | systemf (char *format,...) |
A shell system call that can be issued using a formatted string. | |
static int | wait (shell::pipe_t pointer) |
Wait for a child pipe to terminate. | |
static int | wait (shell::pid_t pid) |
Wait for a child process to terminate. | |
static size_t | write (pipe_t pipe, String &string) |
static size_t | write (String &string) |
static size_t | writes (pipe_t pipe, char *string) |
static size_t | writes (char *string) |
A utility class for generic shell operations.
This includes utilities to parse and expand arguments, and to call system shell services. This also includes a common shell class to parse and process command line arguments which are managed through a local heap.
Definition at line 58 of file shell.h.
ucc::shell::shell | ( | char * | string, | |
size_t | pagesize = 0 | |||
) |
Construct a shell argument list by parsing a simple command string.
This seperates a string into a list of command line arguments which can be used with exec functions.
string | to parse. | |
pagesize | for local heap. |
ucc::shell::shell | ( | int | argc, | |
char ** | argv, | |||
size_t | pagesize = 0 | |||
) |
Construct a shell argument list from existing arguments.
This copies and on some platforms expands the argument list originally passed to main.
argc | from main. | |
argv | from main. | |
pagesize | for local heap. |
ucc::shell::shell | ( | size_t | pagesize = 0 |
) |
Construct an empty shell parser argument list.
pagesize | for local heap. |
unsigned ucc::shell::argc | ( | void | ) | [inline] |
char** ucc::shell::argv | ( | void | ) | [inline] |
static int ucc::shell::cancel | ( | shell::pipe_t | pointer | ) | [static] |
Cancel a child pipe.
If the pipe io handle is dynamic, it is deleted.
pointer | to pipe of child process to cancel. |
static int ucc::shell::cancel | ( | shell::pid_t | pid | ) | [static] |
Cancel a child process.
pid | of child process to cancel. |
static unsigned ucc::shell::count | ( | char ** | argv | ) | [static] |
Get argc count for an existing array.
argv | to count items in. |
int ucc::shell::detach | ( | char * | path, | |
char ** | argv, | |||
char ** | env = ((void *) 0) , |
|||
fd_t * | stdio = ((void *) 0) | |||
) |
Create a detached process.
This creates a new child process that is completely detached from the current process.
path | to executable. | |
argv | list of command arguments for the child process. | |
env | of child process can be explicity set. | |
stdio | handles for stdin, stdout, and stderr. |
static void ucc::shell::errexit | ( | int | exitcode, | |
char * | format = ((void *) 0) , |
|||
... | ||||
) | [static] |
Print error message and exit.
exitcode | to return to parent process. | |
format | string to use. |
static void ucc::shell::errmsg | ( | errmsg_t | id, | |
char * | text | |||
) | [static] |
This is used to set internationalized error messages for the shell parser.
id | of message to set. | |
text | for error message. |
static char* ucc::shell::errmsg | ( | errmsg_t | id | ) | [static] |
This can be used to get internationalized error messages.
The internal text for shell parser errors are passed through here.
id | of error message to use. |
char** ucc::shell::getargv | ( | char ** | argv | ) |
Get the argument list by parsing options, and return the remaining file arguments.
This is used by parse, and can be fed by main by posting ++argv.
argv | of first option. |
char* ucc::shell::getargv0 | ( | char ** | argv | ) |
Parse and extract the argv0 filename alone.
argv | from main. |
char* ucc::shell::getenv | ( | char * | name, | |
char * | value = ((void *) 0) | |||
) |
Get an environment variable.
This creates a local copy of the variable in pager memory.
name | of symbol. | |
value | of symbol if not found. |
unsigned ucc::shell::operator() | ( | void | ) | [inline] |
char* ucc::shell::operator[] | ( | unsigned | offset | ) | [inline] |
static char** ucc::shell::parse | ( | shell & | args, | |
char * | string | |||
) | [inline, static] |
void ucc::shell::parse | ( | int | argc, | |
char ** | argv | |||
) |
Parse the command line arguments using the option table.
File arguments will be expanded for wildcards on some platforms. The argv will be set to the first file argument after all options are parsed.
argc | from main. | |
argv | from main. |
char** ucc::shell::parse | ( | char * | string | ) |
Parse a string as a series of arguments for use in exec calls.
string | to parse. |
static size_t ucc::shell::printf | ( | pipe_t | pipe, | |
char * | format, | |||
... | ||||
) | [static] |
Print to a pipe object.
pipe | to write to. | |
format | string to use. |
static size_t ucc::shell::printf | ( | char * | format, | |
... | ||||
) | [static] |
Print to standard output.
format | string to use. |
static size_t ucc::shell::readln | ( | pipe_t | pipe, | |
char * | buffer, | |||
size_t | size | |||
) | [static] |
Read a line from a pipe object.
pipe | to read from. | |
buffer | to save into. | |
size | of buffer. |
static shell::pipe_t ucc::shell::spawn | ( | char * | path, | |
char ** | argv, | |||
pmode_t | mode, | |||
size_t | size = 512 , |
|||
char ** | env = ((void *) 0) | |||
) | [static] |
Spawn a child pipe.
If the executable path is a pure filename, then the $PATH will be used to find it. The argv array may be created from a string with the shell string parser.
path | to executable. | |
argv | list of command arguments for the child process. | |
mode | of pipe, rd only, wr only, or rdwr. | |
size | of pipe buffer. | |
env | of child process can be explicitly set. |
static shell::pid_t ucc::shell::spawn | ( | char * | path, | |
char ** | argv, | |||
char ** | env = ((void *) 0) , |
|||
fd_t * | stdio = ((void *) 0) | |||
) | [static] |
Spawn a child process.
This creates a new child process. If the executable path is a pure filename, then the $PATH will be used to find it. The argv array may be created from a string with the shell string parser.
path | to executable. | |
argv | list of command arguments for the child process. | |
env | of child process can be explicitly set. | |
stdio | handles for stdin, stdout, and stderr. |
static int ucc::shell::system | ( | char * | command, | |
char ** | env = ((void *) 0) | |||
) | [static] |
A shell system call.
This uses the native system shell to invoke the command.
command | string.. | |
env | array to optionally use. |
static int ucc::shell::systemf | ( | char * | format, | |
... | ||||
) | [static] |
A shell system call that can be issued using a formatted string.
This uses the native system shell to invoke the command.
format | of/command string. |
static int ucc::shell::wait | ( | shell::pipe_t | pointer | ) | [static] |
Wait for a child pipe to terminate.
This operation blocks. If the pipe io handle is dynamic, it is deleted.
pointer | to pipe of child process to wait for. |
static int ucc::shell::wait | ( | shell::pid_t | pid | ) | [static] |
Wait for a child process to terminate.
This operation blocks.
pid | of process to wait for. |