alconfig.c 19 KB

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