78b22cffae25849ebed5715b4f243d602374ffed
[project/uci.git] / util.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 wrappers to standard functions, which
17  * throw exceptions upon failure.
18  */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26
27 #define LINEBUF 32
28 #define LINEBUF_MAX     4096
29
30 static void *uci_malloc(struct uci_context *ctx, size_t size)
31 {
32         void *ptr;
33
34         ptr = malloc(size);
35         if (!ptr)
36                 UCI_THROW(ctx, UCI_ERR_MEM);
37         memset(ptr, 0, size);
38
39         return ptr;
40 }
41
42 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
43 {
44         ptr = realloc(ptr, size);
45         if (!ptr)
46                 UCI_THROW(ctx, UCI_ERR_MEM);
47
48         return ptr;
49 }
50
51 static char *uci_strdup(struct uci_context *ctx, const char *str)
52 {
53         char *ptr;
54
55         ptr = strdup(str);
56         if (!ptr)
57                 UCI_THROW(ctx, UCI_ERR_MEM);
58
59         return ptr;
60 }
61
62 /*
63  * validate strings for names and types, reject special characters
64  * for names, only alphanum and _ is allowed (shell compatibility)
65  * for types, we allow more characters
66  */
67 static bool uci_validate_str(const char *str, bool name)
68 {
69         if (!*str)
70                 return false;
71
72         while (*str) {
73                 char c = *str;
74                 if (!isalnum(c) && c != '_') {
75                         if (name || (c < 33) || (c > 126))
76                                 return false;
77                 }
78                 str++;
79         }
80         return true;
81 }
82
83 static inline bool uci_validate_name(const char *str)
84 {
85         return uci_validate_str(str, true);
86 }
87
88 static void uci_alloc_parse_context(struct uci_context *ctx)
89 {
90         ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
91 }
92
93 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
94 {
95         char *last = NULL;
96
97         UCI_HANDLE_ERR(ctx);
98         UCI_ASSERT(ctx, str && package && section && option);
99
100         *package = strtok(str, ".");
101         if (!*package || !uci_validate_name(*package))
102                 goto error;
103
104         last = *package;
105         *section = strtok(NULL, ".");
106         if (!*section)
107                 goto lastval;
108
109         last = *section;
110         *option = strtok(NULL, ".");
111         if (!*option)
112                 goto lastval;
113
114         last = *option;
115
116 lastval:
117         last = strchr(last, '=');
118         if (last) {
119                 if (!value)
120                         goto error;
121
122                 *last = 0;
123                 last++;
124                 if (!*last)
125                         goto error;
126                 *value = last;
127         }
128
129         if (*section && !uci_validate_name(*section))
130                 goto error;
131         if (*option && !uci_validate_name(*option))
132                 goto error;
133
134         goto done;
135
136 error:
137         UCI_THROW(ctx, UCI_ERR_PARSE);
138
139 done:
140         return 0;
141 }
142
143
144 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
145 {
146         struct uci_parse_context *pctx = ctx->pctx;
147
148         pctx->reason = reason;
149         pctx->byte = pos - pctx->buf;
150         UCI_THROW(ctx, UCI_ERR_PARSE);
151 }
152
153
154 /*
155  * Fetch a new line from the input stream and resize buffer if necessary
156  */
157 static void uci_getln(struct uci_context *ctx, int offset)
158 {
159         struct uci_parse_context *pctx = ctx->pctx;
160         char *p;
161         int ofs;
162
163         if (pctx->buf == NULL) {
164                 pctx->buf = uci_malloc(ctx, LINEBUF);
165                 pctx->bufsz = LINEBUF;
166         }
167
168         ofs = offset;
169         do {
170                 p = &pctx->buf[ofs];
171                 p[ofs] = 0;
172
173                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
174                 if (!p || !*p)
175                         return;
176
177                 ofs += strlen(p);
178                 if (pctx->buf[ofs - 1] == '\n') {
179                         pctx->line++;
180                         pctx->buf[ofs - 1] = 0;
181                         return;
182                 }
183
184                 if (pctx->bufsz > LINEBUF_MAX/2)
185                         uci_parse_error(ctx, p, "line too long");
186
187                 pctx->bufsz *= 2;
188                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
189         } while (1);
190 }
191
192 /*
193  * open a stream and go to the right position
194  *
195  * note: when opening for write and seeking to the beginning of
196  * the stream, truncate the file
197  */
198 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
199 {
200         struct stat statbuf;
201         FILE *file = NULL;
202         int fd, ret;
203         int mode = (write ? O_RDWR : O_RDONLY);
204
205         if (create)
206                 mode |= O_CREAT;
207
208         if (!write && ((stat(filename, &statbuf) < 0) ||
209                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
210                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
211         }
212
213         fd = open(filename, mode, UCI_FILEMODE);
214         if (fd <= 0)
215                 goto error;
216
217         if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
218                 goto error;
219
220         ret = lseek(fd, 0, pos);
221
222         if (ret < 0)
223                 goto error;
224
225         file = fdopen(fd, (write ? "w+" : "r"));
226         if (file)
227                 goto done;
228
229 error:
230         UCI_THROW(ctx, UCI_ERR_IO);
231 done:
232         return file;
233 }
234
235 static void uci_close_stream(FILE *stream)
236 {
237         int fd;
238
239         if (!stream)
240                 return;
241
242         fd = fileno(stream);
243         flock(fd, LOCK_UN);
244         fclose(stream);
245 }
246
247