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
, bool package
)
78 unsigned char c
= *str
;
80 if (isalnum(c
) || c
== '_')
83 if (c
== '-' && package
)
86 if (name
|| (c
< 33) || (c
> 126))
92 bool uci_validate_text(const char *str
)
95 unsigned char c
= *str
;
97 if (c
< 32 && c
!= '\t' && c
!= '\n' && c
!= '\r')
105 __private
void uci_alloc_parse_context(struct uci_context
*ctx
)
107 ctx
->pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
110 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
)
116 UCI_ASSERT(ctx
, str
);
117 UCI_ASSERT(ctx
, ptr
);
119 memset(ptr
, 0, sizeof(struct uci_ptr
));
122 last
= strchr(str
, '=');
129 ptr
->package
= strsep(&str
, ".");
133 ptr
->section
= strsep(&str
, ".");
135 ptr
->target
= UCI_TYPE_PACKAGE
;
139 ptr
->option
= strsep(&str
, ".");
141 ptr
->target
= UCI_TYPE_SECTION
;
144 ptr
->target
= UCI_TYPE_OPTION
;
147 tmp
= strsep(&str
, ".");
152 if (ptr
->package
&& !uci_validate_package(ptr
->package
))
154 if (ptr
->section
&& !uci_validate_name(ptr
->section
))
155 ptr
->flags
|= UCI_LOOKUP_EXTENDED
;
156 if (ptr
->option
&& !uci_validate_name(ptr
->option
))
158 if (ptr
->value
&& !uci_validate_text(ptr
->value
))
164 memset(ptr
, 0, sizeof(struct uci_ptr
));
165 UCI_THROW(ctx
, UCI_ERR_PARSE
);
169 __private
void uci_parse_error(struct uci_context
*ctx
, char *reason
)
171 struct uci_parse_context
*pctx
= ctx
->pctx
;
173 pctx
->reason
= reason
;
174 pctx
->byte
= pctx_pos(pctx
);
175 UCI_THROW(ctx
, UCI_ERR_PARSE
);
181 * open a stream and go to the right position
183 * note: when opening for write and seeking to the beginning of
184 * the stream, truncate the file
186 __private
FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, const char *origfilename
, int pos
, bool write
, bool create
)
191 int flags
= (write
? O_RDWR
: O_RDONLY
);
192 mode_t mode
= UCI_FILEMODE
;
194 char *filename2
= NULL
;
199 name
= basename((char *) origfilename
);
201 name
= basename((char *) filename
);
203 if ((asprintf(&filename2
, "%s/%s", ctx
->confdir
, name
) < 0) || !filename2
) {
204 UCI_THROW(ctx
, UCI_ERR_MEM
);
206 if (stat(filename2
, &statbuf
) == 0)
207 mode
= statbuf
.st_mode
;
213 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
214 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
215 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
218 fd
= open(filename
, flags
, mode
);
222 ret
= flock(fd
, (write
? LOCK_EX
: LOCK_SH
));
223 if ((ret
< 0) && (errno
!= ENOSYS
))
226 ret
= lseek(fd
, 0, pos
);
231 file
= fdopen(fd
, (write
? "w+" : "r"));
236 UCI_THROW(ctx
, UCI_ERR_IO
);
241 __private
void uci_close_stream(FILE *stream
)