conf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. */
  14. #include "conf.h"
  15. #include "log.h"
  16. #include "util.h"
  17. #include "fileaccess.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. static inline uint_fast8_t conf_hash(const char *str)
  23. {
  24. return tiny_hash(str) & CONF_HASHTAB_MASK;
  25. }
  26. #define hashtab_add(tab, element) \
  27. do { \
  28. unsigned int _hash = conf_hash((element)->name); \
  29. typeof(*(element)) *a, *b; \
  30. (element)->next = NULL; \
  31. if (tab[_hash]) { \
  32. b = tab[_hash]; \
  33. for (; b; a = b, b = b->next); \
  34. a->next = (element); \
  35. } else \
  36. tab[_hash] = (element); \
  37. } while (0)
  38. #define hashtab_get(tab, element_name) \
  39. ({ \
  40. typeof(*(tab[0])) *_e; \
  41. for (_e = tab[conf_hash(element_name)]; \
  42. _e && strcmp(_e->name, (element_name)) != 0; \
  43. _e = _e->next); \
  44. _e; \
  45. })
  46. const char * config_get(struct config_file *f,
  47. const char *section,
  48. const char *item,
  49. const char *_default)
  50. {
  51. struct config_section *s;
  52. struct config_item *i;
  53. if (!f || !section || !item)
  54. return _default;
  55. s = hashtab_get(f->section_hashtab, section);
  56. if (s) {
  57. i = hashtab_get(s->item_hashtab, item);
  58. if (i)
  59. return i->value;
  60. }
  61. return _default;
  62. }
  63. static int string_to_int(const char *string, int *i)
  64. {
  65. char *tailptr;
  66. long retval;
  67. retval = strtol(string, &tailptr, 0);
  68. if (tailptr == string || tailptr[0] != '\0')
  69. return -EINVAL;
  70. *i = retval;
  71. return 0;
  72. }
  73. int config_get_int(struct config_file *f,
  74. const char *section,
  75. const char *item,
  76. int _default)
  77. {
  78. const char *value;
  79. int i;
  80. value = config_get(f, section, item, NULL);
  81. if (!value)
  82. return _default;
  83. if (string_to_int(value, &i))
  84. return _default;
  85. return i;
  86. }
  87. int config_get_bool(struct config_file *f,
  88. const char *section,
  89. const char *item,
  90. int _default)
  91. {
  92. const char *value;
  93. int i;
  94. value = config_get(f, section, item, NULL);
  95. if (!value)
  96. return _default;
  97. if (!string_to_int(value, &i))
  98. return !!i;
  99. if (strcasecmp(value, "yes") == 0 ||
  100. strcasecmp(value, "true") == 0 ||
  101. strcasecmp(value, "on") == 0)
  102. return 1;
  103. if (strcasecmp(value, "no") == 0 ||
  104. strcasecmp(value, "false") == 0 ||
  105. strcasecmp(value, "off") == 0)
  106. return 0;
  107. return _default;
  108. }
  109. static void dump_config_tree(struct config_file *f)
  110. {
  111. struct config_section *s;
  112. struct config_item *i;
  113. unsigned int j, k;
  114. int sect_coll, item_coll;
  115. const char *collstr = "\t\t<== ### COLLISION ###";
  116. logverbose("Parsed config file:\n");
  117. for (j = 0; j < CONF_HASHTAB_SIZE; j++) {
  118. sect_coll = 0;
  119. for (s = f->section_hashtab[j]; s; s = s->next) {
  120. logverbose("\t[%02X] Section '%s'%s\n",
  121. j & 0xFF, s->name,
  122. sect_coll ? collstr : "");
  123. for (k = 0; k < CONF_HASHTAB_SIZE; k++) {
  124. item_coll = 0;
  125. for (i = s->item_hashtab[k]; i; i = i->next) {
  126. logverbose("\t\t[%02X] Item '%s'%s\n",
  127. k & 0xFF, i->name,
  128. item_coll ? collstr : "");
  129. item_coll = 1;
  130. }
  131. }
  132. sect_coll = 1;
  133. }
  134. }
  135. }
  136. struct config_file * config_file_parse(const char *path)
  137. {
  138. struct config_file *retval = NULL, *c;
  139. struct config_section *s = NULL;
  140. struct config_item *i;
  141. struct fileaccess *fa = NULL;
  142. LIST_HEAD(lines);
  143. struct text_line *l;
  144. char *line, *name, *value;
  145. int err;
  146. size_t len;
  147. unsigned int lineno = 0;
  148. c = zalloc(sizeof(*c));
  149. if (!c)
  150. goto out_error;
  151. fa = file_open(O_RDONLY, path);
  152. if (!fa)
  153. goto out_ok;
  154. err = file_read_text_lines(fa, &lines, 1);
  155. if (err) {
  156. logerr("Failed to read config file %s: %s\n",
  157. path, strerror(-err));
  158. goto out_error;
  159. }
  160. list_for_each_entry(l, &lines, list) {
  161. lineno++;
  162. line = l->text;
  163. len = strlen(line);
  164. if (!len)
  165. continue;
  166. if (line[0] == '#') /* comment */
  167. continue;
  168. if (len >= 3 && line[0] == '[' && line[len - 1] == ']') {
  169. /* New section */
  170. s = zalloc(sizeof(*s));
  171. if (!s)
  172. goto error_unwind;
  173. s->file = c;
  174. line[len - 1] = '\0';
  175. s->name = strdup(line + 1);
  176. if (!s->name) {
  177. free(s);
  178. goto error_unwind;
  179. }
  180. hashtab_add(c->section_hashtab, s);
  181. continue;
  182. }
  183. if (!s) {
  184. logerr("%s:%u: Stray characters\n", path, lineno);
  185. goto error_unwind;
  186. }
  187. /* Config item in section */
  188. value = strchr(line, '=');
  189. if (!value) {
  190. logerr("%s:%u: Invalid config item\n", path, lineno);
  191. goto error_unwind;
  192. }
  193. value[0] = '\0';
  194. value++;
  195. name = line;
  196. i = zalloc(sizeof(*i));
  197. if (!i)
  198. goto error_unwind;
  199. i->section = s;
  200. i->name = strdup(name);
  201. if (!i->name) {
  202. free(i);
  203. goto error_unwind;
  204. }
  205. i->value = strdup(value);
  206. if (!i->value) {
  207. free(i->name);
  208. free(i);
  209. goto error_unwind;
  210. }
  211. hashtab_add(s->item_hashtab, i);
  212. }
  213. out_ok:
  214. retval = c;
  215. if (loglevel_is_verbose())
  216. dump_config_tree(retval);
  217. out_error:
  218. text_lines_free(&lines);
  219. file_close(fa);
  220. return retval;
  221. error_unwind:
  222. config_file_free(c);
  223. goto out_error;
  224. }
  225. static void free_item(struct config_item *i)
  226. {
  227. if (i) {
  228. free(i->name);
  229. free(i->value);
  230. free(i);
  231. }
  232. }
  233. static void free_section(struct config_section *s)
  234. {
  235. unsigned int i;
  236. struct config_item *it, *next;
  237. if (s) {
  238. for (i = 0; i < CONF_HASHTAB_SIZE; i++) {
  239. for (it = s->item_hashtab[i]; it; it = next) {
  240. next = it->next;
  241. free_item(it);
  242. }
  243. }
  244. free(s->name);
  245. free(s);
  246. }
  247. }
  248. void config_file_free(struct config_file *f)
  249. {
  250. unsigned int i;
  251. struct config_section *s, *next;
  252. if (f) {
  253. for (i = 0; i < CONF_HASHTAB_SIZE; i++) {
  254. for (s = f->section_hashtab[i]; s; s = next) {
  255. next = s->next;
  256. free_section(s);
  257. }
  258. }
  259. free(f);
  260. }
  261. }