[lxc-devel] [PATCH 2/3] Change configuration file parser

Clement Calmels clement.calmels at fr.ibm.com
Thu Sep 16 14:05:49 UTC 2010


This change allow end of line comment and IMHO simplify parse_line function.

Signed-off-by: Clement Calmels <clement.calmels at fr.ibm.com>
---

 src/lxc/confile.c |   69 ++++++++++++++++++-----------------------------------
 1 files changed, 23 insertions(+), 46 deletions(-)

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 76fa9ac..91d54a9 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -745,59 +745,36 @@ static int config_utsname(const char *key, char *value, struct lxc_conf *lxc_con
 	return 0;
 }
 
-static int parse_line(char *buffer, void *data)
+static int parse_line(char *line, void *data)
 {
-	struct config *config;
-	char *line, *linep;
-	char *dot;
-	char *key;
-	char *value;
-	int ret = -1;
+	char *key = NULL, *pound;
+	int ret, count = 0;
 
-	if (lxc_is_line_empty(buffer))
-		return 0;
+	pound = index(line, '#');
+	if (pound) /* chomp comment */
+		*pound = '\0';
 
-	/* we have to dup the buffer otherwise, at the re-exec for
-	 * reboot we modified the original string on the stack by
-	 * replacing '=' by '\0' below
-	 */
-	linep = line = strdup(buffer);
-	if (!line) {
-		SYSERROR("failed to allocate memory for '%s'", buffer);
-		return -1;
-	}
-
-	line += lxc_char_left_gc(line, strlen(line));
-	if (line[0] == '#') {
-		ret = 0;
-		goto out;
-	}
+	ret = sscanf(line, " %a[a-z0-9.] = %n", &key, &count);
+	if (ret == EOF)
+		/* empty line */
+		return 0;
 
-	dot = strstr(line, "=");
-	if (!dot) {
+	if (ret == 1) {
+		struct config *config = getconfig(key);
+		char *value = &line[count];
+
+		if (!config) {
+			ret = -1;
+			ERROR("unknow key %s", key);
+		} else
+			ret = config->cb(key, value, data);
+	} else {
+		ret = -1;
 		ERROR("invalid configuration line: %s", line);
-		goto out;
 	}
 
-	*dot = '\0';
-	value = dot + 1;
-
-	key = line;
-	key[lxc_char_right_gc(key, strlen(key))] = '\0';
-
-	value += lxc_char_left_gc(value, strlen(value));
-	value[lxc_char_right_gc(value, strlen(value))] = '\0';
-
-	config = getconfig(key);
-	if (!config) {
-		ERROR("unknow key %s", key);
-		goto out;
-	}
-
-	ret = config->cb(key, value, data);
-
-out:
-	free(linep);
+	if (key)
+		free(key);
 	return ret;
 }
 





More information about the lxc-devel mailing list