Browse Source

Fix PARAM's default field behaviour

git-svn-id: https://klish.googlecode.com/svn/trunk@415 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
df765aff69
4 changed files with 42 additions and 33 deletions
  1. 4 0
      clish/param.h
  2. 30 0
      clish/param/param.c
  3. 2 32
      clish/pargv/pargv.c
  4. 6 1
      clish/variable/variable_expand.c

+ 4 - 0
clish/param.h

@@ -93,6 +93,10 @@ void clish_paramv_insert(clish_paramv_t * instance, clish_param_t * param);
 clish_param_t *clish_paramv__get_param(const clish_paramv_t * instance,
 				unsigned index);
 const unsigned clish_paramv__get_count(const clish_paramv_t * instance);
+clish_param_t *clish_paramv_find_param(const clish_paramv_t * instance,
+	const char *name);
+const char *clish_paramv_find_default(const clish_paramv_t * instance,
+	const char *name);
 
 #endif				/* _clish_param_h */
 /** @} clish_param */

+ 30 - 0
clish/param/param.c

@@ -290,6 +290,36 @@ clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
 	return result;
 }
 
+/*--------------------------------------------------------- */
+clish_param_t *clish_paramv_find_param(const clish_paramv_t * this,
+	const char *name)
+{
+	clish_param_t *res = NULL;
+	unsigned int i;
+
+	for (i = 0; i < this->paramc; i++) {
+		if (!strcmp(clish_param__get_name(this->paramv[i]), name))
+			return this->paramv[i];
+		if ((res = clish_paramv_find_param(
+			clish_param__get_paramv(this->paramv[i]), name)))
+			return res;
+	}
+
+	return res;
+}
+
+/*--------------------------------------------------------- */
+const char *clish_paramv_find_default(const clish_paramv_t * this,
+	const char *name)
+{
+	clish_param_t *res = clish_paramv_find_param(this, name);
+
+	if (res)
+		return clish_param__get_default(res);
+
+	return NULL;
+}
+
 /*--------------------------------------------------------- */
 const unsigned clish_paramv__get_count(const clish_paramv_t * this)
 {

+ 2 - 32
clish/pargv/pargv.c

@@ -67,34 +67,6 @@ int clish_pargv_insert(clish_pargv_t * this,
 	return 0;
 }
 
-#if 0
-/*--------------------------------------------------------- */
-static void set_defaults(clish_pargv_t * this, const clish_command_t * cmd)
-{
-	unsigned index = 0;
-	const clish_param_t *param;
-
-	/* scan through all the parameters for this command */
-	while ((param = clish_command__get_param(cmd, index++))) {
-		const char *defval = clish_param__get_default(param);
-		if (NULL != defval) {
-			if ('\0' != *defval) {
-				/* add the translated value to the vector */
-				char *translated =
-				    clish_ptype_translate(clish_param__get_ptype
-							  (param),
-							  defval);
-				clish_pargv_insert(this, param, translated);
-				lub_string_free(translated);
-			} else {
-				/* insert the empty default */
-				clish_pargv_insert(this, param, defval);
-			}
-		}
-	}
-}
-#endif
-
 /*--------------------------------------------------------- */
 clish_pargv_status_t clish_pargv_parse(clish_pargv_t * this,
 	const clish_command_t * cmd,
@@ -120,15 +92,13 @@ clish_pargv_status_t clish_pargv_parse(clish_pargv_t * this,
 		up_level = 1;
 
 	while (index < paramc) {
-		const char *arg;
+		const char *arg = NULL;
 		clish_param_t *param = clish_paramv__get_param(paramv,index);
 		clish_param_t *cparam = NULL;
 		int is_switch = 0;
 
 		/* Use real arg or PARAM's default value as argument */
-		if (*idx >= argc)
-			arg = clish_param__get_default(param);
-		else
+		if (*idx < argc)
 			arg = lub_argv__get_arg(argv, *idx);
 
 		/* Is parameter in "switch" mode? */

+ 6 - 1
clish/variable/variable_expand.c

@@ -110,7 +110,12 @@ static char *context_retrieve(const context_t * this, const char *name)
 		if (parg)
 			tmp = clish_parg__get_value(parg);
 	}
-
+	/* try and substitute the param's default */
+	if (!tmp) {
+		if (this && this->cmd)
+			tmp = clish_paramv_find_default(
+				clish_command__get_paramv(this->cmd), name);
+	}
 	/* try and substitute a viewId variable */
 	if (!tmp) {
 		if (this && this->viewid)