2 * configfile.c -- config file functions 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,
26 #include <sys/types.h>
29 #include "configfile.h"
33 /* regex matching blank lines or comments */
34 #define REGEX_IGNORE "^[[:space:]]*(#|$)"
35 #define REGEX_IGNORE_FLAGS REG_EXTENDED|REG_NOSUB
37 /* regex matching foo = "bar", with arbitrary whitespace at beginning and end
38 of line and surrounding the = */
39 #define REGEX_CONFIG1 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$"
40 #define REGEX_CONFIG1_FLAGS REG_EXTENDED
42 /* regex matching foo = bar, with arbitrary whitespace at beginning of line and
44 #define REGEX_CONFIG2 "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$"
45 #define REGEX_CONFIG2_FLAGS REG_EXTENDED|REG_NEWLINE
47 static regex_t re_ignore, re_config1, re_config2;
48 static int re_init = 0;
50 /* Open config file for reading */
51 FILE *open_config_file(void) {
52 char *homedir, *configfile;
56 /* Put together the path to the config file */
57 if (!(homedir = getenv("HOME")))
58 homedir = DEFAULT_HOMEDIR;
59 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
60 if (!(configfile = calloc(len, sizeof(char))))
62 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
64 /* Try to open the config file */
65 if (!(fp = fopen(configfile, "r"))) {
66 /* Try the legacy config file location before giving up
67 XXX we assume here that CONFIGFILE_LOC_OLD is shorter
68 than CONFIGFILE_LOC! */
69 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
70 fp = fopen(configfile, "r");
77 /* Initialize the config file parser
78 Returns 1 if initialization successful, 0 otherwise */
79 int parse_config_file_begin(void) {
80 /* Just return if parser's already been initialized */
84 /* compile regex matching blank lines or comments */
85 if (regcomp(&re_ignore, REGEX_IGNORE, REGEX_IGNORE_FLAGS))
87 /* compile regex matching foo = "bar", with arbitrary whitespace at
88 beginning and end of line and surrounding the = */
89 if (regcomp(&re_config1, REGEX_CONFIG1, REGEX_CONFIG1_FLAGS)) {
93 /* compile regex matching foo = bar, with arbitrary whitespace at
94 beginning of line and surrounding the = */
95 if (regcomp(&re_config2, REGEX_CONFIG2, REGEX_CONFIG2_FLAGS)) {
105 /* End config file parsing and free the resources */
106 void parse_config_file_end(void) {
107 /* Just return if parser's not active */
112 regfree(&re_config1);
113 regfree(&re_config2);
116 /* Read the next line from a config file and store it into a swb_config_line,
117 parsing it into key and value if possible
118 Caller is responsible for freeing the strings in the swb_config_line if
119 call returns successfully
120 Returns 0 on success (whether line parsed or not), 1 on EOF, -1 on error */
121 int parse_config_file_line(FILE *fp, struct swb_config_line *line) {
122 regmatch_t substrs[3];
126 if (!re_init || !fp || !line)
133 if (!(line->key = calloc(MAXLINE, sizeof(char))))
136 /* Read in the next line of the config file
137 XXX doesn't deal with lines longer than MAXLINE */
138 if (!fgets(line->key, MAXLINE, fp)) {
147 /* no need to parse blank lines and comments */
148 if (!regexec(&re_ignore, line->key, 0, NULL, 0))
151 /* Find the substrings corresponding to the key and value
152 If the line doesn't match our idea of a config file entry,
154 if (regexec(&re_config1, line->key, 3, substrs, 0) &&
155 regexec(&re_config2, line->key, 3, substrs, 0))
157 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
160 /* copy the config value into a new string */
161 len = substrs[2].rm_eo - substrs[2].rm_so;
162 if (!(line->value = calloc(len+1, sizeof(char)))) {
167 strncpy(line->value, line->key+substrs[2].rm_so, len);
168 /* calloc() zeroes the memory, so string is automatically
171 /* make key point to a null-terminated string holding the
173 len = substrs[1].rm_eo - substrs[1].rm_so;
174 memmove(line->key, line->key+substrs[1].rm_so, len);
175 line->key[len] = '\0';
177 /* done parsing the line */
182 /* Toss a trailing newline, if present */
183 len = strlen(line->key);
184 if (line->key[len-1] == '\n')
185 line->key[len-1] = '\0';
187 /* Try to shrink the allocation for key, to save space if someone
188 wants to keep it around */
189 len = strlen(line->key);
190 if ((tmp = realloc(line->key, len+1)))