Browse Source

Add default sym

Serj Kalichev 11 years ago
parent
commit
803f3d2e3c

+ 3 - 3
bin/clish.c

@@ -39,7 +39,7 @@ static clish_shell_hooks_t my_hooks = {
     NULL, /* don't worry about init callback */
     clish_access_callback,
     NULL, /* don't worry about cmd_line callback */
-    clish_script_callback,
+    NULL, /* previously clish_script_callback, */
     NULL, /* don't worry about fini callback */
     clish_config_callback,
     clish_log_callback,
@@ -159,7 +159,7 @@ int main(int argc, char **argv)
 			log = BOOL_TRUE;
 			break;
 		case 'd':
-			my_hooks.script_fn = clish_dryrun_callback;
+			my_hooks.script_fn = NULL; /* clish_dryrun_callback; */
 			break;
 		case 'x':
 			xml_path = optarg;
@@ -172,7 +172,7 @@ int main(int argc, char **argv)
 			break;
 		case 'k':
 			lockless = BOOL_TRUE;
-			my_hooks.script_fn = clish_dryrun_callback;
+			my_hooks.script_fn = NULL; /* clish_dryrun_callback; */
 			my_hooks.config_fn = NULL;
 			break;
 		case 't':

+ 1 - 0
clish.xsd

@@ -197,6 +197,7 @@
         </xs:sequence>
         <xs:attribute name="view" type="xs:string" use="required"/>
         <xs:attribute name="viewid" type="xs:string" use="optional"/>
+        <xs:attribute name="default_builtin" type="xs:string" use="optional" default="clish_script@clish"/>
         <xs:attribute name="default_shebang" type="xs:string" use="optional"/>
         <xs:attribute name="timeout" type="xs:string" use="optional"/>
         <xs:attribute name="lock" type="bool_t" use="optional" default="true"/>

+ 0 - 1
clish/module.am

@@ -3,7 +3,6 @@ lib_LTLIBRARIES += libclish.la
 
 libclish_la_SOURCES = \
     clish/callback_access.c \
-    clish/callback_script.c \
     clish/callback_config.c \
     clish/callback_log.c \
     clish/private.h

+ 3 - 0
clish/plugin.h

@@ -16,8 +16,11 @@ typedef struct clish_plugin_s clish_plugin_t;
 #define CLISH_PLUGIN_INIT_NAME "clish_plugin_init"
 #define CLISH_PLUGIN_INIT_FUNC(name) int name(clish_plugin_t *plugin)
 #define CLISH_PLUGIN_INIT CLISH_PLUGIN_INIT_FUNC(CLISH_PLUGIN_INIT_FNAME)
+
 #define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
 
+#define CLISH_DEFAULT_SYM "clish_script@clish"
+
 typedef CLISH_PLUGIN_SYM(clish_plugin_fn_t);
 typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);
 

+ 2 - 2
clish/shell.h

@@ -36,6 +36,7 @@ struct clish_context_s {
 	clish_shell_t *shell;
 	const clish_command_t *cmd;
 	clish_pargv_t *pargv;
+	const clish_action_t *action;
 };
 typedef struct clish_context_s clish_context_t;
 
@@ -280,8 +281,7 @@ clish_ptype_t *clish_shell_find_ptype(clish_shell_t *instance,
 	const char *name);
 int clish_shell_xml_read(clish_shell_t * instance, const char *filename);
 void clish_shell_help(clish_shell_t * instance, const char *line);
-int clish_shell_exec_action(clish_action_t *action,
-	clish_context_t *context, char **out);
+int clish_shell_exec_action(clish_context_t *context, char **out);
 int clish_shell_execute(clish_context_t *context, char **out);
 int clish_shell_forceline(clish_shell_t *instance, const char *line, char ** out);
 int clish_shell_readline(clish_shell_t *instance, char ** out);

+ 1 - 0
clish/shell/module.am

@@ -6,6 +6,7 @@ libclish_la_SOURCES += \
 	clish/shell/shell_dump.c \
 	clish/shell/shell_execute.c \
 	clish/shell/shell_internal.c \
+	clish/shell/shell_script.c \
 	clish/shell/shell_help.c \
 	clish/shell/shell_new.c \
 	clish/shell/shell_parse.c \

+ 7 - 0
clish/shell/private.h

@@ -69,8 +69,11 @@ struct clish_shell_s {
 	bool_t interactive; /* Is shell interactive. */
 	bool_t log; /* If command logging is enabled */
 	struct passwd *user; /* Current user information */
+
+	/* Plugins and symbols */
 	lub_list_t *plugins; /* List of plugins */
 	lub_list_t *syms; /* List of all used symbols. Must be resolved. */
+	clish_sym_t *default_sym; /* The sym to use when builtin is not specified */
 
 	/* Static params for var expanding. The refactoring is needed. */
 	clish_param_t *param_depth;
@@ -131,3 +134,7 @@ void clish_shell__init_pwd(clish_shell_pwd_t *pwd);
 void clish_shell__fini_pwd(clish_shell_pwd_t *pwd);
 int clish_shell_timeout_fn(tinyrl_t *tinyrl);
 int clish_shell_keypress_fn(tinyrl_t *tinyrl, int key);
+
+/* Internal plugin symbols */
+CLISH_PLUGIN_SYM(clish_dryrun);
+CLISH_PLUGIN_SYM(clish_script);

+ 3 - 0
clish/shell/shell_command.c

@@ -96,9 +96,12 @@ void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
 			index--;
 
 		/* Parse command line to get completion pargv's */
+		/* Prepare context */
 		context.shell = this;
 		context.cmd = cmd;
+		context.action = NULL;
 		context.pargv = pargv;
+
 		clish_shell_parse_pargv(pargv, cmd, &context,
 			clish_command__get_paramv(cmd),
 			argv, &idx, completion, index + idx);

+ 4 - 5
clish/shell/shell_execute.c

@@ -86,7 +86,6 @@ int clish_shell_execute(clish_context_t *context, char **out)
 {
 	clish_shell_t *this = context->shell;
 	const clish_command_t *cmd = context->cmd;
-	clish_action_t *action;
 	int result = 0;
 	char *lock_path = clish_shell__get_lockfile(this);
 	int lock_fd = -1;
@@ -96,7 +95,6 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	unsigned int saved_wdog_timeout = this->wdog_timeout;
 
 	assert(cmd);
-	action = clish_command__get_action(cmd);
 
 	/* Pre-change view if the command is from another depth/view */
 	{
@@ -138,7 +136,8 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	}
 
 	/* Execute ACTION */
-	result = clish_shell_exec_action(action, context, out);
+	context->action = clish_command__get_action(cmd);
+	result = clish_shell_exec_action(context, out);
 
 	/* Restore SIGINT, SIGQUIT, SIGHUP */
 	if (!clish_command__get_interrupt(cmd)) {
@@ -199,13 +198,13 @@ error:
 }
 
 /*----------------------------------------------------------- */
-int clish_shell_exec_action(clish_action_t *action,
-	clish_context_t *context, char **out)
+int clish_shell_exec_action(clish_context_t *context, char **out)
 {
 	int result = -1;
 	clish_sym_t *sym;
 	char *script;
 	clish_plugin_fn_t *func = NULL;
+	const clish_action_t *action = context->action;
 
 	if (!(sym = clish_action__get_builtin(action)))
 		return -1;

+ 4 - 0
clish/shell/shell_help.c

@@ -63,9 +63,13 @@ static int available_params(clish_shell_t *this,
 	/* get the parameter definition */
 	completion = clish_pargv_new();
 	pargv = clish_pargv_new();
+
+	/* Prepare context */
 	context.shell = this;
 	context.cmd = cmd;
+	context.action = NULL;
 	context.pargv = pargv;
+
 	status = clish_shell_parse_pargv(pargv, cmd, &context,
 		clish_command__get_paramv(cmd),
 		argv, &idx, completion, index);

+ 2 - 0
clish/shell/shell_internal.c

@@ -196,6 +196,8 @@ CLISH_PLUGIN_INIT
 	clish_plugin_add_sym(plugin, clish_nested_up, "clish_nested_up");
 	clish_plugin_add_sym(plugin, clish_nop, "clish_nop");
 	clish_plugin_add_sym(plugin, clish_wdog, "clish_wdog");
+	clish_plugin_add_sym(plugin, clish_dryrun, "clish_dryrun");
+	clish_plugin_add_sym(plugin, clish_script, "clish_script");
 
 	return 0;
 }

+ 6 - 1
clish/shell/shell_new.c

@@ -22,6 +22,7 @@ static void clish_shell_init(clish_shell_t * this,
 {
 	clish_ptype_t *tmp_ptype = NULL;
 	clish_plugin_t *plugin = NULL;
+	clish_sym_t *sym = NULL;
 
 	/* initialise the tree of views */
 	lub_bintree_init(&this->view_tree,
@@ -40,13 +41,17 @@ static void clish_shell_init(clish_shell_t * this,
 
 	/* Initialize plugin list */
 	this->plugins = lub_list_new(NULL);
+	/* Create internal plugin "clish" */
 	plugin = clish_plugin_new("clish", NULL);
 	lub_list_add(this->plugins, plugin);
 
 	/* Initialise the list of unresolved (yet) symbols */
 	this->syms = lub_list_new(clish_sym_compare);
+	/* Add default sym and save it to shell structure */
+	sym = clish_shell_add_unresolved_sym(this, CLISH_DEFAULT_SYM);
+	this->default_sym = sym;
 
-	assert((NULL != hooks) && (NULL != hooks->script_fn));
+	assert(NULL != hooks);
 
 	/* set up defaults */
 	this->client_hooks = hooks;

+ 2 - 0
clish/shell/shell_parse.c

@@ -25,9 +25,11 @@ clish_pargv_status_t clish_shell_parse(
 		return result;
 
 	/* Now construct the parameters for the command */
+	/* Prepare context */
 	*pargv = clish_pargv_new();
 	context.shell = this;
 	context.cmd = cmd;
+	context.action = NULL;
 	context.pargv = *pargv;
 
 	idx = lub_argv_wordcount(clish_command__get_name(cmd));

+ 9 - 8
clish/callback_script.c → clish/shell/shell_script.c

@@ -3,6 +3,11 @@
  *
  * Callback hook to action a shell script.
  */
+
+#include "private.h"
+#include "lub/string.h"
+#include "konf/buf.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -15,15 +20,12 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "lub/string.h"
-#include "konf/buf.h"
-#include "internal.h"
-
 /*--------------------------------------------------------- */
-int clish_script_callback(clish_context_t *context,
-	clish_action_t *action, const char *script, char **out)
+CLISH_PLUGIN_SYM(clish_script)
 {
+	clish_context_t *context = (clish_context_t *)clish_context;
 	clish_shell_t *this = context->shell;
+	const clish_action_t *action = context->action;
 	const char * shebang = NULL;
 	pid_t cpid = -1;
 	int res;
@@ -157,8 +159,7 @@ int clish_script_callback(clish_context_t *context,
 }
 
 /*--------------------------------------------------------- */
-int clish_dryrun_callback(clish_context_t *context,
-	clish_action_t *action, const char *script, char ** out)
+CLISH_PLUGIN_SYM(clish_dryrun)
 {
 #ifdef DEBUG
 	fprintf(stderr, "DRY-RUN: %s\n", script);

+ 3 - 0
clish/shell/shell_startup.c

@@ -18,9 +18,12 @@ int clish_shell_startup(clish_shell_t *this)
 	if (banner)
 		tinyrl_printf(this->tinyrl, "%s\n", banner);
 
+	/* Prepare context */
 	context.shell = this;
 	context.cmd = this->startup;
+	context.action = clish_command__get_action(this->startup);
 	context.pargv = NULL;
+	
 	/* Call log initialize */
 	if (clish_shell__get_log(this) && this->client_hooks->log_fn)
 		this->client_hooks->log_fn(&context, NULL, 0);

+ 2 - 0
clish/shell/shell_tinyrl.c

@@ -490,7 +490,9 @@ static int clish_shell_execline(clish_shell_t *this, const char *line, char **ou
 	clish_shell_renew_prompt(this);
 
 	/* Set up the context for tinyrl */
+	/* Prepare context */
 	context.cmd = NULL;
+	context.action = NULL;
 	context.pargv = NULL;
 	context.shell = this;
 

+ 2 - 2
clish/shell/shell_var.c

@@ -140,8 +140,8 @@ static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *co
 	/* Try to execute ACTION */
 	if (!res) {
 		char *out = NULL;
-		clish_action_t *action = clish_var__get_action(var);
-		if (clish_shell_exec_action(action, context, &out)) {
+		context->action = clish_var__get_action(var);
+		if (clish_shell_exec_action(context, &out)) {
 			lub_string_free(out);
 			return NULL;
 		}

+ 2 - 0
clish/shell/shell_wdog.c

@@ -50,8 +50,10 @@ int clish_shell_wdog(clish_shell_t *this)
 
 	assert(this->wdog);
 
+	/* Prepare context */
 	context.shell = this;
 	context.cmd = this->wdog;
+	context.action = clish_command__get_action(this->wdog);
 	context.pargv = NULL;
 
 	/* Call watchdog script */

+ 7 - 1
clish/shell/shell_xml.c

@@ -459,6 +459,8 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
 	char *default_shebang =
 		clish_xmlnode_fetch_attr(element, "default_shebang");
+	char *default_builtin =
+		clish_xmlnode_fetch_attr(element, "default_builtin");
 	char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
 	char *lock = clish_xmlnode_fetch_attr(element, "lock");
 	char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
@@ -483,6 +485,9 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	if (default_shebang)
 		clish_shell__set_default_shebang(shell, default_shebang);
 
+	if (default_builtin)
+		clish_sym__set_name(shell->default_sym, default_builtin);
+
 	if (timeout)
 		clish_shell__set_timeout(shell, atoi(timeout));
 
@@ -504,6 +509,7 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	clish_xml_release(view);
 	clish_xml_release(viewid);
 	clish_xml_release(default_shebang);
+	clish_xml_release(default_builtin);
 	clish_xml_release(timeout);
 	clish_xml_release(lock);
 	clish_xml_release(interrupt);
@@ -705,7 +711,7 @@ process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	if (builtin)
 		sym = clish_shell_add_unresolved_sym(shell, builtin);
 	else
-		sym = clish_shell_add_unresolved_sym(shell, builtin);
+		sym = shell->default_sym;
 	clish_action__set_builtin(action, sym);
 	if (shebang)
 		clish_action__set_shebang(action, shebang);