libuci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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] = { 0 };
  118. int err;
  119. err = ctx ? ctx->err : UCI_ERR_INVAL;
  120. if ((err < 0) || (err >= UCI_ERR_LAST))
  121. err = UCI_ERR_UNKNOWN;
  122. if (ctx && ctx->pctx && (err == UCI_ERR_PARSE)) {
  123. snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %d",
  124. (ctx->pctx->reason ? ctx->pctx->reason : "unknown"),
  125. ctx->pctx->line, ctx->pctx->byte);
  126. }
  127. if (!dest) {
  128. strcat(error_info, "\n");
  129. fprintf(stderr, "%s%s%s%s%s%s",
  130. (prefix ? prefix : ""), (prefix ? ": " : ""),
  131. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  132. uci_errstr[err],
  133. error_info);
  134. return;
  135. }
  136. err = asprintf(dest, "%s%s%s%s%s%s",
  137. (prefix ? prefix : ""), (prefix ? ": " : ""),
  138. (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
  139. uci_errstr[err],
  140. error_info);
  141. if (err < 0)
  142. *dest = NULL;
  143. }
  144. int uci_list_configs(struct uci_context *ctx, char ***list)
  145. {
  146. UCI_HANDLE_ERR(ctx);
  147. UCI_ASSERT(ctx, list != NULL);
  148. UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
  149. *list = ctx->backend->list_configs(ctx);
  150. return 0;
  151. }
  152. int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  153. {
  154. struct uci_package *p;
  155. UCI_HANDLE_ERR(ctx);
  156. UCI_ASSERT(ctx, package != NULL);
  157. p = *package;
  158. UCI_ASSERT(ctx, p != NULL);
  159. UCI_ASSERT(ctx, p->backend && p->backend->commit);
  160. p->backend->commit(ctx, package, overwrite);
  161. return 0;
  162. }
  163. int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
  164. {
  165. struct uci_package *p;
  166. UCI_HANDLE_ERR(ctx);
  167. UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
  168. p = ctx->backend->load(ctx, name);
  169. if (package)
  170. *package = p;
  171. return 0;
  172. }
  173. int uci_set_backend(struct uci_context *ctx, const char *name)
  174. {
  175. struct uci_element *e;
  176. UCI_HANDLE_ERR(ctx);
  177. UCI_ASSERT(ctx, name != NULL);
  178. e = uci_lookup_list(&ctx->backends, name);
  179. if (!e)
  180. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  181. ctx->backend = uci_to_backend(e);
  182. return 0;
  183. }