Remove use_other_browser_cmd()
[browser-switch] / config-ui / browser-switchboard-config.c
1 /*
2  * browser-switchboard-config.c -- command-line configuration utility for 
3  * Browser Switchboard
4  * 
5  * Copyright (C) 2009-2010 Steven Luo
6  * 
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)
10  * any later version.
11  *
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.
16  *
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,
20  * USA.
21  */
22
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <getopt.h>
28
29 #include "config.h"
30
31 extern struct swb_config_option swb_config_options[];
32
33 static int get_config_value(char *name) {
34         struct swb_config cfg;
35         int i;
36         struct swb_config_option optinfo;
37         int retval = 1;
38
39         swb_config_init(&cfg);
40
41         if (!swb_config_load(&cfg))
42                 return 1;
43
44         for (i = 0; swb_config_options[i].name; ++i) {
45                 optinfo = swb_config_options[i];
46                 if (strcmp(name, optinfo.name))
47                         continue;
48
49                 switch (optinfo.type) {
50                   case SWB_CONFIG_OPT_STRING:
51                         printf("%s\n", *(char **)cfg.entries[i]);
52                         break;
53                   case SWB_CONFIG_OPT_INT:
54                         printf("%d\n", *(int *)cfg.entries[i]);
55                         break;
56                   default:
57                         break;
58                 }
59                 retval = 0;
60                 break;
61         }
62
63         swb_config_free(&cfg);
64
65         return retval;
66 }
67
68 static int set_config_value(char *name, char *value) {
69         struct swb_config cfg;
70         int i;
71         struct swb_config_option optinfo;
72         int retval = 1;
73
74         swb_config_init(&cfg);
75
76         if (!swb_config_load(&cfg))
77                 return 1;
78
79         for (i = 0; swb_config_options[i].name; ++i) {
80                 optinfo = swb_config_options[i];
81                 if (strcmp(name, optinfo.name))
82                         continue;
83
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]);
89
90                         if (strlen(value) == 0) {
91                                 /* If the new value is empty, clear the config
92                                    setting */
93                                 *(char **)cfg.entries[i] = NULL;
94                                 cfg.flags &= ~optinfo.set_mask;
95                         } else {
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] =
99                                       strdup(value)))
100                                         exit(1);
101                                 cfg.flags |= optinfo.set_mask;
102                         }
103                         break;
104                   case SWB_CONFIG_OPT_INT:
105                         if (strlen(value) == 0) {
106                                 /* If the new value is empty, clear the config
107                                    setting */
108                                 cfg.flags &= ~optinfo.set_mask;
109                         } else {
110                                 *(int *)cfg.entries[i] = atoi(value);
111                                 cfg.flags |= optinfo.set_mask;
112                         }
113                         break;
114                 }
115                 retval = 0;
116                 break;
117         }
118
119         if (!retval)
120                 if (!swb_config_save(&cfg))
121                         retval = 1;
122
123         swb_config_free(&cfg);
124
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");
129
130         return retval;
131 }
132
133 void usage(void) {
134         printf("Usage:\n");
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");
139         printf("\n");
140         printf("  browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
141         printf("\n");
142         printf("  browser-switchboard-config -h -- Show this message\n");
143 }
144
145 int main(int argc, char **argv) {
146         int opt, done = 0;
147         int set = 0;
148         char *selected_opt = NULL;
149         
150         while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
151                 switch (opt) {
152                   case 'h':
153                         usage();
154                         exit(0);
155                         break;
156                   case 's':
157                         set = 1;
158                         break;
159                   case 'b':
160                         selected_opt = "default_browser";
161                         done = 1;
162                         break;
163                   case 'c':
164                         selected_opt = "other_browser_cmd";
165                         done = 1;
166                         break;
167                   case 'm':
168                         selected_opt = "continuous_mode";
169                         done = 1;
170                         break;
171                   case 'o':
172                         selected_opt = optarg;
173                         done = 1;
174                         break;
175                   default:
176                         usage();
177                         exit(1);
178                         break;
179                 }
180         }
181
182         if (!selected_opt) {
183                 printf("Must specify one of -b, -c, -m, -o\n");
184                 usage();
185                 exit(1);
186         }
187
188         if (set) {
189                 if (optind >= argc) {
190                         printf("Value to set config option to not provided\n");
191                         usage();
192                         exit(1);
193                 }
194                 return set_config_value(selected_opt, argv[optind]);
195         } else
196                 return get_config_value(selected_opt);
197 }