29#ifndef _UCOMMON_STRING_H_
33#ifndef _UCOMMON_MEMORY_H_
37#ifndef _UCOMMON_SHELL_H_
38#define _UCOMMON_SHELL_H_
41#define INVALID_PID_VALUE INVALID_HANDLE_VALUE
43#define INVALID_PID_VALUE -1
93 void set0(
char *
argv0);
99 typedef enum {NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET} errmsg_t;
104 typedef enum {NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG} logmode_t;
109 typedef enum {FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0} loglevel_t;
114 typedef enum {NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL} numeric_t;
120 PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL,
121 USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
122 SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
123 SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS,
124 PROGRAM_TEMP} path_t;
142 typedef HANDLE
pid_t;
172 class __EXPORT errormap
189 __DELETE_COPY(Option);
193 const char *long_option;
194 const char *uses_option;
195 const char *help_string;
205 Option(
char short_option = 0,
const char *long_option = NULL,
const char *value_type = NULL,
const char *
help = NULL);
222 virtual const char *
assign(
const char *value) = 0;
224 static void reset(
void);
234 class __EXPORT flagopt :
public Option
240 virtual const char *assign(
const char *value);
242 __DELETE_DEFAULTS(flagopt);
245 flagopt(
char short_option,
const char *long_option = NULL,
const char *
help = NULL,
bool single_use =
true);
247 inline operator bool()
const {
251 inline bool operator!()
const {
255 inline operator unsigned()
const {
259 inline unsigned operator*()
const {
263 inline void set(
unsigned value = 1) {
267 inline flagopt& operator=(
unsigned value) {
278 class __EXPORT groupopt :
public Option
281 virtual const char *assign(
const char *value);
283 __DELETE_DEFAULTS(groupopt);
286 groupopt(
const char *
help);
295 class __EXPORT stringopt :
public Option
300 __DELETE_DEFAULTS(stringopt);
305 virtual const char *
assign(
const char *value);
308 stringopt(
char short_option,
const char *long_option = NULL,
const char *
help = NULL,
const char *type =
"text",
const char *def_text = NULL);
310 inline void set(
const char *
string) {
314 inline stringopt& operator=(
const char *
string) {
319 inline operator bool()
const {
323 inline bool operator!()
const {
327 inline operator const char *()
const {
331 inline const char *operator*()
const {
342 class __EXPORT charopt :
public Option
347 __DELETE_DEFAULTS(charopt);
352 virtual const char *
assign(
const char *value);
355 charopt(
char short_option,
const char *long_option = NULL,
const char *
help = NULL,
const char *type =
"char",
char default_code =
' ');
357 inline void set(
char value) {
361 inline charopt& operator=(
char value) {
366 inline operator bool()
const {
370 inline bool operator!()
const {
374 inline operator char()
const {
378 inline char operator*()
const {
389 class __EXPORT numericopt :
public Option
394 __DELETE_DEFAULTS(numericopt);
399 virtual const char *
assign(
const char *value);
402 numericopt(
char short_option,
const char *long_option = NULL,
const char *
help = NULL,
const char *type =
"numeric",
long def_value = 0);
404 inline void set(
long value) {
413 inline operator bool()
const {
417 inline bool operator!()
const {
421 inline operator long()
const {
425 inline long operator*()
const {
438 class __EXPORT counteropt :
public Option
443 __DELETE_DEFAULTS(counteropt);
448 virtual const char *
assign(
const char *value);
451 counteropt(
char short_option,
const char *long_option = NULL,
const char *
help = NULL,
const char *type =
"numeric",
long def_value = 0);
453 inline void set(
long value) {
462 inline operator bool()
const {
466 inline bool operator!()
const {
470 inline operator long()
const {
474 inline long operator*()
const {
486 shell(
const char *
string,
size_t pagesize = 0);
506 static long getNumeric(
void);
520 static int system(
const char *command,
const char **env = NULL);
528 static int systemf(
const char *format, ...) __PRINTF(1,2);
576 static
void bind(const
char *name);
587 static
void rebind(const
char *name = NULL);
613 const
char *
getenv(const
char *name, const
char *value = NULL);
621 const
char *
getsym(const
char *name, const
char *value = NULL);
628 void setsym(const
char *name, const
char *value);
677 static void errlog(
const char *format, ...) __PRINTF(1, 2);
684 static
void errexit(
int exitcode, const
char *format = NULL, ...) __PRINTF(2, 3);
692 return (test) ? exitcode : 0;
700 static void debug(
unsigned level,
const char *format, ...) __PRINTF(2, 3);
725 static
size_t printf(const
char *format, ...) __PRINTF(1, 2);
732 inline
unsigned argc(
void)
const
750 {
return _argv[offset];}
752 static void exiting(exitproc_t);
796 static int detach(
const char *
path,
char **
argv,
char **env = NULL, fd_t *stdio = NULL);
837 static const char *
text(
const char *
string);
848 static const char *
texts(
const char *singular,
const char *plural,
unsigned long count);
859 static inline fd_t input(
void)
860 {
return GetStdHandle(STD_INPUT_HANDLE);}
862 static inline fd_t output(
void)
863 {
return GetStdHandle(STD_OUTPUT_HANDLE);}
865 static inline fd_t error(
void)
866 {
return GetStdHandle(STD_ERROR_HANDLE);}
869 static inline fd_t input(
void)
872 static inline fd_t output(
void)
875 static inline fd_t
error(
void)
879 static int inkey(
const char *prompt = NULL);
881 static char *getpass(
const char *prompt,
char *buffer,
size_t size);
883 static char *getline(
const char *prompt,
char *buffer,
size_t size);
898#define _STR(x) ((const char *)(x))
899#define __STR(x) (static_cast<const char *>(x))
911inline const char *_TEXT(
const char *s)
Private heaps, pools, and associations.
AppLog & error(AppLog &sl)
Manipulator for error level.
Common namespace for all ucommon objects.
const char * __TEXT(const char *s)
Invoke translation lookup if available.
shell shell_t
Convenience type to manage and pass shell objects.
Common base class for all objects that can be formed into a linked list.
LinkedObject(LinkedObject **root)
Construct base class attached to a chain of objects.
A linked object base class for ordered objects.
mempager(size_t page=0)
Construct a memory pager.
A utility class for generic shell operations.
void restart(void)
Make current process restartable.
static void errmsg(errmsg_t id, const char *text)
This is used to set internationalized error messages for the shell parser.
static int cancel(shell::pid_t pid)
Cancel a child process.
loglevel_t
Level of error logging.
static void rebind(const char *name=NULL)
Rebind is used to change the text domain.
static unsigned count(char **argv)
Get argc count for an existing array.
bool is_sym(const char *name) const
Test if symbol exists.
shell(size_t pagesize=0)
Construct an empty shell parser argument list.
const char * getsym(const char *name, const char *value=NULL)
Get a local symbol.
static const char * text(const char *string)
Text translation and localization.
char ** getargv(char **argv)
Get the argument list by parsing options, and return the remaining file arguments.
const char * operator[](unsigned offset)
Return parser argv element.
static void help(void)
Display shell options.
int pid_t
Standard type of process id for shell class.
cpr_service_t mainproc_t
Main handler.
const char * getenv(const char *name, const char *value=NULL)
Get an environment variable.
void restart(char *argv0, char **argv, char **list)
Execute front-end like gdb based on stripped first argument.
static String path(path_t id)
Get a system path.
static int system(const char *command, const char **env=NULL)
A shell system call.
logmode_t
Type of error logging we are using.
unsigned operator()(void) const
Return argc count.
static const char * texts(const char *singular, const char *plural, unsigned long count)
Plural text translation and localization.
static const char * errmsg(errmsg_t id)
This can be used to get internationalized error messages.
void(* exitproc_t)(void)
Exit handler.
static void relocate(const char *argv0)
Set relative prefix.
static void security(loglevel_t level, const char *format,...)
Print security error message at specific error level.
errmsg_t
Error table index.
static int detach(const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL)
Create a detached process.
static void release(int exit_code=0)
Detach and release from parent process with exit code.
char * getargv0(char **argv)
Parse and extract the argv0 filename alone.
const char * execdir() const
Get the exec directory.
static int wait(shell::pid_t pid)
Wait for a child process to terminate.
numeric_t
Numeric mode of parser.
shell(int argc, char **argv, size_t pagesize=0)
Construct a shell argument list from existing arguments.
static String userid(void)
Get the system login id.
shell(const char *string, size_t pagesize=0)
Construct a shell argument list by parsing a simple command string.
path_t
Path types to retrieve.
char ** argv(void) const
Get saved internal argv count for items in this shell object.
static shell::pid_t spawn(const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL)
Spawn a child process.
static void errlog(const char *format,...)
Print error message and continue.
unsigned argc(void) const
Get saved internal argc count for items.
void detach(mainproc_t mainentry=(mainproc_t) NULL)
Detach current process to daemon for service entry.
static void priority(int pri=1)
Set priority level and enable priority scheduler.
bool(* logproc_t)(loglevel_t level, const char *text)
Log process handler.
static int systemf(const char *format,...)
A shell system call that can be issued using a formatted string.
static int condition(bool test, int exitcode)
Convert condition to exit code if true.
char ** parse(const char *string)
Parse a string as a series of arguments for use in exec calls.
static void errexit(int exitcode, const char *format=NULL,...)
Print error message and exit.
void setsym(const char *name, const char *value)
Set a local symbol.
const char * argv0() const
Get program name (argv0).
static void debug(unsigned level, const char *format,...)
Print a debug message by debug level.
static void log(loglevel_t level, const char *format,...)
Print generic error message at specific error level.
static void bind(const char *name)
Bind application to text domain for internationalization.
Option(char short_option=0, const char *long_option=NULL, const char *value_type=NULL, const char *help=NULL)
Construct a shell parser option.
virtual const char * assign(const char *value)=0
Used to send option into derived receiver.
void disable(void)
Disable a option.
Text option for shell parsing.
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Character option for shell parsing.
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Numeric option for shell parsing.
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Counter option for shell parsing.
virtual const char * assign(const char *value)
Used to send option into derived receiver.
A copy-on-write string class that operates by reference count.
A common string class and character string support functions.