config.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "config.h"
  15. #include "razer_private.h"
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <ctype.h>
  21. static inline int strcmp_case(const char *a, const char *b, bool ignorecase)
  22. {
  23. if (ignorecase)
  24. return strcasecmp(a, b);
  25. return strcmp(a, b);
  26. }
  27. static void free_item(struct config_item *i)
  28. {
  29. if (i) {
  30. free(i->name);
  31. free(i->value);
  32. free(i);
  33. }
  34. }
  35. static void free_items(struct config_item *i)
  36. {
  37. struct config_item *next;
  38. for ( ; i; i = next) {
  39. next = i->next;
  40. free_item(i);
  41. }
  42. }
  43. static void free_section(struct config_section *s)
  44. {
  45. if (s) {
  46. free_items(s->items);
  47. free(s->name);
  48. free(s);
  49. }
  50. }
  51. static void free_sections(struct config_section *s)
  52. {
  53. struct config_section *next;
  54. for ( ; s; s = next) {
  55. next = s->next;
  56. free_section(s);
  57. }
  58. }
  59. void config_for_each_item(struct config_file *f,
  60. void *context, void *data,
  61. const char *section,
  62. bool (*func)(struct config_file *f,
  63. void *context, void *data,
  64. const char *section,
  65. const char *item,
  66. const char *value))
  67. {
  68. struct config_section *s;
  69. struct config_item *i;
  70. if (!f || !section)
  71. return;
  72. for (s = f->sections; s; s = s->next) {
  73. if (strcmp(s->name, section) == 0) {
  74. for (i = s->items; i; i = i->next) {
  75. if (!func(f, context, data, s->name, i->name, i->value))
  76. return;
  77. }
  78. }
  79. }
  80. }
  81. void config_for_each_section(struct config_file *f,
  82. void *context, void *data,
  83. bool (*func)(struct config_file *f,
  84. void *context, void *data,
  85. const char *section))
  86. {
  87. struct config_section *s;
  88. if (!f)
  89. return;
  90. for (s = f->sections; s; s = s->next) {
  91. if (!func(f, context, data, s->name))
  92. return;
  93. }
  94. }
  95. const char * config_get(struct config_file *f,
  96. const char *section,
  97. const char *item,
  98. const char *_default,
  99. unsigned int flags)
  100. {
  101. struct config_section *s;
  102. struct config_item *i;
  103. const char *retval = _default;
  104. if (!f || !section || !item)
  105. return _default;
  106. for (s = f->sections; s; s = s->next) {
  107. if (strcmp_case(s->name, section, !!(flags & CONF_SECT_NOCASE)) == 0) {
  108. for (i = s->items; i; i = i->next) {
  109. if (strcmp_case(i->name, item, !!(flags & CONF_ITEM_NOCASE)) == 0) {
  110. retval = i->value;
  111. break;
  112. }
  113. }
  114. break;
  115. }
  116. }
  117. return retval;
  118. }
  119. int config_get_int(struct config_file *f,
  120. const char *section,
  121. const char *item,
  122. int _default,
  123. unsigned int flags)
  124. {
  125. const char *value;
  126. int i;
  127. value = config_get(f, section, item, NULL, flags);
  128. if (!value)
  129. return _default;
  130. if (razer_string_to_int(value, &i))
  131. return _default;
  132. return i;
  133. }
  134. int config_get_bool(struct config_file *f,
  135. const char *section,
  136. const char *item,
  137. int _default,
  138. unsigned int flags)
  139. {
  140. const char *value;
  141. bool b;
  142. value = config_get(f, section, item, NULL, flags);
  143. if (!value)
  144. return _default;
  145. if (razer_string_to_bool(value, &b))
  146. return _default;
  147. return b;
  148. }
  149. #define list_append(container, baseptr, item) do { \
  150. __typeof__(item) last; \
  151. item->next = NULL; \
  152. if (!(container->baseptr)) { \
  153. container->baseptr = item; \
  154. return; \
  155. } \
  156. for (last = container->baseptr; \
  157. last->next; \
  158. last = last->next) \
  159. ; \
  160. last->next = item; \
  161. } while (0)
  162. static void append_section(struct config_file *f, struct config_section *s)
  163. {
  164. list_append(f, sections, s);
  165. }
  166. static void append_item(struct config_section *s, struct config_item *i)
  167. {
  168. list_append(s, items, i);
  169. }
  170. struct config_file * config_file_parse(const char *path, bool ignore_enoent)
  171. {
  172. struct config_file *f;
  173. struct config_section *s = NULL;
  174. struct config_item *i;
  175. FILE *fd;
  176. char *name, *value;
  177. size_t len;
  178. unsigned int lineno = 0;
  179. ssize_t count;
  180. size_t linebuf_size = 0;
  181. char *linebuf = NULL, *line;
  182. f = zalloc(sizeof(*f));
  183. if (!f)
  184. goto error;
  185. f->path = strdup(path);
  186. if (!f->path)
  187. goto err_free_f;
  188. fd = fopen(path, "rb");
  189. if (!fd) {
  190. if (errno == ENOENT && ignore_enoent) {
  191. razer_info("No config file %s present. Ignoring.\n",
  192. path);
  193. } else {
  194. razer_error("Failed to open config file %s: %s\n",
  195. path, strerror(errno));
  196. }
  197. goto err_free_path;
  198. }
  199. while (1) {
  200. count = getline(&linebuf, &linebuf_size, fd);
  201. if (count <= 0)
  202. break;
  203. line = razer_string_strip(linebuf);
  204. lineno++;
  205. len = strlen(line);
  206. if (!len)
  207. continue;
  208. if (line[0] == '#') /* comment */
  209. continue;
  210. if (len >= 3 && line[0] == '[' && line[len - 1] == ']') {
  211. /* New section */
  212. s = zalloc(sizeof(*s));
  213. if (!s)
  214. goto error_unwind;
  215. s->file = f;
  216. line[len - 1] = '\0'; /* strip ] */
  217. s->name = strdup(line + 1); /* strip [ */
  218. if (!s->name) {
  219. free(s);
  220. goto error_unwind;
  221. }
  222. append_section(f, s);
  223. continue;
  224. }
  225. if (!s) {
  226. razer_error("%s:%u: Stray characters\n", path, lineno);
  227. goto error_unwind;
  228. }
  229. /* Config item in section */
  230. value = strchr(line, '=');
  231. if (!value) {
  232. razer_error("%s:%u: Invalid config item\n", path, lineno);
  233. goto error_unwind;
  234. }
  235. value[0] = '\0';
  236. value++;
  237. name = line;
  238. i = zalloc(sizeof(*i));
  239. if (!i)
  240. goto error_unwind;
  241. i->section = s;
  242. i->name = strdup(name);
  243. if (!i->name) {
  244. free(i);
  245. goto error_unwind;
  246. }
  247. i->value = strdup(value);
  248. if (!i->value) {
  249. free(i->name);
  250. free(i);
  251. goto error_unwind;
  252. }
  253. append_item(s, i);
  254. }
  255. free(linebuf);
  256. fclose(fd);
  257. return f;
  258. error_unwind:
  259. free_sections(f->sections);
  260. free(linebuf);
  261. fclose(fd);
  262. err_free_path:
  263. free(f->path);
  264. err_free_f:
  265. free(f);
  266. error:
  267. return NULL;
  268. }
  269. void config_file_free(struct config_file *f)
  270. {
  271. if (f) {
  272. free_sections(f->sections);
  273. free(f->path);
  274. free(f);
  275. }
  276. }