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