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,
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 i = optinfo - swb_config_options;
52 switch (optinfo->type) {
53 case SWB_CONFIG_OPT_STRING:
54 if (*(char **)cfg.entries[i])
55 printf("%s\n", *(char **)cfg.entries[i]);
57 case SWB_CONFIG_OPT_INT:
58 printf("%d\n", *(int *)cfg.entries[i]);
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 cfg;
107 struct swb_config_option *optinfo;
111 swb_config_init(&cfg);
113 if (!swb_config_load(&cfg))
116 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
117 if (strcmp(name, optinfo->name))
120 i = optinfo - swb_config_options;
121 switch (optinfo->type) {
122 case SWB_CONFIG_OPT_STRING:
123 /* Free any existing string */
124 if (cfg.flags & optinfo->set_mask)
125 free(*(char **)cfg.entries[i]);
127 if (strlen(value) == 0) {
128 /* If the new value is empty, clear the config
130 *(char **)cfg.entries[i] = NULL;
131 cfg.flags &= ~optinfo->set_mask;
133 /* Make a copy of the string -- it's not safe
134 to free value, which comes from argv */
135 if (!(*(char **)cfg.entries[i] =
138 cfg.flags |= optinfo->set_mask;
141 case SWB_CONFIG_OPT_INT:
142 if (strlen(value) == 0) {
143 /* If the new value is empty, clear the config
145 cfg.flags &= ~optinfo->set_mask;
147 *(int *)cfg.entries[i] = atoi(value);
148 cfg.flags |= optinfo->set_mask;
157 if (!swb_config_save(&cfg))
160 swb_config_free(&cfg);
162 /* Try to send SIGHUP to any running browser-switchboard process
163 This causes it to reread config files if in continuous_mode, or
164 die so that the config will be reloaded on next start otherwise */
165 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
172 printf(" browser-switchboard-config -b -- Display default browser\n");
173 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
174 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
175 printf(" browser-switchboard-config -o option -- Display value of option\n");
177 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
179 printf(" browser-switchboard-config -h -- Show this message\n");
182 int main(int argc, char **argv) {
185 char *selected_opt = NULL;
187 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
197 selected_opt = "default_browser";
201 selected_opt = "other_browser_cmd";
205 selected_opt = "continuous_mode";
209 selected_opt = optarg;
220 printf("Must specify one of -b, -c, -m, -o\n");
226 if (optind >= argc) {
227 printf("Value to set config option to not provided\n");
231 return set_config_value(selected_opt, argv[optind]);
232 } else if (!strcmp(selected_opt, "default_browser"))
233 /* Default browser value needs special handling */
234 return get_default_browser();
236 return get_config_value(selected_opt);