Browse Source

Semi-working utility konf to communicate to konfd daemon. TODO: Code duplication, quotes support.

git-svn-id: https://klish.googlecode.com/svn/trunk@212 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 14 years ago
parent
commit
34d2dea2fa
6 changed files with 252 additions and 11 deletions
  1. 19 4
      Makefile.in
  2. 220 0
      bin/konf.c
  3. 2 3
      bin/konfd.c
  4. 7 2
      bin/module.am
  5. 1 1
      konf/net.h
  6. 3 1
      konf/net/net.c

+ 19 - 4
Makefile.in

@@ -37,7 +37,7 @@ PRE_UNINSTALL = :
 POST_UNINSTALL = :
 build_triplet = @build@
 host_triplet = @host@
-bin_PROGRAMS = bin/clish$(EXEEXT) bin/konfd$(EXEEXT) \
+bin_PROGRAMS = bin/clish$(EXEEXT) bin/konfd$(EXEEXT) bin/konf$(EXEEXT) \
 	bin/tclish@TCL_VERSION@$(EXEEXT) $(am__EXEEXT_1)
 DIST_COMMON = README $(am__configure_deps) $(nobase_include_HEADERS) \
 	$(srcdir)/Makefile.am $(srcdir)/Makefile.in \
@@ -440,6 +440,9 @@ PROGRAMS = $(bin_PROGRAMS) $(noinst_PROGRAMS)
 am_bin_clish_OBJECTS = bin/clish.$(OBJEXT)
 bin_clish_OBJECTS = $(am_bin_clish_OBJECTS)
 bin_clish_DEPENDENCIES = libclish.la
+am_bin_konf_OBJECTS = bin/konf.$(OBJEXT)
+bin_konf_OBJECTS = $(am_bin_konf_OBJECTS)
+bin_konf_DEPENDENCIES =
 am_bin_konfd_OBJECTS = bin/konfd.$(OBJEXT)
 bin_konfd_OBJECTS = $(am_bin_konfd_OBJECTS)
 bin_konfd_DEPENDENCIES =
@@ -504,7 +507,7 @@ CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
 SOURCES = $(libclish_la_SOURCES) $(libkonf_la_SOURCES) \
 	$(liblub_la_SOURCES) $(liblubheap_la_SOURCES) \
 	$(libtinyrl_la_SOURCES) $(libtinyxml_la_SOURCES) \
-	$(bin_clish_SOURCES) $(bin_konfd_SOURCES) \
+	$(bin_clish_SOURCES) $(bin_konf_SOURCES) $(bin_konfd_SOURCES) \
 	$(bin_lubheap_SOURCES) $(bin_tclish@TCL_VERSION@_SOURCES) \
 	$(test_bintree_SOURCES) $(test_heap_SOURCES) \
 	$(test_lubMallocTest_SOURCES) $(test_mallocTest_SOURCES) \
@@ -513,7 +516,8 @@ DIST_SOURCES = $(libclish_la_SOURCES) $(libkonf_la_SOURCES) \
 	$(am__liblub_la_SOURCES_DIST) \
 	$(am__liblubheap_la_SOURCES_DIST) $(libtinyrl_la_SOURCES) \
 	$(libtinyxml_la_SOURCES) $(bin_clish_SOURCES) \
-	$(bin_konfd_SOURCES) $(am__bin_lubheap_SOURCES_DIST) \
+	$(bin_konf_SOURCES) $(bin_konfd_SOURCES) \
+	$(am__bin_lubheap_SOURCES_DIST) \
 	$(bin_tclish@TCL_VERSION@_SOURCES) $(test_bintree_SOURCES) \
 	$(am__test_heap_SOURCES_DIST) \
 	$(am__test_lubMallocTest_SOURCES_DIST) \
@@ -719,7 +723,7 @@ EXTRA_DIST = bin/module.am clish/module.am lub/module.am \
 	xml-examples/script.txt xml-examples/startup.xml \
 	xml-examples/types.xml xml-examples/README konf/tree/module.am \
 	konf/query/module.am konf/buf/module.am konf/net/module.am
-bin_clish_SOURCES = bin/clish.cpp	
+bin_clish_SOURCES = bin/clish.cpp
 bin_clish_LDADD = \
     libclish.la    \
     @KONF_LIBS@ \
@@ -735,6 +739,11 @@ bin_konfd_LDADD = \
     @KONF_LIBS@ \
     @LUB_LIBS@
 
+bin_konf_SOURCES = bin/konf.c
+bin_konf_LDADD = \
+    @KONF_LIBS@ \
+    @LUB_LIBS@
+
 bin_tclish@TCL_VERSION@_SOURCES = bin/tclish.cpp
 bin_tclish@TCL_VERSION@_LDADD = \
     libclish.la                   \
@@ -1627,6 +1636,10 @@ bin/clish.$(OBJEXT): bin/$(am__dirstamp) bin/$(DEPDIR)/$(am__dirstamp)
 bin/clish$(EXEEXT): $(bin_clish_OBJECTS) $(bin_clish_DEPENDENCIES) bin/$(am__dirstamp)
 	@rm -f bin/clish$(EXEEXT)
 	$(CXXLINK) $(bin_clish_OBJECTS) $(bin_clish_LDADD) $(LIBS)
+bin/konf.$(OBJEXT): bin/$(am__dirstamp) bin/$(DEPDIR)/$(am__dirstamp)
+bin/konf$(EXEEXT): $(bin_konf_OBJECTS) $(bin_konf_DEPENDENCIES) bin/$(am__dirstamp)
+	@rm -f bin/konf$(EXEEXT)
+	$(LINK) $(bin_konf_OBJECTS) $(bin_konf_LDADD) $(LIBS)
 bin/konfd.$(OBJEXT): bin/$(am__dirstamp) bin/$(DEPDIR)/$(am__dirstamp)
 bin/konfd$(EXEEXT): $(bin_konfd_OBJECTS) $(bin_konfd_DEPENDENCIES) bin/$(am__dirstamp)
 	@rm -f bin/konfd$(EXEEXT)
@@ -1676,6 +1689,7 @@ test/string$(EXEEXT): $(test_string_OBJECTS) $(test_string_DEPENDENCIES) test/$(
 mostlyclean-compile:
 	-rm -f *.$(OBJEXT)
 	-rm -f bin/clish.$(OBJEXT)
+	-rm -f bin/konf.$(OBJEXT)
 	-rm -f bin/konfd.$(OBJEXT)
 	-rm -f bin/lubheap.$(OBJEXT)
 	-rm -f bin/tclish.$(OBJEXT)
@@ -2035,6 +2049,7 @@ distclean-compile:
 	-rm -f *.tab.c
 
 @AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/clish.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/konf.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/konfd.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/lubheap.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/tclish.Po@am__quote@

+ 220 - 0
bin/konf.c

@@ -0,0 +1,220 @@
+/*
+ * konf.c
+ *
+ *
+ * The client to communicate to konfd configuration daemon.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "konf/net.h"
+#include "konf/tree.h"
+#include "konf/query.h"
+#include "konf/buf.h"
+#include "lub/argv.h"
+#include "lub/string.h"
+
+#ifndef UNIX_PATH_MAX
+#define UNIX_PATH_MAX 108
+#endif
+#define MAXMSG 1024
+
+static int receive_answer(konf_client_t * client, konf_buf_t **data);
+static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
+static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
+
+/*--------------------------------------------------------- */
+int main(int argc, char **argv)
+{
+	int res = -1;
+	unsigned i;
+	konf_client_t *client = NULL;
+	konf_buf_t *buf = NULL;
+	char *line = NULL;
+	char *str = NULL;
+	const char *socket_path = KONFD_SOCKET_PATH;
+
+	/* Get request line from the args */
+	for (i = 1; i < argc; i++) {
+		char *space = NULL;
+		if (NULL != line)
+			lub_string_cat(&line, " ");
+		space = strchr(argv[i], ' ');
+		if (space)
+			lub_string_cat(&line, "\"");
+		lub_string_cat(&line, argv[i]);
+		if (space)
+			lub_string_cat(&line, "\"");
+	}
+	if (!line) {
+		fprintf(stderr, "Not enough arguments.\n");
+		goto err;
+	}
+#ifdef DEBUG
+	printf("REQUEST: %s\n", line);
+#endif
+
+	if (!(client = konf_client_new(socket_path))) {
+		fprintf(stderr, "Can't create internal data structures.\n");
+		goto err;
+	}
+
+	if (konf_client_connect(client) < 0) {
+		fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
+		goto err;
+	}
+
+	if (konf_client_send(client, line) < 0) {
+		fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
+		goto err;
+	}
+
+	if (receive_answer(client, &buf) < 0) {
+		fprintf(stderr, "Can't get answer from the config daemon.\n");
+	}
+
+	if (buf) {
+		konf_buf_lseek(buf, 0);
+		while ((str = konf_buf_preparse(buf))) {
+			if (strlen(str) == 0) {
+				lub_string_free(str);
+				break;
+			}
+			fprintf(stdout, "%s\n", str);
+			lub_string_free(str);
+		}
+		konf_buf_delete(buf);
+	}
+
+	res = 0;
+err:
+	lub_string_free(line);
+	konf_client_free(client);
+
+	return res;
+}
+
+/*--------------------------------------------------------- */
+static int receive_answer(konf_client_t * client, konf_buf_t **data)
+{
+	konf_buf_t *buf;
+	int nbytes;
+	char *str;
+	int retval = 0;
+	int processed = 0;
+
+	if ((konf_client_connect(client) < 0))
+		return -1;
+
+	buf = konf_buf_new(konf_client__get_sock(client));
+	while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
+		while ((str = konf_buf_parse(buf))) {
+			konf_buf_t *tmpdata = NULL;
+			retval = process_answer(client, str, buf, &tmpdata);
+			lub_string_free(str);
+			if (retval < 0) {
+				konf_buf_delete(buf);
+				return retval;
+			}
+			if (retval == 0)
+				processed = 1;
+			if (tmpdata) {
+				if (*data)
+					konf_buf_delete(*data);
+				*data = tmpdata;
+			}
+		}
+	}
+	konf_buf_delete(buf);
+
+	return retval;
+}
+
+/*--------------------------------------------------------- */
+static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
+{
+	konf_buf_t *tmpdata;
+	char *str;
+	int processed = 0;
+
+	if ((konf_client_connect(client) < 0))
+		return -1;
+
+	tmpdata = konf_buf_new(konf_client__get_sock(client));
+	do {
+		while ((str = konf_buf_parse(buf))) {
+#ifdef DEBUG
+			fprintf(stderr, "RECV DATA: [%s]\n", str);
+#endif
+			konf_buf_add(tmpdata, str, strlen(str) + 1);
+			if (strlen(str) == 0) {
+				processed = 1;
+				lub_string_free(str);
+				break;
+			}
+			lub_string_free(str);
+		}
+	} while ((!processed) && (konf_buf_read(buf)) > 0);
+	if (!processed) {
+		konf_buf_delete(tmpdata);
+		*data = NULL;
+		return -1;
+	}
+
+	*data = tmpdata;
+
+	return 0;
+}
+
+/*--------------------------------------------------------- */
+static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
+{
+	int res;
+	konf_query_t *query;
+
+	/* Parse query */
+	query = konf_query_new();
+	res = konf_query_parse_str(query, str);
+	if (res < 0) {
+		konf_query_free(query);
+#ifdef DEBUG
+		fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
+#endif
+		return -1;
+	}
+
+#ifdef DEBUG
+	fprintf(stderr, "ANSWER: %s\n", str);
+/*	konf_query_dump(query);
+*/
+#endif
+
+	switch (konf_query__get_op(query)) {
+
+	case KONF_QUERY_OP_OK:
+		res = 0;
+		break;
+
+	case KONF_QUERY_OP_ERROR:
+		res = -1;
+		break;
+
+	case KONF_QUERY_OP_STREAM:
+		if (receive_data(client, buf, data) < 0)
+			res = -1;
+		else
+			res = 1; /* wait for another answer */
+		break;
+
+	default:
+		res = -1;
+		break;
+	}
+
+	/* Free resources */
+	konf_query_free(query);
+
+	return res;
+}

+ 2 - 3
bin/konfd.c

@@ -1,8 +1,8 @@
 /*
- * clish_config_callback.c
+ * konfd.c
  *
+ * The konfd daemon to store user configuration commands.
  *
- * Callback hook to execute config operations.
  */
 
 #include <stdio.h>
@@ -25,7 +25,6 @@
 #include "lub/argv.h"
 #include "lub/string.h"
 
-#define KONFD_SOCKET_PATH "/tmp/konfd.socket"
 #define KONFD_CONFIG_PATH "/tmp/running-config"
 
 #ifndef UNIX_PATH_MAX

+ 7 - 2
bin/module.am

@@ -1,7 +1,7 @@
 ## Process this file with automake to produce Makefile.in
-bin_PROGRAMS += bin/clish bin/konfd
+bin_PROGRAMS += bin/clish bin/konfd bin/konf
 
-bin_clish_SOURCES = bin/clish.cpp	
+bin_clish_SOURCES = bin/clish.cpp
 bin_clish_LDADD  = \
     libclish.la    \
     @KONF_LIBS@ \
@@ -17,6 +17,11 @@ bin_konfd_LDADD  = \
     @KONF_LIBS@ \
     @LUB_LIBS@
 
+bin_konf_SOURCES = bin/konf.c
+bin_konf_LDADD  = \
+    @KONF_LIBS@ \
+    @LUB_LIBS@
+
 bin_PROGRAMS += bin/tclish@TCL_VERSION@
 
 bin_tclish@TCL_VERSION@_SOURCES = bin/tclish.cpp

+ 1 - 1
konf/net.h

@@ -5,7 +5,7 @@ typedef struct konf_client_s konf_client_t;
 
 #define KONFD_SOCKET_PATH "/tmp/konfd.socket"
 
-konf_client_t *konf_client_new(char *path);
+konf_client_t *konf_client_new(const char *path);
 void konf_client_free(konf_client_t *client);
 int konf_client_connect(konf_client_t *client);
 void konf_client_disconnect(konf_client_t *client);

+ 3 - 1
konf/net/net.c

@@ -17,7 +17,7 @@
 #define UNIX_PATH_MAX 108
 #endif
 
-konf_client_t *konf_client_new(char *path)
+konf_client_t *konf_client_new(const char *path)
 {
 	konf_client_t *client;
 
@@ -35,6 +35,8 @@ konf_client_t *konf_client_new(char *path)
 
 void konf_client_free(konf_client_t *client)
 {
+	if (!client)
+		return;
 	if (client->sock != -1)
 		konf_client_disconnect(client);
 	lub_string_free(client->path);