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 if (*(char **)cfg.entries[i])
53 printf("%s\n", *(char **)cfg.entries[i]);
55 case SWB_CONFIG_OPT_INT:
56 printf("%d\n", *(int *)cfg.entries[i]);
65 swb_config_free(&cfg);
70 static int set_config_value(char *name, char *value) {
71 struct swb_config cfg;
72 struct swb_config_option *optinfo;
76 swb_config_init(&cfg);
78 if (!swb_config_load(&cfg))
81 for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
82 if (strcmp(name, optinfo->name))
85 i = optinfo - swb_config_options;
86 switch (optinfo->type) {
87 case SWB_CONFIG_OPT_STRING:
88 /* Free any existing string */
89 if (cfg.flags & optinfo->set_mask)
90 free(*(char **)cfg.entries[i]);
92 if (strlen(value) == 0) {
93 /* If the new value is empty, clear the config
95 *(char **)cfg.entries[i] = NULL;
96 cfg.flags &= ~optinfo->set_mask;
98 /* Make a copy of the string -- it's not safe
99 to free value, which comes from argv */
100 if (!(*(char **)cfg.entries[i] =
103 cfg.flags |= optinfo->set_mask;
106 case SWB_CONFIG_OPT_INT:
107 if (strlen(value) == 0) {
108 /* If the new value is empty, clear the config
110 cfg.flags &= ~optinfo->set_mask;
112 *(int *)cfg.entries[i] = atoi(value);
113 cfg.flags |= optinfo->set_mask;
122 if (!swb_config_save(&cfg))
125 swb_config_free(&cfg);
127 /* Try to send SIGHUP to any running browser-switchboard process
128 This causes it to reread config files if in continuous_mode, or
129 die so that the config will be reloaded on next start otherwise */
130 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
137 printf(" browser-switchboard-config -b -- Display default browser\n");
138 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
139 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
140 printf(" browser-switchboard-config -o option -- Display value of option\n");
142 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
144 printf(" browser-switchboard-config -h -- Show this message\n");
147 int main(int argc, char **argv) {
150 char *selected_opt = NULL;
152 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
162 selected_opt = "default_browser";
166 selected_opt = "other_browser_cmd";
170 selected_opt = "continuous_mode";
174 selected_opt = optarg;
185 printf("Must specify one of -b, -c, -m, -o\n");
191 if (optind >= argc) {
192 printf("Value to set config option to not provided\n");
196 return set_config_value(selected_opt, argv[optind]);
198 return get_config_value(selected_opt);