ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

shell.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
29 #ifndef _UCOMMON_STRING_H_
30 #include <ucommon/string.h>
31 #endif
32 
33 #ifndef _UCOMMON_MEMORY_H_
34 #include <ucommon/memory.h>
35 #endif
36 
37 #ifndef _UCOMMON_SHELL_H_
38 #define _UCOMMON_SHELL_H_
39 
40 #ifdef _MSWINDOWS_
41 #define INVALID_PID_VALUE INVALID_HANDLE_VALUE
42 #else
43 #define INVALID_PID_VALUE -1
44 #endif
45 
46 #ifdef ERR
47 #undef ERR
48 #endif
49 
50 namespace ucommon {
51 
59 class __EXPORT shell : public mempager
60 {
61 private:
62  char **_argv;
63  unsigned _argc;
64  char *_argv0;
65  char *_exedir;
67 
69 
70  class __LOCAL args : public OrderedObject
71  {
72  public:
73  char *item;
74  };
75 
76  class __LOCAL syms : public LinkedObject
77  {
78  public:
79  const char *name;
80  const char *value;
81  };
82 
88  void collapse(LinkedObject *first);
89 
93  void set0(char *argv0);
94 
95 public:
99  typedef enum {NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET} errmsg_t;
100 
104  typedef enum {NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG} logmode_t;
105 
109  typedef enum {FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0} loglevel_t;
110 
114  typedef enum {NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL} numeric_t;
115 
119  typedef enum {
120  PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL,
121  USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
122  SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
123  SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS,
124  PROGRAM_TEMP} path_t;
125 
129  typedef bool (*logproc_t)(loglevel_t level, const char *text);
130 
135 
139  typedef void (*exitproc_t)(void);
140 
141 #ifdef _MSWINDOWS_
142  typedef HANDLE pid_t;
143 #else
144 
147  typedef int pid_t;
148 #endif
149 
156  static const char *errmsg(errmsg_t id);
157 
164  static void errmsg(errmsg_t id, const char *text);
165 
173  {
174  public:
175  inline errormap(errmsg_t id, const char *text)
176  {shell::errmsg(id, text);}
177  };
178 
186  class __EXPORT Option : public LinkedObject
187  {
188  private:
190 
191  public:
193  const char *long_option;
194  const char *uses_option;
195  const char *help_string;
197 
205  Option(char short_option = 0, const char *long_option = NULL, const char *value_type = NULL, const char *help = NULL);
206 
207  virtual ~Option();
208 
209  static LinkedObject *first(void);
210 
215  void disable(void);
216 
222  virtual const char *assign(const char *value) = 0;
223 
224  static void reset(void);
225  };
226 
234  class __EXPORT flagopt : public Option
235  {
236  private:
237  unsigned counter;
238  bool single;
239 
240  virtual const char *assign(const char *value);
241 
243 
244  public:
245  flagopt(char short_option, const char *long_option = NULL, const char *help = NULL, bool single_use = true);
246 
247  inline operator bool() const {
248  return counter > 0;
249  }
250 
251  inline bool operator!() const {
252  return counter == 0;
253  }
254 
255  inline operator unsigned() const {
256  return counter;
257  }
258 
259  inline unsigned operator*() const {
260  return counter;
261  }
262 
263  inline void set(unsigned value = 1) {
264  counter = value;
265  }
266 
267  inline flagopt& operator=(unsigned value) {
268  counter = value;
269  return *this;
270  }
271  };
272 
278  class __EXPORT groupopt : public Option
279  {
280  private:
281  virtual const char *assign(const char *value);
282 
284 
285  public:
286  groupopt(const char *help);
287  };
288 
295  class __EXPORT stringopt : public Option
296  {
297  private:
298  bool used;
299 
301 
302  protected:
303  const char *text;
304 
305  virtual const char *assign(const char *value);
306 
307  public:
308  stringopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "text", const char *def_text = NULL);
309 
310  inline void set(const char *string) {
311  text = string;
312  }
313 
314  inline stringopt& operator=(const char *string) {
315  text = string;
316  return *this;
317  }
318 
319  inline operator bool() const {
320  return used;
321  }
322 
323  inline bool operator!() const {
324  return !used;
325  }
326 
327  inline operator const char *() const {
328  return text;
329  }
330 
331  inline const char *operator*() const {
332  return text;
333  }
334  };
335 
342  class __EXPORT charopt : public Option
343  {
344  private:
345  bool used;
346 
348 
349  protected:
350  char code;
351 
352  virtual const char *assign(const char *value);
353 
354  public:
355  charopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "char", char default_code = ' ');
356 
357  inline void set(char value) {
358  code = value;
359  }
360 
361  inline charopt& operator=(char value) {
362  code = value;
363  return *this;
364  }
365 
366  inline operator bool() const {
367  return used;
368  }
369 
370  inline bool operator!() const {
371  return !used;
372  }
373 
374  inline operator char() const {
375  return code;
376  }
377 
378  inline char operator*() const {
379  return code;
380  }
381  };
382 
389  class __EXPORT numericopt : public Option
390  {
391  private:
392  bool used;
393 
395 
396  protected:
397  long number;
398 
399  virtual const char *assign(const char *value);
400 
401  public:
402  numericopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "numeric", long def_value = 0);
403 
404  inline void set(long value) {
405  number = value;
406  }
407 
408  inline numericopt& operator=(long value) {
409  number = value;
410  return *this;
411  }
412 
413  inline operator bool() const {
414  return used;
415  }
416 
417  inline bool operator!() const {
418  return !used;
419  }
420 
421  inline operator long() const {
422  return number;
423  }
424 
425  inline long operator*() const {
426  return number;
427  }
428  };
429 
438  class __EXPORT counteropt : public Option
439  {
440  private:
441  bool used;
442 
444 
445  protected:
446  long number;
447 
448  virtual const char *assign(const char *value);
449 
450  public:
451  counteropt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "numeric", long def_value = 0);
452 
453  inline void set(long value) {
454  number = value;
455  }
456 
457  inline counteropt& operator=(long value) {
458  number = value;
459  return *this;
460  }
461 
462  inline operator bool() const {
463  return used;
464  }
465 
466  inline bool operator!() const {
467  return !used;
468  }
469 
470  inline operator long() const {
471  return number;
472  }
473 
474  inline long operator*() const {
475  return number;
476  }
477  };
478 
486  shell(const char *string, size_t pagesize = 0);
487 
496  shell(int argc, char **argv, size_t pagesize = 0);
497 
502  shell(size_t pagesize = 0);
503 
504  static void setNumeric(numeric_t);
505 
506  static long getNumeric(void);
507 
511  static void help(void);
512 
520  static int system(const char *command, const char **env = NULL);
521 
528  static int systemf(const char *format, ...) __PRINTF(1,2);
529 
534  static void relocate(const char *argv0);
535 
542  static String path(path_t id);
543 
548  static String userid(void);
549 
556  static String path(path_t id, const char *directory);
557 
563  static String path(String& prefix, const char *directory);
564 
576  static void bind(const char *name);
577 
587  static void rebind(const char *name = NULL);
588 
594  char **parse(const char *string);
595 
604  void parse(int argc, char **argv);
605 
613  const char *getenv(const char *name, const char *value = NULL);
614 
621  const char *getsym(const char *name, const char *value = NULL);
622 
628  void setsym(const char *name, const char *value);
629 
635  bool is_sym(const char *name) const;
636 
642  char *getargv0(char **argv);
643 
651  char **getargv(char **argv);
652 
659  void restart(char *argv0, char **argv, char **list);
660 
664  inline const char *argv0() const
665  {return _argv0;}
666 
670  inline const char *execdir() const
671  {return _exedir;}
672 
677  static void errlog(const char *format, ...) __PRINTF(1, 2);
678 
684  static void errexit(int exitcode, const char *format = NULL, ...) __PRINTF(2, 3);
685 
691  static inline int condition(bool test, int exitcode) {
692  return (test) ? exitcode : 0;
693  }
694 
700  static void debug(unsigned level, const char *format, ...) __PRINTF(2, 3);
701 
707  static void log(loglevel_t level, const char *format, ...) __PRINTF(2, 3);
708 
714  static void security(loglevel_t level, const char *format, ...) __PRINTF(2, 3);
715 
723  static void log(const char *name, loglevel_t level = ERR, logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
724 
725  static size_t printf(const char *format, ...) __PRINTF(1, 2);
726 
732  inline unsigned argc(void) const
733  {return _argc;}
734 
741  inline char **argv(void) const
742  {return _argv;}
743 
749  inline const char *operator[](unsigned offset)
750  {return _argv[offset];}
751 
752  static void exiting(exitproc_t);
753 
757  void detach(mainproc_t mainentry = (mainproc_t)NULL);
758 
762  void restart(void);
763 
775  static shell::pid_t spawn(const char *path, char **argv, char **env = NULL, fd_t *stdio = NULL);
776 
785  static void priority(int pri = 1);
786 
796  static int detach(const char *path, char **argv, char **env = NULL, fd_t *stdio = NULL);
797 
802  static void release(int exit_code = 0);
803 
809  static int wait(shell::pid_t pid);
810 
816  static int cancel(shell::pid_t pid);
817 
822  inline unsigned operator()(void) const
823  {return _argc;}
824 
837  static const char *text(const char *string);
838 
848  static const char *texts(const char *singular, const char *plural, unsigned long count);
849 
855  static unsigned count(char **argv);
856 
857 #ifdef _MSWINDOWS_
858 
859  static inline fd_t input(void)
860  {return GetStdHandle(STD_INPUT_HANDLE);}
861 
862  static inline fd_t output(void)
863  {return GetStdHandle(STD_OUTPUT_HANDLE);}
864 
865  static inline fd_t error(void)
866  {return GetStdHandle(STD_ERROR_HANDLE);}
867 
868 #else
869  static inline fd_t input(void)
870  {return 0;}
871 
872  static inline fd_t output(void)
873  {return 1;}
874 
875  static inline fd_t error(void)
876  {return 2;}
877 #endif
878 
879  static int inkey(const char *prompt = NULL);
880 
881  static char *getpass(const char *prompt, char *buffer, size_t size);
882 
883  static char *getline(const char *prompt, char *buffer, size_t size);
884 };
885 
889 typedef shell shell_t;
890 
894 #undef _TEXT
895 #undef __TEXT
896 #undef _STR
897 #undef __STR
898 #define _STR(x) ((const char *)(x))
899 #define __STR(x) (static_cast<const char *>(x))
900 
908 inline const char *__TEXT(const char *s)
909  {return shell::text(s);}
910 
911 inline const char *_TEXT(const char *s)
912  {return shell::text(s);}
913 
914 } // namespace ucommon
915 
916 #endif
ucommon::shell::charopt::used
bool used
Definition: shell.h:345
ucommon::mempager
Definition: memory.h:184
ucommon::shell::numericopt
Definition: shell.h:389
directory
static shell::flagopt directory('D', "--directory", _TEXT("expand directory into file arguments"))
argv0
static const char * argv0
Definition: car.cpp:40
ucommon::shell::Option::uses_option
const char * uses_option
Definition: shell.h:194
fd_t
int fd_t
Definition: platform.h:415
ucommon::shell::_argv0
char * _argv0
Definition: shell.h:64
ucommon::shell::stringopt::text
const char * text
Definition: shell.h:303
ucommon
Definition: access.cpp:23
ucommon::shell::syms
Definition: shell.h:76
ucommon::shell::errormap
Definition: shell.h:172
ucommon::shell::charopt::set
void set(char value)
Definition: shell.h:357
ucommon::shell::charopt::operator!
bool operator!() const
Definition: shell.h:370
ucommon::shell::SYSTEM_VAR
Definition: shell.h:123
__LOCAL
#define __LOCAL
Definition: platform.h:298
error
static shell::stringopt error('e', "--error", _TEXT("stderr path to use"), "filename")
ucommon::shell::args::item
char * item
Definition: shell.h:73
ucommon::shell::USER_DEFAULTS
Definition: shell.h:120
ost::debug
__EXPORT AppLog & debug(AppLog &sl)
Definition: applog.h:520
ucommon::String
Definition: string.h:78
ucommon::shell::numericopt::operator*
long operator*() const
Definition: shell.h:425
ucommon::shell::_argv
char ** _argv
Definition: shell.h:62
ucommon::shell::groupopt
Definition: shell.h:278
ucommon::shell::syms::name
const char * name
Definition: shell.h:79
ucommon::shell::charopt
Definition: shell.h:342
ucommon::shell::output
static fd_t output(void)
Definition: shell.h:872
ucommon::shell::loglevel_t
loglevel_t
Definition: shell.h:109
ucommon::shell::counteropt::used
bool used
Definition: shell.h:441
ucommon::shell::flagopt::counter
unsigned counter
Definition: shell.h:237
ucommon::shell::stringopt::set
void set(const char *string)
Definition: shell.h:310
ucommon::shell::Option::long_option
const char * long_option
Definition: shell.h:193
ucommon::getline
bool getline(std::istream &in, char *buffer, size_t size)
Definition: stream.cpp:882
ucommon::shell::WARN
Definition: shell.h:109
ucommon::shell::text
static const char * text(const char *string)
Definition: shell.cpp:2104
ucommon::shell::numericopt::number
long number
Definition: shell.h:397
ucommon::shell::syms::value
const char * value
Definition: shell.h:80
ucommon::shell::charopt::operator*
char operator*() const
Definition: shell.h:378
ucommon::shell::numericopt::operator!
bool operator!() const
Definition: shell.h:417
exit_code
static int exit_code
Definition: car.cpp:39
ucommon::shell::Option::help_string
const char * help_string
Definition: shell.h:195
ucommon::shell::USER_DATA
Definition: shell.h:122
ucommon::shell::charopt::operator=
charopt & operator=(char value)
Definition: shell.h:361
output
static FILE * output
Definition: car.cpp:43
ucommon::shell::args
Definition: shell.h:70
ucommon::shell::input
static fd_t input(void)
Definition: shell.h:869
ucommon::shell::numericopt::operator=
numericopt & operator=(long value)
Definition: shell.h:408
ucommon::shell_t
shell shell_t
Definition: shell.h:889
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::shell::USER_LOG
Definition: shell.h:104
ucommon::shell::counteropt
Definition: shell.h:438
input
static shell::stringopt input('i', "--input", _TEXT("stdin path to use"), "filename")
ucommon::shell::counteropt::operator=
counteropt & operator=(long value)
Definition: shell.h:457
ucommon::shell
Definition: shell.h:59
ucommon::shell::numericopt::set
void set(long value)
Definition: shell.h:404
ucommon::shell::OPTION_USED
Definition: shell.h:99
ucommon::counter
Definition: counter.h:43
ucommon::shell::flagopt::single
bool single
Definition: shell.h:238
__PRINTF
#define __PRINTF(x, y)
Definition: platform.h:199
ucommon::shell::mainproc_t
cpr_service_t mainproc_t
Definition: shell.h:134
ucommon::shell::argv
char ** argv(void) const
Definition: shell.h:741
ucommon::shell::errmsg
static const char * errmsg(errmsg_t id)
Definition: shell.cpp:377
ucommon::shell::errormap::errormap
errormap(errmsg_t id, const char *text)
Definition: shell.h:175
ucommon::shell::charopt::code
char code
Definition: shell.h:350
ucommon::shell::flagopt::operator!
bool operator!() const
Definition: shell.h:251
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
ucommon::shell::numericopt::used
bool used
Definition: shell.h:392
ucommon::shell::_argc
unsigned _argc
Definition: shell.h:63
ucommon::shell::flagopt::operator=
flagopt & operator=(unsigned value)
Definition: shell.h:267
ucommon::LinkedObject
Definition: linked.h:55
ucommon::shell::stringopt::operator=
stringopt & operator=(const char *string)
Definition: shell.h:314
ucommon::shell::Option::trigger_option
bool trigger_option
Definition: shell.h:196
buffer
static uint8_t buffer[65536]
Definition: zerofill.cpp:27
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::shell::errmsg_t
errmsg_t
Definition: shell.h:99
ucommon::shell::stringopt::used
bool used
Definition: shell.h:298
ucommon::shell::pid_t
int pid_t
Definition: shell.h:147
ucommon::shell::error
static fd_t error(void)
Definition: shell.h:875
ucommon::shell::counteropt::operator!
bool operator!() const
Definition: shell.h:466
ucommon::shell::operator[]
const char * operator[](unsigned offset)
Definition: shell.h:749
string.h
ucommon::shell::flagopt::operator*
unsigned operator*() const
Definition: shell.h:259
ucommon::shell::Option::short_option
char short_option
Definition: shell.h:192
prefix
static char prefix[80]
Definition: args.cpp:33
ucommon::shell::execdir
const char * execdir() const
Definition: shell.h:670
ucommon::shell::counteropt::operator*
long operator*() const
Definition: shell.h:474
cpr_service_t
void(* cpr_service_t)(int argc, char **argv)
Definition: platform.h:534
ucommon::shell::counteropt::set
void set(long value)
Definition: shell.h:453
ucommon::__TEXT
const char * __TEXT(const char *s)
Definition: shell.h:908
ucommon::shell::flagopt::set
void set(unsigned value=1)
Definition: shell.h:263
ucommon::shell::_syms
LinkedObject * _syms
Definition: shell.h:66
ucommon::OrderedObject
Definition: linked.h:312
ucommon::shell::stringopt
Definition: shell.h:295
ucommon::shell::flagopt
Definition: shell.h:234
ucommon::shell::stringopt::operator*
const char * operator*() const
Definition: shell.h:331
ucommon::shell::stringopt::operator!
bool operator!() const
Definition: shell.h:323
ucommon::shell::Option
Definition: shell.h:186
ucommon::shell::counteropt::number
long number
Definition: shell.h:446
memory.h
ucommon::shell::operator()
unsigned operator()(void) const
Definition: shell.h:822
ucommon::_TEXT
const char * _TEXT(const char *s)
Definition: shell.h:911
ucommon::shell::_exedir
char * _exedir
Definition: shell.h:65