cmake: Add ubox library and include dir lookup
[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, bool package)
73 {
74         if (!*str)
75                 return false;
76
77         for (; *str; str++) {
78                 unsigned char c = *str;
79
80                 if (isalnum(c) || c == '_')
81                         continue;
82
83                 if (c == '-' && package)
84                         continue;
85
86                 if (name || (c < 33) || (c > 126))
87                         return false;
88         }
89         return true;
90 }
91
92 bool uci_validate_text(const char *str)
93 {
94         while (*str) {
95                 unsigned char c = *str;
96
97                 if (c < 32 && c != '\t' && c != '\n' && c != '\r')
98                         return false;
99
100                 str++;
101         }
102         return true;
103 }
104
105 __private void uci_alloc_parse_context(struct uci_context *ctx)
106 {
107         ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
108 }
109
110 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
111 {
112         char *last = NULL;
113         char *tmp;
114
115         UCI_HANDLE_ERR(ctx);
116         UCI_ASSERT(ctx, str);
117         UCI_ASSERT(ctx, ptr);
118
119         memset(ptr, 0, sizeof(struct uci_ptr));
120
121         /* value */
122         last = strchr(str, '=');
123         if (last) {
124                 *last = 0;
125                 last++;
126                 ptr->value = last;
127         }
128
129         ptr->package = strsep(&str, ".");
130         if (!ptr->package)
131                 goto error;
132
133         ptr->section = strsep(&str, ".");
134         if (!ptr->section) {
135                 ptr->target = UCI_TYPE_PACKAGE;
136                 goto lastval;
137         }
138
139         ptr->option = strsep(&str, ".");
140         if (!ptr->option) {
141                 ptr->target = UCI_TYPE_SECTION;
142                 goto lastval;
143         } else {
144                 ptr->target = UCI_TYPE_OPTION;
145         }
146
147         tmp = strsep(&str, ".");
148         if (tmp)
149                 goto error;
150
151 lastval:
152         if (ptr->package && !uci_validate_package(ptr->package))
153                 goto error;
154         if (ptr->section && !uci_validate_name(ptr->section))
155                 ptr->flags |= UCI_LOOKUP_EXTENDED;
156         if (ptr->option && !uci_validate_name(ptr->option))
157                 goto error;
158         if (ptr->value && !uci_validate_text(ptr->value))
159                 goto error;
160
161         return 0;
162
163 error:
164         memset(ptr, 0, sizeof(struct uci_ptr));
165         UCI_THROW(ctx, UCI_ERR_PARSE);
166 }
167
168
169 __private void uci_parse_error(struct uci_context *ctx, char *reason)
170 {
171         struct uci_parse_context *pctx = ctx->pctx;
172
173         pctx->reason = reason;
174         pctx->byte = pctx_pos(pctx);
175         UCI_THROW(ctx, UCI_ERR_PARSE);
176 }
177
178
179
180 /*
181  * open a stream and go to the right position
182  *
183  * note: when opening for write and seeking to the beginning of
184  * the stream, truncate the file
185  */
186 __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
187 {
188         struct stat statbuf;
189         FILE *file = NULL;
190         int fd, ret;
191         int flags = (write ? O_RDWR : O_RDONLY);
192         mode_t mode = UCI_FILEMODE;
193         char *name = NULL;
194         char *filename2 = NULL;
195
196         if (create) {
197                 flags |= O_CREAT;
198                 if (origfilename) {
199                         name = basename((char *) origfilename);
200                 } else {
201                         name = basename((char *) filename);
202                 }
203                 if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
204                         UCI_THROW(ctx, UCI_ERR_MEM);
205                 } else {
206                         if (stat(filename2, &statbuf) == 0)
207                                 mode = statbuf.st_mode;
208
209                         free(filename2);
210                 }
211         }
212
213         if (!write && ((stat(filename, &statbuf) < 0) ||
214                 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
215                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
216         }
217
218         fd = open(filename, flags, mode);
219         if (fd < 0)
220                 goto error;
221
222         ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
223         if ((ret < 0) && (errno != ENOSYS))
224                 goto error;
225
226         ret = lseek(fd, 0, pos);
227
228         if (ret < 0)
229                 goto error;
230
231         file = fdopen(fd, (write ? "w+" : "r"));
232         if (file)
233                 goto done;
234
235 error:
236         UCI_THROW(ctx, UCI_ERR_IO);
237 done:
238         return file;
239 }
240
241 __private void uci_close_stream(FILE *stream)
242 {
243         int fd;
244
245         if (!stream)
246                 return;
247
248         fflush(stream);
249         fd = fileno(stream);
250         flock(fd, LOCK_UN);
251         fclose(stream);
252 }
253
254