2007-11-14 02:02:18 +00:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 1999-2007 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 12:11:03 +00:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-11-14 02:02:18 +00:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
2010-03-18 20:13:44 +00:00
|
|
|
#ifdef _WIN32
|
2012-09-11 08:39:05 +00:00
|
|
|
#ifdef __MINGW32__
|
2010-03-18 20:13:44 +00:00
|
|
|
#define _WIN32_IE 0x501
|
|
|
|
#else
|
2009-06-08 02:11:46 +00:00
|
|
|
#define _WIN32_IE 0x400
|
|
|
|
#endif
|
2010-03-18 20:13:44 +00:00
|
|
|
#endif
|
2009-06-08 02:11:46 +00:00
|
|
|
|
2008-01-16 22:09:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2009-06-08 02:11:46 +00:00
|
|
|
#ifdef _WIN32_IE
|
2015-05-02 11:44:06 +00:00
|
|
|
#include <windows.h>
|
2008-06-05 01:09:21 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-28 07:37:42 +00:00
|
|
|
#include "alMain.h"
|
|
|
|
#include "compat.h"
|
2014-08-19 21:48:13 +00:00
|
|
|
#include "bool.h"
|
2014-03-28 07:37:42 +00:00
|
|
|
|
2014-02-23 22:07:09 +00:00
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
typedef struct ConfigEntry {
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
} ConfigEntry;
|
|
|
|
|
|
|
|
typedef struct ConfigBlock {
|
|
|
|
ConfigEntry *entries;
|
2011-09-20 18:40:56 +00:00
|
|
|
unsigned int entryCount;
|
2007-11-14 02:02:18 +00:00
|
|
|
} ConfigBlock;
|
2013-06-17 02:52:56 +00:00
|
|
|
static ConfigBlock cfgBlock;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
static char *lstrip(char *line)
|
|
|
|
{
|
|
|
|
while(isspace(line[0]))
|
|
|
|
line++;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *rstrip(char *line)
|
|
|
|
{
|
|
|
|
size_t len = strlen(line);
|
|
|
|
while(len > 0 && isspace(line[len-1]))
|
|
|
|
len--;
|
|
|
|
line[len] = 0;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:07:09 +00:00
|
|
|
static int readline(FILE *f, char **output, size_t *maxlen)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
|
|
|
|
;
|
|
|
|
if(c == EOF)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(len+1 >= *maxlen)
|
|
|
|
{
|
|
|
|
void *temp = NULL;
|
|
|
|
size_t newmax;
|
|
|
|
|
|
|
|
newmax = (*maxlen ? (*maxlen)<<1 : 32);
|
|
|
|
if(newmax > *maxlen)
|
|
|
|
temp = realloc(*output, newmax);
|
|
|
|
if(!temp)
|
|
|
|
{
|
2014-04-19 16:42:03 +00:00
|
|
|
ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
|
2014-02-23 22:07:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*output = temp;
|
|
|
|
*maxlen = newmax;
|
|
|
|
}
|
|
|
|
(*output)[len++] = c;
|
|
|
|
(*output)[len] = '\0';
|
|
|
|
} while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 01:25:05 +00:00
|
|
|
static char *expdup(const char *str)
|
|
|
|
{
|
|
|
|
char *output = NULL;
|
|
|
|
size_t maxlen = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
while(*str != '\0')
|
|
|
|
{
|
|
|
|
const char *addstr;
|
|
|
|
size_t addstrlen;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if(str[0] != '$')
|
|
|
|
{
|
|
|
|
const char *next = strchr(str, '$');
|
|
|
|
addstr = str;
|
|
|
|
addstrlen = next ? (size_t)(next-str) : strlen(str);
|
|
|
|
|
|
|
|
str += addstrlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
if(*str == '$')
|
|
|
|
{
|
|
|
|
const char *next = strchr(str+1, '$');
|
|
|
|
addstr = str;
|
|
|
|
addstrlen = next ? (size_t)(next-str) : strlen(str);
|
|
|
|
|
|
|
|
str += addstrlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-19 21:48:13 +00:00
|
|
|
bool hasbraces;
|
2014-02-27 01:25:05 +00:00
|
|
|
char envname[1024];
|
|
|
|
size_t k = 0;
|
|
|
|
|
2014-08-19 21:48:13 +00:00
|
|
|
hasbraces = (*str == '{');
|
|
|
|
if(hasbraces) str++;
|
|
|
|
|
2014-02-27 01:25:05 +00:00
|
|
|
while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
|
|
|
|
envname[k++] = *(str++);
|
|
|
|
envname[k++] = '\0';
|
|
|
|
|
2014-08-19 21:48:13 +00:00
|
|
|
if(hasbraces && *str != '}')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(hasbraces) str++;
|
2014-02-27 01:25:05 +00:00
|
|
|
if((addstr=getenv(envname)) == NULL)
|
|
|
|
continue;
|
|
|
|
addstrlen = strlen(addstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(addstrlen == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(addstrlen >= maxlen-len)
|
|
|
|
{
|
|
|
|
void *temp = NULL;
|
|
|
|
size_t newmax;
|
|
|
|
|
2014-05-04 07:37:25 +00:00
|
|
|
newmax = len+addstrlen+1;
|
2014-02-27 01:25:05 +00:00
|
|
|
if(newmax > maxlen)
|
|
|
|
temp = realloc(output, newmax);
|
|
|
|
if(!temp)
|
|
|
|
{
|
2014-04-19 16:42:03 +00:00
|
|
|
ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
|
2014-02-27 01:25:05 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
output = temp;
|
|
|
|
maxlen = newmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0;i < addstrlen;i++)
|
|
|
|
output[len++] = addstr[i];
|
|
|
|
output[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return output ? output : calloc(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
static void LoadConfigFromFile(FILE *f)
|
|
|
|
{
|
2013-06-17 02:52:56 +00:00
|
|
|
char curSection[128] = "";
|
2014-02-23 22:07:09 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t maxlen = 0;
|
2007-11-14 02:02:18 +00:00
|
|
|
ConfigEntry *ent;
|
|
|
|
|
2014-02-23 22:07:09 +00:00
|
|
|
while(readline(f, &buffer, &maxlen))
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 01:42:27 +00:00
|
|
|
char *line, *comment;
|
|
|
|
char key[256] = "";
|
|
|
|
char value[256] = "";
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
line = rstrip(lstrip(buffer));
|
2015-08-28 21:59:45 +00:00
|
|
|
if(!line[0]) continue;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
if(line[0] == '[')
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 01:42:27 +00:00
|
|
|
char *section = line+1;
|
|
|
|
char *endsection;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
endsection = strchr(section, ']');
|
2015-08-28 21:59:45 +00:00
|
|
|
if(!endsection || section == endsection)
|
|
|
|
{
|
|
|
|
ERR("config parse error: bad line \"%s\"\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(endsection[1] != 0)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2015-08-28 21:59:45 +00:00
|
|
|
char *end = endsection+1;
|
|
|
|
while(isspace(*end))
|
|
|
|
++end;
|
|
|
|
if(*end != 0 && *end != '#')
|
|
|
|
{
|
|
|
|
ERR("config parse error: bad line \"%s\"\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
2013-06-17 01:42:27 +00:00
|
|
|
*endsection = 0;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 02:52:56 +00:00
|
|
|
if(strcasecmp(section, "general") == 0)
|
|
|
|
curSection[0] = 0;
|
|
|
|
else
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2017-04-01 13:07:14 +00:00
|
|
|
size_t len, p = 0;
|
|
|
|
do {
|
|
|
|
char *nextp = strchr(section, '%');
|
|
|
|
if(!nextp)
|
|
|
|
{
|
|
|
|
strncpy(curSection+p, section, sizeof(curSection)-1-p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = nextp - section;
|
|
|
|
if(len > sizeof(curSection)-1-p)
|
|
|
|
len = sizeof(curSection)-1-p;
|
|
|
|
strncpy(curSection+p, section, len);
|
|
|
|
p += len;
|
|
|
|
section = nextp;
|
|
|
|
|
|
|
|
if(((section[1] >= '0' && section[1] <= '9') ||
|
|
|
|
(section[1] >= 'a' && section[1] <= 'f') ||
|
|
|
|
(section[1] >= 'A' && section[1] <= 'F')) &&
|
|
|
|
((section[2] >= '0' && section[2] <= '9') ||
|
|
|
|
(section[2] >= 'a' && section[2] <= 'f') ||
|
|
|
|
(section[2] >= 'A' && section[2] <= 'F')))
|
|
|
|
{
|
|
|
|
unsigned char b = 0;
|
|
|
|
if(section[1] >= '0' && section[1] <= '9')
|
|
|
|
b = (section[1]-'0') << 4;
|
|
|
|
else if(section[1] >= 'a' && section[1] <= 'f')
|
|
|
|
b = (section[1]-'a'+0xa) << 4;
|
|
|
|
else if(section[1] >= 'A' && section[1] <= 'F')
|
|
|
|
b = (section[1]-'A'+0x0a) << 4;
|
|
|
|
if(section[2] >= '0' && section[2] <= '9')
|
|
|
|
b |= (section[2]-'0');
|
|
|
|
else if(section[2] >= 'a' && section[2] <= 'f')
|
|
|
|
b |= (section[2]-'a'+0xa);
|
|
|
|
else if(section[2] >= 'A' && section[2] <= 'F')
|
|
|
|
b |= (section[2]-'A'+0x0a);
|
|
|
|
if(p < sizeof(curSection)-1)
|
|
|
|
curSection[p++] = b;
|
|
|
|
section += 3;
|
|
|
|
}
|
|
|
|
else if(section[1] == '%')
|
|
|
|
{
|
|
|
|
if(p < sizeof(curSection)-1)
|
|
|
|
curSection[p++] = '%';
|
|
|
|
section += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(p < sizeof(curSection)-1)
|
|
|
|
curSection[p++] = '%';
|
|
|
|
section += 1;
|
|
|
|
}
|
|
|
|
if(p < sizeof(curSection)-1)
|
|
|
|
curSection[p] = 0;
|
|
|
|
} while(p < sizeof(curSection)-1 && *section != 0);
|
2013-06-17 02:52:56 +00:00
|
|
|
curSection[sizeof(curSection)-1] = 0;
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:59:45 +00:00
|
|
|
comment = strchr(line, '#');
|
|
|
|
if(comment) *(comment++) = 0;
|
|
|
|
if(!line[0]) continue;
|
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
|
|
|
|
sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
|
|
|
|
sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 01:42:27 +00:00
|
|
|
/* sscanf doesn't handle '' or "" as empty values, so clip it
|
|
|
|
* manually. */
|
|
|
|
if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
|
|
|
|
value[0] = 0;
|
2014-02-23 17:54:26 +00:00
|
|
|
}
|
|
|
|
else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
|
|
|
|
{
|
|
|
|
/* Special case for 'key =' */
|
|
|
|
value[0] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("config parse error: malformed option line: \"%s\"\n\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rstrip(key);
|
|
|
|
|
|
|
|
if(curSection[0] != 0)
|
|
|
|
{
|
|
|
|
size_t len = strlen(curSection);
|
|
|
|
memmove(&key[len+1], key, sizeof(key)-1-len);
|
|
|
|
key[len] = '/';
|
|
|
|
memcpy(key, curSection, len);
|
|
|
|
}
|
2013-06-17 02:52:56 +00:00
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
/* Check if we already have this option set */
|
2013-06-17 02:52:56 +00:00
|
|
|
ent = cfgBlock.entries;
|
|
|
|
while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 01:42:27 +00:00
|
|
|
if(strcasecmp(ent->key, key) == 0)
|
2007-11-14 02:02:18 +00:00
|
|
|
break;
|
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
|
2013-06-17 02:52:56 +00:00
|
|
|
if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
|
|
|
/* Allocate a new option entry */
|
2013-06-17 02:52:56 +00:00
|
|
|
ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
|
2007-11-14 02:02:18 +00:00
|
|
|
if(!ent)
|
|
|
|
{
|
2011-07-13 08:43:00 +00:00
|
|
|
ERR("config parse error: error reallocating config entries\n");
|
2007-11-14 02:02:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-17 02:52:56 +00:00
|
|
|
cfgBlock.entries = ent;
|
|
|
|
ent = cfgBlock.entries + cfgBlock.entryCount;
|
|
|
|
cfgBlock.entryCount++;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 01:42:27 +00:00
|
|
|
ent->key = strdup(key);
|
2007-11-14 02:02:18 +00:00
|
|
|
ent->value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ent->value);
|
2014-02-27 01:25:05 +00:00
|
|
|
ent->value = expdup(value);
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2011-07-11 04:44:42 +00:00
|
|
|
TRACE("found '%s' = '%s'\n", ent->key, ent->value);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
2014-02-23 22:07:09 +00:00
|
|
|
|
|
|
|
free(buffer);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
#ifdef _WIN32
|
2007-11-14 02:02:18 +00:00
|
|
|
void ReadALConfig(void)
|
|
|
|
{
|
2014-03-28 08:21:05 +00:00
|
|
|
WCHAR buffer[PATH_MAX];
|
|
|
|
const WCHAR *str;
|
2016-06-04 17:45:44 +00:00
|
|
|
al_string ppath;
|
2007-11-14 02:02:18 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
|
2008-06-05 01:09:21 +00:00
|
|
|
{
|
2015-02-07 12:19:50 +00:00
|
|
|
al_string filepath = AL_STRING_INIT_STATIC();
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_copy_wcstr(&filepath, buffer);
|
|
|
|
alstr_append_cstr(&filepath, "\\alsoft.ini");
|
2013-06-17 03:43:31 +00:00
|
|
|
|
2017-04-04 13:58:53 +00:00
|
|
|
TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
|
|
|
|
f = al_fopen(alstr_get_cstr(filepath), "rt");
|
2014-03-28 08:21:05 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_reset(&filepath);
|
2014-03-28 08:21:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 17:45:44 +00:00
|
|
|
ppath = GetProcPath();
|
2017-04-04 13:58:53 +00:00
|
|
|
if(!alstr_empty(ppath))
|
2016-06-04 17:45:44 +00:00
|
|
|
{
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_append_cstr(&ppath, "\\alsoft.ini");
|
|
|
|
TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
|
|
|
|
f = al_fopen(alstr_get_cstr(ppath), "r");
|
2016-06-04 17:45:44 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
|
|
|
|
{
|
2015-02-07 12:19:50 +00:00
|
|
|
al_string filepath = AL_STRING_INIT_STATIC();
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_copy_wcstr(&filepath, str);
|
2015-02-07 12:19:50 +00:00
|
|
|
|
2017-04-04 13:58:53 +00:00
|
|
|
TRACE("Loading config %s...\n", alstr_get_cstr(filepath));
|
|
|
|
f = al_fopen(alstr_get_cstr(filepath), "rt");
|
2008-06-05 01:09:21 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_reset(&filepath);
|
2008-06-05 01:09:21 +00:00
|
|
|
}
|
2016-06-04 17:45:44 +00:00
|
|
|
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_reset(&ppath);
|
2014-03-28 08:21:05 +00:00
|
|
|
}
|
2007-11-14 02:02:18 +00:00
|
|
|
#else
|
2014-03-28 08:21:05 +00:00
|
|
|
void ReadALConfig(void)
|
|
|
|
{
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
const char *str;
|
2016-06-04 17:45:44 +00:00
|
|
|
al_string ppath;
|
2014-03-28 08:21:05 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2013-06-17 03:43:31 +00:00
|
|
|
str = "/etc/openal/alsoft.conf";
|
|
|
|
|
|
|
|
TRACE("Loading config %s...\n", str);
|
2014-03-28 07:37:42 +00:00
|
|
|
f = al_fopen(str, "r");
|
2007-11-14 02:02:18 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2013-06-17 05:55:11 +00:00
|
|
|
|
|
|
|
if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
|
|
|
|
str = "/etc/xdg";
|
|
|
|
strncpy(buffer, str, sizeof(buffer)-1);
|
|
|
|
buffer[sizeof(buffer)-1] = 0;
|
|
|
|
/* Go through the list in reverse, since "the order of base directories
|
|
|
|
* denotes their importance; the first directory listed is the most
|
|
|
|
* important". Ergo, we need to load the settings from the later dirs
|
|
|
|
* first so that the settings in the earlier dirs override them.
|
|
|
|
*/
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
char *next = strrchr(buffer, ':');
|
|
|
|
if(next) *(next++) = 0;
|
|
|
|
else next = buffer;
|
|
|
|
|
|
|
|
if(next[0] != '/')
|
2013-06-20 06:24:06 +00:00
|
|
|
WARN("Ignoring XDG config dir: %s\n", next);
|
|
|
|
else
|
2013-06-17 05:55:11 +00:00
|
|
|
{
|
|
|
|
size_t len = strlen(next);
|
|
|
|
strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
|
|
|
|
buffer[sizeof(buffer)-1] = 0;
|
|
|
|
|
2013-06-20 06:24:06 +00:00
|
|
|
TRACE("Loading config %s...\n", next);
|
2014-03-28 07:37:42 +00:00
|
|
|
f = al_fopen(next, "r");
|
2013-06-20 06:24:06 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2013-06-17 05:55:11 +00:00
|
|
|
}
|
|
|
|
if(next == buffer)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-17 17:15:53 +00:00
|
|
|
if((str=getenv("HOME")) != NULL && *str)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2011-05-17 17:15:53 +00:00
|
|
|
snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
|
2013-06-17 03:43:31 +00:00
|
|
|
|
|
|
|
TRACE("Loading config %s...\n", buffer);
|
2014-03-28 07:37:42 +00:00
|
|
|
f = al_fopen(buffer, "r");
|
2007-11-14 02:02:18 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2013-06-17 05:55:11 +00:00
|
|
|
|
|
|
|
if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[0] = 0;
|
|
|
|
if((str=getenv("HOME")) != NULL && str[0] != 0)
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
|
|
|
|
}
|
|
|
|
if(buffer[0] != 0)
|
|
|
|
{
|
|
|
|
TRACE("Loading config %s...\n", buffer);
|
2014-03-28 07:37:42 +00:00
|
|
|
f = al_fopen(buffer, "r");
|
2013-06-17 05:55:11 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 17:45:44 +00:00
|
|
|
ppath = GetProcPath();
|
2017-04-04 13:58:53 +00:00
|
|
|
if(!alstr_empty(ppath))
|
2016-06-04 17:45:44 +00:00
|
|
|
{
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_append_cstr(&ppath, "/alsoft.conf");
|
|
|
|
TRACE("Loading config %s...\n", alstr_get_cstr(ppath));
|
|
|
|
f = al_fopen(alstr_get_cstr(ppath), "r");
|
2016-06-04 17:45:44 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-17 17:15:53 +00:00
|
|
|
if((str=getenv("ALSOFT_CONF")) != NULL && *str)
|
2008-11-13 15:58:39 +00:00
|
|
|
{
|
2013-06-17 03:43:31 +00:00
|
|
|
TRACE("Loading config %s...\n", str);
|
2014-03-28 07:37:42 +00:00
|
|
|
f = al_fopen(str, "r");
|
2008-11-13 15:58:39 +00:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 17:45:44 +00:00
|
|
|
|
2017-04-04 13:58:53 +00:00
|
|
|
alstr_reset(&ppath);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
2014-03-28 08:21:05 +00:00
|
|
|
#endif
|
2007-11-14 02:02:18 +00:00
|
|
|
|
|
|
|
void FreeALConfig(void)
|
|
|
|
{
|
2011-09-20 18:40:56 +00:00
|
|
|
unsigned int i;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 02:52:56 +00:00
|
|
|
for(i = 0;i < cfgBlock.entryCount;i++)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 02:52:56 +00:00
|
|
|
free(cfgBlock.entries[i].key);
|
|
|
|
free(cfgBlock.entries[i].value);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
2013-06-17 02:52:56 +00:00
|
|
|
free(cfgBlock.entries);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 02:52:56 +00:00
|
|
|
unsigned int i;
|
|
|
|
char key[256];
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2010-05-12 14:30:45 +00:00
|
|
|
if(!keyName)
|
|
|
|
return def;
|
|
|
|
|
2013-06-17 02:52:56 +00:00
|
|
|
if(blockName && strcasecmp(blockName, "general") != 0)
|
2015-08-28 21:10:39 +00:00
|
|
|
{
|
|
|
|
if(devName)
|
2015-09-04 22:47:48 +00:00
|
|
|
snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
|
2015-08-28 21:10:39 +00:00
|
|
|
else
|
|
|
|
snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
|
|
|
|
}
|
2013-06-17 02:52:56 +00:00
|
|
|
else
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
if(devName)
|
|
|
|
snprintf(key, sizeof(key), "%s/%s", devName, keyName);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(key, keyName, sizeof(key)-1);
|
|
|
|
key[sizeof(key)-1] = 0;
|
|
|
|
}
|
2013-06-17 02:52:56 +00:00
|
|
|
}
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2013-06-17 02:52:56 +00:00
|
|
|
for(i = 0;i < cfgBlock.entryCount;i++)
|
|
|
|
{
|
2015-09-04 23:09:09 +00:00
|
|
|
if(strcmp(cfgBlock.entries[i].key, key) == 0)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2013-06-17 02:52:56 +00:00
|
|
|
TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
|
|
|
|
if(cfgBlock.entries[i].value[0])
|
|
|
|
return cfgBlock.entries[i].value;
|
|
|
|
return def;
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
if(!devName)
|
|
|
|
{
|
|
|
|
TRACE("Key %s not found\n", key);
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
return GetConfigValue(NULL, blockName, keyName, def);
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
|
2009-12-28 21:08:15 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2009-12-28 21:08:15 +00:00
|
|
|
return !!val[0];
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
|
2011-09-19 18:29:18 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2011-09-19 18:29:18 +00:00
|
|
|
if(!val[0]) return 0;
|
|
|
|
|
|
|
|
*ret = val;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2011-09-18 23:16:55 +00:00
|
|
|
if(!val[0]) return 0;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2011-09-18 23:16:55 +00:00
|
|
|
*ret = strtol(val, NULL, 0);
|
|
|
|
return 1;
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
|
2007-11-14 02:02:18 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2011-09-18 23:16:55 +00:00
|
|
|
if(!val[0]) return 0;
|
|
|
|
|
|
|
|
*ret = strtoul(val, NULL, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
|
2011-09-18 23:16:55 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2011-09-18 23:16:55 +00:00
|
|
|
if(!val[0]) return 0;
|
2007-11-14 02:02:18 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_STRTOF
|
2011-09-18 23:16:55 +00:00
|
|
|
*ret = strtof(val, NULL);
|
2007-11-14 02:02:18 +00:00
|
|
|
#else
|
2011-09-18 23:16:55 +00:00
|
|
|
*ret = (float)strtod(val, NULL);
|
2007-11-14 02:02:18 +00:00
|
|
|
#endif
|
2011-09-18 23:16:55 +00:00
|
|
|
return 1;
|
2007-11-14 02:02:18 +00:00
|
|
|
}
|
2009-09-16 05:18:13 +00:00
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
|
2014-11-23 04:52:08 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2014-11-23 04:52:08 +00:00
|
|
|
if(!val[0]) return 0;
|
|
|
|
|
|
|
|
*ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
|
|
|
|
strcasecmp(val, "on") == 0 || atoi(val) != 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:10:39 +00:00
|
|
|
int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
|
2009-09-16 05:18:13 +00:00
|
|
|
{
|
2015-08-28 21:10:39 +00:00
|
|
|
const char *val = GetConfigValue(devName, blockName, keyName, "");
|
2009-09-16 05:18:13 +00:00
|
|
|
|
|
|
|
if(!val[0]) return !!def;
|
|
|
|
return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
|
|
|
|
strcasecmp(val, "on") == 0 || atoi(val) != 0);
|
|
|
|
}
|