Merge commit 'diablo-package-3.3b1-1' into fremantle-package
[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 <stddef.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <getopt.h>
29
30 #include "config.h"
31
32 extern struct swb_config_option swb_config_options[];
33
34 static int get_config_value(char *name) {
35         struct swb_config cfg;
36         struct swb_config_option *optinfo;
37         ptrdiff_t i;
38         int retval = 1;
39
40         swb_config_init(&cfg);
41
42         if (!swb_config_load(&cfg))
43                 return 1;
44
45         for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
46                 if (strcmp(name, optinfo->name))
47                         continue;
48
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]);
54                         break;
55                   case SWB_CONFIG_OPT_INT:
56                         printf("%d\n", *(int *)cfg.entries[i]);
57                         break;
58                   default:
59                         break;
60                 }
61                 retval = 0;
62                 break;
63         }
64
65         swb_config_free(&cfg);
66
67         return retval;
68 }
69
70 static int set_config_value(char *name, char *value) {
71         struct swb_config cfg;
72         struct swb_config_option *optinfo;
73         ptrdiff_t i;
74         int retval = 1;
75
76         swb_config_init(&cfg);
77
78         if (!swb_config_load(&cfg))
79                 return 1;
80
81         for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
82                 if (strcmp(name, optinfo->name))
83                         continue;
84
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]);
91
92                         if (strlen(value) == 0) {
93                                 /* If the new value is empty, clear the config
94                                    setting */
95                                 *(char **)cfg.entries[i] = NULL;
96                                 cfg.flags &= ~optinfo->set_mask;
97                         } else {
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] =
101                                       strdup(value)))
102                                         exit(1);
103                                 cfg.flags |= optinfo->set_mask;
104                         }
105                         break;
106                   case SWB_CONFIG_OPT_INT:
107                         if (strlen(value) == 0) {
108                                 /* If the new value is empty, clear the config
109                                    setting */
110                                 cfg.flags &= ~optinfo->set_mask;
111                         } else {
112                                 *(int *)cfg.entries[i] = atoi(value);
113                                 cfg.flags |= optinfo->set_mask;
114                         }
115                         break;
116                 }
117                 retval = 0;
118                 break;
119         }
120
121         if (!retval)
122                 if (!swb_config_save(&cfg))
123                         retval = 1;
124
125         swb_config_free(&cfg);
126
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");
131
132         return retval;
133 }
134
135 void usage(void) {
136         printf("Usage:\n");
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");
141         printf("\n");
142         printf("  browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
143         printf("\n");
144         printf("  browser-switchboard-config -h -- Show this message\n");
145 }
146
147 int main(int argc, char **argv) {
148         int opt, done = 0;
149         int set = 0;
150         char *selected_opt = NULL;
151         
152         while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
153                 switch (opt) {
154                   case 'h':
155                         usage();
156                         exit(0);
157                         break;
158                   case 's':
159                         set = 1;
160                         break;
161                   case 'b':
162                         selected_opt = "default_browser";
163                         done = 1;
164                         break;
165                   case 'c':
166                         selected_opt = "other_browser_cmd";
167                         done = 1;
168                         break;
169                   case 'm':
170                         selected_opt = "continuous_mode";
171                         done = 1;
172                         break;
173                   case 'o':
174                         selected_opt = optarg;
175                         done = 1;
176                         break;
177                   default:
178                         usage();
179                         exit(1);
180                         break;
181                 }
182         }
183
184         if (!selected_opt) {
185                 printf("Must specify one of -b, -c, -m, -o\n");
186                 usage();
187                 exit(1);
188         }
189
190         if (set) {
191                 if (optind >= argc) {
192                         printf("Value to set config option to not provided\n");
193                         usage();
194                         exit(1);
195                 }
196                 return set_config_value(selected_opt, argv[optind]);
197         } else
198                 return get_config_value(selected_opt);
199 }