2 * main.c -- config file parsing and main loop for browser-switchboard
4 * Copyright (C) 2009 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
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.
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,
28 #include <sys/types.h>
31 #include <dbus/dbus-glib.h>
33 #include "browser-switchboard.h"
35 #include "dbus-server-bindings.h"
37 #define DEFAULT_HOMEDIR "/home/user"
38 #define CONFIGFILE_LOC "/.config/browser-switchboard"
39 #define CONFIGFILE_LOC_OLD "/.config/browser-proxy"
42 struct swb_context ctx;
44 static void set_config_defaults(struct swb_context *ctx) {
47 free(ctx->other_browser_cmd);
48 ctx->continuous_mode = 0;
49 ctx->default_browser_launcher = NULL;
50 ctx->other_browser_cmd = NULL;
53 static void waitforzombies(int signalnum) {
54 while (waitpid(-1, NULL, WNOHANG) > 0)
55 printf("Waited for a zombie\n");
58 static void read_config(int signalnum) {
59 char *homedir, *configfile;
63 char *default_browser = NULL;
65 regex_t re_ignore, re_config1, re_config2;
66 regmatch_t substrs[3];
68 set_config_defaults(&ctx);
70 if (!(homedir = getenv("HOME")))
71 homedir = DEFAULT_HOMEDIR;
72 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
73 if (!(configfile = calloc(len, sizeof(char))))
75 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
77 if (!(fp = fopen(configfile, "r"))) {
78 /* Try the legacy config file location before giving up
79 XXX we assume here that CONFIGFILE_LOC_OLD is shorter
80 than CONFIGFILE_LOC! */
81 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
82 if (!(fp = fopen(configfile, "r")))
86 /* compile regex matching blank lines or comments */
87 if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
89 /* compile regex matching foo = "bar", with arbitrary whitespace at
90 beginning and end of line and surrounding the = */
91 if (regcomp(&re_config1,
92 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$",
97 /* compile regex matching foo = bar, with arbitrary whitespace at
98 beginning of line and surrounding the = */
99 if (regcomp(&re_config2,
100 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$",
101 REG_EXTENDED|REG_NEWLINE)) {
103 regfree(&re_config1);
107 /* Read in the config file one line at a time and parse it
108 XXX doesn't deal with lines longer than MAXLINE */
109 while (fgets(buf, MAXLINE, fp)) {
110 /* skip blank lines and comments */
111 if (!regexec(&re_ignore, buf, 0, NULL, 0))
114 /* Find the substrings corresponding to the key and value
115 If the line doesn't match our idea of a config file entry,
117 if (regexec(&re_config1, buf, 3, substrs, 0) &&
118 regexec(&re_config2, buf, 3, substrs, 0))
120 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
123 /* copy the config value into a new string */
124 len = substrs[2].rm_eo - substrs[2].rm_so;
125 if (!(value = calloc(len+1, sizeof(char))))
127 strncpy(value, buf+substrs[2].rm_so, len);
128 /* calloc() zeroes the memory, so string is automatically
131 /* make key point to a null-terminated string holding the
133 key = buf + substrs[1].rm_so;
134 buf[substrs[1].rm_eo] = '\0';
136 if (!strcmp(key, "continuous_mode")) {
137 ctx.continuous_mode = atoi(value);
139 } else if (!strcmp(key, "default_browser")) {
140 if (!default_browser)
141 default_browser = value;
142 } else if (!strcmp(key, "other_browser_cmd")) {
143 if (!ctx.other_browser_cmd)
144 ctx.other_browser_cmd = value;
146 /* Don't need this line's contents */
152 printf("continuous_mode: %d\n", ctx.continuous_mode);
153 printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
154 printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
158 regfree(&re_config1);
159 regfree(&re_config2);
163 update_default_browser(&ctx, default_browser);
165 free(default_browser);
170 OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
172 GError *error = NULL;
176 if (ctx.continuous_mode) {
177 struct sigaction act;
178 act.sa_flags = SA_RESTART;
179 sigemptyset(&(act.sa_mask));
181 act.sa_handler = waitforzombies;
182 if (sigaction(SIGCHLD, &act, NULL) == -1) {
183 printf("Installing signal handler failed\n");
187 act.sa_handler = read_config;
188 if (sigaction(SIGHUP, &act, NULL) == -1) {
189 printf("Installing signal handler failed\n");
196 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
197 &dbus_glib_osso_browser_object_info);
199 ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
200 if (!ctx.session_bus) {
201 printf("Couldn't get a D-Bus bus connection\n");
204 ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
205 "org.freedesktop.DBus", "/org/freedesktop/DBus",
206 "org.freedesktop.DBus");
207 if (!ctx.dbus_proxy) {
208 printf("Couldn't get an org.freedesktop.DBus proxy\n");
212 dbus_request_osso_browser_name(&ctx);
214 obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
215 obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
216 dbus_g_connection_register_g_object(ctx.session_bus,
217 "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
218 dbus_g_connection_register_g_object(ctx.session_bus,
219 "/com/nokia/osso_browser/request",
220 G_OBJECT(obj_osso_browser_req));
222 mainloop = g_main_loop_new(NULL, FALSE);
223 printf("Starting main loop\n");
224 g_main_loop_run(mainloop);
225 printf("Main loop completed\n");