🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
15 KiB

  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. #include "config.h"
  21. #include "alconfig.h"
  22. #include <cstdlib>
  23. #include <cctype>
  24. #include <cstring>
  25. #ifdef _WIN32
  26. #include <windows.h>
  27. #include <shlobj.h>
  28. #endif
  29. #ifdef __APPLE__
  30. #include <CoreFoundation/CoreFoundation.h>
  31. #endif
  32. #include <algorithm>
  33. #include <cstdio>
  34. #include <string>
  35. #include <utility>
  36. #include "alfstream.h"
  37. #include "alstring.h"
  38. #include "core/helpers.h"
  39. #include "core/logging.h"
  40. #include "strutils.h"
  41. #include "vector.h"
  42. namespace {
  43. struct ConfigEntry {
  44. std::string key;
  45. std::string value;
  46. };
  47. al::vector<ConfigEntry> ConfOpts;
  48. std::string &lstrip(std::string &line)
  49. {
  50. size_t pos{0};
  51. while(pos < line.length() && std::isspace(line[pos]))
  52. ++pos;
  53. line.erase(0, pos);
  54. return line;
  55. }
  56. bool readline(std::istream &f, std::string &output)
  57. {
  58. while(f.good() && f.peek() == '\n')
  59. f.ignore();
  60. return std::getline(f, output) && !output.empty();
  61. }
  62. std::string expdup(const char *str)
  63. {
  64. std::string output;
  65. std::string envval;
  66. while(*str != '\0')
  67. {
  68. const char *addstr;
  69. size_t addstrlen;
  70. if(str[0] != '$')
  71. {
  72. const char *next = std::strchr(str, '$');
  73. addstr = str;
  74. addstrlen = next ? static_cast<size_t>(next-str) : std::strlen(str);
  75. str += addstrlen;
  76. }
  77. else
  78. {
  79. str++;
  80. if(*str == '$')
  81. {
  82. const char *next = std::strchr(str+1, '$');
  83. addstr = str;
  84. addstrlen = next ? static_cast<size_t>(next-str) : std::strlen(str);
  85. str += addstrlen;
  86. }
  87. else
  88. {
  89. const bool hasbraces{(*str == '{')};
  90. if(hasbraces) str++;
  91. const char *envstart = str;
  92. while(std::isalnum(*str) || *str == '_')
  93. ++str;
  94. if(hasbraces && *str != '}')
  95. continue;
  96. const std::string envname{envstart, str};
  97. if(hasbraces) str++;
  98. envval = al::getenv(envname.c_str()).value_or(std::string{});
  99. addstr = envval.data();
  100. addstrlen = envval.length();
  101. }
  102. }
  103. if(addstrlen == 0)
  104. continue;
  105. output.append(addstr, addstrlen);
  106. }
  107. return output;
  108. }
  109. void LoadConfigFromFile(std::istream &f)
  110. {
  111. std::string curSection;
  112. std::string buffer;
  113. while(readline(f, buffer))
  114. {
  115. if(lstrip(buffer).empty())
  116. continue;
  117. if(buffer[0] == '[')
  118. {
  119. char *line{&buffer[0]};
  120. char *section = line+1;
  121. char *endsection;
  122. endsection = std::strchr(section, ']');
  123. if(!endsection || section == endsection)
  124. {
  125. ERR(" config parse error: bad line \"%s\"\n", line);
  126. continue;
  127. }
  128. if(endsection[1] != 0)
  129. {
  130. char *end = endsection+1;
  131. while(std::isspace(*end))
  132. ++end;
  133. if(*end != 0 && *end != '#')
  134. {
  135. ERR(" config parse error: bad line \"%s\"\n", line);
  136. continue;
  137. }
  138. }
  139. *endsection = 0;
  140. curSection.clear();
  141. if(al::strcasecmp(section, "general") != 0)
  142. {
  143. do {
  144. char *nextp = std::strchr(section, '%');
  145. if(!nextp)
  146. {
  147. curSection += section;
  148. break;
  149. }
  150. curSection.append(section, nextp);
  151. section = nextp;
  152. if(((section[1] >= '0' && section[1] <= '9') ||
  153. (section[1] >= 'a' && section[1] <= 'f') ||
  154. (section[1] >= 'A' && section[1] <= 'F')) &&
  155. ((section[2] >= '0' && section[2] <= '9') ||
  156. (section[2] >= 'a' && section[2] <= 'f') ||
  157. (section[2] >= 'A' && section[2] <= 'F')))
  158. {
  159. int b{0};
  160. if(section[1] >= '0' && section[1] <= '9')
  161. b = (section[1]-'0') << 4;
  162. else if(section[1] >= 'a' && section[1] <= 'f')
  163. b = (section[1]-'a'+0xa) << 4;
  164. else if(section[1] >= 'A' && section[1] <= 'F')
  165. b = (section[1]-'A'+0x0a) << 4;
  166. if(section[2] >= '0' && section[2] <= '9')
  167. b |= (section[2]-'0');
  168. else if(section[2] >= 'a' && section[2] <= 'f')
  169. b |= (section[2]-'a'+0xa);
  170. else if(section[2] >= 'A' && section[2] <= 'F')
  171. b |= (section[2]-'A'+0x0a);
  172. curSection += static_cast<char>(b);
  173. section += 3;
  174. }
  175. else if(section[1] == '%')
  176. {
  177. curSection += '%';
  178. section += 2;
  179. }
  180. else
  181. {
  182. curSection += '%';
  183. section += 1;
  184. }
  185. } while(*section != 0);
  186. }
  187. continue;
  188. }
  189. auto cmtpos = std::min(buffer.find('#'), buffer.size());
  190. while(cmtpos > 0 && std::isspace(buffer[cmtpos-1]))
  191. --cmtpos;
  192. if(!cmtpos) continue;
  193. buffer.erase(cmtpos);
  194. auto sep = buffer.find('=');
  195. if(sep == std::string::npos)
  196. {
  197. ERR(" config parse error: malformed option line: \"%s\"\n", buffer.c_str());
  198. continue;
  199. }
  200. auto keyend = sep++;
  201. while(keyend > 0 && std::isspace(buffer[keyend-1]))
  202. --keyend;
  203. if(!keyend)
  204. {
  205. ERR(" config parse error: malformed option line: \"%s\"\n", buffer.c_str());
  206. continue;
  207. }
  208. while(sep < buffer.size() && std::isspace(buffer[sep]))
  209. sep++;
  210. std::string fullKey;
  211. if(!curSection.empty())
  212. {
  213. fullKey += curSection;
  214. fullKey += '/';
  215. }
  216. fullKey += buffer.substr(0u, keyend);
  217. std::string value{(sep < buffer.size()) ? buffer.substr(sep) : std::string{}};
  218. if(value.size() > 1)
  219. {
  220. if((value.front() == '"' && value.back() == '"')
  221. || (value.front() == '\'' && value.back() == '\''))
  222. {
  223. value.pop_back();
  224. value.erase(value.begin());
  225. }
  226. }
  227. TRACE(" found '%s' = '%s'\n", fullKey.c_str(), value.c_str());
  228. /* Check if we already have this option set */
  229. auto find_key = [&fullKey](const ConfigEntry &entry) -> bool
  230. { return entry.key == fullKey; };
  231. auto ent = std::find_if(ConfOpts.begin(), ConfOpts.end(), find_key);
  232. if(ent != ConfOpts.end())
  233. {
  234. if(!value.empty())
  235. ent->value = expdup(value.c_str());
  236. else
  237. ConfOpts.erase(ent);
  238. }
  239. else if(!value.empty())
  240. ConfOpts.emplace_back(ConfigEntry{std::move(fullKey), expdup(value.c_str())});
  241. }
  242. ConfOpts.shrink_to_fit();
  243. }
  244. const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName)
  245. {
  246. if(!keyName)
  247. return nullptr;
  248. std::string key;
  249. if(blockName && al::strcasecmp(blockName, "general") != 0)
  250. {
  251. key = blockName;
  252. if(devName)
  253. {
  254. key += '/';
  255. key += devName;
  256. }
  257. key += '/';
  258. key += keyName;
  259. }
  260. else
  261. {
  262. if(devName)
  263. {
  264. key = devName;
  265. key += '/';
  266. }
  267. key += keyName;
  268. }
  269. auto iter = std::find_if(ConfOpts.cbegin(), ConfOpts.cend(),
  270. [&key](const ConfigEntry &entry) -> bool
  271. { return entry.key == key; });
  272. if(iter != ConfOpts.cend())
  273. {
  274. TRACE("Found %s = \"%s\"\n", key.c_str(), iter->value.c_str());
  275. if(!iter->value.empty())
  276. return iter->value.c_str();
  277. return nullptr;
  278. }
  279. if(!devName)
  280. {
  281. TRACE("Key %s not found\n", key.c_str());
  282. return nullptr;
  283. }
  284. return GetConfigValue(nullptr, blockName, keyName);
  285. }
  286. } // namespace
  287. #ifdef _WIN32
  288. void ReadALConfig()
  289. {
  290. WCHAR buffer[MAX_PATH];
  291. if(SHGetSpecialFolderPathW(nullptr, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  292. {
  293. std::string filepath{wstr_to_utf8(buffer)};
  294. filepath += "\\alsoft.ini";
  295. TRACE("Loading config %s...\n", filepath.c_str());
  296. al::ifstream f{filepath};
  297. if(f.is_open())
  298. LoadConfigFromFile(f);
  299. }
  300. std::string ppath{GetProcBinary().path};
  301. if(!ppath.empty())
  302. {
  303. ppath += "\\alsoft.ini";
  304. TRACE("Loading config %s...\n", ppath.c_str());
  305. al::ifstream f{ppath};
  306. if(f.is_open())
  307. LoadConfigFromFile(f);
  308. }
  309. if(auto confpath = al::getenv(L"ALSOFT_CONF"))
  310. {
  311. TRACE("Loading config %s...\n", wstr_to_utf8(confpath->c_str()).c_str());
  312. al::ifstream f{*confpath};
  313. if(f.is_open())
  314. LoadConfigFromFile(f);
  315. }
  316. }
  317. #else
  318. void ReadALConfig()
  319. {
  320. const char *str{"/etc/openal/alsoft.conf"};
  321. TRACE("Loading config %s...\n", str);
  322. al::ifstream f{str};
  323. if(f.is_open())
  324. LoadConfigFromFile(f);
  325. f.close();
  326. std::string confpaths{al::getenv("XDG_CONFIG_DIRS").value_or("/etc/xdg")};
  327. /* Go through the list in reverse, since "the order of base directories
  328. * denotes their importance; the first directory listed is the most
  329. * important". Ergo, we need to load the settings from the later dirs
  330. * first so that the settings in the earlier dirs override them.
  331. */
  332. std::string fname;
  333. while(!confpaths.empty())
  334. {
  335. auto next = confpaths.find_last_of(':');
  336. if(next < confpaths.length())
  337. {
  338. fname = confpaths.substr(next+1);
  339. confpaths.erase(next);
  340. }
  341. else
  342. {
  343. fname = confpaths;
  344. confpaths.clear();
  345. }
  346. if(fname.empty() || fname.front() != '/')
  347. WARN("Ignoring XDG config dir: %s\n", fname.c_str());
  348. else
  349. {
  350. if(fname.back() != '/') fname += "/alsoft.conf";
  351. else fname += "alsoft.conf";
  352. TRACE("Loading config %s...\n", fname.c_str());
  353. f = al::ifstream{fname};
  354. if(f.is_open())
  355. LoadConfigFromFile(f);
  356. }
  357. fname.clear();
  358. }
  359. #ifdef __APPLE__
  360. CFBundleRef mainBundle = CFBundleGetMainBundle();
  361. if(mainBundle)
  362. {
  363. unsigned char fileName[PATH_MAX];
  364. CFURLRef configURL;
  365. if((configURL=CFBundleCopyResourceURL(mainBundle, CFSTR(".alsoftrc"), CFSTR(""), nullptr)) &&
  366. CFURLGetFileSystemRepresentation(configURL, true, fileName, sizeof(fileName)))
  367. {
  368. f = al::ifstream{reinterpret_cast<char*>(fileName)};
  369. if(f.is_open())
  370. LoadConfigFromFile(f);
  371. }
  372. }
  373. #endif
  374. if(auto homedir = al::getenv("HOME"))
  375. {
  376. fname = *homedir;
  377. if(fname.back() != '/') fname += "/.alsoftrc";
  378. else fname += ".alsoftrc";
  379. TRACE("Loading config %s...\n", fname.c_str());
  380. f = al::ifstream{fname};
  381. if(f.is_open())
  382. LoadConfigFromFile(f);
  383. }
  384. if(auto configdir = al::getenv("XDG_CONFIG_HOME"))
  385. {
  386. fname = *configdir;
  387. if(fname.back() != '/') fname += "/alsoft.conf";
  388. else fname += "alsoft.conf";
  389. }
  390. else
  391. {
  392. fname.clear();
  393. if(auto homedir = al::getenv("HOME"))
  394. {
  395. fname = *homedir;
  396. if(fname.back() != '/') fname += "/.config/alsoft.conf";
  397. else fname += ".config/alsoft.conf";
  398. }
  399. }
  400. if(!fname.empty())
  401. {
  402. TRACE("Loading config %s...\n", fname.c_str());
  403. f = al::ifstream{fname};
  404. if(f.is_open())
  405. LoadConfigFromFile(f);
  406. }
  407. std::string ppath{GetProcBinary().path};
  408. if(!ppath.empty())
  409. {
  410. if(ppath.back() != '/') ppath += "/alsoft.conf";
  411. else ppath += "alsoft.conf";
  412. TRACE("Loading config %s...\n", ppath.c_str());
  413. f = al::ifstream{ppath};
  414. if(f.is_open())
  415. LoadConfigFromFile(f);
  416. }
  417. if(auto confname = al::getenv("ALSOFT_CONF"))
  418. {
  419. TRACE("Loading config %s...\n", confname->c_str());
  420. f = al::ifstream{*confname};
  421. if(f.is_open())
  422. LoadConfigFromFile(f);
  423. }
  424. }
  425. #endif
  426. al::optional<std::string> ConfigValueStr(const char *devName, const char *blockName, const char *keyName)
  427. {
  428. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  429. return al::make_optional<std::string>(val);
  430. return al::nullopt;
  431. }
  432. al::optional<int> ConfigValueInt(const char *devName, const char *blockName, const char *keyName)
  433. {
  434. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  435. return al::make_optional(static_cast<int>(std::strtol(val, nullptr, 0)));
  436. return al::nullopt;
  437. }
  438. al::optional<unsigned int> ConfigValueUInt(const char *devName, const char *blockName, const char *keyName)
  439. {
  440. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  441. return al::make_optional(static_cast<unsigned int>(std::strtoul(val, nullptr, 0)));
  442. return al::nullopt;
  443. }
  444. al::optional<float> ConfigValueFloat(const char *devName, const char *blockName, const char *keyName)
  445. {
  446. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  447. return al::make_optional(std::strtof(val, nullptr));
  448. return al::nullopt;
  449. }
  450. al::optional<bool> ConfigValueBool(const char *devName, const char *blockName, const char *keyName)
  451. {
  452. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  453. return al::make_optional(al::strcasecmp(val, "on") == 0 || al::strcasecmp(val, "yes") == 0
  454. || al::strcasecmp(val, "true")==0 || atoi(val) != 0);
  455. return al::nullopt;
  456. }
  457. bool GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, bool def)
  458. {
  459. if(const char *val{GetConfigValue(devName, blockName, keyName)})
  460. return (al::strcasecmp(val, "on") == 0 || al::strcasecmp(val, "yes") == 0
  461. || al::strcasecmp(val, "true") == 0 || atoi(val) != 0);
  462. return def;
  463. }