Browse Source

Add ostream to context and shell. Use ostream for normal data exchange. Use stderr for error messages and help.

git-svn-id: https://klish.googlecode.com/svn/trunk@122 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
847dfa9196

+ 8 - 6
clish/clish_config_callback.c

@@ -146,7 +146,7 @@ clish_config_callback(const clish_shell_t * shell,
 	client = clish_shell__get_client(shell);
 
 #ifdef DEBUG
-	printf("CONFIG request: %s\n", command);
+	fprintf(stderr, "CONFIG request: %s\n", command);
 #endif
 
 	if (send_request(client, command) < 0) {
@@ -172,7 +172,8 @@ clish_config_callback(const clish_shell_t * shell,
 				if (!(fd = fopen(filename, "r")))
 					break;
 				while (fgets(str, sizeof(str), fd))
-					fprintf(stdout, "%s", str);
+					fprintf(clish_shell__get_ostream(shell),
+						"%s", str);
 				fclose(fd);
 			}
 			if (buf) {
@@ -183,7 +184,8 @@ clish_config_callback(const clish_shell_t * shell,
 						lub_string_free(str);
 						break;
 					}
-					printf("%s\n", str);
+					fprintf(clish_shell__get_ostream(shell),
+						"%s\n", str);
 					lub_string_free(str);
 				}
 				konf_buf_delete(buf);
@@ -262,7 +264,7 @@ static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **da
 	do {
 		while ((str = konf_buf_parse(buf))) {
 #ifdef DEBUG
-			printf("RECV DATA: [%s]\n", str);
+			fprintf(stderr, "RECV DATA: [%s]\n", str);
 #endif
 			konf_buf_add(tmpdata, str, strlen(str) + 1);
 			if (strlen(str) == 0) {
@@ -295,13 +297,13 @@ static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, ko
 	if (res < 0) {
 		konf_query_free(query);
 #ifdef DEBUG
-		printf("CONFIG error: Cannot parse answer string.\n");
+		fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
 #endif
 		return -1;
 	}
 
 #ifdef DEBUG
-	printf("CONFIG answer: %s\n", str);
+	fprintf(stderr, "CONFIG answer: %s\n", str);
 	// konf_query_dump(query);
 #endif
 

+ 1 - 1
clish/clish_script_callback.c

@@ -12,7 +12,7 @@
 bool_t clish_script_callback(const clish_shell_t * shell, const char *script)
 {
 #ifdef DEBUG
-	printf("SYSTEM: %s\n", script);
+	fprintf(stderr, "SYSTEM: %s\n", script);
 #endif				/* DEBUG */
 
 	return (0 == system(script)) ? BOOL_TRUE : BOOL_FALSE;

+ 1 - 1
clish/command/command.c

@@ -176,7 +176,7 @@ void clish_command_help(const clish_command_t * this, const char *line)
 
 	/* Line has no any parameters */
 	if (strlen(line) <= strlen(name)) {
-		printf("%s  %s\n", name,
+		fprintf(stderr, "%s  %s\n", name,
 			clish_command__get_text(this));
 		return;
 	}

+ 5 - 4
clish/param/param.c

@@ -166,17 +166,18 @@ void clish_param_help(const clish_param_t * this, size_t offset)
 	else
 		name = clish_ptype__get_text(this->ptype);
 
-	printf("%s %*c%s", name, (int)(offset - strlen(name)), ' ', this->text);
+	fprintf(stderr, "%s %*c%s",
+		name, (int)(offset - strlen(name)), ' ', this->text);
 	if (NULL != range) {
-		printf(" (%s)", range);
+		fprintf(stderr, " (%s)", range);
 	}
-	printf("\n");
+	fprintf(stderr, "\n");
 }
 
 /*--------------------------------------------------------- */
 void clish_param_help_arrow(const clish_param_t * this, size_t offset)
 {
-	printf("%*c\n", (int)offset, '^');
+	fprintf(stderr, "%*c\n", (int)offset, '^');
 }
 
 /*--------------------------------------------------------- */

+ 5 - 2
clish/shell.h

@@ -276,7 +276,7 @@ clish_shell_spawn_from_file(const clish_shell_hooks_t * hooks,
 			    void *cookie, const char *filename);
 
 clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
-			       void *cookie, FILE * istream);
+			       void *cookie, FILE * istream, FILE * ostream);
 /*-----------------
  * methods
  *----------------- */
@@ -322,13 +322,16 @@ const char *clish_shell__get_pwd(const clish_shell_t * instance,
 				 unsigned index);
 char *clish_shell__get_line(const clish_command_t * cmd, clish_pargv_t * pargv);
 konf_client_t *clish_shell__get_client(const clish_shell_t * instance);
+FILE *clish_shell__get_istream(const clish_shell_t * instance);
+FILE *clish_shell__get_ostream(const clish_shell_t * instance);
 
 /* Context */
 typedef struct clish_context_s clish_context_t;
 
 int clish_shell_wait(clish_context_t * instance);
 clish_context_t *clish_shell_spawn_stream(const pthread_attr_t * attr,
-	const clish_shell_hooks_t * hooks, void *cookie, FILE * istream);
+	const clish_shell_hooks_t * hooks, void *cookie, 
+	FILE * istream, FILE * ostream);
 
 _END_C_DECL
 #endif				/* _clish_shell_h */

+ 1 - 0
clish/shell/private.h

@@ -21,6 +21,7 @@ struct clish_context_s {
 	const clish_shell_hooks_t *hooks;
 	void *cookie;
 	FILE *istream;
+	FILE *ostream;
 	clish_shell_t *shell;
 	clish_pargv_t *pargv;
 	char *prompt;

+ 3 - 23
clish/shell/shell_execute.c

@@ -17,7 +17,9 @@
 static clish_shell_builtin_fn_t
     clish_close,
     clish_overview,
-    clish_source, clish_source_nostop, clish_history, clish_running_config;
+    clish_source,
+    clish_source_nostop,
+    clish_history;
 
 static clish_shell_builtin_t clish_cmd_list[] = {
 	{"clish_close", clish_close},
@@ -25,7 +27,6 @@ static clish_shell_builtin_t clish_cmd_list[] = {
 	{"clish_source", clish_source},
 	{"clish_source_nostop", clish_source_nostop},
 	{"clish_history", clish_history},
-	{"clish_running_config", clish_running_config},
 	{NULL, NULL}
 };
 
@@ -157,27 +158,6 @@ static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
 	return BOOL_TRUE;
 }
 
-/*----------------------------------------------------------- */
-/*
- Write running-config to the specified file.
-*/
-static bool_t
-clish_running_config(const clish_shell_t * shell, const lub_argv_t * argv)
-{
-/*    bool_t result = BOOL_FALSE;
-    FILE *stream  = stdout;
-    clish_conf_t *conf = clish_shell__get_conf(shell);
-
-    if (argv && (lub_argv__get_count(argv) > 0)) {
-        if (!(stream = fopen(lub_argv__get_arg(argv,0),"w")))
-            return BOOL_FALSE;
-        clish_conf_fprintf(stream, conf);
-        fclose(stream);
-    } else
-        clish_conf_fprintf(stream, conf);
-*/
-}
-
 /*----------------------------------------------------------- */
 /*
  * Searches for a builtin command to execute

+ 3 - 3
clish/shell/shell_help.c

@@ -40,7 +40,7 @@ available_commands(clish_shell_t * this, const char *line, bool_t full)
 			buf = lub_string_dup("");
 		}
 		/* indicate the point of error */
-		printf("%*s\n", error_offset, "^");
+		fprintf(stderr, "%*s\n", error_offset, "^");
 	} else {
 		/* take a copy */
 		buf = lub_string_dup(line);
@@ -70,7 +70,7 @@ available_commands(clish_shell_t * this, const char *line, bool_t full)
 		} else {
 			name = clish_command__get_suffix(cmd);
 		}
-		printf("%-*s  %s\n",
+		fprintf(stderr, "%-*s  %s\n",
 		       (int)max_width, name, clish_command__get_text(cmd));
 	}
 	/* cleanup */
@@ -104,7 +104,7 @@ void clish_shell_help(clish_shell_t * this, const char *line)
 				const char *detail =
 				    clish_command__get_detail(cmd);
 				if (NULL != detail) {
-					printf("%s\n", detail);
+					fprintf(stderr, "%s\n", detail);
 				} else {
 					/* get the command to describe itself */
 					clish_command_help(cmd, line);

+ 5 - 4
clish/shell/shell_new.c

@@ -9,7 +9,7 @@
 static void
 clish_shell_init(clish_shell_t * this,
 		 const clish_shell_hooks_t * hooks,
-		 void *cookie, FILE * istream)
+		 void *cookie, FILE * istream, FILE * ostream)
 {
 	/* initialise the tree of views */
 	lub_bintree_init(&this->view_tree,
@@ -33,7 +33,7 @@ clish_shell_init(clish_shell_t * this,
 	this->state = SHELL_STATE_INITIALISING;
 	this->overview = NULL;
 	clish_shell_iterator_init(&this->iter, CLISH_NSPACE_NONE);
-	this->tinyrl = clish_shell_tinyrl_new(istream, stdout, 0);
+	this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
 	this->current_file = NULL;
 	this->cfg_pwdv = NULL;
 	this->cfg_pwdc = 0;
@@ -43,12 +43,13 @@ clish_shell_init(clish_shell_t * this,
 
 /*-------------------------------------------------------- */
 clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
-			       void *cookie, FILE * istream)
+		void *cookie, FILE * istream, FILE * ostream)
 {
 	clish_shell_t *this = malloc(sizeof(clish_shell_t));
 
 	if (this) {
-		clish_shell_init(this, hooks, cookie, istream);
+		clish_shell_init(this, hooks, cookie, 
+			istream, ostream);
 
 		if (hooks->init_fn) {
 			/* now call the client initialisation */

+ 11 - 6
clish/shell/shell_spawn.c

@@ -162,7 +162,8 @@ static void *clish_shell_thread(void *arg)
 	/* create a shell object... */
 	this = context->shell = clish_shell_new(context->hooks,
 						context->cookie,
-						context->istream);
+						context->istream,
+						context->ostream);
 	assert(this);
 
 	/*
@@ -262,7 +263,8 @@ static void *clish_shell_thread(void *arg)
 /*-------------------------------------------------------- */
 static clish_context_t *_clish_shell_spawn(const pthread_attr_t * attr,
 				     const clish_shell_hooks_t * hooks,
-				     void *cookie, FILE * istream)
+				     void *cookie, FILE * istream,
+				     FILE * ostream)
 {
 	int rtn;
 	clish_context_t *context = malloc(sizeof(clish_context_t));
@@ -272,6 +274,7 @@ static clish_context_t *_clish_shell_spawn(const pthread_attr_t * attr,
 		context->hooks = hooks;
 		context->cookie = cookie;
 		context->istream = istream;
+		context->ostream = ostream;
 		context->shell = NULL;
 		context->prompt = NULL;
 		context->pargv = NULL;
@@ -293,7 +296,8 @@ _clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks,
 			    void *cookie, FILE * file)
 {
 	void *result = NULL;
-	clish_context_t *context = _clish_shell_spawn(NULL, hooks, cookie, file);
+	clish_context_t *context = _clish_shell_spawn(NULL, hooks, cookie, 
+		file, stdout);
 
 	if (context) {
 		/* join the shell's thread and wait for it to exit */
@@ -323,9 +327,10 @@ int clish_shell_wait(clish_context_t * this)
 /*-------------------------------------------------------- */
 clish_context_t *clish_shell_spawn_stream(const pthread_attr_t * attr,
 				     const clish_shell_hooks_t * hooks,
-				     void *cookie, FILE * istream)
+				     void *cookie, FILE * istream,
+				     FILE * ostream)
 {
-	return _clish_shell_spawn(attr, hooks, cookie, istream);
+	return _clish_shell_spawn(attr, hooks, cookie, istream, ostream);
 }
 
 /*-------------------------------------------------------- */
@@ -344,7 +349,7 @@ clish_shell_spawn(pthread_t * pthread,
 	bool_t result = BOOL_FALSE;
 
 	/* spawn the thread... */
-	context = _clish_shell_spawn(attr, hooks, cookie, stdin);
+	context = _clish_shell_spawn(attr, hooks, cookie, stdin, stdout);
 
 	if (NULL != context) {
 		result = BOOL_TRUE;

+ 16 - 2
clish/shell/shell_tinyrl.c

@@ -91,7 +91,7 @@ static clish_pargv_status_t clish_shell_tinyrl_expand(tinyrl_t * this)
 		break;
 	case 2:
 		/* just display line */
-		printf("\n%s", buffer);
+		fprintf(tinyrl__get_ostream(this), "\n%s", buffer);
 		free(buffer);
 		buffer = NULL;
 		break;
@@ -261,7 +261,7 @@ static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
 			case clish_BAD_CMD:
 			case clish_BAD_PARAM:
 				tinyrl_crlf(this);
-				printf("Error. Illegal command line.\n");
+				fprintf(stderr, "Error. Illegal command line.\n");
 				tinyrl_crlf(this);
 				tinyrl_reset_line_state(this);
 				break;
@@ -399,3 +399,17 @@ clish_shell_readline(clish_shell_t * this,
 }
 
 /*-------------------------------------------------------- */
+
+FILE * clish_shell__get_istream(const clish_shell_t * this)
+{
+	return tinyrl__get_istream(this->tinyrl);
+}
+
+/*-------------------------------------------------------- */
+
+FILE * clish_shell__get_ostream(const clish_shell_t * this)
+{
+	return tinyrl__get_ostream(this->tinyrl);
+}
+
+/*-------------------------------------------------------- */