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
62 class __EXPORT shell :
public mempager
71 class __LOCAL args :
public OrderedObject
77 class __LOCAL syms :
public LinkedObject
89 void collapse(LinkedObject *first);
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);
135 typedef cpr_service_t mainproc_t;
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;
162 static const char *errmsg(errmsg_t
id);
170 static void errmsg(errmsg_t
id,
const char *text);
178 class __EXPORT errormap
181 inline errormap(errmsg_t
id,
const char *text)
182 {shell::errmsg(
id, text);};
192 class __EXPORT Option :
public OrderedObject
196 const char *long_option;
197 const char *uses_option;
198 const char *help_string;
208 Option(
char short_option = 0,
const char *long_option = NULL,
const char *value_type = NULL,
const char *help = NULL);
212 static LinkedObject *first(
void);
225 virtual const char *assign(
const char *value) = 0;
227 static void reset(
void);
237 class __EXPORT flagopt :
public Option
243 virtual const char *assign(
const char *value);
246 flagopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
bool single_use =
true);
248 inline operator bool()
249 {
return counter > 0;};
251 inline bool operator!()
252 {
return counter == 0;};
254 inline operator unsigned()
257 inline unsigned operator*()
260 inline void set(
unsigned value = 1)
269 class __EXPORT groupopt :
public Option
272 virtual const char *assign(
const char *value);
275 groupopt(
const char *help);
284 class __EXPORT stringopt :
public Option
292 virtual const char *assign(
const char *value);
295 stringopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"text",
const char *def_text = NULL);
297 inline void set(
const char *string)
300 inline operator bool()
303 inline bool operator!()
306 inline operator const char *()
309 inline const char *operator*()
319 class __EXPORT charopt :
public Option
327 virtual const char *assign(
const char *value);
330 charopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"char",
char default_code =
' ');
332 inline void set(
char value)
335 inline operator bool()
338 inline bool operator!()
341 inline operator char()
344 inline char operator*()
354 class __EXPORT numericopt :
public Option
362 virtual const char *assign(
const char *value);
365 numericopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
367 inline void set(
long value)
370 inline operator bool()
373 inline bool operator!()
376 inline operator long()
379 inline long operator*()
391 class __EXPORT counteropt :
public Option
399 virtual const char *assign(
const char *value);
402 counteropt(
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)
407 inline operator bool()
410 inline bool operator!()
413 inline operator long()
416 inline long operator*()
427 shell(
const char *
string,
size_t pagesize = 0);
437 shell(
int argc,
char **argv,
size_t pagesize = 0);
443 shell(
size_t pagesize = 0);
445 static void setNumeric(numeric_t);
447 static long getNumeric(
void);
452 static void help(
void);
461 static int system(
const char *command,
const char **env = NULL);
469 static int systemf(
const char *format, ...) __PRINTF(1,2);
475 static
void relocate(const
char *argv0);
483 static
String path(path_t
id);
489 static
String userid(
void);
497 static
String path(path_t
id, const
char *directory);
504 static
String path(
String& prefix, const
char *directory);
517 static
void bind(const
char *name);
528 static
void rebind(const
char *name = NULL);
535 char **parse(const
char *
string);
545 void parse(
int argc,
char **argv);
554 const
char *getenv(const
char *name, const
char *value = NULL);
562 const
char *getsym(const
char *name, const
char *value = NULL);
569 void setsym(const
char *name, const
char *value);
576 bool issym(const
char *name);
583 char *getargv0(
char **argv);
592 char **getargv(
char **argv);
600 void restart(
char *argv0,
char **argv,
char **list);
608 inline static
char **parse(shell &args, const
char *
string)
609 {
return args.parse(
string);}
614 inline const char *argv0()
const
620 inline const char *execdir()
const
627 static void error(
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
704 inline char **argv(
void)
const
712 inline const char *operator[](
unsigned offset)
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);
772 static int wait(shell::pid_t pid);
779 static int cancel(shell::pid_t pid);
785 inline unsigned operator()(
void)
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);
853 typedef shell shell_t;
868 inline const char *_TEXT(
const char *s)
869 {
return shell::text(s);}
871 inline const char *_STR(
String& s)