uci_internal.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 General Public License for more details.
  13. */
  14. #ifndef __UCI_INTERNAL_H
  15. #define __UCI_INTERNAL_H
  16. #define __public
  17. #ifdef UCI_PLUGIN_SUPPORT
  18. #define __plugin extern
  19. #else
  20. #define __plugin static
  21. #endif
  22. struct uci_parse_context
  23. {
  24. /* error context */
  25. const char *reason;
  26. int line;
  27. int byte;
  28. /* private: */
  29. struct uci_package *package;
  30. struct uci_section *section;
  31. bool merge;
  32. FILE *file;
  33. const char *name;
  34. char *buf;
  35. int bufsz;
  36. };
  37. __plugin void *uci_malloc(struct uci_context *ctx, size_t size);
  38. __plugin void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size);
  39. __plugin char *uci_strdup(struct uci_context *ctx, const char *str);
  40. __plugin bool uci_validate_str(const char *str, bool name);
  41. __plugin void uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value);
  42. __plugin void uci_free_history(struct uci_history *h);
  43. __plugin struct uci_package *uci_alloc_package(struct uci_context *ctx, const char *name);
  44. #ifdef UCI_PLUGIN_SUPPORT
  45. /**
  46. * uci_add_backend: add an extra backend
  47. * @ctx: uci context
  48. * @name: name of the backend
  49. *
  50. * The default backend is "file", which uses /etc/config for config storage
  51. */
  52. __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
  53. /**
  54. * uci_add_backend: add an extra backend
  55. * @ctx: uci context
  56. * @name: name of the backend
  57. *
  58. * The default backend is "file", which uses /etc/config for config storage
  59. */
  60. __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b);
  61. #endif
  62. #define UCI_BACKEND(_var, _name, ...) \
  63. struct uci_backend _var = { \
  64. .e.list = { \
  65. .next = &_var.e.list, \
  66. .prev = &_var.e.list, \
  67. }, \
  68. .e.name = _name, \
  69. .e.type = UCI_TYPE_BACKEND, \
  70. .ptr = &_var, \
  71. __VA_ARGS__ \
  72. }
  73. /*
  74. * functions for debug and error handling, for internal use only
  75. */
  76. #ifdef UCI_DEBUG
  77. #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
  78. #else
  79. #define DPRINTF(...)
  80. #endif
  81. /*
  82. * throw an uci exception and store the error number
  83. * in the context.
  84. */
  85. #define UCI_THROW(ctx, err) do { \
  86. DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
  87. longjmp(ctx->trap, err); \
  88. } while (0)
  89. /*
  90. * store the return address for handling exceptions
  91. * needs to be called in every externally visible library function
  92. *
  93. * NB: this does not handle recursion at all. Calling externally visible
  94. * functions from other uci functions is only allowed at the end of the
  95. * calling function, or by wrapping the function call in UCI_TRAP_SAVE
  96. * and UCI_TRAP_RESTORE.
  97. */
  98. #define UCI_HANDLE_ERR(ctx) do { \
  99. DPRINTF("ENTER: %s\n", __func__); \
  100. int __val = 0; \
  101. ctx->errno = 0; \
  102. if (!ctx) \
  103. return UCI_ERR_INVAL; \
  104. if (!ctx->internal) \
  105. __val = setjmp(ctx->trap); \
  106. ctx->internal = false; \
  107. if (__val) { \
  108. DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
  109. ctx->errno = __val; \
  110. return __val; \
  111. } \
  112. } while (0)
  113. /*
  114. * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
  115. * are intercepted and redirected to the label specified in 'handler'
  116. * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
  117. * exception handler is restored
  118. */
  119. #define UCI_TRAP_SAVE(ctx, handler) do { \
  120. jmp_buf __old_trap; \
  121. int __val; \
  122. memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
  123. __val = setjmp(ctx->trap); \
  124. if (__val) { \
  125. ctx->errno = __val; \
  126. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  127. goto handler; \
  128. }
  129. #define UCI_TRAP_RESTORE(ctx) \
  130. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  131. } while(0)
  132. /**
  133. * UCI_INTERNAL: Do an internal call of a public API function
  134. *
  135. * Sets Exception handling to passthrough mode.
  136. * Allows API functions to change behavior compared to public use
  137. */
  138. #define UCI_INTERNAL(func, ctx, ...) do { \
  139. ctx->internal = true; \
  140. func(ctx, __VA_ARGS__); \
  141. } while (0)
  142. /*
  143. * check the specified condition.
  144. * throw an invalid argument exception if it's false
  145. */
  146. #define UCI_ASSERT(ctx, expr) do { \
  147. if (!(expr)) { \
  148. DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
  149. UCI_THROW(ctx, UCI_ERR_INVAL); \
  150. } \
  151. } while (0)
  152. #endif