Add some comments to the code
[browser-switch] / main.c
1 /*
2  * main.c -- config file parsing and main loop for browser-switchboard
3  *
4  * Copyright (C) 2009 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) 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 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <regex.h>
31 #include <dbus/dbus-glib.h>
32
33 #include "browser-switchboard.h"
34 #include "launcher.h"
35 #include "dbus-server-bindings.h"
36
37 #define DEFAULT_HOMEDIR "/home/user"
38 #define CONFIGFILE_LOC "/.config/browser-switchboard"
39 #define CONFIGFILE_LOC_OLD "/.config/browser-proxy"
40 #define MAXLINE 1024
41
42 struct swb_context ctx;
43
44 static void set_config_defaults(struct swb_context *ctx) {
45         if (!ctx)
46                 return;
47         free(ctx->other_browser_cmd);
48         ctx->continuous_mode = 0;
49         ctx->default_browser_launcher = NULL;
50         ctx->other_browser_cmd = NULL;
51 }
52
53 static void waitforzombies(int signalnum) {
54         while (waitpid(-1, NULL, WNOHANG) > 0)
55                 printf("Waited for a zombie\n");
56 }
57
58 static void read_config(int signalnum) {
59         char *homedir, *configfile;
60         size_t len;
61         char buf[MAXLINE];
62         char *key, *value;
63         char *default_browser = NULL;
64         FILE *fp;
65         regex_t re_ignore, re_config1, re_config2;
66         regmatch_t substrs[3];
67
68         set_config_defaults(&ctx);
69
70         /* Put together the path to the config file */
71         if (!(homedir = getenv("HOME")))
72                 homedir = DEFAULT_HOMEDIR;
73         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
74         if (!(configfile = calloc(len, sizeof(char))))
75                 goto out_noopen;
76         snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
77
78         /* Try to open the config file */
79         if (!(fp = fopen(configfile, "r"))) {
80                 /* Try the legacy config file location before giving up
81                    XXX we assume here that CONFIGFILE_LOC_OLD is shorter
82                    than CONFIGFILE_LOC! */
83                 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
84                 if (!(fp = fopen(configfile, "r")))
85                         goto out_noopen;
86         }
87
88         /* compile regex matching blank lines or comments */
89         if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
90                 goto out_nore;
91         /* compile regex matching foo = "bar", with arbitrary whitespace at
92            beginning and end of line and surrounding the = */
93         if (regcomp(&re_config1,
94                     "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$",
95                     REG_EXTENDED)) {
96                 regfree(&re_ignore);
97                 goto out_nore;
98         }
99         /* compile regex matching foo = bar, with arbitrary whitespace at
100            beginning of line and surrounding the = */
101         if (regcomp(&re_config2,
102                     "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$",
103                     REG_EXTENDED|REG_NEWLINE)) {
104                 regfree(&re_ignore);
105                 regfree(&re_config1);
106                 goto out_nore;
107         }
108
109         /* Read in the config file one line at a time and parse it
110            XXX doesn't deal with lines longer than MAXLINE */
111         while (fgets(buf, MAXLINE, fp)) {
112                 /* skip blank lines and comments */
113                 if (!regexec(&re_ignore, buf, 0, NULL, 0))
114                         continue;
115
116                 /* Find the substrings corresponding to the key and value
117                    If the line doesn't match our idea of a config file entry,
118                    skip it */
119                 if (regexec(&re_config1, buf, 3, substrs, 0) &&
120                     regexec(&re_config2, buf, 3, substrs, 0))
121                         continue;
122                 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
123                         continue;
124
125                 /* copy the config value into a new string */
126                 len = substrs[2].rm_eo - substrs[2].rm_so;
127                 if (!(value = calloc(len+1, sizeof(char))))
128                         goto out;
129                 strncpy(value, buf+substrs[2].rm_so, len);
130                 /* calloc() zeroes the memory, so string is automatically
131                    null terminated */
132
133                 /* make key point to a null-terminated string holding the 
134                    config key */
135                 key = buf + substrs[1].rm_so;
136                 buf[substrs[1].rm_eo] = '\0';
137
138                 if (!strcmp(key, "continuous_mode")) {
139                         ctx.continuous_mode = atoi(value);
140                         free(value);
141                 } else if (!strcmp(key, "default_browser")) {
142                         if (!default_browser)
143                                 default_browser = value;
144                 } else if (!strcmp(key, "other_browser_cmd")) {
145                         if (!ctx.other_browser_cmd)
146                                 ctx.other_browser_cmd = value;
147                 } else {
148                         /* Don't need this line's contents */
149                         free(value);
150                 }
151                 value = NULL;
152         }
153
154         printf("continuous_mode: %d\n", ctx.continuous_mode);
155         printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
156         printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
157
158 out:
159         regfree(&re_ignore);
160         regfree(&re_config1);
161         regfree(&re_config2);
162 out_nore:
163         fclose(fp);
164 out_noopen:
165         update_default_browser(&ctx, default_browser);
166         free(configfile);
167         free(default_browser);
168         return;
169 }
170
171 int main() {
172         OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
173         GMainLoop *mainloop;
174         GError *error = NULL;
175
176         read_config(0);
177
178         if (ctx.continuous_mode) {
179                 /* Install signal handlers */
180                 struct sigaction act;
181                 act.sa_flags = SA_RESTART;
182                 sigemptyset(&(act.sa_mask));
183
184                 /* SIGCHLD -- clean up after zombies */
185                 act.sa_handler = waitforzombies;
186                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
187                         printf("Installing signal handler failed\n");
188                         return 1;
189                 }
190
191                 /* SIGHUP -- reread config file */
192                 act.sa_handler = read_config;
193                 if (sigaction(SIGHUP, &act, NULL) == -1) {
194                         printf("Installing signal handler failed\n");
195                         return 1;
196                 }
197         }
198
199         g_type_init();
200
201         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
202                         &dbus_glib_osso_browser_object_info);
203
204         /* Get a connection to the D-Bus session bus */
205         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
206         if (!ctx.session_bus) {
207                 printf("Couldn't get a D-Bus bus connection\n");
208                 return 1;
209         }
210         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
211                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
212                         "org.freedesktop.DBus");
213         if (!ctx.dbus_proxy) {
214                 printf("Couldn't get an org.freedesktop.DBus proxy\n");
215                 return 1;
216         }
217
218         dbus_request_osso_browser_name(&ctx);
219
220         /* Register ourselves to handle the osso_browser D-Bus methods */
221         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
222         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
223         dbus_g_connection_register_g_object(ctx.session_bus,
224                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
225         dbus_g_connection_register_g_object(ctx.session_bus,
226                         "/com/nokia/osso_browser/request",
227                         G_OBJECT(obj_osso_browser_req));
228
229         mainloop = g_main_loop_new(NULL, FALSE);
230         printf("Starting main loop\n");
231         g_main_loop_run(mainloop);
232         printf("Main loop completed\n");
233
234         return 0;
235 }