uci_internal.h 7.2 KB

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