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 General Public License for more details.
16 * This file contains misc utility functions and wrappers to standard
17 * functions, which throw exceptions upon failure.
19 #include <sys/types.h>
28 __plugin void *uci_malloc(struct uci_context *ctx, size_t size)
34 UCI_THROW(ctx, UCI_ERR_MEM);
40 __plugin void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
42 ptr = realloc(ptr, size);
44 UCI_THROW(ctx, UCI_ERR_MEM);
49 __plugin char *uci_strdup(struct uci_context *ctx, const char *str)
55 UCI_THROW(ctx, UCI_ERR_MEM);
60 /* Based on an efficient hash function published by D. J. Bernstein */
61 static unsigned int djbhash(unsigned int hash, char *str)
63 int len = strlen(str);
70 for(i = 0; i < len; i++) {
71 hash = ((hash << 5) + hash) + str[i];
73 return (hash & 0x7FFFFFFF);
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
81 __plugin bool uci_validate_str(const char *str, bool name)
87 unsigned char c = *str;
88 if (!isalnum(c) && c != '_') {
89 if (name || (c < 33) || (c > 126))
97 bool uci_validate_text(const char *str)
100 unsigned char c = *str;
101 if ((c == '\r') || (c == '\n') ||
102 ((c < 32) && (c != '\t')))
109 __private void uci_alloc_parse_context(struct uci_context *ctx)
111 ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
114 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
120 UCI_ASSERT(ctx, str);
121 UCI_ASSERT(ctx, ptr);
123 memset(ptr, 0, sizeof(struct uci_ptr));
126 last = strchr(str, '=');
133 ptr->package = strsep(&str, ".");
137 ptr->section = strsep(&str, ".");
139 ptr->target = UCI_TYPE_PACKAGE;
143 ptr->option = strsep(&str, ".");
145 ptr->target = UCI_TYPE_SECTION;
148 ptr->target = UCI_TYPE_OPTION;
151 tmp = strsep(&str, ".");
156 if (ptr->package && !uci_validate_package(ptr->package))
158 if (ptr->section && !uci_validate_name(ptr->section))
159 ptr->flags |= UCI_LOOKUP_EXTENDED;
160 if (ptr->option && !uci_validate_name(ptr->option))
162 if (ptr->value && !uci_validate_text(ptr->value))
168 memset(ptr, 0, sizeof(struct uci_ptr));
169 UCI_THROW(ctx, UCI_ERR_PARSE);
173 __private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
175 struct uci_parse_context *pctx = ctx->pctx;
177 pctx->reason = reason;
178 pctx->byte = pos - pctx->buf;
179 UCI_THROW(ctx, UCI_ERR_PARSE);
185 * open a stream and go to the right position
187 * note: when opening for write and seeking to the beginning of
188 * the stream, truncate the file
190 __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
195 int mode = (write ? O_RDWR : O_RDONLY);
200 if (!write && ((stat(filename, &statbuf) < 0) ||
201 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
202 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
205 fd = open(filename, mode, UCI_FILEMODE);
209 ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
210 if ((ret < 0) && (errno != ENOSYS))
213 ret = lseek(fd, 0, pos);
218 file = fdopen(fd, (write ? "w+" : "r"));
223 UCI_THROW(ctx, UCI_ERR_IO);
228 __private void uci_close_stream(FILE *stream)