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;
121 USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
122 SERVICE_CACHE,
USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
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;
156 static const char *errmsg(
errmsg_t id);
164 static void errmsg(
errmsg_t id,
const char *text);
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 {
255 inline operator unsigned()
const {
263 inline void set(
unsigned value = 1) {
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) {
319 inline operator bool()
const {
327 inline operator const char *()
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) {
366 inline operator bool()
const {
374 inline operator char()
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 {
421 inline operator long()
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 {
470 inline operator long()
const {
486 shell(
const char *
string,
size_t pagesize = 0);
496 shell(
int argc,
char **argv,
size_t pagesize = 0);
502 shell(
size_t pagesize = 0);
504 static void setNumeric(numeric_t);
506 static long getNumeric(
void);
511 static void help(
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);
542 static
String path(path_t
id);
548 static
String userid(
void);
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);
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);
757 void detach(mainproc_t mainentry = (mainproc_t)NULL);
775 static shell::pid_t spawn(
const char *path,
char **argv,
char **env = NULL,
fd_t *stdio = NULL);
785 static void priority(
int pri = 1);
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);
855 static unsigned count(
char **argv);
860 {
return GetStdHandle(STD_INPUT_HANDLE);}
863 {
return GetStdHandle(STD_OUTPUT_HANDLE);}
866 {
return GetStdHandle(STD_ERROR_HANDLE);}
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)) 911 inline const char *
_TEXT(
const char *s)