Browse Source

Interactive/non-interactive mode. Fix issue #41.

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

+ 10 - 1
bin/clish.cpp

@@ -51,6 +51,7 @@ int main(int argc, char **argv)
 	const char *socket_path = KONFD_SOCKET_PATH;
 	bool_t lockless = BOOL_FALSE;
 	bool_t stop_on_error = BOOL_FALSE;
+	bool_t interactive = BOOL_TRUE;
 	const char *xml_path = getenv("CLISH_PATH");
 	const char *view = getenv("CLISH_VIEW");
 	const char *viewid = getenv("CLISH_VIEWID");
@@ -59,7 +60,7 @@ int main(int argc, char **argv)
 	struct sigaction sigpipe_act;
 	sigset_t sigpipe_set;
 
-	static const char *shortopts = "hvs:ledx:w:i:";
+	static const char *shortopts = "hvs:ledx:w:i:b";
 #ifdef HAVE_GETOPT_H
 	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
@@ -71,6 +72,7 @@ int main(int argc, char **argv)
 		{"xml-path",	1, NULL, 'x'},
 		{"view",	1, NULL, 'w'},
 		{"viewid",	1, NULL, 'i'},
+		{"background",	0, NULL, 'b'},
 		{NULL,		0, NULL, 0}
 	};
 #endif
@@ -104,6 +106,9 @@ int main(int argc, char **argv)
 		case 'e':
 			stop_on_error = BOOL_TRUE;
 			break;
+		case 'b':
+			interactive = BOOL_FALSE;
+			break;
 		case 'd':
 			my_hooks.script_fn = clish_dryrun_callback;
 			break;
@@ -144,6 +149,9 @@ int main(int argc, char **argv)
 	/* Set lockless mode */
 	if (lockless)
 		clish_shell__set_lockfile(shell, NULL);
+	/* Set interactive mode */
+	if (!interactive)
+		clish_shell__set_interactive(shell, interactive);
 	/* Set startup view */
 	if (view)
 		clish_shell__set_startup_view(shell, view);
@@ -205,6 +213,7 @@ static void help(int status, const char *argv0)
 			"of the konfd daemon.\n");
 		printf("\t-l, --lockless\tDon't use locking mechanism.\n");
 		printf("\t-e, --stop-on-error\tStop programm execution on error.\n");
+		printf("\t-b, --background\tStart shell using non-interactive mode.\n");
 		printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
 		printf("\t-x, --xml-path\tPath to XML scheme files.\n");
 		printf("\t-w, --view\tSet the startup view.\n");

+ 2 - 0
clish/shell.h

@@ -386,6 +386,8 @@ char * clish_shell__expand_text(const clish_shell_t *instance,
 char * clish_shell__expand_variable(const clish_shell_t *instance,
 	clish_command_t *cmd, clish_pargv_t *pargv, const char *var);
 const char * clish_shell__get_fifo(clish_shell_t * instance);
+void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
+bool_t clish_shell__get_interactive(const clish_shell_t * instance);
 
 _END_C_DECL
 

+ 7 - 3
clish/shell/private.h

@@ -61,13 +61,17 @@ struct clish_shell_s {
 	clish_shell_pwd_t **cfg_pwdv;	/* Levels for the config file structure */
 	unsigned cfg_pwdc;
 	konf_client_t *client;
-	clish_param_t *param_depth;
-	clish_param_t *param_pwd;
 	char * lockfile;
 	pthread_t pthread;
 	clish_context_t context;
 	char * default_shebang;
-	char * fifo_name; /* The name of temporary fifo file */
+	char * fifo_name; /* The name of temporary fifo file. */
+	bool_t interactive; /* Is shell interactive. */
+
+	/* Static params for var expanding. The refactoring is needed. */
+	clish_param_t *param_depth;
+	clish_param_t *param_pwd;
+	clish_param_t *param_interactive;
 };
 
 /**

+ 1 - 0
clish/shell/shell_delete.c

@@ -62,6 +62,7 @@ static void clish_shell_fini(clish_shell_t * this)
 	/* Free internal params */
 	clish_param_delete(this->param_depth);
 	clish_param_delete(this->param_pwd);
+	clish_param_delete(this->param_interactive);
 
 	lub_string_free(this->lockfile);
 	lub_string_free(this->default_shebang);

+ 13 - 4
clish/shell/shell_new.c

@@ -47,6 +47,7 @@ clish_shell_init(clish_shell_t * this,
 	this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
 	this->default_shebang = lub_string_dup("/bin/sh");
 	this->fifo_name = NULL;
+	this->interactive = BOOL_TRUE; /* The interactive shell by default. */
 
 	/* Create internal ptypes and params */
 	/* Current depth */
@@ -65,6 +66,14 @@ clish_shell_init(clish_shell_t * this,
 	this->param_pwd = clish_param_new("__cur_pwd",
 		"Current path", tmp_ptype);
 	clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
+	/* Interactive */
+	tmp_ptype = clish_shell_find_create_ptype(this,
+		"__INTERACTIVE", "Interactive flag", "[01]",
+		CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
+	assert(tmp_ptype);
+	this->param_interactive = clish_param_new("__interactive",
+		"Interactive flag", tmp_ptype);
+	clish_param__set_hidden(this->param_interactive, BOOL_TRUE);
 
 	/* Initialize context */
 	this->context.completion_pargv = NULL;
@@ -77,10 +86,10 @@ clish_shell_init(clish_shell_t * this,
 
 /*-------------------------------------------------------- */
 clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
-		void *cookie,
-		FILE * istream,
-		FILE * ostream,
-		bool_t stop_on_error)
+	void *cookie,
+	FILE * istream,
+	FILE * ostream,
+	bool_t stop_on_error)
 {
 	clish_shell_t *this = malloc(sizeof(clish_shell_t));
 

+ 8 - 0
clish/shell/shell_parse.c

@@ -21,14 +21,22 @@ clish_pargv_status_t clish_shell_parse(
 	if (*pargv) {
 		char str[100];
 		char * tmp;
+		/* Variable __cur_depth */
 		int depth = clish_shell__get_depth(this);
 		snprintf(str, sizeof(str) - 1, "%u", depth);
 		clish_pargv_insert(*pargv, this->param_depth, str);
+		/* Variable __cur_pwd */
 		tmp = clish_shell__get_pwd_full(this, depth);
 		if (tmp) {
 			clish_pargv_insert(*pargv, this->param_pwd, tmp);
 			lub_string_free(tmp);
 		}
+		/* Variable __interactive */
+		if (clish_shell__get_interactive(this))
+			tmp = "1";
+		else
+			tmp = "0";
+		clish_pargv_insert(*pargv, this->param_interactive, tmp);
 	}
 
 	return result;

+ 14 - 2
clish/shell/shell_tinyrl.c

@@ -458,17 +458,29 @@ bool_t clish_shell_readline(clish_shell_t *this, char ** out)
 }
 
 /*-------------------------------------------------------- */
-
 FILE * clish_shell__get_istream(const clish_shell_t * this)
 {
 	return tinyrl__get_istream(this->tinyrl);
 }
 
 /*-------------------------------------------------------- */
-
 FILE * clish_shell__get_ostream(const clish_shell_t * this)
 {
 	return tinyrl__get_ostream(this->tinyrl);
 }
 
 /*-------------------------------------------------------- */
+void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
+{
+	assert(this);
+	this->interactive = interactive;
+}
+
+/*-------------------------------------------------------- */
+bool_t clish_shell__get_interactive(const clish_shell_t * this)
+{
+	assert(this);
+	return this->interactive;
+}
+
+/*-------------------------------------------------------- */