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 /* 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))))
76 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
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")))
88 /* compile regex matching blank lines or comments */
89 if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
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:]]*$",
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)) {
105 regfree(&re_config1);
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))
116 /* Find the substrings corresponding to the key and value
117 If the line doesn't match our idea of a config file entry,
119 if (regexec(&re_config1, buf, 3, substrs, 0) &&
120 regexec(&re_config2, buf, 3, substrs, 0))
122 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
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))))
129 strncpy(value, buf+substrs[2].rm_so, len);
130 /* calloc() zeroes the memory, so string is automatically
133 /* make key point to a null-terminated string holding the
135 key = buf + substrs[1].rm_so;
136 buf[substrs[1].rm_eo] = '\0';
138 if (!strcmp(key, "continuous_mode")) {
139 ctx.continuous_mode = atoi(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;
148 /* Don't need this line's contents */
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");
160 regfree(&re_config1);
161 regfree(&re_config2);
165 update_default_browser(&ctx, default_browser);
167 free(default_browser);
172 OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
174 GError *error = NULL;
178 if (ctx.continuous_mode) {
179 /* Install signal handlers */
180 struct sigaction act;
181 act.sa_flags = SA_RESTART;
182 sigemptyset(&(act.sa_mask));
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");
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");
201 dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
202 &dbus_glib_osso_browser_object_info);
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");
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");
218 dbus_request_osso_browser_name(&ctx);
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));
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");