Forráskód Böngészése

UNFINISHED: lub_list changes

Serj Kalichev 6 éve
szülő
commit
53aacc8207

+ 1 - 1
clish/plugin.h

@@ -90,7 +90,7 @@ _CLISH_GET(sym, clish_sym_api_e, api);
 /* Plugin */
 
 clish_plugin_t *clish_plugin_new(const char *name, void *userdata);
-void clish_plugin_free(clish_plugin_t *instance);
+void clish_plugin_free(void *instance);
 int clish_plugin_load(clish_plugin_t *instance);
 clish_sym_t *clish_plugin_get_sym(clish_plugin_t *instance,
 	const char *name, int type);

+ 3 - 1
clish/plugin/plugin.c

@@ -45,8 +45,10 @@ clish_plugin_t *clish_plugin_new(const char *name, void *userdata)
 }
 
 /*--------------------------------------------------------- */
-void clish_plugin_free(clish_plugin_t *this)
+void clish_plugin_free(void *plugin)
 {
+	clish_plugin_t *this = (clish_plugin_t *)plugin;
+
 	if (!this)
 		return;
 

+ 5 - 18
clish/shell.h

@@ -88,24 +88,11 @@ typedef enum {
 
 _BEGIN_C_DECL
 
-/*-----------------
- * meta functions
- *----------------- */
-
-clish_shell_t *clish_shell_new(FILE * istream, FILE * ostream,
-	bool_t stop_on_error);
-/*-----------------
- * methods
- *----------------- */
-/*
- * Called to invoke the startup command for this shell
- */
-int clish_shell_startup(clish_shell_t * instance);
-void clish_shell_delete(clish_shell_t * instance);
-clish_view_t *clish_shell_find_create_view(clish_shell_t * instance,
-	const char *name,
-	const char *prompt);
-clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * instance,
+clish_shell_t *clish_shell_new(FILE *istream, FILE *ostream, bool_t stop_on_error);
+int clish_shell_startup(clish_shell_t *instance);
+void clish_shell_delete(clish_shell_t *instance);
+clish_view_t *clish_shell_find_create_view(clish_shell_t *instance, const char *name);
+clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t *instance,
 	const char *name,
 	const char *text,
 	const char *pattern,

+ 0 - 2
clish/shell/shell_new.c

@@ -97,10 +97,8 @@ static void clish_shell_init(clish_shell_t * this,
 /*--------------------------------------------------------- */
 static void clish_shell_fini(clish_shell_t *this)
 {
-	clish_view_t *view;
 	clish_var_t *var;
 	unsigned int i;
-	lub_list_node_t *iter;
 
 	/* Free all loaded plugins */
 	lub_list_free_all(this->plugins);

+ 2 - 2
clish/shell/shell_plugin.c

@@ -54,7 +54,7 @@ clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *this,
 	const char *name)
 {
 	clish_plugin_t *plugin = clish_shell_find_plugin(this, name);
-	return (plugin ? plugin : clish_shell_create_plugin(this, name);
+	return (plugin ? plugin : clish_shell_create_plugin(this, name));
 }
 
 /*----------------------------------------------------------------------- */
@@ -74,7 +74,7 @@ int clish_shell_load_plugins(clish_shell_t *this)
 	for(iter = lub_list__get_head(this->plugins);
 		iter; iter = lub_list_node__get_next(iter)) {
 		plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
-		if (clish_plugin_load(plugin, (void *)this))
+		if (clish_plugin_load(plugin))
 			return -1;
 #ifdef DEBUG
 		clish_plugin_dump(plugin);

+ 13 - 9
clish/shell/shell_startup.c

@@ -142,10 +142,10 @@ 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;
+	lub_list_t *view_tree, *nspace_tree;
+	lub_list_node_t *nspace_iter, *view_iter;
+	lub_bintree_t *cmd_tree;
+	lub_bintree_iterator_t cmd_iter;
 	clish_hook_access_fn_t *access_fn = NULL;
 	clish_paramv_t *paramv;
 	int i = 0;
@@ -189,10 +189,13 @@ int clish_shell_prepare(clish_shell_t *this)
 	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)) {
+	view_tree = this->view_tree;
+	view_iter = lub_list_iterator_init(view_tree);
+	while(view_iter) {
+		lub_list_node_t *old_view_iter;
+		view = (clish_view_t *)lub_list_node__get_data(view_iter);
+		old_view_iter = view_iter;
+		view_iter = lub_list_node__get_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))) {
@@ -200,7 +203,8 @@ int clish_shell_prepare(clish_shell_t *this)
 			fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
 				clish_view__get_name(view));
 #endif
-			lub_bintree_remove(view_tree, view);
+			lub_list_del(view_tree, old_view_iter);
+			lub_list_node_free(old_view_iter);
 			clish_view_delete(view);
 			continue;
 		}

+ 15 - 20
clish/shell/shell_view.c

@@ -3,38 +3,33 @@
  */
 
 #include <assert.h>
+#include <string.h>
 
 #include "private.h"
 
-/*--------------------------------------------------------- */
-clish_view_t *clish_shell_find_create_view(clish_shell_t * this,
-	const char *name, const char *prompt)
+static int find_view(const void *key, const void *data)
 {
-	clish_view_t *view = lub_bintree_find(&this->view_tree, name);
-
-	if (!view) {
-		/* create a view */
-		view = clish_view_new(name, prompt);
-		assert(view);
-		clish_shell_insert_view(this, view);
-	} else {
-		/* set the prompt */
-		if (prompt)
-			clish_view__set_prompt(view, prompt);
-	}
-	return view;
+	clish_view_t *view = (clish_view_t *)data;
+	const char *name = key;
+	return strcmp(name, clish_view__get_name(view));
 }
 
 /*--------------------------------------------------------- */
-clish_view_t *clish_shell_find_view(clish_shell_t * this, const char *name)
+clish_view_t *clish_shell_find_create_view(clish_shell_t *this,
+	const char *name)
 {
-	return lub_bintree_find(&this->view_tree, name);
+	clish_view_t *view = clish_shell_find_view(this, name);
+	if (view)
+		return view;
+	view = clish_view_new(name);
+	lub_list_add(this->view_tree, view);
+	return view;
 }
 
 /*--------------------------------------------------------- */
-void clish_shell_insert_view(clish_shell_t * this, clish_view_t * view)
+clish_view_t *clish_shell_find_view(clish_shell_t *this, const char *name)
 {
-	(void)lub_bintree_insert(&this->view_tree, view);
+	return lub_list_find(this->view_tree, find_view, name);
 }
 
 /*--------------------------------------------------------- */

+ 5 - 3
clish/shell/shell_xml.c

@@ -304,8 +304,7 @@ static int process_clish_module(clish_shell_t *shell, clish_xmlnode_t *element,
 {
 	/* Create the global view */
 	if (!shell->global)
-		shell->global = clish_shell_find_create_view(shell,
-			"__view_global", "");
+		shell->global = clish_shell_find_create_view(shell, "__view_global");
 
 	parent = parent; /* Happy compiler */
 
@@ -332,7 +331,10 @@ static int process_view(clish_shell_t *shell, clish_xmlnode_t *element,
 	}
 
 	/* re-use a view if it already exists */
-	view = clish_shell_find_create_view(shell, name, prompt);
+	view = clish_shell_find_create_view(shell, name);
+
+	if (prompt)
+		clish_view__set_prompt(view, prompt);
 
 	if (depth && (lub_ctype_isdigit(*depth))) {
 		unsigned int res = 0;

+ 2 - 2
clish/view.h

@@ -29,9 +29,9 @@ typedef enum {
 #include "clish/nspace.h"
 #include "clish/var.h"
 
-clish_view_t *clish_view_new(const char *name, const char *prompt);
+clish_view_t *clish_view_new(const char *name);
 int clish_view_compare(const void *clientnode, const void *clientkey);
-void clish_view_delete(clish_view_t * instance);
+void clish_view_delete(void *instance);
 clish_command_t *clish_view_new_command(clish_view_t * instance,
 	const char *name, const char *text);
 clish_command_t *clish_view_find_command(clish_view_t * instance,

+ 8 - 14
clish/view/view.c

@@ -15,16 +15,15 @@
 #include <stdio.h>
 
 /*-------------------------------------------------------- */
-int clish_view_compare(const void *clientnode, const void *clientkey)
+int clish_view_compare(const void *first, const void *second)
 {
-	const clish_view_t *this = clientnode;
-	const char *key = clientkey;
-
-	return strcmp(this->name, key);
+	const clish_view_t *f = (const clish_view_t *)first;
+	const clish_view_t *s = (const clish_view_t *)second;
+	return strcmp(f->name, s->name);
 }
 
 /*-------------------------------------------------------- */
-static void clish_view_init(clish_view_t * this, const char *name, const char *prompt)
+static void clish_view_init(clish_view_t * this, const char *name)
 {
 	/* set up defaults */
 	this->name = lub_string_dup(name);
@@ -44,9 +43,6 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 	 */
 	this->nspaces = lub_list_new(NULL, clish_nspace_delete);
 
-	/* set up the defaults */
-	clish_view__set_prompt(this, prompt);
-
 	/* Initialize hotkey structures */
 	this->hotkeys = clish_hotkeyv_new();
 }
@@ -55,8 +51,6 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 static void clish_view_fini(clish_view_t * this)
 {
 	clish_command_t *cmd;
-	lub_list_node_t *iter;
-	clish_nspace_t *nspace;
 
 	/* delete each command held by this view */
 	while ((cmd = lub_bintree_findfirst(&this->tree))) {
@@ -79,19 +73,19 @@ static void clish_view_fini(clish_view_t * this)
 }
 
 /*--------------------------------------------------------- */
-clish_view_t *clish_view_new(const char *name, const char *prompt)
+clish_view_t *clish_view_new(const char *name)
 {
 	clish_view_t *this = malloc(sizeof(clish_view_t));
 
 	if (this)
-		clish_view_init(this, name, prompt);
+		clish_view_init(this, name);
 	return this;
 }
 
 /*-------------------------------------------------------- */
 void clish_view_delete(void *data)
 {
-	clish_view_t this = (clish_view_t *)data;
+	clish_view_t *this = (clish_view_t *)data;
 	clish_view_fini(this);
 	free(this);
 }

+ 2 - 0
lub/list.h

@@ -43,6 +43,8 @@ lub_list_node_t *lub_list_match_node(lub_list_t *list,
 void *lub_list_match(lub_list_t *list,
 	lub_list_match_fn matchFn, const void *userkey,
 	lub_list_node_t **saveptr);
+void *lub_list_find(lub_list_t *list,
+	lub_list_match_fn matchFn, const void *userkey);
 
 _END_C_DECL
 #endif				/* _lub_list_h */

+ 7 - 0
lub/list/list.c

@@ -267,6 +267,13 @@ void *lub_list_match(lub_list_t *this,
 	return lub_list_node__get_data(res);
 }
 
+/*--------------------------------------------------------- */
+void *lub_list_find(lub_list_t *this,
+	lub_list_match_fn matchFn, const void *userkey)
+{
+	return lub_list_match(this, matchFn, userkey, NULL);
+}
+
 /*--------------------------------------------------------- */
 lub_list_node_t *lub_list_search_node(lub_list_t *this, void *data)
 {