Browse Source

Function to expand only specific types of var. By Peter Kosyh

Serj Kalichev 8 years ago
parent
commit
690495c606
2 changed files with 27 additions and 6 deletions
  1. 10 0
      clish/shell.h
  2. 17 6
      clish/shell/shell_var.c

+ 10 - 0
clish/shell.h

@@ -72,6 +72,15 @@ typedef enum {
 	SHELL_VAR_REGEX /* Variable expanding for regex usage */
 } clish_shell_var_t;
 
+typedef enum {
+	SHELL_EXPAND_PARAM = 1,
+	SHELL_EXPAND_VIEW = 2,
+	SHELL_EXPAND_CONTEXT = 4,
+	SHELL_EXPAND_VAR = 8,
+	SHELL_EXPAND_ENV = 16,
+	SHELL_EXPAND_ALL = 255
+} clish_shell_expand_e;
+
 _BEGIN_C_DECL
 
 /*-----------------
@@ -121,6 +130,7 @@ int clish_shell_push_fd(clish_shell_t * instance, FILE * file,
 void clish_shell_insert_var(clish_shell_t *instance, clish_var_t *var);
 clish_var_t *clish_shell_find_var(clish_shell_t *instance, const char *name);
 char *clish_shell_expand_var(const char *name, clish_context_t *context);
+char *clish_shell_expand_var_ex(const char *name, clish_context_t *context, clish_shell_expand_e flags);
 char *clish_shell_expand(const char *str, clish_shell_var_t vtype, clish_context_t *context);
 
 /*-----------------

+ 17 - 6
clish/shell/shell_var.c

@@ -479,6 +479,12 @@ char *clish_shell__get_full_line(clish_context_t *context)
 
 /*--------------------------------------------------------- */
 char *clish_shell_expand_var(const char *name, clish_context_t *context)
+{
+	return clish_shell_expand_var_ex(name, context, SHELL_EXPAND_ALL);
+}
+
+/*----------------------------------------------------------- */
+char *clish_shell_expand_var_ex(const char *name, clish_context_t *context, clish_shell_expand_e flags)
 {
 	clish_shell_t *this;
 	const clish_command_t *cmd;
@@ -494,26 +500,31 @@ char *clish_shell_expand_var(const char *name, clish_context_t *context)
 	pargv = clish_context__get_pargv(context);
 
 	/* try and substitute a parameter value */
-	if (pargv) {
+	if (pargv && (flags & SHELL_EXPAND_PARAM)) {
 		const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
 		if (parg)
 			tmp = clish_parg__get_value(parg);
 	}
+
 	/* try and substitute the param's default */
-	if (!tmp && cmd)
+	if (!tmp && cmd && (flags & SHELL_EXPAND_PARAM))
 		tmp = clish_paramv_find_default(
 			clish_command__get_paramv(cmd), name);
+
 	/* try and substitute a viewId variable */
-	if (!tmp && this)
+	if (!tmp && this && (flags & SHELL_EXPAND_VIEW))
 		tmp = string = find_viewid_var(name, context);
+
 	/* try and substitute context fixed variable */
-	if (!tmp)
+	if (!tmp && (flags & SHELL_EXPAND_CONTEXT))
 		tmp = string = find_context_var(name, context);
+
 	/* try and substitute a global var value */
-	if (!tmp && this)
+	if (!tmp && this && (flags & SHELL_EXPAND_VAR))
 		tmp = string = find_global_var(name, context);
+
 	/* get the contents of an environment variable */
-	if (!tmp)
+	if (!tmp && (flags & SHELL_EXPAND_ENV))
 		tmp = getenv(name);
 
 	if (string)