Expand environment variables for all config option values when loading
[openal-soft.git] / Alc / alcConfig.c
bloba2355fc0feeb84a77996c90b33849e9e0caa15d5
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <limits.h>
37 #include "alMain.h"
39 #ifdef _WIN32_IE
40 #include <shlobj.h>
41 #endif
43 #ifndef PATH_MAX
44 #ifdef MAX_PATH
45 #define PATH_MAX MAX_PATH
46 #else
47 #define PATH_MAX 4096
48 #endif
49 #endif
51 typedef struct ConfigEntry {
52 char *key;
53 char *value;
54 } ConfigEntry;
56 typedef struct ConfigBlock {
57 ConfigEntry *entries;
58 unsigned int entryCount;
59 } ConfigBlock;
60 static ConfigBlock cfgBlock;
63 static char *lstrip(char *line)
65 while(isspace(line[0]))
66 line++;
67 return line;
70 static char *rstrip(char *line)
72 size_t len = strlen(line);
73 while(len > 0 && isspace(line[len-1]))
74 len--;
75 line[len] = 0;
76 return line;
79 static int readline(FILE *f, char **output, size_t *maxlen)
81 size_t len = 0;
82 int c;
84 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
86 if(c == EOF)
87 return 0;
89 do {
90 if(len+1 >= *maxlen)
92 void *temp = NULL;
93 size_t newmax;
95 newmax = (*maxlen ? (*maxlen)<<1 : 32);
96 if(newmax > *maxlen)
97 temp = realloc(*output, newmax);
98 if(!temp)
100 ERR("Failed to realloc %lu bytes from %lu!\n", newmax, *maxlen);
101 return 0;
104 *output = temp;
105 *maxlen = newmax;
107 (*output)[len++] = c;
108 (*output)[len] = '\0';
109 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
111 return 1;
115 static char *expdup(const char *str)
117 char *output = NULL;
118 size_t maxlen = 0;
119 size_t len = 0;
121 while(*str != '\0')
123 const char *addstr;
124 size_t addstrlen;
125 size_t i;
127 if(str[0] != '$')
129 const char *next = strchr(str, '$');
130 addstr = str;
131 addstrlen = next ? (size_t)(next-str) : strlen(str);
133 str += addstrlen;
135 else
137 str++;
138 if(*str == '$')
140 const char *next = strchr(str+1, '$');
141 addstr = str;
142 addstrlen = next ? (size_t)(next-str) : strlen(str);
144 str += addstrlen;
146 else
148 char envname[1024];
149 size_t k = 0;
151 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
152 envname[k++] = *(str++);
153 envname[k++] = '\0';
155 if((addstr=getenv(envname)) == NULL)
156 continue;
157 addstrlen = strlen(addstr);
160 if(addstrlen == 0)
161 continue;
163 if(addstrlen >= maxlen-len)
165 void *temp = NULL;
166 size_t newmax;
168 newmax = NextPowerOf2(len+addstrlen+1);
169 if(newmax > maxlen)
170 temp = realloc(output, newmax);
171 if(!temp)
173 ERR("Failed to realloc %lu bytes from %lu!\n", newmax, maxlen);
174 return output;
177 output = temp;
178 maxlen = newmax;
181 for(i = 0;i < addstrlen;i++)
182 output[len++] = addstr[i];
183 output[len] = '\0';
186 return output ? output : calloc(1, 1);
190 static void LoadConfigFromFile(FILE *f)
192 char curSection[128] = "";
193 char *buffer = NULL;
194 size_t maxlen = 0;
195 ConfigEntry *ent;
197 while(readline(f, &buffer, &maxlen))
199 char *line, *comment;
200 char key[256] = "";
201 char value[256] = "";
203 comment = strchr(buffer, '#');
204 if(comment)
206 *(comment++) = 0;
207 comment = rstrip(lstrip(comment));
210 line = rstrip(lstrip(buffer));
211 if(!line[0])
212 continue;
214 if(line[0] == '[')
216 char *section = line+1;
217 char *endsection;
219 endsection = strchr(section, ']');
220 if(!endsection || section == endsection || endsection[1] != 0)
222 ERR("config parse error: bad line \"%s\"\n", line);
223 continue;
225 *endsection = 0;
227 if(strcasecmp(section, "general") == 0)
228 curSection[0] = 0;
229 else
231 strncpy(curSection, section, sizeof(curSection)-1);
232 curSection[sizeof(curSection)-1] = 0;
235 continue;
238 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
239 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
240 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
242 /* sscanf doesn't handle '' or "" as empty values, so clip it
243 * manually. */
244 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
245 value[0] = 0;
247 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
249 /* Special case for 'key =' */
250 value[0] = 0;
252 else
254 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
255 continue;
257 rstrip(key);
259 if(curSection[0] != 0)
261 size_t len = strlen(curSection);
262 memmove(&key[len+1], key, sizeof(key)-1-len);
263 key[len] = '/';
264 memcpy(key, curSection, len);
267 /* Check if we already have this option set */
268 ent = cfgBlock.entries;
269 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
271 if(strcasecmp(ent->key, key) == 0)
272 break;
273 ent++;
276 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
278 /* Allocate a new option entry */
279 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
280 if(!ent)
282 ERR("config parse error: error reallocating config entries\n");
283 continue;
285 cfgBlock.entries = ent;
286 ent = cfgBlock.entries + cfgBlock.entryCount;
287 cfgBlock.entryCount++;
289 ent->key = strdup(key);
290 ent->value = NULL;
293 free(ent->value);
294 ent->value = expdup(value);
296 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
299 free(buffer);
302 void ReadALConfig(void)
304 char buffer[PATH_MAX];
305 const char *str;
306 FILE *f;
308 #ifdef _WIN32
309 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
311 size_t p = strlen(buffer);
312 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
314 TRACE("Loading config %s...\n", buffer);
315 f = fopen(buffer, "rt");
316 if(f)
318 LoadConfigFromFile(f);
319 fclose(f);
322 #else
323 str = "/etc/openal/alsoft.conf";
325 TRACE("Loading config %s...\n", str);
326 f = fopen(str, "r");
327 if(f)
329 LoadConfigFromFile(f);
330 fclose(f);
333 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
334 str = "/etc/xdg";
335 strncpy(buffer, str, sizeof(buffer)-1);
336 buffer[sizeof(buffer)-1] = 0;
337 /* Go through the list in reverse, since "the order of base directories
338 * denotes their importance; the first directory listed is the most
339 * important". Ergo, we need to load the settings from the later dirs
340 * first so that the settings in the earlier dirs override them.
342 while(1)
344 char *next = strrchr(buffer, ':');
345 if(next) *(next++) = 0;
346 else next = buffer;
348 if(next[0] != '/')
349 WARN("Ignoring XDG config dir: %s\n", next);
350 else
352 size_t len = strlen(next);
353 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
354 buffer[sizeof(buffer)-1] = 0;
356 TRACE("Loading config %s...\n", next);
357 f = fopen(next, "r");
358 if(f)
360 LoadConfigFromFile(f);
361 fclose(f);
364 if(next == buffer)
365 break;
368 if((str=getenv("HOME")) != NULL && *str)
370 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
372 TRACE("Loading config %s...\n", buffer);
373 f = fopen(buffer, "r");
374 if(f)
376 LoadConfigFromFile(f);
377 fclose(f);
381 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
382 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
383 else
385 buffer[0] = 0;
386 if((str=getenv("HOME")) != NULL && str[0] != 0)
387 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
389 if(buffer[0] != 0)
391 TRACE("Loading config %s...\n", buffer);
392 f = fopen(buffer, "r");
393 if(f)
395 LoadConfigFromFile(f);
396 fclose(f);
399 #endif
401 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
403 TRACE("Loading config %s...\n", str);
404 f = fopen(str, "r");
405 if(f)
407 LoadConfigFromFile(f);
408 fclose(f);
413 void FreeALConfig(void)
415 unsigned int i;
417 for(i = 0;i < cfgBlock.entryCount;i++)
419 free(cfgBlock.entries[i].key);
420 free(cfgBlock.entries[i].value);
422 free(cfgBlock.entries);
425 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
427 unsigned int i;
428 char key[256];
430 if(!keyName)
431 return def;
433 if(blockName && strcasecmp(blockName, "general") != 0)
434 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
435 else
437 strncpy(key, keyName, sizeof(key)-1);
438 key[sizeof(key)-1] = 0;
441 for(i = 0;i < cfgBlock.entryCount;i++)
443 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
445 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
446 if(cfgBlock.entries[i].value[0])
447 return cfgBlock.entries[i].value;
448 return def;
452 TRACE("Key %s not found\n", key);
453 return def;
456 int ConfigValueExists(const char *blockName, const char *keyName)
458 const char *val = GetConfigValue(blockName, keyName, "");
459 return !!val[0];
462 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
464 const char *val = GetConfigValue(blockName, keyName, "");
465 if(!val[0]) return 0;
467 *ret = val;
468 return 1;
471 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
473 const char *val = GetConfigValue(blockName, keyName, "");
474 if(!val[0]) return 0;
476 *ret = strtol(val, NULL, 0);
477 return 1;
480 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
482 const char *val = GetConfigValue(blockName, keyName, "");
483 if(!val[0]) return 0;
485 *ret = strtoul(val, NULL, 0);
486 return 1;
489 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
491 const char *val = GetConfigValue(blockName, keyName, "");
492 if(!val[0]) return 0;
494 #ifdef HAVE_STRTOF
495 *ret = strtof(val, NULL);
496 #else
497 *ret = (float)strtod(val, NULL);
498 #endif
499 return 1;
502 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
504 const char *val = GetConfigValue(blockName, keyName, "");
506 if(!val[0]) return !!def;
507 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
508 strcasecmp(val, "on") == 0 || atoi(val) != 0);