Browse Source

The variable can execute ACTION to expand

git-svn-id: https://klish.googlecode.com/svn/trunk@431 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
9950865913
3 changed files with 71 additions and 38 deletions
  1. 2 0
      clish/shell.h
  2. 37 31
      clish/shell/shell_execute.c
  3. 32 7
      clish/shell/shell_var.c

+ 2 - 0
clish/shell.h

@@ -307,6 +307,8 @@ clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * instance,
 	clish_ptype_preprocess_e preprocess);
 int clish_shell_xml_read(clish_shell_t * instance, const char *filename);
 void clish_shell_help(clish_shell_t * instance, const char *line);
+bool_t clish_shell_exec_action(clish_action_t *action,
+	clish_context_t *context, char **out);
 bool_t clish_shell_execute(clish_context_t *context, char **out);
 bool_t clish_shell_line(clish_shell_t * instance, const char *prompt,
 	const clish_command_t ** cmd, clish_pargv_t ** pargv, const char *str);

+ 37 - 31
clish/shell/shell_execute.c

@@ -183,8 +183,6 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 	clish_pargv_t *pargv = context->pargv;
 	clish_action_t *action;
 	bool_t result = BOOL_TRUE;
-	const char *builtin;
-	char *script;
 	char *lock_path = clish_shell__get_lockfile(this);
 	int lock_fd = -1;
 	sigset_t old_sigs;
@@ -266,35 +264,7 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 	}
 
 	/* Execute ACTION */
-	builtin = clish_action__get_builtin(action);
-	script = clish_shell_expand(clish_action__get_script(action), context);
-	/* account for thread cancellation whilst running a script */
-	pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
-		script);
-	if (builtin) {
-		clish_shell_builtin_fn_t *callback;
-		lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
-
-		result = BOOL_FALSE;
-
-		/* search for an internal command */
-		callback = find_builtin_callback(clish_cmd_list, builtin);
-
-		if (!callback) {
-			/* search for a client command */
-			callback = find_builtin_callback(
-				this->client_hooks->cmd_list, builtin);
-		}
-		/* invoke the builtin callback */
-		if (callback)
-			result = callback(this, argv);
-		if (argv)
-			lub_argv_delete(argv);
-	} else if (script) {
-		/* now get the client to interpret the resulting script */
-		result = this->client_hooks->script_fn(context, script, out);
-	}
-	pthread_cleanup_pop(1);
+	result = clish_shell_exec_action(action, context, out);
 
 	/* Restore SIGINT and SIGQUIT */
 	if (!clish_command__get_interrupt(cmd)) {
@@ -345,6 +315,42 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 	return result;
 }
 
+/*----------------------------------------------------------- */
+bool_t clish_shell_exec_action(clish_action_t *action,
+	clish_context_t *context, char **out)
+{
+	clish_shell_t *this = context->shell;
+	bool_t result = BOOL_TRUE;
+	const char *builtin;
+	char *script;
+
+	builtin = clish_action__get_builtin(action);
+	script = clish_shell_expand(clish_action__get_script(action), context);
+	if (builtin) {
+		clish_shell_builtin_fn_t *callback;
+		lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
+		result = BOOL_FALSE;
+		/* search for an internal command */
+		callback = find_builtin_callback(clish_cmd_list, builtin);
+		if (!callback) {
+			/* search for a client command */
+			callback = find_builtin_callback(
+				this->client_hooks->cmd_list, builtin);
+		}
+		/* invoke the builtin callback */
+		if (callback)
+			result = callback(this, argv);
+		if (argv)
+			lub_argv_delete(argv);
+	} else if (script) {
+		/* now get the client to interpret the resulting script */
+		result = this->client_hooks->script_fn(context, script, out);
+	}
+	lub_string_free(script);
+
+	return result;
+}
+
 /*----------------------------------------------------------- */
 /*
  * Find out the previous view in the stack and go to it

+ 32 - 7
clish/shell/shell_var.c

@@ -110,6 +110,36 @@ static char *find_context_var(const char *name, clish_context_t *this)
 	return result;
 }
 
+/*--------------------------------------------------------- */
+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;
+
+	if (!var)
+		return NULL;
+
+	/* Try the 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;
+		}
+		return out;
+	}
+
+	return NULL;
+}
+
 /*--------------------------------------------------------- */
 /*
  * return the next segment of text from the provided string
@@ -299,13 +329,8 @@ char *clish_shell_expand_var(const char *name, void *context)
 	if (!tmp)
 		tmp = string = find_context_var(name, context);
 	/* try and substitute a global var value */
-	if (!tmp && this) {
-		clish_var_t *var = clish_shell_find_var(this, name);
-		/* substitute the command line value */
-		if (var)
-			tmp = string = clish_shell_expand(
-				clish_var__get_value(var), context);
-	}
+	if (!tmp && this)
+		tmp = string = find_global_var(name, context);
 	/* get the contents of an environment variable */
 	if (!tmp)
 		tmp = getenv(name);