]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt.h
Opt: Generalization of hooks
[libucw.git] / ucw / opt.h
index 54bcde42e1738bddb6852ec0199cc9f84ad12057..25db7d45cd8b7190be391840d8b92674881ef18e 100644 (file)
--- a/ucw/opt.h
+++ b/ucw/opt.h
@@ -2,6 +2,7 @@
  *     UCW Library -- Parsing of command line options
  *
  *     (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
+ *     (c) 2014 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 #include <stdio.h>
 
 #ifdef CONFIG_UCW_CLEAN_ABI
+#define cf_def_file ucw_cf_def_file
+#define cf_env_file ucw_cf_env_file
 #define opt_conf_hook_internal ucw_opt_conf_hook_internal
-#define opt_conf_internal ucw_opt_conf_internal
-#define opt_conf_parsed_count ucw_opt_conf_parsed_count
-#define opt_help_internal ucw_opt_help_internal
+#define opt_handle_config ucw_opt_handle_config
+#define opt_handle_dumpconfig ucw_opt_handle_dumpconfig
+#define opt_handle_help ucw_opt_handle_help
+#define opt_handle_set ucw_opt_handle_set
+#define opt_help ucw_opt_help
 #define opt_parse ucw_opt_parse
-#define opt_parsed_count ucw_opt_parsed_count
-#define opt_section_root ucw_opt_section_root
 #endif
 
 #define OPT_EXIT_BAD_ARGS 2
@@ -55,6 +58,7 @@ struct opt_item {
     struct opt_section * section;      // subsection for OPT_SECTION
     int value;                         // value for OPT_SWITCH
     void (* call)(struct opt_item * opt, const char * value, void * data);  // function to call for OPT_CALL
+    void (* hook)(struct opt_item * opt, uns event, const char * value, void * data);  // function to call for OPT_CL_HOOK
     struct cf_user_type * utype;       // specification of the user-defined type
   } u;
   u16 flags;
@@ -74,7 +78,6 @@ struct opt_section {
  *
  *  OPT_HELP_OPTION declares --help and prints a line about that
  *  OPT_HELP prints a line into help
- *  OPT_HELP2 prints two strings onto a line using the same tab structure as the option listing
  *  OPT_BOOL declares boolean option with an auto-negation (--sth and --no-sth). It's also possible to write --sth=y/yes/true/1/n/no/false/0.
  *  OPT_STRING, OPT_UNS, OPT_INT declare simple string/uns/int option
  *  OPT_SWITCH declares one choice of a switch statement; these have common target and different `value`s; last wins unless OPT_SINGLE is set;
@@ -91,7 +94,7 @@ struct opt_section {
  *
  ***/
 
-#define OPT_HELP_OPTION OPT_CALL(0, "help", opt_show_help_internal, NULL, OPT_NO_VALUE, "Show this help")
+#define OPT_HELP_OPTION(help) OPT_CALL(0, "help", opt_handle_help, &help, OPT_BEFORE_CONFIG | OPT_NO_VALUE, "\tShow this help")
 #define OPT_HELP(line) { .help = line, .cls = OPT_CL_HELP }
 #define OPT_BOOL(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_BOOL, .type = CT_INT }
 #define OPT_STRING(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_STRING }
@@ -109,7 +112,7 @@ struct opt_section {
 // FIXME: Check that the target is of the right type (likewise in other statically typed options)
 #define OPT_INC(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .flags = fl, .help = desc, .cls = OPT_CL_INC, .type = CT_INT }
 #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec }
-#define OPT_HOOK(fn, data, fl) { .cls = OPT_CL_HOOK, .u.call = fn, .flags = OPT_NO_HELP | fl, .ptr = data }
+#define OPT_HOOK(fn, data, events) { .cls = OPT_CL_HOOK, .u.hook = fn, .flags = events, .ptr = data }
 #define OPT_END { .cls = OPT_CL_END }
 
 /***
@@ -125,16 +128,27 @@ struct opt_section {
 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_HOOK
 #endif
 
-#define OPT_CONF_CONFIG            OPT_CALL('C', "config", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Override the default configuration file")
-#define OPT_CONF_SET       OPT_CALL('S', "set", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Manual setting of a configuration item")
-#define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_conf_internal, NULL, OPT_NO_VALUE, "Dump program configuration")
-#define OPT_CONF_HOOK      OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE)
+#define OPT_CONF_CONFIG            OPT_CALL('C', "config", opt_handle_config, NULL, OPT_BEFORE_CONFIG | OPT_REQUIRED_VALUE, "<file>\tOverride the default configuration file")
+#define OPT_CONF_SET       OPT_CALL('S', "set", opt_handle_set, NULL, OPT_BEFORE_CONFIG | OPT_REQUIRED_VALUE, "<item>\tManual setting of a configuration item")
+#define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_handle_dumpconfig, NULL, OPT_NO_VALUE, "\tDump program configuration")
+#define OPT_CONF_HOOK      OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE | OPT_HOOK_FINAL)
 
-void opt_conf_internal(struct opt_item * opt, const char * value, void * data);
-void opt_conf_hook_internal(struct opt_item * opt, const char * value, void * data);
+void opt_handle_config(struct opt_item * opt, const char * value, void * data);
+void opt_handle_set(struct opt_item * opt, const char * value, void * data);
+void opt_handle_dumpconfig(struct opt_item * opt, const char * value, void * data);
+void opt_conf_hook_internal(struct opt_item * opt, uns event, const char * value, void * data);
 
-extern int opt_parsed_count;       /** How many opts have been already parsed. **/
-extern int opt_conf_parsed_count;
+// XXX: This is duplicated with <ucw/getopt.h>, but that one will hopefully go away one day.
+/**
+ * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded.
+ * You can set it to something else manually.
+ */
+extern char *cf_def_file;
+/**
+ * Name of environment variable that can override what configuration is loaded.
+ * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
+ **/
+extern char *cf_env_file;
 
 /***
  * Predefined shortopt arguments
@@ -152,7 +166,7 @@ extern int opt_conf_parsed_count;
 // FIXME: Is numbering from 1 natural here?
 // FIXME: Are there any rules for mixing of positional arguments with options?
 #define OPT_POSITIONAL(n)   (OPT_POSITIONAL_TAIL+(n))
-#define OPT_POSITIONAL_TAIL 256
+#define OPT_POSITIONAL_TAIL 128
 
 
 /***
@@ -170,53 +184,36 @@ extern int opt_conf_parsed_count;
 #define OPT_LAST_ARG       0x40        /** Stop processing argv after this line **/
 #define OPT_SINGLE         0x100       /** Argument must appear at most once **/
 #define OPT_MULTIPLE       0x200       /** Argument may appear any time; will save all the values into a simple list **/
-#define OPT_HOOK_BEFORE_ARG    0x1000  /** Call before option parsing **/
-#define OPT_HOOK_BEFORE_VALUE  0x2000  /** Call before value parsing **/
-#define OPT_HOOK_AFTER_VALUE   0x4000  /** Call after value parsing **/
+#define OPT_SEEN_AS_LONG    0x400      // Used internally
+#define OPT_BEFORE_CONFIG   0x800      /** Argument may appear before config file is loaded **/
 
+// For hooks, the flags contain a combination of events.
+#define OPT_HOOK_BEFORE_ARG    0x1     /** Call before option parsing **/
+#define OPT_HOOK_BEFORE_VALUE  0x2     /** Call before value parsing **/
+#define OPT_HOOK_AFTER_VALUE   0x4     /** Call after value parsing **/
+#define OPT_HOOK_FINAL         0x8     /** Call just before opt_parse() returns **/
 
-/***
- * Value flags defaults
- * ~~~~~~~~~~~~~~~~~~~~
- *
- * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
- * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
- * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
- * must be specified for OPT_CALL and OPT_USER.
- ***/
-
-static uns opt_default_value_flags[] = {
-  [OPT_CL_BOOL] = OPT_NO_VALUE,
-  [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
-  [OPT_CL_SWITCH] = OPT_NO_VALUE,
-  [OPT_CL_INC] = OPT_NO_VALUE,
-  [OPT_CL_CALL] = 0,
-  [OPT_CL_USER] = 0,
-  [OPT_CL_SECTION] = 0,
-  [OPT_CL_HELP] = 0
-};
+void opt_help(const struct opt_section * sec);
+void opt_handle_help(struct opt_item * opt, const char * value, void * data);
 
-extern const struct opt_section * opt_section_root;
-void opt_help_internal(const struct opt_section * help);
-
-static void opt_help(void) {
-  opt_help_internal(opt_section_root);
-}
-
-static void opt_usage(void) {
+// FIXME: Should this be public?
+static inline void opt_usage(void) {
   fprintf(stderr, "Run with argument --help for more information.\n");
 }
 
-static void opt_show_help_internal(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
-  opt_help();
-  exit(0);
-}
-
 /**
- * Parse all the arguments.
+ * Parse all arguments, given in a NULL-terminated array of strings.
+ *
+ * Typically, this is called from `main(argc, argv)` as `opt_parse(options, argv+1)`,
+ * skipping the 0th argument, which contains program name.
+ *
+ * Returns the number of arguments used (which need not be all of them
+ * if `OPT_LAST_ARG` was encountered).
+ *
+ * The argument array is left untouched.
+ * However, option values are not necessarily copied, the variables
+ * set by the parser may point to the argument array.
  **/
-void opt_parse(const struct opt_section * options, char ** argv);
-// FIXME: When parsing finishes (possibly due to OPT_LAST_ARG), what is guaranteed
-// about the state of argv[]?
+int opt_parse(const struct opt_section * options, char ** argv);
 
 #endif