663858a0d931be65d71905c697fbdffdb867f91d
[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 <string.h>
25
26 #include "configfile.h"
27 #include "config.h"
28
29 /* Initialize a swb_config struct with configuration defaults */
30 void swb_config_init(struct swb_config *cfg) {
31         if (!cfg)
32                 return;
33
34         cfg->continuous_mode = 0;
35         cfg->default_browser = "microb";
36         cfg->other_browser_cmd = NULL;
37         cfg->logging = "stdout";
38
39         cfg->flags = SWB_CONFIG_INITIALIZED;
40 }
41
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) {
45         if (!cfg)
46                 return;
47         if (!(cfg->flags & SWB_CONFIG_INITIALIZED))
48                 return;
49
50         if (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET) {
51                 free(cfg->default_browser);
52                 cfg->default_browser = NULL;
53         }
54         if (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET) {
55                 free(cfg->other_browser_cmd);
56                 cfg->other_browser_cmd = NULL;
57         }
58         if (cfg->flags & SWB_CONFIG_LOGGING_SET) {
59                 free(cfg->logging);
60                 cfg->logging = NULL;
61         }
62
63         cfg->flags = 0;
64 }
65
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) {
70         FILE *fp;
71         struct swb_config_line line;
72
73         if (!cfg || !(cfg->flags & SWB_CONFIG_INITIALIZED))
74                 return 0;
75
76         if (!(fp = open_config_file()))
77                 goto out_noopen;
78
79         /* Parse the config file
80            TODO: should we handle errors differently than EOF? */
81         if (!parse_config_file_begin())
82                 goto out;
83         while (!parse_config_file_line(fp, &line)) {
84                 if (line.parsed) {
85                         if (!strcmp(line.key, "continuous_mode")) {
86                                 if (!(cfg->flags &
87                                       SWB_CONFIG_CONTINUOUS_MODE_SET)) {
88                                         cfg->continuous_mode = atoi(line.value);
89                                         cfg->flags |=
90                                                 SWB_CONFIG_CONTINUOUS_MODE_SET;
91                                 }
92                                 free(line.value);
93                         } else if (!strcmp(line.key, "default_browser")) {
94                                 if (!(cfg->flags &
95                                       SWB_CONFIG_DEFAULT_BROWSER_SET)) {
96                                         cfg->default_browser = line.value;
97                                         cfg->flags |=
98                                                 SWB_CONFIG_DEFAULT_BROWSER_SET;
99                                 }
100                         } else if (!strcmp(line.key, "other_browser_cmd")) {
101                                 if (!(cfg->flags &
102                                       SWB_CONFIG_OTHER_BROWSER_CMD_SET)) {
103                                         cfg->other_browser_cmd = line.value;
104                                         cfg->flags |=
105                                                 SWB_CONFIG_OTHER_BROWSER_CMD_SET;
106                                 }
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;
111                                 }
112                         } else {
113                                 /* Don't need this line's contents */
114                                 free(line.value);
115                         }
116                 }
117                 free(line.key);
118         }
119         parse_config_file_end();
120
121 out:
122         fclose(fp);
123 out_noopen:
124         return 1;
125 }