Browse Source

Implement sequences in config. Unfinished.

git-svn-id: https://klish.googlecode.com/svn/trunk@137 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
3996b6ea93
7 changed files with 65 additions and 10 deletions
  1. 3 2
      bin/konfd.c
  2. 2 0
      konf/query.h
  3. 2 0
      konf/query/private.h
  4. 33 4
      konf/query/query.c
  5. 4 1
      konf/tree.h
  6. 1 0
      konf/tree/private.h
  7. 20 3
      konf/tree/tree.c

+ 3 - 2
bin/konfd.c

@@ -232,8 +232,9 @@ static char * process_query(int sock, konf_tree_t * conf, char *str)
 			break;
 		}
 		konf_tree_del_pattern(iconf, konf_query__get_pattern(query));
-		tmpconf = konf_tree_new_conf(iconf, 
-			konf_query__get_line(query), konf_query__get_priority(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);
 		if (!tmpconf) {
 			ret = KONF_QUERY_OP_ERROR;
 			break;

+ 2 - 0
konf/query.h

@@ -27,5 +27,7 @@ const char * konf_query__get_pattern(konf_query_t *instance);
 const char * konf_query__get_line(konf_query_t *instance);
 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);
 
 #endif

+ 2 - 0
konf/query/private.h

@@ -8,6 +8,8 @@ struct konf_query_s {
 	konf_query_op_t op;
 	char *pattern;
 	unsigned short priority;
+	bool_t seq; /* sequence aka auto priority */
+	unsigned short seq_num; /* sequence number */
 	unsigned pwdc;
 	char **pwd;
 	char *line;

+ 33 - 4
konf/query/query.c

@@ -22,6 +22,8 @@ konf_query_t *konf_query_new(void)
 	query->op = KONF_QUERY_OP_NONE;
 	query->pattern = NULL;
 	query->priority = 0x7f00;
+	query->seq = BOOL_FALSE;
+	query->seq_num = 0;
 	query->pwdc = 0;
 	query->pwd = NULL;
 	query->line = NULL;
@@ -58,6 +60,7 @@ void konf_query_dump(konf_query_t *query)
 	}
 	printf("pattern=%s\n", query->pattern);
 	printf("priority=%u\n", query->priority);
+	printf("sequence=%u\n", query->seq);
 	printf("line=%s\n", query->line);
 	printf("path=%s\n", query->path);
 	printf("pwdc=%u\n", query->pwdc);
@@ -104,7 +107,7 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 	unsigned i = 0;
 	int pwdc = 0;
 
-	static const char *shortopts = "suoedtp:r:l:f:i";
+	static const char *shortopts = "suoedtp:qn:r:l:f:i";
 /*	static const struct option longopts[] = {
 		{"set",		0, NULL, 's'},
 		{"unset",	0, NULL, 'u'},
@@ -113,6 +116,8 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 		{"dump",	0, NULL, 'd'},
 		{"stream",	0, NULL, 't'},
 		{"priority",	1, NULL, 'p'},
+		{"seq",		0, NULL, 'q'},
+		{"seq_num",	1, NULL, 'n'},
 		{"pattern",	1, NULL, 'r'},
 		{"line",	1, NULL, 'l'},
 		{"file",	1, NULL, 'f'},
@@ -151,15 +156,29 @@ int konf_query_parse(konf_query_t *query, int argc, char **argv)
 			{
 			long val = 0;
 			char *endptr;
-			unsigned short pri;
 
 			val = strtol(optarg, &endptr, 0);
 			if (endptr == optarg)
 				break;
 			if ((val > 0xffff) || (val < 0))
 				break;
-			pri = (unsigned short)val;
-			query->priority = pri;
+			query->priority = (unsigned short)val;
+			break;
+			}
+		case 'q':
+			query->seq = BOOL_TRUE;
+			break;
+		case 'n':
+			{
+			long val = 0;
+			char *endptr;
+
+			val = strtol(optarg, &endptr, 0);
+			if (endptr == optarg)
+				break;
+			if ((val > 0xffff) || (val < 0))
+				break;
+			query->seq_num = (unsigned short)val;
 			break;
 			}
 		case 'r':
@@ -264,3 +283,13 @@ bool_t konf_query__get_splitter(konf_query_t *this)
 {
 	return this->splitter;
 }
+
+bool_t konf_query__get_seq(konf_query_t *this)
+{
+	return this->seq;
+}
+
+unsigned short konf_query__get_seq_num(konf_query_t *this)
+{
+	return this->seq_num;
+}

+ 4 - 1
konf/tree.h

@@ -40,7 +40,8 @@ void konf_tree_fprintf(konf_tree_t * instance, FILE * stream,
 		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);
+	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,
@@ -55,6 +56,8 @@ unsigned char konf_tree__get_priority_hi(const konf_tree_t * instance);
 unsigned char konf_tree__get_priority_lo(const konf_tree_t * instance);
 bool_t konf_tree__get_splitter(const konf_tree_t * instance);
 void konf_tree__set_splitter(konf_tree_t *instance, bool_t splitter);
+unsigned short konf_tree__get_seq_num(const konf_tree_t * instance);
+void konf_tree__set_seq_num(konf_tree_t * instance, unsigned short seq_num);
 
 #endif				/* _konf_tree_h */
 /** @} clish_conf */

+ 1 - 0
konf/tree/private.h

@@ -16,6 +16,7 @@ struct konf_tree_s {
 	lub_bintree_node_t bt_node;
 	char *line;
 	unsigned short priority;
+	unsigned short seq_num;
 	bool_t splitter;
 };
 

+ 20 - 3
konf/tree/tree.c

@@ -60,6 +60,7 @@ konf_tree_init(konf_tree_t * this, const char *line, unsigned short priority)
 	/* set up defaults */
 	this->line = lub_string_dup(line);
 	this->priority = priority;
+	this->seq_num = 0;
 	this->splitter = BOOL_TRUE;
 
 	/* Be a good binary tree citizen */
@@ -159,13 +160,17 @@ void konf_tree_fprintf(konf_tree_t * this, FILE * stream,
 
 /*--------------------------------------------------------- */
 konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
-					const char *line, unsigned short priority)
+	const char *line, unsigned short priority,
+	bool_t seq, unsigned short seq_num, unsigned short seq_step)
 {
-	/* allocate the memory for a new child element */
+	/* Allocate the memory for a new child element */
 	konf_tree_t *conf = konf_tree_new(line, priority);
 	assert(conf);
 
-	/* ...insert it into the binary tree for this conf */
+	if (seq)
+		konf_tree__set_seq_num(conf, seq_num);
+
+	/* Insert it into the binary tree for this conf */
 	if (-1 == lub_bintree_insert(&this->tree, conf)) {
 		/* inserting a duplicate command is bad */
 		konf_tree_delete(conf);
@@ -258,3 +263,15 @@ void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
 {
 	this->splitter = splitter;
 }
+
+/*--------------------------------------------------------- */
+unsigned short konf_tree__get_seq_num(const konf_tree_t * this)
+{
+	return this->seq_num;
+}
+
+/*--------------------------------------------------------- */
+void konf_tree__set_seq_num(konf_tree_t * this, unsigned short seq_num)
+{
+	this->seq_num = seq_num;
+}