29 #ifndef _UCOMMON_STRING_H_
33 #ifndef _UCOMMON_MEMORY_H_
37 #ifndef _UCOMMON_BUFFER_H_
41 #ifndef _UCOMMON_SHELL_H_
42 #define _UCOMMON_SHELL_H_
45 #define INVALID_PID_VALUE INVALID_HANDLE_VALUE
47 #define INVALID_PID_VALUE -1
97 void set0(
char *argv0);
103 typedef enum {NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET} errmsg_t;
108 typedef enum {NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG} logmode_t;
113 typedef enum {FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0} loglevel_t;
118 typedef enum {NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL} numeric_t;
124 PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL,
125 USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
126 SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
127 SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS,
128 PROGRAM_TEMP} path_t;
133 typedef bool (*logproc_t)(
loglevel_t level,
const char *text);
143 typedef void (*exitproc_t)(void);
146 typedef HANDLE
pid_t;
160 static const char *errmsg(
errmsg_t id);
168 static void errmsg(
errmsg_t id,
const char *text);
194 const char *long_option;
195 const char *uses_option;
196 const char *help_string;
206 Option(
char short_option = 0,
const char *long_option = NULL,
const char *value_type = NULL,
const char *help = NULL);
223 virtual const char *assign(
const char *value) = 0;
225 static void reset(
void);
241 virtual const char *assign(
const char *value);
244 flagopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
bool single_use =
true);
246 inline operator bool()
const
247 {
return counter > 0;}
249 inline bool operator!()
const
250 {
return counter == 0;}
252 inline operator unsigned()
const
255 inline unsigned operator*()
const
258 inline void set(
unsigned value = 1)
270 virtual const char *assign(
const char *value);
290 virtual const char *assign(
const char *value);
293 stringopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"text",
const char *def_text = NULL);
295 inline void set(
const char *
string)
298 inline operator bool()
const
301 inline bool operator!()
const
304 inline operator const char *()
const
307 inline const char *operator*()
const
325 virtual const char *assign(
const char *value);
328 charopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"char",
char default_code =
' ');
330 inline void set(
char value)
333 inline operator bool()
const
336 inline bool operator!()
const
339 inline operator char()
const
342 inline char operator*()
const
360 virtual const char *assign(
const char *value);
363 numericopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
365 inline void set(
long value)
368 inline operator bool()
const
371 inline bool operator!()
const
374 inline operator long()
const
377 inline long operator*()
const
397 virtual const char *assign(
const char *value);
400 counteropt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
402 inline void set(
long value)
405 inline operator bool()
const
408 inline bool operator!()
const
411 inline operator long()
const
414 inline long operator*()
const
425 shell(
const char *
string,
size_t pagesize = 0);
435 shell(
int argc,
char **argv,
size_t pagesize = 0);
441 shell(
size_t pagesize = 0);
445 static long getNumeric(
void);
450 static void help(
void);
459 static int system(
const char *command,
const char **env = NULL);
467 static int systemf(
const char *format, ...) __PRINTF(1,2);
473 static
void relocate(const
char *argv0);
487 static
String userid(
void);
502 static
String path(
String& prefix, const
char *directory);
515 static
void bind(const
char *name);
526 static
void rebind(const
char *name = NULL);
533 char **parse(const
char *
string);
543 void parse(
int argc,
char **argv);
552 const
char *env(const
char *name, const
char *value = NULL);
554 inline const
char *getenv(const
char *name, const
char *value = NULL)
555 {
return env(name, value);}
563 const char *
get(
const char *name,
const char *value = NULL);
565 inline const char *getsym(
const char *name,
const char *value = NULL)
566 {
return get(name, value);}
573 void set(
const char *name,
const char *value);
575 inline void setsym(
const char *name,
const char *value)
576 {
return set(name, value);}
583 bool is_sym(
const char *name)
const;
590 char *getargv0(
char **argv);
599 char **getargv(
char **argv);
607 void restart(
char *argv0,
char **argv,
char **list);
625 static void error(
const char *format, ...) __PRINTF(1, 2);
627 static
void errlog(const
char *format, ...) __PRINTF(1, 2);
634 static
void errexit(
int exitcode, const
char *format = NULL, ...) __PRINTF(2, 3);
642 static inline
int condition(
bool test,
int exitcode)
643 {
return (test) ? exitcode : 0;}
650 static void debug(
unsigned level,
const char *format, ...) __PRINTF(2, 3);
657 static
void log(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
664 static
void security(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
673 static
void log(const
char *name, loglevel_t level = ERR, logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
679 static
size_t printf(const
char *format, ...) __PRINTF(1, 2);
681 static
size_t readln(
char *address,
size_t size);
683 static
size_t writes(const
char *
string);
685 static
size_t read(
String&
string);
687 inline static
size_t write(
String&
string)
688 {
return writes(
string.c_str());}
695 inline unsigned argc(
void)
const
713 {
return _argv[offset];}
715 static void exiting(exitproc_t);
720 void detach(mainproc_t mainentry = (mainproc_t)NULL);
738 static shell::pid_t spawn(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
748 static void priority(
int pri = 1);
759 static int detach(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
765 static void release(
int exit_code = 0);
800 static const char *text(
const char *
string);
811 static const char *texts(
const char *singular,
const char *plural,
unsigned long count);
818 static unsigned count(
char **argv);
822 static inline fd_t input(
void)
823 {
return GetStdHandle(STD_INPUT_HANDLE);}
825 static inline fd_t output(
void)
826 {
return GetStdHandle(STD_OUTPUT_HANDLE);}
828 static inline fd_t error(
void)
829 {
return GetStdHandle(STD_ERROR_HANDLE);}
832 static inline fd_t input(
void)
835 static inline fd_t output(
void)
838 static inline fd_t error(
void)
842 static int inkey(
const char *prompt = NULL);
844 static char *getpass(
const char *prompt,
char *buffer,
size_t size);
846 static char *getline(
const char *prompt,
char *buffer,
size_t size);
860 #define _STR(x) (const char *)(x)
869 inline const char *
_TEXT(
const char *s)
A common string class and character string support functions.
unsigned argc(void) const
Get saved internal argc count for items.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
numeric_t
Numeric mode of parser.
Character option for shell parsing.
const char * execdir() const
Get the exec directory.
Flag option for shell parsing.
int pid_t
Standard type of process id for shell class.
A managed private heap for small allocations.
Common base class for all objects that can be formed into a linked list.
unsigned operator()(void) const
Return argc count.
A linked object base class for ordered objects.
A copy-on-write string class that operates by reference count.
Private heaps, pools, and associations.
A utility class for generic shell operations.
static const char * text(const char *string)
Text translation and localization.
char ** argv(void) const
Get saved internal argv count for items in this shell object.
A class to redefine error messages.
errmsg_t
Error table index.
Text option for shell parsing.
Numeric option for shell parsing.
const char * argv0() const
Get program name (argv0).
Common namespace for all ucommon objects.
static const char * errmsg(errmsg_t id)
This can be used to get internationalized error messages.
const char * operator[](unsigned offset)
Return parser argv element.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
const char * _TEXT(const char *s)
Invoke translation lookup if available.
A base class used to create parsable shell options.
shell shell_t
Convenience type to manage and pass shell objects.
Classes which use the buffer protocol to stream data.
path_t
Path types to retrieve.
loglevel_t
Level of error logging.
Counter option for shell parsing.
Automatic integer counting class.
cpr_service_t mainproc_t
Main handler.