* for names, only alphanum and _ is allowed (shell compatibility)
* for types, we allow more characters
*/
-__private bool uci_validate_str(const char *str, bool name)
+__private bool uci_validate_str(const char *str, bool name, bool package)
{
if (!*str)
return false;
- while (*str) {
+ for (; *str; str++) {
unsigned char c = *str;
- if (!isalnum(c) && c != '_') {
- if (name || (c < 33) || (c > 126))
- return false;
- }
- str++;
+
+ if (isalnum(c) || c == '_')
+ continue;
+
+ if (c == '-' && package)
+ continue;
+
+ if (name || (c < 33) || (c > 126))
+ return false;
}
return true;
}
{
while (*str) {
unsigned char c = *str;
- if ((c == '\r') || (c == '\n') ||
- ((c < 32) && (c != '\t')))
+
+ if (c < 32 && c != '\t' && c != '\n' && c != '\r')
return false;
+
str++;
}
return true;
}
-__private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
+__private void uci_parse_error(struct uci_context *ctx, char *reason)
{
struct uci_parse_context *pctx = ctx->pctx;
pctx->reason = reason;
- pctx->byte = pos - pctx->buf;
+ pctx->byte = pctx_pos(pctx);
UCI_THROW(ctx, UCI_ERR_PARSE);
}
* note: when opening for write and seeking to the beginning of
* the stream, truncate the file
*/
-__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
+__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
{
struct stat statbuf;
FILE *file = NULL;
if (create) {
flags |= O_CREAT;
- name = basename((char *) filename);
+ if (origfilename) {
+ name = basename((char *) origfilename);
+ } else {
+ name = basename((char *) filename);
+ }
if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
UCI_THROW(ctx, UCI_ERR_MEM);
} else {
ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
if ((ret < 0) && (errno != ENOSYS))
- goto error;
+ goto error_close;
ret = lseek(fd, 0, pos);
if (ret < 0)
- goto error;
+ goto error_unlock;
file = fdopen(fd, (write ? "w+" : "r"));
if (file)
goto done;
+error_unlock:
+ flock(fd, LOCK_UN);
+error_close:
+ close(fd);
error:
UCI_THROW(ctx, UCI_ERR_IO);
done: