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