/usr/include/ucommon/shell.h is in libucommon-dev 6.1.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* Generic shell parsing and application services.
* @file ucommon/shell.h
*/
/**
* Example of shell parsing.
* @example shell.cpp
*/
#ifndef _UCOMMON_STRING_H_
#include <ucommon/string.h>
#endif
#ifndef _UCOMMON_MEMORY_H_
#include <ucommon/memory.h>
#endif
#ifndef _UCOMMON_BUFFER_H_
#include <ucommon/buffer.h>
#endif
#ifndef _UCOMMON_SHELL_H_
#define _UCOMMON_SHELL_H_
#ifdef _MSWINDOWS_
#define INVALID_PID_VALUE INVALID_HANDLE_VALUE
#else
#define INVALID_PID_VALUE -1
#endif
#ifdef ERR
#undef ERR
#endif
namespace ucommon {
/**
* A utility class for generic shell operations. This includes utilities
* to parse and expand arguments, and to call system shell services. This
* also includes a common shell class to parse and process command line
* arguments which are managed through a local heap.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT shell : public mempager
{
private:
char **_argv;
unsigned _argc;
char *_argv0;
char *_exedir;
LinkedObject *_syms;
class __LOCAL args : public OrderedObject
{
public:
char *item;
};
class __LOCAL syms : public LinkedObject
{
public:
const char *name;
const char *value;
};
/**
* Collapse argument list. This is used internally to collapse args
* that are created in a pool heap when they need to be turned into
* an argv style array.
*/
void collapse(LinkedObject *first);
/**
* Set argv0. This gets the simple filename of argv[0].
*/
void set0(char *argv0);
public:
/**
* Error table index.
*/
typedef enum {NOARGS = 0, NOARGUMENT, INVARGUMENT, BADOPTION, OPTION_USED, BAD_VALUE, NUMERIC_SET} errmsg_t;
/**
* Type of error logging we are using.
*/
typedef enum {NONE = 0, CONSOLE_LOG, USER_LOG, SYSTEM_LOG, SECURITY_LOG} logmode_t;
/**
* Level of error logging.
*/
typedef enum {FAIL = 0, ERR, WARN, NOTIFY, INFO, DEBUG0} loglevel_t;
/**
* Numeric mode of parser.
*/
typedef enum {NO_NUMERIC, NUMERIC_PLUS, NUMERIC_DASH, NUMERIC_ALL} numeric_t;
/**
* Path types to retrieve.
*/
typedef enum {
PROGRAM_CONFIG, SERVICE_CONFIG, USER_DEFAULTS, SERVICE_CONTROL,
USER_HOME = USER_DEFAULTS + 3, SERVICE_DATA, SYSTEM_TEMP, USER_CACHE,
SERVICE_CACHE, USER_DATA, USER_CONFIG, SYSTEM_CFG, SYSTEM_ETC,
SYSTEM_VAR, SYSTEM_PREFIX, SYSTEM_SHARE, PROGRAM_PLUGINS,
PROGRAM_TEMP} path_t;
/**
* Log process handler.
*/
typedef bool (*logproc_t)(loglevel_t level, const char *text);
/**
* Main handler.
*/
typedef cpr_service_t mainproc_t;
/**
* Exit handler.
*/
typedef void (*exitproc_t)(void);
#ifdef _MSWINDOWS_
typedef HANDLE pid_t;
#else
/**
* Standard type of process id for shell class.
*/
typedef int pid_t;
#endif
/**
* This can be used to get internationalized error messages. The internal
* text for shell parser errors are passed through here.
* @param id of error message to use.
* @return published text of error message.
*/
static const char *errmsg(errmsg_t id);
/**
* This is used to set internationalized error messages for the shell
* parser.
* @param id of message to set.
* @param text for error message.
*/
static void errmsg(errmsg_t id, const char *text);
/**
* A class to redefine error messages. This can be used as a statically
* initialized object to remap error messages for easier
* internationalization.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT errormap
{
public:
inline errormap(errmsg_t id, const char *text)
{shell::errmsg(id, text);}
};
/**
* A base class used to create parsable shell options. The virtual
* is invoked when the shell option is detected. Both short and long
* forms of argument parsing are supported. An instance of a derived
* class is created to perform the argument parsing.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT Option : public LinkedObject
{
public:
char short_option;
const char *long_option;
const char *uses_option;
const char *help_string;
bool trigger_option;
/**
* Construct a shell parser option.
* @param short_option for single character code.
* @param long_option for extended string.
* @param value_type if -x value or -long=yyy.
* @param help string, future use.
*/
Option(char short_option = 0, const char *long_option = NULL, const char *value_type = NULL, const char *help = NULL);
virtual ~Option();
static LinkedObject *first(void);
/**
* Disable a option. Might happen if argv0 name suggests an
* option is no longer actively needed.
*/
void disable(void);
/**
* Used to send option into derived receiver.
* @param value option that was received.
* @return NULL or error string to use.
*/
virtual const char *assign(const char *value) = 0;
static void reset(void);
};
/**
* Flag option for shell parsing. This offers a quick-use class
* to parse a shell flag, along with a counter for how many times
* the flag was selected. The counter might be used for -vvvv style
* verbose options, for example.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT flagopt : public Option
{
private:
unsigned counter;
bool single;
virtual const char *assign(const char *value);
public:
flagopt(char short_option, const char *long_option = NULL, const char *help = NULL, bool single_use = true);
inline operator bool()
{return counter > 0;}
inline bool operator!()
{return counter == 0;}
inline operator unsigned()
{return counter;}
inline unsigned operator*()
{return counter;}
inline void set(unsigned value = 1)
{counter = value;}
};
/**
* Grouping option. This is used to create a grouping entry in
* the shell::help() listing.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT groupopt : public Option
{
private:
virtual const char *assign(const char *value);
public:
groupopt(const char *help);
};
/**
* Text option for shell parsing. This offers a quick-use class
* to parse a shell flag, along with a numeric text that may be
* saved and a use counter, as multiple invocations is an error.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT stringopt : public Option
{
private:
bool used;
protected:
const char *text;
virtual const char *assign(const char *value);
public:
stringopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "text", const char *def_text = NULL);
inline void set(const char *string)
{text = string;}
inline operator bool()
{return used;}
inline bool operator!()
{return !used;}
inline operator const char *()
{return text;}
inline const char *operator*()
{return text;}
};
/**
* Character option for shell parsing. This offers a quick-use class
* to parse a shell flag, along with a character code that may be
* saved. Multiple invocations is an error.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT charopt : public Option
{
private:
bool used;
protected:
char code;
virtual const char *assign(const char *value);
public:
charopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "char", char default_code = ' ');
inline void set(char value)
{code = value;}
inline operator bool()
{return used;}
inline bool operator!()
{return !used;}
inline operator char()
{return code;}
inline char operator*()
{return code;}
};
/**
* Numeric option for shell parsing. This offers a quick-use class
* to parse a shell flag, along with a numeric value that may be
* saved and a use counter, as multiple invocations is an error.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT numericopt : public Option
{
private:
bool used;
protected:
long number;
virtual const char *assign(const char *value);
public:
numericopt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "numeric", long def_value = 0);
inline void set(long value)
{number = value;}
inline operator bool()
{return used;}
inline bool operator!()
{return !used;}
inline operator long()
{return number;}
inline long operator*()
{return number;}
};
/**
* Counter option for shell parsing. This offers a quick-use class
* to parse a shell flag, along with a numeric value that may be
* saved and a use counter, as multiple invocations is an error. Unlike
* numeric options, the short mode flag is a trigger option, and each
* use of the short flag is considered a counter increment.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT counteropt : public Option
{
private:
bool used;
protected:
long number;
virtual const char *assign(const char *value);
public:
counteropt(char short_option, const char *long_option = NULL, const char *help = NULL, const char *type = "numeric", long def_value = 0);
inline void set(long value)
{number = value;}
inline operator bool()
{return used;}
inline bool operator!()
{return !used;}
inline operator long()
{return number;}
inline long operator*()
{return number;}
};
/**
* Construct a shell argument list by parsing a simple command string.
* This separates a string into a list of command line arguments which
* can be used with exec functions.
* @param string to parse.
* @param pagesize for local heap.
*/
shell(const char *string, size_t pagesize = 0);
/**
* Construct a shell argument list from existing arguments. This
* copies and on some platforms expands the argument list originally
* passed to main.
* @param argc from main.
* @param argv from main.
* @param pagesize for local heap.
*/
shell(int argc, char **argv, size_t pagesize = 0);
/**
* Construct an empty shell parser argument list.
* @param pagesize for local heap.
*/
shell(size_t pagesize = 0);
static void setNumeric(numeric_t);
static long getNumeric(void);
/**
* Display shell options.
*/
static void help(void);
/**
* A shell system call. This uses the native system shell to invoke the
* command.
* @param command string..
* @param env array to optionally use.
* @return error code of child process.
*/
static int system(const char *command, const char **env = NULL);
/**
* A shell system call that can be issued using a formatted string. This
* uses the native system shell to invoke the command.
* @param format of/command string.
* @return error code of child process.
*/
static int systemf(const char *format, ...) __PRINTF(1,2);
/**
* Set relative prefix. Used for OS/X relocatable applications.
* @param argv0 path of executable.
*/
static void relocate(const char *argv0);
/**
* Get a system path. This is used to get directories for application
* specific data stores and default paths for config keys.
* @param id of path to return.
* @return path string or emptry string if not supported.
*/
static String path(path_t id);
/**
* Get the system login id.
* @return login id.
*/
static String userid(void);
/**
* Get a merged path. If the path requested is a full path, then
* the prefix is ignored.
* @param id of prefix.
* @param directory path to merge with prefix.
*/
static String path(path_t id, const char *directory);
/**
* Join a prefix with a path.
* @param prefix to merge with.
* @param directory or file path to merge.
*/
static String path(String& prefix, const char *directory);
/**
* Bind application to text domain for internationalization. This
* is useful if the argv0 argument can vary because of a symlinked
* executable. This is the name of the .po/.mo message files for
* your application. If bind is not called before shell processing,
* then the argv0 is used as the bind name. Bind can be called
* multiple times to change the default message catalog name of the
* application, and this usage may be needed for plugins, though it's
* generally recommended to use only once, and at the start of main().
* @param name of text domain for the application.
*/
static void bind(const char *name);
/**
* Rebind is used to change the text domain. This may be needed in
* applications which have separately built plugins that have thier
* own message catalogs. Normally the plugin would call bind itself
* at initialization, and then use rebind to select either the
* application's domain, or the plugins. On systems without
* internationalization, this has no effect.
* @param name of text domain of plugin or NULL to restore application.
*/
static void rebind(const char *name = NULL);
/**
* Parse a string as a series of arguments for use in exec calls.
* @param string to parse.
* @return argument array.
*/
char **parse(const char *string);
/**
* Parse the command line arguments using the option table. File
* arguments will be expanded for wildcards on some platforms.
* The argv will be set to the first file argument after all options
* are parsed.
* @param argc from main.
* @param argv from main.
*/
void parse(int argc, char **argv);
/**
* Get an environment variable. This creates a local copy of the
* variable in pager memory.
* @param name of symbol.
* @param value of symbol if not found.
* @return value of symbol.
*/
const char *env(const char *name, const char *value = NULL);
inline const char *getenv(const char *name, const char *value = NULL)
{return env(name, value);}
/**
* Get a local symbol. This uses getenv if no local symbol is found.
* @param name of symbol.
* @param value of symbol if not found.
* @return value of symbol.
*/
const char *get(const char *name, const char *value = NULL);
inline const char *getsym(const char *name, const char *value = NULL)
{return get(name, value);}
/**
* Set a local symbol.
* @param name of symbol to set.
* @param value of symbol to set.
*/
void set(const char *name, const char *value);
inline void setsym(const char *name, const char *value)
{return set(name, value);}
/**
* Test if symbol exists.
* @param name of symbol.
* @return true if found.
*/
bool is_sym(const char *name);
/**
* Parse and extract the argv0 filename alone.
* @param argv from main.
* @return argv0 simple path name.
*/
char *getargv0(char **argv);
/**
* Get the argument list by parsing options, and return the remaining
* file arguments. This is used by parse, and can be fed by main by
* posting ++argv.
* @param argv of first option.
* @return argv of non-option file list.
*/
char **getargv(char **argv);
/**
* Execute front-end like gdb based on stripped first argument.
* @param argv0 of our executable.
* @param argv to pass to child.
* @param list of arguments to execute in front of argv.
*/
void restart(char *argv0, char **argv, char **list);
/**
* Get program name (argv0).
*/
inline const char *argv0() const
{return _argv0;}
/**
* Get the exec directory.
*/
inline const char *execdir() const
{return _exedir;}
/**
* Print error message and continue.
* @param format string to use.
*/
static void error(const char *format, ...) __PRINTF(1, 2);
/**
* Print error message and exit. Ignored if exitcode == 0.
* @param exitcode to return to parent process.
* @param format string to use.
*/
static void errexit(int exitcode, const char *format = NULL, ...) __PRINTF(2, 3);
/**
* Convert condition to exit code if true.
* @param test condition.
* @param exitcode to use if true.
*/
static inline int condition(bool test, int exitcode)
{ return (test) ? exitcode : 0;}
/**
* Print a debug message by debug level.
* @param level of debug message.
* @param format string to use.
*/
static void debug(unsigned level, const char *format, ...) __PRINTF(2, 3);
/**
* Print generic error message at specific error level.
* @param level of error condition.
* @param format string to use.
*/
static void log(loglevel_t level, const char *format, ...) __PRINTF(2, 3);
/**
* Print security error message at specific error level.
* @param level of error condition.
* @param format string to use.
*/
static void security(loglevel_t level, const char *format, ...) __PRINTF(2, 3);
/**
* Set logging level and state.
* @param name of logging entity.
* @param level of error conditions to log.
* @param mode of logging.
* @param handler for log messages.
*/
static void log(const char *name, loglevel_t level = ERR, logmode_t mode = USER_LOG, logproc_t handler = (logproc_t)NULL);
/**
* Print to standard output.
* @param format string to use.
*/
static size_t printf(const char *format, ...) __PRINTF(1, 2);
static size_t readln(char *address, size_t size);
static size_t writes(const char *string);
static size_t read(String& string);
inline static size_t write(String& string)
{return writes(string.c_str());}
/**
* Get saved internal argc count for items. This may be items that
* remain after shell expansion and options have been parsed.
* @return count of remaining arguments.
*/
inline unsigned argc(void) const
{return _argc;}
/**
* Get saved internal argv count for items in this shell object. This
* may be filename items only that remain after shell expansion and
* options that have been parsed.
* @return list of remaining arguments.
*/
inline char **argv(void) const
{return _argv;}
/**
* Return parser argv element.
* @param offset into array.
* @return argument string.
*/
inline const char *operator[](unsigned offset)
{return _argv[offset];}
static void exiting(exitproc_t);
/**
* Detach current process to daemon for service entry.
*/
void detach(mainproc_t mainentry = (mainproc_t)NULL);
/**
* Make current process restartable.
*/
void restart(void);
/**
* Spawn a child process. This creates a new child process. If
* the executable path is a pure filename, then the $PATH will be
* used to find it. The argv array may be created from a string
* with the shell string parser.
* @param path to executable.
* @param argv list of command arguments for the child process.
* @param env of child process can be explicitly set.
* @param stdio handles for stdin, stdout, and stderr.
* @return process id of child or INVALID_PID_VALUE if fails.
*/
static shell::pid_t spawn(const char *path, char **argv, char **env = NULL, fd_t *stdio = NULL);
/**
* Set priority level and enable priority scheduler. This activates the
* realtime priority scheduler when a priority > 0 is requested for the
* process, assuming scheduler support exists and the process is
* sufficiently privileged. Negative priorities are essentially the
* same as nice.
* @param pri level for process.
*/
static void priority(int pri = 1);
/**
* Create a detached process. This creates a new child process that
* is completely detached from the current process.
* @param path to executable.
* @param argv list of command arguments for the child process.
* @param env of child process can be explicitly set.
* @param stdio handles for stdin, stdout, and stderr.
* @return 0 if success, -1 on error.
*/
static int detach(const char *path, char **argv, char **env = NULL, fd_t *stdio = NULL);
/**
* Detach and release from parent process with exit code.
* @param exit_code to send to parent process.
*/
static void release(int exit_code = 0);
/**
* Wait for a child process to terminate. This operation blocks.
* @param pid of process to wait for.
* @return exit code of process, -1 if fails or pid is invalid.
*/
static int wait(shell::pid_t pid);
/**
* Cancel a child process.
* @param pid of child process to cancel.
* @return exit code of process, -1 if fails or pid is invalid.
*/
static int cancel(shell::pid_t pid);
/**
* Return argc count.
* @return argc count.
*/
inline unsigned operator()(void)
{return _argc;}
/**
* Text translation and localization. This function does nothing but
* return the original string if no internationalization support is
* available. If internationalization support exists, then it may
* return a substituted translation based on the current locale. This
* offers a single function that can be safely used either when
* internationalization support is present, or it is absent, eliminating
* the need for the application to be coded differently based on
* awareness of support.
* @param string to translate.
* @return translation if found or original text.
*/
static const char *text(const char *string);
/**
* Plural text translation and localization. This does nothing but
* return single or plural forms if no internationalization is
* enabled. Else it uses ngettext().
* @param singular string to translate.
* @param plural string to translate.
* @param count of objects.
* @return string to use.
*/
static const char *texts(const char *singular, const char *plural, unsigned long count);
/**
* Get argc count for an existing array.
* @param argv to count items in.
* @return argc count of array.
*/
static unsigned count(char **argv);
#ifdef _MSWINDOWS_
static inline fd_t input(void)
{return GetStdHandle(STD_INPUT_HANDLE);}
static inline fd_t output(void)
{return GetStdHandle(STD_OUTPUT_HANDLE);}
static inline fd_t error(void)
{return GetStdHandle(STD_ERROR_HANDLE);}
#else
static inline fd_t input(void)
{return 0;}
static inline fd_t output(void)
{return 1;}
static inline fd_t error(void)
{return 2;}
#endif
static int inkey(const char *prompt = NULL);
static char *getpass(const char *prompt, char *buffer, size_t size);
static char *getline(const char *prompt, char *buffer, size_t size);
};
/**
* Convenience type to manage and pass shell objects.
*/
typedef shell shell_t;
/**
* Abusive compilers...
*/
#undef _TEXT
#undef _STR
#define _STR(x) (const char *)(x)
/**
* Invoke translation lookup if available. This can also be used to
* mark text constants that need to be translated. It should not be
* used with pointer variables, which should instead call shell::text
* directly. The primary purpose is to allow extraction of text to
* be internationalized with xgettext "--keyword=_TEXT:1".
*/
inline const char *_TEXT(const char *s)
{return shell::text(s);}
} // namespace ucommon
#endif
|