UCommon
|
A utility class for generic shell operations. More...
#include <shell.h>
Data Structures | |
class | charopt |
Character option for shell parsing. More... | |
class | counteropt |
Counter option for shell parsing. More... | |
class | errormap |
A class to redefine error messages. More... | |
class | flagopt |
Flag option for shell parsing. More... | |
class | groupopt |
Grouping option. More... | |
class | numericopt |
Numeric option for shell parsing. More... | |
class | Option |
A base class used to create parsable shell options. More... | |
class | stringopt |
Text option for shell parsing. More... | |
Public Types | |
enum | errmsg_t { NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET } |
Error table index. | |
typedef void(* | exitproc_t) (void) |
Exit handler. | |
enum | loglevel_t { FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0 } |
Level of error logging. | |
enum | logmode_t { NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG } |
Type of error logging we are using. | |
typedef bool(* | logproc_t) (loglevel_t level, const char *text) |
Log process handler. | |
typedef cpr_service_t | mainproc_t |
Main handler. | |
enum | numeric_t { NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL } |
Numeric mode of parser. | |
enum | path_t { PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL, USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE, SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC, SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS, PROGRAM_TEMP } |
Path types to retrieve. | |
typedef int | pid_t |
Standard type of process id for shell class. | |
Public Member Functions | |
unsigned | argc (void) const |
Get saved internal argc count for items. More... | |
char ** | argv (void) const |
Get saved internal argv count for items in this shell object. More... | |
const char * | argv0 () const |
Get program name (argv0). | |
void | detach (mainproc_t mainentry=(mainproc_t) NULL) |
Detach current process to daemon for service entry. | |
const char * | execdir () const |
Get the exec directory. | |
char ** | getargv (char **argv) |
Get the argument list by parsing options, and return the remaining file arguments. More... | |
char * | getargv0 (char **argv) |
Parse and extract the argv0 filename alone. More... | |
const char * | getenv (const char *name, const char *value=NULL) |
Get an environment variable. More... | |
const char * | getsym (const char *name, const char *value=NULL) |
Get a local symbol. More... | |
bool | is_sym (const char *name) const |
Test if symbol exists. More... | |
unsigned | operator() (void) const |
Return argc count. More... | |
const char * | operator[] (unsigned offset) |
Return parser argv element. More... | |
char ** | parse (const char *string) |
Parse a string as a series of arguments for use in exec calls. More... | |
void | parse (int argc, char **argv) |
Parse the command line arguments using the option table. More... | |
void | restart (char *argv0, char **argv, char **list) |
Execute front-end like gdb based on stripped first argument. More... | |
void | restart (void) |
Make current process restartable. | |
void | setsym (const char *name, const char *value) |
Set a local symbol. More... | |
shell (const char *string, size_t pagesize=0) | |
Construct a shell argument list by parsing a simple command string. More... | |
shell (int argc, char **argv, size_t pagesize=0) | |
Construct a shell argument list from existing arguments. More... | |
shell (size_t pagesize=0) | |
Construct an empty shell parser argument list. More... | |
![]() | |
void | assign (mempager &source) |
Assign foreign pager to us. More... | |
virtual void | dealloc (void *memory) |
Return memory back to pager heap. More... | |
mempager (size_t page=0) | |
Construct a memory pager. More... | |
mempager (const mempager ©) | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. | |
unsigned | utilization (void) |
Determine fragmentation level of acquired heap pages. More... | |
virtual | ~mempager () |
Destroy a memory pager. More... | |
![]() | |
void | assign (memalloc &source) |
Assign foreign pager to us. More... | |
unsigned | max (void) const |
Get the maximum number of pages that are permitted. More... | |
memalloc (size_t page=0) | |
Construct a memory pager. More... | |
memalloc (const memalloc ©) | |
unsigned | pages (void) const |
Get the number of pages that have been allocated from the real heap. More... | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. | |
size_t | size (void) const |
Get the size of a memory page. More... | |
unsigned | utilization (void) const |
Determine fragmentation level of acquired heap pages. More... | |
virtual | ~memalloc () |
Destroy a memory pager. More... | |
Static Public Member Functions | |
static void | bind (const char *name) |
Bind application to text domain for internationalization. More... | |
static int | cancel (shell::pid_t pid) |
Cancel a child process. More... | |
static int | condition (bool test, int exitcode) |
Convert condition to exit code if true. More... | |
static unsigned | count (char **argv) |
Get argc count for an existing array. More... | |
static void | debug (unsigned level, const char *format,...) |
Print a debug message by debug level. More... | |
static int | detach (const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL) |
Create a detached process. More... | |
static void | errexit (int exitcode, const char *format=NULL,...) |
Print error message and exit. More... | |
static void | errlog (const char *format,...) |
Print error message and continue. More... | |
static const char * | errmsg (errmsg_t id) |
This can be used to get internationalized error messages. More... | |
static void | errmsg (errmsg_t id, const char *text) |
This is used to set internationalized error messages for the shell parser. More... | |
static fd_t | error (void) |
static void | exiting (exitproc_t) |
static char * | getline (const char *prompt, char *buffer, size_t size) |
static long | getNumeric (void) |
static char * | getpass (const char *prompt, char *buffer, size_t size) |
static void | help (void) |
Display shell options. | |
static int | inkey (const char *prompt=NULL) |
static fd_t | input (void) |
static void | log (loglevel_t level, const char *format,...) |
Print generic error message at specific error level. More... | |
static void | log (const char *name, loglevel_t level=ERR, logmode_t mode=USER_LOG, logproc_t handler=(logproc_t) NULL) |
Set logging level and state. More... | |
static fd_t | output (void) |
static String | path (path_t id) |
Get a system path. More... | |
static String | path (path_t id, const char *directory) |
Get a merged path. More... | |
static String | path (String &prefix, const char *directory) |
Join a prefix with a path. More... | |
static size_t | printf (const char *format,...) |
static void | priority (int pri=1) |
Set priority level and enable priority scheduler. More... | |
static void | rebind (const char *name=NULL) |
Rebind is used to change the text domain. More... | |
static void | release (int exit_code=0) |
Detach and release from parent process with exit code. More... | |
static void | relocate (const char *argv0) |
Set relative prefix. More... | |
static void | security (loglevel_t level, const char *format,...) |
Print security error message at specific error level. More... | |
static void | setNumeric (numeric_t) |
static shell::pid_t | spawn (const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL) |
Spawn a child process. More... | |
static int | system (const char *command, const char **env=NULL) |
A shell system call. More... | |
static int | systemf (const char *format,...) |
A shell system call that can be issued using a formatted string. More... | |
static const char * | text (const char *string) |
Text translation and localization. More... | |
static const char * | texts (const char *singular, const char *plural, unsigned long count) |
Plural text translation and localization. More... | |
static String | userid (void) |
Get the system login id. More... | |
static int | wait (shell::pid_t pid) |
Wait for a child process to terminate. More... | |
Additional Inherited Members | |
![]() | |
virtual void * | _alloc (size_t size) |
Allocate memory from the pager heap. More... | |
virtual void | _lock (void) |
Lock the memory pager mutex. More... | |
virtual void | _unlock (void) |
Unlock the memory pager mutex. | |
![]() | |
page_t * | pager (void) |
Acquire a new page from the heap. More... | |
![]() | |
unsigned | limit |
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.
ucommon::shell::shell | ( | const char * | string, |
size_t | pagesize = 0 |
||
) |
Construct a shell argument list by parsing a simple command string.
This separates a string into a list of command line arguments which can be used with exec functions.
string | to parse. |
pagesize | for local heap. |
ucommon::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. |
ucommon::shell::shell | ( | size_t | pagesize = 0 | ) |
Construct an empty shell parser argument list.
pagesize | for local heap. |
|
inline |
|
inline |
|
static |
Bind application to text domain for internationalization.
This is useful if the argv0 argument can vary because of a symlinked executable. This is the name of the .po/.mo message files for your application. If bind is not called before shell processing, then the argv0 is used as the bind name. Bind can be called multiple times to change the default message catalog name of the application, and this usage may be needed for plugins, though it's generally recommended to use only once, and at the start of main().
name | of text domain for the application. |
|
static |
Cancel a child process.
pid | of child process to cancel. |
|
inlinestatic |
|
static |
Get argc count for an existing array.
argv | to count items in. |
|
static |
Print a debug message by debug level.
level | of debug message. |
format | string to use. |
|
static |
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 explicitly set. |
stdio | handles for stdin, stdout, and stderr. |
|
static |
Print error message and exit.
Ignored if exitcode == 0.
exitcode | to return to parent process. |
format | string to use. |
|
static |
Print error message and continue.
format | string to use. |
|
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. |
|
static |
This is used to set internationalized error messages for the shell parser.
id | of message to set. |
text | for error message. |
char** ucommon::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* ucommon::shell::getargv0 | ( | char ** | argv | ) |
Parse and extract the argv0 filename alone.
argv | from main. |
const char* ucommon::shell::getenv | ( | const char * | name, |
const char * | value = NULL |
||
) |
Get an environment variable.
This creates a local copy of the variable in pager memory.
name | of symbol. |
value | of symbol if not found. |
const char* ucommon::shell::getsym | ( | const char * | name, |
const char * | value = NULL |
||
) |
Get a local symbol.
This uses getenv if no local symbol is found.
name | of symbol. |
value | of symbol if not found. |
bool ucommon::shell::is_sym | ( | const char * | name | ) | const |
Test if symbol exists.
name | of symbol. |
|
static |
Print generic error message at specific error level.
level | of error condition. |
format | string to use. |
|
static |
Set logging level and state.
name | of logging entity. |
level | of error conditions to log. |
mode | of logging. |
handler | for log messages. |
|
inline |
|
inline |
char** ucommon::shell::parse | ( | const char * | string | ) |
Parse a string as a series of arguments for use in exec calls.
string | to parse. |
void ucommon::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. |
Get a system path.
This is used to get directories for application specific data stores and default paths for config keys.
id | of path to return. |
Get a merged path.
If the path requested is a full path, then the prefix is ignored.
id | of prefix. |
directory | path to merge with prefix. |
Join a prefix with a path.
prefix | to merge with. |
directory | or file path to merge. |
|
static |
Set priority level and enable priority scheduler.
This activates the realtime priority scheduler when a priority > 0 is requested for the process, assuming scheduler support exists and the process is sufficiently privileged. Negative priorities are essentially the same as nice.
pri | level for process. |
|
static |
Rebind is used to change the text domain.
This may be needed in applications which have separately built plugins that have thier own message catalogs. Normally the plugin would call bind itself at initialization, and then use rebind to select either the application's domain, or the plugins. On systems without internationalization, this has no effect.
name | of text domain of plugin or NULL to restore application. |
|
static |
Detach and release from parent process with exit code.
exit_code | to send to parent process. |
|
static |
Set relative prefix.
Used for OS/X relocatable applications.
argv0 | path of executable. |
void ucommon::shell::restart | ( | char * | argv0, |
char ** | argv, | ||
char ** | list | ||
) |
Execute front-end like gdb based on stripped first argument.
argv0 | of our executable. |
argv | to pass to child. |
list | of arguments to execute in front of argv. |
|
static |
Print security error message at specific error level.
level | of error condition. |
format | string to use. |
void ucommon::shell::setsym | ( | const char * | name, |
const char * | value | ||
) |
Set a local symbol.
name | of symbol to set. |
value | of symbol to set. |
|
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 |
A shell system call.
This uses the native system shell to invoke the command.
command | string.. |
env | array to optionally use. |
|
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 |
Text translation and localization.
This function does nothing but return the original string if no internationalization support is available. If internationalization support exists, then it may return a substituted translation based on the current locale. This offers a single function that can be safely used either when internationalization support is present, or it is absent, eliminating the need for the application to be coded differently based on awareness of support.
string | to translate. |
|
static |
Plural text translation and localization.
This does nothing but return single or plural forms if no internationalization is enabled. Else it uses ngettext().
singular | string to translate. |
plural | string to translate. |
count | of objects. |
|
static |
Get the system login id.
|
static |
Wait for a child process to terminate.
This operation blocks.
pid | of process to wait for. |