libuci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 *) calloc(1, sizeof(struct uci_context));
  45. if (!ctx)
  46. return NULL;
  47. uci_list_init(&ctx->root);
  48. uci_list_init(&ctx->delta_path);
  49. uci_list_init(&ctx->backends);
  50. ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_DELTA;
  51. ctx->confdir = (char *) uci_confdir;
  52. ctx->savedir = (char *) uci_savedir;
  53. uci_add_delta_path(ctx, 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->delta_path, tmp, e) {
  72. uci_free_element(e);
  73. }
  74. UCI_TRAP_RESTORE(ctx);
  75. free(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. __private 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
  109. uci_perror(struct uci_context *ctx, const char *str)
  110. {
  111. uci_get_errorstr(ctx, NULL, str);
  112. }
  113. void
  114. uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
  115. {
  116. static char error_info[128] = { 0 };
  117. int err;
  118. err = ctx ? ctx->err : UCI_ERR_INVAL;
  119. if ((err < 0) || (err >= UCI_ERR_LAST))
  120. err = UCI_ERR_UNKNOWN;
  121. if (ctx && ctx->pctx && (err == UCI_ERR_PARSE)) {
  122. snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %zu",
  123. (ctx->pctx->reason ? ctx->pctx->reason : "unknown"),
  124. ctx->pctx->line, ctx->pctx->byte);
  125. }
  126. if (!dest) {
  127. strcat(error_info, "\n");
  128. fprintf(stderr, "%s%s%s%s%s%s",
  129. (prefix ? prefix : ""), (prefix ? ": " : ""),
  130. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  131. uci_errstr[err],
  132. error_info);
  133. return;
  134. }
  135. err = asprintf(dest, "%s%s%s%s%s%s",
  136. (prefix ? prefix : ""), (prefix ? ": " : ""),
  137. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  138. uci_errstr[err],
  139. error_info);
  140. if (err < 0)
  141. *dest = NULL;
  142. }
  143. int uci_list_configs(struct uci_context *ctx, char ***list)
  144. {
  145. UCI_HANDLE_ERR(ctx);
  146. UCI_ASSERT(ctx, list != NULL);
  147. UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
  148. *list = ctx->backend->list_configs(ctx);
  149. return 0;
  150. }
  151. int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  152. {
  153. struct uci_package *p;
  154. UCI_HANDLE_ERR(ctx);
  155. UCI_ASSERT(ctx, package != NULL);
  156. p = *package;
  157. UCI_ASSERT(ctx, p != NULL);
  158. UCI_ASSERT(ctx, p->backend && p->backend->commit);
  159. p->backend->commit(ctx, package, overwrite);
  160. return 0;
  161. }
  162. int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
  163. {
  164. struct uci_package *p;
  165. UCI_HANDLE_ERR(ctx);
  166. UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
  167. p = ctx->backend->load(ctx, name);
  168. if (package)
  169. *package = p;
  170. return 0;
  171. }
  172. int uci_set_backend(struct uci_context *ctx, const char *name)
  173. {
  174. struct uci_element *e;
  175. UCI_HANDLE_ERR(ctx);
  176. UCI_ASSERT(ctx, name != NULL);
  177. e = uci_lookup_list(&ctx->backends, name);
  178. if (!e)
  179. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  180. ctx->backend = uci_to_backend(e);
  181. return 0;
  182. }