alcConfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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.
  8. *
  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.
  13. *
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW32__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #ifdef _WIN32_IE
  33. #include <windows.h>
  34. #include <shlobj.h>
  35. #endif
  36. #include "alMain.h"
  37. #include "compat.h"
  38. #include "bool.h"
  39. typedef struct ConfigEntry {
  40. char *key;
  41. char *value;
  42. } ConfigEntry;
  43. typedef struct ConfigBlock {
  44. ConfigEntry *entries;
  45. unsigned int entryCount;
  46. } ConfigBlock;
  47. static ConfigBlock cfgBlock;
  48. static char *lstrip(char *line)
  49. {
  50. while(isspace(line[0]))
  51. line++;
  52. return line;
  53. }
  54. static char *rstrip(char *line)
  55. {
  56. size_t len = strlen(line);
  57. while(len > 0 && isspace(line[len-1]))
  58. len--;
  59. line[len] = 0;
  60. return line;
  61. }
  62. static int readline(FILE *f, char **output, size_t *maxlen)
  63. {
  64. size_t len = 0;
  65. int c;
  66. while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
  67. ;
  68. if(c == EOF)
  69. return 0;
  70. do {
  71. if(len+1 >= *maxlen)
  72. {
  73. void *temp = NULL;
  74. size_t newmax;
  75. newmax = (*maxlen ? (*maxlen)<<1 : 32);
  76. if(newmax > *maxlen)
  77. temp = realloc(*output, newmax);
  78. if(!temp)
  79. {
  80. ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
  81. return 0;
  82. }
  83. *output = temp;
  84. *maxlen = newmax;
  85. }
  86. (*output)[len++] = c;
  87. (*output)[len] = '\0';
  88. } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
  89. return 1;
  90. }
  91. static char *expdup(const char *str)
  92. {
  93. char *output = NULL;
  94. size_t maxlen = 0;
  95. size_t len = 0;
  96. while(*str != '\0')
  97. {
  98. const char *addstr;
  99. size_t addstrlen;
  100. size_t i;
  101. if(str[0] != '$')
  102. {
  103. const char *next = strchr(str, '$');
  104. addstr = str;
  105. addstrlen = next ? (size_t)(next-str) : strlen(str);
  106. str += addstrlen;
  107. }
  108. else
  109. {
  110. str++;
  111. if(*str == '$')
  112. {
  113. const char *next = strchr(str+1, '$');
  114. addstr = str;
  115. addstrlen = next ? (size_t)(next-str) : strlen(str);
  116. str += addstrlen;
  117. }
  118. else
  119. {
  120. bool hasbraces;
  121. char envname[1024];
  122. size_t k = 0;
  123. hasbraces = (*str == '{');
  124. if(hasbraces) str++;
  125. while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
  126. envname[k++] = *(str++);
  127. envname[k++] = '\0';
  128. if(hasbraces && *str != '}')
  129. continue;
  130. if(hasbraces) str++;
  131. if((addstr=getenv(envname)) == NULL)
  132. continue;
  133. addstrlen = strlen(addstr);
  134. }
  135. }
  136. if(addstrlen == 0)
  137. continue;
  138. if(addstrlen >= maxlen-len)
  139. {
  140. void *temp = NULL;
  141. size_t newmax;
  142. newmax = len+addstrlen+1;
  143. if(newmax > maxlen)
  144. temp = realloc(output, newmax);
  145. if(!temp)
  146. {
  147. ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
  148. return output;
  149. }
  150. output = temp;
  151. maxlen = newmax;
  152. }
  153. for(i = 0;i < addstrlen;i++)
  154. output[len++] = addstr[i];
  155. output[len] = '\0';
  156. }
  157. return output ? output : calloc(1, 1);
  158. }
  159. static void LoadConfigFromFile(FILE *f)
  160. {
  161. char curSection[128] = "";
  162. char *buffer = NULL;
  163. size_t maxlen = 0;
  164. ConfigEntry *ent;
  165. while(readline(f, &buffer, &maxlen))
  166. {
  167. char *line, *comment;
  168. char key[256] = "";
  169. char value[256] = "";
  170. line = rstrip(lstrip(buffer));
  171. if(!line[0]) continue;
  172. if(line[0] == '[')
  173. {
  174. char *section = line+1;
  175. char *endsection;
  176. endsection = strchr(section, ']');
  177. if(!endsection || section == endsection)
  178. {
  179. ERR("config parse error: bad line \"%s\"\n", line);
  180. continue;
  181. }
  182. if(endsection[1] != 0)
  183. {
  184. char *end = endsection+1;
  185. while(isspace(*end))
  186. ++end;
  187. if(*end != 0 && *end != '#')
  188. {
  189. ERR("config parse error: bad line \"%s\"\n", line);
  190. continue;
  191. }
  192. }
  193. *endsection = 0;
  194. if(strcasecmp(section, "general") == 0)
  195. curSection[0] = 0;
  196. else
  197. {
  198. strncpy(curSection, section, sizeof(curSection)-1);
  199. curSection[sizeof(curSection)-1] = 0;
  200. }
  201. continue;
  202. }
  203. comment = strchr(line, '#');
  204. if(comment) *(comment++) = 0;
  205. if(!line[0]) continue;
  206. if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
  207. sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
  208. sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
  209. {
  210. /* sscanf doesn't handle '' or "" as empty values, so clip it
  211. * manually. */
  212. if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
  213. value[0] = 0;
  214. }
  215. else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
  216. {
  217. /* Special case for 'key =' */
  218. value[0] = 0;
  219. }
  220. else
  221. {
  222. ERR("config parse error: malformed option line: \"%s\"\n\n", line);
  223. continue;
  224. }
  225. rstrip(key);
  226. if(curSection[0] != 0)
  227. {
  228. size_t len = strlen(curSection);
  229. memmove(&key[len+1], key, sizeof(key)-1-len);
  230. key[len] = '/';
  231. memcpy(key, curSection, len);
  232. }
  233. /* Check if we already have this option set */
  234. ent = cfgBlock.entries;
  235. while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
  236. {
  237. if(strcasecmp(ent->key, key) == 0)
  238. break;
  239. ent++;
  240. }
  241. if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
  242. {
  243. /* Allocate a new option entry */
  244. ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
  245. if(!ent)
  246. {
  247. ERR("config parse error: error reallocating config entries\n");
  248. continue;
  249. }
  250. cfgBlock.entries = ent;
  251. ent = cfgBlock.entries + cfgBlock.entryCount;
  252. cfgBlock.entryCount++;
  253. ent->key = strdup(key);
  254. ent->value = NULL;
  255. }
  256. free(ent->value);
  257. ent->value = expdup(value);
  258. TRACE("found '%s' = '%s'\n", ent->key, ent->value);
  259. }
  260. free(buffer);
  261. }
  262. #ifdef _WIN32
  263. void ReadALConfig(void)
  264. {
  265. WCHAR buffer[PATH_MAX];
  266. const WCHAR *str;
  267. al_string ppath;
  268. FILE *f;
  269. if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  270. {
  271. al_string filepath = AL_STRING_INIT_STATIC();
  272. al_string_copy_wcstr(&filepath, buffer);
  273. al_string_append_cstr(&filepath, "\\alsoft.ini");
  274. TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
  275. f = al_fopen(al_string_get_cstr(filepath), "rt");
  276. if(f)
  277. {
  278. LoadConfigFromFile(f);
  279. fclose(f);
  280. }
  281. al_string_deinit(&filepath);
  282. }
  283. ppath = GetProcPath();
  284. if(!al_string_empty(ppath))
  285. {
  286. al_string_append_cstr(&ppath, "\\alsoft.ini");
  287. TRACE("Loading config %s...\n", al_string_get_cstr(ppath));
  288. f = al_fopen(al_string_get_cstr(ppath), "r");
  289. if(f)
  290. {
  291. LoadConfigFromFile(f);
  292. fclose(f);
  293. }
  294. }
  295. if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
  296. {
  297. al_string filepath = AL_STRING_INIT_STATIC();
  298. al_string_copy_wcstr(&filepath, str);
  299. TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
  300. f = al_fopen(al_string_get_cstr(filepath), "rt");
  301. if(f)
  302. {
  303. LoadConfigFromFile(f);
  304. fclose(f);
  305. }
  306. al_string_deinit(&filepath);
  307. }
  308. al_string_deinit(&ppath);
  309. }
  310. #else
  311. void ReadALConfig(void)
  312. {
  313. char buffer[PATH_MAX];
  314. const char *str;
  315. al_string ppath;
  316. FILE *f;
  317. str = "/etc/openal/alsoft.conf";
  318. TRACE("Loading config %s...\n", str);
  319. f = al_fopen(str, "r");
  320. if(f)
  321. {
  322. LoadConfigFromFile(f);
  323. fclose(f);
  324. }
  325. if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
  326. str = "/etc/xdg";
  327. strncpy(buffer, str, sizeof(buffer)-1);
  328. buffer[sizeof(buffer)-1] = 0;
  329. /* Go through the list in reverse, since "the order of base directories
  330. * denotes their importance; the first directory listed is the most
  331. * important". Ergo, we need to load the settings from the later dirs
  332. * first so that the settings in the earlier dirs override them.
  333. */
  334. while(1)
  335. {
  336. char *next = strrchr(buffer, ':');
  337. if(next) *(next++) = 0;
  338. else next = buffer;
  339. if(next[0] != '/')
  340. WARN("Ignoring XDG config dir: %s\n", next);
  341. else
  342. {
  343. size_t len = strlen(next);
  344. strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
  345. buffer[sizeof(buffer)-1] = 0;
  346. TRACE("Loading config %s...\n", next);
  347. f = al_fopen(next, "r");
  348. if(f)
  349. {
  350. LoadConfigFromFile(f);
  351. fclose(f);
  352. }
  353. }
  354. if(next == buffer)
  355. break;
  356. }
  357. if((str=getenv("HOME")) != NULL && *str)
  358. {
  359. snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
  360. TRACE("Loading config %s...\n", buffer);
  361. f = al_fopen(buffer, "r");
  362. if(f)
  363. {
  364. LoadConfigFromFile(f);
  365. fclose(f);
  366. }
  367. }
  368. if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
  369. snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
  370. else
  371. {
  372. buffer[0] = 0;
  373. if((str=getenv("HOME")) != NULL && str[0] != 0)
  374. snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
  375. }
  376. if(buffer[0] != 0)
  377. {
  378. TRACE("Loading config %s...\n", buffer);
  379. f = al_fopen(buffer, "r");
  380. if(f)
  381. {
  382. LoadConfigFromFile(f);
  383. fclose(f);
  384. }
  385. }
  386. ppath = GetProcPath();
  387. if(!al_string_empty(ppath))
  388. {
  389. al_string_append_cstr(&ppath, "/alsoft.conf");
  390. TRACE("Loading config %s...\n", al_string_get_cstr(ppath));
  391. f = al_fopen(al_string_get_cstr(ppath), "r");
  392. if(f)
  393. {
  394. LoadConfigFromFile(f);
  395. fclose(f);
  396. }
  397. }
  398. if((str=getenv("ALSOFT_CONF")) != NULL && *str)
  399. {
  400. TRACE("Loading config %s...\n", str);
  401. f = al_fopen(str, "r");
  402. if(f)
  403. {
  404. LoadConfigFromFile(f);
  405. fclose(f);
  406. }
  407. }
  408. al_string_deinit(&ppath);
  409. }
  410. #endif
  411. void FreeALConfig(void)
  412. {
  413. unsigned int i;
  414. for(i = 0;i < cfgBlock.entryCount;i++)
  415. {
  416. free(cfgBlock.entries[i].key);
  417. free(cfgBlock.entries[i].value);
  418. }
  419. free(cfgBlock.entries);
  420. }
  421. const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
  422. {
  423. unsigned int i;
  424. char key[256];
  425. if(!keyName)
  426. return def;
  427. if(blockName && strcasecmp(blockName, "general") != 0)
  428. {
  429. if(devName)
  430. snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
  431. else
  432. snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
  433. }
  434. else
  435. {
  436. if(devName)
  437. snprintf(key, sizeof(key), "%s/%s", devName, keyName);
  438. else
  439. {
  440. strncpy(key, keyName, sizeof(key)-1);
  441. key[sizeof(key)-1] = 0;
  442. }
  443. }
  444. for(i = 0;i < cfgBlock.entryCount;i++)
  445. {
  446. if(strcmp(cfgBlock.entries[i].key, key) == 0)
  447. {
  448. TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
  449. if(cfgBlock.entries[i].value[0])
  450. return cfgBlock.entries[i].value;
  451. return def;
  452. }
  453. }
  454. if(!devName)
  455. {
  456. TRACE("Key %s not found\n", key);
  457. return def;
  458. }
  459. return GetConfigValue(NULL, blockName, keyName, def);
  460. }
  461. int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
  462. {
  463. const char *val = GetConfigValue(devName, blockName, keyName, "");
  464. return !!val[0];
  465. }
  466. int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
  467. {
  468. const char *val = GetConfigValue(devName, blockName, keyName, "");
  469. if(!val[0]) return 0;
  470. *ret = val;
  471. return 1;
  472. }
  473. int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
  474. {
  475. const char *val = GetConfigValue(devName, blockName, keyName, "");
  476. if(!val[0]) return 0;
  477. *ret = strtol(val, NULL, 0);
  478. return 1;
  479. }
  480. int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
  481. {
  482. const char *val = GetConfigValue(devName, blockName, keyName, "");
  483. if(!val[0]) return 0;
  484. *ret = strtoul(val, NULL, 0);
  485. return 1;
  486. }
  487. int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
  488. {
  489. const char *val = GetConfigValue(devName, blockName, keyName, "");
  490. if(!val[0]) return 0;
  491. #ifdef HAVE_STRTOF
  492. *ret = strtof(val, NULL);
  493. #else
  494. *ret = (float)strtod(val, NULL);
  495. #endif
  496. return 1;
  497. }
  498. int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
  499. {
  500. const char *val = GetConfigValue(devName, blockName, keyName, "");
  501. if(!val[0]) return 0;
  502. *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  503. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  504. return 1;
  505. }
  506. int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
  507. {
  508. const char *val = GetConfigValue(devName, blockName, keyName, "");
  509. if(!val[0]) return !!def;
  510. return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  511. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  512. }