From: Steven Luo Date: Wed, 16 Dec 2009 12:02:08 +0000 (-0800) Subject: Initial refactor of config file handling X-Git-Tag: v3.0rc1~16 X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=commitdiff_plain;h=ca61ab9737a563adac6436de4fe35243b40345ef Initial refactor of config file handling Move the config file locating code, the location #defines, and the parsing regexes into new files, so that they can be shared by the config UI. --- diff --git a/Makefile b/Makefile index 8974eb2..67153b4 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = `pkg-config --libs dbus-glib-1` 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) diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..ee0e085 --- /dev/null +++ b/configfile.c @@ -0,0 +1,53 @@ +/* + * 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 +#include +#include + +#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; +} diff --git a/configfile.h b/configfile.h new file mode 100644 index 0000000..c7863a7 --- /dev/null +++ b/configfile.h @@ -0,0 +1,52 @@ +/* + * 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 +#include +#include + +#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 */ diff --git a/main.c b/main.c index 9adacbe..84b78e0 100644 --- a/main.c +++ b/main.c @@ -33,11 +33,7 @@ #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; @@ -56,51 +52,31 @@ static void waitforzombies(int signalnum) { } 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; @@ -163,7 +139,6 @@ out_nore: fclose(fp); out_noopen: update_default_browser(&ctx, default_browser); - free(configfile); free(default_browser); return; }