提交 c234adbb 编写于 作者: wafwerar's avatar wafwerar

shell: fix shell chinese error

上级 150269b6
...@@ -104,8 +104,6 @@ extern "C" { ...@@ -104,8 +104,6 @@ extern "C" {
#include "osTimezone.h" #include "osTimezone.h"
#include "osEnv.h" #include "osEnv.h"
void osDefaultInit();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -631,7 +631,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) { ...@@ -631,7 +631,7 @@ static int32_t taosSetServerCfg(SConfig *pCfg) {
int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char **envCmd, int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDir, const char **envCmd,
const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) {
osDefaultInit(); if (tsCfg == NULL) osDefaultInit();
SConfig *pCfg = cfgInit(); SConfig *pCfg = cfgInit();
if (pCfg == NULL) return -1; if (pCfg == NULL) return -1;
......
...@@ -88,11 +88,11 @@ void taosSetSystemLocale(const char *inLocale, const char *inCharSet) { ...@@ -88,11 +88,11 @@ void taosSetSystemLocale(const char *inLocale, const char *inCharSet) {
void taosGetSystemLocale(char *outLocale, char *outCharset) { void taosGetSystemLocale(char *outLocale, char *outCharset) {
#ifdef WINDOWS #ifdef WINDOWS
char *locale = setlocale(LC_CTYPE, "chs"); char *locale = setlocale(LC_CTYPE, "en_US.UTF-8");
if (locale != NULL) { if (locale != NULL) {
tstrncpy(outLocale, locale, TD_LOCALE_LEN); tstrncpy(outLocale, locale, TD_LOCALE_LEN);
} }
strcpy(outCharset, "cp936"); strcpy(outCharset, "UTF-8");
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
/* /*
......
...@@ -52,7 +52,7 @@ echo wal 0 >> %TAOS_CFG% ...@@ -52,7 +52,7 @@ echo wal 0 >> %TAOS_CFG%
echo asyncLog 0 >> %TAOS_CFG% echo asyncLog 0 >> %TAOS_CFG%
echo locale en_US.UTF-8 >> %TAOS_CFG% echo locale en_US.UTF-8 >> %TAOS_CFG%
echo enableCoreFile 1 >> %TAOS_CFG% echo enableCoreFile 1 >> %TAOS_CFG%
echo charset cp65001 >> %TAOS_CFG% echo charset UTF-8 >> %TAOS_CFG%
set "FILE_NAME=testSuite.sim" set "FILE_NAME=testSuite.sim"
if "%1" == "-f" set "FILE_NAME=%2" if "%1" == "-f" set "FILE_NAME=%2"
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#define UP 3 #define UP 3
#define DOWN 4 #define DOWN 4
#define PSIZE shell.info.promptSize #define PSIZE shell.info.promptSize
#define SHELL_INPUT_MAX_COMMAND_SIZE 10000
typedef struct { typedef struct {
char *buffer; char *buffer;
...@@ -227,6 +228,7 @@ void shellPrintChar(char c, int32_t times) { ...@@ -227,6 +228,7 @@ void shellPrintChar(char c, int32_t times) {
} }
void shellPositionCursor(int32_t step, int32_t direction) { void shellPositionCursor(int32_t step, int32_t direction) {
#ifndef WINDOWS
if (step > 0) { if (step > 0) {
if (direction == LEFT) { if (direction == LEFT) {
fprintf(stdout, "\033[%dD", step); fprintf(stdout, "\033[%dD", step);
...@@ -239,6 +241,7 @@ void shellPositionCursor(int32_t step, int32_t direction) { ...@@ -239,6 +241,7 @@ void shellPositionCursor(int32_t step, int32_t direction) {
} }
fflush(stdout); fflush(stdout);
} }
#endif
} }
void shellUpdateBuffer(SShellCmd *cmd) { void shellUpdateBuffer(SShellCmd *cmd) {
...@@ -330,10 +333,14 @@ void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) { ...@@ -330,10 +333,14 @@ void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) {
int32_t command_x = ecmd_pos / ws_col; int32_t command_x = ecmd_pos / ws_col;
shellPositionCursor(cursor_y, LEFT); shellPositionCursor(cursor_y, LEFT);
shellPositionCursor(command_x - cursor_x, DOWN); shellPositionCursor(command_x - cursor_x, DOWN);
#ifndef WINDOWS
fprintf(stdout, "\033[2K"); fprintf(stdout, "\033[2K");
#endif
for (int32_t i = 0; i < command_x; i++) { for (int32_t i = 0; i < command_x; i++) {
shellPositionCursor(1, UP); shellPositionCursor(1, UP);
#ifndef WINDOWS
fprintf(stdout, "\033[2K"); fprintf(stdout, "\033[2K");
#endif
} }
fflush(stdout); fflush(stdout);
} }
...@@ -394,6 +401,38 @@ void shellShowOnScreen(SShellCmd *cmd) { ...@@ -394,6 +401,38 @@ void shellShowOnScreen(SShellCmd *cmd) {
fflush(stdout); fflush(stdout);
} }
char taosGetConsoleChar() {
#ifdef WINDOWS
static void *console = NULL;
if (console == NULL) {
console = GetStdHandle(STD_INPUT_HANDLE);
}
static TdWchar buf[SHELL_INPUT_MAX_COMMAND_SIZE];
static char mbStr[5];
static unsigned long bufLen = 0;
static uint16_t bufIndex = 0, mbStrIndex = 0, mbStrLen = 0;
if (bufLen == 0) {
ReadConsoleW(console, buf, SHELL_INPUT_MAX_COMMAND_SIZE, &bufLen, NULL);
bufIndex = 0;
}
if (mbStrLen == 0){
mbStrLen = WideCharToMultiByte(CP_UTF8, 0, &buf[bufIndex], 1, mbStr, sizeof(mbStr), NULL, NULL);
mbStrIndex = 0;
bufIndex++;
}
mbStrIndex++;
if (mbStrIndex == mbStrLen) {
mbStrLen = 0;
if (bufIndex == bufLen) {
bufLen = 0;
}
}
return mbStr[mbStrIndex-1];
#else
return (char)getchar(); // getchar() return an 'int32_t' value
#endif
}
int32_t shellReadCommand(char *command) { int32_t shellReadCommand(char *command) {
SShellHistory *pHistory = &shell.history; SShellHistory *pHistory = &shell.history;
SShellCmd cmd = {0}; SShellCmd cmd = {0};
...@@ -407,7 +446,7 @@ int32_t shellReadCommand(char *command) { ...@@ -407,7 +446,7 @@ int32_t shellReadCommand(char *command) {
// Read input. // Read input.
char c; char c;
while (1) { while (1) {
c = (char)getchar(); // getchar() return an 'int32_t' value c = taosGetConsoleChar();
if (c == EOF) { if (c == EOF) {
return c; return c;
...@@ -417,7 +456,7 @@ int32_t shellReadCommand(char *command) { ...@@ -417,7 +456,7 @@ int32_t shellReadCommand(char *command) {
int32_t count = shellCountPrefixOnes(c); int32_t count = shellCountPrefixOnes(c);
utf8_array[0] = c; utf8_array[0] = c;
for (int32_t k = 1; k < count; k++) { for (int32_t k = 1; k < count; k++) {
c = (char)getchar(); c = taosGetConsoleChar();
utf8_array[k] = c; utf8_array[k] = c;
} }
shellInsertChar(&cmd, utf8_array, count); shellInsertChar(&cmd, utf8_array, count);
...@@ -446,6 +485,7 @@ int32_t shellReadCommand(char *command) { ...@@ -446,6 +485,7 @@ int32_t shellReadCommand(char *command) {
shellBackspaceChar(&cmd); shellBackspaceChar(&cmd);
break; break;
case '\n': case '\n':
break;
case '\r': case '\r':
#ifdef WINDOWS #ifdef WINDOWS
#else #else
...@@ -472,10 +512,10 @@ int32_t shellReadCommand(char *command) { ...@@ -472,10 +512,10 @@ int32_t shellReadCommand(char *command) {
break; break;
} }
} else if (c == '\033') { } else if (c == '\033') {
c = (char)getchar(); c = taosGetConsoleChar();
switch (c) { switch (c) {
case '[': case '[':
c = (char)getchar(); c = taosGetConsoleChar();
switch (c) { switch (c) {
case 'A': // Up arrow case 'A': // Up arrow
if (hist_counter != pHistory->hstart) { if (hist_counter != pHistory->hstart) {
...@@ -502,35 +542,35 @@ int32_t shellReadCommand(char *command) { ...@@ -502,35 +542,35 @@ int32_t shellReadCommand(char *command) {
shellMoveCursorLeft(&cmd); shellMoveCursorLeft(&cmd);
break; break;
case '1': case '1':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// Home key // Home key
shellPositionCursorHome(&cmd); shellPositionCursorHome(&cmd);
} }
break; break;
case '2': case '2':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// Insert key // Insert key
} }
break; break;
case '3': case '3':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// Delete key // Delete key
shellDeleteChar(&cmd); shellDeleteChar(&cmd);
} }
break; break;
case '4': case '4':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// End key // End key
shellPositionCursorEnd(&cmd); shellPositionCursorEnd(&cmd);
} }
break; break;
case '5': case '5':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// Page up key // Page up key
} }
break; break;
case '6': case '6':
if ((c = (char)getchar()) == '~') { if ((c = taosGetConsoleChar()) == '~') {
// Page down key // Page down key
} }
break; break;
......
...@@ -393,15 +393,11 @@ void shellPrintNChar(const char *str, int32_t length, int32_t width) { ...@@ -393,15 +393,11 @@ void shellPrintNChar(const char *str, int32_t length, int32_t width) {
break; break;
} }
int w = 0; int w = 0;
#ifdef WINDOWS
w = bytes;
#else
if(*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r'){ if(*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r'){
w = bytes; w = bytes;
}else{ }else{
w = taosWcharWidth(wc); w = taosWcharWidth(wc);
} }
#endif
pos += bytes; pos += bytes;
if (w <= 0) { if (w <= 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册