Browse Source

Access field for PARAM

Serj Kalichev 9 years ago
parent
commit
4efff9ef74
4 changed files with 78 additions and 11 deletions
  1. 1 3
      clish/command/command_dump.c
  2. 1 0
      clish/param.h
  3. 35 1
      clish/param/param.c
  4. 41 7
      clish/shell/shell_startup.c

+ 1 - 3
clish/command/command_dump.c

@@ -20,9 +20,7 @@ void clish_command_dump(const clish_command_t * this)
 		this->link ?
 		clish_command__get_name(this->link) : LUB_DUMP_NULL);
 	lub_dump_printf("alias      : %s\n", LUB_DUMP_STR(this->alias));
-	lub_dump_printf("alias_view : %s\n",
-		this->alias_view ?
-		clish_view__get_name(this->alias_view) : LUB_DUMP_NULL);
+	lub_dump_printf("alias_view : %s\n", LUB_DUMP_STR(this->alias_view));
 	lub_dump_printf("paramc     : %d\n",
 		clish_paramv__get_count(this->paramv));
 	lub_dump_printf("detail     : %s\n", LUB_DUMP_STR(this->detail));

+ 1 - 0
clish/param.h

@@ -95,6 +95,7 @@ char *clish_param__get_access(const clish_param_t *instance);
 clish_paramv_t *clish_paramv_new(void);
 void clish_paramv_delete(clish_paramv_t * instance);
 void clish_paramv_insert(clish_paramv_t * instance, clish_param_t * param);
+int clish_paramv_remove(clish_paramv_t *instance, unsigned int index); /* Remove param from vector */
 clish_param_t *clish_paramv__get_param(const clish_paramv_t * instance,
 				unsigned index);
 unsigned int clish_paramv__get_count(const clish_paramv_t * instance);

+ 35 - 1
clish/param/param.c

@@ -291,9 +291,43 @@ void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
 	}
 }
 
+/*--------------------------------------------------------- */
+int clish_paramv_remove(clish_paramv_t *this, unsigned int index)
+{
+	size_t new_size;
+	clish_param_t **tmp;
+	clish_param_t **dst, **src;
+	size_t n;
+
+	if (this->paramc < 1)
+		return -1;
+	if (index >= this->paramc)
+		return -1;
+
+	new_size = ((this->paramc - 1) * sizeof(clish_param_t *));
+	dst = this->paramv + index;
+	src = dst + 1;
+	n = this->paramc - index - 1;
+	if (n)
+		memmove(dst, src, n * sizeof(clish_param_t *));
+	/* Resize the parameter vector */
+	if (new_size) {
+		tmp = realloc(this->paramv, new_size);
+		if (!tmp)
+			return -1;
+		this->paramv = tmp;
+	} else {
+		free(this->paramv);
+		this->paramv = NULL;
+	}
+	this->paramc--;
+
+	return 0;
+}
+
 /*--------------------------------------------------------- */
 clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
-	unsigned index)
+	unsigned int index)
 {
 	clish_param_t *result = NULL;
 

+ 41 - 7
clish/shell/shell_startup.c

@@ -79,6 +79,36 @@ const char * clish_shell__get_default_shebang(const clish_shell_t *this)
 	return this->default_shebang;
 }
 
+/*-------------------------------------------------------- */
+static int iterate_paramv(clish_shell_t *this, clish_paramv_t *paramv,
+	clish_hook_access_fn_t *access_fn)
+{
+	int i = 0;
+	clish_param_t *param;
+
+	while((param = clish_paramv__get_param(paramv, i))) {
+		clish_paramv_t *nested_paramv;
+
+		if (access_fn && clish_param__get_access(param) &&
+			access_fn(this, clish_param__get_access(param))) {
+			fprintf(stderr, "Warning: Access denied. Remove PARAM \"%s\"\n",
+				clish_param__get_name(param));
+			if (clish_paramv_remove(paramv, i) < 0) {
+				fprintf(stderr, "Error: Some system problem\n");
+				return -1;
+			}
+			clish_param_delete(param);
+			continue; /* Don't increment index */
+		}
+		nested_paramv = clish_param__get_paramv(param);
+		if (iterate_paramv(this, nested_paramv, access_fn) < 0)
+			return -1;
+		i++;
+	}
+
+	return 0;
+}
+
 /*-------------------------------------------------------- */
 int clish_shell_prepare(clish_shell_t *this)
 {
@@ -91,6 +121,7 @@ int clish_shell_prepare(clish_shell_t *this)
 	lub_list_node_t *nspace_iter;
 	clish_hook_access_fn_t *access_fn = NULL;
 	int i;
+	clish_paramv_t *paramv;
 
 	/* Add default plugin to the list of plugins */
 	if (this->default_plugin) {
@@ -129,7 +160,7 @@ int clish_shell_prepare(clish_shell_t *this)
 		/* Check access rights for the VIEW */
 		if (access_fn && clish_view__get_access(view) &&
 			access_fn(this, clish_view__get_access(view))) {
-			fprintf(stderr, "Warning: Access denied. Remove VIEW %s\n",
+			fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
 				clish_view__get_name(view));
 			lub_bintree_remove(view_tree, view);
 			clish_view_delete(view);
@@ -148,7 +179,7 @@ int clish_shell_prepare(clish_shell_t *this)
 			/* Resolve NAMESPACEs and remove unresolved ones */
 			ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
 			if (!ref_view) {
-				fprintf(stderr, "Warning: Remove unresolved NAMESPACE %s from %s VIEW\n",
+				fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
 					clish_nspace__get_view_name(nspace), clish_view__get_name(view));
 				lub_list_del(nspace_tree, old_nspace_iter);
 				lub_list_node_free(old_nspace_iter);
@@ -160,7 +191,7 @@ int clish_shell_prepare(clish_shell_t *this)
 			/* Check access rights for the NAMESPACE */
 			if (access_fn && clish_nspace__get_access(nspace) &&
 				access_fn(this, clish_nspace__get_access(nspace))) {
-				fprintf(stderr, "Warning: Access denied. Remove NAMESPACE %s from %s VIEW\n",
+				fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
 					clish_nspace__get_view_name(nspace), clish_view__get_name(view));
 				lub_list_del(nspace_tree, old_nspace_iter);
 				lub_list_node_free(old_nspace_iter);
@@ -184,19 +215,19 @@ int clish_shell_prepare(clish_shell_t *this)
 				else
 					aview = clish_shell_find_view(this, alias_view);
 				if (!aview) {
-					fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias %s\n",
+					fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
 						clish_command__get_name(cmd));
 					return -1;
 				}
 				cmdref = clish_view_find_command(aview,
 					clish_command__get_alias(cmd), BOOL_FALSE);
 				if (!cmdref) {
-					fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
+					fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
 						clish_command__get_name(cmd));
 					return -1;
 				}
 				if (!clish_command_alias_to_link(cmd, cmdref)) {
-					fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias %s\n",
+					fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
 						clish_command__get_name(cmd));
 					return -1;
 				}
@@ -204,12 +235,15 @@ int clish_shell_prepare(clish_shell_t *this)
 			/* Check access rights for the COMMAND */
 			if (access_fn && clish_command__get_access(cmd) &&
 				access_fn(this, clish_command__get_access(cmd))) {
-				fprintf(stderr, "Warning: Access denied. Remove COMMAND %s from VIEW %s\n",
+				fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
 					clish_command__get_name(cmd), clish_view__get_name(view));
 				lub_bintree_remove(cmd_tree, cmd);
 				clish_command_delete(cmd);
 				continue;
 			}
+			paramv = clish_command__get_paramv(cmd);
+			if (iterate_paramv(this, paramv, access_fn) < 0)
+				return -1;
 		}
 	}