uci_internal.h 7.3 KB

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