uci_internal.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Lesser General Public License for more details.
  13. */
  14. #ifndef __UCI_INTERNAL_H
  15. #define __UCI_INTERNAL_H
  16. #define __private __attribute__((visibility("hidden")))
  17. #define __public
  18. struct uci_parse_context
  19. {
  20. /* error context */
  21. const char *reason;
  22. int line;
  23. size_t byte;
  24. /* private: */
  25. struct uci_package *package;
  26. struct uci_section *section;
  27. bool merge;
  28. FILE *file;
  29. const char *name;
  30. char *buf;
  31. size_t bufsz;
  32. size_t buf_filled;
  33. size_t pos;
  34. };
  35. #define pctx_pos(pctx) ((pctx)->pos)
  36. #define pctx_str(pctx, i) (&(pctx)->buf[(i)])
  37. #define pctx_cur_str(pctx) pctx_str(pctx, pctx_pos(pctx))
  38. #define pctx_char(pctx, i) ((pctx)->buf[(i)])
  39. #define pctx_cur_char(pctx) pctx_char(pctx, pctx_pos(pctx))
  40. #define uci_alloc_element(ctx, type, name, datasize) \
  41. uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))
  42. extern const char *uci_confdir;
  43. extern const char *uci_savedir;
  44. __private void *uci_malloc(struct uci_context *ctx, size_t size);
  45. __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size);
  46. __private char *uci_strdup(struct uci_context *ctx, const char *str);
  47. __private bool uci_validate_str(const char *str, bool name, bool package);
  48. __private void uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value);
  49. __private void uci_free_delta(struct uci_delta *h);
  50. __private struct uci_package *uci_alloc_package(struct uci_context *ctx, const char *name);
  51. __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create);
  52. __private void uci_close_stream(FILE *stream);
  53. __private void uci_getln(struct uci_context *ctx, size_t offset);
  54. __private void uci_parse_error(struct uci_context *ctx, char *reason);
  55. __private void uci_alloc_parse_context(struct uci_context *ctx);
  56. __private void uci_cleanup(struct uci_context *ctx);
  57. __private struct uci_element *uci_lookup_list(struct uci_list *list, const char *name);
  58. __private void uci_free_package(struct uci_package **package);
  59. __private struct uci_element *uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size);
  60. __private void uci_free_element(struct uci_element *e);
  61. __private struct uci_element *uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete);
  62. __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush);
  63. static inline bool uci_validate_package(const char *str)
  64. {
  65. return uci_validate_str(str, false, true);
  66. }
  67. static inline bool uci_validate_type(const char *str)
  68. {
  69. return uci_validate_str(str, false, false);
  70. }
  71. static inline bool uci_validate_name(const char *str)
  72. {
  73. return uci_validate_str(str, true, false);
  74. }
  75. /* initialize a list head/item */
  76. static inline void uci_list_init(struct uci_list *ptr)
  77. {
  78. ptr->prev = ptr;
  79. ptr->next = ptr;
  80. }
  81. /* inserts a new list entry after a given entry */
  82. static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
  83. {
  84. list->next->prev = ptr;
  85. ptr->prev = list;
  86. ptr->next = list->next;
  87. list->next = ptr;
  88. }
  89. /* inserts a new list entry at the tail of the list */
  90. static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
  91. {
  92. /* NB: head->prev points at the tail */
  93. uci_list_insert(head->prev, ptr);
  94. }
  95. static inline void uci_list_del(struct uci_list *ptr)
  96. {
  97. struct uci_list *next, *prev;
  98. next = ptr->next;
  99. prev = ptr->prev;
  100. prev->next = next;
  101. next->prev = prev;
  102. uci_list_init(ptr);
  103. }
  104. extern struct uci_backend uci_file_backend;
  105. #ifdef UCI_PLUGIN_SUPPORT
  106. /**
  107. * uci_add_backend: add an extra backend
  108. * @ctx: uci context
  109. * @name: name of the backend
  110. *
  111. * The default backend is "file", which uses /etc/config for config storage
  112. */
  113. __private int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
  114. /**
  115. * uci_add_backend: add an extra backend
  116. * @ctx: uci context
  117. * @name: name of the backend
  118. *
  119. * The default backend is "file", which uses /etc/config for config storage
  120. */
  121. __private int uci_del_backend(struct uci_context *ctx, struct uci_backend *b);
  122. #endif
  123. #define UCI_BACKEND(_var, _name, ...) \
  124. struct uci_backend _var = { \
  125. .e.list = { \
  126. .next = &_var.e.list, \
  127. .prev = &_var.e.list, \
  128. }, \
  129. .e.name = _name, \
  130. .e.type = UCI_TYPE_BACKEND, \
  131. .ptr = &_var, \
  132. __VA_ARGS__ \
  133. }
  134. /*
  135. * functions for debug and error handling, for internal use only
  136. */
  137. #ifdef UCI_DEBUG
  138. #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
  139. #else
  140. #define DPRINTF(...)
  141. #endif
  142. /*
  143. * throw an uci exception and store the error number
  144. * in the context.
  145. */
  146. #define UCI_THROW(ctx, err) do { \
  147. DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
  148. longjmp(ctx->trap, err); \
  149. } while (0)
  150. /*
  151. * store the return address for handling exceptions
  152. * needs to be called in every externally visible library function
  153. *
  154. * NB: this does not handle recursion at all. Calling externally visible
  155. * functions from other uci functions is only allowed at the end of the
  156. * calling function, or by wrapping the function call in UCI_TRAP_SAVE
  157. * and UCI_TRAP_RESTORE.
  158. */
  159. #define UCI_HANDLE_ERR(ctx) do { \
  160. DPRINTF("ENTER: %s\n", __func__); \
  161. int __val = 0; \
  162. if (!ctx) \
  163. return UCI_ERR_INVAL; \
  164. ctx->err = 0; \
  165. if (!ctx->internal && !ctx->nested) \
  166. __val = setjmp(ctx->trap); \
  167. ctx->internal = false; \
  168. ctx->nested = false; \
  169. if (__val) { \
  170. DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
  171. ctx->err = __val; \
  172. return __val; \
  173. } \
  174. } while (0)
  175. /*
  176. * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
  177. * are intercepted and redirected to the label specified in 'handler'
  178. * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
  179. * exception handler is restored
  180. */
  181. #define UCI_TRAP_SAVE(ctx, handler) do { \
  182. jmp_buf __old_trap; \
  183. int __val; \
  184. memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
  185. __val = setjmp(ctx->trap); \
  186. if (__val) { \
  187. ctx->err = __val; \
  188. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  189. goto handler; \
  190. } while(0)
  191. #define UCI_TRAP_RESTORE(ctx) \
  192. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  193. } while(0)
  194. /**
  195. * UCI_INTERNAL: Do an internal call of a public API function
  196. *
  197. * Sets Exception handling to passthrough mode.
  198. * Allows API functions to change behavior compared to public use
  199. */
  200. #define UCI_INTERNAL(func, ctx, ...) do { \
  201. ctx->internal = true; \
  202. func(ctx, __VA_ARGS__); \
  203. } while (0)
  204. /**
  205. * UCI_NESTED: Do an normal nested call of a public API function
  206. *
  207. * Sets Exception handling to passthrough mode.
  208. * Allows API functions to change behavior compared to public use
  209. */
  210. #define UCI_NESTED(func, ctx, ...) do { \
  211. ctx->nested = true; \
  212. func(ctx, __VA_ARGS__); \
  213. } while (0)
  214. /*
  215. * check the specified condition.
  216. * throw an invalid argument exception if it's false
  217. */
  218. #define UCI_ASSERT(ctx, expr) do { \
  219. if (!(expr)) { \
  220. DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
  221. UCI_THROW(ctx, UCI_ERR_INVAL); \
  222. } \
  223. } while (0)
  224. #endif