Browse Source

Refactoring for __cur_depth, __cur_pwd

Serj Kalichev 10 years ago
parent
commit
f354c2d6e4
5 changed files with 25 additions and 40 deletions
  1. 0 4
      clish/shell/private.h
  2. 0 20
      clish/shell/shell_new.c
  3. 0 14
      clish/shell/shell_parse.c
  4. 2 2
      clish/shell/shell_pwd.c
  5. 23 0
      clish/shell/shell_var.c

+ 0 - 4
clish/shell/private.h

@@ -65,10 +65,6 @@ struct clish_shell_s {
 	bool_t log; /* If command logging is enabled */
 	int log_facility; /* Syslog facility */
 	struct passwd *user; /* Current user information */
-
-	/* Static params for var expanding. The refactoring is needed. */
-	clish_param_t *param_depth;
-	clish_param_t *param_pwd;
 };
 
 /**

+ 0 - 20
clish/shell/shell_new.c

@@ -64,22 +64,6 @@ static void clish_shell_init(clish_shell_t * this,
 	this->user = lub_db_getpwuid(getuid()); /* Get user information */
 
 	/* Create internal ptypes and params */
-	/* Current depth */
-	tmp_ptype = clish_shell_find_create_ptype(this,
-		"__DEPTH", "Depth", "[0-9]+",
-		CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
-	assert(tmp_ptype);
-	this->param_depth = clish_param_new("_cur_depth",
-		"Current depth", tmp_ptype);
-	clish_param__set_hidden(this->param_depth, BOOL_TRUE);
-	/* Current pwd */
-	tmp_ptype = clish_shell_find_create_ptype(this,
-		"__PWD", "Path", ".+",
-		CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
-	assert(tmp_ptype);
-	this->param_pwd = clish_param_new("_cur_pwd",
-		"Current path", tmp_ptype);
-	clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
 	/* Args */
 	tmp_ptype = clish_shell_find_create_ptype(this,
 		"internal_ARGS",
@@ -142,10 +126,6 @@ static void clish_shell_fini(clish_shell_t * this)
 	free(this->pwdv);
 	konf_client_free(this->client);
 
-	/* Free internal params */
-	clish_param_delete(this->param_depth);
-	clish_param_delete(this->param_pwd);
-
 	lub_string_free(this->lockfile);
 	lub_string_free(this->default_shebang);
 	free(this->user);

+ 0 - 14
clish/shell/shell_parse.c

@@ -40,20 +40,6 @@ clish_pargv_status_t clish_shell_parse(
 		clish_pargv_delete(*pargv);
 		*pargv = NULL;
 	}
-	if (*pargv) {
-		char str[100];
-		char * tmp;
-		/* Variable __cur_depth */
-		int depth = clish_shell__get_depth(this);
-		snprintf(str, sizeof(str) - 1, "%u", depth);
-		clish_pargv_insert(*pargv, this->param_depth, str);
-		/* Variable __cur_pwd */
-		tmp = clish_shell__get_pwd_full(this, depth);
-		if (tmp) {
-			clish_pargv_insert(*pargv, this->param_pwd, tmp);
-			lub_string_free(tmp);
-		}
-	}
 
 	return result;
 }

+ 2 - 2
clish/shell/shell_pwd.c

@@ -84,10 +84,10 @@ char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
 }
 
 /*--------------------------------------------------------- */
-char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned depth)
+char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
 {
 	char *pwd = NULL;
-	unsigned i;
+	unsigned int i;
 
 	for (i = 1; i <= depth; i++) {
 		const char *str =

+ 23 - 0
clish/shell/shell_var.c

@@ -57,44 +57,56 @@ static char *find_context_var(const char *name, clish_context_t *this)
 			tinyrl__get_width(shell->tinyrl));
 		tmp[sizeof(tmp) - 1] = '\0';
 		result = strdup(tmp);
+
 	} else if (!lub_string_nocasecmp(name, "_height")) {
 		char tmp[5];
 		snprintf(tmp, sizeof(tmp), "%u",
 			tinyrl__get_height(shell->tinyrl));
 		tmp[sizeof(tmp) - 1] = '\0';
 		result = strdup(tmp);
+
 	} else if (!lub_string_nocasecmp(name, "_watchdog_timeout")) {
 		char tmp[5];
 		snprintf(tmp, sizeof(tmp), "%u", shell->wdog_timeout);
 		tmp[sizeof(tmp) - 1] = '\0';
 		result = strdup(tmp);
+
 	} else if (!this->cmd) { /* The vars dependent on command */
 		return NULL;
+
 	} else if (!lub_string_nocasecmp(name, "_full_cmd")) {
 		result = lub_string_dup(clish_command__get_name(this->cmd));
+
 	} else if (!lub_string_nocasecmp(name, "_cmd")) {
 		result = lub_string_dup(clish_command__get_name(
 			clish_command__get_cmd(this->cmd)));
+
 	} else if (!lub_string_nocasecmp(name, "_orig_cmd")) {
 		result = lub_string_dup(clish_command__get_name(
 			clish_command__get_orig(this->cmd)));
+
 	} else if (!lub_string_nocasecmp(name, "_line")) {
 		result = clish_shell__get_line(this);
+
 	} else if (!lub_string_nocasecmp(name, "_full_line")) {
 		result = clish_shell__get_full_line(this);
+
 	} else if (!lub_string_nocasecmp(name, "_params")) {
 		if (this->pargv)
 			result = clish_shell__get_params(this);
+
 	} else if (!lub_string_nocasecmp(name, "_interactive")) {
 		if (clish_shell__get_interactive(this->shell))
 			result = strdup("1");
 		else
 			result = strdup("0");
+
 	} else if (!lub_string_nocasecmp(name, "_isatty")) {
 		if (tinyrl__get_isatty(this->shell->tinyrl))
 			result = strdup("1");
 		else
 			result = strdup("0");
+
 	} else if (lub_string_nocasestr(name, "_prefix") == name) {
 		int idx = 0;
 		int pnum = 0;
@@ -108,6 +120,17 @@ static char *find_context_var(const char *name, clish_context_t *this)
 			result = lub_string_dup(lub_argv__get_arg(argv, idx));
 			lub_argv_delete(argv);
 		}
+
+	} else if (!lub_string_nocasecmp(name, "_cur_depth")) {
+		char tmp[10];
+		int depth = clish_shell__get_depth(shell);
+		snprintf(tmp, sizeof(tmp), "%u", depth);
+		tmp[sizeof(tmp) - 1] = '\0';
+		result = strdup(tmp);
+
+	} else if (!lub_string_nocasecmp(name, "_cur_pwd")) {
+		int depth = clish_shell__get_depth(shell);
+		result = clish_shell__get_pwd_full(shell, depth);
 	}
 
 	return result;