Browse Source

Client can send empty commands to server. Server answers with renewed prompt

Serj Kalichev 5 months ago
parent
commit
79062a0e8c
3 changed files with 18 additions and 9 deletions
  1. 0 5
      bin/klish/klish.c
  2. 4 1
      klish/ktp/ktp_session.c
  3. 14 3
      klish/ktp/ktpd_session.c

+ 0 - 5
bin/klish/klish.c

@@ -747,11 +747,6 @@ static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
 	tinyrl_multi_crlf(tinyrl);
 	tinyrl_reset_line_state(tinyrl);
 	line = tinyrl_line(tinyrl);
-	// Don't do anything on empty line
-	if (faux_str_is_empty(line)) {
-		faux_error_free(error);
-		return BOOL_TRUE;
-	}
 
 	ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
 

+ 4 - 1
klish/ktp/ktp_session.c

@@ -417,6 +417,9 @@ static bool_t ktp_session_process_cmd_ack(ktp_session_t *ktp, const faux_msg_t *
 		return BOOL_TRUE;
 	}
 
+	// If retcode param is not present it means all is ok (retcode = 0).
+	// Server will not send retcode in a case of empty command. Empty command
+	// doesn't execute real actions
 	if (faux_msg_get_param_by_type(msg, KTP_PARAM_RETCODE,
 		(void **)&retcode8bit, NULL))
 		ktp->cmd_retcode = (int)(*retcode8bit);
@@ -641,7 +644,7 @@ static bool_t ktp_session_drop_state(ktp_session_t *ktp, faux_error_t *error)
 		return BOOL_FALSE;
 
 	ktp->error = error;
-	ktp->cmd_retcode = -1;
+	ktp->cmd_retcode = 0;
 	ktp->cmd_retcode_available = BOOL_FALSE;
 	ktp->request_done = BOOL_FALSE;
 	ktp->cmd_features = KTP_STATUS_NONE;

+ 14 - 3
klish/ktp/ktpd_session.c

@@ -350,14 +350,25 @@ static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
 	bool_t ret = BOOL_TRUE;
 	char *prompt = NULL;
 	bool_t view_was_changed = BOOL_FALSE;
+	faux_msg_t *ack = NULL;
 
 	assert(ktpd);
 	assert(msg);
 
 	// Get line from message
 	if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
-		ktp_send_error(ktpd->async, cmd, "The line is not specified");
-		return BOOL_FALSE;
+		// Line is not specified. User sent empty command.
+		// It's not bug. Send OK to user and regenerate prompt
+		ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
+		// Generate prompt
+		prompt = generate_prompt(ktpd);
+		if (prompt) {
+			faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
+			faux_str_free(prompt);
+		}
+		faux_msg_send_async(ack, ktpd->async);
+		faux_msg_free(ack);
+		return BOOL_TRUE;
 	}
 
 	// Get dry-run flag from message
@@ -395,7 +406,7 @@ static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
 	}
 
 	// Prepare ACK message
-	faux_msg_t *ack = ktp_msg_preform(cmd, status);
+	ack = ktp_msg_preform(cmd, status);
 	if (rc) {
 		uint8_t retcode8bit = 0;
 		retcode8bit = (uint8_t)(retcode & 0xff);