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 wrappers to standard functions, which
17 * throw exceptions upon failure.
19 #include <sys/types.h>
28 #define LINEBUF_MAX 4096
30 static void *uci_malloc(struct uci_context
*ctx
, size_t size
)
36 UCI_THROW(ctx
, UCI_ERR_MEM
);
42 static void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
)
44 ptr
= realloc(ptr
, size
);
46 UCI_THROW(ctx
, UCI_ERR_MEM
);
51 static char *uci_strdup(struct uci_context
*ctx
, const char *str
)
57 UCI_THROW(ctx
, UCI_ERR_MEM
);
62 static bool uci_validate_name(const char *str
)
68 if (!isalnum(*str
) && (*str
!= '_'))
75 int uci_parse_tuple(struct uci_context
*ctx
, char *str
, char **package
, char **section
, char **option
, char **value
)
80 UCI_ASSERT(ctx
, str
&& package
&& section
&& option
);
82 *package
= strtok(str
, ".");
83 if (!*package
|| !uci_validate_name(*package
))
87 *section
= strtok(NULL
, ".");
92 *option
= strtok(NULL
, ".");
99 last
= strchr(last
, '=');
111 if (*section
&& !uci_validate_name(*section
))
113 if (*option
&& !uci_validate_name(*option
))
119 UCI_THROW(ctx
, UCI_ERR_PARSE
);
126 static void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
128 struct uci_parse_context
*pctx
= ctx
->pctx
;
130 pctx
->reason
= reason
;
131 pctx
->byte
= pos
- pctx
->buf
;
132 UCI_THROW(ctx
, UCI_ERR_PARSE
);
137 * Fetch a new line from the input stream and resize buffer if necessary
139 static void uci_getln(struct uci_context
*ctx
, int offset
)
141 struct uci_parse_context
*pctx
= ctx
->pctx
;
145 if (pctx
->buf
== NULL
) {
146 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
147 pctx
->bufsz
= LINEBUF
;
155 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
160 if (pctx
->buf
[ofs
- 1] == '\n') {
162 pctx
->buf
[ofs
- 1] = 0;
166 if (pctx
->bufsz
> LINEBUF_MAX
/2)
167 uci_parse_error(ctx
, p
, "line too long");
170 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
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 static 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 if (flock(fd
, (write
? LOCK_EX
: LOCK_SH
)) < 0)
202 ret
= lseek(fd
, 0, pos
);
207 file
= fdopen(fd
, (write
? "w+" : "r"));
212 UCI_THROW(ctx
, UCI_ERR_IO
);
217 static void uci_close_stream(FILE *stream
)