ucommon
7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
![]() ![]() |
#include <shell.h>
Classes | |
class | args |
class | charopt |
class | counteropt |
class | errormap |
class | flagopt |
class | groupopt |
class | numericopt |
class | Option |
class | stringopt |
class | syms |
Public Types | |
enum | errmsg_t { NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET } |
enum | logmode_t { NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG } |
enum | loglevel_t { FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0 } |
enum | numeric_t { NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL } |
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 } |
typedef bool(* | logproc_t) (loglevel_t level, const char *text) |
typedef cpr_service_t | mainproc_t |
typedef void(* | exitproc_t) (void) |
typedef int | pid_t |
Public Member Functions | |
shell (const char *string, size_t pagesize=0) | |
shell (int argc, char **argv, size_t pagesize=0) | |
shell (size_t pagesize=0) | |
char ** | parse (const char *string) |
void | parse (int argc, char **argv) |
const char * | getenv (const char *name, const char *value=NULL) |
const char * | getsym (const char *name, const char *value=NULL) |
void | setsym (const char *name, const char *value) |
bool | is_sym (const char *name) const |
char * | getargv0 (char **argv) |
char ** | getargv (char **argv) |
void | restart (char *argv0, char **argv, char **list) |
const char * | argv0 () const |
const char * | execdir () const |
static size_t unsigned | argc (void) const |
char ** | argv (void) const |
const char * | operator[] (unsigned offset) |
void | detach (mainproc_t mainentry=(mainproc_t) NULL) |
void | restart (void) |
unsigned | operator() (void) const |
![]() | |
mempager (size_t page=0) | |
mempager (const mempager ©) | |
virtual | ~mempager () |
unsigned | utilization (void) |
void | purge (void) |
virtual void | dealloc (void *memory) |
void | assign (mempager &source) |
![]() | |
memalloc (size_t page=0) | |
memalloc (const memalloc ©) | |
virtual | ~memalloc () |
unsigned | pages (void) const |
unsigned | max (void) const |
size_t | size (void) const |
unsigned | utilization (void) const |
void | purge (void) |
void | assign (memalloc &source) |
Static Public Member Functions | |
static const char * | errmsg (errmsg_t id) |
static void | errmsg (errmsg_t id, const char *text) |
static void | setNumeric (numeric_t) |
static long | getNumeric (void) |
static void | help (void) |
static int | system (const char *command, const char **env=NULL) |
static int | systemf (const char *format,...) __PRINTF(1 |
static int static void | relocate (const char *argv0) |
static String | path (path_t id) |
static String | userid (void) |
static String | path (path_t id, const char *directory) |
static String | path (String &prefix, const char *directory) |
static void | bind (const char *name) |
static void | rebind (const char *name=NULL) |
static void | errlog (const char *format,...) __PRINTF(1 |
static void static void | errexit (int exitcode, const char *format=NULL,...) __PRINTF(2 |
static void static void static int | condition (bool test, int exitcode) |
static void | debug (unsigned level, const char *format,...) __PRINTF(2 |
static void static void | log (loglevel_t level, const char *format,...) __PRINTF(2 |
static void static void static void | security (loglevel_t level, const char *format,...) __PRINTF(2 |
static void static void static void static void | log (const char *name, loglevel_t level=ERR, logmode_t mode=USER_LOG, logproc_t handler=(logproc_t) NULL) |
static size_t | printf (const char *format,...) __PRINTF(1 |
static void | exiting (exitproc_t) |
static shell::pid_t | spawn (const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL) |
static void | priority (int pri=1) |
static int | detach (const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL) |
static void | release (int exit_code=0) |
static int | wait (shell::pid_t pid) |
static int | cancel (shell::pid_t pid) |
static const char * | text (const char *string) |
static const char * | texts (const char *singular, const char *plural, unsigned long count) |
static unsigned | count (char **argv) |
static fd_t | input (void) |
static fd_t | output (void) |
static fd_t | error (void) |
static int | inkey (const char *prompt=NULL) |
static char * | getpass (const char *prompt, char *buffer, size_t size) |
static char * | getline (const char *prompt, char *buffer, size_t size) |
Private Member Functions | |
__DELETE_COPY (shell) | |
void | collapse (LinkedObject *first) |
void | set0 (char *argv0) |
Private Attributes | |
char ** | _argv |
unsigned | _argc |
char * | _argv0 |
char * | _exedir |
LinkedObject * | _syms |
Additional Inherited Members | |
![]() | |
virtual void | _lock (void) __OVERRIDE |
virtual void | _unlock (void) __OVERRIDE |
virtual void * | _alloc (size_t size) __OVERRIDE |
![]() | |
page_t * | pager (void) |
![]() | |
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.
typedef void(* ucommon::shell::exitproc_t) (void) |
typedef bool(* ucommon::shell::logproc_t) (loglevel_t level, const char *text) |
typedef int ucommon::shell::pid_t |
ucommon::shell::shell | ( | const char * | string, |
size_t | pagesize = 0 |
||
) |
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. |
Definition at line 356 of file shell.cpp.
References _argc, _argv, _argv0, _syms, argc(), argv(), and parse().
ucommon::shell::shell | ( | size_t | pagesize = 0 | ) |
|
private |
|
inline |
|
inline |
Get saved internal argv count for items in this shell object. This may be filename items only that remain after shell expansion and options that have been parsed.
Definition at line 741 of file shell.h.
Referenced by collapse(), count(), detach(), getargv(), getargv0(), main(), parse(), restart(), shell(), and spawn().
|
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. |
Definition at line 2113 of file shell.cpp.
References ucommon::_domain, bindtextdomain, path(), setlocale, SYSTEM_SHARE, and textdomain.
|
static |
Cancel a child process.
pid | of child process to cancel. |
Definition at line 2083 of file shell.cpp.
References wait().
Referenced by ucommon::pipestream::terminate().
|
private |
Collapse argument list. This is used internally to collapse args that are created in a pool heap when they need to be turned into an argv style array.
Definition at line 280 of file shell.cpp.
References ucommon::mempager::_alloc(), _argc, _argv, argv(), ucommon::is(), ucommon::shell::args::item, and ucommon::linked_pointer< T >::next().
|
inlinestatic |
|
static |
|
static |
Print a debug message by debug level.
level | of debug message. |
format | string to use. |
Definition at line 2173 of file shell.cpp.
References DEBUG0, ucommon::errlevel, and ucommon::errname.
|
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. |
Definition at line 1903 of file shell.cpp.
References argv(), INVALID_HANDLE_VALUE, ucommon::memalloc::max(), path(), and ucommon::String::set().
void ucommon::shell::detach | ( | mainproc_t | mainentry = (mainproc_t)NULL | ) |
|
static |
Print error message and exit. Ignored if exitcode == 0.
exitcode | to return to parent process. |
format | string to use. |
Definition at line 828 of file shell.cpp.
References ucommon::eq(), ucommon::errlevel, ucommon::errmode, ucommon::errname, FAIL, NONE, and ucommon::String::set().
Referenced by binarydecode(), getargv(), getargv0(), main(), process(), report(), result(), streamdecode(), and zerofill().
|
static |
Print error message and continue.
format | string to use. |
Definition at line 787 of file shell.cpp.
References ucommon::eq(), ERR, ucommon::errlevel, ucommon::errmode, ucommon::errname, NONE, and ucommon::String::set().
|
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. |
Definition at line 377 of file shell.cpp.
References dgettext, and ucommon::msgs.
Referenced by ucommon::shell::flagopt::assign(), ucommon::shell::stringopt::assign(), ucommon::shell::charopt::assign(), ucommon::shell::numericopt::assign(), ucommon::shell::counteropt::assign(), ucommon::shell::errormap::errormap(), getargv(), and getargv0().
|
static |
This is used to set internationalized error messages for the shell parser.
id | of message to set. |
text | for error message. |
Definition at line 382 of file shell.cpp.
References ucommon::msgs, and text().
|
inline |
|
static |
Definition at line 1771 of file shell.cpp.
References ucommon::_exitproc, ucommon::abort_handler(), and ucommon::exit_handler().
Referenced by main().
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. |
Definition at line 570 of file shell.cpp.
References ucommon::mempager::_alloc(), _argc, _argv, _argv0, argv(), ucommon::shell::Option::assign(), BADOPTION, ucommon::OrderedIndex::begin(), collapse(), ucommon::dup(), ucommon::OrderedObject::enlist(), ucommon::eq(), errexit(), errmsg(), ucommon::shell::Option::first(), INVALID_HANDLE_VALUE, INVARGUMENT, ucommon::is(), ucommon::shell::args::item, ucommon::shell::Option::long_option, ucommon::linked_pointer< T >::next(), NOARGUMENT, NUMERIC_ALL, NUMERIC_DASH, ucommon::numeric_mode, NUMERIC_PLUS, NUMERIC_SET, ucommon::numeric_value, ucommon::String::set(), ucommon::shell::Option::short_option, text(), ucommon::shell::Option::trigger_option, and ucommon::shell::Option::uses_option.
Referenced by parse().
char * ucommon::shell::getargv0 | ( | char ** | argv | ) |
const char * ucommon::shell::getenv | ( | const char * | name, |
const char * | value = NULL |
||
) |
|
static |
Definition at line 1451 of file shell.cpp.
References buffer, ucommon::echo(), input(), ucommon::fsys::is_tty(), ucommon::noecho(), printf(), and ucommon::memalloc::size().
|
static |
Definition at line 393 of file shell.cpp.
References ucommon::numeric_value.
|
static |
Definition at line 1483 of file shell.cpp.
References buffer, count(), ucommon::echo(), ucommon::noecho(), and ucommon::memalloc::size().
Referenced by main().
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. |
Definition at line 2450 of file shell.cpp.
References _syms, ucommon::Mutex::acquire(), ucommon::eq(), getenv(), ucommon::is(), ucommon::shell::syms::name, ucommon::Mutex::release(), ucommon::symlock, and ucommon::shell::syms::value.
|
static |
Display shell options.
Definition at line 408 of file shell.cpp.
References count(), ucommon::shell::Option::first(), ucommon::shell::Option::help_string, ucommon::is(), ucommon::shell::Option::long_option, printf(), ucommon::shell::Option::short_option, text(), ucommon::shell::Option::trigger_option, and ucommon::shell::Option::uses_option.
Referenced by main(), and ucommon::shell::Option::Option().
|
static |
Definition at line 1506 of file shell.cpp.
References ch, ucommon::echo(), ucommon::fsys::is_tty(), and ucommon::noecho().
Referenced by process().
|
inlinestatic |
Definition at line 869 of file shell.h.
Referenced by digest(), encodestream(), and getline().
bool ucommon::shell::is_sym | ( | const char * | name | ) | const |
Test if symbol exists.
name | of symbol. |
Definition at line 2433 of file shell.cpp.
References ucommon::Mutex::acquire(), ucommon::eq(), ucommon::is(), ucommon::shell::syms::name, ucommon::Mutex::release(), and ucommon::symlock.
|
static |
Set logging level and state.
name | of logging entity. |
level | of error conditions to log. |
mode | of logging. |
handler | for log messages. |
Definition at line 2334 of file shell.cpp.
References ucommon::errlevel, ucommon::errmode, ucommon::errname, and ucommon::errproc.
|
static |
Print generic error message at specific error level.
level | of error condition. |
format | string to use. |
Definition at line 2367 of file shell.cpp.
References cpr_runtime_error(), DEBUG0, ucommon::errlevel, ucommon::errmode, ucommon::errname, ucommon::errproc, FAIL, and NONE.
|
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. |
Definition at line 489 of file shell.cpp.
References ucommon::mempager::_alloc(), _argc, _argv, ucommon::OrderedIndex::begin(), collapse(), ucommon::dup(), ucommon::OrderedObject::enlist(), ucommon::shell::args::item, quote, and set0().
Referenced by shell().
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. |
Definition at line 552 of file shell.cpp.
References ucommon::_orig, argv(), getargv(), and getargv0().
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. |
Definition at line 1551 of file shell.cpp.
References ucommon::_domain, ucommon::dir::create(), getenv(), ucommon::fsys::OWNER_PRIVATE, PROGRAM_CONFIG, PROGRAM_PLUGINS, PROGRAM_TEMP, result(), SERVICE_CACHE, SERVICE_CONFIG, SERVICE_CONTROL, SERVICE_DATA, ucommon::str(), SYSTEM_CFG, SYSTEM_ETC, SYSTEM_PREFIX, ucommon::system_prefix, SYSTEM_SHARE, SYSTEM_TEMP, SYSTEM_VAR, USER_CACHE, USER_CONFIG, USER_DATA, USER_DEFAULTS, and USER_HOME.
Referenced by bind(), detach(), ucommon::secure::oscerts(), path(), and spawn().
Join a prefix with a path.
prefix | to merge with. |
directory | or file path to merge. |
Definition at line 2489 of file shell.cpp.
References prefix, and ucommon::str().
|
static |
|
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. |
Definition at line 2143 of file shell.cpp.
References ucommon::memalloc::max(), and ucommon::min().
|
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. |
Definition at line 2130 of file shell.cpp.
References ucommon::_domain, and textdomain.
|
static |
|
static |
Set relative prefix. Used for OS/X relocatable applications.
argv0 | path of executable. |
Definition at line 1520 of file shell.cpp.
References argv0(), ucommon::eq(), and ucommon::system_prefix.
void ucommon::shell::restart | ( | char * | argv0, |
char ** | argv, | ||
char ** | list | ||
) |
void ucommon::shell::restart | ( | void | ) |
|
static |
Print security error message at specific error level.
level | of error condition. |
format | string to use. |
Definition at line 2344 of file shell.cpp.
References cpr_runtime_error(), DEBUG0, ucommon::errmode, ucommon::errname, FAIL, and NONE.
|
private |
Set argv0. This gets the simple filename of argv[0].
Definition at line 291 of file shell.cpp.
References _argv0, ucommon::_domain, _exedir, ucommon::String::add(), argv0(), bind(), ucommon::dup(), ucommon::eq(), ucommon::eq_case(), prefix, ucommon::fsys::prefix(), and ucommon::String::set().
Referenced by getargv0(), and parse().
|
static |
Definition at line 387 of file shell.cpp.
References ucommon::numeric_mode, and ucommon::numeric_value.
void ucommon::shell::setsym | ( | const char * | name, |
const char * | value | ||
) |
Set a local symbol.
name | of symbol to set. |
value | of symbol to set. |
Definition at line 2468 of file shell.cpp.
References ucommon::mempager::_alloc(), _syms, ucommon::Mutex::acquire(), ucommon::dup(), ucommon::LinkedObject::enlist(), ucommon::eq(), ucommon::is(), ucommon::shell::syms::name, ucommon::Mutex::release(), ucommon::symlock, and ucommon::shell::syms::value.
|
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. |
Definition at line 2012 of file shell.cpp.
References argv(), INVALID_HANDLE_VALUE, INVALID_PID_VALUE, ucommon::memalloc::max(), path(), and ucommon::String::set().
Referenced by ucommon::fsys::exec(), and ucommon::pipestream::open().
|
static |
A shell system call. This uses the native system shell to invoke the command.
command | string.. |
env | array to optionally use. |
Definition at line 1674 of file shell.cpp.
References ucommon::memalloc::max(), and ucommon::String::set().
Referenced by systemf().
|
static |
|
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. |
Definition at line 2104 of file shell.cpp.
References gettext.
Referenced by ucommon::__TEXT(), ucommon::_TEXT(), ucommon::shell::stringopt::assign(), errmsg(), getargv(), and help().
|
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.
Definition at line 1541 of file shell.cpp.
References getenv(), and ucommon::str().
|
static |
Wait for a child process to terminate. This operation blocks.
pid | of process to wait for. |
Definition at line 2070 of file shell.cpp.
References INVALID_PID_VALUE, and WEXITSTATUS.
Referenced by cancel(), ucommon::pipestream::close(), and ucommon::fsys::exec().
|
private |
|
private |
|
private |
|
private |
|
private |