Explorar el Código

Unconfuse with SHELL_STATE...

git-svn-id: https://klish.googlecode.com/svn/trunk@382 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev hace 13 años
padre
commit
498e7fe6a7

+ 10 - 12
clish/shell/shell_command_generator.c

@@ -159,34 +159,32 @@ static char *clish_shell_command_generator(clish_shell_t * this,
 
 /*--------------------------------------------------------- */
 char *clish_shell_word_generator(clish_shell_t * this,
-				 const char *line,
-				 unsigned offset, unsigned state)
+	const char *line, unsigned offset, unsigned state)
 {
 	char *result = NULL;
 	const clish_command_t *cmd, *next = NULL;
 
 	/* try and resolve a command which is a prefix of the line */
 	cmd = clish_shell_resolve_command(this, line);
-	if (NULL != cmd) {
+	if (cmd) {
 		clish_shell_iterator_t iter;
 		/* see whether there is an extended extension */
 		clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
 		next = clish_shell_find_next_completion(this, line, &iter);
 	}
-	if ((NULL != cmd) && (NULL == next)) {
+	if (cmd && !next) {
 		/* this needs to be completed as a parameter */
-		result =
-		    clish_shell_param_generator(this, cmd, line, offset,
-						   state);
+		result = clish_shell_param_generator(this, cmd, line, offset,
+			state);
 	} else {
 		/* this needs to be completed as a command */
-		result =
-		    clish_shell_command_generator(this, line, offset, state);
+		result = clish_shell_command_generator(this, line, offset,
+			state);
 	}
-	if (0 == state) {
-		/* reset the state from a help perspective */
+	/* reset the state from a help perspective */
+	if (0 == state)
 		this->state = SHELL_STATE_READY;
-	}
+
 	return result;
 }
 

+ 4 - 7
clish/shell/shell_help.c

@@ -84,19 +84,17 @@ void clish_shell_help(clish_shell_t * this, const char *line)
 
 	/* if there are further commands then we need to show them too */
 	cmd = clish_shell_resolve_prefix(this, line);
-	if (NULL != cmd) {
+	if (cmd) {
 		clish_shell_iterator_t iter;
 
 		/* skip the command already known about */
 		clish_shell_iterator_init(&iter, CLISH_NSPACE_HELP);
-
 		first_cmd = clish_shell_find_next_completion(this, line, &iter);
 		next_cmd = clish_shell_find_next_completion(this, line, &iter);
 	} else {
 		first_cmd = next_cmd = NULL;
 	}
-	if ((NULL != cmd) && (NULL == next_cmd)
-	    && (!first_cmd || (first_cmd == cmd))) {
+	if (cmd && !next_cmd && (!first_cmd || (first_cmd == cmd))) {
 		/* we've resolved a particular command */
 		switch (this->state) {
 		case SHELL_STATE_HELPING:
@@ -124,11 +122,10 @@ void clish_shell_help(clish_shell_t * this, const char *line)
 		available_commands(this, line, BOOL_FALSE);
 	}
 	/* update the state */
-	if (this->state == SHELL_STATE_HELPING) {
+	if (this->state == SHELL_STATE_HELPING)
 		this->state = SHELL_STATE_READY;
-	} else {
+	else
 		this->state = SHELL_STATE_HELPING;
-	}
 }
 
 /*--------------------------------------------------------- */

+ 1 - 3
clish/shell/shell_new.c

@@ -96,12 +96,10 @@ clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
 	if (this) {
 		clish_shell_init(this, hooks, cookie,
 			istream, ostream, stop_on_error);
-
 		if (hooks->init_fn) {
 			/* now call the client initialisation */
-			if (BOOL_TRUE != hooks->init_fn(this)) {
+			if (BOOL_TRUE != hooks->init_fn(this))
 				this->state = SHELL_STATE_CLOSING;
-			}
 		}
 	}
 	return this;

+ 57 - 74
clish/shell/shell_tinyrl.c

@@ -226,86 +226,69 @@ static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
 	const char *line = tinyrl__get_line(this);
 	bool_t result = BOOL_FALSE;
 
-	if (*line) {
-		/* try and parse the command */
-		cmd = clish_shell_resolve_command(context->shell, line);
-		if (NULL == cmd) {
-			tinyrl_match_e status =
-			    clish_shell_tinyrl_complete(this);
-			switch (status) {
-			case TINYRL_NO_MATCH:
-			case TINYRL_AMBIGUOUS:
-			case TINYRL_COMPLETED_AMBIGUOUS:
-				{
-					/* failed to get a unique match... */
-					break;
-				}
-			case TINYRL_MATCH:
-			case TINYRL_MATCH_WITH_EXTENSIONS:
-			case TINYRL_COMPLETED_MATCH:
-				{
-					/* re-fetch the line as it may have changed
-					 * due to auto-completion
-					 */
-					line = tinyrl__get_line(this);
-					/* get the command to parse? */
-					cmd =
-					    clish_shell_resolve_command
-					    (context->shell, line);
-					if (NULL == cmd) {
-						/*
-						 * We have had a match but it is not a command
-						 * so add a space so as not to confuse the user
-						 */
-						result =
-						    tinyrl_insert_text(this,
-								       " ");
-					}
-					break;
-				}
-			}
-		}
-		if (NULL != cmd) {
-			clish_pargv_status_t arg_status;
-			tinyrl_crlf(this);
-			/* we've got a command so check the syntax */
-			arg_status = clish_shell_parse(context->shell,
-						       line,
-						       &context->cmd,
-						       &context->pargv);
-			switch (arg_status) {
-			case CLISH_LINE_OK:
-				tinyrl_done(this);
-				result = BOOL_TRUE;
-				break;
-			case CLISH_BAD_HISTORY:
-				fprintf(stderr, "Error: Bad history entry.\n");
-				break;
-			case CLISH_BAD_CMD:
-				fprintf(stderr, "Error: Illegal command line.\n");
-				break;
-			case CLISH_BAD_PARAM:
-				fprintf(stderr, "Error: Illegal parameter.\n");
-				break;
-			case CLISH_LINE_PARTIAL:
-				fprintf(stderr, "Error: The command is not completed.\n");
-				break;
-			}
-			if (CLISH_LINE_OK != arg_status)
-				tinyrl_reset_line_state(this);
-		}
-	} else {
-		/* nothing to pass simply move down the screen */
+	/* nothing to pass simply move down the screen */
+	if (!*line) {
 		tinyrl_crlf(this);
 		tinyrl_reset_line_state(this);
-		result = BOOL_TRUE;
+		return BOOL_TRUE;
 	}
-	if ((BOOL_FALSE == result) && (BOOL_FALSE == tinyrl__get_isatty(this))) {
-		/* we've found an error in a script */
-		context->shell->state = SHELL_STATE_SCRIPT_ERROR;
+
+	/* try and parse the command */
+	cmd = clish_shell_resolve_command(context->shell, line);
+	if (!cmd) {
+		tinyrl_match_e status = clish_shell_tinyrl_complete(this);
+		switch (status) {
+		case TINYRL_MATCH:
+		case TINYRL_MATCH_WITH_EXTENSIONS:
+		case TINYRL_COMPLETED_MATCH:
+			/* re-fetch the line as it may have changed
+			 * due to auto-completion
+			 */
+			line = tinyrl__get_line(this);
+			/* get the command to parse? */
+			cmd = clish_shell_resolve_command
+				(context->shell, line);
+			/*
+			 * We have had a match but it is not a command
+			 * so add a space so as not to confuse the user
+			 */
+			if (!cmd)
+				result = tinyrl_insert_text(this, " ");
+			break;
+		default:
+			/* failed to get a unique match... */
+			break;
+		}
+	} else {
+		clish_pargv_status_t arg_status;
+		tinyrl_crlf(this);
+		/* we've got a command so check the syntax */
+		arg_status = clish_shell_parse(context->shell,
+			line, &context->cmd, &context->pargv);
+		switch (arg_status) {
+		case CLISH_LINE_OK:
+			tinyrl_done(this);
+			result = BOOL_TRUE;
+			break;
+		case CLISH_BAD_HISTORY:
+			fprintf(stderr, "Error: Bad history entry.\n");
+			break;
+		case CLISH_BAD_CMD:
+			fprintf(stderr, "Error: Illegal command line.\n");
+			break;
+		case CLISH_BAD_PARAM:
+			fprintf(stderr, "Error: Illegal parameter.\n");
+			break;
+		case CLISH_LINE_PARTIAL:
+			fprintf(stderr, "Error: The command is not completed.\n");
+			break;
+		}
+		if (CLISH_LINE_OK != arg_status)
+			tinyrl_reset_line_state(this);
 	}
 	/* keep the compiler happy */
 	key = key;
+
 	return result;
 }