make use of the history path feature in the cli
[project/uci.git] / libuci.c
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 /*
16  * This file contains some common code for the uci library
17  */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "uci.h"
26 #include "err.h"
27
28 static const char *uci_confdir = UCI_CONFDIR;
29 static const char *uci_savedir = UCI_SAVEDIR;
30
31 static const char *uci_errstr[] = {
32         [UCI_OK] =            "Success",
33         [UCI_ERR_MEM] =       "Out of memory",
34         [UCI_ERR_INVAL] =     "Invalid argument",
35         [UCI_ERR_NOTFOUND] =  "Entry not found",
36         [UCI_ERR_IO] =        "I/O error",
37         [UCI_ERR_PARSE] =     "Parse error",
38         [UCI_ERR_DUPLICATE] = "Duplicate entry",
39         [UCI_ERR_UNKNOWN] =   "Unknown error",
40 };
41
42 #include "util.c"
43 #include "list.c"
44 #include "file.c"
45
46 /* exported functions */
47 struct uci_context *uci_alloc_context(void)
48 {
49         struct uci_context *ctx;
50
51         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
52         memset(ctx, 0, sizeof(struct uci_context));
53         uci_list_init(&ctx->root);
54         uci_list_init(&ctx->history_path);
55         ctx->flags = UCI_FLAG_STRICT;
56
57         ctx->confdir = (char *) uci_confdir;
58         ctx->savedir = (char *) uci_savedir;
59
60         return ctx;
61 }
62
63 void uci_free_context(struct uci_context *ctx)
64 {
65         struct uci_element *e, *tmp;
66
67         if (ctx->confdir != uci_confdir)
68                 free(ctx->confdir);
69         if (ctx->savedir != uci_savedir)
70                 free(ctx->savedir);
71
72         UCI_TRAP_SAVE(ctx, ignore);
73         uci_cleanup(ctx);
74         uci_foreach_element_safe(&ctx->root, tmp, e) {
75                 struct uci_package *p = uci_to_package(e);
76                 uci_free_package(&p);
77         }
78         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
79                 uci_free_element(e);
80         }
81         free(ctx);
82         UCI_TRAP_RESTORE(ctx);
83
84 ignore:
85         return;
86 }
87
88 int uci_add_history_path(struct uci_context *ctx, const char *dir)
89 {
90         struct uci_element *e;
91
92         UCI_HANDLE_ERR(ctx);
93         UCI_ASSERT(ctx, dir != NULL);
94         e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
95         uci_list_add(&ctx->history_path, &e->list);
96
97         return 0;
98 }
99
100 int uci_set_confdir(struct uci_context *ctx, const char *dir)
101 {
102         char *cdir;
103
104         UCI_HANDLE_ERR(ctx);
105         UCI_ASSERT(ctx, dir != NULL);
106
107         cdir = uci_strdup(ctx, dir);
108         if (ctx->confdir != uci_confdir)
109                 free(ctx->confdir);
110         ctx->confdir = cdir;
111         return 0;
112 }
113
114 int uci_set_savedir(struct uci_context *ctx, const char *dir)
115 {
116         char *sdir;
117
118         UCI_HANDLE_ERR(ctx);
119         UCI_ASSERT(ctx, dir != NULL);
120
121         sdir = uci_strdup(ctx, dir);
122         if (ctx->savedir != uci_savedir)
123                 free(ctx->savedir);
124         ctx->savedir = sdir;
125         return 0;
126 }
127
128 int uci_cleanup(struct uci_context *ctx)
129 {
130         UCI_HANDLE_ERR(ctx);
131         uci_file_cleanup(ctx);
132         return 0;
133 }
134
135 void uci_perror(struct uci_context *ctx, const char *prefix)
136 {
137         int err;
138
139         if (!ctx)
140                 err = UCI_ERR_INVAL;
141         else
142                 err = ctx->errno;
143
144         if ((err < 0) || (err >= UCI_ERR_LAST))
145                 err = UCI_ERR_UNKNOWN;
146
147         if (prefix)
148                 fprintf(stderr, "%s: ", prefix);
149         if (ctx->func)
150                 fprintf(stderr, "%s: ", ctx->func);
151
152         switch (err) {
153         case UCI_ERR_PARSE:
154                 if (ctx->pctx) {
155                         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);
156                         break;
157                 }
158                 /* fall through */
159         default:
160                 fprintf(stderr, "%s\n", uci_errstr[err]);
161                 break;
162         }
163 }
164
165