Browse Source

User can enter wrong command and get error message. Earlier user can't press enter on non-existent command

Serj Kalichev 3 years ago
parent
commit
272af1c965
3 changed files with 34 additions and 37 deletions
  1. 30 34
      clish/shell/shell_tinyrl.c
  2. 3 3
      tinyrl/tinyrl.c
  3. 1 0
      tinyrl/tinyrl.h

+ 30 - 34
clish/shell/shell_tinyrl.c

@@ -223,58 +223,61 @@ 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;
 	char *errmsg = NULL;
+	tinyrl_history_t *history;
 
-	/* Inc line counter */
+	// Increment line counter
 	if (shell->current_file)
 		shell->current_file->line++;
 
-	/* nothing to pass simply move down the screen */
+	// Nothing to pass simply move down the screen
 	if (!*line) {
 		tinyrl_multi_crlf(this);
 		tinyrl_done(this);
 		return BOOL_TRUE;
 	}
 
-	/* try and parse the command */
+	// Resolve command
 	cmd = clish_shell_resolve_command(shell, line);
+	// Try to complete command if it's not found
 	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
-			 */
+			// 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(shell, line);
-			/*
-			 * We have had a match but it is not a command
-			 * so add a space so as not to confuse the user
-			 */
+			// 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... */
-			if (!tinyrl__get_isatty(this)) {
-				/* batch mode */
-				tinyrl_multi_crlf(this);
-				errmsg = "Unknown command";
-			}
+			errmsg = "Unknown command";
 			break;
 		}
 	}
+	// Workaround on ugly history.
+	// The line can be the pointer to history entry. Now we must fix it
+	// and copy string to real buffer.
+	tinyrl_changed_line(this);
+	line = tinyrl__get_line(this);
+
+	// Add any not-null line to history
+	if (tinyrl__get_isatty(this)) {
+		history = tinyrl__get_history(this);
+		tinyrl_history_add(history, tinyrl__get_line(this));
+	}
+	tinyrl_multi_crlf(this);
+
 	if (cmd) {
 		clish_pargv_status_e arg_status;
-		tinyrl_multi_crlf(this);
-		/* we've got a command so check the syntax */
 		arg_status = clish_shell_parse(shell,
 			line, &context->cmd, &context->pargv);
 		switch (arg_status) {
 		case CLISH_LINE_OK:
-			tinyrl_done(this);
 			result = BOOL_TRUE;
 			break;
 		case CLISH_BAD_HISTORY:
@@ -294,22 +297,22 @@ static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
 			break;
 		}
 	}
-	/* If error then print message */
+
+	// If error then print message
 	if (errmsg) {
 		if (tinyrl__get_isatty(this) || !shell->current_file) {
 			fprintf(stderr, "Syntax error: %s\n", errmsg);
-			tinyrl_reset_line_state(this);
 		} else {
 			char *fname = "stdin";
 			if (shell->current_file->fname)
 				fname = shell->current_file->fname;
 			fprintf(stderr, "Syntax error on line %s:%u \"%s\": "
-			"%s\n", fname, shell->current_file->line,
-			line, errmsg);
+			"%s\n", fname, shell->current_file->line, line, errmsg);
 		}
 	}
-	/* keep the compiler happy */
-	key = key;
+
+	tinyrl_done(this);
+	key = key; // Happy compiler
 
 	return result;
 }
@@ -473,7 +476,6 @@ static int clish_shell_execline(clish_shell_t *this, const char *line, char **ou
 {
 	char *str;
 	clish_context_t context;
-	tinyrl_history_t *history;
 	int lerror = 0;
 
 	assert(this);
@@ -509,13 +511,7 @@ static int clish_shell_execline(clish_shell_t *this, const char *line, char **ou
 		};
 		return -1;
 	}
-
-	/* Deal with the history list */
-	if (tinyrl__get_isatty(this->tinyrl)) {
-		history = tinyrl__get_history(this->tinyrl);
-		tinyrl_history_add(history, str);
-	}
-	free(str);
+	lub_string_free(str);
 
 	/* Execute the provided command */
 	if (context.cmd && context.pargv) {

+ 3 - 3
tinyrl/tinyrl.c

@@ -246,7 +246,7 @@ This is called whenever a line is edited in any way.
 It signals that if we are currently viewing a history line we should transfer it
 to the current buffer
 */
-static void changed_line(tinyrl_t * this)
+void tinyrl_changed_line(tinyrl_t * this)
 {
 	/* if the current line is not our buffer then make it so */
 	if (this->line != this->buffer) {
@@ -1091,7 +1091,7 @@ bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
 	 * If the client wants to change the line ensure that the line and buffer
 	 * references are in sync
 	 */
-	changed_line(this);
+	tinyrl_changed_line(this);
 
 	if ((delta + this->end) > (this->buffer_size)) {
 		/* extend the current buffer */
@@ -1176,7 +1176,7 @@ void tinyrl_delete_text(tinyrl_t * this, unsigned int start, unsigned int end)
 	 * If the client wants to change the line ensure that the line and buffer
 	 * references are in sync
 	 */
-	changed_line(this);
+	tinyrl_changed_line(this);
 
 	/* make sure we play it safe */
 	if (start > end) {

+ 1 - 0
tinyrl/tinyrl.h

@@ -126,6 +126,7 @@ extern void tinyrl_multi_crlf(const tinyrl_t * instance);
 extern void tinyrl_ding(const tinyrl_t * instance);
 
 extern void tinyrl_reset_line_state(tinyrl_t * instance);
+extern void tinyrl_changed_line(tinyrl_t * instance);
 
 extern bool_t tinyrl_insert_text(tinyrl_t * instance, const char *text);
 extern void