PREFIX = /usr/local
APP = browser-switchboard
-obj = main.o launcher.o dbus-server-bindings.o
+obj = main.o launcher.o dbus-server-bindings.o configfile.o
all: $(APP)
--- /dev/null
+/*
+ * configfile.c -- config file functions for browser-switchboard
+ *
+ * Copyright (C) 2009 Steven Luo
+ * Derived from a Python implementation by Jason Simpson and Steven Luo
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "configfile.h"
+
+FILE *open_config_file(void) {
+ char *homedir, *configfile;
+ size_t len;
+ FILE *fp;
+
+ /* Put together the path to the config file */
+ if (!(homedir = getenv("HOME")))
+ homedir = DEFAULT_HOMEDIR;
+ len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
+ if (!(configfile = calloc(len, sizeof(char))))
+ return NULL;
+ snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
+
+ /* Try to open the config file */
+ if (!(fp = fopen(configfile, "r"))) {
+ /* Try the legacy config file location before giving up
+ XXX we assume here that CONFIGFILE_LOC_OLD is shorter
+ than CONFIGFILE_LOC! */
+ snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
+ fp = fopen(configfile, "r");
+ }
+
+ free(configfile);
+ return fp;
+}
--- /dev/null
+/*
+ * configfile.h -- definitions for config file parsing
+ *
+ * Copyright (C) 2009 Steven Luo
+ * Derived from a Python implementation by Jason Simpson and Steven Luo
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#ifndef _CONFIGFILE_H
+#define _CONFIGFILE_H
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <regex.h>
+
+#define DEFAULT_HOMEDIR "/home/user"
+#define CONFIGFILE_LOC "/.config/browser-switchboard"
+#define CONFIGFILE_LOC_OLD "/.config/browser-proxy"
+#define MAXLINE 1024
+
+/* regex matching blank lines or comments */
+#define REGEX_IGNORE "^[[:space:]]*(#|$)"
+#define REGEX_IGNORE_FLAGS REG_EXTENDED|REG_NOSUB
+
+/* regex matching foo = "bar", with arbitrary whitespace at beginning and end
+ of line and surrounding the = */
+#define REGEX_CONFIG1 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$"
+#define REGEX_CONFIG1_FLAGS REG_EXTENDED
+
+/* regex matching foo = bar, with arbitrary whitespace at beginning of line and
+ surrounding the = */
+#define REGEX_CONFIG2 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$"
+#define REGEX_CONFIG2_FLAGS REG_EXTENDED|REG_NEWLINE
+
+
+FILE *open_config_file(void);
+
+#endif /* _CONFIGFILE_H */
#include "browser-switchboard.h"
#include "launcher.h"
#include "dbus-server-bindings.h"
-
-#define DEFAULT_HOMEDIR "/home/user"
-#define CONFIGFILE_LOC "/.config/browser-switchboard"
-#define CONFIGFILE_LOC_OLD "/.config/browser-proxy"
-#define MAXLINE 1024
+#include "configfile.h"
struct swb_context ctx;
}
static void read_config(int signalnum) {
- char *homedir, *configfile;
- size_t len;
- char buf[MAXLINE];
- char *key, *value;
- char *default_browser = NULL;
FILE *fp;
regex_t re_ignore, re_config1, re_config2;
regmatch_t substrs[3];
+ char buf[MAXLINE];
+ char *key, *value;
+ char *default_browser = NULL;
+ size_t len;
set_config_defaults(&ctx);
- /* Put together the path to the config file */
- if (!(homedir = getenv("HOME")))
- homedir = DEFAULT_HOMEDIR;
- len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
- if (!(configfile = calloc(len, sizeof(char))))
+ if (!(fp = open_config_file()))
goto out_noopen;
- snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
-
- /* Try to open the config file */
- if (!(fp = fopen(configfile, "r"))) {
- /* Try the legacy config file location before giving up
- XXX we assume here that CONFIGFILE_LOC_OLD is shorter
- than CONFIGFILE_LOC! */
- snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
- if (!(fp = fopen(configfile, "r")))
- goto out_noopen;
- }
/* compile regex matching blank lines or comments */
- if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
+ if (regcomp(&re_ignore, REGEX_IGNORE, REGEX_IGNORE_FLAGS))
goto out_nore;
/* compile regex matching foo = "bar", with arbitrary whitespace at
beginning and end of line and surrounding the = */
- if (regcomp(&re_config1,
- "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$",
- REG_EXTENDED)) {
+ if (regcomp(&re_config1, REGEX_CONFIG1, REGEX_CONFIG1_FLAGS)) {
regfree(&re_ignore);
goto out_nore;
}
/* compile regex matching foo = bar, with arbitrary whitespace at
beginning of line and surrounding the = */
- if (regcomp(&re_config2,
- "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$",
- REG_EXTENDED|REG_NEWLINE)) {
+ if (regcomp(&re_config2, REGEX_CONFIG2, REGEX_CONFIG2_FLAGS)) {
regfree(&re_ignore);
regfree(&re_config1);
goto out_nore;
fclose(fp);
out_noopen:
update_default_browser(&ctx, default_browser);
- free(configfile);
free(default_browser);
return;
}