Browse Source

Add access fields to command, view, nspace

Serj Kalichev 10 years ago
parent
commit
91def1dd3d

+ 2 - 0
clish/command.h

@@ -87,5 +87,7 @@ void clish_command__set_dynamic(clish_command_t * instance, bool_t dynamic);
 bool_t clish_command__get_dynamic(const clish_command_t * instance);
 bool_t clish_command__get_interrupt(const clish_command_t * instance);
 void clish_command__set_interrupt(clish_command_t * instance, bool_t interrupt);
+void clish_command__set_access(clish_command_t *instance, const char *access);
+char *clish_command__get_access(const clish_command_t *instance);
 
 #endif				/* _clish_command_h */

+ 17 - 1
clish/command/command.c

@@ -43,6 +43,7 @@ clish_command_init(clish_command_t *this, const char *name, const char *text)
 	this->lock = BOOL_TRUE;
 	this->interrupt = BOOL_FALSE;
 	this->dynamic = BOOL_FALSE;
+	this->access = NULL;
 }
 
 /*--------------------------------------------------------- */
@@ -66,6 +67,7 @@ static void clish_command_fini(clish_command_t * this)
 	lub_string_free(this->detail);
 	lub_string_free(this->escape_chars);
 	lub_string_free(this->regex_chars);
+	lub_string_free(this->access);
 	if (this->args)
 		clish_param_delete(this->args);
 }
@@ -121,7 +123,7 @@ clish_command_t *clish_command_new_link(const char *name,
 	*this = *ref;
 	/* Initialise the name (other than original name) */
 	this->name = lub_string_dup(name);
-	/* Initialise the name (other than original name) */
+	/* Initialise the help (other than original help) */
 	this->text = lub_string_dup(help);
 	/* Be a good binary tree citizen */
 	lub_bintree_node_init(&this->bt_node);
@@ -466,3 +468,17 @@ void clish_command__set_interrupt(clish_command_t * this, bool_t interrupt)
 {
 	this->interrupt = interrupt;
 }
+
+/*--------------------------------------------------------- */
+void clish_command__set_access(clish_command_t *this, const char *access)
+{
+	if (this->access)
+		lub_string_free(this->access);
+	this->access = lub_string_dup(access);
+}
+
+/*--------------------------------------------------------- */
+char *clish_command__get_access(const clish_command_t *this)
+{
+	return this->access;
+}

+ 1 - 0
clish/command/command_dump.c

@@ -24,6 +24,7 @@ void clish_command_dump(const clish_command_t * this)
 	lub_dump_printf("paramc      : %d\n", clish_paramv__get_count(this->paramv));
 	lub_dump_printf("detail      : %s\n",
 			this->detail ? this->detail : "(null)");
+	lub_dump_printf("access      : %s\n", this->access);
 	clish_action_dump(this->action);
 	clish_config_dump(this->config);
 

+ 1 - 0
clish/command/private.h

@@ -19,6 +19,7 @@ struct clish_command_s {
 	char *detail;
 	char *escape_chars;
 	char *regex_chars;
+	char *access;
 	clish_param_t *args;
 	const struct clish_command_s *link;
 	clish_view_t *alias_view;

+ 2 - 0
clish/nspace.h

@@ -65,6 +65,8 @@ void clish_nspace__set_help(clish_nspace_t * instance, bool_t help);
 void clish_nspace__set_completion(clish_nspace_t * instance, bool_t help);
 void clish_nspace__set_context_help(clish_nspace_t * instance, bool_t help);
 void clish_nspace__set_inherit(clish_nspace_t * instance, bool_t inherit);
+void clish_nspace__set_access(clish_nspace_t *instance, const char *access);
+char *clish_nspace__get_access(const clish_nspace_t *instance);
 
 #endif				/* _clish_nspace_h */
 /** @} clish_nspace */

+ 16 - 0
clish/nspace/nspace.c

@@ -28,6 +28,7 @@ static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
 	this->context_help = BOOL_FALSE;
 	this->inherit = BOOL_TRUE;
 	this->prefix_cmd = NULL;
+	this->access = NULL;
 
 	/* initialise the tree of commands links for this nspace */
 	lub_bintree_init(&this->tree,
@@ -57,6 +58,7 @@ static void clish_nspace_fini(clish_nspace_t * this)
 		clish_command_delete(this->prefix_cmd);
 		this->prefix_cmd = NULL;
 	}
+	lub_string_free(this->access);
 }
 
 /*--------------------------------------------------------- */
@@ -380,3 +382,17 @@ bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
 }
 
 /*--------------------------------------------------------- */
+void clish_nspace__set_access(clish_nspace_t *this, const char *access)
+{
+	if (this->access)
+		lub_string_free(this->access);
+	this->access = lub_string_dup(access);
+}
+
+/*--------------------------------------------------------- */
+char *clish_nspace__get_access(const clish_nspace_t *this)
+{
+	return this->access;
+}
+
+/*--------------------------------------------------------- */

+ 2 - 0
clish/nspace/nspace_dump.c

@@ -16,6 +16,8 @@ void clish_nspace_dump(const clish_nspace_t * this)
 			clish_view__get_name(this->view));
 	lub_dump_printf("prefix       : %s\n",
 			this->prefix ? this->prefix : "(null)");
+	lub_dump_printf("access       : %s\n",
+			this->access ? this->access : "(null)");
 	lub_dump_printf("help         : %s\n", this->help ? "true" : "false");
 	lub_dump_printf("completion   : %s\n",
 			this->completion ? "true" : "false");

+ 1 - 0
clish/nspace/private.h

@@ -12,6 +12,7 @@ struct clish_nspace_s {
 	lub_bintree_t tree;	/* Tree of command links */
 	clish_view_t *view;	/* The view to import commands from */
 	char *prefix;		/* if non NULL the prefix for imported commands */
+	char *access;
 	regex_t prefix_regex;
 	bool_t help;
 	bool_t completion;

+ 9 - 30
clish/shell/shell_xml.c

@@ -305,7 +305,6 @@ process_clish_module(clish_shell_t * shell, clish_xmlnode_t * element, void *par
 static int process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 {
 	clish_view_t *view;
-//	int allowed = 1;
 	int res = -1;
 
 	char *name = clish_xmlnode_fetch_attr(element, "name");
@@ -314,15 +313,6 @@ static int process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *
 	char *restore = clish_xmlnode_fetch_attr(element, "restore");
 	char *access = clish_xmlnode_fetch_attr(element, "access");
 
-	/* Check permissions */
-//	if (access) {
-//		allowed = 0;
-//		if (shell->hooks->access_fn)
-//			allowed = shell->hooks->access_fn(shell, access);
-//	}
-//	if (!allowed)
-//		goto process_view_end;
-
 	/* Check syntax */
 	if (!name) {
 		fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
@@ -346,6 +336,9 @@ static int process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *
 			clish_view__set_restore(view, CLISH_RESTORE_NONE);
 	}
 
+	if (access)
+		clish_view__set_access(view, access);
+
 //process_view_end:
 	res = process_children(shell, element, view);
 error:
@@ -442,7 +435,6 @@ process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	clish_command_t *old;
 	char *alias_name = NULL;
 	clish_view_t *alias_view = NULL;
-//	int allowed = 1;
 	int res = -1;
 
 	char *access = clish_xmlnode_fetch_attr(element, "access");
@@ -467,15 +459,6 @@ process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 		goto error;
 	}
 
-	/* Check permissions */
-//	if (access) {
-//		allowed = 0;
-//		if (shell->hooks->access_fn)
-//			allowed = shell->hooks->access_fn(shell, access);
-//	}
-//	if (!allowed)
-//		goto process_command_end;
-
 	/* check this command doesn't already exist */
 	old = clish_view_find_command(v, name, BOOL_FALSE);
 	if (old) {
@@ -553,6 +536,9 @@ process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	else
 		clish_command__set_interrupt(cmd, BOOL_FALSE);
 
+	if (access)
+		clish_command__set_access(cmd, access);
+
 	/* Set alias */
 	if (alias_name) {
 		/* Check syntax */
@@ -926,16 +912,6 @@ process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent
 	char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
 	char *access = clish_xmlnode_fetch_attr(element, "access");
 
-//	int allowed = 1;
-
-//	if (access) {
-//		allowed = 0;
-//		if (shell->hooks->access_fn)
-//			allowed = shell->hooks->access_fn(shell, access);
-//	}
-//	if (!allowed)
-//		goto process_namespace_end;
-
 	/* Check syntax */
 	if (!view) {
 		fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
@@ -984,6 +960,9 @@ process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent
 	else
 		clish_nspace__set_inherit(nspace, BOOL_TRUE);
 
+	if (access)
+		clish_nspace__set_access(nspace, access);
+
 process_namespace_end:
 	res = 0;
 error:

+ 2 - 0
clish/view.h

@@ -75,6 +75,8 @@ 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);
 
 #endif				/* _clish_view_h */
 /** @} clish_view */

+ 1 - 0
clish/view/private.h

@@ -13,6 +13,7 @@ struct clish_view_s {
 	lub_bintree_node_t bt_node;
 	char *name;
 	char *prompt;
+	char *access;
 	unsigned int nspacec;
 	clish_nspace_t **nspacev;
 	clish_hotkeyv_t *hotkeys;

+ 16 - 0
clish/view/view.c

@@ -45,6 +45,7 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 	this->nspacev = NULL;
 	this->depth = 0;
 	this->restore = CLISH_RESTORE_NONE;
+	this->access = NULL;
 
 	/* Be a good binary tree citizen */
 	lub_bintree_node_init(&this->bt_node);
@@ -91,6 +92,7 @@ static void clish_view_fini(clish_view_t * this)
 	/* free our memory */
 	lub_string_free(this->name);
 	lub_string_free(this->prompt);
+	lub_string_free(this->access);
 }
 
 /*---------------------------------------------------------
@@ -407,3 +409,17 @@ lub_bintree_t *clish_view__cmd_tree(clish_view_t *this)
 }
 
 /*--------------------------------------------------------- */
+void clish_view__set_access(clish_view_t *this, const char *access)
+{
+	if (this->access)
+		lub_string_free(this->access);
+	this->access = lub_string_dup(access);
+}
+
+/*--------------------------------------------------------- */
+char *clish_view__get_access(const clish_view_t *this)
+{
+	return this->access;
+}
+
+/*--------------------------------------------------------- */

+ 3 - 2
clish/view/view_dump.c

@@ -16,8 +16,9 @@ void clish_view_dump(clish_view_t * this)
 	lub_dump_printf("view(%p)\n", this);
 	lub_dump_indent();
 
-	lub_dump_printf("name  : %s\n", clish_view__get_name(this));
-	lub_dump_printf("depth : %u\n", clish_view__get_depth(this));
+	lub_dump_printf("name   : %s\n", clish_view__get_name(this));
+	lub_dump_printf("depth  : %u\n", clish_view__get_depth(this));
+	lub_dump_printf("access : %u\n", clish_view__get_access(this));
 
 	/* Get each namespace to dump their details */
 	for (i = 0; i < this->nspacec; i++) {