|
@@ -9,10 +9,12 @@
|
|
|
#include <string.h>
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
-#include "clish/private.h"
|
|
|
+#include "konf/buf.h"
|
|
|
+#include "konf/query.h"
|
|
|
#include "lub/string.h"
|
|
|
#include "private.h"
|
|
|
|
|
|
+
|
|
|
#ifndef UNIX_PATH_MAX
|
|
|
#define UNIX_PATH_MAX 108
|
|
|
#endif
|
|
@@ -20,83 +22,83 @@
|
|
|
|
|
|
konf_client_t *konf_client_new(const char *path)
|
|
|
{
|
|
|
- konf_client_t *client;
|
|
|
+ konf_client_t *this;
|
|
|
|
|
|
if (!path)
|
|
|
return NULL;
|
|
|
|
|
|
- if (!(client = malloc(sizeof(*client))))
|
|
|
+ if (!(this = malloc(sizeof(*this))))
|
|
|
return NULL;
|
|
|
|
|
|
- client->sock = -1;
|
|
|
- client->path = lub_string_dup(path);
|
|
|
+ this->sock = -1;
|
|
|
+ this->path = lub_string_dup(path);
|
|
|
|
|
|
- return client;
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
|
|
|
-void konf_client_free(konf_client_t *client)
|
|
|
+void konf_client_free(konf_client_t *this)
|
|
|
{
|
|
|
- if (!client)
|
|
|
+ if (!this)
|
|
|
return;
|
|
|
- if (client->sock != -1)
|
|
|
- konf_client_disconnect(client);
|
|
|
- lub_string_free(client->path);
|
|
|
+ if (this->sock != -1)
|
|
|
+ konf_client_disconnect(this);
|
|
|
+ lub_string_free(this->path);
|
|
|
|
|
|
- free(client);
|
|
|
+ free(this);
|
|
|
}
|
|
|
|
|
|
|
|
|
-int konf_client_connect(konf_client_t *client)
|
|
|
+int konf_client_connect(konf_client_t *this)
|
|
|
{
|
|
|
struct sockaddr_un raddr;
|
|
|
|
|
|
- if (client->sock >= 0)
|
|
|
- return client->sock;
|
|
|
+ if (this->sock >= 0)
|
|
|
+ return this->sock;
|
|
|
|
|
|
- if ((client->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
|
- return client->sock;
|
|
|
+ if ((this->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
|
+ return this->sock;
|
|
|
|
|
|
raddr.sun_family = AF_UNIX;
|
|
|
- strncpy(raddr.sun_path, client->path, UNIX_PATH_MAX);
|
|
|
+ strncpy(raddr.sun_path, this->path, UNIX_PATH_MAX);
|
|
|
raddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
|
|
|
- if (connect(client->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
|
|
|
- close(client->sock);
|
|
|
- client->sock = -1;
|
|
|
+ if (connect(this->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
|
|
|
+ close(this->sock);
|
|
|
+ this->sock = -1;
|
|
|
}
|
|
|
|
|
|
- return client->sock;
|
|
|
+ return this->sock;
|
|
|
}
|
|
|
|
|
|
|
|
|
-void konf_client_disconnect(konf_client_t *client)
|
|
|
+void konf_client_disconnect(konf_client_t *this)
|
|
|
{
|
|
|
- if (client->sock >= 0) {
|
|
|
- close(client->sock);
|
|
|
- client->sock = -1;
|
|
|
+ if (this->sock >= 0) {
|
|
|
+ close(this->sock);
|
|
|
+ this->sock = -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-int konf_client_reconnect(konf_client_t *client)
|
|
|
+int konf_client_reconnect(konf_client_t *this)
|
|
|
{
|
|
|
- konf_client_disconnect(client);
|
|
|
- return konf_client_connect(client);
|
|
|
+ konf_client_disconnect(this);
|
|
|
+ return konf_client_connect(this);
|
|
|
}
|
|
|
|
|
|
|
|
|
-int konf_client_send(konf_client_t *client, char *command)
|
|
|
+int konf_client_send(konf_client_t *this, char *command)
|
|
|
{
|
|
|
- if (client->sock < 0)
|
|
|
- return client->sock;
|
|
|
+ if (this->sock < 0)
|
|
|
+ return this->sock;
|
|
|
|
|
|
- return send(client->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
|
|
|
+ return send(this->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
|
|
|
}
|
|
|
|
|
|
|
|
|
-int konf_client__get_sock(konf_client_t *client)
|
|
|
+int konf_client__get_sock(konf_client_t *this)
|
|
|
{
|
|
|
- return client->sock;
|
|
|
+ return this->sock;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -129,3 +131,91 @@ konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+static int process_answer(konf_client_t * this, char *str, konf_buf_t *buf, konf_buf_t **data)
|
|
|
+{
|
|
|
+ int res;
|
|
|
+ konf_query_t *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);
|
|
|
+
|
|
|
+*/
|
|
|
+#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 (!(*data = konf_client_recv_data(this, buf)))
|
|
|
+ res = -1;
|
|
|
+ else
|
|
|
+ res = 1;
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ res = -1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ konf_query_free(query);
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int konf_client_recv_answer(konf_client_t * this, konf_buf_t **data)
|
|
|
+{
|
|
|
+ konf_buf_t *buf;
|
|
|
+ int nbytes;
|
|
|
+ char *str;
|
|
|
+ int retval = 0;
|
|
|
+ int processed = 0;
|
|
|
+
|
|
|
+ if ((konf_client_connect(this) < 0))
|
|
|
+ return -1;
|
|
|
+
|
|
|
+ buf = konf_buf_new(konf_client__get_sock(this));
|
|
|
+ while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
|
|
|
+ while ((str = konf_buf_parse(buf))) {
|
|
|
+ konf_buf_t *tmpdata = NULL;
|
|
|
+ retval = process_answer(this, 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;
|
|
|
+}
|
|
|
+
|