Also register for path / on D-Bus
[browser-switch] / config.c
index 8928a3d..fad8271 100644 (file)
--- a/config.c
+++ b/config.c
 
 /* The Browser Switchboard config file options */
 struct swb_config_option swb_config_options[] = {
 
 /* The Browser Switchboard config file options */
 struct swb_config_option swb_config_options[] = {
-       { "continuous_mode", SWB_CONFIG_OPT_INT, SWB_CONFIG_CONTINUOUS_MODE_SET },
-       { "default_browser", SWB_CONFIG_OPT_STRING, SWB_CONFIG_DEFAULT_BROWSER_SET },
-       { "other_browser_cmd", SWB_CONFIG_OPT_STRING, SWB_CONFIG_OTHER_BROWSER_CMD_SET },
-       { "logging", SWB_CONFIG_OPT_STRING, SWB_CONFIG_LOGGING_SET },
-       { NULL, 0, 0 },
+       { "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 },
 };
 
 /* Browser Switchboard configuration defaults */
 static struct swb_config swb_config_defaults = {
        .flags = SWB_CONFIG_INITIALIZED,
 };
 
 /* Browser Switchboard configuration defaults */
 static struct swb_config swb_config_defaults = {
        .flags = SWB_CONFIG_INITIALIZED,
-       .continuous_mode = 0,
+       .continuous_mode = 1,
        .default_browser = "microb",
        .other_browser_cmd = NULL,
        .logging = "stdout",
        .default_browser = "microb",
        .other_browser_cmd = NULL,
        .logging = "stdout",
+       .autostart_microb = -1,
 };
 
 
 };
 
 
-/* Copy the contents of an swb_config struct
-   The entries[] array means that the standard copy will not work */
-void swb_config_copy(struct swb_config *dst, struct swb_config *src) {
-       if (!dst || !src)
-               return;
-
-       dst->entries[0] = &(dst->continuous_mode);
-       dst->entries[1] = &(dst->default_browser);
-       dst->entries[2] = &(dst->other_browser_cmd);
-       dst->entries[3] = &(dst->logging);
-
-       dst->flags = src->flags;
-
-       dst->continuous_mode = src->continuous_mode;
-       dst->default_browser = src->default_browser;
-       dst->other_browser_cmd = src->other_browser_cmd;
-       dst->logging = src->logging;
-}
-
 /* Initialize a swb_config struct with configuration defaults */
 /* Initialize a swb_config struct with configuration defaults */
-void swb_config_init(struct swb_config *cfg) {
-       swb_config_copy(cfg, &swb_config_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;
 }
 
 /* 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)
                return;
@@ -81,11 +65,12 @@ void swb_config_free(struct swb_config *cfg) {
                return;
 
        for (i = 0; swb_config_options[i].name; ++i) {
                return;
 
        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:
                if (cfg->flags & swb_config_options[i].set_mask) {
                        switch (swb_config_options[i].type) {
                          case SWB_CONFIG_OPT_STRING:
-                               free(*(char **)cfg->entries[i]);
-                               *(char **)cfg->entries[i] = NULL;
+                               free(*(char **)entry);
+                               *(char **)entry = NULL;
                                break;
                          default:
                                break;
                                break;
                          default:
                                break;
@@ -100,34 +85,36 @@ void swb_config_free(struct swb_config *cfg) {
 static int swb_config_load_option(struct swb_config *cfg,
                                  char *name, char *value) {
        struct swb_config_option *opt;
 static int swb_config_load_option(struct swb_config *cfg,
                                  char *name, char *value) {
        struct swb_config_option *opt;
-       ptrdiff_t i;
-       int retval = 0;
+       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)) {
        for (opt = swb_config_options; opt->name; ++opt) {
                if (strcmp(name, opt->name))
                        continue;
 
                if (!(cfg->flags & opt->set_mask)) {
-                       i = opt - swb_config_options;
+                       entry = (char *)cfg + opt->offset;
                        switch (opt->type) {
                          case SWB_CONFIG_OPT_STRING:
                        switch (opt->type) {
                          case SWB_CONFIG_OPT_STRING:
-                               *(char **)cfg->entries[i] = value;
+                               *(char **)entry = value;
                                break;
                          case SWB_CONFIG_OPT_INT:
                                break;
                          case SWB_CONFIG_OPT_INT:
-                               *(int *)cfg->entries[i] = atoi(value);
+                               *(int *)entry = atoi(value);
                                free(value);
                                break;
                        }
                        cfg->flags |= opt->set_mask;
                                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);
                }
                }
-               retval = 1;
-               break;
+               return 1;
        }
 
        }
 
-       if (!retval)
-               free(value);
-
-       return retval;
+       /* Unrecognized config option */
+       free(value);
+       return 0;
 }
 
 /* Read the config file and load settings into the provided swb_config struct
 }
 
 /* Read the config file and load settings into the provided swb_config struct