2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains misc utility functions and wrappers to standard
17 * functions, which throw exceptions upon failure.
20 #include <sys/types.h>
33 #include "uci_internal.h"
35 __private void *uci_malloc(struct uci_context *ctx, size_t size)
41 UCI_THROW(ctx, UCI_ERR_MEM);
47 __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
49 ptr = realloc(ptr, size);
51 UCI_THROW(ctx, UCI_ERR_MEM);
56 __private char *uci_strdup(struct uci_context *ctx, const char *str)
62 UCI_THROW(ctx, UCI_ERR_MEM);
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
72 __private bool uci_validate_str(const char *str, bool name)
78 unsigned char c = *str;
79 if (!isalnum(c) && c != '_') {
80 if (name || (c < 33) || (c > 126))
88 bool uci_validate_text(const char *str)
91 unsigned char c = *str;
93 if (c < 32 && c != '\t' && c != '\n' && c != '\r')
101 __private void uci_alloc_parse_context(struct uci_context *ctx)
103 ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
106 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
112 UCI_ASSERT(ctx, str);
113 UCI_ASSERT(ctx, ptr);
115 memset(ptr, 0, sizeof(struct uci_ptr));
118 last = strchr(str, '=');
125 ptr->package = strsep(&str, ".");
129 ptr->section = strsep(&str, ".");
131 ptr->target = UCI_TYPE_PACKAGE;
135 ptr->option = strsep(&str, ".");
137 ptr->target = UCI_TYPE_SECTION;
140 ptr->target = UCI_TYPE_OPTION;
143 tmp = strsep(&str, ".");
148 if (ptr->package && !uci_validate_package(ptr->package))
150 if (ptr->section && !uci_validate_name(ptr->section))
151 ptr->flags |= UCI_LOOKUP_EXTENDED;
152 if (ptr->option && !uci_validate_name(ptr->option))
154 if (ptr->value && !uci_validate_text(ptr->value))
160 memset(ptr, 0, sizeof(struct uci_ptr));
161 UCI_THROW(ctx, UCI_ERR_PARSE);
165 __private void uci_parse_error(struct uci_context *ctx, char *reason)
167 struct uci_parse_context *pctx = ctx->pctx;
169 pctx->reason = reason;
170 pctx->byte = pctx_pos(pctx);
171 UCI_THROW(ctx, UCI_ERR_PARSE);
177 * open a stream and go to the right position
179 * note: when opening for write and seeking to the beginning of
180 * the stream, truncate the file
182 __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
187 int flags = (write ? O_RDWR : O_RDONLY);
188 mode_t mode = UCI_FILEMODE;
190 char *filename2 = NULL;
195 name = basename((char *) origfilename);
197 name = basename((char *) filename);
199 if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
200 UCI_THROW(ctx, UCI_ERR_MEM);
202 if (stat(filename2, &statbuf) == 0)
203 mode = statbuf.st_mode;
209 if (!write && ((stat(filename, &statbuf) < 0) ||
210 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
211 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
214 fd = open(filename, flags, mode);
218 ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
219 if ((ret < 0) && (errno != ENOSYS))
222 ret = lseek(fd, 0, pos);
227 file = fdopen(fd, (write ? "w+" : "r"));
232 UCI_THROW(ctx, UCI_ERR_IO);
237 __private void uci_close_stream(FILE *stream)