From 73deba97daeab24671c4be347dd3691fa018cc27 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 1 Mar 2021 14:10:41 +0800 Subject: [PATCH] fix bug --- src/kit/shell/inc/shell.h | 2 +- src/kit/shell/src/shellDarwin.c | 13 ++++++++++--- src/kit/shell/src/shellLinux.c | 17 ++++++++++++++--- src/kit/shell/src/shellWindows.c | 13 ++++++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index 3702a7d02d..50bceb1a71 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -64,7 +64,7 @@ extern TAOS* shellInit(SShellArguments* args); extern void* shellLoopQuery(void* arg); extern void taos_error(TAOS_RES* tres, int64_t st); extern int regex_match(const char* s, const char* reg, int cflags); -void shellReadCommand(TAOS* con, char command[]); +int32_t shellReadCommand(TAOS* con, char command[]); int32_t shellRunCommand(TAOS* con, char* command); void shellRunCommandOnServer(TAOS* con, char command[]); void read_history(); diff --git a/src/kit/shell/src/shellDarwin.c b/src/kit/shell/src/shellDarwin.c index dbec3fdb05..31ad7046e9 100644 --- a/src/kit/shell/src/shellDarwin.c +++ b/src/kit/shell/src/shellDarwin.c @@ -180,7 +180,7 @@ void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { } } -void shellReadCommand(TAOS *con, char *command) { +int32_t shellReadCommand(TAOS *con, char *command) { unsigned hist_counter = history.hend; char utf8_array[10] = "\0"; Command cmd; @@ -233,7 +233,7 @@ void shellReadCommand(TAOS *con, char *command) { sprintf(command, "%s%s", cmd.buffer, cmd.command); tfree(cmd.buffer); tfree(cmd.command); - return; + return 0; } else { updateBuffer(&cmd); } @@ -324,6 +324,8 @@ void shellReadCommand(TAOS *con, char *command) { insertChar(&cmd, &c, 1); } } + + return 0; } void *shellLoopQuery(void *arg) { @@ -342,11 +344,16 @@ void *shellLoopQuery(void *arg) { return NULL; } + int32_t err = 0; + do { // Read command from shell. memset(command, 0, MAX_COMMAND_SIZE); set_terminal_mode(); - shellReadCommand(con, command); + err = shellReadCommand(con, command); + if (err) { + break; + } reset_terminal_mode(); } while (shellRunCommand(con, command) == 0); diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index a8f457377d..07b21531a7 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -172,7 +172,7 @@ void shellParseArgument(int argc, char *argv[], SShellArguments *arguments) { } } -void shellReadCommand(TAOS *con, char *command) { +int32_t shellReadCommand(TAOS *con, char *command) { unsigned hist_counter = history.hend; char utf8_array[10] = "\0"; Command cmd; @@ -186,6 +186,10 @@ void shellReadCommand(TAOS *con, char *command) { while (1) { c = (char)getchar(); // getchar() return an 'int' value + if (c == EOF) { + return c; + } + if (c < 0) { // For UTF-8 int count = countPrefixOnes(c); utf8_array[0] = c; @@ -225,7 +229,7 @@ void shellReadCommand(TAOS *con, char *command) { sprintf(command, "%s%s", cmd.buffer, cmd.command); tfree(cmd.buffer); tfree(cmd.command); - return; + return 0; } else { updateBuffer(&cmd); } @@ -316,6 +320,8 @@ void shellReadCommand(TAOS *con, char *command) { insertChar(&cmd, &c, 1); } } + + return 0; } void *shellLoopQuery(void *arg) { @@ -333,12 +339,17 @@ void *shellLoopQuery(void *arg) { uError("failed to malloc command"); return NULL; } + + int32_t err = 0; do { // Read command from shell. memset(command, 0, MAX_COMMAND_SIZE); set_terminal_mode(); - shellReadCommand(con, command); + err = shellReadCommand(con, command); + if (err) { + break; + } reset_terminal_mode(); } while (shellRunCommand(con, command) == 0); diff --git a/src/kit/shell/src/shellWindows.c b/src/kit/shell/src/shellWindows.c index 56021d9c34..11c1ac34d8 100644 --- a/src/kit/shell/src/shellWindows.c +++ b/src/kit/shell/src/shellWindows.c @@ -221,7 +221,7 @@ void insertChar(Command *cmd, char c) { cmd->command[cmd->cursorOffset++] = c; } -void shellReadCommand(TAOS *con, char command[]) { +int32_t shellReadCommand(TAOS *con, char command[]) { Command cmd; memset(&cmd, 0, sizeof(cmd)); cmd.buffer = (char *)calloc(1, MAX_COMMAND_SIZE); @@ -241,7 +241,7 @@ void shellReadCommand(TAOS *con, char command[]) { cmd.buffer = NULL; free(cmd.command); cmd.command = NULL; - return; + return 0; } else { shellPrintContinuePrompt(); updateBuffer(&cmd); @@ -251,6 +251,8 @@ void shellReadCommand(TAOS *con, char command[]) { insertChar(&cmd, c); } } + + return 0; } void *shellLoopQuery(void *arg) { @@ -258,12 +260,17 @@ void *shellLoopQuery(void *arg) { char *command = malloc(MAX_COMMAND_SIZE); if (command == NULL) return NULL; + int32_t err = 0; + do { memset(command, 0, MAX_COMMAND_SIZE); shellPrintPrompt(); // Read command from shell. - shellReadCommand(con, command); + err = shellReadCommand(con, command); + if (err) { + break; + } } while (shellRunCommand(con, command) == 0); return NULL; -- GitLab