config-file.c 17 KB

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