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