libuci.c 5.6 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 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 "uci.h"
  24. static const char *uci_confdir = UCI_CONFDIR;
  25. static const char *uci_savedir = UCI_SAVEDIR;
  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. static void uci_cleanup(struct uci_context *ctx);
  37. #include "uci_internal.h"
  38. #include "util.c"
  39. #include "list.c"
  40. #include "history.c"
  41. #include "file.c"
  42. /* exported functions */
  43. struct uci_context *uci_alloc_context(void)
  44. {
  45. struct uci_context *ctx;
  46. ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
  47. memset(ctx, 0, sizeof(struct uci_context));
  48. uci_list_init(&ctx->root);
  49. uci_list_init(&ctx->history_path);
  50. uci_list_init(&ctx->backends);
  51. ctx->flags = UCI_FLAG_STRICT;
  52. ctx->confdir = (char *) uci_confdir;
  53. ctx->savedir = (char *) uci_savedir;
  54. uci_list_add(&ctx->backends, &uci_file_backend.e.list);
  55. ctx->backend = &uci_file_backend;
  56. return ctx;
  57. }
  58. void uci_free_context(struct uci_context *ctx)
  59. {
  60. struct uci_element *e, *tmp;
  61. if (ctx->confdir != uci_confdir)
  62. free(ctx->confdir);
  63. if (ctx->savedir != uci_savedir)
  64. free(ctx->savedir);
  65. uci_cleanup(ctx);
  66. UCI_TRAP_SAVE(ctx, ignore);
  67. uci_foreach_element_safe(&ctx->root, tmp, e) {
  68. struct uci_package *p = uci_to_package(e);
  69. uci_free_package(&p);
  70. }
  71. uci_foreach_element_safe(&ctx->history_path, tmp, e) {
  72. uci_free_element(e);
  73. }
  74. free(ctx);
  75. UCI_TRAP_RESTORE(ctx);
  76. ignore:
  77. return;
  78. }
  79. int uci_set_confdir(struct uci_context *ctx, const char *dir)
  80. {
  81. char *cdir;
  82. UCI_HANDLE_ERR(ctx);
  83. UCI_ASSERT(ctx, dir != NULL);
  84. cdir = uci_strdup(ctx, dir);
  85. if (ctx->confdir != uci_confdir)
  86. free(ctx->confdir);
  87. ctx->confdir = cdir;
  88. return 0;
  89. }
  90. static void uci_cleanup(struct uci_context *ctx)
  91. {
  92. struct uci_parse_context *pctx;
  93. if (ctx->buf) {
  94. free(ctx->buf);
  95. ctx->buf = NULL;
  96. ctx->bufsz = 0;
  97. }
  98. pctx = ctx->pctx;
  99. if (!pctx)
  100. return;
  101. ctx->pctx = NULL;
  102. if (pctx->package)
  103. uci_free_package(&pctx->package);
  104. if (pctx->buf)
  105. free(pctx->buf);
  106. free(pctx);
  107. }
  108. void uci_perror(struct uci_context *ctx, const char *prefix)
  109. {
  110. int err;
  111. if (!ctx)
  112. err = UCI_ERR_INVAL;
  113. else
  114. err = ctx->errno;
  115. if ((err < 0) || (err >= UCI_ERR_LAST))
  116. err = UCI_ERR_UNKNOWN;
  117. if (prefix)
  118. fprintf(stderr, "%s: ", prefix);
  119. if (ctx->func)
  120. fprintf(stderr, "%s: ", ctx->func);
  121. switch (err) {
  122. case UCI_ERR_PARSE:
  123. if (ctx->pctx) {
  124. fprintf(stderr, "%s (%s) at line %d, byte %d\n", uci_errstr[err], (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
  125. break;
  126. }
  127. /* fall through */
  128. default:
  129. fprintf(stderr, "%s\n", uci_errstr[err]);
  130. break;
  131. }
  132. }
  133. int uci_list_configs(struct uci_context *ctx, char ***list)
  134. {
  135. UCI_HANDLE_ERR(ctx);
  136. UCI_ASSERT(ctx, list != NULL);
  137. UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
  138. *list = ctx->backend->list_configs(ctx);
  139. return 0;
  140. }
  141. int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  142. {
  143. struct uci_package *p;
  144. UCI_HANDLE_ERR(ctx);
  145. UCI_ASSERT(ctx, package != NULL);
  146. p = *package;
  147. UCI_ASSERT(ctx, p != NULL);
  148. UCI_ASSERT(ctx, p->backend && p->backend->commit);
  149. p->backend->commit(ctx, package, overwrite);
  150. return 0;
  151. }
  152. int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
  153. {
  154. struct uci_package *p;
  155. UCI_HANDLE_ERR(ctx);
  156. UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
  157. p = ctx->backend->load(ctx, name);
  158. if (package)
  159. *package = p;
  160. return 0;
  161. }
  162. #ifdef UCI_PLUGIN_SUPPORT
  163. __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b)
  164. {
  165. struct uci_element *e;
  166. UCI_HANDLE_ERR(ctx);
  167. e = uci_lookup_list(&ctx->backends, b->e.name);
  168. if (e)
  169. UCI_THROW(ctx, UCI_ERR_DUPLICATE);
  170. e = uci_malloc(ctx, sizeof(struct uci_backend));
  171. memcpy(e, b, sizeof(struct uci_backend));
  172. uci_list_add(&ctx->backends, &e->list);
  173. return 0;
  174. }
  175. __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b)
  176. {
  177. struct uci_element *e, *tmp;
  178. UCI_HANDLE_ERR(ctx);
  179. e = uci_lookup_list(&ctx->backends, b->e.name);
  180. if (!e || uci_to_backend(e)->ptr != b->ptr)
  181. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  182. b = uci_to_backend(e);
  183. if (ctx->backend && ctx->backend->ptr == b->ptr)
  184. ctx->backend = &uci_file_backend;
  185. uci_foreach_element_safe(&ctx->root, tmp, e) {
  186. struct uci_package *p = uci_to_package(e);
  187. if (!p->backend)
  188. continue;
  189. if (p->backend->ptr == b->ptr)
  190. UCI_INTERNAL(uci_unload, ctx, p);
  191. }
  192. uci_list_del(&b->e.list);
  193. free(b);
  194. return 0;
  195. }
  196. #endif
  197. int uci_set_backend(struct uci_context *ctx, const char *name)
  198. {
  199. struct uci_element *e;
  200. UCI_HANDLE_ERR(ctx);
  201. UCI_ASSERT(ctx, name != NULL);
  202. e = uci_lookup_list(&ctx->backends, name);
  203. if (!e)
  204. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  205. ctx->backend = uci_to_backend(e);
  206. return 0;
  207. }