Browse Source

Delete entries using sequence_number

git-svn-id: https://klish.googlecode.com/svn/trunk@166 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
1b7517dfad
4 changed files with 42 additions and 13 deletions
  1. 6 2
      bin/konfd.c
  2. 6 0
      clish/clish_config_callback.c
  3. 3 1
      konf/tree.h
  4. 27 10
      konf/tree/tree.c

+ 6 - 2
bin/konfd.c

@@ -231,7 +231,9 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 			ret = KONF_QUERY_OP_OK;
 			break;
 		}
-		konf_tree_del_pattern(iconf, konf_query__get_pattern(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));
@@ -244,7 +246,9 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 		break;
 
 	case KONF_QUERY_OP_UNSET:
-		konf_tree_del_pattern(iconf, konf_query__get_pattern(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));
 		ret = KONF_QUERY_OP_OK;
 		break;
 

+ 6 - 0
clish/clish_config_callback.c

@@ -116,6 +116,12 @@ clish_config_callback(const clish_shell_t * shell,
 			lub_string_free(pattern);
 
 			if (clish_command__get_seq(cmd) == BOOL_TRUE) {
+	                        /* Send priority too */
+				snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
+					clish_command__get_priority(cmd));
+				tmp[sizeof(tmp) - 1] = '\0';
+				lub_string_cat(&command, tmp);
+
 				lub_string_cat(&command, " -q");
 				if (clish_command__get_seq_num(cmd,
 					viewid, pargv) != 0) {

+ 3 - 1
konf/tree.h

@@ -47,7 +47,9 @@ konf_tree_t *konf_tree_new_conf(konf_tree_t * instance,
 	bool_t seq, unsigned short seq_num);
 konf_tree_t *konf_tree_find_conf(konf_tree_t * instance,
 	const char *line, unsigned short priority, unsigned short sequence);
-void konf_tree_del_pattern(konf_tree_t *this, const char *pattern);
+int konf_tree_del_pattern(konf_tree_t * instance,
+	const char *pattern, unsigned short priority,
+	bool_t seq, unsigned short seq_num);
 
 /*-----------------
  * attributes 

+ 27 - 10
konf/tree/tree.c

@@ -254,14 +254,14 @@ konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
 
 /*--------------------------------------------------------- */
 konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
-	const char *line, unsigned short priority, unsigned short sequence)
+	const char *line, unsigned short priority, unsigned short seq_num)
 {
 	konf_tree_t *conf;
 	lub_bintree_key_t key;
 	lub_bintree_iterator_t iter;
 
-	if ((0 != priority) && (0 != sequence)) {
-		konf_tree_key(&key, priority, sequence,
+	if ((0 != priority) && (0 != seq_num)) {
+		konf_tree_key(&key, priority, seq_num,
 			KONF_ENTRY_OK, line);
 		return lub_bintree_find(&this->tree, &key);
 	}
@@ -281,16 +281,21 @@ konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
 }
 
 /*--------------------------------------------------------- */
-void konf_tree_del_pattern(konf_tree_t *this,
-	const char *pattern)
+int konf_tree_del_pattern(konf_tree_t *this,
+	const char *pattern, unsigned short priority,
+	bool_t seq, unsigned short seq_num)
 {
 	konf_tree_t *conf;
 	lub_bintree_iterator_t iter;
 	regex_t regexp;
+	int del_cnt = 0; /* how many strings were deleted */
+
+	if (seq && (0 == priority))
+		return -1;
 
 	/* Is tree empty? */
 	if (!(conf = lub_bintree_findfirst(&this->tree)))
-		return;
+		return 0;
 
 	/* Compile regular expression */
 	regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
@@ -298,13 +303,25 @@ void konf_tree_del_pattern(konf_tree_t *this,
 	/* Iterate configuration tree */
 	lub_bintree_iterator_init(&iter, &this->tree, conf);
 	do {
-		if (0 == regexec(&regexp, conf->line, 0, NULL, 0)) {
-			lub_bintree_remove(&this->tree, conf);
-			konf_tree_delete(conf);
-		}
+		if (0 != regexec(&regexp, conf->line, 0, NULL, 0))
+			continue;
+		if ((0 != priority) &&
+			(priority != conf->priority))
+			continue;
+		if (seq && (seq_num != 0) &&
+			(seq_num != conf->seq_num))
+			continue;
+		lub_bintree_remove(&this->tree, conf);
+		konf_tree_delete(conf);
+		del_cnt++;
 	} while ((conf = lub_bintree_iterator_next(&iter)));
 
 	regfree(&regexp);
+
+	if (seq && (del_cnt != 0))
+		normalize_seq(this, priority);
+
+	return 0;
 }
 
 /*--------------------------------------------------------- */