Also register for path / on D-Bus
[browser-switch] / config.c
index 663858a..fad8271 100644 (file)
--- a/config.c
+++ b/config.c
  */
 
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 
 #include "configfile.h"
 #include "config.h"
 
-/* Initialize a swb_config struct with configuration defaults */
-void swb_config_init(struct swb_config *cfg) {
-       if (!cfg)
-               return;
+/* The Browser Switchboard config file options */
+struct swb_config_option swb_config_options[] = {
+       { "continuous_mode", SWB_CONFIG_OPT_INT, SWB_CONFIG_CONTINUOUS_MODE_SET, offsetof(struct swb_config, continuous_mode) },
+       { "default_browser", SWB_CONFIG_OPT_STRING, SWB_CONFIG_DEFAULT_BROWSER_SET, offsetof(struct swb_config, default_browser) },
+       { "other_browser_cmd", SWB_CONFIG_OPT_STRING, SWB_CONFIG_OTHER_BROWSER_CMD_SET, offsetof(struct swb_config, other_browser_cmd) },
+       { "logging", SWB_CONFIG_OPT_STRING, SWB_CONFIG_LOGGING_SET, offsetof(struct swb_config, logging) },
+       { "autostart_microb", SWB_CONFIG_OPT_INT, SWB_CONFIG_AUTOSTART_MICROB_SET, offsetof(struct swb_config, autostart_microb) },
+       { NULL, 0, 0, 0 },
+};
 
-       cfg->continuous_mode = 0;
-       cfg->default_browser = "microb";
-       cfg->other_browser_cmd = NULL;
-       cfg->logging = "stdout";
+/* Browser Switchboard configuration defaults */
+static struct swb_config swb_config_defaults = {
+       .flags = SWB_CONFIG_INITIALIZED,
+       .continuous_mode = 1,
+       .default_browser = "microb",
+       .other_browser_cmd = NULL,
+       .logging = "stdout",
+       .autostart_microb = -1,
+};
 
-       cfg->flags = SWB_CONFIG_INITIALIZED;
+
+/* Initialize a swb_config struct with configuration defaults */
+inline void swb_config_init(struct swb_config *cfg) {
+       *cfg = swb_config_defaults;
 }
 
 /* Free all heap memory used in an swb_config struct
    This MUST NOT be done if any of the strings are being used elsewhere! */
 void swb_config_free(struct swb_config *cfg) {
+       int i;
+       void *entry;
+
        if (!cfg)
                return;
        if (!(cfg->flags & SWB_CONFIG_INITIALIZED))
                return;
 
-       if (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET) {
-               free(cfg->default_browser);
-               cfg->default_browser = NULL;
-       }
-       if (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET) {
-               free(cfg->other_browser_cmd);
-               cfg->other_browser_cmd = NULL;
-       }
-       if (cfg->flags & SWB_CONFIG_LOGGING_SET) {
-               free(cfg->logging);
-               cfg->logging = NULL;
+       for (i = 0; swb_config_options[i].name; ++i) {
+               entry = (char *)cfg + swb_config_options[i].offset;
+               if (cfg->flags & swb_config_options[i].set_mask) {
+                       switch (swb_config_options[i].type) {
+                         case SWB_CONFIG_OPT_STRING:
+                               free(*(char **)entry);
+                               *(char **)entry = NULL;
+                               break;
+                         default:
+                               break;
+                       }
+               }
        }
 
        cfg->flags = 0;
 }
 
+/* Load a value into the part of a struct swb_config indicated by name */
+static int swb_config_load_option(struct swb_config *cfg,
+                                 char *name, char *value) {
+       struct swb_config_option *opt;
+       void *entry;
+
+       /* Search through list of recognized config options for a match */
+       for (opt = swb_config_options; opt->name; ++opt) {
+               if (strcmp(name, opt->name))
+                       continue;
+
+               if (!(cfg->flags & opt->set_mask)) {
+                       entry = (char *)cfg + opt->offset;
+                       switch (opt->type) {
+                         case SWB_CONFIG_OPT_STRING:
+                               *(char **)entry = value;
+                               break;
+                         case SWB_CONFIG_OPT_INT:
+                               *(int *)entry = atoi(value);
+                               free(value);
+                               break;
+                       }
+                       cfg->flags |= opt->set_mask;
+               } else {
+                       /* Option was repeated in the config file
+                          We want the first value, so ignore this one */
+                       free(value);
+               }
+               return 1;
+       }
+
+       /* Unrecognized config option */
+       free(value);
+       return 0;
+}
+
 /* Read the config file and load settings into the provided swb_config struct
    Caller is responsible for freeing allocated strings with free()
    Returns true on success, false otherwise */
@@ -81,39 +135,8 @@ int swb_config_load(struct swb_config *cfg) {
        if (!parse_config_file_begin())
                goto out;
        while (!parse_config_file_line(fp, &line)) {
-               if (line.parsed) {
-                       if (!strcmp(line.key, "continuous_mode")) {
-                               if (!(cfg->flags &
-                                     SWB_CONFIG_CONTINUOUS_MODE_SET)) {
-                                       cfg->continuous_mode = atoi(line.value);
-                                       cfg->flags |=
-                                               SWB_CONFIG_CONTINUOUS_MODE_SET;
-                               }
-                               free(line.value);
-                       } else if (!strcmp(line.key, "default_browser")) {
-                               if (!(cfg->flags &
-                                     SWB_CONFIG_DEFAULT_BROWSER_SET)) {
-                                       cfg->default_browser = line.value;
-                                       cfg->flags |=
-                                               SWB_CONFIG_DEFAULT_BROWSER_SET;
-                               }
-                       } else if (!strcmp(line.key, "other_browser_cmd")) {
-                               if (!(cfg->flags &
-                                     SWB_CONFIG_OTHER_BROWSER_CMD_SET)) {
-                                       cfg->other_browser_cmd = line.value;
-                                       cfg->flags |=
-                                               SWB_CONFIG_OTHER_BROWSER_CMD_SET;
-                               }
-                       } else if (!strcmp(line.key, "logging")) {
-                               if (!(cfg->flags & SWB_CONFIG_LOGGING_SET)) {
-                                       cfg->logging = line.value;
-                                       cfg->flags |= SWB_CONFIG_LOGGING_SET;
-                               }
-                       } else {
-                               /* Don't need this line's contents */
-                               free(line.value);
-                       }
-               }
+               if (line.parsed)
+                       swb_config_load_option(cfg, line.key, line.value);
                free(line.key);
        }
        parse_config_file_end();