Avoid structure copies when looping through swb_config_options
[browser-switch] / config.c
1 /*
2  * config.c -- configuration functions for Browser Switchboard
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26
27 #include "configfile.h"
28 #include "config.h"
29
30 /* The Browser Switchboard config file options */
31 struct swb_config_option swb_config_options[] = {
32         { "continuous_mode", SWB_CONFIG_OPT_INT, SWB_CONFIG_CONTINUOUS_MODE_SET },
33         { "default_browser", SWB_CONFIG_OPT_STRING, SWB_CONFIG_DEFAULT_BROWSER_SET },
34         { "other_browser_cmd", SWB_CONFIG_OPT_STRING, SWB_CONFIG_OTHER_BROWSER_CMD_SET },
35         { "logging", SWB_CONFIG_OPT_STRING, SWB_CONFIG_LOGGING_SET },
36         { NULL, 0, 0 },
37 };
38
39 /* Browser Switchboard configuration defaults */
40 static struct swb_config swb_config_defaults = {
41         .flags = SWB_CONFIG_INITIALIZED,
42         .continuous_mode = 0,
43         .default_browser = "microb",
44         .other_browser_cmd = NULL,
45         .logging = "stdout",
46 };
47
48
49 /* Copy the contents of an swb_config struct
50    The entries[] array means that the standard copy will not work */
51 void swb_config_copy(struct swb_config *dst, struct swb_config *src) {
52         if (!dst || !src)
53                 return;
54
55         dst->entries[0] = &(dst->continuous_mode);
56         dst->entries[1] = &(dst->default_browser);
57         dst->entries[2] = &(dst->other_browser_cmd);
58         dst->entries[3] = &(dst->logging);
59
60         dst->flags = src->flags;
61
62         dst->continuous_mode = src->continuous_mode;
63         dst->default_browser = src->default_browser;
64         dst->other_browser_cmd = src->other_browser_cmd;
65         dst->logging = src->logging;
66 }
67
68 /* Initialize a swb_config struct with configuration defaults */
69 void swb_config_init(struct swb_config *cfg) {
70         swb_config_copy(cfg, &swb_config_defaults);
71 }
72
73 /* Free all heap memory used in an swb_config struct
74    This MUST NOT be done if any of the strings are being used elsewhere! */
75 void swb_config_free(struct swb_config *cfg) {
76         if (!cfg)
77                 return;
78         if (!(cfg->flags & SWB_CONFIG_INITIALIZED))
79                 return;
80
81         if (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET) {
82                 free(cfg->default_browser);
83                 cfg->default_browser = NULL;
84         }
85         if (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET) {
86                 free(cfg->other_browser_cmd);
87                 cfg->other_browser_cmd = NULL;
88         }
89         if (cfg->flags & SWB_CONFIG_LOGGING_SET) {
90                 free(cfg->logging);
91                 cfg->logging = NULL;
92         }
93
94         cfg->flags = 0;
95 }
96
97 /* Load a value into the part of a struct swb_config indicated by name */
98 static int swb_config_load_option(struct swb_config *cfg,
99                                   char *name, char *value) {
100         struct swb_config_option *opt;
101         ptrdiff_t i;
102         int retval = 0;
103
104         for (opt = swb_config_options; opt->name; ++opt) {
105                 if (strcmp(name, opt->name))
106                         continue;
107
108                 if (!(cfg->flags & opt->set_mask)) {
109                         i = opt - swb_config_options;
110                         switch (opt->type) {
111                           case SWB_CONFIG_OPT_STRING:
112                                 *(char **)cfg->entries[i] = value;
113                                 break;
114                           case SWB_CONFIG_OPT_INT:
115                                 *(int *)cfg->entries[i] = atoi(value);
116                                 free(value);
117                                 break;
118                         }
119                         cfg->flags |= opt->set_mask;
120                 }
121                 retval = 1;
122                 break;
123         }
124
125         if (!retval)
126                 free(value);
127
128         return retval;
129 }
130
131 /* Read the config file and load settings into the provided swb_config struct
132    Caller is responsible for freeing allocated strings with free()
133    Returns true on success, false otherwise */
134 int swb_config_load(struct swb_config *cfg) {
135         FILE *fp;
136         struct swb_config_line line;
137
138         if (!cfg || !(cfg->flags & SWB_CONFIG_INITIALIZED))
139                 return 0;
140
141         if (!(fp = open_config_file()))
142                 goto out_noopen;
143
144         /* Parse the config file
145            TODO: should we handle errors differently than EOF? */
146         if (!parse_config_file_begin())
147                 goto out;
148         while (!parse_config_file_line(fp, &line)) {
149                 if (line.parsed)
150                         swb_config_load_option(cfg, line.key, line.value);
151                 free(line.key);
152         }
153         parse_config_file_end();
154
155 out:
156         fclose(fp);
157 out_noopen:
158         return 1;
159 }