Browse Source

The pattern of CONFIG tag bacame a regexp.

git-svn-id: https://klish.googlecode.com/svn/trunk@131 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
069a414a1f
3 changed files with 17 additions and 9 deletions
  1. 1 1
      clish.xsd
  2. 1 1
      clish/shell/shell_tinyxml_read.cpp
  3. 15 7
      konf/tree/tree.c

+ 1 - 1
clish.xsd

@@ -379,7 +379,7 @@
     <xs:complexType name="config_t">
         <xs:attribute name="operation" type="operation_t" use="optional" default="set"/>
         <xs:attribute name="priority" type="xs:string" use="optional" default="0x7f00"/>
-        <xs:attribute name="pattern" type="xs:string" use="optional" default="${cmd}"/>
+        <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:complexType>

+ 1 - 1
clish/shell/shell_tinyxml_read.cpp

@@ -519,7 +519,7 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	if (pattern != NULL)
 		clish_command__set_pattern(cmd, pattern);
 	else
-		clish_command__set_pattern(cmd, "${cmd}");
+		clish_command__set_pattern(cmd, "^${cmd}");
 
 	if (file != NULL)
 		clish_command__set_file(cmd, file);

+ 15 - 7
konf/tree/tree.c

@@ -1,8 +1,9 @@
 /*
- * conf.c
+ * tree.c
  *
- * This file provides the implementation of a conf class
+ * This file provides the implementation of a konf_tree class
  */
+
 #include "private.h"
 #include "lub/argv.h"
 #include "lub/string.h"
@@ -12,6 +13,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <regex.h>
 
 /*---------------------------------------------------------
  * PRIVATE META FUNCTIONS
@@ -22,9 +25,6 @@ int konf_tree_bt_compare(const void *clientnode, const void *clientkey)
 	unsigned short *pri = (unsigned short *)clientkey;
 	char *line = ((char *)clientkey + sizeof(unsigned short));
 
-/*	printf("COMPARE: node_pri=%d node_line=[%s] key_pri=%d key_line=[%s]\n",
-	konf_tree__get_priority(this), this->line, *pri, line);
-*/
 	if (konf_tree__get_priority(this) == *pri)
 		return lub_string_nocasecmp(this->line, line);
 
@@ -118,6 +118,7 @@ void konf_tree_delete(konf_tree_t * this)
 	free(this);
 }
 
+/*--------------------------------------------------------- */
 void konf_tree_fprintf(konf_tree_t * this, FILE * stream,
 		const char *pattern, int depth,
 		unsigned char prev_pri_hi)
@@ -207,18 +208,25 @@ void konf_tree_del_pattern(konf_tree_t *this,
 {
 	konf_tree_t *conf;
 	lub_bintree_iterator_t iter;
+	regex_t regexp;
 
-	/* Empty tree */
+	/* Is tree empty? */
 	if (!(conf = lub_bintree_findfirst(&this->tree)))
 		return;
 
+	/* Compile regular expression */
+	regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
+
+	/* Iterate configuration tree */
 	lub_bintree_iterator_init(&iter, &this->tree, conf);
 	do {
-		if (lub_string_nocasestr(conf->line, pattern) == conf->line) {
+		if (0 == regexec(&regexp, conf->line, 0, NULL, 0)) {
 			lub_bintree_remove(&this->tree, conf);
 			konf_tree_delete(conf);
 		}
 	} while ((conf = lub_bintree_iterator_next(&iter)));
+
+	regfree(&regexp);
 }
 
 /*--------------------------------------------------------- */