uci.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * this program is free software; you can redistribute it and/or modify
  6. * it under the terms of the gnu lesser general public license version 2.1
  7. * as published by the free software foundation
  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. #ifndef __LIBUCI_H
  15. #define __LIBUCI_H
  16. /*
  17. * you can use these defines to enable debugging behavior for
  18. * apps compiled against libuci:
  19. *
  20. * #define UCI_DEBUG_TYPECAST:
  21. * enable uci_element typecast checking at run time
  22. *
  23. */
  24. #ifdef DEBUG_ALL
  25. #define UCI_DEBUG
  26. #define UCI_DEBUG_TYPECAST
  27. #endif
  28. #include <setjmp.h>
  29. #include <stdio.h>
  30. #define UCI_CONFDIR "/etc/config"
  31. enum
  32. {
  33. UCI_OK = 0,
  34. UCI_ERR_MEM,
  35. UCI_ERR_INVAL,
  36. UCI_ERR_NOTFOUND,
  37. UCI_ERR_IO,
  38. UCI_ERR_PARSE,
  39. UCI_ERR_UNKNOWN,
  40. UCI_ERR_LAST
  41. };
  42. struct uci_list;
  43. struct uci_list
  44. {
  45. struct uci_list *next;
  46. struct uci_list *prev;
  47. };
  48. struct uci_element;
  49. struct uci_package;
  50. struct uci_section;
  51. struct uci_option;
  52. struct uci_history;
  53. struct uci_parse_context;
  54. /**
  55. * uci_alloc_context: Allocate a new uci context
  56. */
  57. extern struct uci_context *uci_alloc_context(void);
  58. /**
  59. * uci_free_context: Free the uci context including all of its data
  60. */
  61. extern void uci_free_context(struct uci_context *ctx);
  62. /**
  63. * uci_perror: Print the last uci error that occured
  64. * @ctx: uci context
  65. * @str: string to print before the error message
  66. */
  67. extern void uci_perror(struct uci_context *ctx, const char *str);
  68. /**
  69. * uci_import: Import uci config data from a stream
  70. * @ctx: uci context
  71. * @stream: file stream to import from
  72. * @name: (optional) assume the config has the given name
  73. * @package: (optional) store the last parsed config package in this variable
  74. *
  75. * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
  76. */
  77. extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package);
  78. /**
  79. * uci_export: Export one or all uci config packages
  80. * @ctx: uci context
  81. * @stream: output stream
  82. * @package: (optional) uci config package to export
  83. */
  84. extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package);
  85. /**
  86. * uci_load: Parse an uci config file and store it in the uci context
  87. *
  88. * @ctx: uci context
  89. * @name: name of the config file (relative to the config directory)
  90. * @package: store the loaded config package in this variable
  91. */
  92. extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);
  93. /**
  94. * uci_unload: Unload a config file from the uci context
  95. *
  96. * @ctx: uci context
  97. * @name: name of the config file
  98. */
  99. extern int uci_unload(struct uci_context *ctx, const char *name);
  100. /**
  101. * uci_cleanup: Clean up after an error
  102. *
  103. * @ctx: uci context
  104. */
  105. extern int uci_cleanup(struct uci_context *ctx);
  106. /**
  107. * uci_lookup: Look up an uci element
  108. *
  109. * @ctx: uci context
  110. * @res: where to store the result
  111. * @package: config package
  112. * @section: config section (optional)
  113. * @option: option to search for (optional)
  114. *
  115. * If section is omitted, then a pointer to the config package is returned
  116. * If option is omitted, then a pointer to the config section is returned
  117. */
  118. extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option);
  119. /**
  120. * uci_list_configs: List available uci config files
  121. *
  122. * @ctx: uci context
  123. */
  124. extern char **uci_list_configs(struct uci_context *ctx);
  125. /* UCI data structures */
  126. enum uci_type {
  127. UCI_TYPE_PACKAGE = 0,
  128. UCI_TYPE_SECTION = 1,
  129. UCI_TYPE_OPTION = 2
  130. };
  131. struct uci_element
  132. {
  133. struct uci_list list;
  134. enum uci_type type;
  135. char *name;
  136. };
  137. struct uci_context
  138. {
  139. /* list of config packages */
  140. struct uci_list root;
  141. /* parser context, use for error handling only */
  142. struct uci_parse_context *pctx;
  143. /* private: */
  144. int errno;
  145. jmp_buf trap;
  146. char *buf;
  147. int bufsz;
  148. };
  149. struct uci_parse_context
  150. {
  151. /* error context */
  152. const char *reason;
  153. int line;
  154. int byte;
  155. /* private: */
  156. struct uci_package *package;
  157. struct uci_section *section;
  158. FILE *file;
  159. const char *name;
  160. char *buf;
  161. int bufsz;
  162. };
  163. struct uci_package
  164. {
  165. struct uci_element e;
  166. struct uci_list sections;
  167. struct uci_context *ctx;
  168. /* private: */
  169. int n_section;
  170. };
  171. struct uci_section
  172. {
  173. struct uci_element e;
  174. struct uci_list options;
  175. struct uci_package *package;
  176. char *type;
  177. };
  178. struct uci_option
  179. {
  180. struct uci_element e;
  181. struct uci_section *section;
  182. char *value;
  183. };
  184. enum uci_command {
  185. UCI_CMD_ADD,
  186. UCI_CMD_REMOVE,
  187. UCI_CMD_CHANGE
  188. };
  189. struct uci_history
  190. {
  191. struct uci_list list;
  192. enum uci_command cmd;
  193. union {
  194. struct uci_element element;
  195. struct uci_package package;
  196. struct uci_section section;
  197. struct uci_option option;
  198. } data;
  199. };
  200. /* linked list handling */
  201. #ifndef offsetof
  202. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  203. #endif
  204. /**
  205. * container_of - cast a member of a structure out to the containing structure
  206. * @ptr: the pointer to the member.
  207. * @type: the type of the container struct this is embedded in.
  208. * @member: the name of the member within the struct.
  209. */
  210. #define container_of(ptr, type, member) \
  211. ((type *) ((char *)ptr - offsetof(type,member)))
  212. /**
  213. * uci_list_entry: casts an uci_list pointer to the containing struct.
  214. * @_type: config, section or option
  215. * @_ptr: pointer to the uci_list struct
  216. */
  217. #define element_to(type, ptr) \
  218. container_of(ptr, struct uci_ ## type, e)
  219. #define list_to_element(ptr) \
  220. container_of(ptr, struct uci_element, list)
  221. /**
  222. * uci_foreach_entry: loop through a list of uci elements
  223. * @_list: pointer to the uci_list struct
  224. * @_ptr: iteration variable, struct uci_element
  225. *
  226. * use like a for loop, e.g:
  227. * uci_foreach(&list, p) {
  228. * ...
  229. * }
  230. */
  231. #define uci_foreach_element(_list, _ptr) \
  232. for(_ptr = list_to_element((_list)->next); \
  233. &_ptr->list != (_list); \
  234. _ptr = list_to_element(_ptr->list.next))
  235. /**
  236. * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
  237. * @_list: pointer to the uci_list struct
  238. * @_tmp: temporary variable, struct uci_element *
  239. * @_ptr: iteration variable, struct uci_element *
  240. *
  241. * use like a for loop, e.g:
  242. * uci_foreach(&list, p) {
  243. * ...
  244. * }
  245. */
  246. #define uci_foreach_element_safe(_list, _tmp, _ptr) \
  247. for(_ptr = list_to_element((_list)->next), \
  248. _tmp = list_to_element(_ptr->list.next); \
  249. &_ptr->list != (_list); \
  250. _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))
  251. /* returns true if a list is empty */
  252. #define uci_list_empty(list) ((list)->next == (list))
  253. /* wrappers for dynamic type handling */
  254. #define uci_type_package UCI_TYPE_PACKAGE
  255. #define uci_type_section UCI_TYPE_SECTION
  256. #define uci_type_option UCI_TYPE_OPTION
  257. /* element typecasting */
  258. #ifdef UCI_DEBUG_TYPECAST
  259. static const char *uci_typestr[] = {
  260. [uci_type_package] = "package",
  261. [uci_type_section] = "section",
  262. [uci_type_option] = "option"
  263. };
  264. static void uci_typecast_error(int from, int to)
  265. {
  266. fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
  267. }
  268. #define BUILD_CAST(_type) \
  269. static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
  270. { \
  271. if (e->type != uci_type_ ## _type) { \
  272. uci_typecast_error(e->type, uci_type_ ## _type); \
  273. } \
  274. return (struct uci_ ## _type *) e; \
  275. }
  276. BUILD_CAST(package)
  277. BUILD_CAST(section)
  278. BUILD_CAST(option)
  279. #else
  280. #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
  281. #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
  282. #define uci_to_option(ptr) container_of(ptr, struct uci_option, e)
  283. #endif
  284. /**
  285. * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
  286. * @ctx: uci context
  287. * @type: {package,section,option}
  288. * @name: string containing the name of the element
  289. * @datasize: additional buffer size to reserve at the end of the struct
  290. */
  291. #define uci_alloc_element(ctx, type, name, datasize) \
  292. uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
  293. #define uci_dataptr(ptr) \
  294. (((char *) ptr) + sizeof(*ptr))
  295. #endif