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 extern struct swb_config_option swb_config_options[];
34 static int get_config_value(char *name) {
35 struct swb_config cfg;
36 struct swb_config_option *optinfo;
40 swb_config_init(&cfg);
42 if (!swb_config_load(&cfg))
45 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
46 if (strcmp(name, optinfo->name))
49 i = optinfo - swb_config_options;
50 switch (optinfo->type) {
51 case SWB_CONFIG_OPT_STRING:
52 printf("%s\n", *(char **)cfg.entries[i]);
54 case SWB_CONFIG_OPT_INT:
55 printf("%d\n", *(int *)cfg.entries[i]);
64 swb_config_free(&cfg);
69 static int set_config_value(char *name, char *value) {
70 struct swb_config cfg;
71 struct swb_config_option *optinfo;
75 swb_config_init(&cfg);
77 if (!swb_config_load(&cfg))
80 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
81 if (strcmp(name, optinfo->name))
84 i = optinfo - swb_config_options;
85 switch (optinfo->type) {
86 case SWB_CONFIG_OPT_STRING:
87 /* Free any existing string */
88 if (cfg.flags & optinfo->set_mask)
89 free(*(char **)cfg.entries[i]);
91 if (strlen(value) == 0) {
92 /* If the new value is empty, clear the config
94 *(char **)cfg.entries[i] = NULL;
95 cfg.flags &= ~optinfo->set_mask;
97 /* Make a copy of the string -- it's not safe
98 to free value, which comes from argv */
99 if (!(*(char **)cfg.entries[i] =
102 cfg.flags |= optinfo->set_mask;
105 case SWB_CONFIG_OPT_INT:
106 if (strlen(value) == 0) {
107 /* If the new value is empty, clear the config
109 cfg.flags &= ~optinfo->set_mask;
111 *(int *)cfg.entries[i] = atoi(value);
112 cfg.flags |= optinfo->set_mask;
121 if (!swb_config_save(&cfg))
124 swb_config_free(&cfg);
126 /* Try to send SIGHUP to any running browser-switchboard process
127 This causes it to reread config files if in continuous_mode, or
128 die so that the config will be reloaded on next start otherwise */
129 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
136 printf(" browser-switchboard-config -b -- Display default browser\n");
137 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
138 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
139 printf(" browser-switchboard-config -o option -- Display value of option\n");
141 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
143 printf(" browser-switchboard-config -h -- Show this message\n");
146 int main(int argc, char **argv) {
149 char *selected_opt = NULL;
151 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
161 selected_opt = "default_browser";
165 selected_opt = "other_browser_cmd";
169 selected_opt = "continuous_mode";
173 selected_opt = optarg;
184 printf("Must specify one of -b, -c, -m, -o\n");
190 if (optind >= argc) {
191 printf("Value to set config option to not provided\n");
195 return set_config_value(selected_opt, argv[optind]);
197 return get_config_value(selected_opt);