ucimap.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * ucimap - library for mapping uci sections into data structures
  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 General Public License version 2
  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 __UCIMAP_H
  15. #define __UCIMAP_H
  16. #include <stdbool.h>
  17. #include "uci_list.h"
  18. #include "uci.h"
  19. #ifndef ARRAY_SIZE
  20. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  21. #endif
  22. #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
  23. #define CLR_BIT(_name, _bit) do { \
  24. _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
  25. } while (0)
  26. #define SET_BIT(_name, _bit) do { \
  27. _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
  28. } while (0)
  29. #define TEST_BIT(_name, _bit) \
  30. (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
  31. #ifndef __GNUC__
  32. #define __optmap_gen_type(_type, _field) -1
  33. #ifndef likely
  34. #define likely(_expr) !!(_expr)
  35. #endif
  36. #ifndef unlikely
  37. #define unlikely(_expr) !!(_expr)
  38. #endif
  39. #else /* __GNUC__ */
  40. #define __compatible(_type, _field, _newtype) \
  41. __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
  42. #define __list_compatible(_type, _field, __val, __else) \
  43. __builtin_choose_expr(__compatible(_type, _field, struct ucimap_list *), __val, __else)
  44. #define __int_compatible(_type, _field, __val, __else) \
  45. __builtin_choose_expr(__compatible(_type, _field, int), __val, \
  46. __builtin_choose_expr(__compatible(_type, _field, unsigned int), __val, \
  47. __else))
  48. #define __string_compatible(_type, _field, __val, __else) \
  49. __builtin_choose_expr(__compatible(_type, _field, char *), __val, \
  50. __builtin_choose_expr(__compatible(_type, _field, unsigned char *), __val, \
  51. __builtin_choose_expr(__compatible(_type, _field, const char *), __val, \
  52. __builtin_choose_expr(__compatible(_type, _field, const unsigned char *), __val, \
  53. __else))))
  54. #define __bool_compatible(_type, _field, __val, __else) \
  55. __builtin_choose_expr(__compatible(_type, _field, bool), __val, __else)
  56. #define __optmap_gen_type(_type, _field) \
  57. __list_compatible(_type, _field, UCIMAP_LIST, \
  58. __int_compatible(_type, _field, UCIMAP_INT, \
  59. __string_compatible(_type, _field, UCIMAP_STRING, \
  60. __bool_compatible(_type, _field, UCIMAP_BOOL, \
  61. -1))))
  62. #ifndef likely
  63. #define likely(x) __builtin_expect(!!(x), 1)
  64. #endif
  65. #ifndef unlikely
  66. #define unlikely(x) __builtin_expect(!!(x), 0)
  67. #endif
  68. #endif /* __GNUC__ */
  69. #define UCIMAP_OPTION(_type, _field) \
  70. .type = UCIMAP_CUSTOM, \
  71. .name = #_field, \
  72. .offset = offsetof(_type, _field), \
  73. .detected_type = __optmap_gen_type(_type, _field), \
  74. .type_name = #_type
  75. #define UCIMAP_SECTION(_name, _field) \
  76. .alloc_len = sizeof(_name), \
  77. .smap_offset = offsetof(_name, _field), \
  78. .type_name = #_name
  79. struct uci_sectionmap;
  80. struct uci_optmap;
  81. struct ucimap_list;
  82. struct uci_alloc;
  83. struct uci_alloc_custom;
  84. struct uci_map {
  85. struct uci_sectionmap **sections;
  86. unsigned int n_sections;
  87. struct list_head sdata;
  88. struct list_head fixup;
  89. struct list_head pending;
  90. bool parsed;
  91. void *priv; /* user data */
  92. };
  93. enum ucimap_type {
  94. /* types */
  95. UCIMAP_SIMPLE = 0x00,
  96. UCIMAP_LIST = 0x10,
  97. UCIMAP_TYPE = 0xf0, /* type mask */
  98. /* subtypes */
  99. UCIMAP_STRING = 0x0,
  100. UCIMAP_BOOL = 0x1,
  101. UCIMAP_INT = 0x2,
  102. UCIMAP_SECTION = 0x3,
  103. UCIMAP_CUSTOM = 0x4,
  104. UCIMAP_SUBTYPE = 0xf, /* subtype mask */
  105. /* automatically create lists from
  106. * options with space-separated items */
  107. UCIMAP_LIST_AUTO = 0x0100,
  108. UCIMAP_FLAGS = 0xff00, /* flags mask */
  109. };
  110. union ucimap_data {
  111. int i;
  112. bool b;
  113. char *s;
  114. void *ptr;
  115. void **data;
  116. struct ucimap_list *list;
  117. };
  118. struct ucimap_section_data {
  119. struct list_head list;
  120. struct uci_map *map;
  121. struct uci_sectionmap *sm;
  122. const char *section_name;
  123. /* list of allocations done by ucimap */
  124. struct uci_alloc *allocmap;
  125. struct uci_alloc_custom *alloc_custom;
  126. unsigned int allocmap_len;
  127. unsigned int alloc_custom_len;
  128. /* map for changed fields */
  129. unsigned char *cmap;
  130. bool done;
  131. };
  132. struct uci_sectionmap {
  133. /* type string for the uci section */
  134. const char *type;
  135. /* length of the struct to map into, filled in by macro */
  136. unsigned int alloc_len;
  137. /* sectionmap offset, filled in by macro */
  138. unsigned int smap_offset;
  139. /* return a pointer to the section map data (allocate if necessary) */
  140. struct ucimap_section_data *(*alloc)(struct uci_map *map,
  141. struct uci_sectionmap *sm, struct uci_section *s);
  142. /* give the caller time to initialize the preallocated struct */
  143. int (*init)(struct uci_map *map, void *section, struct uci_section *s);
  144. /* pass the fully processed struct to the callback after the section end */
  145. int (*add)(struct uci_map *map, void *section);
  146. /* let the callback clean up its own stuff in the section */
  147. int (*free)(struct uci_map *map, void *section);
  148. /* list of option mappings for this section */
  149. struct uci_optmap *options;
  150. unsigned int n_options;
  151. unsigned int options_size;
  152. /* internal */
  153. const char *type_name;
  154. };
  155. struct uci_optmap {
  156. unsigned int offset;
  157. const char *name;
  158. enum ucimap_type type;
  159. int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
  160. int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
  161. void (*free)(void *section, struct uci_optmap *om, void *ptr);
  162. union {
  163. struct {
  164. int base;
  165. int min;
  166. int max;
  167. } i;
  168. struct {
  169. int maxlen;
  170. } s;
  171. struct uci_sectionmap *sm;
  172. } data;
  173. /* internal */
  174. int detected_type;
  175. const char *type_name;
  176. };
  177. struct ucimap_list {
  178. int n_items;
  179. int size;
  180. union ucimap_data item[];
  181. };
  182. extern int ucimap_init(struct uci_map *map);
  183. extern void ucimap_cleanup(struct uci_map *map);
  184. extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
  185. extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd);
  186. extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
  187. extern int ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s);
  188. extern void ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd);
  189. extern int ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items);
  190. extern void ucimap_free_item(struct ucimap_section_data *sd, void *item);
  191. #endif