config-file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <inttypes.h>
  17. #include <stdio.h>
  18. #include <wchar.h>
  19. #include "config-file.h"
  20. #include "threading.h"
  21. #include "platform.h"
  22. #include "base.h"
  23. #include "bmem.h"
  24. #include "lexer.h"
  25. #include "dstr.h"
  26. #include "uthash.h"
  27. struct config_item {
  28. char *name;
  29. char *value;
  30. UT_hash_handle hh;
  31. };
  32. static inline void config_item_free(struct config_item *item)
  33. {
  34. bfree(item->name);
  35. bfree(item->value);
  36. bfree(item);
  37. }
  38. struct config_section {
  39. char *name;
  40. struct config_item *items;
  41. UT_hash_handle hh;
  42. };
  43. static inline void config_section_free(struct config_section *section)
  44. {
  45. struct config_item *item;
  46. struct config_item *temp;
  47. HASH_ITER (hh, section->items, item, temp) {
  48. HASH_DELETE(hh, section->items, item);
  49. config_item_free(item);
  50. }
  51. bfree(section->name);
  52. bfree(section);
  53. }
  54. struct config_data {
  55. char *file;
  56. struct config_section *sections;
  57. struct config_section *defaults;
  58. pthread_mutex_t mutex;
  59. };
  60. config_t *config_create(const char *file)
  61. {
  62. struct config_data *config;
  63. FILE *f;
  64. f = os_fopen(file, "wb");
  65. if (!f)
  66. return NULL;
  67. fclose(f);
  68. config = bzalloc(sizeof(struct config_data));
  69. if (pthread_mutex_init_recursive(&config->mutex) != 0) {
  70. bfree(config);
  71. return NULL;
  72. }
  73. config->file = bstrdup(file);
  74. return config;
  75. }
  76. static bool config_parse_string(struct lexer *lex, struct strref *ref, char end)
  77. {
  78. bool success = end != 0;
  79. struct base_token token;
  80. base_token_clear(&token);
  81. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  82. if (end) {
  83. if (*token.text.array == end) {
  84. success = true;
  85. break;
  86. } else if (is_newline(*token.text.array)) {
  87. success = false;
  88. break;
  89. }
  90. } else {
  91. if (is_newline(*token.text.array)) {
  92. success = true;
  93. break;
  94. }
  95. }
  96. strref_add(ref, &token.text);
  97. }
  98. //remove_ref_whitespace(ref);
  99. return success;
  100. }
  101. static void unescape(struct dstr *str)
  102. {
  103. char *read = str->array;
  104. char *write = str->array;
  105. for (; *read; read++, write++) {
  106. char cur = *read;
  107. if (cur == '\\') {
  108. char next = read[1];
  109. if (next == '\\') {
  110. read++;
  111. } else if (next == 'r') {
  112. cur = '\r';
  113. read++;
  114. } else if (next == 'n') {
  115. cur = '\n';
  116. read++;
  117. }
  118. }
  119. if (read != write)
  120. *write = cur;
  121. }
  122. if (read != write)
  123. *write = '\0';
  124. }
  125. static void config_add_item(struct config_item **items, struct strref *name, struct strref *value)
  126. {
  127. struct config_item *item;
  128. struct dstr item_value;
  129. item = bzalloc(sizeof(struct config_item));
  130. item->name = bstrdup_n(name->array, name->len);
  131. if (!strref_is_empty(value)) {
  132. dstr_init_copy_strref(&item_value, value);
  133. unescape(&item_value);
  134. item->value = bstrdup_n(item_value.array, item_value.len);
  135. dstr_free(&item_value);
  136. } else {
  137. item->value = bzalloc(1);
  138. }
  139. HASH_ADD_STR(*items, name, item);
  140. }
  141. static void config_parse_section(struct config_section *section, struct lexer *lex)
  142. {
  143. struct base_token token;
  144. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  145. struct strref name, value;
  146. while (token.type == BASETOKEN_WHITESPACE) {
  147. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  148. return;
  149. }
  150. if (token.type == BASETOKEN_OTHER) {
  151. if (*token.text.array == '#') {
  152. do {
  153. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  154. return;
  155. } while (!is_newline(*token.text.array));
  156. continue;
  157. } else if (*token.text.array == '[') {
  158. lex->offset--;
  159. return;
  160. }
  161. }
  162. strref_copy(&name, &token.text);
  163. if (!config_parse_string(lex, &name, '='))
  164. continue;
  165. strref_clear(&value);
  166. config_parse_string(lex, &value, 0);
  167. config_add_item(&section->items, &name, &value);
  168. }
  169. }
  170. static void parse_config_data(struct config_section **sections, struct lexer *lex)
  171. {
  172. struct strref section_name;
  173. struct base_token token;
  174. base_token_clear(&token);
  175. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  176. struct config_section *section;
  177. while (token.type == BASETOKEN_WHITESPACE) {
  178. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  179. return;
  180. }
  181. if (*token.text.array != '[') {
  182. while (!is_newline(*token.text.array)) {
  183. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  184. return;
  185. }
  186. continue;
  187. }
  188. strref_clear(&section_name);
  189. config_parse_string(lex, &section_name, ']');
  190. if (!section_name.len)
  191. return;
  192. section = bzalloc(sizeof(struct config_section));
  193. section->name = bstrdup_n(section_name.array, section_name.len);
  194. config_parse_section(section, lex);
  195. HASH_ADD_STR(*sections, name, section);
  196. }
  197. }
  198. static int config_parse_file(struct config_section **sections, const char *file, bool always_open)
  199. {
  200. char *file_data;
  201. struct lexer lex;
  202. FILE *f;
  203. f = os_fopen(file, "rb");
  204. if (always_open && !f)
  205. f = os_fopen(file, "w+");
  206. if (!f)
  207. return CONFIG_FILENOTFOUND;
  208. os_fread_utf8(f, &file_data);
  209. fclose(f);
  210. if (!file_data)
  211. return CONFIG_SUCCESS;
  212. lexer_init(&lex);
  213. lexer_start_move(&lex, file_data);
  214. parse_config_data(sections, &lex);
  215. lexer_free(&lex);
  216. return CONFIG_SUCCESS;
  217. }
  218. int config_open(config_t **config, const char *file, enum config_open_type open_type)
  219. {
  220. int errorcode;
  221. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  222. if (!config)
  223. return CONFIG_ERROR;
  224. *config = bzalloc(sizeof(struct config_data));
  225. if (!*config)
  226. return CONFIG_ERROR;
  227. if (pthread_mutex_init_recursive(&(*config)->mutex) != 0) {
  228. bfree(*config);
  229. return CONFIG_ERROR;
  230. }
  231. (*config)->file = bstrdup(file);
  232. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  233. if (errorcode != CONFIG_SUCCESS) {
  234. config_close(*config);
  235. *config = NULL;
  236. }
  237. return errorcode;
  238. }
  239. int config_open_string(config_t **config, const char *str)
  240. {
  241. struct lexer lex;
  242. if (!config)
  243. return CONFIG_ERROR;
  244. *config = bzalloc(sizeof(struct config_data));
  245. if (!*config)
  246. return CONFIG_ERROR;
  247. if (pthread_mutex_init_recursive(&(*config)->mutex) != 0) {
  248. bfree(*config);
  249. return CONFIG_ERROR;
  250. }
  251. (*config)->file = NULL;
  252. lexer_init(&lex);
  253. lexer_start(&lex, str);
  254. parse_config_data(&(*config)->sections, &lex);
  255. lexer_free(&lex);
  256. return CONFIG_SUCCESS;
  257. }
  258. int config_open_defaults(config_t *config, const char *file)
  259. {
  260. if (!config)
  261. return CONFIG_ERROR;
  262. return config_parse_file(&config->defaults, file, false);
  263. }
  264. int config_save(config_t *config)
  265. {
  266. FILE *f;
  267. struct dstr str, tmp;
  268. int ret = CONFIG_ERROR;
  269. if (!config)
  270. return CONFIG_ERROR;
  271. if (!config->file)
  272. return CONFIG_ERROR;
  273. dstr_init(&str);
  274. dstr_init(&tmp);
  275. pthread_mutex_lock(&config->mutex);
  276. f = os_fopen(config->file, "wb");
  277. if (!f) {
  278. pthread_mutex_unlock(&config->mutex);
  279. return CONFIG_FILENOTFOUND;
  280. }
  281. struct config_section *section, *stmp;
  282. struct config_item *item, *itmp;
  283. int idx = 0;
  284. HASH_ITER (hh, config->sections, section, stmp) {
  285. if (idx++)
  286. dstr_cat(&str, "\n");
  287. dstr_cat(&str, "[");
  288. dstr_cat(&str, section->name);
  289. dstr_cat(&str, "]\n");
  290. HASH_ITER (hh, section->items, item, itmp) {
  291. dstr_copy(&tmp, item->value ? item->value : "");
  292. dstr_replace(&tmp, "\\", "\\\\");
  293. dstr_replace(&tmp, "\r", "\\r");
  294. dstr_replace(&tmp, "\n", "\\n");
  295. dstr_cat(&str, item->name);
  296. dstr_cat(&str, "=");
  297. dstr_cat(&str, tmp.array);
  298. dstr_cat(&str, "\n");
  299. }
  300. }
  301. #ifdef _WIN32
  302. if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1)
  303. goto cleanup;
  304. #endif
  305. if (fwrite(str.array, str.len, 1, f) != 1)
  306. goto cleanup;
  307. ret = CONFIG_SUCCESS;
  308. cleanup:
  309. fclose(f);
  310. pthread_mutex_unlock(&config->mutex);
  311. dstr_free(&tmp);
  312. dstr_free(&str);
  313. return ret;
  314. }
  315. int config_save_safe(config_t *config, const char *temp_ext, const char *backup_ext)
  316. {
  317. struct dstr temp_file = {0};
  318. struct dstr backup_file = {0};
  319. char *file = config->file;
  320. int ret;
  321. if (!temp_ext || !*temp_ext) {
  322. blog(LOG_ERROR, "config_save_safe: invalid "
  323. "temporary extension specified");
  324. return CONFIG_ERROR;
  325. }
  326. pthread_mutex_lock(&config->mutex);
  327. dstr_copy(&temp_file, config->file);
  328. if (*temp_ext != '.')
  329. dstr_cat(&temp_file, ".");
  330. dstr_cat(&temp_file, temp_ext);
  331. config->file = temp_file.array;
  332. ret = config_save(config);
  333. config->file = file;
  334. if (ret != CONFIG_SUCCESS) {
  335. blog(LOG_ERROR,
  336. "config_save_safe: failed to "
  337. "write to %s",
  338. temp_file.array);
  339. goto cleanup;
  340. }
  341. if (backup_ext && *backup_ext) {
  342. dstr_copy(&backup_file, config->file);
  343. if (*backup_ext != '.')
  344. dstr_cat(&backup_file, ".");
  345. dstr_cat(&backup_file, backup_ext);
  346. }
  347. if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
  348. ret = CONFIG_ERROR;
  349. cleanup:
  350. pthread_mutex_unlock(&config->mutex);
  351. dstr_free(&temp_file);
  352. dstr_free(&backup_file);
  353. return ret;
  354. }
  355. void config_close(config_t *config)
  356. {
  357. struct config_section *section, *temp;
  358. if (!config)
  359. return;
  360. HASH_ITER (hh, config->sections, section, temp) {
  361. HASH_DELETE(hh, config->sections, section);
  362. config_section_free(section);
  363. }
  364. HASH_ITER (hh, config->defaults, section, temp) {
  365. HASH_DELETE(hh, config->defaults, section);
  366. config_section_free(section);
  367. }
  368. bfree(config->file);
  369. pthread_mutex_destroy(&config->mutex);
  370. bfree(config);
  371. }
  372. size_t config_num_sections(config_t *config)
  373. {
  374. return HASH_CNT(hh, config->sections);
  375. }
  376. const char *config_get_section(config_t *config, size_t idx)
  377. {
  378. struct config_section *section;
  379. struct config_section *temp;
  380. const char *name = NULL;
  381. size_t ctr = 0;
  382. pthread_mutex_lock(&config->mutex);
  383. if (idx >= config_num_sections(config))
  384. goto unlock;
  385. HASH_ITER (hh, config->sections, section, temp) {
  386. if (idx == ctr++) {
  387. name = section->name;
  388. break;
  389. }
  390. }
  391. unlock:
  392. pthread_mutex_unlock(&config->mutex);
  393. return name;
  394. }
  395. static const struct config_item *config_find_item(const struct config_section *sections, const char *section,
  396. const char *name)
  397. {
  398. struct config_section *sec;
  399. struct config_item *res;
  400. HASH_FIND_STR(sections, section, sec);
  401. if (!sec)
  402. return NULL;
  403. HASH_FIND_STR(sec->items, name, res);
  404. return res;
  405. }
  406. static void config_set_item(config_t *config, struct config_section **sections, const char *section, const char *name,
  407. char *value)
  408. {
  409. struct config_section *sec;
  410. struct config_item *item;
  411. pthread_mutex_lock(&config->mutex);
  412. HASH_FIND_STR(*sections, section, sec);
  413. if (!sec) {
  414. sec = bzalloc(sizeof(struct config_section));
  415. sec->name = bstrdup(section);
  416. HASH_ADD_STR(*sections, name, sec);
  417. }
  418. HASH_FIND_STR(sec->items, name, item);
  419. if (!item) {
  420. item = bzalloc(sizeof(struct config_item));
  421. item->name = bstrdup(name);
  422. item->value = value;
  423. HASH_ADD_STR(sec->items, name, item);
  424. } else {
  425. bfree(item->value);
  426. item->value = value;
  427. }
  428. pthread_mutex_unlock(&config->mutex);
  429. }
  430. static void config_set_item_default(config_t *config, const char *section, const char *name, char *value)
  431. {
  432. config_set_item(config, &config->defaults, section, name, value);
  433. if (!config_has_user_value(config, section, name))
  434. config_set_item(config, &config->sections, section, name, bstrdup(value));
  435. }
  436. void config_set_string(config_t *config, const char *section, const char *name, const char *value)
  437. {
  438. if (!value)
  439. value = "";
  440. config_set_item(config, &config->sections, section, name, bstrdup(value));
  441. }
  442. void config_set_int(config_t *config, const char *section, const char *name, int64_t value)
  443. {
  444. struct dstr str;
  445. dstr_init(&str);
  446. dstr_printf(&str, "%" PRId64, value);
  447. config_set_item(config, &config->sections, section, name, str.array);
  448. }
  449. void config_set_uint(config_t *config, const char *section, const char *name, uint64_t value)
  450. {
  451. struct dstr str;
  452. dstr_init(&str);
  453. dstr_printf(&str, "%" PRIu64, value);
  454. config_set_item(config, &config->sections, section, name, str.array);
  455. }
  456. void config_set_bool(config_t *config, const char *section, const char *name, bool value)
  457. {
  458. char *str = bstrdup(value ? "true" : "false");
  459. config_set_item(config, &config->sections, section, name, str);
  460. }
  461. void config_set_double(config_t *config, const char *section, const char *name, double value)
  462. {
  463. char *str = bzalloc(64);
  464. os_dtostr(value, str, 64);
  465. config_set_item(config, &config->sections, section, name, str);
  466. }
  467. void config_set_default_string(config_t *config, const char *section, const char *name, const char *value)
  468. {
  469. if (!value)
  470. value = "";
  471. config_set_item_default(config, section, name, bstrdup(value));
  472. }
  473. void config_set_default_int(config_t *config, const char *section, const char *name, int64_t value)
  474. {
  475. struct dstr str;
  476. dstr_init(&str);
  477. dstr_printf(&str, "%" PRId64, value);
  478. config_set_item_default(config, section, name, str.array);
  479. }
  480. void config_set_default_uint(config_t *config, const char *section, const char *name, uint64_t value)
  481. {
  482. struct dstr str;
  483. dstr_init(&str);
  484. dstr_printf(&str, "%" PRIu64, value);
  485. config_set_item_default(config, section, name, str.array);
  486. }
  487. void config_set_default_bool(config_t *config, const char *section, const char *name, bool value)
  488. {
  489. char *str = bstrdup(value ? "true" : "false");
  490. config_set_item_default(config, section, name, str);
  491. }
  492. void config_set_default_double(config_t *config, const char *section, const char *name, double value)
  493. {
  494. struct dstr str;
  495. dstr_init(&str);
  496. dstr_printf(&str, "%g", value);
  497. config_set_item_default(config, section, name, str.array);
  498. }
  499. const char *config_get_string(config_t *config, const char *section, const char *name)
  500. {
  501. const struct config_item *item;
  502. const char *value = NULL;
  503. pthread_mutex_lock(&config->mutex);
  504. item = config_find_item(config->sections, section, name);
  505. if (!item)
  506. item = config_find_item(config->defaults, section, name);
  507. if (item)
  508. value = item->value;
  509. pthread_mutex_unlock(&config->mutex);
  510. return value;
  511. }
  512. static inline int64_t str_to_int64(const char *str)
  513. {
  514. if (!str || !*str)
  515. return 0;
  516. if (str[0] == '0' && str[1] == 'x')
  517. return strtoll(str + 2, NULL, 16);
  518. else
  519. return strtoll(str, NULL, 10);
  520. }
  521. static inline uint64_t str_to_uint64(const char *str)
  522. {
  523. if (!str || !*str)
  524. return 0;
  525. if (str[0] == '0' && str[1] == 'x')
  526. return strtoull(str + 2, NULL, 16);
  527. else
  528. return strtoull(str, NULL, 10);
  529. }
  530. int64_t config_get_int(config_t *config, const char *section, const char *name)
  531. {
  532. const char *value = config_get_string(config, section, name);
  533. if (value)
  534. return str_to_int64(value);
  535. return 0;
  536. }
  537. uint64_t config_get_uint(config_t *config, const char *section, const char *name)
  538. {
  539. const char *value = config_get_string(config, section, name);
  540. if (value)
  541. return str_to_uint64(value);
  542. return 0;
  543. }
  544. bool config_get_bool(config_t *config, const char *section, const char *name)
  545. {
  546. const char *value = config_get_string(config, section, name);
  547. if (value)
  548. return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  549. return false;
  550. }
  551. double config_get_double(config_t *config, const char *section, const char *name)
  552. {
  553. const char *value = config_get_string(config, section, name);
  554. if (value)
  555. return os_strtod(value);
  556. return 0.0;
  557. }
  558. bool config_remove_value(config_t *config, const char *section, const char *name)
  559. {
  560. struct config_section *sec;
  561. struct config_item *item;
  562. bool success = false;
  563. pthread_mutex_lock(&config->mutex);
  564. HASH_FIND_STR(config->sections, section, sec);
  565. if (sec) {
  566. HASH_FIND_STR(sec->items, name, item);
  567. if (item) {
  568. HASH_DELETE(hh, sec->items, item);
  569. config_item_free(item);
  570. success = true;
  571. }
  572. }
  573. pthread_mutex_unlock(&config->mutex);
  574. return success;
  575. }
  576. const char *config_get_default_string(config_t *config, const char *section, const char *name)
  577. {
  578. const struct config_item *item;
  579. const char *value = NULL;
  580. pthread_mutex_lock(&config->mutex);
  581. item = config_find_item(config->defaults, section, name);
  582. if (item)
  583. value = item->value;
  584. pthread_mutex_unlock(&config->mutex);
  585. return value;
  586. }
  587. int64_t config_get_default_int(config_t *config, const char *section, const char *name)
  588. {
  589. const char *value = config_get_default_string(config, section, name);
  590. if (value)
  591. return str_to_int64(value);
  592. return 0;
  593. }
  594. uint64_t config_get_default_uint(config_t *config, const char *section, const char *name)
  595. {
  596. const char *value = config_get_default_string(config, section, name);
  597. if (value)
  598. return str_to_uint64(value);
  599. return 0;
  600. }
  601. bool config_get_default_bool(config_t *config, const char *section, const char *name)
  602. {
  603. const char *value = config_get_default_string(config, section, name);
  604. if (value)
  605. return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  606. return false;
  607. }
  608. double config_get_default_double(config_t *config, const char *section, const char *name)
  609. {
  610. const char *value = config_get_default_string(config, section, name);
  611. if (value)
  612. return os_strtod(value);
  613. return 0.0;
  614. }
  615. bool config_has_user_value(config_t *config, const char *section, const char *name)
  616. {
  617. bool success;
  618. pthread_mutex_lock(&config->mutex);
  619. success = config_find_item(config->sections, section, name) != NULL;
  620. pthread_mutex_unlock(&config->mutex);
  621. return success;
  622. }
  623. bool config_has_default_value(config_t *config, const char *section, const char *name)
  624. {
  625. bool success;
  626. pthread_mutex_lock(&config->mutex);
  627. success = config_find_item(config->defaults, section, name) != NULL;
  628. pthread_mutex_unlock(&config->mutex);
  629. return success;
  630. }