summaryrefslogtreecommitdiff
path: root/sys/config
diff options
context:
space:
mode:
authorMarko Kiiskila <marko@runtime.io>2016-03-25 14:40:11 -0700
committerMarko Kiiskila <marko@runtime.io>2016-03-28 17:37:31 -0700
commit8e000c7e84a95ce149d02e927210d69a3eaef4ca (patch)
tree37725b8592ef67f83d8a69f6a80a5cbc0b3e6701 /sys/config
parent313e9404fe5046f02d4ef876da9a6a6f02bdbac6 (diff)
Separate line parsing/generation for conf files to separate file.
Rename json parser function.
Diffstat (limited to 'sys/config')
-rw-r--r--sys/config/src/config_file.c81
-rw-r--r--sys/config/src/config_json_line.c2
-rw-r--r--sys/config/src/config_line.c105
-rw-r--r--sys/config/src/config_priv.h6
4 files changed, 127 insertions, 67 deletions
diff --git a/sys/config/src/config_file.c b/sys/config/src/config_file.c
index 42d258c0..faeef1f8 100644
--- a/sys/config/src/config_file.c
+++ b/sys/config/src/config_file.c
@@ -21,7 +21,6 @@
#include <string.h>
#include <assert.h>
-#include <ctype.h>
#include <os/os.h>
#include <fs/fs.h>
@@ -31,10 +30,10 @@
#include "config_priv.h"
static int conf_file_load(struct conf_store *, load_cb cb, void *cb_arg);
-static int conf_file_save_start(struct conf_store *cs);
-static int conf_file_save(struct conf_store *cs, struct conf_handler *ch,
+static int conf_file_save_start(struct conf_store *);
+static int conf_file_save(struct conf_store *, struct conf_handler *ch,
char *name, char *value);
-static int conf_file_save_end(struct conf_store *cs);
+static int conf_file_save_end(struct conf_store *);
static struct conf_store_itf conf_file_itf = {
.csi_load = conf_file_load,
@@ -104,53 +103,6 @@ conf_getnext_line(struct fs_file *file, char *buf, int blen, uint32_t *loc)
return blen;
}
-static int
-conf_file_line(char *buf, char **namep, char **valp)
-{
- char *cp;
- enum {
- FIND_NAME,
- FIND_NAME_END,
- FIND_VAL,
- FIND_VAL_END
- } state = FIND_NAME;
-
- for (cp = buf; *cp != '\0'; cp++) {
- switch (state) {
- case FIND_NAME:
- if (!isspace(*cp)) {
- *namep = cp;
- state = FIND_NAME_END;
- }
- break;
- case FIND_NAME_END:
- if (*cp == '=') {
- *cp = '\0';
- state = FIND_VAL;
- } else if (isspace(*cp)) {
- *cp = '\0';
- }
- break;
- case FIND_VAL:
- if (!isspace(*cp)) {
- *valp = cp;
- state = FIND_VAL_END;
- }
- break;
- case FIND_VAL_END:
- if (isspace(*cp)) {
- *cp = '\0';
- }
- break;
- }
- }
- if (state == FIND_VAL_END) {
- return 0;
- } else {
- return -1;
- }
-}
-
/*
* Called to load configuration items. cb must be called for every configuration
* item found.
@@ -180,7 +132,7 @@ conf_file_load(struct conf_store *cs, load_cb cb, void *cb_arg)
if (rc < 0) {
continue;
}
- rc = conf_file_line(tmpbuf, &name_str, &val_str);
+ rc = conf_line_parse(tmpbuf, &name_str, &val_str);
if (rc != 0) {
continue;
}
@@ -231,22 +183,16 @@ conf_file_save(struct conf_store *cs, struct conf_handler *ch,
{
struct conf_file *cf = (struct conf_file *)cs;
char tmpbuf[CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32];
- int len;
int off;
- len = strlen(ch->ch_name);
- memcpy(tmpbuf, ch->ch_name, len);
- off = len;
- tmpbuf[off++] = '/';
-
- len = strlen(name);
- memcpy(tmpbuf + off, name, len);
- off += len;
- tmpbuf[off++] = '=';
+ if (!name) {
+ return OS_INVALID_PARM;
+ }
- len = strlen(value);
- memcpy(tmpbuf + off, value, len);
- off += len;
+ off = conf_line_make(tmpbuf, sizeof(tmpbuf), ch, name, value);
+ if (off < 0 || off + 2 > sizeof(tmpbuf)) {
+ return OS_INVALID_PARM;
+ }
tmpbuf[off++] = '\n';
if (fs_write(cf->cf_save_fp, tmpbuf, off)) {
@@ -273,6 +219,11 @@ conf_file_save_end(struct conf_store *cs)
if (rc && rc != FS_ENOENT) {
return OS_EINVAL;
}
+
+ /*
+ * XXX at conf_file_load(), look for .tm2/.tmp if actual file does not
+ * exist.
+ */
rc = fs_rename(name2, cf->cf_name);
if (rc) {
return OS_EINVAL;
diff --git a/sys/config/src/config_json_line.c b/sys/config/src/config_json_line.c
index 9d51127a..d8dc7450 100644
--- a/sys/config/src/config_json_line.c
+++ b/sys/config/src/config_json_line.c
@@ -24,7 +24,7 @@
#include <json/json.h>
int
-conf_parse_line(struct json_buffer *jb, char *name, int nlen, char *value,
+conf_json_line(struct json_buffer *jb, char *name, int nlen, char *value,
int vlen)
{
const struct json_attr_t val_attr[3] = {
diff --git a/sys/config/src/config_line.c b/sys/config/src/config_line.c
new file mode 100644
index 00000000..32270381
--- /dev/null
+++ b/sys/config/src/config_line.c
@@ -0,0 +1,105 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <ctype.h>
+#include <string.h>
+
+#include "config/config.h"
+#include "config_priv.h"
+
+int
+conf_line_parse(char *buf, char **namep, char **valp)
+{
+ char *cp;
+ enum {
+ FIND_NAME,
+ FIND_NAME_END,
+ FIND_VAL,
+ FIND_VAL_END
+ } state = FIND_NAME;
+
+ *valp = NULL;
+ for (cp = buf; *cp != '\0'; cp++) {
+ switch (state) {
+ case FIND_NAME:
+ if (!isspace(*cp)) {
+ *namep = cp;
+ state = FIND_NAME_END;
+ }
+ break;
+ case FIND_NAME_END:
+ if (*cp == '=') {
+ *cp = '\0';
+ state = FIND_VAL;
+ } else if (isspace(*cp)) {
+ *cp = '\0';
+ }
+ break;
+ case FIND_VAL:
+ if (!isspace(*cp)) {
+ *valp = cp;
+ state = FIND_VAL_END;
+ }
+ break;
+ case FIND_VAL_END:
+ if (isspace(*cp)) {
+ *cp = '\0';
+ }
+ break;
+ }
+ }
+ if (state == FIND_VAL_END || state == FIND_VAL) {
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+int
+conf_line_make(char *dst, int dlen, struct conf_handler *ch, char *name,
+ char *value)
+{
+ int clen;
+ int nlen;
+ int vlen;
+ int off;
+
+ clen = strlen(ch->ch_name);
+ nlen = strlen(name);
+ if (value) {
+ vlen = strlen(value);
+ } else {
+ vlen = 0;
+ }
+ if (clen + nlen + vlen + 2 > dlen) {
+ return -1;
+ }
+ memcpy(dst, ch->ch_name, clen);
+ off = clen;
+ dst[off++] = '/';
+
+ memcpy(dst + off, name, nlen);
+ off += nlen;
+ dst[off++] = '=';
+
+ memcpy(dst + off, value, vlen);
+ off += vlen;
+
+ return off;
+}
diff --git a/sys/config/src/config_priv.h b/sys/config/src/config_priv.h
index bbe1b8bf..0354efa5 100644
--- a/sys/config/src/config_priv.h
+++ b/sys/config/src/config_priv.h
@@ -24,9 +24,13 @@ int conf_cli_register(void);
int conf_nmgr_register(void);
struct json_buffer;
-int conf_parse_line(struct json_buffer *jb, char *name, int nlen, char *value,
+int conf_json_line(struct json_buffer *jb, char *name, int nlen, char *value,
int vlen);
+int conf_line_parse(char *buf, char **namep, char **valp);
+int conf_line_make(char *dst, int dlen, struct conf_handler *ch, char *name,
+ char *val);
+
/*
* API for config storage.
*/