Browse Source

Replace pwd struct by cwd (current working dir)

Serj Kalichev 5 years ago
parent
commit
8c78163174

+ 7 - 7
clish/shell.h

@@ -158,19 +158,19 @@ clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
 clish_view_t *clish_shell__set_depth(clish_shell_t *instance, unsigned int depth);
 const char *clish_shell__get_viewid(const clish_shell_t * instance);
 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
-void clish_shell__set_pwd(clish_shell_t *instance, const char * line,
+void clish_shell__set_cwd(clish_shell_t *instance, const char * line,
 	clish_view_t * view, const char * viewid, clish_context_t *context);
-char *clish_shell__get_pwd_line(const clish_shell_t * instance,
+char *clish_shell__get_cwd_line(const clish_shell_t * instance,
 	 unsigned int index);
-clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *instance,
+clish_pargv_t *clish_shell__get_cwd_pargv(const clish_shell_t *instance,
 	unsigned int index);
-char *clish_shell__get_pwd_cmd(const clish_shell_t *instance,
+char *clish_shell__get_cwd_cmd(const clish_shell_t *instance,
 	unsigned int index);
-char *clish_shell__get_pwd_prefix(const clish_shell_t *instance,
+char *clish_shell__get_cwd_prefix(const clish_shell_t *instance,
 	unsigned int index);
-char *clish_shell__get_pwd_full(const clish_shell_t * instance,
+char *clish_shell__get_cwd_full(const clish_shell_t * instance,
 	unsigned int depth);
-clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
+clish_view_t *clish_shell__get_cwd_view(const clish_shell_t * instance,
 	unsigned int index);
 FILE *clish_shell__get_istream(const clish_shell_t * instance);
 FILE *clish_shell__get_ostream(const clish_shell_t * instance);

+ 1 - 1
clish/shell/module.am

@@ -12,7 +12,7 @@ libclish_la_SOURCES += \
 	clish/shell/shell_loop.c \
 	clish/shell/shell_startup.c \
 	clish/shell/shell_wdog.c \
-	clish/shell/shell_pwd.c \
+	clish/shell/shell_cwd.c \
 	clish/shell/shell_tinyrl.c \
 	clish/shell/shell_plugin.c \
 	clish/shell/shell_xml.c \

+ 5 - 5
clish/shell/private.h

@@ -33,7 +33,7 @@ typedef struct {
 	clish_pargv_t *pargv; /* Saved pargv structure */
 	char *cmd; /* Command name without prefix */
 	char *prefix; /* Prefix string if exists */
-} clish_shell_pwd_t;
+} clish_shell_cwd_t;
 
 /* Context structure */
 struct clish_context_s {
@@ -65,8 +65,8 @@ struct clish_shell_s {
 	char *overview; /* Overview text for this shell */
 	tinyrl_t *tinyrl; /* Tiny readline instance */
 	clish_shell_file_t *current_file; /* file currently in use for input */
-	clish_shell_pwd_t **pwdv; /* Levels for the config file structure */
-	unsigned int pwdc;
+	clish_shell_cwd_t **cwdv; /* Levels for the config file structure */
+	unsigned int cwdc;
 	int depth;
 	konf_client_t *client;
 	char *lockfile;
@@ -139,7 +139,7 @@ char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
 	const char *line, unsigned start, unsigned end);
 void clish_shell__expand_viewid(const char *viewid, lub_list_t *vars,
 	clish_context_t *context);
-void clish_shell__init_pwd(clish_shell_pwd_t *pwd);
-void clish_shell__fini_pwd(clish_shell_pwd_t *pwd);
+void clish_shell__init_cwd(clish_shell_cwd_t *cwd);
+void clish_shell__fini_cwd(clish_shell_cwd_t *cwd);
 int clish_shell_timeout_fn(tinyrl_t *tinyrl);
 int clish_shell_keypress_fn(tinyrl_t *tinyrl, int key);

+ 178 - 0
clish/shell/shell_cwd.c

@@ -0,0 +1,178 @@
+/*
+ * shell_cwd.c
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <syslog.h>
+
+#include "lub/string.h"
+#include "private.h"
+
+/*--------------------------------------------------------- */
+void clish_shell__init_cwd(clish_shell_cwd_t *cwd)
+{
+	cwd->line = NULL;
+	cwd->view = NULL;
+	cwd->pargv = NULL;
+	cwd->cmd = NULL;
+	cwd->prefix = NULL;
+
+	/* Init VARs */
+	cwd->viewid = lub_list_new(clish_var_compare, clish_var_delete);
+}
+
+/*--------------------------------------------------------- */
+void clish_shell__fini_cwd(clish_shell_cwd_t *cwd)
+{
+	lub_string_free(cwd->line);
+	lub_string_free(cwd->cmd);
+	if (cwd->prefix)
+		lub_string_free(cwd->prefix);
+	cwd->view = NULL;
+	clish_pargv_delete(cwd->pargv);
+
+	/* Free VARs  */
+	lub_list_free_all(cwd->viewid);
+}
+
+/*--------------------------------------------------------- */
+void clish_shell__set_cwd(clish_shell_t *this,
+	const char *line, clish_view_t *view, const char *viewid, clish_context_t *context)
+{
+	clish_shell_cwd_t **tmp;
+	size_t new_size = 0;
+	unsigned int i;
+	unsigned int index = clish_view__get_depth(view);
+	clish_shell_cwd_t *newcwd;
+	const clish_command_t *full_cmd = clish_context__get_cmd(context);
+
+	/* Create new element */
+	newcwd = malloc(sizeof(*newcwd));
+	assert(newcwd);
+	clish_shell__init_cwd(newcwd);
+
+	/* Resize the cwd vector */
+	if (index >= this->cwdc) {
+		new_size = (index + 1) * sizeof(clish_shell_cwd_t *);
+		tmp = realloc(this->cwdv, new_size);
+		assert(tmp);
+		this->cwdv = tmp;
+		/* Initialize new elements */
+		for (i = this->cwdc; i <= index; i++) {
+			clish_shell_cwd_t *cwd = malloc(sizeof(*cwd));
+			assert(cwd);
+			clish_shell__init_cwd(cwd);
+			this->cwdv[i] = cwd;
+		}
+		this->cwdc = index + 1;
+	}
+
+	/* Fill the new cwd entry */
+	newcwd->line = line ? lub_string_dup(line) : NULL;
+	newcwd->view = view;
+	newcwd->pargv = clish_pargv_clone(clish_context__get_pargv(context));
+	if (full_cmd) {
+		const clish_command_t *cmd = clish_command__get_cmd(full_cmd);
+		newcwd->cmd = lub_string_dup(clish_command__get_name(cmd));
+		if (cmd != full_cmd) {
+			const char *full_cmd_name = clish_command__get_name(full_cmd);
+			const char *cmd_name = clish_command__get_name(cmd);
+			int len = strlen(full_cmd_name) - strlen(cmd_name);
+			if (len > 1)
+				newcwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
+		}
+	}
+	clish_shell__expand_viewid(viewid, newcwd->viewid, context);
+	clish_shell__fini_cwd(this->cwdv[index]);
+	free(this->cwdv[index]);
+	this->cwdv[index] = newcwd;
+	this->depth = index;
+}
+
+/*--------------------------------------------------------- */
+char *clish_shell__get_cwd_line(const clish_shell_t *this, unsigned int index)
+{
+	if (index >= this->cwdc)
+		return NULL;
+
+	return this->cwdv[index]->line;
+}
+
+/*--------------------------------------------------------- */
+clish_pargv_t *clish_shell__get_cwd_pargv(const clish_shell_t *this, unsigned int index)
+{
+	if (index >= this->cwdc)
+		return NULL;
+
+	return this->cwdv[index]->pargv;
+}
+
+/*--------------------------------------------------------- */
+char *clish_shell__get_cwd_cmd(const clish_shell_t *this, unsigned int index)
+{
+	if (index >= this->cwdc)
+		return NULL;
+
+	return this->cwdv[index]->cmd;
+}
+
+/*--------------------------------------------------------- */
+char *clish_shell__get_cwd_prefix(const clish_shell_t *this, unsigned int index)
+{
+	if (index >= this->cwdc)
+		return NULL;
+
+	return this->cwdv[index]->prefix;
+}
+
+/*--------------------------------------------------------- */
+char *clish_shell__get_cwd_full(const clish_shell_t * this, unsigned int depth)
+{
+	char *cwd = NULL;
+	unsigned int i;
+
+	for (i = 1; i <= depth; i++) {
+		const char *str =
+			clish_shell__get_cwd_line(this, i);
+		/* Cannot get full path */
+		if (!str) {
+			lub_string_free(cwd);
+			return NULL;
+		}
+		if (cwd)
+			lub_string_cat(&cwd, " ");
+		lub_string_cat(&cwd, "\"");
+		lub_string_cat(&cwd, str);
+		lub_string_cat(&cwd, "\"");
+	}
+
+	return cwd;
+}
+
+/*--------------------------------------------------------- */
+clish_view_t *clish_shell__get_cwd_view(const clish_shell_t * this, unsigned int index)
+{
+	if (index >= this->cwdc)
+		return NULL;
+
+	return this->cwdv[index]->view;
+}
+
+/*--------------------------------------------------------- */
+int clish_shell__set_socket(clish_shell_t * this, const char * path)
+{
+	if (!this || !path)
+		return -1;
+
+	konf_client_free(this->client);
+	this->client = konf_client_new(path);
+
+	return 0;
+}
+
+CLISH_SET_STR(shell, lockfile);
+CLISH_GET_STR(shell, lockfile);
+CLISH_SET(shell, int, log_facility);
+CLISH_GET(shell, int, log_facility);
+CLISH_GET(shell, konf_client_t *, client);

+ 3 - 3
clish/shell/shell_execute.c

@@ -105,7 +105,7 @@ int clish_shell_execute(clish_context_t *context, char **out)
 		if ((CLISH_RESTORE_VIEW == restore) &&
 			(clish_command__get_pview(cmd) != cur_view)) {
 			clish_view_t *view = clish_command__get_pview(cmd);
-			clish_shell__set_pwd(this, NULL, view, NULL, context);
+			clish_shell__set_cwd(this, NULL, view, NULL, context);
 		} else if ((CLISH_RESTORE_DEPTH == restore) &&
 			(clish_command__get_depth(cmd) < this->depth)) {
 			this->depth = clish_command__get_depth(cmd);
@@ -166,10 +166,10 @@ int clish_shell_execute(clish_context_t *context, char **out)
 				fprintf(stderr, "System error: Can't "
 					"change view to %s\n", viewname);
 			lub_string_free(viewname);
-			/* Save the PWD */
+			/* Save the CWD */
 			if (view) {
 				char *line = clish_shell__get_line(context);
-				clish_shell__set_pwd(this, line, view,
+				clish_shell__set_cwd(this, line, view,
 					clish_command__get_viewid(cmd), context);
 				lub_string_free(line);
 			}

+ 8 - 8
clish/shell/shell_new.c

@@ -55,8 +55,8 @@ static void clish_shell_init(clish_shell_t * this,
 	this->overview = NULL;
 	this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
 	this->current_file = NULL;
-	this->pwdv = NULL;
-	this->pwdc = 0;
+	this->cwdv = NULL;
+	this->cwdc = 0;
 	this->depth = -1; /* Current depth is undefined */
 	this->client = NULL;
 	this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
@@ -130,13 +130,13 @@ static void clish_shell_fini(clish_shell_t *this)
 	/* delete the tinyrl object */
 	clish_shell_tinyrl_delete(this->tinyrl);
 
-	/* finalize each of the pwd strings */
-	for (i = 0; i < this->pwdc; i++) {
-		clish_shell__fini_pwd(this->pwdv[i]);
-		free(this->pwdv[i]);
+	/* finalize each of the cwd strings */
+	for (i = 0; i < this->cwdc; i++) {
+		clish_shell__fini_cwd(this->cwdv[i]);
+		free(this->cwdv[i]);
 	}
-	/* free the pwd vector */
-	free(this->pwdv);
+	/* free the cwd vector */
+	free(this->cwdv);
 	konf_client_free(this->client);
 
 	lub_string_free(this->lockfile);

+ 0 - 178
clish/shell/shell_pwd.c

@@ -1,178 +0,0 @@
-/*
- * shell_pwd.c
- */
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <syslog.h>
-
-#include "lub/string.h"
-#include "private.h"
-
-/*--------------------------------------------------------- */
-void clish_shell__init_pwd(clish_shell_pwd_t *pwd)
-{
-	pwd->line = NULL;
-	pwd->view = NULL;
-	pwd->pargv = NULL;
-	pwd->cmd = NULL;
-	pwd->prefix = NULL;
-
-	/* Init VARs */
-	pwd->viewid = lub_list_new(clish_var_compare, clish_var_delete);
-}
-
-/*--------------------------------------------------------- */
-void clish_shell__fini_pwd(clish_shell_pwd_t *pwd)
-{
-	lub_string_free(pwd->line);
-	lub_string_free(pwd->cmd);
-	if (pwd->prefix)
-		lub_string_free(pwd->prefix);
-	pwd->view = NULL;
-	clish_pargv_delete(pwd->pargv);
-
-	/* Free VARs  */
-	lub_list_free_all(pwd->viewid);
-}
-
-/*--------------------------------------------------------- */
-void clish_shell__set_pwd(clish_shell_t *this,
-	const char *line, clish_view_t *view, const char *viewid, clish_context_t *context)
-{
-	clish_shell_pwd_t **tmp;
-	size_t new_size = 0;
-	unsigned int i;
-	unsigned int index = clish_view__get_depth(view);
-	clish_shell_pwd_t *newpwd;
-	const clish_command_t *full_cmd = clish_context__get_cmd(context);
-
-	/* Create new element */
-	newpwd = malloc(sizeof(*newpwd));
-	assert(newpwd);
-	clish_shell__init_pwd(newpwd);
-
-	/* Resize the pwd vector */
-	if (index >= this->pwdc) {
-		new_size = (index + 1) * sizeof(clish_shell_pwd_t *);
-		tmp = realloc(this->pwdv, new_size);
-		assert(tmp);
-		this->pwdv = tmp;
-		/* Initialize new elements */
-		for (i = this->pwdc; i <= index; i++) {
-			clish_shell_pwd_t *pwd = malloc(sizeof(*pwd));
-			assert(pwd);
-			clish_shell__init_pwd(pwd);
-			this->pwdv[i] = pwd;
-		}
-		this->pwdc = index + 1;
-	}
-
-	/* Fill the new pwd entry */
-	newpwd->line = line ? lub_string_dup(line) : NULL;
-	newpwd->view = view;
-	newpwd->pargv = clish_pargv_clone(clish_context__get_pargv(context));
-	if (full_cmd) {
-		const clish_command_t *cmd = clish_command__get_cmd(full_cmd);
-		newpwd->cmd = lub_string_dup(clish_command__get_name(cmd));
-		if (cmd != full_cmd) {
-			const char *full_cmd_name = clish_command__get_name(full_cmd);
-			const char *cmd_name = clish_command__get_name(cmd);
-			int len = strlen(full_cmd_name) - strlen(cmd_name);
-			if (len > 1)
-				newpwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
-		}
-	}
-	clish_shell__expand_viewid(viewid, newpwd->viewid, context);
-	clish_shell__fini_pwd(this->pwdv[index]);
-	free(this->pwdv[index]);
-	this->pwdv[index] = newpwd;
-	this->depth = index;
-}
-
-/*--------------------------------------------------------- */
-char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
-{
-	if (index >= this->pwdc)
-		return NULL;
-
-	return this->pwdv[index]->line;
-}
-
-/*--------------------------------------------------------- */
-clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *this, unsigned int index)
-{
-	if (index >= this->pwdc)
-		return NULL;
-
-	return this->pwdv[index]->pargv;
-}
-
-/*--------------------------------------------------------- */
-char *clish_shell__get_pwd_cmd(const clish_shell_t *this, unsigned int index)
-{
-	if (index >= this->pwdc)
-		return NULL;
-
-	return this->pwdv[index]->cmd;
-}
-
-/*--------------------------------------------------------- */
-char *clish_shell__get_pwd_prefix(const clish_shell_t *this, unsigned int index)
-{
-	if (index >= this->pwdc)
-		return NULL;
-
-	return this->pwdv[index]->prefix;
-}
-
-/*--------------------------------------------------------- */
-char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
-{
-	char *pwd = NULL;
-	unsigned int i;
-
-	for (i = 1; i <= depth; i++) {
-		const char *str =
-			clish_shell__get_pwd_line(this, i);
-		/* Cannot get full path */
-		if (!str) {
-			lub_string_free(pwd);
-			return NULL;
-		}
-		if (pwd)
-			lub_string_cat(&pwd, " ");
-		lub_string_cat(&pwd, "\"");
-		lub_string_cat(&pwd, str);
-		lub_string_cat(&pwd, "\"");
-	}
-
-	return pwd;
-}
-
-/*--------------------------------------------------------- */
-clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int index)
-{
-	if (index >= this->pwdc)
-		return NULL;
-
-	return this->pwdv[index]->view;
-}
-
-/*--------------------------------------------------------- */
-int clish_shell__set_socket(clish_shell_t * this, const char * path)
-{
-	if (!this || !path)
-		return -1;
-
-	konf_client_free(this->client);
-	this->client = konf_client_new(path);
-
-	return 0;
-}
-
-CLISH_SET_STR(shell, lockfile);
-CLISH_GET_STR(shell, lockfile);
-CLISH_SET(shell, int, log_facility);
-CLISH_GET(shell, int, log_facility);
-CLISH_GET(shell, konf_client_t *, client);

+ 1 - 1
clish/shell/shell_tinyrl.c

@@ -326,7 +326,7 @@ static bool_t clish_shell_tinyrl_hotkey(tinyrl_t *this, int key)
 
 	i = clish_shell__get_depth(shell);
 	while (i >= 0) {
-		view = clish_shell__get_pwd_view(shell, i);
+		view = clish_shell__get_cwd_view(shell, i);
 		cmd = clish_view_find_hotkey(view, key);
 		if (cmd)
 			break;

+ 4 - 3
clish/shell/shell_var.c

@@ -139,9 +139,10 @@ static char *find_context_var(const char *name, clish_context_t *this)
 		tmp[sizeof(tmp) - 1] = '\0';
 		result = strdup(tmp);
 
-	} else if (!lub_string_nocasecmp(name, "_cur_pwd")) {
+	} else if (!lub_string_nocasecmp(name, "_cur_pwd") ||
+		!lub_string_nocasecmp(name, "_cwd")) {
 		int depth = clish_shell__get_depth(shell);
-		result = clish_shell__get_pwd_full(shell, depth);
+		result = clish_shell__get_cwd_full(shell, depth);
 	}
 
 	return result;
@@ -205,7 +206,7 @@ static char *find_viewid_var(const char *name, clish_context_t *context)
 	int depth = clish_shell__get_depth(shell);
 	if (depth < 0)
 		return NULL;
-	return find_var(name, shell->pwdv[depth]->viewid, context);
+	return find_var(name, shell->cwdv[depth]->viewid, context);
 }
 
 static char * chardiff(const char *syms, const char *minus)

+ 2 - 2
clish/shell/shell_view.c

@@ -38,7 +38,7 @@ clish_view_t *clish_shell__get_view(const clish_shell_t * this)
 	assert(this);
 	if (this->depth < 0)
 		return NULL;
-	return this->pwdv[this->depth]->view;
+	return this->cwdv[this->depth]->view;
 }
 
 /*--------------------------------------------------------- */
@@ -48,7 +48,7 @@ clish_view_t *clish_shell__set_depth(clish_shell_t *this, unsigned int depth)
 
 	assert(this);
 	/* Check if target view is valid = is not NULL */
-	tmp = this->pwdv[depth]->view;
+	tmp = this->cwdv[depth]->view;
 	if (!tmp)
 		return NULL;
 	this->depth = depth;

+ 2 - 2
plugins/clish/hook_config.c

@@ -148,7 +148,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 		lub_string_free(str);
 	}
 
-	/* Add pwd */
+	/* Add cwd */
 	if (clish_config__get_depth(config)) {
 		str = clish_shell_expand(clish_config__get_depth(config), SHELL_VAR_ACTION, clish_context);
 		num = str2ushort(str);
@@ -156,7 +156,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 	} else {
 		num = clish_command__get_depth(cmd);
 	}
-	str = clish_shell__get_pwd_full(this, num);
+	str = clish_shell__get_cwd_full(this, num);
 	if (str) {
 		lub_string_cat(&command, " ");
 		lub_string_cat(&command, str);