remove internal usage of redundant uci_ptr.last
[project/uci.git] / libuci.c
index 550eca23db04390205429eec340899d1ff2477cf..ae4c96476507c42e55f6817dd67d8a5c15bbcf1a 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -48,19 +48,18 @@ struct uci_context *uci_alloc_context(void)
 {
        struct uci_context *ctx;
 
-       ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
+       ctx = (struct uci_context *) calloc(1, sizeof(struct uci_context));
        if (!ctx)
                return NULL;
 
-       memset(ctx, 0, sizeof(struct uci_context));
        uci_list_init(&ctx->root);
        uci_list_init(&ctx->delta_path);
        uci_list_init(&ctx->backends);
-       uci_list_init(&ctx->hooks);
        ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_DELTA;
 
        ctx->confdir = (char *) uci_confdir;
        ctx->savedir = (char *) uci_savedir;
+       uci_add_delta_path(ctx, uci_savedir);
 
        uci_list_add(&ctx->backends, &uci_file_backend.e.list);
        ctx->backend = &uci_file_backend;
@@ -140,50 +139,37 @@ uci_perror(struct uci_context *ctx, const char *str)
 void
 uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
 {
-       static char error_info[128];
+       static char error_info[128] = { 0 };
        int err;
-       const char *format =
-               "%s%s" /* prefix */
-               "%s%s" /* function */
-               "%s" /* error */
-               "%s"; /* details */
-
-       error_info[0] = 0;
-
-       if (!ctx)
-               err = UCI_ERR_INVAL;
-       else
-               err = ctx->err;
 
+       err = ctx ? ctx->err : UCI_ERR_INVAL;
        if ((err < 0) || (err >= UCI_ERR_LAST))
                err = UCI_ERR_UNKNOWN;
 
-       switch (err) {
-       case UCI_ERR_PARSE:
-               if (ctx->pctx) {
-                       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);
-                       break;
-               }
-               break;
-       default:
-               break;
+       if (ctx && ctx->pctx && (err == UCI_ERR_PARSE)) {
+               snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %zu",
+                        (ctx->pctx->reason ? ctx->pctx->reason : "unknown"),
+                        ctx->pctx->line, ctx->pctx->byte);
        }
-       if (dest) {
-               err = asprintf(dest, format,
-                       (prefix ? prefix : ""), (prefix ? ": " : ""),
-                       (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
-                       uci_errstr[err],
-                       error_info);
-               if (err < 0)
-                       *dest = NULL;
-       } else {
+
+       if (!dest) {
                strcat(error_info, "\n");
-               fprintf(stderr, format,
+               fprintf(stderr, "%s%s%s%s%s%s",
                        (prefix ? prefix : ""), (prefix ? ": " : ""),
                        (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
                        uci_errstr[err],
                        error_info);
+               return;
        }
+
+       err = asprintf(dest, "%s%s%s%s%s%s",
+               (prefix ? prefix : ""), (prefix ? ": " : ""),
+               (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
+               uci_errstr[err],
+               error_info);
+
+       if (err < 0)
+               *dest = NULL;
 }
 
 int uci_list_configs(struct uci_context *ctx, char ***list)
@@ -210,16 +196,10 @@ int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overw
 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
 {
        struct uci_package *p;
-       struct uci_element *e;
 
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
        p = ctx->backend->load(ctx, name);
-       uci_foreach_element(&ctx->hooks, e) {
-               struct uci_hook *h = uci_to_hook(e);
-               if (h->ops->load)
-                       h->ops->load(h->ops, p);
-       }
        if (package)
                *package = p;
 
@@ -238,40 +218,3 @@ int uci_set_backend(struct uci_context *ctx, const char *name)
        ctx->backend = uci_to_backend(e);
        return 0;
 }
-
-int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
-{
-       struct uci_element *e;
-       struct uci_hook *h;
-
-       UCI_HANDLE_ERR(ctx);
-
-       /* check for duplicate elements */
-       uci_foreach_element(&ctx->hooks, e) {
-               h = uci_to_hook(e);
-               if (h->ops == ops)
-                       return UCI_ERR_INVAL;
-       }
-
-       h = uci_alloc_element(ctx, hook, "", 0);
-       h->ops = ops;
-       uci_list_init(&h->e.list);
-       uci_list_add(&ctx->hooks, &h->e.list);
-
-       return 0;
-}
-
-int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
-{
-       struct uci_element *e;
-
-       uci_foreach_element(&ctx->hooks, e) {
-               struct uci_hook *h = uci_to_hook(e);
-               if (h->ops == ops) {
-                       uci_list_del(&e->list);
-                       uci_free_element(e);
-                       return 0;
-               }
-       }
-       return UCI_ERR_NOTFOUND;
-}