Browse Source

Implement non-unique entries. The 'unique' field in CONFIG tag. Fix issue #21.

git-svn-id: https://klish.googlecode.com/svn/trunk@171 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
0b8eb8b9b0

+ 11 - 6
bin/konfd.c

@@ -227,13 +227,18 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 	switch (konf_query__get_op(query)) {
 
 	case KONF_QUERY_OP_SET:
-		if (konf_tree_find_conf(iconf, konf_query__get_line(query), 0, 0)) {
-			ret = KONF_QUERY_OP_OK;
-			break;
+		if (konf_query__get_unique(query)) {
+			if (konf_tree_find_conf(iconf,
+				konf_query__get_line(query), 0, 0)) {
+				ret = KONF_QUERY_OP_OK;
+				break;
+			}
+			konf_tree_del_pattern(iconf,
+				konf_query__get_pattern(query),
+				konf_query__get_priority(query),
+				konf_query__get_seq(query),
+				konf_query__get_seq_num(query));
 		}
-		konf_tree_del_pattern(iconf,
-			konf_query__get_pattern(query), konf_query__get_priority(query),
-			konf_query__get_seq(query), konf_query__get_seq_num(query));
 		tmpconf = konf_tree_new_conf(iconf,
 			konf_query__get_line(query), konf_query__get_priority(query),
 			konf_query__get_seq(query), konf_query__get_seq_num(query));

+ 1 - 0
clish.xsd

@@ -407,6 +407,7 @@
         <xs:attribute name="file" type="xs:string" use="optional" default="startup-config"/>
         <xs:attribute name="splitter" type="bool_t" use="optional" default="true"/>
         <xs:attribute name="sequence" type="xs:string" use="optional" default="0"/>
+        <xs:attribute name="unique" type="bool_t" use="optional" default="true"/>
     </xs:complexType>
 
 

+ 3 - 0
clish/clish_config_callback.c

@@ -69,6 +69,9 @@ clish_config_callback(const clish_shell_t * shell,
 			if (clish_command__get_splitter(cmd) == BOOL_FALSE)
 				lub_string_cat(&command, " -i");
 
+			if (clish_command__get_unique(cmd) == BOOL_FALSE)
+				lub_string_cat(&command, " -n");
+
 			if (clish_command__get_priority(cmd) != 0) {
 				snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
 					clish_command__get_priority(cmd));

+ 2 - 0
clish/command.h

@@ -98,5 +98,7 @@ void clish_command__set_seq(clish_command_t * instance, const char * seq_num);
 unsigned short clish_command__get_seq(const clish_command_t * instance,
 	const char *viewid, clish_pargv_t * pargv);
 clish_view_restore_t clish_command__get_restore(const clish_command_t * instance);
+bool_t clish_command__get_unique(const clish_command_t * instance);
+void clish_command__set_unique(clish_command_t * instance, bool_t unique);
 
 #endif				/* _clish_command_h */

+ 12 - 0
clish/command/command.c

@@ -45,6 +45,7 @@ clish_command_init(clish_command_t * this, const char *name, const char *text)
 	this->file = NULL;
 	this->splitter = BOOL_TRUE;
 	this->seq = NULL;
+	this->unique = BOOL_TRUE;
 }
 
 /*--------------------------------------------------------- */
@@ -528,3 +529,14 @@ clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
 	return clish_view__get_restore(this->pview);
 }
 
+/*--------------------------------------------------------- */
+bool_t clish_command__get_unique(const clish_command_t * this)
+{
+	return this->unique;
+}
+
+/*--------------------------------------------------------- */
+void clish_command__set_unique(clish_command_t * this, bool_t unique)
+{
+	this->unique = unique;
+}

+ 1 - 0
clish/command/private.h

@@ -30,4 +30,5 @@ struct clish_command_s {
 	char *file;
 	bool_t splitter;
 	char *seq;
+	bool_t unique;
 };

+ 10 - 0
clish/shell/shell_tinyxml_read.cpp

@@ -520,6 +520,7 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *file = element->Attribute("file");
 	const char *splitter = element->Attribute("splitter");
 	const char *seq = element->Attribute("sequence");
+	const char *unique = element->Attribute("unique");
 
 	if (operation && !lub_string_nocasecmp(operation, "unset"))
 		clish_command__set_cfg_op(cmd, CLISH_CONFIG_UNSET);
@@ -565,8 +566,17 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	else
 		clish_command__set_splitter(cmd, BOOL_TRUE);
 
+	if (unique && (lub_string_nocasecmp(unique, "false") == 0))
+		clish_command__set_unique(cmd, BOOL_FALSE);
+	else
+		clish_command__set_unique(cmd, BOOL_TRUE);
+
 	if (seq != NULL)
 		clish_command__set_seq(cmd, seq);
+	else
+		/* The entries without sequence cannot be non-unique */
+		clish_command__set_unique(cmd, BOOL_TRUE);
+
 }
 
 ///////////////////////////////////////

+ 1 - 0
konf/query.h

@@ -29,5 +29,6 @@ unsigned short konf_query__get_priority(konf_query_t *instance);
 bool_t konf_query__get_splitter(konf_query_t *instance);
 bool_t konf_query__get_seq(konf_query_t *instance);
 unsigned short konf_query__get_seq_num(konf_query_t *instance);
+bool_t konf_query__get_unique(konf_query_t *instance);
 
 #endif

+ 1 - 0
konf/query/private.h

@@ -15,6 +15,7 @@ struct konf_query_s {
 	char *line;
 	char *path;
 	bool_t splitter;
+	bool_t unique;
 };
 
 #endif

+ 13 - 1
konf/query/query.c

@@ -29,6 +29,7 @@ konf_query_t *konf_query_new(void)
 	query->line = NULL;
 	query->path = NULL;
 	query->splitter = BOOL_TRUE;
+	query->unique = BOOL_TRUE;
 
 	return query;
 }
@@ -65,6 +66,8 @@ void konf_query_dump(konf_query_t *query)
 	printf("line=%s\n", query->line);
 	printf("path=%s\n", query->path);
 	printf("pwdc=%u\n", query->pwdc);
+	printf("splitter : %s\n", query->splitter ? "true" : "false");
+	printf("unique   : %s\n", query->unique ? "true" : "false");
 }
 
 void konf_query_add_pwd(konf_query_t *query, char *str)
@@ -108,7 +111,7 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 	unsigned i = 0;
 	int pwdc = 0;
 
-	static const char *shortopts = "suoedtp:q:r:l:f:i";
+	static const char *shortopts = "suoedtp:q:r:l:f:in";
 /*	static const struct option longopts[] = {
 		{"set",		0, NULL, 's'},
 		{"unset",	0, NULL, 'u'},
@@ -122,6 +125,7 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 		{"line",	1, NULL, 'l'},
 		{"file",	1, NULL, 'f'},
 		{"splitter",	0, NULL, 'i'},
+		{"non-unique",	0, NULL, 'n'},
 		{NULL,		0, NULL, 0}
 	};
 */
@@ -191,6 +195,9 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 		case 'i':
 			query->splitter = BOOL_FALSE;
 			break;
+		case 'n':
+			query->unique = BOOL_FALSE;
+			break;
 		default:
 			break;
 		}
@@ -291,3 +298,8 @@ unsigned short konf_query__get_seq_num(konf_query_t *this)
 {
 	return this->seq_num;
 }
+
+bool_t konf_query__get_unique(konf_query_t *this)
+{
+	return this->unique;
+}