Browse Source

The ACTION is separate class now

git-svn-id: https://klish.googlecode.com/svn/trunk@429 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
7fc8b1bc17

File diff suppressed because it is too large
+ 16 - 12
Makefile.in


+ 35 - 0
clish/action.h

@@ -0,0 +1,35 @@
+/*
+ * action.h
+ */
+#ifndef _clish_action_h
+#define _clish_action_h
+
+typedef struct clish_action_s clish_action_t;
+
+#include "lub/bintree.h"
+
+/*=====================================
+ * ACTION INTERFACE
+ *===================================== */
+/*-----------------
+ * meta functions
+ *----------------- */
+clish_action_t *clish_action_new(void);
+
+/*-----------------
+ * methods
+ *----------------- */
+void clish_action_delete(clish_action_t *instance);
+void clish_action_dump(const clish_action_t *instance);
+
+/*-----------------
+ * attributes
+ *----------------- */
+void clish_action__set_script(clish_action_t *instance, const char *script);
+char *clish_action__get_script(const clish_action_t *instance);
+void clish_action__set_builtin(clish_action_t *instance, const char *builtin);
+const char *clish_action__get_builtin(const clish_action_t *instance);
+void clish_action__set_shebang(clish_action_t *instance, const char *shebang);
+const char *clish_action__get_shebang(const clish_action_t *instance);
+
+#endif				/* _clish_action_h */

+ 103 - 0
clish/action/action.c

@@ -0,0 +1,103 @@
+/*
+ * action.c
+ *
+ * This file provides the implementation of a action definition
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "private.h"
+#include "lub/bintree.h"
+#include "lub/string.h"
+
+/*---------------------------------------------------------
+ * PRIVATE METHODS
+ *--------------------------------------------------------- */
+static void clish_action_init(clish_action_t *this)
+{
+	this->script = NULL;
+	this->builtin = NULL;
+	this->shebang = NULL;
+}
+
+/*--------------------------------------------------------- */
+static void clish_action_fini(clish_action_t *this)
+{
+	lub_string_free(this->script);
+	lub_string_free(this->builtin);
+	lub_string_free(this->shebang);
+}
+
+/*---------------------------------------------------------
+ * PUBLIC META FUNCTIONS
+ *--------------------------------------------------------- */
+clish_action_t *clish_action_new(void)
+{
+	clish_action_t *this = malloc(sizeof(clish_action_t));
+
+	if (this)
+		clish_action_init(this);
+
+	return this;
+}
+
+/*---------------------------------------------------------
+ * PUBLIC METHODS
+ *--------------------------------------------------------- */
+void clish_action_delete(clish_action_t *this)
+{
+	clish_action_fini(this);
+	free(this);
+}
+
+/*---------------------------------------------------------
+ * PUBLIC ATTRIBUTES
+ *--------------------------------------------------------- */
+void clish_action__set_script(clish_action_t *this, const char *script)
+{
+	if (this->script)
+		lub_string_free(this->script);
+	this->script = lub_string_dup(script);
+}
+
+/*--------------------------------------------------------- */
+char *clish_action__get_script(const clish_action_t *this)
+{
+	return this->script;
+}
+
+/*--------------------------------------------------------- */
+void clish_action__set_builtin(clish_action_t *this, const char *builtin)
+{
+	if (this->builtin)
+		lub_string_free(this->builtin);
+	this->builtin = lub_string_dup(builtin);
+}
+
+/*--------------------------------------------------------- */
+const char *clish_action__get_builtin(const clish_action_t *this)
+{
+	return this->builtin;
+}
+
+/*--------------------------------------------------------- */
+void clish_action__set_shebang(clish_action_t *this, const char *shebang)
+{
+	const char *prog = shebang;
+	const char *prefix = "#!";
+
+	if (this->shebang)
+		lub_string_free(this->shebang);
+	if (lub_string_nocasestr(shebang, prefix) == shebang)
+		prog += strlen(prefix);
+	this->shebang = lub_string_dup(prog);
+}
+
+/*--------------------------------------------------------- */
+const char *clish_action__get_shebang(const clish_action_t *this)
+{
+	return this->shebang;
+}

+ 24 - 0
clish/action/action_dump.c

@@ -0,0 +1,24 @@
+/*
+ * action_dump.c
+ */
+
+#include "lub/dump.h"
+#include "private.h"
+
+/*--------------------------------------------------------- */
+void clish_action_dump(const clish_action_t *this)
+{
+	lub_dump_printf("action(%p)\n", this);
+	lub_dump_indent();
+
+	lub_dump_printf("script  : %s\n",
+		this->script ? this->script : "(null)");
+	lub_dump_printf("builtin : %s\n",
+		this->builtin ? this->builtin : "(null)");
+	lub_dump_printf("shebang : %s\n",
+		this->shebang ? this->shebang : "(null)");
+
+	lub_dump_undent();
+}
+
+/*--------------------------------------------------------- */

+ 4 - 0
clish/action/module.am

@@ -0,0 +1,4 @@
+libclish_la_SOURCES += \
+	clish/action/action.c \
+	clish/action/action_dump.c \
+	clish/action/private.h

+ 14 - 0
clish/action/private.h

@@ -0,0 +1,14 @@
+/*
+ * action.h
+ */
+
+#include "clish/action.h"
+
+/*---------------------------------------------------------
+ * PRIVATE TYPES
+ *--------------------------------------------------------- */
+struct clish_action_s {
+	char *script;
+	char *builtin;
+	char *shebang;
+};

+ 1 - 1
clish/callback_script.c

@@ -46,7 +46,7 @@ bool_t clish_script_callback(clish_context_t *context,
 		return BOOL_TRUE;
 
 	/* Find out shebang */
-	shebang = clish_command__get_shebang(cmd);
+	shebang = clish_action__get_shebang(clish_command__get_action(cmd));
 	if (!shebang)
 		shebang = clish_shell__get_default_shebang(this);
 	assert(shebang);

+ 3 - 8
clish/command.h

@@ -12,6 +12,7 @@ typedef struct clish_command_s clish_command_t;
 #include "clish/pargv.h"
 #include "clish/view.h"
 #include "clish/param.h"
+#include "clish/action.h"
 
 typedef enum {
 	CLISH_CONFIG_NONE,
@@ -55,20 +56,16 @@ const char *clish_command__get_name(const clish_command_t * instance);
 const char *clish_command__get_suffix(const clish_command_t * instance);
 const char *clish_command__get_text(const clish_command_t * instance);
 const char *clish_command__get_detail(const clish_command_t * instance);
-const char *clish_command__get_builtin(const clish_command_t * instance);
 const char *clish_command__get_escape_chars(const clish_command_t * instance);
 const clish_param_t *clish_command__get_args(const clish_command_t * instance);
-char *clish_command__get_action(const clish_command_t *instance);
-char *clish_command__expand_action(const clish_command_t *instance, void *context);
+clish_action_t *clish_command__get_action(const clish_command_t *instance);
+char *clish_command__expand_script(const clish_command_t *instance, void *context);
 clish_view_t *clish_command__get_view(const clish_command_t * instance);
 char *clish_command__get_viewid(const clish_command_t *instance, void *context);
 const unsigned clish_command__get_param_count(const clish_command_t * instance);
 const clish_param_t *clish_command__get_param(const clish_command_t * instance,
 	unsigned index);
 clish_paramv_t *clish_command__get_paramv(const clish_command_t * instance);
-void clish_command__set_action(clish_command_t * instance, const char *action);
-void
-clish_command__set_builtin(clish_command_t * instance, const char *builtin);
 void
 clish_command__set_escape_chars(clish_command_t * instance,
 	const char *escape_chars);
@@ -107,8 +104,6 @@ void clish_command__set_cfg_depth(clish_command_t * instance, const char * cfg_d
 unsigned clish_command__get_cfg_depth(const clish_command_t *instance, void *context);
 bool_t clish_command__get_lock(const clish_command_t * instance);
 void clish_command__set_lock(clish_command_t * instance, bool_t lock);
-const char * clish_command__get_shebang(const clish_command_t * instance);
-void clish_command__set_shebang(clish_command_t * instance, const char * shebang);
 
 void clish_command__set_alias(clish_command_t * instance, const char * alias);
 const char * clish_command__get_alias(const clish_command_t * instance);

+ 7 - 68
clish/command/command.c

@@ -36,7 +36,7 @@ clish_command_init(clish_command_t * this, const char *name, const char *text,
 	this->paramv = clish_paramv_new();
 	this->viewid = NULL;
 	this->view = NULL;
-	this->action = NULL;
+	this->action = clish_action_new();
 	this->detail = NULL;
 	this->escape_chars = NULL;
 	this->args = NULL;
@@ -45,10 +45,6 @@ clish_command_init(clish_command_t * this, const char *name, const char *text,
 	this->interrupt = BOOL_FALSE;
 	this->dynamic = BOOL_FALSE;
 
-	/* ACTION params */
-	this->builtin = NULL;
-	this->shebang = NULL;
-
 	/* CONFIG params */
 	this->cfg_op = CLISH_CONFIG_NONE;
 	this->priority = 0; /* medium priority by default */
@@ -64,9 +60,7 @@ clish_command_init(clish_command_t * this, const char *name, const char *text,
 static void clish_command_fini(clish_command_t * this)
 {
 	lub_string_free(this->name);
-	this->name = NULL;
 	lub_string_free(this->text);
-	this->text = NULL;
 
 	/* Link need not full cleanup */
 	if (this->link)
@@ -76,34 +70,16 @@ static void clish_command_fini(clish_command_t * this)
 	clish_paramv_delete(this->paramv);
 
 	lub_string_free(this->alias);
-	this->alias = NULL;
 	lub_string_free(this->viewid);
-	this->viewid = NULL;
-	lub_string_free(this->action);
-	this->action = NULL;
+	clish_action_delete(this->action);
 	lub_string_free(this->detail);
-	this->detail = NULL;
-	lub_string_free(this->builtin);
-	this->builtin = NULL;
-	lub_string_free(this->shebang);
-	this->shebang = NULL;
 	lub_string_free(this->escape_chars);
-	this->escape_chars = NULL;
-
-	if (this->args) {
+	if (this->args)
 		clish_param_delete(this->args);
-		this->args = NULL;
-	}
-
-	this->pview = NULL;
 	lub_string_free(this->pattern);
-	this->pattern = NULL;
 	lub_string_free(this->file);
-	this->file = NULL;
 	lub_string_free(this->seq);
-	this->seq = NULL;
 	lub_string_free(this->cfg_depth);
-	this->cfg_depth = NULL;
 }
 
 /*---------------------------------------------------------
@@ -262,13 +238,6 @@ const char *clish_command__get_text(const clish_command_t * this)
 	return this->text;
 }
 
-/*--------------------------------------------------------- */
-void clish_command__set_action(clish_command_t * this, const char *action)
-{
-	assert(NULL == this->action);
-	this->action = lub_string_dup(action);
-}
-
 /*--------------------------------------------------------- */
 const char *clish_command__get_detail(const clish_command_t * this)
 {
@@ -283,15 +252,16 @@ void clish_command__set_detail(clish_command_t * this, const char *detail)
 }
 
 /*--------------------------------------------------------- */
-char *clish_command__get_action(const clish_command_t *this)
+clish_action_t *clish_command__get_action(const clish_command_t *this)
 {
 	return this->action;
 }
 
 /*--------------------------------------------------------- */
-char *clish_command__expand_action(const clish_command_t *this, void *context)
+char *clish_command__expand_script(const clish_command_t *this, void *context)
 {
-	return this->var_expand_fn(this->action, context);
+	return this->var_expand_fn(clish_action__get_script(this->action),
+		context);
 }
 
 /*--------------------------------------------------------- */
@@ -345,19 +315,6 @@ const char *clish_command__get_suffix(const clish_command_t * this)
 	return lub_string_suffix(this->name);
 }
 
-/*--------------------------------------------------------- */
-void clish_command__set_builtin(clish_command_t * this, const char *builtin)
-{
-	assert(NULL == this->builtin);
-	this->builtin = lub_string_dup(builtin);
-}
-
-/*--------------------------------------------------------- */
-const char *clish_command__get_builtin(const clish_command_t * this)
-{
-	return this->builtin;
-}
-
 /*--------------------------------------------------------- */
 void
 clish_command__set_escape_chars(clish_command_t * this,
@@ -600,24 +557,6 @@ void clish_command__set_lock(clish_command_t * this, bool_t lock)
 	this->lock = lock;
 }
 
-/*--------------------------------------------------------- */
-const char * clish_command__get_shebang(const clish_command_t * this)
-{
-	return this->shebang;
-}
-
-/*--------------------------------------------------------- */
-void clish_command__set_shebang(clish_command_t * this, const char * shebang)
-{
-	const char *prog = shebang;
-	const char *prefix = "#!";
-
-	assert(!this->shebang);
-	if (lub_string_nocasestr(shebang, prefix) == shebang)
-		prog += strlen(prefix);
-	this->shebang = lub_string_dup(prog);
-}
-
 /*--------------------------------------------------------- */
 void clish_command__set_alias(clish_command_t * this, const char * alias)
 {

+ 4 - 5
clish/command/command_dump.c

@@ -1,8 +1,10 @@
 /*
  * command_dump.c
  */
-#include "private.h"
+
 #include "lub/dump.h"
+#include "clish/action.h"
+#include "private.h"
 
 /*--------------------------------------------------------- */
 void clish_command_dump(const clish_command_t * this)
@@ -19,13 +21,10 @@ void clish_command_dump(const clish_command_t * this)
 	lub_dump_printf("alias       : %s\n", this->alias);
 	lub_dump_printf("alias_view  : %s\n",
 		this->alias_view ? clish_view__get_name(this->alias_view) : "(null)");
-	lub_dump_printf("action      : %s\n",
-			this->action ? this->action : "(null)");
+	clish_action_dump(this->action);
 	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("builtin     : %s\n",
-			this->builtin ? this->builtin : "(null)");
 	switch (this->cfg_op) {
 	case CLISH_CONFIG_NONE:
 		cfg_op = "NONE";

+ 4 - 6
clish/command/private.h

@@ -1,6 +1,8 @@
 /*
  * command.h
  */
+
+#include "clish/action.h"
 #include "clish/command.h"
 
 /*---------------------------------------------------------
@@ -11,13 +13,13 @@ struct clish_command_s {
 	char *name;
 	char *text;
 	clish_paramv_t *paramv;
-	char *action;
+	clish_action_t *action;
 	clish_view_t *view;
 	char *viewid;
 	char *detail;
 	char *escape_chars;
 	clish_param_t *args;
-	const struct clish_command_s * link;
+	const struct clish_command_s *link;
 	clish_view_t *alias_view;
 	char *alias;
 	clish_view_t *pview;
@@ -26,10 +28,6 @@ struct clish_command_s {
 	bool_t dynamic; /* Is command dynamically created */
 	clish_var_expand_fn_t *var_expand_fn;
 
-	/* ACTION params: */
-	char *builtin;
-	char *shebang;
-
 	/* CONFIG params:
 	 * TODO: create special structure for CONFIG params.
 	 */

+ 3 - 0
clish/module.am

@@ -25,6 +25,7 @@ nobase_include_HEADERS += \
 	clish/view.h \
 	clish/nspace.h \
 	clish/var.h \
+	clish/action.h \
 	clish/internal.h
 
 EXTRA_DIST += \
@@ -36,6 +37,7 @@ EXTRA_DIST += \
 	clish/view/module.am \
 	clish/nspace/module.am \
 	clish/var/module.am \
+	clish/action/module.am \
 	clish/README
 
 include $(top_srcdir)/clish/command/module.am
@@ -46,3 +48,4 @@ include $(top_srcdir)/clish/shell/module.am
 include $(top_srcdir)/clish/view/module.am
 include $(top_srcdir)/clish/nspace/module.am
 include $(top_srcdir)/clish/var/module.am
+include $(top_srcdir)/clish/action/module.am

+ 1 - 0
clish/shell/private.h

@@ -7,6 +7,7 @@
 #include "clish/pargv.h"
 #include "clish/variable.h"
 #include "clish/var.h"
+#include "clish/action.h"
 
 /*-------------------------------------
  * PRIVATE TYPES

+ 2 - 2
clish/shell/shell_execute.c

@@ -264,8 +264,8 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 	}
 
 	/* Execute ACTION */
-	builtin = clish_command__get_builtin(cmd);
-	script = clish_command__expand_action(cmd, context);
+	builtin = clish_action__get_builtin(clish_command__get_action(cmd));
+	script = clish_command__expand_script(cmd, context);
 	/* account for thread cancellation whilst running a script */
 	pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
 		script);

+ 15 - 20
clish/shell/shell_tinyxml.cpp

@@ -470,31 +470,26 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 static void
 process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
 {
-	clish_command_t *cmd = NULL;
-	clish_var_t *var = NULL;
+	clish_action_t *action = NULL;
+	TiXmlNode *text = element->FirstChild();
+	const char *builtin = element->Attribute("builtin");
+	const char *shebang = element->Attribute("shebang");
 
 	if (!lub_string_nocasecmp(element->Parent()->Value(), "VAR"))
-		var = (clish_var_t *)parent;
+		action = clish_var__get_action((clish_var_t *)parent);
 	else
-		cmd = (clish_command_t *)parent;
-
-	if (cmd) {
-		// read the following text element
-		TiXmlNode *text = element->FirstChild();
-		const char *builtin = element->Attribute("builtin");
-		const char *shebang = element->Attribute("shebang");
-
-		if (text) {
-			assert(TiXmlNode::TEXT == text->Type());
-			// store the action
-			clish_command__set_action(cmd, text->Value());
-		}
+		action = clish_command__get_action((clish_command_t *)parent);
+	assert(action);
+
+	if (text) {
+		assert(TiXmlNode::TEXT == text->Type());
 		// store the action
-		if (builtin)
-			clish_command__set_builtin(cmd, builtin);
-		if (shebang)
-			clish_command__set_shebang(cmd, shebang);
+		clish_action__set_script(action, text->Value());
 	}
+	if (builtin)
+		clish_action__set_builtin(action, builtin);
+	if (shebang)
+		clish_action__set_shebang(action, shebang);
 }
 
 ////////////////////////////////////////

+ 2 - 0
clish/var.h

@@ -3,6 +3,7 @@
 
 #include "lub/types.h"
 #include "lub/bintree.h"
+#include "clish/action.h"
 
 typedef struct clish_var_s clish_var_t;
 
@@ -32,5 +33,6 @@ void clish_var__set_dynamic(clish_var_t *instance, bool_t defval);
 bool_t clish_var__get_dynamic(const clish_var_t *instance);
 void clish_var__set_value(clish_var_t *instance, const char *value);
 char *clish_var__get_value(const clish_var_t *instance);
+clish_action_t *clish_var__get_action(const clish_var_t *instance);
 
 #endif /* _clish_var_h */

+ 1 - 0
clish/var/private.h

@@ -12,4 +12,5 @@ struct clish_var_s {
 	char *name;
 	bool_t dynamic;
 	char *value;
+	clish_action_t *action;
 };

+ 8 - 0
clish/var/var.c

@@ -19,6 +19,7 @@ static void clish_var_init(clish_var_t *this, const char *name)
 	this->name = lub_string_dup(name);
 	this->dynamic = BOOL_FALSE;
 	this->value = NULL;
+	this->action = clish_action_new();
 
 	/* Be a good binary tree citizen */
 	lub_bintree_node_init(&this->bt_node);
@@ -29,6 +30,7 @@ static void clish_var_fini(clish_var_t *this)
 {
 	lub_string_free(this->name);
 	lub_string_free(this->value);
+	clish_action_delete(this->action);
 }
 
 /*---------------------------------------------------------
@@ -117,3 +119,9 @@ char *clish_var__get_value(const clish_var_t *this)
 {
 	return this->value;
 }
+
+/*--------------------------------------------------------- */
+clish_action_t *clish_var__get_action(const clish_var_t *this)
+{
+	return this->action;
+}

+ 5 - 1
clish/var/var_dump.c

@@ -1,8 +1,11 @@
 /*
  * var_dump.c
  */
-#include "private.h"
+
 #include "lub/dump.h"
+#include "clish/action.h"
+#include "private.h"
+
 /*--------------------------------------------------------- */
 void clish_var_dump(const clish_var_t *this)
 {
@@ -13,6 +16,7 @@ void clish_var_dump(const clish_var_t *this)
 	lub_dump_printf("dynamic  : %s\n",
 		this->dynamic ? "true" : "false");
 	lub_dump_printf("value    : %s\n", this->value);
+	clish_action_dump(this->action);
 
 	lub_dump_undent();
 }

+ 7 - 4
clish/view/view.c

@@ -184,21 +184,24 @@ clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
 }
 
 /*--------------------------------------------------------- */
-clish_command_t *clish_view_resolve_command(clish_view_t * this,
+clish_command_t *clish_view_resolve_command(clish_view_t *this,
 	const char *line, bool_t inherit)
 {
 	clish_command_t *result = clish_view_resolve_prefix(this, line, inherit);
 
 	if (result) {
-		if (!clish_command__get_action(result) && (NULL == clish_command__get_builtin(result)) &&
+		clish_action_t *action = clish_command__get_action(result);
+		if (!clish_action__get_script(action) &&
+			(!clish_action__get_builtin(action)) &&
 			(CLISH_CONFIG_NONE == clish_command__get_cfg_op(result)) &&
-			(NULL == clish_command__get_view(result))) {
+			(!clish_command__get_view(result))) {
 			/* if this doesn't do anything we've
-			 * not resolved a command 
+			 * not resolved a command
 			 */
 			result = NULL;
 		}
 	}
+
 	return result;
 }
 

Some files were not shown because too many files changed in this diff