ucimap.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * ucimap.h - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008-2009 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 Lesser General Public License for more details.
  13. */
  14. /*
  15. * This file contains ucimap, an API for mapping UCI to C data structures
  16. */
  17. #ifndef __UCIMAP_H
  18. #define __UCIMAP_H
  19. #include <stdbool.h>
  20. #include "uci.h"
  21. #ifndef ARRAY_SIZE
  22. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  23. #endif
  24. #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
  25. #define CLR_BIT(_name, _bit) do { \
  26. _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
  27. } while (0)
  28. #define SET_BIT(_name, _bit) do { \
  29. _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
  30. } while (0)
  31. #define TEST_BIT(_name, _bit) \
  32. (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
  33. #ifndef __GNUC__
  34. #define __optmap_gen_type(_type, _field) -1
  35. #ifndef likely
  36. #define likely(_expr) !!(_expr)
  37. #endif
  38. #ifndef unlikely
  39. #define unlikely(_expr) !!(_expr)
  40. #endif
  41. #else /* __GNUC__ */
  42. #define __compatible(_type, _field, _newtype) \
  43. __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
  44. #define __list_compatible(_type, _field, __val, __else) \
  45. __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
  46. #define __int_compatible(_type, _field, __val, __else) \
  47. __builtin_choose_expr(__compatible(_type, _field, int), __val, \
  48. __builtin_choose_expr(__compatible(_type, _field, unsigned int), __val, \
  49. __else))
  50. #define __string_compatible(_type, _field, __val, __else) \
  51. __builtin_choose_expr(__compatible(_type, _field, char *), __val, \
  52. __builtin_choose_expr(__compatible(_type, _field, unsigned char *), __val, \
  53. __builtin_choose_expr(__compatible(_type, _field, const char *), __val, \
  54. __builtin_choose_expr(__compatible(_type, _field, const unsigned char *), __val, \
  55. __else))))
  56. #define __bool_compatible(_type, _field, __val, __else) \
  57. __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
  58. #define __optmap_gen_type(_type, _field) \
  59. __list_compatible(_type, _field, UCIMAP_LIST, \
  60. __int_compatible(_type, _field, UCIMAP_INT, \
  61. __string_compatible(_type, _field, UCIMAP_STRING, \
  62. __bool_compatible(_type, _field, UCIMAP_BOOL, \
  63. -1))))
  64. #ifndef likely
  65. #define likely(x) __builtin_expect(!!(x), 1)
  66. #endif
  67. #ifndef unlikely
  68. #define unlikely(x) __builtin_expect(!!(x), 0)
  69. #endif
  70. #endif /* __GNUC__ */
  71. #define UCIMAP_OPTION(_type, _field) \
  72. .name = #_field, \
  73. .offset = offsetof(_type, _field), \
  74. .detected_type = __optmap_gen_type(_type, _field), \
  75. .type_name = #_type
  76. #define UCIMAP_SECTION(_name, _field) \
  77. .alloc_len = sizeof(_name), \
  78. .smap_offset = offsetof(_name, _field), \
  79. .type_name = #_name
  80. struct uci_sectionmap;
  81. struct uci_optmap;
  82. struct ucimap_list;
  83. struct ucimap_fixup;
  84. struct ucimap_alloc;
  85. struct ucimap_alloc_custom;
  86. struct ucimap_section_data;
  87. struct uci_map {
  88. struct uci_sectionmap **sections;
  89. unsigned int n_sections;
  90. bool parsed;
  91. void *priv;
  92. /* private */
  93. struct ucimap_fixup *fixup;
  94. struct ucimap_fixup **fixup_tail;
  95. struct ucimap_section_data *sdata;
  96. struct ucimap_section_data *pending;
  97. struct ucimap_section_data **sdata_tail;
  98. };
  99. enum ucimap_type {
  100. /* types */
  101. UCIMAP_SIMPLE = 0x00,
  102. UCIMAP_LIST = 0x10,
  103. UCIMAP_TYPE = 0xf0, /* type mask */
  104. /* subtypes */
  105. UCIMAP_CUSTOM = 0x0,
  106. UCIMAP_STRING = 0x1,
  107. UCIMAP_BOOL = 0x2,
  108. UCIMAP_INT = 0x3,
  109. UCIMAP_SECTION = 0x4,
  110. UCIMAP_SUBTYPE = 0xf, /* subtype mask */
  111. /* automatically create lists from
  112. * options with space-separated items */
  113. UCIMAP_LIST_AUTO = 0x0100,
  114. UCIMAP_FLAGS = 0xff00, /* flags mask */
  115. };
  116. union ucimap_data {
  117. int i;
  118. bool b;
  119. char *s;
  120. void *ptr;
  121. void **data;
  122. struct ucimap_list *list;
  123. };
  124. struct ucimap_section_data {
  125. struct uci_map *map;
  126. struct uci_sectionmap *sm;
  127. const char *section_name;
  128. /* map for changed fields */
  129. unsigned char *cmap;
  130. bool done;
  131. /* internal */
  132. struct ucimap_section_data *next, **ref;
  133. struct ucimap_alloc *allocmap;
  134. struct ucimap_alloc_custom *alloc_custom;
  135. unsigned int allocmap_len;
  136. unsigned int alloc_custom_len;
  137. };
  138. struct uci_sectionmap {
  139. /* type string for the uci section */
  140. const char *type;
  141. /* length of the struct to map into, filled in by macro */
  142. unsigned int alloc_len;
  143. /* sectionmap offset, filled in by macro */
  144. unsigned int smap_offset;
  145. /* return a pointer to the section map data (allocate if necessary) */
  146. struct ucimap_section_data *(*alloc)(struct uci_map *map,
  147. struct uci_sectionmap *sm, struct uci_section *s);
  148. /* give the caller time to initialize the preallocated struct */
  149. int (*init)(struct uci_map *map, void *section, struct uci_section *s);
  150. /* pass the fully processed struct to the callback after the section end */
  151. int (*add)(struct uci_map *map, void *section);
  152. /* let the callback clean up its own stuff in the section */
  153. int (*free)(struct uci_map *map, void *section);
  154. /* list of option mappings for this section */
  155. struct uci_optmap *options;
  156. unsigned int n_options;
  157. unsigned int options_size;
  158. /* internal */
  159. const char *type_name;
  160. };
  161. struct uci_optmap {
  162. unsigned int offset;
  163. const char *name;
  164. enum ucimap_type type;
  165. int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
  166. int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
  167. void (*free)(void *section, struct uci_optmap *om, void *ptr);
  168. union {
  169. struct {
  170. int base;
  171. int min;
  172. int max;
  173. } i;
  174. struct {
  175. int maxlen;
  176. } s;
  177. struct uci_sectionmap *sm;
  178. } data;
  179. /* internal */
  180. int detected_type;
  181. const char *type_name;
  182. };
  183. struct ucimap_list {
  184. int n_items;
  185. int size;
  186. union ucimap_data item[];
  187. };
  188. /**
  189. * ucimap_init: initialize the ucimap data structure
  190. * @map: ucimap data structure
  191. *
  192. * you must call this function before doing any other ucimap operation
  193. * on the data structure
  194. */
  195. extern int ucimap_init(struct uci_map *map);
  196. /**
  197. * ucimap_cleanup: clean up all allocated data from ucimap
  198. * @map: ucimap data structure
  199. */
  200. extern void ucimap_cleanup(struct uci_map *map);
  201. /**
  202. * ucimap_parse: parse all sections in an uci package using ucimap
  203. * @map: ucimap data structure
  204. * @pkg: uci package
  205. */
  206. extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
  207. /**
  208. * ucimap_set_changed: mark a field in a custom data structure as changed
  209. * @sd: pointer to the ucimap section data
  210. * @field: pointer to the field inside the custom data structure
  211. *
  212. * @sd must be set to the section data inside the data structure that contains @field
  213. */
  214. extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
  215. /**
  216. * ucimap_store_section: copy all changed data from the converted data structure to uci
  217. * @map: ucimap data structure
  218. * @p: uci package to store the changes in
  219. * @sd: pointer to the ucimap section data
  220. *
  221. * changes are not saved or committed automatically
  222. */
  223. extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
  224. /**
  225. * ucimap_parse_section: parse a single section
  226. * @map: ucimap data structure
  227. * @sm: uci section map
  228. * @sd: pointer to the ucimap section data
  229. * @s: pointer to the uci section
  230. *
  231. * this function overwrites the ucimap section data, do not use on a section
  232. * that has been parsed already
  233. */
  234. extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
  235. /**
  236. * ucimap_free_section: free a data structure for a converted section
  237. * @map: ucimap data structure
  238. * @sd: pointer to the ucimap section data
  239. *
  240. * this function will clean up all data that was allocated by ucimap for this section.
  241. * all references to the data structure become invalid
  242. */
  243. extern void ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd);
  244. /**
  245. * ucimap_resize_list: allocate or resize a uci list
  246. * @sd: pointer to the ucimap section data
  247. * @list: pointer to the list field
  248. * @items: new size
  249. *
  250. * @sd must point to the data structure that contains @list.
  251. * @list must point to the field containing a pointer to the list, not the list directly
  252. * the memory allocated for this list is tracked for the section and freed automatically
  253. */
  254. extern int ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items);
  255. /**
  256. * ucimap_free_item: free the allocated memory for a data structure member
  257. * @sd: pointer to the ucimap section data
  258. * @item: pointer to the field inside the data structure
  259. *
  260. * @sd must point to the data structure that contains @item.
  261. * @item must point to the field containing a pointer to the allocated item
  262. */
  263. extern void ucimap_free_item(struct ucimap_section_data *sd, void *item);
  264. #endif