libuci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /*
  15. * This file contains some common code for the uci library
  16. */
  17. #define _GNU_SOURCE
  18. #include <sys/types.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <dlfcn.h>
  24. #include <glob.h>
  25. #include "uci.h"
  26. static const char *uci_errstr[] = {
  27. [UCI_OK] = "Success",
  28. [UCI_ERR_MEM] = "Out of memory",
  29. [UCI_ERR_INVAL] = "Invalid argument",
  30. [UCI_ERR_NOTFOUND] = "Entry not found",
  31. [UCI_ERR_IO] = "I/O error",
  32. [UCI_ERR_PARSE] = "Parse error",
  33. [UCI_ERR_DUPLICATE] = "Duplicate entry",
  34. [UCI_ERR_UNKNOWN] = "Unknown error",
  35. };
  36. #include "uci_internal.h"
  37. #include "list.c"
  38. __private const char *uci_confdir = UCI_CONFDIR;
  39. __private const char *uci_savedir = UCI_SAVEDIR;
  40. /* exported functions */
  41. struct uci_context *uci_alloc_context(void)
  42. {
  43. struct uci_context *ctx;
  44. ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
  45. if (!ctx)
  46. return NULL;
  47. memset(ctx, 0, sizeof(struct uci_context));
  48. uci_list_init(&ctx->root);
  49. uci_list_init(&ctx->delta_path);
  50. uci_list_init(&ctx->backends);
  51. ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_DELTA;
  52. ctx->confdir = (char *) uci_confdir;
  53. ctx->savedir = (char *) uci_savedir;
  54. uci_add_delta_path(ctx, uci_savedir);
  55. uci_list_add(&ctx->backends, &uci_file_backend.e.list);
  56. ctx->backend = &uci_file_backend;
  57. return ctx;
  58. }
  59. void uci_free_context(struct uci_context *ctx)
  60. {
  61. struct uci_element *e, *tmp;
  62. if (ctx->confdir != uci_confdir)
  63. free(ctx->confdir);
  64. if (ctx->savedir != uci_savedir)
  65. free(ctx->savedir);
  66. uci_cleanup(ctx);
  67. UCI_TRAP_SAVE(ctx, ignore);
  68. uci_foreach_element_safe(&ctx->root, tmp, e) {
  69. struct uci_package *p = uci_to_package(e);
  70. uci_free_package(&p);
  71. }
  72. uci_foreach_element_safe(&ctx->delta_path, tmp, e) {
  73. uci_free_element(e);
  74. }
  75. UCI_TRAP_RESTORE(ctx);
  76. free(ctx);
  77. ignore:
  78. return;
  79. }
  80. int uci_set_confdir(struct uci_context *ctx, const char *dir)
  81. {
  82. char *cdir;
  83. UCI_HANDLE_ERR(ctx);
  84. UCI_ASSERT(ctx, dir != NULL);
  85. cdir = uci_strdup(ctx, dir);
  86. if (ctx->confdir != uci_confdir)
  87. free(ctx->confdir);
  88. ctx->confdir = cdir;
  89. return 0;
  90. }
  91. __private void uci_cleanup(struct uci_context *ctx)
  92. {
  93. struct uci_parse_context *pctx;
  94. if (ctx->buf) {
  95. free(ctx->buf);
  96. ctx->buf = NULL;
  97. ctx->bufsz = 0;
  98. }
  99. pctx = ctx->pctx;
  100. if (!pctx)
  101. return;
  102. ctx->pctx = NULL;
  103. if (pctx->package)
  104. uci_free_package(&pctx->package);
  105. if (pctx->buf)
  106. free(pctx->buf);
  107. free(pctx);
  108. }
  109. void
  110. uci_perror(struct uci_context *ctx, const char *str)
  111. {
  112. uci_get_errorstr(ctx, NULL, str);
  113. }
  114. void
  115. uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
  116. {
  117. static char error_info[128];
  118. int err;
  119. const char *format =
  120. "%s%s" /* prefix */
  121. "%s%s" /* function */
  122. "%s" /* error */
  123. "%s"; /* details */
  124. error_info[0] = 0;
  125. if (!ctx)
  126. err = UCI_ERR_INVAL;
  127. else
  128. err = ctx->err;
  129. if ((err < 0) || (err >= UCI_ERR_LAST))
  130. err = UCI_ERR_UNKNOWN;
  131. switch (err) {
  132. case UCI_ERR_PARSE:
  133. if (ctx->pctx) {
  134. snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %d", (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
  135. break;
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. if (dest) {
  142. err = asprintf(dest, format,
  143. (prefix ? prefix : ""), (prefix ? ": " : ""),
  144. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  145. uci_errstr[err],
  146. error_info);
  147. if (err < 0)
  148. *dest = NULL;
  149. } else {
  150. strcat(error_info, "\n");
  151. fprintf(stderr, format,
  152. (prefix ? prefix : ""), (prefix ? ": " : ""),
  153. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  154. uci_errstr[err],
  155. error_info);
  156. }
  157. }
  158. int uci_list_configs(struct uci_context *ctx, char ***list)
  159. {
  160. UCI_HANDLE_ERR(ctx);
  161. UCI_ASSERT(ctx, list != NULL);
  162. UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
  163. *list = ctx->backend->list_configs(ctx);
  164. return 0;
  165. }
  166. int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  167. {
  168. struct uci_package *p;
  169. UCI_HANDLE_ERR(ctx);
  170. UCI_ASSERT(ctx, package != NULL);
  171. p = *package;
  172. UCI_ASSERT(ctx, p != NULL);
  173. UCI_ASSERT(ctx, p->backend && p->backend->commit);
  174. p->backend->commit(ctx, package, overwrite);
  175. return 0;
  176. }
  177. int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
  178. {
  179. struct uci_package *p;
  180. UCI_HANDLE_ERR(ctx);
  181. UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
  182. p = ctx->backend->load(ctx, name);
  183. if (package)
  184. *package = p;
  185. return 0;
  186. }
  187. int uci_set_backend(struct uci_context *ctx, const char *name)
  188. {
  189. struct uci_element *e;
  190. UCI_HANDLE_ERR(ctx);
  191. UCI_ASSERT(ctx, name != NULL);
  192. e = uci_lookup_list(&ctx->backends, name);
  193. if (!e)
  194. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  195. ctx->backend = uci_to_backend(e);
  196. return 0;
  197. }