config-file.c 16 KB

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