uci_internal.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. struct uci_parse_context
  17. {
  18. /* error context */
  19. const char *reason;
  20. int line;
  21. int byte;
  22. /* private: */
  23. struct uci_package *package;
  24. struct uci_section *section;
  25. bool merge;
  26. FILE *file;
  27. const char *name;
  28. char *buf;
  29. int bufsz;
  30. };
  31. int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
  32. void uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value);
  33. void uci_free_history(struct uci_history *h);
  34. /*
  35. * functions for debug and error handling, for internal use only
  36. */
  37. #ifdef UCI_DEBUG
  38. #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
  39. #else
  40. #define DPRINTF(...)
  41. #endif
  42. /*
  43. * throw an uci exception and store the error number
  44. * in the context.
  45. */
  46. #define UCI_THROW(ctx, err) do { \
  47. DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
  48. longjmp(ctx->trap, err); \
  49. } while (0)
  50. /*
  51. * store the return address for handling exceptions
  52. * needs to be called in every externally visible library function
  53. *
  54. * NB: this does not handle recursion at all. Calling externally visible
  55. * functions from other uci functions is only allowed at the end of the
  56. * calling function, or by wrapping the function call in UCI_TRAP_SAVE
  57. * and UCI_TRAP_RESTORE.
  58. */
  59. #define UCI_HANDLE_ERR(ctx) do { \
  60. DPRINTF("ENTER: %s\n", __func__); \
  61. int __val = 0; \
  62. ctx->errno = 0; \
  63. if (!ctx) \
  64. return UCI_ERR_INVAL; \
  65. if (!ctx->internal) \
  66. __val = setjmp(ctx->trap); \
  67. ctx->internal = false; \
  68. if (__val) { \
  69. DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
  70. ctx->errno = __val; \
  71. return __val; \
  72. } \
  73. } while (0)
  74. /*
  75. * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
  76. * are intercepted and redirected to the label specified in 'handler'
  77. * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
  78. * exception handler is restored
  79. */
  80. #define UCI_TRAP_SAVE(ctx, handler) do { \
  81. jmp_buf __old_trap; \
  82. int __val; \
  83. memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
  84. __val = setjmp(ctx->trap); \
  85. if (__val) { \
  86. ctx->errno = __val; \
  87. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  88. goto handler; \
  89. }
  90. #define UCI_TRAP_RESTORE(ctx) \
  91. memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
  92. } while(0)
  93. /**
  94. * UCI_INTERNAL: Do an internal call of a public API function
  95. *
  96. * Sets Exception handling to passthrough mode.
  97. * Allows API functions to change behavior compared to public use
  98. */
  99. #define UCI_INTERNAL(func, ctx, ...) do { \
  100. ctx->internal = true; \
  101. func(ctx, __VA_ARGS__); \
  102. } while (0)
  103. /*
  104. * check the specified condition.
  105. * throw an invalid argument exception if it's false
  106. */
  107. #define UCI_ASSERT(ctx, expr) do { \
  108. if (!(expr)) { \
  109. DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
  110. UCI_THROW(ctx, UCI_ERR_INVAL); \
  111. } \
  112. } while (0)
  113. #endif