2 * config.c -- configuration functions for Browser Switchboard
4 * Copyright (C) 2009-2010 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
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.
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.
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,
26 #include "configfile.h"
29 /* Initialize a swb_config struct with configuration defaults */
30 void swb_config_init(struct swb_config *cfg) {
34 cfg->continuous_mode = 0;
35 cfg->default_browser = "microb";
36 cfg->other_browser_cmd = NULL;
37 cfg->logging = "stdout";
39 cfg->flags = SWB_CONFIG_INITIALIZED;
42 /* Free all heap memory used in an swb_config struct
43 This MUST NOT be done if any of the strings are being used elsewhere! */
44 void swb_config_free(struct swb_config *cfg) {
47 if (!(cfg->flags & SWB_CONFIG_INITIALIZED))
50 if (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET) {
51 free(cfg->default_browser);
52 cfg->default_browser = NULL;
54 if (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET) {
55 free(cfg->other_browser_cmd);
56 cfg->other_browser_cmd = NULL;
58 if (cfg->flags & SWB_CONFIG_LOGGING_SET) {
66 /* Read the config file and load settings into the provided swb_config struct
67 Caller is responsible for freeing allocated strings with free()
68 Returns true on success, false otherwise */
69 int swb_config_load(struct swb_config *cfg) {
71 struct swb_config_line line;
73 if (!cfg || !(cfg->flags & SWB_CONFIG_INITIALIZED))
76 if (!(fp = open_config_file()))
79 /* Parse the config file
80 TODO: should we handle errors differently than EOF? */
81 if (!parse_config_file_begin())
83 while (!parse_config_file_line(fp, &line)) {
85 if (!strcmp(line.key, "continuous_mode")) {
87 SWB_CONFIG_CONTINUOUS_MODE_SET)) {
88 cfg->continuous_mode = atoi(line.value);
90 SWB_CONFIG_CONTINUOUS_MODE_SET;
93 } else if (!strcmp(line.key, "default_browser")) {
95 SWB_CONFIG_DEFAULT_BROWSER_SET)) {
96 cfg->default_browser = line.value;
98 SWB_CONFIG_DEFAULT_BROWSER_SET;
100 } else if (!strcmp(line.key, "other_browser_cmd")) {
102 SWB_CONFIG_OTHER_BROWSER_CMD_SET)) {
103 cfg->other_browser_cmd = line.value;
105 SWB_CONFIG_OTHER_BROWSER_CMD_SET;
107 } else if (!strcmp(line.key, "logging")) {
108 if (!(cfg->flags & SWB_CONFIG_LOGGING_SET)) {
109 cfg->logging = line.value;
110 cfg->flags |= SWB_CONFIG_LOGGING_SET;
113 /* Don't need this line's contents */
119 parse_config_file_end();