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;
157 static const char *errmsg(errmsg_t
id);
165 static void errmsg(errmsg_t
id,
const char *text);
173 class __EXPORT errormap
176 inline errormap(errmsg_t
id,
const char *text)
177 {shell::errmsg(
id, text);}
187 class __EXPORT Option :
public LinkedObject
191 const char *long_option;
192 const char *uses_option;
193 const char *help_string;
203 Option(
char short_option = 0,
const char *long_option = NULL,
const char *value_type = NULL,
const char *help = NULL);
207 static LinkedObject *first(
void);
220 virtual const char *assign(
const char *value) = 0;
222 static void reset(
void);
232 class __EXPORT flagopt :
public Option
238 virtual const char *assign(
const char *value);
241 flagopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
bool single_use =
true);
243 inline operator bool()
const
244 {
return counter > 0;}
246 inline bool operator!()
const
247 {
return counter == 0;}
249 inline operator unsigned()
const
252 inline unsigned operator*()
const
255 inline void set(
unsigned value = 1)
264 class __EXPORT groupopt :
public Option
267 virtual const char *assign(
const char *value);
270 groupopt(
const char *help);
279 class __EXPORT stringopt :
public Option
287 virtual const char *assign(
const char *value);
290 stringopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"text",
const char *def_text = NULL);
292 inline void set(
const char *
string)
295 inline operator bool()
const
298 inline bool operator!()
const
301 inline operator const char *()
const
304 inline const char *operator*()
const
314 class __EXPORT charopt :
public Option
322 virtual const char *assign(
const char *value);
325 charopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"char",
char default_code =
' ');
327 inline void set(
char value)
330 inline operator bool()
const
333 inline bool operator!()
const
336 inline operator char()
const
339 inline char operator*()
const
349 class __EXPORT numericopt :
public Option
357 virtual const char *assign(
const char *value);
360 numericopt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
362 inline void set(
long value)
365 inline operator bool()
const
368 inline bool operator!()
const
371 inline operator long()
const
374 inline long operator*()
const
386 class __EXPORT counteropt :
public Option
394 virtual const char *assign(
const char *value);
397 counteropt(
char short_option,
const char *long_option = NULL,
const char *help = NULL,
const char *type =
"numeric",
long def_value = 0);
399 inline void set(
long value)
402 inline operator bool()
const
405 inline bool operator!()
const
408 inline operator long()
const
411 inline long operator*()
const
422 shell(
const char *
string,
size_t pagesize = 0);
432 shell(
int argc,
char **argv,
size_t pagesize = 0);
438 shell(
size_t pagesize = 0);
440 static void setNumeric(numeric_t);
442 static long getNumeric(
void);
447 static void help(
void);
456 static int system(
const char *command,
const char **env = NULL);
464 static int systemf(
const char *format, ...) __PRINTF(1,2);
470 static
void relocate(const
char *argv0);
478 static String path(path_t
id);
484 static String userid(
void);
492 static String path(path_t
id, const
char *directory);
499 static String path(String& prefix, const
char *directory);
512 static
void bind(const
char *name);
523 static
void rebind(const
char *name = NULL);
530 char **parse(const
char *
string);
540 void parse(
int argc,
char **argv);
549 const
char *env(const
char *name, const
char *value = NULL);
551 inline const
char *getenv(const
char *name, const
char *value = NULL)
552 {
return env(name, value);}
560 const char *
get(
const char *name,
const char *value = NULL);
562 inline const char *getsym(
const char *name,
const char *value = NULL)
563 {
return get(name, value);}
570 void set(
const char *name,
const char *value);
572 inline void setsym(
const char *name,
const char *value)
573 {
return set(name, value);}
580 bool is_sym(
const char *name)
const;
587 char *getargv0(
char **argv);
596 char **getargv(
char **argv);
604 void restart(
char *argv0,
char **argv,
char **list);
609 inline const char *argv0()
const
615 inline const char *execdir()
const
622 static void error(
const char *format, ...) __PRINTF(1, 2);
629 static
void errexit(
int exitcode, const
char *format = NULL, ...) __PRINTF(2, 3);
637 static inline
int condition(
bool test,
int exitcode)
638 {
return (test) ? exitcode : 0;}
645 static void debug(
unsigned level,
const char *format, ...) __PRINTF(2, 3);
652 static
void log(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
659 static
void security(loglevel_t level, const
char *format, ...) __PRINTF(2, 3);
668 static
void log(const
char *name, loglevel_t level = ERR, logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
674 static
size_t printf(const
char *format, ...) __PRINTF(1, 2);
676 static
size_t readln(
char *address,
size_t size);
678 static
size_t writes(const
char *
string);
680 static
size_t read(String&
string);
682 inline static
size_t write(String&
string)
683 {
return writes(
string.c_str());}
690 inline unsigned argc(
void)
const
699 inline char **argv(
void)
const
707 inline const char *operator[](
unsigned offset)
708 {
return _argv[offset];}
710 static void exiting(exitproc_t);
715 void detach(mainproc_t mainentry = (mainproc_t)NULL);
733 static shell::pid_t spawn(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
743 static void priority(
int pri = 1);
754 static int detach(
const char *path,
char **argv,
char **env = NULL, fd_t *stdio = NULL);
760 static void release(
int exit_code = 0);
767 static int wait(shell::pid_t pid);
774 static int cancel(shell::pid_t pid);
780 inline unsigned operator()(
void)
const
795 static const char *text(
const char *
string);
806 static const char *texts(
const char *singular,
const char *plural,
unsigned long count);
813 static unsigned count(
char **argv);
817 static inline fd_t input(
void)
818 {
return GetStdHandle(STD_INPUT_HANDLE);}
820 static inline fd_t output(
void)
821 {
return GetStdHandle(STD_OUTPUT_HANDLE);}
823 static inline fd_t error(
void)
824 {
return GetStdHandle(STD_ERROR_HANDLE);}
827 static inline fd_t input(
void)
830 static inline fd_t output(
void)
833 static inline fd_t error(
void)
837 static int inkey(
const char *prompt = NULL);
839 static char *getpass(
const char *prompt,
char *buffer,
size_t size);
841 static char *getline(
const char *prompt,
char *buffer,
size_t size);
848 typedef shell shell_t;
855 #define _STR(x) (const char *)(x)
864 inline const char *_TEXT(
const char *s)
865 {
return shell::text(s);}
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Common namespace for all ucommon objects.
Classes which use the buffer protocol to stream data.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
A common string class and character string support functions.
Private heaps, pools, and associations.