84b78e01b4892762084c0371e70cc8560f3a2170
[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 #include "configfile.h"
37
38 struct swb_context ctx;
39
40 static void set_config_defaults(struct swb_context *ctx) {
41         if (!ctx)
42                 return;
43         free(ctx->other_browser_cmd);
44         ctx->continuous_mode = 0;
45         ctx->default_browser_launcher = NULL;
46         ctx->other_browser_cmd = NULL;
47 }
48
49 static void waitforzombies(int signalnum) {
50         while (waitpid(-1, NULL, WNOHANG) > 0)
51                 printf("Waited for a zombie\n");
52 }
53
54 static void read_config(int signalnum) {
55         FILE *fp;
56         regex_t re_ignore, re_config1, re_config2;
57         regmatch_t substrs[3];
58         char buf[MAXLINE];
59         char *key, *value;
60         char *default_browser = NULL;
61         size_t len;
62
63         set_config_defaults(&ctx);
64
65         if (!(fp = open_config_file()))
66                 goto out_noopen;
67
68         /* compile regex matching blank lines or comments */
69         if (regcomp(&re_ignore, REGEX_IGNORE, REGEX_IGNORE_FLAGS))
70                 goto out_nore;
71         /* compile regex matching foo = "bar", with arbitrary whitespace at
72            beginning and end of line and surrounding the = */
73         if (regcomp(&re_config1, REGEX_CONFIG1, REGEX_CONFIG1_FLAGS)) {
74                 regfree(&re_ignore);
75                 goto out_nore;
76         }
77         /* compile regex matching foo = bar, with arbitrary whitespace at
78            beginning of line and surrounding the = */
79         if (regcomp(&re_config2, REGEX_CONFIG2, REGEX_CONFIG2_FLAGS)) {
80                 regfree(&re_ignore);
81                 regfree(&re_config1);
82                 goto out_nore;
83         }
84
85         /* Read in the config file one line at a time and parse it
86            XXX doesn't deal with lines longer than MAXLINE */
87         while (fgets(buf, MAXLINE, fp)) {
88                 /* skip blank lines and comments */
89                 if (!regexec(&re_ignore, buf, 0, NULL, 0))
90                         continue;
91
92                 /* Find the substrings corresponding to the key and value
93                    If the line doesn't match our idea of a config file entry,
94                    skip it */
95                 if (regexec(&re_config1, buf, 3, substrs, 0) &&
96                     regexec(&re_config2, buf, 3, substrs, 0))
97                         continue;
98                 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
99                         continue;
100
101                 /* copy the config value into a new string */
102                 len = substrs[2].rm_eo - substrs[2].rm_so;
103                 if (!(value = calloc(len+1, sizeof(char))))
104                         goto out;
105                 strncpy(value, buf+substrs[2].rm_so, len);
106                 /* calloc() zeroes the memory, so string is automatically
107                    null terminated */
108
109                 /* make key point to a null-terminated string holding the 
110                    config key */
111                 key = buf + substrs[1].rm_so;
112                 buf[substrs[1].rm_eo] = '\0';
113
114                 if (!strcmp(key, "continuous_mode")) {
115                         ctx.continuous_mode = atoi(value);
116                         free(value);
117                 } else if (!strcmp(key, "default_browser")) {
118                         if (!default_browser)
119                                 default_browser = value;
120                 } else if (!strcmp(key, "other_browser_cmd")) {
121                         if (!ctx.other_browser_cmd)
122                                 ctx.other_browser_cmd = value;
123                 } else {
124                         /* Don't need this line's contents */
125                         free(value);
126                 }
127                 value = NULL;
128         }
129
130         printf("continuous_mode: %d\n", ctx.continuous_mode);
131         printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
132         printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
133
134 out:
135         regfree(&re_ignore);
136         regfree(&re_config1);
137         regfree(&re_config2);
138 out_nore:
139         fclose(fp);
140 out_noopen:
141         update_default_browser(&ctx, default_browser);
142         free(default_browser);
143         return;
144 }
145
146 int main() {
147         OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
148         GMainLoop *mainloop;
149         GError *error = NULL;
150
151         read_config(0);
152
153         if (ctx.continuous_mode) {
154                 /* Install signal handlers */
155                 struct sigaction act;
156                 act.sa_flags = SA_RESTART;
157                 sigemptyset(&(act.sa_mask));
158
159                 /* SIGCHLD -- clean up after zombies */
160                 act.sa_handler = waitforzombies;
161                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
162                         printf("Installing signal handler failed\n");
163                         return 1;
164                 }
165
166                 /* SIGHUP -- reread config file */
167                 act.sa_handler = read_config;
168                 if (sigaction(SIGHUP, &act, NULL) == -1) {
169                         printf("Installing signal handler failed\n");
170                         return 1;
171                 }
172         }
173
174         g_type_init();
175
176         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
177                         &dbus_glib_osso_browser_object_info);
178
179         /* Get a connection to the D-Bus session bus */
180         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
181         if (!ctx.session_bus) {
182                 printf("Couldn't get a D-Bus bus connection\n");
183                 return 1;
184         }
185         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
186                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
187                         "org.freedesktop.DBus");
188         if (!ctx.dbus_proxy) {
189                 printf("Couldn't get an org.freedesktop.DBus proxy\n");
190                 return 1;
191         }
192
193         dbus_request_osso_browser_name(&ctx);
194
195         /* Register ourselves to handle the osso_browser D-Bus methods */
196         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
197         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
198         dbus_g_connection_register_g_object(ctx.session_bus,
199                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
200         dbus_g_connection_register_g_object(ctx.session_bus,
201                         "/com/nokia/osso_browser/request",
202                         G_OBJECT(obj_osso_browser_req));
203
204         mainloop = g_main_loop_new(NULL, FALSE);
205         printf("Starting main loop\n");
206         g_main_loop_run(mainloop);
207         printf("Main loop completed\n");
208
209         return 0;
210 }