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,
32 #include "save-config.h"
35 extern struct swb_config_option swb_config_options[];
37 static int get_config_value(char *name) {
38 struct swb_config cfg;
39 struct swb_config_option *optinfo;
43 swb_config_init(&cfg);
45 if (!swb_config_load(&cfg))
48 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
49 if (strcmp(name, optinfo->name))
52 i = optinfo - swb_config_options;
53 switch (optinfo->type) {
54 case SWB_CONFIG_OPT_STRING:
55 if (*(char **)cfg.entries[i])
56 printf("%s\n", *(char **)cfg.entries[i]);
58 case SWB_CONFIG_OPT_INT:
59 printf("%d\n", *(int *)cfg.entries[i]);
68 swb_config_free(&cfg);
73 static int get_default_browser(void) {
74 struct swb_config cfg;
77 swb_config_init(&cfg);
79 if (!swb_config_load(&cfg))
82 /* Check to see if the configured default browser is installed
83 If not, report the default default browser */
84 for (i = 0; browsers[i].config; ++i) {
85 if (strcmp(browsers[i].config, cfg.default_browser))
88 if (browsers[i].binary && access(browsers[i].binary, X_OK))
89 printf("%s\n", browsers[0].config);
91 printf("%s\n", browsers[i].config);
96 if (!browsers[i].config)
97 /* Unknown browser configured as default, report the default
99 printf("%s\n", browsers[0].config);
101 swb_config_free(&cfg);
106 static int set_config_value(char *name, char *value) {
107 struct swb_config orig_cfg, cfg;
108 struct swb_config_option *optinfo;
112 swb_config_init(&orig_cfg);
114 if (!swb_config_load(&orig_cfg))
117 swb_config_copy(&cfg, &orig_cfg);
119 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
120 if (strcmp(name, optinfo->name))
123 i = optinfo - swb_config_options;
124 switch (optinfo->type) {
125 case SWB_CONFIG_OPT_STRING:
126 if (strlen(value) == 0) {
127 /* If the new value is empty, clear the config
129 *(char **)cfg.entries[i] = NULL;
130 cfg.flags &= ~optinfo->set_mask;
132 /* Make a copy of the string -- it's not safe
133 to free value, which comes from argv */
134 if (!(*(char **)cfg.entries[i] =
137 cfg.flags |= optinfo->set_mask;
140 case SWB_CONFIG_OPT_INT:
141 if (strlen(value) == 0) {
142 /* If the new value is empty, clear the config
144 cfg.flags &= ~optinfo->set_mask;
146 *(int *)cfg.entries[i] = atoi(value);
147 cfg.flags |= optinfo->set_mask;
156 if (!swb_config_save(&cfg))
159 /* Reconfigure a running browser-switchboard, if present */
160 swb_reconfig(&orig_cfg, &cfg);
162 swb_config_free(&orig_cfg);
163 /* XXX can't free all of cfg, it contains pointers to memory we just
165 swb_config_free(&cfg); */
166 if (optinfo->name && optinfo->type == SWB_CONFIG_OPT_STRING)
167 free(*(char **)cfg.entries[i]);
174 printf(" browser-switchboard-config -b -- Display default browser\n");
175 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
176 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
177 printf(" browser-switchboard-config -o option -- Display value of option\n");
179 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
181 printf(" browser-switchboard-config -h -- Show this message\n");
184 int main(int argc, char **argv) {
187 char *selected_opt = NULL;
189 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
199 selected_opt = "default_browser";
203 selected_opt = "other_browser_cmd";
207 selected_opt = "continuous_mode";
211 selected_opt = optarg;
222 printf("Must specify one of -b, -c, -m, -o\n");
228 if (optind >= argc) {
229 printf("Value to set config option to not provided\n");
233 return set_config_value(selected_opt, argv[optind]);
234 } else if (!strcmp(selected_opt, "default_browser"))
235 /* Default browser value needs special handling */
236 return get_default_browser();
238 return get_config_value(selected_opt);