]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt-conf.c
xtypes: added first shot on unit parser
[libucw.git] / ucw / opt-conf.c
index 780c87478f0d4abab15725c9d7a488227baee59e..c29f23b973dd2573c130646b463850605090f57e 100644 (file)
@@ -24,60 +24,69 @@ static void opt_conf_end_of_options(struct cf_context *cc) {
     opt_failure("Loading of configuration failed");
 }
 
-void opt_conf_internal(struct opt_item * opt, const char * value, void * data UNUSED) {
-  struct cf_context *cc = cf_get_context();
-  switch (opt->letter) {
-    case 'S':
-      cf_load_default(cc);
-      if (cf_set(value))
-       opt_failure("Cannot set %s", value);
+static void opt_conf_check(struct opt_context *oc)
+{
+  switch (oc->conf_state) {
+    case OPT_CONF_HOOK_BEGIN:
+      oc->conf_state = OPT_CONF_HOOK_CONFIG;
       break;
-    case 'C':
-      if (cf_load(value))
-       opt_failure("Cannot load config file %s", value);
+    case OPT_CONF_HOOK_CONFIG:
       break;
-#ifdef CONFIG_UCW_DEBUG
-    case '0':
-      opt_conf_end_of_options(cc);
-      struct fastbuf *b = bfdopen(1, 4096);
-      cf_dump_sections(b);
-      bclose(b);
-      exit(0);
+    case OPT_CONF_HOOK_OTHERS:
+      opt_failure("Config options must stand before other options.");
       break;
-#endif
+    default:
+      ASSERT(0);
   }
 }
 
-void opt_conf_hook_internal(struct opt_item * opt, const char * value UNUSED, void * data UNUSED) {
-  static enum {
-    OPT_CONF_HOOK_BEGIN,
-    OPT_CONF_HOOK_CONFIG,
-    OPT_CONF_HOOK_OTHERS
-  } state = OPT_CONF_HOOK_BEGIN;
+void opt_handle_config(const struct opt_item * opt UNUSED, const char * value, void * data)
+{
+  opt_conf_check(data);
+  if (cf_load(value))
+    exit(1);           // Error message is already printed by cf_load()
+}
+
+void opt_handle_set(const struct opt_item * opt UNUSED, const char * value, void * data)
+{
+  opt_conf_check(data);
+  struct cf_context *cc = cf_get_context();
+  cf_load_default(cc);
+  if (cf_set(value))
+    opt_failure("Cannot set %s", value);
+}
+
+void opt_handle_dumpconfig(const struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED)
+{
+  struct cf_context *cc = cf_get_context();
+  opt_conf_end_of_options(cc);
+  struct fastbuf *b = bfdopen(1, 4096);
+  cf_dump_sections(b);
+  bclose(b);
+  exit(0);
+}
 
-  int confopt = 0;
+void opt_conf_hook_internal(const struct opt_item * opt, uint event, const char * value UNUSED, void * data) {
+  struct opt_context *oc = data;
+  struct cf_context *cc = cf_get_context();
 
-  if (opt->letter == 'S' || opt->letter == 'C' || (opt->name && !strcmp(opt->name, "dumpconfig")))
-    confopt = 1;
+  if (event == OPT_HOOK_FINAL) {
+      opt_conf_end_of_options(cc);
+      return;
+  }
+
+  ASSERT(event == OPT_HOOK_BEFORE_VALUE);
+
+  if (opt->flags & OPT_BEFORE_CONFIG)
+    return;
 
-  switch (state) {
+  switch (oc->conf_state) {
     case OPT_CONF_HOOK_BEGIN:
-      if (confopt)
-       state = OPT_CONF_HOOK_CONFIG;
-      else {
-       opt_conf_end_of_options(cf_get_context());
-       state = OPT_CONF_HOOK_OTHERS;
-      }
-      break;
     case OPT_CONF_HOOK_CONFIG:
-      if (!confopt) {
-       opt_conf_end_of_options(cf_get_context());
-       state = OPT_CONF_HOOK_OTHERS;
-      }
+      opt_conf_end_of_options(cc);
+      oc->conf_state = OPT_CONF_HOOK_OTHERS;
       break;
     case OPT_CONF_HOOK_OTHERS:
-      if (confopt)
-       opt_failure("Config options (-C, -S) must stand before other options.");
       break;
     default:
       ASSERT(0);