28 #ifndef _UCOMMON_STRING_H_
32 #ifndef _UCOMMON_MEMORY_H_
36 #ifndef _UCOMMON_BUFFER_H_
40 #ifndef _UCOMMON_SHELL_H_
41 #define _UCOMMON_SHELL_H_
44 #define INVALID_PID_VALUE INVALID_HANDLE_VALUE
46 #define INVALID_PID_VALUE -1
94 void set0(
char *argv0);
100 typedef enum {NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET} errmsg_t;
105 typedef enum {NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG} logmode_t;
110 typedef enum {FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0} loglevel_t;
115 typedef enum {NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL} numeric_t;
121 PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL,
122 USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
123 SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
124 SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS,
125 PROGRAM_TEMP} path_t;
130 typedef bool (*logproc_t)(
loglevel_t level,
const char *text);
140 typedef void (*exitproc_t)(void);
143 typedef HANDLE
pid_t;
154 typedef enum {RD = BufferProtocol::BUF_RD, WR = BufferProtocol::BUF_WR, RDWR = BufferProtocol::BUF_RDWR} pmode_t;
183 int spawn(
const char *path,
char **argv,
pmode_t mode,
size_t size = 512,
char **env = NULL);
206 size_t read(
void *address,
size_t size);
216 size_t write(
const void *address,
size_t size);
236 virtual int _err(
void)
const;
237 virtual void _clear(
void);
239 virtual size_t _push(
const char *address,
size_t size);
240 virtual size_t _pull(
char *address,
size_t size);
248 iobuf(
size_t size = 0);
260 iobuf(
const char *path,
char **argv,
pmode_t mode,
size_t size = 512,
char **env = NULL);
276 void open(
const char *path,
char **argv,
pmode_t mode,
size_t size = 512,
char **env = NULL);
327 {shell::errmsg(
id, text);};
341 const char *long_option;
342 const char *uses_option;
343 const char *help_string;
353 Option(
char short_option = 0,
const char *long_option = NULL,
const char *value_type = NULL,
const char *help = NULL);
370 virtual const char *assign(
const char *value) = 0;
372 static void reset(
void);
388 virtual const char *assign(
const char *value);
391 flagopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
bool single_use =
true);
393 inline operator bool()
396 inline bool operator!()
399 inline operator unsigned()
402 inline unsigned operator*()
405 inline void set(
unsigned value = 1)
417 virtual const char *assign(
const char *value);
437 virtual const char *assign(
const char *value);
440 stringopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"text",
const char *def_text = NULL);
442 inline void set(
const char *
string)
445 inline operator bool()
448 inline bool operator!()
451 inline operator const char *()
454 inline const char *operator*()
472 virtual const char *assign(
const char *value);
475 charopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"char",
char default_code =
' ');
477 inline void set(
char value)
480 inline operator bool()
483 inline bool operator!()
486 inline operator char()
489 inline char operator*()
507 virtual const char *assign(
const char *value);
510 numericopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
512 inline void set(
long value)
515 inline operator bool()
518 inline bool operator!()
521 inline operator long()
524 inline long operator*()
544 virtual const char *assign(
const char *value);
547 counteropt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
549 inline void set(
long value)
552 inline operator bool()
555 inline bool operator!()
558 inline operator long()
561 inline long operator*()
572 shell(
const char *
string,
size_t pagesize = 0);
582 shell(
int argc,
char **argv,
size_t pagesize = 0);
588 shell(
size_t pagesize = 0);
592 static long getNumeric(
void);
597 static void help(
void);
606 static int system(
const char *command,
const char **env = NULL);
614 static int systemf(
const char *format, ...) __PRINTF(1,2);
620 static
void relocate(const
char *argv0);
634 static
String userid(
void);
649 static
String path(
String& prefix, const
char *directory);
662 static
void bind(const
char *name);
673 static
void rebind(const
char *name = NULL);
680 char **parse(const
char *
string);
690 void parse(
int argc,
char **argv);
699 const
char *getenv(const
char *name, const
char *value = NULL);
707 const
char *getsym(const
char *name, const
char *value = NULL);
714 void setsym(const
char *name, const
char *value);
721 bool issym(const
char *name);
728 char *getargv0(
char **argv);
737 char **getargv(
char **argv);
745 void restart(
char *argv0,
char **argv,
char **list);
753 inline static
char **parse(
shell &args, const
char *
string)
754 {
return args.parse(
string);}
772 static void error(
const char *format, ...) __PRINTF(1, 2);
779 static
void errexit(
int exitcode, const
char *format = NULL, ...) __PRINTF(2, 3);
787 static inline
int condition(
bool test,
int exitcode)
788 {
return (test) ? exitcode : 0;};
795 static void debug(
unsigned level,
const char *format, ...) __PRINTF(2, 3);
802 static
void log(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
809 static
void security(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
818 static
void log(const
char *name, loglevel_t level = ERR, logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
824 static
size_t printf(const
char *format, ...) __PRINTF(1, 2);
826 static
size_t readln(
char *address,
size_t size);
828 static
size_t writes(const
char *
string);
830 static
size_t read(
String&
string);
832 inline static
size_t write(
String&
string)
833 {
return writes(
string.c_str());};
841 static size_t printf(pipe_t pipe,
const char *format, ...) __PRINTF(2, 3);
850 static
size_t readln(pipe_t pipe,
char *buffer,
size_t size);
852 static
size_t read(pipe_t pipe,
String&
string);
854 static
size_t writes(pipe_t pipe, const
char *
string);
856 inline static
size_t write(pipe_t pipe,
String&
string)
857 {
return writes(pipe,
string.c_str());};
864 inline unsigned argc(
void)
const
882 {
return _argv[offset];};
884 static void exiting(exitproc_t);
889 void detach(mainproc_t mainentry = (mainproc_t)NULL);
907 static shell::pid_t spawn(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
920 static shell::pipe_t spawn(
const char *path,
char **argv, pmode_t mode,
size_t size = 512,
char **env = NULL);
941 static int detach(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
947 static void release(
int exit_code = 0);
997 static const char *
text(
const char *
string);
1008 static const char *
texts(
const char *singular,
const char *plural,
unsigned long count);
1015 static unsigned count(
char **argv);
1019 static inline fd_t input(
void)
1020 {
return GetStdHandle(STD_INPUT_HANDLE);};
1022 static inline fd_t output(
void)
1023 {
return GetStdHandle(STD_OUTPUT_HANDLE);};
1025 static inline fd_t error(
void)
1026 {
return GetStdHandle(STD_ERROR_HANDLE);};
1029 static inline fd_t input(
void)
1032 static inline fd_t output(
void)
1035 static inline fd_t error(
void)
1039 static int inkey(
const char *prompt = NULL);
1041 static char *getpass(
const char *prompt,
char *buffer,
size_t size);
1043 static char *getline(
const char *prompt,
char *buffer,
size_t size);
1066 {
return shell::text(s);}
1068 inline const char *_STR(
String& s)