Browse Source

Implementation of 'restore' feature

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

+ 2 - 2
clish/clish_config_callback.c

@@ -82,7 +82,7 @@ clish_config_callback(const clish_shell_t * shell,
 
 			for (i = 0; i < clish_command__get_depth(cmd); i++) {
 				const char *str =
-				    clish_shell__get_pwd(shell, i);
+				    clish_shell__get_pwd_line(shell, i);
 				if (!str)
 					return BOOL_FALSE;
 				lub_string_cat(&command, " \"");
@@ -121,7 +121,7 @@ clish_config_callback(const clish_shell_t * shell,
 
 			for (i = 0; i < clish_command__get_depth(cmd); i++) {
 				const char *str =
-				    clish_shell__get_pwd(shell, i);
+				    clish_shell__get_pwd_line(shell, i);
 				if (!str)
 					return BOOL_FALSE;
 				lub_string_cat(&command, " \"");

+ 1 - 0
clish/command.h

@@ -97,5 +97,6 @@ bool_t clish_command__get_seq(const clish_command_t * instance);
 void clish_command__set_seq(clish_command_t * instance, bool_t seq);
 void clish_command__set_seq_num(clish_command_t * instance, unsigned short seq_num);
 unsigned short clish_command__get_seq_num(const clish_command_t * instance);
+clish_view_restore_t clish_command__get_restore(const clish_command_t * instance);
 
 #endif				/* _clish_command_h */

+ 9 - 0
clish/command/command.c

@@ -499,3 +499,12 @@ unsigned short clish_command__get_seq_num(const clish_command_t * this)
 {
 	return this->seq_num;
 }
+
+/*--------------------------------------------------------- */
+clish_view_restore_t clish_command__get_restore(const clish_command_t * this)
+{
+	if (!this->pview)
+		return CLISH_RESTORE_NONE;
+	return clish_view__get_restore(this->pview);
+}
+

+ 6 - 2
clish/shell.h

@@ -317,8 +317,12 @@ tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
 void *clish_shell__get_client_cookie(const clish_shell_t * instance);
 void
 clish_shell__set_pwd(clish_shell_t * instance, unsigned index,
-		     const char *element);
-const char *clish_shell__get_pwd(const clish_shell_t * instance,
+	const char * line, clish_view_t * view, char * viewid);
+char *clish_shell__get_pwd_line(const clish_shell_t * instance,
+				 unsigned index);
+clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
+				 unsigned index);
+char *clish_shell__get_pwd_viewid(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);

+ 7 - 1
clish/shell/private.h

@@ -51,6 +51,12 @@ struct clish_shell_file_s {
 	bool_t stop_on_error;	/* stop on error for file input  */
 };
 
+typedef struct {
+	char *line;
+	clish_view_t *view;
+	char *viewid;
+} clish_shell_pwd_t;
+
 struct clish_shell_s {
 	lub_bintree_t view_tree;	/* Maintain a tree of views      */
 	lub_bintree_t ptype_tree;	/* Maintain a tree of ptypes     */
@@ -65,7 +71,7 @@ struct clish_shell_s {
 	char *viewid;		/* The current view ID string     */
 	tinyrl_t *tinyrl;	/* Tiny readline instance          */
 	clish_shell_file_t *current_file;	/* file currently in use for input */
-	char **cfg_pwdv;	/* Levels for the config file structure */
+	clish_shell_pwd_t **cfg_pwdv;	/* Levels for the config file structure */
 	unsigned cfg_pwdc;
 	konf_client_t *client;
 	clish_pargv_t *completion_pargv;

+ 3 - 1
clish/shell/shell_delete.c

@@ -46,7 +46,9 @@ static void clish_shell_fini(clish_shell_t * this)
 
 	/* finalize each of the pwd strings */
 	for (i = 0; i < this->cfg_pwdc; i++) {
-		lub_string_free(this->cfg_pwdv[i]);
+		lub_string_free(this->cfg_pwdv[i]->line);
+		lub_string_free(this->cfg_pwdv[i]->viewid);
+		free(this->cfg_pwdv[i]);
 	}
 	/* free the pwd vector */
 	free(this->cfg_pwdv);

+ 38 - 8
clish/shell/shell_execute.c

@@ -196,6 +196,33 @@ clish_shell_execute(clish_shell_t * this,
 
 	assert(NULL != cmd);
 
+	/* Pre-change view if the command is from another depth/view */
+        {
+		clish_view_t *view = NULL;
+		char *viewid = NULL;
+		clish_view_restore_t restore = clish_command__get_restore(cmd);
+
+		if ((CLISH_RESTORE_VIEW == restore) &&
+			(clish_command__get_pview(cmd) != this->view))
+			view = clish_command__get_pview(cmd);
+		else if ((CLISH_RESTORE_DEPTH == restore) &&
+			(clish_command__get_depth(cmd) <
+			clish_view__get_depth(this->view))) {
+			view = clish_shell__get_pwd_view(this,
+				clish_command__get_depth(cmd));
+			viewid = clish_shell__get_pwd_viewid(this,
+				clish_command__get_depth(cmd));
+		}
+
+		if (NULL != view) {
+			this->view = view;
+			/* cleanup */
+			lub_string_free(this->viewid);
+			this->viewid = viewid;
+		}
+	}
+
+	/* Execute action */
 	builtin = clish_command__get_builtin(cmd);
 	script = clish_command__get_action(cmd, this->viewid, *pargv);
 	/* account for thread cancellation whilst running a script */
@@ -229,27 +256,30 @@ clish_shell_execute(clish_shell_t * this,
 
 	}
 	pthread_cleanup_pop(1);
+
 	if (BOOL_TRUE == result) {
-		/* move into the new view */
-		clish_view_t *view = clish_command__get_view(cmd);
-		char *viewid =
-		    clish_command__get_viewid(cmd, this->viewid, *pargv);
+		clish_view_t *view = NULL;
+		char *viewid = NULL;
 
-		/* now get the client to config operations */
+		/* Now get the client to config operations */
 		if (this->client_hooks->config_fn)
 			this->client_hooks->config_fn(this, cmd, *pargv);
 
+		/* Move into the new view */
+		view = clish_command__get_view(cmd);
+		viewid = clish_command__get_viewid(cmd, this->viewid, *pargv);
+
 		if (NULL != view) {
 			/* Save the current config PWD */
 			char *line = clish_variable__get_line(cmd, *pargv);
 			clish_shell__set_pwd(this,
-					     clish_command__get_depth(cmd),
-					     line);
+				clish_command__get_depth(cmd),
+				line, this->view, this->viewid);
 			lub_string_free(line);
 			/* Change view */
 			this->view = view;
 		}
-		if (viewid) {
+		if (viewid || view) {
 			/* cleanup */
 			lub_string_free(this->viewid);
 			this->viewid = viewid;

+ 39 - 10
clish/shell/shell_pwd.c

@@ -9,34 +9,63 @@
 
 /*--------------------------------------------------------- */
 void
-clish_shell__set_pwd(clish_shell_t * this, unsigned index, const char *element)
+clish_shell__set_pwd(clish_shell_t * this, unsigned index,
+	const char * line, clish_view_t * view, char * viewid)
 {
-	char **tmp;
+	clish_shell_pwd_t **tmp;
 	size_t new_size = 0;
 	unsigned i;
 
+	/* Create new element */
 	if (index >= this->cfg_pwdc) {
-		/* create new element */
-		new_size = (index + 1) * sizeof(char *);
+		new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
 		/* resize the pwd vector */
 		tmp = realloc(this->cfg_pwdv, new_size);
 		assert(tmp);
 		this->cfg_pwdv = tmp;
-		for (i = this->cfg_pwdc; i <= index; i++)
-			this->cfg_pwdv[i] = NULL;
+		/* Initialize new elements */
+		for (i = this->cfg_pwdc; i <= index; i++) {
+			clish_shell_pwd_t *pwd;
+
+			pwd = malloc(sizeof(*pwd));
+			assert(pwd);
+			pwd->line = NULL;
+			pwd->view = NULL;
+			pwd->viewid = NULL;
+			this->cfg_pwdv[i] = pwd;
+		}
 		this->cfg_pwdc = index + 1;
 	}
 
-	lub_string_free(this->cfg_pwdv[index]);
-	this->cfg_pwdv[index] = lub_string_dup(element);
+	lub_string_free(this->cfg_pwdv[index]->line);
+	this->cfg_pwdv[index]->line = line ? lub_string_dup(line) : NULL;
+	this->cfg_pwdv[index]->view = view;
+	lub_string_free(this->cfg_pwdv[index]->viewid);
+	this->cfg_pwdv[index]->viewid = viewid ? lub_string_dup(viewid) : NULL;
+}
+
+char *clish_shell__get_pwd_line(const clish_shell_t * this, unsigned index)
+{
+	if (index >= this->cfg_pwdc)
+		return NULL;
+
+	return this->cfg_pwdv[index]->line;
+}
+
+clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned index)
+{
+	if (index >= this->cfg_pwdc)
+		return NULL;
+
+	return this->cfg_pwdv[index]->view;
 }
 
-const char *clish_shell__get_pwd(const clish_shell_t * this, unsigned index)
+char *clish_shell__get_pwd_viewid(const clish_shell_t * this, unsigned index)
 {
 	if (index >= this->cfg_pwdc)
 		return NULL;
 
-	return this->cfg_pwdv[index];
+	return this->cfg_pwdv[index]->viewid;
 }
 
 konf_client_t *clish_shell__get_client(const clish_shell_t * this)