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 extern struct swb_config_option swb_config_options[];
33 static int get_config_value(char *name) {
34 struct swb_config cfg;
36 struct swb_config_option optinfo;
39 swb_config_init(&cfg);
41 if (!swb_config_load(&cfg))
44 for (i = 0; swb_config_options[i].name; ++i) {
45 optinfo = swb_config_options[i];
46 if (strcmp(name, optinfo.name))
49 switch (optinfo.type) {
50 case SWB_CONFIG_OPT_STRING:
51 printf("%s\n", *(char **)cfg.entries[i]);
53 case SWB_CONFIG_OPT_INT:
54 printf("%d\n", *(int *)cfg.entries[i]);
63 swb_config_free(&cfg);
68 static int set_config_value(char *name, char *value) {
69 struct swb_config cfg;
71 struct swb_config_option optinfo;
74 swb_config_init(&cfg);
76 if (!swb_config_load(&cfg))
79 for (i = 0; swb_config_options[i].name; ++i) {
80 optinfo = swb_config_options[i];
81 if (strcmp(name, optinfo.name))
84 switch (optinfo.type) {
85 case SWB_CONFIG_OPT_STRING:
86 /* Free any existing string */
87 if (cfg.flags & optinfo.set_mask)
88 free(*(char **)cfg.entries[i]);
90 if (strlen(value) == 0) {
91 /* If the new value is empty, clear the config
93 *(char **)cfg.entries[i] = NULL;
94 cfg.flags &= ~optinfo.set_mask;
96 /* Make a copy of the string -- it's not safe
97 to free value, which comes from argv */
98 if (!(*(char **)cfg.entries[i] =
101 cfg.flags |= optinfo.set_mask;
104 case SWB_CONFIG_OPT_INT:
105 if (strlen(value) == 0) {
106 /* If the new value is empty, clear the config
108 cfg.flags &= ~optinfo.set_mask;
110 *(int *)cfg.entries[i] = atoi(value);
111 cfg.flags |= optinfo.set_mask;
120 if (!swb_config_save(&cfg))
123 swb_config_free(&cfg);
125 /* Try to send SIGHUP to any running browser-switchboard process
126 This causes it to reread config files if in continuous_mode, or
127 die so that the config will be reloaded on next start otherwise */
128 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
135 printf(" browser-switchboard-config -b -- Display default browser\n");
136 printf(" browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
137 printf(" browser-switchboard-config -m -- Display continuous mode setting\n");
138 printf(" browser-switchboard-config -o option -- Display value of option\n");
140 printf(" browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
142 printf(" browser-switchboard-config -h -- Show this message\n");
145 int main(int argc, char **argv) {
148 char *selected_opt = NULL;
150 while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
160 selected_opt = "default_browser";
164 selected_opt = "other_browser_cmd";
168 selected_opt = "continuous_mode";
172 selected_opt = optarg;
183 printf("Must specify one of -b, -c, -m, -o\n");
189 if (optind >= argc) {
190 printf("Value to set config option to not provided\n");
194 return set_config_value(selected_opt, argv[optind]);
196 return get_config_value(selected_opt);