diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 54a3cfef7be19beb58e927b6eec616895c27065e..aec63f3322e2188b44986acaa1c4cb1b90ab1e91 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -47,7 +47,6 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes); int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes); -int32_t taosSystem(const char *cmd); void taosKillSystem(); int32_t taosGetSystemUUID(char *uid, int32_t uidlen); char *taosGetCmdlineByPID(int32_t pid); diff --git a/include/os/osSystem.h b/include/os/osSystem.h index 8554c4ad12f8737aac63a1815b63acf9cf0f69ff..f130e9d8f19c76171c44361fc8676b10bc650080 100644 --- a/include/os/osSystem.h +++ b/include/os/osSystem.h @@ -20,11 +20,23 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define popen POPEN_FUNC_TAOS_FORBID + #define pclose PCLOSE_FUNC_TAOS_FORBID + #define tcsetattr TCSETATTR_FUNC_TAOS_FORBID + #define tcgetattr TCGETATTR_FUNC_TAOS_FORBID +#endif + +int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize); void* taosLoadDll(const char* filename); void* taosLoadSym(void* handle, char* name); void taosCloseDll(void* handle); int32_t taosSetConsoleEcho(bool on); +void setTerminalMode(); +int32_t getOldTerminalMode(); +void resetTerminalMode(); #ifdef __cplusplus } diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index b24a20d8c09457498cb9ff52b8cced9a629e94d1..25c748a8d88a99a10e120e6fc7992792b2fbd799 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -671,41 +671,6 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { #endif } -int taosSystem(const char *cmd) { -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - printf("taosSystem not support"); - return -1; -#elif defined(_TD_DARWIN_64) - printf("no support funtion"); - return -1; -#else - FILE *fp; - int res; - char buf[1024]; - if (cmd == NULL) { - // printf("taosSystem cmd is NULL!"); - return -1; - } - - if ((fp = popen(cmd, "r")) == NULL) { - // printf("popen cmd:%s error: %s", cmd, strerror(errno)); - return -1; - } else { - while (fgets(buf, sizeof(buf), fp)) { - // printf("popen result:%s", buf); - } - - if ((res = pclose(fp)) == -1) { - // printf("close popen file pointer fp error!"); - } else { - // printf("popen res is :%d", res); - } - - return res; - } -#endif -} - void taosKillSystem() { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) printf("function taosKillSystem, exit!"); diff --git a/source/os/src/osSystem.c b/source/os/src/osSystem.c index 717cae0fbd1b5f1ab464a93fb16038a99613ea63..5c98db33d1007ff8f21872ca48096a108912724f 100644 --- a/source/os/src/osSystem.c +++ b/source/os/src/osSystem.c @@ -13,78 +13,80 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#elif defined(_TD_DARWIN_64) +#else +#include +#include +#include +#endif -/* - * windows implementation - */ +struct termios oldtio; -void* taosLoadDll(const char* filename) { return NULL; } -void* taosLoadSym(void* handle, char* name) { return NULL; } -void taosCloseDll(void* handle) {} +int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + FILE *fp; + if (cmd == NULL) { + // printf("taosSystem cmd is NULL!"); + return -1; + } -int taosSetConsoleEcho(bool on) { - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - DWORD mode = 0; - GetConsoleMode(hStdin, &mode); - if (on) { - mode |= ENABLE_ECHO_INPUT; + if ((fp = _popen(cmd, "r")) == NULL) { + // printf("popen cmd:%s error: %s", cmd, strerror(errno)); + return -1; } else { - mode &= ~ENABLE_ECHO_INPUT; - } - SetConsoleMode(hStdin, mode); + while (fgets(buf, bufSize, fp)) { + // printf("popen result:%s", buf); + } - return 0; -} + if (!_pclose(fp)) { + // printf("close popen file pointer fp error!"); + return -1; + } else { + // printf("popen res is :%d", res); + } + return 0; #elif defined(_TD_DARWIN_64) - -/* - * darwin implementation - */ - -void* taosLoadDll(const char* filename) { return NULL; } -void* taosLoadSym(void* handle, char* name) { return NULL; } -void taosCloseDll(void* handle) {} - -int taosSetConsoleEcho(bool on) { -#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) - int err; - struct termios term; - - if (tcgetattr(STDIN_FILENO, &term) == -1) { - perror("Cannot get the attribution of the terminal"); + printf("no support funtion"); + return -1; +#else + FILE *fp; + int32_t res; + if (cmd == NULL) { + // printf("taosSystem cmd is NULL!"); return -1; } - if (on) - term.c_lflag |= ECHOFLAGS; - else - term.c_lflag &= ~ECHOFLAGS; - - err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); - if (err == -1 && err == EINTR) { - perror("Cannot set the attribution of the terminal"); + if ((fp = popen(cmd, "r")) == NULL) { + // printf("popen cmd:%s error: %s", cmd, strerror(errno)); return -1; - } - - return 0; -} - -#else + } else { + while (fgets(buf, bufSize, fp)) { + // printf("popen result:%s", buf); + } -/* - * linux implementation - */ + if ((res = pclose(fp)) == -1) { + // printf("close popen file pointer fp error!"); + } else { + // printf("popen res is :%d", res); + } -#include -#include -#include + return res; + } +#endif +} void* taosLoadDll(const char* filename) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return NULL; +#elif defined(_TD_DARWIN_64) + return NULL; +#else void* handle = dlopen(filename, RTLD_LAZY); if (!handle) { //printf("load dll:%s failed, error:%s", filename, dlerror()); @@ -94,9 +96,15 @@ void* taosLoadDll(const char* filename) { //printf("dll %s loaded", filename); return handle; +#endif } void* taosLoadSym(void* handle, char* name) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return NULL; +#elif defined(_TD_DARWIN_64) + return NULL; +#else void* sym = dlsym(handle, name); char* error = NULL; @@ -108,15 +116,57 @@ void* taosLoadSym(void* handle, char* name) { //printf("sym %s loaded", name); return sym; +#endif } -void taosCloseDll(void* handle) { +void taosCloseDll(void* handle) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + return; +#elif defined(_TD_DARWIN_64) + return; +#else if (handle) { dlclose(handle); } +#endif } int taosSetConsoleEcho(bool on) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(hStdin, &mode); + if (on) { + mode |= ENABLE_ECHO_INPUT; + } else { + mode &= ~ENABLE_ECHO_INPUT; + } + SetConsoleMode(hStdin, mode); + + return 0; +#elif defined(_TD_DARWIN_64) +#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) + int err; + struct termios term; + + if (tcgetattr(STDIN_FILENO, &term) == -1) { + perror("Cannot get the attribution of the terminal"); + return -1; + } + + if (on) + term.c_lflag |= ECHOFLAGS; + else + term.c_lflag &= ~ECHOFLAGS; + + err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + if (err == -1 && err == EINTR) { + perror("Cannot set the attribution of the terminal"); + return -1; + } + + return 0; +#else #define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) int err; struct termios term; @@ -138,6 +188,111 @@ int taosSetConsoleEcho(bool on) { } return 0; +#endif +} + +void setTerminalMode() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +#elif defined(_TD_DARWIN_64) + struct termios newtio; + + /* if (atexit() != 0) { */ + /* fprintf(stderr, "Error register exit function!\n"); */ + /* exit(EXIT_FAILURE); */ + /* } */ + + memcpy(&newtio, &oldtio, sizeof(oldtio)); + + // Set new terminal attributes. + newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP); + newtio.c_iflag |= IGNBRK; + + // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); + newtio.c_oflag |= OPOST; + newtio.c_oflag |= ONLCR; + newtio.c_oflag &= ~(OCRNL | ONLRET); + + newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG); + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + + if (tcsetattr(0, TCSANOW, &newtio) != 0) { + fprintf(stderr, "Fail to set terminal properties!\n"); + exit(EXIT_FAILURE); + } +#else + struct termios newtio; + + /* if (atexit() != 0) { */ + /* fprintf(stderr, "Error register exit function!\n"); */ + /* exit(EXIT_FAILURE); */ + /* } */ + + memcpy(&newtio, &oldtio, sizeof(oldtio)); + + // Set new terminal attributes. + newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP); + newtio.c_iflag |= IGNBRK; + + // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); + newtio.c_oflag |= OPOST; + newtio.c_oflag |= ONLCR; + newtio.c_oflag &= ~(OCRNL | ONLRET); + + newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG); + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + + if (tcsetattr(0, TCSANOW, &newtio) != 0) { + fprintf(stderr, "Fail to set terminal properties!\n"); + exit(EXIT_FAILURE); + } +#endif +} + +int32_t getOldTerminalMode() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +#elif defined(_TD_DARWIN_64) + /* Make sure stdin is a terminal. */ + if (!isatty(STDIN_FILENO)) { + return -1; + } + + // Get the parameter of current terminal + if (tcgetattr(0, &oldtio) != 0) { + return -1; + } + + return 1; +#else + /* Make sure stdin is a terminal. */ + if (!isatty(STDIN_FILENO)) { + return -1; + } + + // Get the parameter of current terminal + if (tcgetattr(0, &oldtio) != 0) { + return -1; + } + + return 1; +#endif } +void resetTerminalMode() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +#elif defined(_TD_DARWIN_64) + if (tcsetattr(0, TCSANOW, &oldtio) != 0) { + fprintf(stderr, "Fail to reset the terminal properties!\n"); + exit(EXIT_FAILURE); + } +#else + if (tcsetattr(0, TCSANOW, &oldtio) != 0) { + fprintf(stderr, "Fail to reset the terminal properties!\n"); + exit(EXIT_FAILURE); + } #endif +} \ No newline at end of file diff --git a/tools/shell/inc/shell.h b/tools/shell/inc/shell.h index cf31e9d9d9e8b75d8f542414ffdc0e36a3829a70..7a16ee9d2cd8ca1a46471dc7efe59e8a4a4baa90 100644 --- a/tools/shell/inc/shell.h +++ b/tools/shell/inc/shell.h @@ -86,10 +86,6 @@ extern char PROMPT_HEADER[]; extern char CONTINUE_PROMPT[]; extern int prompt_size; extern SShellHistory history; -extern struct termios oldtio; -extern void set_terminal_mode(); -extern int get_old_terminal_mode(struct termios* tio); -extern void reset_terminal_mode(); extern SShellArguments args; extern int64_t result; diff --git a/tools/shell/src/backup/shellDarwin.c b/tools/shell/src/backup/shellDarwin.c index f2ab468574e29d178cf15cf47c618b11cd869030..e4cf09358bc1b0b6e4777577373288c577477efc 100644 --- a/tools/shell/src/backup/shellDarwin.c +++ b/tools/shell/src/backup/shellDarwin.c @@ -358,7 +358,7 @@ int32_t shellReadCommand(TAOS *con, char *command) { void *shellLoopQuery(void *arg) { if (indicator) { - get_old_terminal_mode(&oldtio); + getOldTerminalMode(); indicator = 0; } @@ -379,12 +379,12 @@ void *shellLoopQuery(void *arg) { do { // Read command from shell. memset(command, 0, MAX_COMMAND_SIZE); - set_terminal_mode(); + setTerminalMode(); err = shellReadCommand(con, command); if (err) { break; } - reset_terminal_mode(); + resetTerminalMode(); } while (shellRunCommand(con, command) == 0); tfree(command); @@ -395,56 +395,6 @@ void *shellLoopQuery(void *arg) { return NULL; } -int get_old_terminal_mode(struct termios *tio) { - /* Make sure stdin is a terminal. */ - if (!isatty(STDIN_FILENO)) { - return -1; - } - - // Get the parameter of current terminal - if (tcgetattr(0, &oldtio) != 0) { - return -1; - } - - return 1; -} - -void reset_terminal_mode() { - if (tcsetattr(0, TCSANOW, &oldtio) != 0) { - fprintf(stderr, "Fail to reset the terminal properties!\n"); - exit(EXIT_FAILURE); - } -} - -void set_terminal_mode() { - struct termios newtio; - - /* if (atexit(reset_terminal_mode) != 0) { */ - /* fprintf(stderr, "Error register exit function!\n"); */ - /* exit(EXIT_FAILURE); */ - /* } */ - - memcpy(&newtio, &oldtio, sizeof(oldtio)); - - // Set new terminal attributes. - newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP); - newtio.c_iflag |= IGNBRK; - - // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); - newtio.c_oflag |= OPOST; - newtio.c_oflag |= ONLCR; - newtio.c_oflag &= ~(OCRNL | ONLRET); - - newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG); - newtio.c_cc[VMIN] = 1; - newtio.c_cc[VTIME] = 0; - - if (tcsetattr(0, TCSANOW, &newtio) != 0) { - fprintf(stderr, "Fail to set terminal properties!\n"); - exit(EXIT_FAILURE); - } -} - void get_history_path(char *history) { sprintf(history, "%s/%s", getpwuid(getuid())->pw_dir, HISTORY_FILE); } void clearScreen(int ecmd_pos, int cursor_pos) { @@ -541,9 +491,9 @@ void showOnScreen(Command *cmd) { fflush(stdout); } -void cleanup_handler(void *arg) { tcsetattr(0, TCSANOW, &oldtio); } +void cleanup_handler(void *arg) { resetTerminalMode(); } void exitShell() { - tcsetattr(0, TCSANOW, &oldtio); + resetTerminalMode(); exit(EXIT_SUCCESS); } diff --git a/tools/shell/src/backup/shellImport.c b/tools/shell/src/backup/shellImport.c index 36489a448be3a17444bb511d60591ed814d8fb8d..b42f77e87e0a90e152d1a8b937ab8a27e6a5ec20 100644 --- a/tools/shell/src/backup/shellImport.c +++ b/tools/shell/src/backup/shellImport.c @@ -39,14 +39,14 @@ static int shellGetFilesNum(const char *directoryName, const char *prefix) char cmd[1024] = { 0 }; sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix); - FILE *fp = popen(cmd, "r"); - if (fp == NULL) { + char buf[1024] = { 0 }; + if (taosSystem(cmd, buf, sizeof(buf)) < 0) { fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); exit(0); } int fileNum = 0; - if (fscanf(fp, "%d", &fileNum) != 1) { + if (sscanf(buf, "%d", &fileNum) != 1) { fprintf(stderr, "ERROR: failed to execute:%s, parse result error\n", cmd); exit(0); } @@ -56,7 +56,6 @@ static int shellGetFilesNum(const char *directoryName, const char *prefix) exit(0); } - pclose(fp); return fileNum; } @@ -65,14 +64,14 @@ static void shellParseDirectory(const char *directoryName, const char *prefix, c char cmd[1024] = { 0 }; sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix); - FILE *fp = popen(cmd, "r"); - if (fp == NULL) { + char buf[1024] = { 0 }; + if (taosSystem(cmd, buf, sizeof(buf)) < 0) { fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno)); exit(0); } int fileNum = 0; - while (fscanf(fp, "%128s", fileArray[fileNum++])) { + while (sscanf(buf, "%128s", fileArray[fileNum++])) { if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) { fileNum--; } @@ -85,8 +84,6 @@ static void shellParseDirectory(const char *directoryName, const char *prefix, c fprintf(stderr, "ERROR: directory:%s changed while read\n", directoryName); exit(0); } - - pclose(fp); } static void shellCheckTablesSQLFile(const char *directoryName) diff --git a/tools/shell/src/shellLinux.c b/tools/shell/src/shellLinux.c index 57d6df00512014191d23fba41c4ee7c800556982..cc497688d1bfcea14aba2e6869984962b305b075 100644 --- a/tools/shell/src/shellLinux.c +++ b/tools/shell/src/shellLinux.c @@ -388,7 +388,7 @@ int32_t shellReadCommand(TAOS *con, char *command) { void *shellLoopQuery(void *arg) { if (indicator) { - get_old_terminal_mode(&oldtio); + getOldTerminalMode(); indicator = 0; } @@ -409,12 +409,12 @@ void *shellLoopQuery(void *arg) { do { // Read command from shell. memset(command, 0, MAX_COMMAND_SIZE); - set_terminal_mode(); + setTerminalMode(); err = shellReadCommand(con, command); if (err) { break; } - reset_terminal_mode(); + resetTerminalMode(); } while (shellRunCommand(con, command) == 0); tfree(command); @@ -425,56 +425,6 @@ void *shellLoopQuery(void *arg) { return NULL; } -int get_old_terminal_mode(struct termios *tio) { - /* Make sure stdin is a terminal. */ - if (!isatty(STDIN_FILENO)) { - return -1; - } - - // Get the parameter of current terminal - if (tcgetattr(0, &oldtio) != 0) { - return -1; - } - - return 1; -} - -void reset_terminal_mode() { - if (tcsetattr(0, TCSANOW, &oldtio) != 0) { - fprintf(stderr, "Fail to reset the terminal properties!\n"); - exit(EXIT_FAILURE); - } -} - -void set_terminal_mode() { - struct termios newtio; - - /* if (atexit(reset_terminal_mode) != 0) { */ - /* fprintf(stderr, "Error register exit function!\n"); */ - /* exit(EXIT_FAILURE); */ - /* } */ - - memcpy(&newtio, &oldtio, sizeof(oldtio)); - - // Set new terminal attributes. - newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP); - newtio.c_iflag |= IGNBRK; - - // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); - newtio.c_oflag |= OPOST; - newtio.c_oflag |= ONLCR; - newtio.c_oflag &= ~(OCRNL | ONLRET); - - newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG); - newtio.c_cc[VMIN] = 1; - newtio.c_cc[VTIME] = 0; - - if (tcsetattr(0, TCSANOW, &newtio) != 0) { - fprintf(stderr, "Fail to set terminal properties!\n"); - exit(EXIT_FAILURE); - } -} - void get_history_path(char *_history) { snprintf(_history, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), HISTORY_FILE); } void clearScreen(int ecmd_pos, int cursor_pos) { @@ -571,10 +521,10 @@ void showOnScreen(Command *cmd) { fflush(stdout); } -void cleanup_handler(void *arg) { tcsetattr(0, TCSANOW, &oldtio); } +void cleanup_handler(void *arg) { resetTerminalMode(); } void exitShell() { - /*int32_t ret =*/ tcsetattr(STDIN_FILENO, TCSANOW, &oldtio); + /*int32_t ret =*/ resetTerminalMode(); taos_cleanup(); exit(EXIT_SUCCESS); } diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index f62c43773d286a03aa9ad073d21128a5eed3f18d..2832855517e2529d511a39adcf749b87a6c7d1d3 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -41,11 +41,11 @@ void *cancelHandler(void *arg) { taosReleaseRef(tscObjRef, rid); #endif #else - reset_terminal_mode(); + resetTerminalMode(); printf("\nReceive ctrl+c or other signal, quit shell.\n"); exit(0); #endif - reset_terminal_mode(); + resetTerminalMode(); printf("\nReceive ctrl+c or other signal, quit shell.\n"); exit(0); }