瀏覽代碼

Unconfuse some code. Analyze this->state.

git-svn-id: https://klish.googlecode.com/svn/trunk@381 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 年之前
父節點
當前提交
ae0d7a2dc9
共有 3 個文件被更改,包括 37 次插入48 次删除
  1. 11 17
      clish/shell/shell_execute.c
  2. 24 31
      clish/shell/shell_spawn.c
  3. 2 0
      clish/shell/shell_tinyrl.c

+ 11 - 17
clish/shell/shell_execute.c

@@ -37,15 +37,13 @@ static clish_shell_builtin_t clish_cmd_list[] = {
 };
 
 /*----------------------------------------------------------- */
-/*
- Terminate the current shell session 
-*/
+/* Terminate the current shell session */
 static bool_t clish_close(const clish_shell_t * shell, const lub_argv_t * argv)
 {
 	/* the exception proves the rule... */
 	clish_shell_t *this = (clish_shell_t *) shell;
 
-	argv = argv;		/* not used */
+	argv = argv; /* not used */
 	this->state = SHELL_STATE_CLOSING;
 
 	return BOOL_TRUE;
@@ -59,7 +57,7 @@ static bool_t clish_close(const clish_shell_t * shell, const lub_argv_t * argv)
 */
 static bool_t
 clish_source_internal(const clish_shell_t * shell,
-		      const lub_argv_t * argv, bool_t stop_on_error)
+	const lub_argv_t * argv, bool_t stop_on_error)
 {
 	bool_t result = BOOL_FALSE;
 	const char *filename = lub_argv__get_arg(argv, 0);
@@ -73,7 +71,7 @@ clish_source_internal(const clish_shell_t * shell,
 	 */
 	if ((0 == stat((char *)filename, &fileStat)) &&
 		(!S_ISDIR(fileStat.st_mode))) {
-		/* 
+		/*
 		 * push this file onto the file stack associated with this
 		 * session. This will be closed by clish_shell_pop_file() 
 		 * when it is finished with.
@@ -114,7 +112,7 @@ clish_source_nostop(const clish_shell_t * shell, const lub_argv_t * argv)
 static bool_t
 clish_overview(const clish_shell_t * this, const lub_argv_t * argv)
 {
-	argv = argv;		/* not used */
+	argv = argv; /* not used */
 
 	tinyrl_printf(this->tinyrl, "%s\n", this->overview);
 
@@ -132,7 +130,6 @@ static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
 
 	if ((NULL != arg) && ('\0' != *arg)) {
 		limit = (unsigned)atoi(arg);
-
 		if (0 == limit) {
 			/* unlimit the history list */
 			(void)tinyrl_history_unstifle(history);
@@ -142,12 +139,12 @@ static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
 		}
 	}
 	for (entry = tinyrl_history_getfirst(history, &iter);
-	     entry; entry = tinyrl_history_getnext(&iter)) {
+		entry; entry = tinyrl_history_getnext(&iter)) {
 		/* dump the details of this entry */
 		tinyrl_printf(this->tinyrl,
-			      "%5d  %s\n",
-			      tinyrl_history_entry__get_index(entry),
-			      tinyrl_history_entry__get_line(entry));
+			"%5d  %s\n",
+			tinyrl_history_entry__get_index(entry),
+			tinyrl_history_entry__get_line(entry));
 	}
 	return BOOL_TRUE;
 }
@@ -157,17 +154,14 @@ static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
  * Searches for a builtin command to execute
  */
 static clish_shell_builtin_fn_t *find_builtin_callback(const
-						       clish_shell_builtin_t *
-						       cmd_list,
-						       const char *name)
+	clish_shell_builtin_t * cmd_list, const char *name)
 {
 	const clish_shell_builtin_t *result;
 
 	/* search a list of commands */
 	for (result = cmd_list; result && result->name; result++) {
-		if (0 == strcmp(name, result->name)) {
+		if (0 == strcmp(name, result->name))
 			break;
-		}
 	}
 	return (result && result->name) ? result->callback : NULL;
 }

+ 24 - 31
clish/shell/shell_spawn.c

@@ -41,9 +41,8 @@ static char *clish_shell_tilde_expand(const char *path)
 		count++;
 		p++;
 	}
-	if (count) {
+	if (count)
 		lub_string_catn(&result, segment, count);
-	}
 	return result;
 }
 
@@ -55,16 +54,15 @@ void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
 	char *dirname;
 	char *saveptr;
 
-	if (NULL == path) {
-		/* use the default path */
+	/* use the default path */
+	if (NULL == path)
 		path = default_path;
-	}
 	/* take a copy of the path */
 	buffer = clish_shell_tilde_expand(path);
 
 	/* now loop though each directory */
 	for (dirname = strtok_r(buffer, ";", &saveptr);
-	     dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
+		dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
 		DIR *dir;
 		struct dirent *entry;
 
@@ -73,31 +71,28 @@ void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
 		if (NULL == dir) {
 #ifdef DEBUG
 			tinyrl_printf(this->tinyrl,
-				      "*** Failed to open '%s' directory\n",
-				      dirname);
+				"*** Failed to open '%s' directory\n",
+				dirname);
 #endif
 			continue;
 		}
 		for (entry = readdir(dir); entry; entry = readdir(dir)) {
 			const char *extension = strrchr(entry->d_name, '.');
 			/* check the filename */
-			if (NULL != extension) {
-				if (0 == strcmp(".xml", extension)) {
-					char *filename = NULL;
-
-					/* build the filename */
-					lub_string_cat(&filename, dirname);
-					lub_string_cat(&filename, "/");
-					lub_string_cat(&filename,
-						       entry->d_name);
-
-					/* load this file */
-					(void)clish_shell_xml_read(this,
-								   filename);
-
-					/* release the resource */
-					lub_string_free(filename);
-				}
+			if ((NULL != extension) &&
+				(0 == strcmp(".xml", extension))) {
+				char *filename = NULL;
+
+				/* build the filename */
+				lub_string_cat(&filename, dirname);
+				lub_string_cat(&filename, "/");
+				lub_string_cat(&filename, entry->d_name);
+
+				/* load this file */
+				(void)clish_shell_xml_read(this, filename);
+
+				/* release the resource */
+				lub_string_free(filename);
 			}
 		}
 		/* all done for this directory */
@@ -106,7 +101,6 @@ void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
 	/* tidy up */
 	lub_string_free(buffer);
 #ifdef DEBUG
-
 	clish_shell_dump(this);
 #endif
 }
@@ -128,7 +122,6 @@ static bool_t _loop(clish_shell_t * this, bool_t is_thread)
 	/* Loop reading and executing lines until the user quits */
 	while (running) {
 		/* Get input from the stream */
-		this->state = SHELL_STATE_READY;
 		running = clish_shell_readline(this, NULL);
 		if (!running) {
 			switch (this->state) {
@@ -139,14 +132,14 @@ static bool_t _loop(clish_shell_t * this, bool_t is_thread)
 					!this->current_file->stop_on_error)
 					running = BOOL_TRUE;
 				break;
-			case SHELL_STATE_EOF:
-				/* We've reached the end of a file */
-				running = clish_shell_pop_file(this);
-				break;
 			default:
 				break;
 			}
 		}
+		if (SHELL_STATE_CLOSING == this->state)
+			running = BOOL_FALSE;
+		if (!running)
+			running = clish_shell_pop_file(this);
 		/* test for cancellation */
 		if (is_thread)
 			pthread_testcancel();

+ 2 - 0
clish/shell/shell_tinyrl.c

@@ -24,6 +24,7 @@ struct _context {
 	const clish_command_t *cmd;
 	clish_pargv_t *pargv;
 };
+
 /*-------------------------------------------------------- */
 static bool_t clish_shell_tinyrl_key_help(tinyrl_t * this, int key)
 {
@@ -389,6 +390,7 @@ bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
 	int lerror = 0;
 
 	assert(this);
+	this->state = SHELL_STATE_READY;
 	if (!line && !tinyrl__get_istream(this->tinyrl)) {
 		this->state = SHELL_STATE_SYSTEM_ERROR;
 		return BOOL_FALSE;