move history code into history.c
[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 static bool uci_validate_name(const char *str)
63 {
64 if (!*str)
65 return false;
66
67 while (*str) {
68 if (!isalnum(*str) && (*str != '_'))
69 return false;
70 str++;
71 }
72 return true;
73 }
74
75 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
76 {
77 char *last = NULL;
78
79 UCI_HANDLE_ERR(ctx);
80 UCI_ASSERT(ctx, str && package && section && option);
81
82 *package = strtok(str, ".");
83 if (!*package || !uci_validate_name(*package))
84 goto error;
85
86 last = *package;
87 *section = strtok(NULL, ".");
88 if (!*section)
89 goto lastval;
90
91 last = *section;
92 *option = strtok(NULL, ".");
93 if (!*option)
94 goto lastval;
95
96 last = *option;
97
98 lastval:
99 last = strchr(last, '=');
100 if (last) {
101 if (!value)
102 goto error;
103
104 *last = 0;
105 last++;
106 if (!*last)
107 goto error;
108 *value = last;
109 }
110
111 if (*section && !uci_validate_name(*section))
112 goto error;
113 if (*option && !uci_validate_name(*option))
114 goto error;
115
116 goto done;
117
118 error:
119 UCI_THROW(ctx, UCI_ERR_PARSE);
120
121 done:
122 return 0;
123 }
124
125
126 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
127 {
128 struct uci_parse_context *pctx = ctx->pctx;
129
130 pctx->reason = reason;
131 pctx->byte = pos - pctx->buf;
132 UCI_THROW(ctx, UCI_ERR_PARSE);
133 }
134
135
136 /*
137 * Fetch a new line from the input stream and resize buffer if necessary
138 */
139 static void uci_getln(struct uci_context *ctx, int offset)
140 {
141 struct uci_parse_context *pctx = ctx->pctx;
142 char *p;
143 int ofs;
144
145 if (pctx->buf == NULL) {
146 pctx->buf = uci_malloc(ctx, LINEBUF);
147 pctx->bufsz = LINEBUF;
148 }
149
150 ofs = offset;
151 do {
152 p = &pctx->buf[ofs];
153 p[ofs] = 0;
154
155 p = fgets(p, pctx->bufsz - ofs, pctx->file);
156 if (!p || !*p)
157 return;
158
159 ofs += strlen(p);
160 if (pctx->buf[ofs - 1] == '\n') {
161 pctx->line++;
162 pctx->buf[ofs - 1] = 0;
163 return;
164 }
165
166 if (pctx->bufsz > LINEBUF_MAX/2)
167 uci_parse_error(ctx, p, "line too long");
168
169 pctx->bufsz *= 2;
170 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
171 } while (1);
172 }
173
174 /*
175 * open a stream and go to the right position
176 *
177 * note: when opening for write and seeking to the beginning of
178 * the stream, truncate the file
179 */
180 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
181 {
182 struct stat statbuf;
183 FILE *file = NULL;
184 int fd, ret;
185 int mode = (write ? O_RDWR : O_RDONLY);
186
187 if (create)
188 mode |= O_CREAT;
189
190 if (!write && ((stat(filename, &statbuf) < 0) ||
191 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
192 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
193 }
194
195 fd = open(filename, mode, UCI_FILEMODE);
196 if (fd <= 0)
197 goto error;
198
199 if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
200 goto error;
201
202 ret = lseek(fd, 0, pos);
203
204 if (ret < 0)
205 goto error;
206
207 file = fdopen(fd, (write ? "w+" : "r"));
208 if (file)
209 goto done;
210
211 error:
212 UCI_THROW(ctx, UCI_ERR_IO);
213 done:
214 return file;
215 }
216
217 static void uci_close_stream(FILE *stream)
218 {
219 int fd;
220
221 if (!stream)
222 return;
223
224 fd = fileno(stream);
225 flock(fd, LOCK_UN);
226 fclose(stream);
227 }
228
229