2 * browser-switchboard-config.c -- command-line configuration utility for
5 * Copyright (C) 2009-2010 Steven Luo
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
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,
31 #include "save-config.h"
34 extern struct swb_config_option swb_config_options[];
36 static int get_config_value(char *name) {
37 struct swb_config cfg;
38 struct swb_config_option *optinfo;
42 swb_config_init(&cfg);
44 if (!swb_config_load(&cfg))
47 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
48 if (strcmp(name, optinfo->name))
51 entry = (char *)&cfg + optinfo->offset;
52 switch (optinfo->type) {
53 case SWB_CONFIG_OPT_STRING:
55 printf("%s\n", *(char **)entry);
57 case SWB_CONFIG_OPT_INT:
58 printf("%d\n", *(int *)entry);
67 swb_config_free(&cfg);
72 static int get_default_browser(void) {
73 struct swb_config cfg;
76 swb_config_init(&cfg);
78 if (!swb_config_load(&cfg))
81 /* Check to see if the configured default browser is installed
82 If not, report the default default browser */
83 for (i = 0; browsers[i].config; ++i) {
84 if (strcmp(browsers[i].config, cfg.default_browser))
87 if (browsers[i].binary && access(browsers[i].binary, X_OK))
88 printf("%s\n", browsers[0].config);
90 printf("%s\n", browsers[i].config);
95 if (!browsers[i].config)
96 /* Unknown browser configured as default, report the default
98 printf("%s\n", browsers[0].config);
100 swb_config_free(&cfg);
105 static int set_config_value(char *name, char *value) {
106 struct swb_config orig_cfg, cfg;
107 struct swb_config_option *optinfo;
111 swb_config_init(&orig_cfg);
113 if (!swb_config_load(&orig_cfg))
118 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
119 if (strcmp(name, optinfo->name))
122 entry = (char *)&cfg + optinfo->offset;
123 switch (optinfo->type) {
124 case SWB_CONFIG_OPT_STRING:
125 if (strlen(value) == 0) {
126 /* If the new value is empty, clear the config
128 *(char **)entry = NULL;
129 cfg.flags &= ~optinfo->set_mask;
131 /* Make a copy of the string -- it's not safe
132 to free value, which comes from argv */
133 if (!(*(char **)entry =
136 cfg.flags |= optinfo->set_mask;
139 case SWB_CONFIG_OPT_INT:
140 if (strlen(value) == 0) {
141 /* If the new value is empty, clear the config
143 cfg.flags &= ~optinfo->set_mask;
145 *(int *)entry = atoi(value);
146 cfg.flags |= optinfo->set_mask;
155 if (!swb_config_save(&cfg))
158 /* Reconfigure a running browser-switchboard, if present */
159 swb_reconfig(&orig_cfg, &cfg);
161 swb_config_free(&orig_cfg);
162 /* XXX can't free all of cfg, it contains pointers to memory we just
164 swb_config_free(&cfg); */
165 if (optinfo->name && optinfo->type == SWB_CONFIG_OPT_STRING)
166 free(*(char **)entry);
173 printf(" browser-switchboard-config -b -- Display default browser\n");
174 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
175 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
176 printf(" browser-switchboard-config -o option -- Display value of option\n");
178 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
180 printf(" browser-switchboard-config -h -- Show this message\n");
183 int main(int argc, char **argv) {
186 char *selected_opt = NULL;
188 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
198 selected_opt = "default_browser";
202 selected_opt = "other_browser_cmd";
206 selected_opt = "continuous_mode";
210 selected_opt = optarg;
221 printf("Must specify one of -b, -c, -m, -o\n");
227 if (optind >= argc) {
228 printf("Value to set config option to not provided\n");
232 return set_config_value(selected_opt, argv[optind]);
233 } else if (!strcmp(selected_opt, "default_browser"))
234 /* Default browser value needs special handling */
235 return get_default_browser();
237 return get_config_value(selected_opt);