Browse Source

Use saved value of static variable. Renew prompt when empty line

git-svn-id: https://klish.googlecode.com/svn/trunk@432 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
6f6d9523ad
9 changed files with 89 additions and 23 deletions
  1. 2 2
      clish/callback_script.c
  2. 20 1
      clish/shell/shell_tinyrl.c
  3. 31 13
      clish/shell/shell_var.c
  4. 2 0
      clish/var.h
  5. 1 0
      clish/var/private.h
  6. 16 0
      clish/var/var.c
  7. 1 1
      tinyrl/private.h
  8. 15 6
      tinyrl/tinyrl.c
  9. 1 0
      tinyrl/tinyrl.h

+ 2 - 2
clish/callback_script.c

@@ -41,12 +41,12 @@ bool_t clish_script_callback(clish_context_t *context,
 	sigset_t sig_set;
 
 	assert(this);
-	assert(cmd);
 	if (!script) /* Nothing to do */
 		return BOOL_TRUE;
 
 	/* Find out shebang */
-	shebang = clish_action__get_shebang(clish_command__get_action(cmd));
+	if (cmd)
+		shebang = clish_action__get_shebang(clish_command__get_action(cmd));
 	if (!shebang)
 		shebang = clish_shell__get_default_shebang(this);
 	assert(shebang);

+ 20 - 1
clish/shell/shell_tinyrl.c

@@ -18,6 +18,22 @@
 
 #include "lub/string.h"
 
+/*-------------------------------------------------------- */
+static void clish_shell_renew_prompt(tinyrl_t *this)
+{
+	clish_context_t *context = tinyrl__get_context(this);
+	char *prompt = NULL;
+	const clish_view_t *view;
+
+	/* Obtain the prompt */
+	view = clish_shell__get_view(context->shell);
+	assert(view);
+	prompt = clish_view__get_prompt(view, context);
+	assert(prompt);
+	tinyrl__set_prompt(this, prompt);
+	lub_string_free(prompt);
+}
+
 /*-------------------------------------------------------- */
 static bool_t clish_shell_tinyrl_key_help(tinyrl_t * this, int key)
 {
@@ -186,13 +202,16 @@ static bool_t clish_shell_tinyrl_key_space(tinyrl_t * this, int key)
 }
 
 /*-------------------------------------------------------- */
-static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
+static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
 {
 	clish_context_t *context = tinyrl__get_context(this);
 	const clish_command_t *cmd = NULL;
 	const char *line = tinyrl__get_line(this);
 	bool_t result = BOOL_FALSE;
 
+	/* Renew prompt */
+	clish_shell_renew_prompt(this);
+
 	/* nothing to pass simply move down the screen */
 	if (!*line) {
 		tinyrl_crlf(this);

+ 31 - 13
clish/shell/shell_var.c

@@ -116,28 +116,46 @@ static char *find_global_var(const char *name, clish_context_t *context)
 	clish_shell_t *this = context->shell;
 	clish_var_t *var = clish_shell_find_var(this, name);
 	clish_action_t *action;
-	char *value = NULL;
-	char *script = NULL;
+	char *value;
+	char *script;
+	bool_t dynamic;
+	char *res = NULL;
 
 	if (!var)
 		return NULL;
 
-	/* Try the value field */
+	/* Try to get saved value for static var */
+	dynamic = clish_var__get_dynamic(var);
+	if (!dynamic) {
+		char *saved = clish_var__get_saved(var);
+		if (saved)
+			return lub_string_dup(saved);
+	}
+
+	/* Try to expand value field */
 	value = clish_var__get_value(var);
 	if (value)
-		return clish_shell_expand(value, context);
-	action = clish_var__get_action(var);
-	script = clish_action__get_script(action);
-	if (script) {
-		char *out = NULL;
-		if (!clish_shell_exec_action(action, context, &out)) {
-			lub_string_free(out);
-			return NULL;
+		res = clish_shell_expand(value, context);
+
+	/* Try to execute ACTION */
+	if (!res) {
+		action = clish_var__get_action(var);
+		script = clish_action__get_script(action);
+		if (script) {
+			char *out = NULL;
+			if (!clish_shell_exec_action(action, context, &out)) {
+				lub_string_free(out);
+				return NULL;
+			}
+			res = out;
 		}
-		return out;
 	}
 
-	return NULL;
+	/* Save value for static var */
+	if (!dynamic && res)
+		clish_var__set_saved(var, res);
+
+	return res;
 }
 
 /*--------------------------------------------------------- */

+ 2 - 0
clish/var.h

@@ -34,5 +34,7 @@ bool_t clish_var__get_dynamic(const clish_var_t *instance);
 void clish_var__set_value(clish_var_t *instance, const char *value);
 char *clish_var__get_value(const clish_var_t *instance);
 clish_action_t *clish_var__get_action(const clish_var_t *instance);
+void clish_var__set_saved(clish_var_t *instance, const char *value);
+char *clish_var__get_saved(const clish_var_t *instance);
 
 #endif /* _clish_var_h */

+ 1 - 0
clish/var/private.h

@@ -12,5 +12,6 @@ struct clish_var_s {
 	char *name;
 	bool_t dynamic;
 	char *value;
+	char *saved; /* Saved value of static variable */
 	clish_action_t *action;
 };

+ 16 - 0
clish/var/var.c

@@ -20,6 +20,7 @@ static void clish_var_init(clish_var_t *this, const char *name)
 	this->dynamic = BOOL_FALSE;
 	this->value = NULL;
 	this->action = clish_action_new();
+	this->saved = NULL;
 
 	/* Be a good binary tree citizen */
 	lub_bintree_node_init(&this->bt_node);
@@ -31,6 +32,7 @@ static void clish_var_fini(clish_var_t *this)
 	lub_string_free(this->name);
 	lub_string_free(this->value);
 	clish_action_delete(this->action);
+	lub_string_free(this->saved);
 }
 
 /*---------------------------------------------------------
@@ -125,3 +127,17 @@ clish_action_t *clish_var__get_action(const clish_var_t *this)
 {
 	return this->action;
 }
+
+/*--------------------------------------------------------- */
+void clish_var__set_saved(clish_var_t *this, const char *value)
+{
+	if (this->saved)
+		lub_string_free(this->saved);
+	this->saved = lub_string_dup(value);
+}
+
+/*--------------------------------------------------------- */
+char *clish_var__get_saved(const clish_var_t *this)
+{
+	return this->saved;
+}

+ 1 - 1
tinyrl/private.h

@@ -7,7 +7,7 @@
 struct _tinyrl {
 	const char *line;
 	unsigned max_line_length;
-	const char *prompt;
+	char *prompt;
 	size_t prompt_size;
 	char *buffer;
 	size_t buffer_size;

+ 15 - 6
tinyrl/tinyrl.c

@@ -390,11 +390,9 @@ static void tinyrl_fini(tinyrl_t * this)
 
 	/* free up any dynamic strings */
 	lub_string_free(this->buffer);
-	this->buffer = NULL;
 	lub_string_free(this->kill_string);
-	this->kill_string = NULL;
 	lub_string_free(this->last_buffer);
-	this->last_buffer = NULL;
+	lub_string_free(this->prompt);
 }
 
 /*-------------------------------------------------------- */
@@ -694,11 +692,10 @@ static char *internal_readline(tinyrl_t * this,
 	this->buffer = lub_string_dup("");
 	this->buffer_size = strlen(this->buffer);
 	this->line = this->buffer;
-	this->prompt = prompt;
-	this->prompt_size = strlen(prompt);
+	tinyrl__set_prompt(this, prompt);
 	this->context = context;
 
-	if ((BOOL_TRUE == this->isatty) && (!str)) {
+	if (this->isatty && !str) {
 		/* set the terminal into raw input mode */
 		tty_set_raw_mode(this);
 		tinyrl_reset_line_state(this);
@@ -1311,6 +1308,18 @@ const char *tinyrl__get_prompt(const tinyrl_t * this)
 	return this->prompt;
 }
 
+/*-------------------------------------------------------- */
+void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
+{
+	if (this->prompt) {
+		lub_string_free(this->prompt);
+		this->prompt_size = 0;
+	}
+	this->prompt = lub_string_dup(prompt);
+	if (this->prompt)
+		this->prompt_size = strlen(this->prompt);
+}
+
 /*-------------------------------------------------------- */
 bool_t tinyrl__get_utf8(const tinyrl_t * this)
 {

+ 1 - 0
tinyrl/tinyrl.h

@@ -81,6 +81,7 @@ extern void tinyrl_delete(tinyrl_t * instance);
 extern tinyrl_history_t *tinyrl__get_history(const tinyrl_t * instance);
 
 extern const char *tinyrl__get_prompt(const tinyrl_t * instance);
+extern void tinyrl__set_prompt(tinyrl_t *instance, const char *prompt);
 
 extern void tinyrl_done(tinyrl_t * instance);