Browse Source

The script callback can return the output of the executed action. The konf_buf class now use fd instead sock and can return the whole readed line.

git-svn-id: https://klish.googlecode.com/svn/trunk@283 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
2b8b3f67a5
6 changed files with 53 additions and 43 deletions
  1. 9 11
      clish/clish_script_callback.c
  2. 4 3
      clish/shell.h
  3. 1 2
      clish/shell/shell_execute.c
  4. 6 5
      konf/buf.h
  5. 32 21
      konf/buf/buf.c
  6. 1 1
      konf/buf/private.h

+ 9 - 11
clish/clish_script_callback.c

@@ -16,23 +16,20 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#include "konf/buf.h"
 #include "private.h"
 
-#define KLISH_FIFO "/tmp/klish.fifo"
-
 /*--------------------------------------------------------- */
 bool_t clish_script_callback(clish_shell_t * this,
-	const clish_command_t * cmd, const char *script)
+	const clish_command_t * cmd, const char *script, char ** out)
 {
 	const char * shebang = NULL;
 	pid_t cpid;
-	char buf;
 	int res;
 	const char *fifo_name;
 	FILE *rpipe, *wpipe;
 	char *command = NULL;
 	bool_t is_sh = BOOL_FALSE;
-	char **out = NULL;
 
 	/* Signal vars */
 	struct sigaction sig_old_int;
@@ -97,12 +94,11 @@ bool_t clish_script_callback(clish_shell_t * this,
 	} else {
 		lub_string_cat(&command, script);
 	}
-#ifdef DEBUG
-	fprintf(stderr, "COMMAND: %s\n", command);
-#endif /* DEBUG */
 
 	/* If the stdout of script is needed */
 	if (out) {
+		konf_buf_t *buf;
+
 		/* Ignore SIGINT and SIGQUIT */
 		sigemptyset(&sig_set);
 		sig_new.sa_flags = 0;
@@ -128,8 +124,10 @@ bool_t clish_script_callback(clish_shell_t * this,
 			return BOOL_FALSE;
 		}
 		/* Read the result of script execution */
-		while (read(fileno(rpipe), &buf, 1) > 0)
-			write(fileno(clish_shell__get_ostream(this)), &buf, 1);
+		buf = konf_buf_new(fileno(rpipe));
+		while (konf_buf_read(buf) > 0);
+		*out = konf_buf__dup_line(buf);
+		konf_buf_delete(buf);
 		/* Wait for the writing process */
 		if (!is_sh)
 			waitpid(cpid, NULL, 0);
@@ -152,7 +150,7 @@ bool_t clish_script_callback(clish_shell_t * this,
 
 /*--------------------------------------------------------- */
 bool_t clish_dryrun_callback(clish_shell_t * this,
-	const clish_command_t * cmd, const char *script)
+	const clish_command_t * cmd, const char *script, char ** out)
 {
 #ifdef DEBUG
 	fprintf(stderr, "DRY-RUN: %s\n", script);

+ 4 - 3
clish/shell.h

@@ -137,15 +137,16 @@ typedef bool_t clish_shell_script_fn_t(
 	/** 
          * The shell instance which invoked this call
          */
-					clish_shell_t * instance,
+	clish_shell_t * instance,
 	/** 
          * The command which invoked this call
          */
-					      const clish_command_t * cmd,
+	const clish_command_t * cmd,
 	/** 
          * The script to be evaluated
          */
-					      const char *script);
+	const char *script,
+	char ** out);
 
 /**
   * A hook function used to control config file write

+ 1 - 2
clish/shell/shell_execute.c

@@ -279,8 +279,7 @@ clish_shell_execute(clish_shell_t * this,
 		}
 	} else if (NULL != script) {
 		/* now get the client to interpret the resulting script */
-		result = this->client_hooks->script_fn(this, cmd, script);
-
+		result = this->client_hooks->script_fn(this, cmd, script, NULL);
 	}
 	pthread_cleanup_pop(1);
 

+ 6 - 5
konf/buf.h

@@ -26,7 +26,7 @@ typedef struct konf_buf_s konf_buf_t;
 /*-----------------
  * meta functions
  *----------------- */
-konf_buf_t *konf_buf_new(int sock);
+konf_buf_t *konf_buf_new(int fd);
 int konf_buf_bt_compare(const void *clientnode, const void *clientkey);
 void konf_buf_bt_getkey(const void *clientnode, lub_bintree_key_t * key);
 size_t konf_buf_bt_offset(void);
@@ -40,12 +40,13 @@ char * konf_buf_string(char *instance, int len);
 char * konf_buf_parse(konf_buf_t *instance);
 char * konf_buf_preparse(konf_buf_t *instance);
 int konf_buf_lseek(konf_buf_t *instance, int newpos);
-int konf_buf__get_sock(const konf_buf_t *instance);
+int konf_buf__get_fd(const konf_buf_t *instance);
 int konf_buf__get_len(const konf_buf_t *instance);
+char * konf_buf__dup_line(const konf_buf_t *instance);
 
-int konf_buftree_read(lub_bintree_t *instance, int sock);
-char * konf_buftree_parse(lub_bintree_t *instance, int sock);
-void konf_buftree_remove(lub_bintree_t *instance, int sock);
+int konf_buftree_read(lub_bintree_t *instance, int fd);
+char * konf_buftree_parse(lub_bintree_t *instance, int fd);
+void konf_buftree_remove(lub_bintree_t *instance, int fd);
 
 #endif				/* _konf_buf_h */
 /** @} clish_conf */

+ 32 - 21
konf/buf/buf.c

@@ -22,18 +22,18 @@
 int konf_buf_bt_compare(const void *clientnode, const void *clientkey)
 {
 	const konf_buf_t *this = clientnode;
-	int keysock;
+	int keyfd;
 
-	memcpy(&keysock, clientkey, sizeof(keysock));
+	memcpy(&keyfd, clientkey, sizeof(keyfd));
 
-	return (this->sock - keysock);
+	return (this->fd - keyfd);
 }
 
 /*-------------------------------------------------------- */
 static void konf_buf_key(lub_bintree_key_t * key,
-	int sock)
+	int fd)
 {
-	memcpy(key, &sock, sizeof(sock));
+	memcpy(key, &fd, sizeof(fd));
 }
 
 /*-------------------------------------------------------- */
@@ -41,16 +41,16 @@ void konf_buf_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
 {
 	const konf_buf_t *this = clientnode;
 
-	konf_buf_key(key, this->sock);
+	konf_buf_key(key, this->fd);
 }
 
 /*---------------------------------------------------------
  * PRIVATE METHODS
  *--------------------------------------------------------- */
 static void
-konf_buf_init(konf_buf_t * this, int sock)
+konf_buf_init(konf_buf_t * this, int fd)
 {
-	this->sock = sock;
+	this->fd = fd;
 	this->buf = malloc(KONF_BUF_CHUNK);
 	this->size = KONF_BUF_CHUNK;
 	this->pos = 0;
@@ -75,12 +75,12 @@ size_t konf_buf_bt_offset(void)
 }
 
 /*--------------------------------------------------------- */
-konf_buf_t *konf_buf_new(int sock)
+konf_buf_t *konf_buf_new(int fd)
 {
 	konf_buf_t *this = malloc(sizeof(konf_buf_t));
 
 	if (this) {
-		konf_buf_init(this, sock);
+		konf_buf_init(this, fd);
 	}
 
 	return this;
@@ -136,7 +136,7 @@ int konf_buf_read(konf_buf_t *this)
 	buffer_size = this->size - this->pos;
 	buffer = this->buf + this->pos;
 
-	nbytes = read(this->sock, buffer, buffer_size);
+	nbytes = read(this->fd, buffer, buffer_size);
 	if (nbytes > 0)
 		this->pos += nbytes;
 
@@ -217,9 +217,9 @@ int konf_buf_lseek(konf_buf_t *this, int newpos)
 }
 
 /*--------------------------------------------------------- */
-int konf_buf__get_sock(const konf_buf_t * this)
+int konf_buf__get_fd(const konf_buf_t * this)
 {
-	return this->sock;
+	return this->fd;
 }
 
 /*--------------------------------------------------------- */
@@ -228,28 +228,39 @@ int konf_buf__get_len(const konf_buf_t *this)
 	return this->pos;
 }
 
+/*--------------------------------------------------------- */
+char * konf_buf__dup_line(const konf_buf_t *this)
+{
+	char *str;
+
+	str = malloc(this->pos + 1);
+	memcpy(str, this->buf, this->pos);
+	str[this->pos] = '\0';
+	return str;
+}
+
 /*---------------------------------------------------------
  * buftree functions
  *--------------------------------------------------------- */
 
 /*--------------------------------------------------------- */
 konf_buf_t *konf_buftree_find(lub_bintree_t * this,
-	int sock)
+	int fd)
 {
 	lub_bintree_key_t key;
 
-	konf_buf_key(&key, sock);
+	konf_buf_key(&key, fd);
 
 	return lub_bintree_find(this, &key);
 }
 
 /*--------------------------------------------------------- */
 void konf_buftree_remove(lub_bintree_t * this,
-	int sock)
+	int fd)
 {
 	konf_buf_t *tbuf;
 
-	if ((tbuf = konf_buftree_find(this, sock)) == NULL)
+	if ((tbuf = konf_buftree_find(this, fd)) == NULL)
 		return;
 
 	lub_bintree_remove(this, tbuf);
@@ -257,11 +268,11 @@ void konf_buftree_remove(lub_bintree_t * this,
 }
 
 /*--------------------------------------------------------- */
-int konf_buftree_read(lub_bintree_t * this, int sock)
+int konf_buftree_read(lub_bintree_t * this, int fd)
 {
 	konf_buf_t *buf;
 
-	buf = konf_buftree_find(this, sock);
+	buf = konf_buftree_find(this, fd);
 	if (!buf)
 		return -1;
 
@@ -271,11 +282,11 @@ int konf_buftree_read(lub_bintree_t * this, int sock)
 
 /*--------------------------------------------------------- */
 char * konf_buftree_parse(lub_bintree_t * this,
-	int sock)
+	int fd)
 {
 	konf_buf_t *buf;
 
-	buf = konf_buftree_find(this, sock);
+	buf = konf_buftree_find(this, fd);
 	if (!buf)
 		return NULL;
 

+ 1 - 1
konf/buf/private.h

@@ -12,7 +12,7 @@
  *--------------------------------------------------------- */
 struct konf_buf_s {
 	lub_bintree_node_t bt_node;
-	int sock;
+	int fd;
 	int size;
 	char *buf;
 	int pos;