Browse Source

Make context structure from shell_spawn.c is accessible for user. Three functions works with clish_context_t: clish_context_spawn(), clish_context_spawn_fd(), clish_context_wait().

git-svn-id: https://klish.googlecode.com/svn/trunk@112 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
324535e335
3 changed files with 66 additions and 21 deletions
  1. 9 0
      clish/shell.h
  2. 16 0
      clish/shell/private.h
  3. 41 21
      clish/shell/shell_spawn.c

+ 9 - 0
clish/shell.h

@@ -323,6 +323,15 @@ const char *clish_shell__get_pwd(const clish_shell_t * instance,
 char *clish_shell__get_line(const clish_command_t * cmd, clish_pargv_t * pargv);
 conf_client_t *clish_shell__get_client(const clish_shell_t * instance);
 
+/* Context */
+typedef struct clish_context_s clish_context_t;
+
+int clish_context_wait(const clish_context_t * instance);
+clish_context_t *clish_context_spawn(const pthread_attr_t * attr,
+	const clish_shell_hooks_t * hooks, void *cookie, FILE * istream);
+clish_context_t *clish_context_spawn_fd(const pthread_attr_t * attr,
+	const clish_shell_hooks_t * hooks, void *cookie, int fd);
+
 _END_C_DECL
 #endif				/* _clish_shell_h */
 /** @} clish_shell */

+ 16 - 0
clish/shell/private.h

@@ -10,6 +10,22 @@
 /*-------------------------------------
  * PRIVATE TYPES 
  *------------------------------------- */
+
+/*-------------------------------------------------------- */
+/* 
+ * The context structure is used to simplify the cleanup of 
+ * a CLI session when a thread is cancelled.
+ */
+struct clish_context_s {
+	pthread_t pthread;
+	const clish_shell_hooks_t *hooks;
+	void *cookie;
+	FILE *istream;
+	clish_shell_t *shell;
+	clish_pargv_t *pargv;
+	char *prompt;
+};
+
 typedef enum {
 	SHELL_STATE_INITIALISING,
 	SHELL_STATE_READY,

+ 41 - 21
clish/shell/shell_spawn.c

@@ -18,21 +18,6 @@
  */
 const char *default_path = "/etc/clish;~/.clish";
 
-/*-------------------------------------------------------- */
-/* 
- * The context structure is used to simplify the cleanup of 
- * a CLI session when a thread is cancelled.
- */
-typedef struct _context context_t;
-struct _context {
-	pthread_t pthread;
-	const clish_shell_hooks_t *hooks;
-	void *cookie;
-	FILE *istream;
-	clish_shell_t *shell;
-	clish_pargv_t *pargv;
-	char *prompt;
-};
 /*-------------------------------------------------------- */
 /* perform a simple tilde substitution for the home directory
  * defined in HOME
@@ -129,7 +114,7 @@ void clish_shell_load_files(clish_shell_t * this)
 /*
  * This is invoked when the thread ends or is cancelled.
  */
-static void clish_shell_cleanup(context_t * context)
+static void clish_shell_cleanup(clish_context_t * context)
 {
 #ifdef __vxworks
 	int last_state;
@@ -166,7 +151,7 @@ static void clish_shell_cleanup(context_t * context)
  */
 static void *clish_shell_thread(void *arg)
 {
-	context_t *context = arg;
+	clish_context_t *context = arg;
 	bool_t running = BOOL_TRUE;
 	clish_shell_t *this;
 	int last_type;
@@ -277,12 +262,12 @@ static void *clish_shell_thread(void *arg)
 }
 
 /*-------------------------------------------------------- */
-static context_t *_clish_shell_spawn(const pthread_attr_t * attr,
+static clish_context_t *_clish_shell_spawn(const pthread_attr_t * attr,
 				     const clish_shell_hooks_t * hooks,
 				     void *cookie, FILE * istream)
 {
 	int rtn;
-	context_t *context = malloc(sizeof(context_t));
+	clish_context_t *context = malloc(sizeof(clish_context_t));
 	assert(context);
 
 	if (context) {
@@ -310,7 +295,7 @@ _clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks,
 			    void *cookie, FILE * file)
 {
 	void *result = NULL;
-	context_t *context = _clish_shell_spawn(NULL, hooks, cookie, file);
+	clish_context_t *context = _clish_shell_spawn(NULL, hooks, cookie, file);
 
 	if (context) {
 		/* join the shell's thread and wait for it to exit */
@@ -319,6 +304,41 @@ _clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks,
 	return result ? BOOL_TRUE : BOOL_FALSE;
 }
 
+/*-------------------------------------------------------- */
+int clish_context_wait(const clish_context_t * this)
+{
+	void *result = NULL;
+
+	if (!this || !this->pthread)
+		return BOOL_FALSE;
+
+	/* join the shell's thread and wait for it to exit */
+	(void)pthread_join(this->pthread, &result);
+
+	return result ? BOOL_TRUE : BOOL_FALSE;
+}
+
+/*-------------------------------------------------------- */
+clish_context_t *clish_context_spawn(const pthread_attr_t * attr,
+				     const clish_shell_hooks_t * hooks,
+				     void *cookie, FILE * istream)
+{
+	return _clish_shell_spawn(attr, hooks, cookie, istream);
+}
+
+
+/*-------------------------------------------------------- */
+clish_context_t *clish_context_spawn_fd(const pthread_attr_t * attr,
+				     const clish_shell_hooks_t * hooks,
+				     void *cookie, int fd)
+{
+	FILE *istream;
+
+	istream = fdopen(fd, "r");
+
+	return _clish_shell_spawn(attr, hooks, cookie, istream);
+}
+
 /*-------------------------------------------------------- */
 int clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks, void *cookie)
 {
@@ -331,7 +351,7 @@ clish_shell_spawn(pthread_t * pthread,
 		  const pthread_attr_t * attr,
 		  const clish_shell_hooks_t * hooks, void *cookie)
 {
-	context_t *context;
+	clish_context_t *context;
 	bool_t result = BOOL_FALSE;
 
 	/* spawn the thread... */