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>
32 #include "uci_internal.h"
34 __plugin
void *uci_malloc(struct uci_context
*ctx
, size_t size
)
40 UCI_THROW(ctx
, UCI_ERR_MEM
);
46 __plugin
void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
)
48 ptr
= realloc(ptr
, size
);
50 UCI_THROW(ctx
, UCI_ERR_MEM
);
55 __plugin
char *uci_strdup(struct uci_context
*ctx
, const char *str
)
61 UCI_THROW(ctx
, UCI_ERR_MEM
);
67 * validate strings for names and types, reject special characters
68 * for names, only alphanum and _ is allowed (shell compatibility)
69 * for types, we allow more characters
71 __plugin
bool uci_validate_str(const char *str
, bool name
)
77 unsigned char c
= *str
;
78 if (!isalnum(c
) && c
!= '_') {
79 if (name
|| (c
< 33) || (c
> 126))
87 bool uci_validate_text(const char *str
)
90 unsigned char c
= *str
;
91 if ((c
== '\r') || (c
== '\n') ||
92 ((c
< 32) && (c
!= '\t')))
99 __private
void uci_alloc_parse_context(struct uci_context
*ctx
)
101 ctx
->pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
104 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
)
110 UCI_ASSERT(ctx
, str
);
111 UCI_ASSERT(ctx
, ptr
);
113 memset(ptr
, 0, sizeof(struct uci_ptr
));
116 last
= strchr(str
, '=');
123 ptr
->package
= strsep(&str
, ".");
127 ptr
->section
= strsep(&str
, ".");
129 ptr
->target
= UCI_TYPE_PACKAGE
;
133 ptr
->option
= strsep(&str
, ".");
135 ptr
->target
= UCI_TYPE_SECTION
;
138 ptr
->target
= UCI_TYPE_OPTION
;
141 tmp
= strsep(&str
, ".");
146 if (ptr
->package
&& !uci_validate_package(ptr
->package
))
148 if (ptr
->section
&& !uci_validate_name(ptr
->section
))
149 ptr
->flags
|= UCI_LOOKUP_EXTENDED
;
150 if (ptr
->option
&& !uci_validate_name(ptr
->option
))
152 if (ptr
->value
&& !uci_validate_text(ptr
->value
))
158 memset(ptr
, 0, sizeof(struct uci_ptr
));
159 UCI_THROW(ctx
, UCI_ERR_PARSE
);
163 __private
void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
165 struct uci_parse_context
*pctx
= ctx
->pctx
;
167 pctx
->reason
= reason
;
168 pctx
->byte
= pos
- pctx
->buf
;
169 UCI_THROW(ctx
, UCI_ERR_PARSE
);
175 * open a stream and go to the right position
177 * note: when opening for write and seeking to the beginning of
178 * the stream, truncate the file
180 __private
FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
)
185 int mode
= (write
? O_RDWR
: O_RDONLY
);
190 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
191 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
192 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
195 fd
= open(filename
, mode
, UCI_FILEMODE
);
199 ret
= flock(fd
, (write
? LOCK_EX
: LOCK_SH
));
200 if ((ret
< 0) && (errno
!= ENOSYS
))
203 ret
= lseek(fd
, 0, pos
);
208 file
= fdopen(fd
, (write
? "w+" : "r"));
213 UCI_THROW(ctx
, UCI_ERR_IO
);
218 __private
void uci_close_stream(FILE *stream
)