Browse Source

clish_shell_prepare()

Serj Kalichev 9 years ago
parent
commit
4f712703c6

+ 3 - 2
bin/clish.c

@@ -298,6 +298,9 @@ int main(int argc, char **argv)
 		goto end;
 	if (clish_shell_link_plugins(shell) < 0)
 		goto end;
+	/* Link aliases and check access rights */
+	if (clish_shell_prepare(shell) < 0)
+		goto end;
 	/* Dryrun config and log hooks */
 	if (dryrun_config) {
 		if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_CONFIG)))
@@ -305,8 +308,6 @@ int main(int argc, char **argv)
 		if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_LOG)))
 			clish_sym__set_permanent(sym, BOOL_FALSE);
 	}
-	/* Check access rights of VIEWs, COMMANDs etc. */
-	clish_shell_check_access(shell);
 
 	/* Set source of command stream: files or interactive tty */
 	if(optind < argc) {

+ 1 - 1
clish/plugin.h

@@ -36,7 +36,7 @@ typedef struct clish_plugin_s clish_plugin_t;
 
 #define CLISH_PLUGIN_FINI(name) int name(void *clish_shell, clish_plugin_t *plugin)
 #define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
-#define CLISH_HOOK_ACCESS(name) int name(void *clish_context, const char *access)
+#define CLISH_HOOK_ACCESS(name) int name(void *clish_shell, const char *access)
 #define CLISH_HOOK_CONFIG(name) int name(void *clish_context)
 #define CLISH_HOOK_LOG(name) int name(void *clish_context, const char *line, int retcode)
 

+ 4 - 2
clish/shell.h

@@ -29,6 +29,9 @@
 #define CLISH_LOCK_PATH "/tmp/clish.lock"
 #define CLISH_LOCK_WAIT 20
 
+#define CLISH_XML_ERROR_STR "Error parsing XML: "
+#define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n"
+
 typedef struct clish_shell_s clish_shell_t;
 typedef struct clish_context_s clish_context_t;
 
@@ -193,7 +196,6 @@ clish_sym_t *clish_shell_get_hook(const clish_shell_t *instance, int type);
 
 /* Hook wrappers */
 void *clish_shell_check_hook(const clish_context_t *clish_context, int type);
-CLISH_HOOK_ACCESS(clish_shell_exec_access);
 CLISH_HOOK_CONFIG(clish_shell_exec_config);
 CLISH_HOOK_LOG(clish_shell_exec_log);
 
@@ -204,7 +206,7 @@ int clish_shell__set_udata(clish_shell_t *instance,
 	const char *name, void *data);
 
 /* Access functions */
-int clish_shell_check_access(clish_shell_t *instance);
+int clish_shell_prepare(clish_shell_t *instance);
 
 _END_C_DECL
 

+ 0 - 8
clish/shell/shell_execute.c

@@ -244,14 +244,6 @@ void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
 	return func;
 }
 
-/*----------------------------------------------------------- */
-CLISH_HOOK_ACCESS(clish_shell_exec_access)
-{
-	clish_hook_access_fn_t *func = NULL;
-	func = clish_shell_check_hook(clish_context, CLISH_SYM_TYPE_ACCESS);
-	return func ? func(clish_context, access) : 0;
-}
-
 /*----------------------------------------------------------- */
 CLISH_HOOK_CONFIG(clish_shell_exec_config)
 {

+ 55 - 2
clish/shell/shell_startup.c

@@ -71,9 +71,62 @@ const char * clish_shell__get_default_shebang(const clish_shell_t *this)
 }
 
 /*-------------------------------------------------------- */
-int clish_shell_check_access(clish_shell_t *this)
+int clish_shell_prepare(clish_shell_t *this)
 {
-
+	clish_command_t *cmd;
+	clish_view_t *view;
+	clish_nspace_t *nspace;
+	lub_bintree_t *view_tree, *cmd_tree;
+	lub_list_t *nspace_tree;
+	lub_bintree_iterator_t cmd_iter, view_iter;
+	lub_list_node_t *nspace_iter;
+	clish_hook_access_fn_t *access_fn = NULL;
+
+	access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
+
+	/* Iterate the VIEWs */
+	view_tree = &this->view_tree;
+	view = lub_bintree_findfirst(view_tree);
+	for (lub_bintree_iterator_init(&view_iter, view_tree, view);
+		view; view = lub_bintree_iterator_next(&view_iter)) {
+		/* Check access rights for the VIEW */
+		if (access_fn && clish_view__get_access(view) &&
+			(access_fn(this, clish_view__get_access(view)) < 0)) {
+			lub_bintree_remove(view_tree, view);
+			clish_view_delete(view);
+			continue;
+		}
+		/* Iterate the NAMESPACEs */
+		nspace_tree = clish_view__get_nspace_tree(view);
+		nspace_iter = lub_list__get_head(nspace_tree);
+		while(nspace_iter) {
+			lub_list_node_t *old_nspace_iter;
+			nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
+			old_nspace_iter = nspace_iter;
+			nspace_iter = lub_list_node__get_next(nspace_iter);
+			/* Resolve NAMESPACEs and remove unresolved ones */
+			/* Check access rights for the NAMESPACE */
+			if (access_fn && clish_nspace__get_access(nspace) &&
+				(access_fn(this, clish_nspace__get_access(nspace)) < 0)) {
+				lub_list_del(nspace_tree, old_nspace_iter);
+				lub_list_node_free(old_nspace_iter);
+				clish_nspace_delete(nspace);
+				continue;
+			}
+		}
+
+		/* Iterate the COMMANDs */
+		cmd_tree = clish_view__get_command_tree(view);
+		cmd = lub_bintree_findfirst(cmd_tree);
+		for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
+			cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
+			if (!clish_command_alias_to_link(cmd)) {
+				fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
+					clish_command__get_name(cmd));
+				return -1;
+			}
+		}
+	}
 
 	return 0;
 }

+ 0 - 25
clish/shell/shell_xml.c

@@ -27,9 +27,6 @@ const char* clish_plugin_default_hook[] = {
 	"clish_hook_log@clish"
 	};
 
-#define CLISH_XML_ERROR_STR "Error parsing XML: "
-#define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n"
-
 typedef int (PROCESS_FN) (clish_shell_t *instance,
 	clish_xmlnode_t *element, void *parent);
 
@@ -93,10 +90,6 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
 	char *saveptr = NULL;
 	int res = 0;
 	int i = 0;
-	clish_command_t *cmd;
-	clish_view_t *view;
-	lub_bintree_t *view_tree, *cmd_tree;
-	lub_bintree_iterator_t cmd_iter, view_iter;
 
 	/* use the default path */
 	if (!path)
@@ -177,24 +170,6 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
 			lub_list_add(this->syms, this->hooks[i]);
 	}
 
-	/* Resolve COMMAND aliases */
-	view_tree = &this->view_tree;
-	view = lub_bintree_findfirst(view_tree);
-	for (lub_bintree_iterator_init(&view_iter, view_tree, view);
-		view; view = lub_bintree_iterator_next(&view_iter)) {
-		/* Iterate the tree of commands */
-		cmd_tree = clish_view__get_command_tree(view);
-		cmd = lub_bintree_findfirst(cmd_tree);
-		for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
-			cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
-			if (!clish_command_alias_to_link(cmd)) {
-				fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
-					clish_command__get_name(cmd));
-				res = -1;
-			}
-		}
-	}
-
 #ifdef DEBUG
 	clish_shell_dump(this);
 #endif

+ 2 - 1
clish/view.h

@@ -24,6 +24,7 @@ typedef enum {
 	CLISH_RESTORE_VIEW
 } clish_view_restore_t;
 
+#include "lub/list.h"
 #include "clish/command.h"
 #include "clish/nspace.h"
 #include "clish/var.h"
@@ -43,6 +44,7 @@ size_t clish_view_bt_offset(void);
  * methods
  *----------------- */
 lub_bintree_t * clish_view__get_command_tree(clish_view_t *instance);
+lub_list_t * clish_view__get_nspace_tree(clish_view_t *instance);
 void clish_view_delete(clish_view_t * instance);
 clish_command_t *clish_view_new_command(clish_view_t * instance,
 	const char *name, const char *text);
@@ -72,7 +74,6 @@ void clish_view__set_restore(clish_view_t * instance,
 clish_view_restore_t clish_view__get_restore(const clish_view_t * instance);
 int clish_view_insert_hotkey(const clish_view_t *instance, const char *key, const char *cmd);
 const char *clish_view_find_hotkey(const clish_view_t *instance, int code);
-lub_bintree_t *clish_view__cmd_tree(clish_view_t *instance);
 void clish_view__set_access(clish_view_t *instance, const char *access);
 char *clish_view__get_access(const clish_view_t *instance);
 

+ 6 - 6
clish/view/view.c

@@ -329,6 +329,12 @@ lub_bintree_t * clish_view__get_command_tree(clish_view_t *this)
 	return &this->tree;
 }
 
+/*--------------------------------------------------------- */
+lub_list_t * clish_view__get_nspace_tree(clish_view_t *this)
+{
+	return this->nspaces;
+}
+
 /*--------------------------------------------------------- */
 const char *clish_view__get_name(const clish_view_t * this)
 {
@@ -385,12 +391,6 @@ const char *clish_view_find_hotkey(const clish_view_t *this, int code)
 	return clish_hotkeyv_cmd_by_code(this->hotkeys, code);
 }
 
-/*--------------------------------------------------------- */
-lub_bintree_t *clish_view__cmd_tree(clish_view_t *this)
-{
-	return &this->tree;
-}
-
 /*--------------------------------------------------------- */
 void clish_view__set_access(clish_view_t *this, const char *access)
 {

+ 1 - 1
plugins/clish/hook_access.c

@@ -76,7 +76,7 @@ CLISH_HOOK_ACCESS(clish_hook_access)
 	free(group_list);
 #endif
 
-	clish_context = clish_context; /* Happy compiler */
+	clish_shell = clish_shell; /* Happy compiler */
 
 	return allowed;
 }