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;
129 typedef bool (*logproc_t)(
loglevel_t level,
const char *text);
139 typedef void (*exitproc_t)(void);
142 typedef HANDLE
pid_t;
176 {shell::errmsg(
id, text);}
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);
240 virtual const char *assign(
const char *value);
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) {
281 virtual const char *assign(
const char *value);
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 {
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 {
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 {
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);
496 shell(
int argc,
char **argv,
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);
534 static
void relocate(const
char *argv0);
576 static
void bind(const
char *name);
587 static
void rebind(const
char *name = NULL);
594 char **parse(const
char *
string);
604 void parse(
int argc,
char **argv);
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);
635 bool is_sym(const
char *name) const;
642 char *getargv0(
char **argv);
651 char **getargv(
char **argv);
659 void restart(
char *argv0,
char **argv,
char **list);
664 inline const
char *argv0()
const
677 static void errlog(
const char *format, ...) __PRINTF(1, 2);
684 static
void errexit(
int exitcode, const
char *format = NULL, ...) __PRINTF(2, 3);
691 static inline
int condition(
bool test,
int exitcode) {
692 return (test) ? exitcode : 0;
700 static void debug(
unsigned level,
const char *format, ...) __PRINTF(2, 3);
707 static
void log(
loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
714 static
void security(
loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
723 static
void log(const
char *name,
loglevel_t level = ERR,
logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
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);
775 static shell::pid_t spawn(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
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.
Automatic integer counting class.
Common base class for all objects that can be formed into a linked list.
A linked object base class for ordered objects.
A managed private heap for small allocations.
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 unsigned count(char **argv)
Get argc count for an existing array.
shell(size_t pagesize=0)
Construct an empty shell parser argument list.
static const char * text(const char *string)
Text translation and localization.
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.
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.
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.
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.
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.
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.
static int systemf(const char *format,...)
A shell system call that can be issued using a formatted string.
static void debug(unsigned level, const char *format,...)
Print a debug message by debug level.
A class to redefine error messages.
A base class used to create parsable shell options.
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.
Flag option for shell parsing.
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.