UCommon
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
50namespace ucommon {
51
59class __EXPORT shell : public mempager
60{
61private:
62 char **_argv;
63 unsigned _argc;
64 char *_argv0;
65 char *_exedir;
66 LinkedObject *_syms;
67
68 __DELETE_COPY(shell);
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
95public:
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
134 typedef cpr_service_t mainproc_t;
135
139 typedef void (*exitproc_t)(void);
140
141#ifdef _MSWINDOWS_
142 typedef HANDLE pid_t;
143#else
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
172 class __EXPORT errormap
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:
189 __DELETE_COPY(Option);
190
191 public:
192 char short_option;
193 const char *long_option;
194 const char *uses_option;
195 const char *help_string;
196 bool trigger_option;
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
242 __DELETE_DEFAULTS(flagopt);
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
283 __DELETE_DEFAULTS(groupopt);
284
285 public:
286 groupopt(const char *help);
287 };
288
295 class __EXPORT stringopt : public Option
296 {
297 private:
298 bool used;
299
300 __DELETE_DEFAULTS(stringopt);
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
347 __DELETE_DEFAULTS(charopt);
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
394 __DELETE_DEFAULTS(numericopt);
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
443 __DELETE_DEFAULTS(counteropt);
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
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
908inline const char *__TEXT(const char *s)
909 {return shell::text(s);}
910
911inline const char *_TEXT(const char *s)
912 {return shell::text(s);}
913
914} // namespace ucommon
915
916#endif
Private heaps, pools, and associations.
AppLog & error(AppLog &sl)
Manipulator for error level.
Definition applog.h:536
Common namespace for all ucommon objects.
Definition access.h:47
const char * __TEXT(const char *s)
Invoke translation lookup if available.
Definition shell.h:908
shell shell_t
Convenience type to manage and pass shell objects.
Definition shell.h:889
Automatic integer counting class.
Definition counter.h:44
Common base class for all objects that can be formed into a linked list.
Definition linked.h:56
A linked object base class for ordered objects.
Definition linked.h:313
A managed private heap for small allocations.
Definition memory.h:185
A utility class for generic shell operations.
Definition shell.h:60
void restart(void)
Make current process restartable.
static void errmsg(errmsg_t id, const char *text)
This is used to set internationalized error messages for the shell parser.
static int cancel(shell::pid_t pid)
Cancel a child process.
loglevel_t
Level of error logging.
Definition shell.h:109
static unsigned count(char **argv)
Get argc count for an existing array.
shell(size_t pagesize=0)
Construct an empty shell parser argument list.
static const char * text(const char *string)
Text translation and localization.
const char * operator[](unsigned offset)
Return parser argv element.
Definition shell.h:749
static void help(void)
Display shell options.
int pid_t
Standard type of process id for shell class.
Definition shell.h:147
cpr_service_t mainproc_t
Main handler.
Definition shell.h:134
static int system(const char *command, const char **env=NULL)
A shell system call.
logmode_t
Type of error logging we are using.
Definition shell.h:104
unsigned operator()(void) const
Return argc count.
Definition shell.h:822
static const char * texts(const char *singular, const char *plural, unsigned long count)
Plural text translation and localization.
static const char * errmsg(errmsg_t id)
This can be used to get internationalized error messages.
errmsg_t
Error table index.
Definition shell.h:99
static int detach(const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL)
Create a detached process.
static void release(int exit_code=0)
Detach and release from parent process with exit code.
const char * execdir() const
Get the exec directory.
Definition shell.h:670
static int wait(shell::pid_t pid)
Wait for a child process to terminate.
numeric_t
Numeric mode of parser.
Definition shell.h:114
shell(int argc, char **argv, size_t pagesize=0)
Construct a shell argument list from existing arguments.
shell(const char *string, size_t pagesize=0)
Construct a shell argument list by parsing a simple command string.
path_t
Path types to retrieve.
Definition shell.h:119
char ** argv(void) const
Get saved internal argv count for items in this shell object.
Definition shell.h:741
static shell::pid_t spawn(const char *path, char **argv, char **env=NULL, fd_t *stdio=NULL)
Spawn a child process.
static void errlog(const char *format,...)
Print error message and continue.
void detach(mainproc_t mainentry=(mainproc_t) NULL)
Detach current process to daemon for service entry.
static void priority(int pri=1)
Set priority level and enable priority scheduler.
static int systemf(const char *format,...)
A shell system call that can be issued using a formatted string.
static void debug(unsigned level, const char *format,...)
Print a debug message by debug level.
A class to redefine error messages.
Definition shell.h:173
A base class used to create parsable shell options.
Definition shell.h:187
Option(char short_option=0, const char *long_option=NULL, const char *value_type=NULL, const char *help=NULL)
Construct a shell parser option.
virtual const char * assign(const char *value)=0
Used to send option into derived receiver.
void disable(void)
Disable a option.
Flag option for shell parsing.
Definition shell.h:235
Grouping option.
Definition shell.h:279
Text option for shell parsing.
Definition shell.h:296
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Character option for shell parsing.
Definition shell.h:343
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Numeric option for shell parsing.
Definition shell.h:390
virtual const char * assign(const char *value)
Used to send option into derived receiver.
Counter option for shell parsing.
Definition shell.h:439
virtual const char * assign(const char *value)
Used to send option into derived receiver.
A copy-on-write string class that operates by reference count.
Definition string.h:79
A common string class and character string support functions.