Browse Source

The sequence and sequence_num params for CONFIG. Unfinished.

git-svn-id: https://klish.googlecode.com/svn/trunk@138 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
e2a7348328
9 changed files with 146 additions and 27 deletions
  1. 2 2
      bin/konfd.c
  2. 2 0
      clish.xsd
  3. 33 2
      clish/clish_config_callback.c
  4. 4 0
      clish/command.h
  5. 27 0
      clish/command/command.c
  6. 6 0
      clish/command/private.h
  7. 27 0
      clish/shell/shell_tinyxml_read.cpp
  8. 3 5
      konf/tree.h
  9. 42 18
      konf/tree/tree.c

+ 2 - 2
bin/konfd.c

@@ -212,7 +212,7 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 	for (i = 0; i < konf_query__get_pwdc(query); i++) {
 		if (!
 		    (iconf =
-		     konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0))) {
+		     konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0, 0))) {
 			iconf = NULL;
 			break;
 		}
@@ -227,7 +227,7 @@ 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)) {
+		if (konf_tree_find_conf(iconf, konf_query__get_line(query), 0, 0)) {
 			ret = KONF_QUERY_OP_OK;
 			break;
 		}

+ 2 - 0
clish.xsd

@@ -382,6 +382,8 @@
         <xs:attribute name="pattern" type="xs:string" use="optional" default="^${cmd}"/>
         <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="bool_t" use="optional" default="false"/>
+        <xs:attribute name="sequence_num" type="xs:string" use="optional" default="0"/>
     </xs:complexType>
 
 

+ 33 - 2
clish/clish_config_callback.c

@@ -63,13 +63,22 @@ clish_config_callback(const clish_shell_t * shell,
 			lub_string_free(pattern);
 
 			if (clish_command__get_splitter(cmd) == BOOL_FALSE)
-				lub_string_cat(&command, " -i");
 
 			snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
-				 clish_command__get_priority(cmd));
+				clish_command__get_priority(cmd));
 			tmp[sizeof(tmp) - 1] = '\0';
 			lub_string_cat(&command, tmp);
 
+			if (clish_command__get_seq(cmd) == BOOL_TRUE) {
+				lub_string_cat(&command, " -q");
+				if (clish_command__get_seq_num(cmd) != 0) {
+					snprintf(tmp, sizeof(tmp) - 1, " -n %u",
+						clish_command__get_seq_num(cmd));
+					tmp[sizeof(tmp) - 1] = '\0';
+					lub_string_cat(&command, tmp);
+				}
+			}
+
 			for (i = 0; i < clish_command__get_depth(cmd); i++) {
 				const char *str =
 				    clish_shell__get_pwd(shell, i);
@@ -99,6 +108,16 @@ clish_config_callback(const clish_shell_t * shell,
 			lub_string_cat(&command, "\"");
 			lub_string_free(pattern);
 
+			if (clish_command__get_seq(cmd) == BOOL_TRUE) {
+				lub_string_cat(&command, " -q");
+				if (clish_command__get_seq_num(cmd) != 0) {
+					snprintf(tmp, sizeof(tmp) - 1, " -n %u",
+						clish_command__get_seq_num(cmd));
+					tmp[sizeof(tmp) - 1] = '\0';
+					lub_string_cat(&command, tmp);
+				}
+			}
+
 			for (i = 0; i < clish_command__get_depth(cmd); i++) {
 				const char *str =
 				    clish_shell__get_pwd(shell, i);
@@ -114,6 +133,7 @@ clish_config_callback(const clish_shell_t * shell,
 	case CLISH_CONFIG_DUMP:
 		{
 			char *file;
+			char tmp[100];
 
 			lub_string_cat(&command, "-d");
 
@@ -126,6 +146,17 @@ clish_config_callback(const clish_shell_t * shell,
 					lub_string_cat(&command, "/tmp/running-config");
 				lub_string_cat(&command, "\"");
 			}
+
+			if (clish_command__get_seq(cmd) == BOOL_TRUE) {
+				lub_string_cat(&command, " -q");
+				if (clish_command__get_seq_num(cmd) != 0) {
+					snprintf(tmp, sizeof(tmp) - 1, " -n %u",
+						clish_command__get_seq_num(cmd));
+					tmp[sizeof(tmp) - 1] = '\0';
+					lub_string_cat(&command, tmp);
+				}
+			}
+
 			break;
 		}
 

+ 4 - 0
clish/command.h

@@ -93,5 +93,9 @@ char *clish_command__get_file(const clish_command_t * instance,
 			      clish_pargv_t * pargv);
 void clish_command__set_splitter(clish_command_t * instance, bool_t splitter);
 bool_t clish_command__get_splitter(const clish_command_t * instance);
+bool_t clish_command__get_seq(const clish_command_t * instance);
+void clish_command__set_seq(clish_command_t * instance, bool_t seq);
+void clish_command__set_seq_num(clish_command_t * instance, unsigned short seq_num);
+unsigned short clish_command__get_seq_num(const clish_command_t * instance);
 
 #endif				/* _clish_command_h */

+ 27 - 0
clish/command/command.c

@@ -37,11 +37,15 @@ clish_command_init(clish_command_t * this, const char *name, const char *text)
 	this->escape_chars = NULL;
 	this->args = NULL;
 	this->pview = NULL;
+	
+	/* CONFIG params */
 	this->cfg_op = CLISH_CONFIG_NONE;
 	this->priority = 0x7f00; /* medium priority by default */
 	this->pattern = NULL;
 	this->file = NULL;
 	this->splitter = BOOL_TRUE;
+	this->seq = BOOL_FALSE;
+	this->seq_num = 0;
 }
 
 /*--------------------------------------------------------- */
@@ -472,4 +476,27 @@ void clish_command__set_splitter(clish_command_t * this, bool_t splitter)
 	this->splitter = splitter;
 }
 
+/*--------------------------------------------------------- */
+bool_t clish_command__get_seq(const clish_command_t * this)
+{
+	return this->seq;
+}
+
+/*--------------------------------------------------------- */
+void clish_command__set_seq(clish_command_t * this, bool_t seq)
+{
+	this->seq = seq;
+}
+
+/*--------------------------------------------------------- */
+void clish_command__set_seq_num(clish_command_t * this, unsigned short seq_num)
+{
+	this->seq_num = seq_num;
+}
+
+/*--------------------------------------------------------- */
+unsigned short clish_command__get_seq_num(const clish_command_t * this)
+{
+	return this->seq_num;
+}
 

+ 6 - 0
clish/command/private.h

@@ -20,9 +20,15 @@ struct clish_command_s {
 	clish_param_t *args;
 	bool_t link;
 	clish_view_t *pview;
+
+	/* CONFIG params:
+	 * TODO: create special structure for CONFIG params.
+	 */
 	clish_config_operation_t cfg_op;
 	unsigned short priority;
 	char *pattern;
 	char *file;
 	bool_t splitter;
+	bool_t seq;
+	unsigned short seq_num;
 };

+ 27 - 0
clish/shell/shell_tinyxml_read.cpp

@@ -487,6 +487,8 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *pattern = element->Attribute("pattern");
 	const char *file = element->Attribute("file");
 	const char *splitter = element->Attribute("splitter");
+	const char *seq = element->Attribute("sequence");
+	const char *seq_num = element->Attribute("sequence_num");
 
 	if (operation && !lub_string_nocasecmp(operation, "unset"))
 		clish_command__set_cfg_op(cmd, CLISH_CONFIG_UNSET);
@@ -529,6 +531,31 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	else
 		clish_command__set_splitter(cmd, BOOL_TRUE);
 
+	if (seq && (lub_string_nocasecmp(seq, "true") == 0))
+		clish_command__set_seq(cmd, BOOL_TRUE);
+	else
+		clish_command__set_seq(cmd, BOOL_FALSE);
+
+	if ((seq_num != NULL) && (*seq_num != '\0')) {
+		long val = 0;
+		char *endptr;
+		unsigned short pri;
+
+		val = strtol(seq_num, &endptr, 0);
+		if (endptr == seq_num)
+			pri = 0;
+		else if (val > 0xffff)
+			pri = 0xffff;
+		else if (val < 0)
+			pri = 0;
+		else
+			pri = (unsigned short)val;
+		if (0 != pri) {
+			clish_command__set_seq(cmd, BOOL_TRUE);
+			clish_command__set_seq_num(cmd, pri);
+		}
+	}
+
 }
 
 ///////////////////////////////////////

+ 3 - 5
konf/tree.h

@@ -37,15 +37,13 @@ size_t konf_tree_bt_offset(void);
  *----------------- */
 void konf_tree_delete(konf_tree_t * instance);
 void konf_tree_fprintf(konf_tree_t * instance, FILE * stream,
-		const char *pattern,
-		int depth, unsigned char prev_pri_hi);
+	const char *pattern, int depth, unsigned char prev_pri_hi);
 konf_tree_t *konf_tree_new_conf(konf_tree_t * instance,
 	const char *line, unsigned short priority,
 	bool_t seq, unsigned short seq_num, unsigned short seq_step);
 konf_tree_t *konf_tree_find_conf(konf_tree_t * instance,
-				const char *line, unsigned short priority);
-void konf_tree_del_pattern(konf_tree_t *this,
-				const char *pattern);
+	const char *line, unsigned short priority, unsigned short sequence);
+void konf_tree_del_pattern(konf_tree_t *this, const char *pattern);
 
 /*-----------------
  * attributes 

+ 42 - 18
konf/tree/tree.c

@@ -23,23 +23,31 @@ int konf_tree_bt_compare(const void *clientnode, const void *clientkey)
 {
 	const konf_tree_t *this = clientnode;
 	unsigned short *pri = (unsigned short *)clientkey;
+	unsigned short *seq = (unsigned short *)clientkey + 1;
 	char *line = ((char *)clientkey + sizeof(unsigned short));
 
-	if (konf_tree__get_priority(this) == *pri)
-		return lub_string_nocasecmp(this->line, line);
-
-	return (konf_tree__get_priority(this) - *pri);
+	/* Priority check */
+	if (this->priority != *pri)
+		return (this->priority - *pri);
+	/* Sequence check */
+	if (this->seq_num != *seq)
+		return (this->seq_num - *seq);
+	/* Line check */
+	return lub_string_nocasecmp(this->line, line);
 }
 
 /*-------------------------------------------------------- */
 static void konf_tree_key(lub_bintree_key_t * key,
-	unsigned short priority, const char *text)
+	unsigned short priority, unsigned short sequence,
+	const char *text)
 {
 	unsigned short *pri = (unsigned short *)key;
-	char *line = ((char *)key + sizeof(unsigned short));
+	unsigned short *seq = (unsigned short *)key + 1;
+	char *line = ((char *)key + (2 * sizeof(unsigned short)));
 
 	/* fill out the opaque key */
 	*pri = priority;
+	*seq = sequence;
 	strcpy(line, text);
 }
 
@@ -48,7 +56,7 @@ void konf_tree_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
 	const konf_tree_t *this = clientnode;
 
-	konf_tree_key(key, konf_tree__get_priority(this), this->line);
+	konf_tree_key(key, this->priority, this->seq_num, this->line);
 }
 
 /*---------------------------------------------------------
@@ -163,33 +171,49 @@ konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
 	const char *line, unsigned short priority,
 	bool_t seq, unsigned short seq_num, unsigned short seq_step)
 {
+	konf_tree_t *conf;
 	/* Allocate the memory for a new child element */
-	konf_tree_t *conf = konf_tree_new(line, priority);
-	assert(conf);
+	konf_tree_t *newconf = konf_tree_new(line, priority);
+	assert(newconf);
+
+	/* Sequence */
+	if (seq) {
+		konf_tree__set_seq_num(newconf, seq_num);
+		/* If tree is empty */
+/*		conf = lub_bintree_findfirst(&this->tree);
+			konf_tree__set_seq_num(newconf, seq_num);
+			return NULL;
+		else {
+		
+		}
+*/
+	}
 
-	if (seq)
-		konf_tree__set_seq_num(conf, seq_num);
+	/* Non empty tree */
+	if (seq && conf) {
+
+	}
 
 	/* Insert it into the binary tree for this conf */
-	if (-1 == lub_bintree_insert(&this->tree, conf)) {
+	if (-1 == lub_bintree_insert(&this->tree, newconf)) {
 		/* inserting a duplicate command is bad */
-		konf_tree_delete(conf);
-		conf = NULL;
+		konf_tree_delete(newconf);
+		newconf = NULL;
 	}
 
-	return conf;
+	return newconf;
 }
 
 /*--------------------------------------------------------- */
 konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
-	const char *line, unsigned short priority)
+	const char *line, unsigned short priority, unsigned short sequence)
 {
 	konf_tree_t *conf;
 	lub_bintree_key_t key;
 	lub_bintree_iterator_t iter;
 
-	if (0 != priority) {
-		konf_tree_key(&key, priority, line);
+	if ((0 != priority) && (0 != sequence)) {
+		konf_tree_key(&key, priority, sequence, line);
 		return lub_bintree_find(&this->tree, &key);
 	}