Merge pull request #10684 from taosdata/fix/ZhiqiangWang/TD-13770-redefine-system-api
[TD-13770]<fix>: redefine system api.
This commit is contained in:
commit
c0864a80cf
|
@ -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 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 taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes);
|
||||||
|
|
||||||
int32_t taosSystem(const char *cmd);
|
|
||||||
void taosKillSystem();
|
void taosKillSystem();
|
||||||
int32_t taosGetSystemUUID(char *uid, int32_t uidlen);
|
int32_t taosGetSystemUUID(char *uid, int32_t uidlen);
|
||||||
char *taosGetCmdlineByPID(int32_t pid);
|
char *taosGetCmdlineByPID(int32_t pid);
|
||||||
|
|
|
@ -20,11 +20,23 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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* taosLoadDll(const char* filename);
|
||||||
void* taosLoadSym(void* handle, char* name);
|
void* taosLoadSym(void* handle, char* name);
|
||||||
void taosCloseDll(void* handle);
|
void taosCloseDll(void* handle);
|
||||||
|
|
||||||
int32_t taosSetConsoleEcho(bool on);
|
int32_t taosSetConsoleEcho(bool on);
|
||||||
|
void setTerminalMode();
|
||||||
|
int32_t getOldTerminalMode();
|
||||||
|
void resetTerminalMode();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -671,41 +671,6 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
|
||||||
#endif
|
#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() {
|
void taosKillSystem() {
|
||||||
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
||||||
printf("function taosKillSystem, exit!");
|
printf("function taosKillSystem, exit!");
|
||||||
|
|
|
@ -13,20 +13,126 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ALLOW_FORBID_FUNC
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
|
|
||||||
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
||||||
|
#elif defined(_TD_DARWIN_64)
|
||||||
|
#else
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
struct termios oldtio;
|
||||||
* windows implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
void* taosLoadDll(const char* filename) { return NULL; }
|
int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) {
|
||||||
void* taosLoadSym(void* handle, char* name) { return NULL; }
|
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
||||||
void taosCloseDll(void* handle) {}
|
FILE *fp;
|
||||||
|
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, bufSize, fp)) {
|
||||||
|
// printf("popen result:%s", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
printf("no support funtion");
|
||||||
|
return -1;
|
||||||
|
#else
|
||||||
|
FILE *fp;
|
||||||
|
int32_t res;
|
||||||
|
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, bufSize, 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* 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());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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;
|
||||||
|
|
||||||
|
if ((error = dlerror()) != NULL) {
|
||||||
|
//printf("load sym:%s failed, error:%s", name, dlerror());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//printf("sym %s loaded", name);
|
||||||
|
|
||||||
|
return sym;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
int taosSetConsoleEcho(bool on) {
|
||||||
|
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
|
||||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||||
DWORD mode = 0;
|
DWORD mode = 0;
|
||||||
GetConsoleMode(hStdin, &mode);
|
GetConsoleMode(hStdin, &mode);
|
||||||
|
@ -38,19 +144,7 @@ int taosSetConsoleEcho(bool on) {
|
||||||
SetConsoleMode(hStdin, mode);
|
SetConsoleMode(hStdin, mode);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(_TD_DARWIN_64)
|
#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)
|
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
|
||||||
int err;
|
int err;
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
@ -72,51 +166,7 @@ int taosSetConsoleEcho(bool on) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
/*
|
|
||||||
* linux implementation
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <dlfcn.h>
|
|
||||||
#include <termios.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
void* taosLoadDll(const char* filename) {
|
|
||||||
void* handle = dlopen(filename, RTLD_LAZY);
|
|
||||||
if (!handle) {
|
|
||||||
//printf("load dll:%s failed, error:%s", filename, dlerror());
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//printf("dll %s loaded", filename);
|
|
||||||
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* taosLoadSym(void* handle, char* name) {
|
|
||||||
void* sym = dlsym(handle, name);
|
|
||||||
char* error = NULL;
|
|
||||||
|
|
||||||
if ((error = dlerror()) != NULL) {
|
|
||||||
//printf("load sym:%s failed, error:%s", name, dlerror());
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//printf("sym %s loaded", name);
|
|
||||||
|
|
||||||
return sym;
|
|
||||||
}
|
|
||||||
|
|
||||||
void taosCloseDll(void* handle) {
|
|
||||||
if (handle) {
|
|
||||||
dlclose(handle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int taosSetConsoleEcho(bool on) {
|
|
||||||
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
|
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
|
||||||
int err;
|
int err;
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
@ -138,6 +188,111 @@ int taosSetConsoleEcho(bool on) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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
|
#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
|
||||||
|
}
|
|
@ -86,10 +86,6 @@ extern char PROMPT_HEADER[];
|
||||||
extern char CONTINUE_PROMPT[];
|
extern char CONTINUE_PROMPT[];
|
||||||
extern int prompt_size;
|
extern int prompt_size;
|
||||||
extern SShellHistory history;
|
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 SShellArguments args;
|
||||||
extern int64_t result;
|
extern int64_t result;
|
||||||
|
|
||||||
|
|
|
@ -358,7 +358,7 @@ int32_t shellReadCommand(TAOS *con, char *command) {
|
||||||
|
|
||||||
void *shellLoopQuery(void *arg) {
|
void *shellLoopQuery(void *arg) {
|
||||||
if (indicator) {
|
if (indicator) {
|
||||||
get_old_terminal_mode(&oldtio);
|
getOldTerminalMode();
|
||||||
indicator = 0;
|
indicator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,12 +379,12 @@ void *shellLoopQuery(void *arg) {
|
||||||
do {
|
do {
|
||||||
// Read command from shell.
|
// Read command from shell.
|
||||||
memset(command, 0, MAX_COMMAND_SIZE);
|
memset(command, 0, MAX_COMMAND_SIZE);
|
||||||
set_terminal_mode();
|
setTerminalMode();
|
||||||
err = shellReadCommand(con, command);
|
err = shellReadCommand(con, command);
|
||||||
if (err) {
|
if (err) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
reset_terminal_mode();
|
resetTerminalMode();
|
||||||
} while (shellRunCommand(con, command) == 0);
|
} while (shellRunCommand(con, command) == 0);
|
||||||
|
|
||||||
tfree(command);
|
tfree(command);
|
||||||
|
@ -395,56 +395,6 @@ void *shellLoopQuery(void *arg) {
|
||||||
return NULL;
|
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 get_history_path(char *history) { sprintf(history, "%s/%s", getpwuid(getuid())->pw_dir, HISTORY_FILE); }
|
||||||
|
|
||||||
void clearScreen(int ecmd_pos, int cursor_pos) {
|
void clearScreen(int ecmd_pos, int cursor_pos) {
|
||||||
|
@ -541,9 +491,9 @@ void showOnScreen(Command *cmd) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup_handler(void *arg) { tcsetattr(0, TCSANOW, &oldtio); }
|
void cleanup_handler(void *arg) { resetTerminalMode(); }
|
||||||
|
|
||||||
void exitShell() {
|
void exitShell() {
|
||||||
tcsetattr(0, TCSANOW, &oldtio);
|
resetTerminalMode();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,14 +39,14 @@ static int shellGetFilesNum(const char *directoryName, const char *prefix)
|
||||||
char cmd[1024] = { 0 };
|
char cmd[1024] = { 0 };
|
||||||
sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix);
|
sprintf(cmd, "ls %s/*.%s | wc -l ", directoryName, prefix);
|
||||||
|
|
||||||
FILE *fp = popen(cmd, "r");
|
char buf[1024] = { 0 };
|
||||||
if (fp == NULL) {
|
if (taosSystem(cmd, buf, sizeof(buf)) < 0) {
|
||||||
fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
|
fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fileNum = 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);
|
fprintf(stderr, "ERROR: failed to execute:%s, parse result error\n", cmd);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,6 @@ static int shellGetFilesNum(const char *directoryName, const char *prefix)
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pclose(fp);
|
|
||||||
return fileNum;
|
return fileNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,14 +64,14 @@ static void shellParseDirectory(const char *directoryName, const char *prefix, c
|
||||||
char cmd[1024] = { 0 };
|
char cmd[1024] = { 0 };
|
||||||
sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix);
|
sprintf(cmd, "ls %s/*.%s | sort", directoryName, prefix);
|
||||||
|
|
||||||
FILE *fp = popen(cmd, "r");
|
char buf[1024] = { 0 };
|
||||||
if (fp == NULL) {
|
if (taosSystem(cmd, buf, sizeof(buf)) < 0) {
|
||||||
fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
|
fprintf(stderr, "ERROR: failed to execute:%s, error:%s\n", cmd, strerror(errno));
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fileNum = 0;
|
int fileNum = 0;
|
||||||
while (fscanf(fp, "%128s", fileArray[fileNum++])) {
|
while (sscanf(buf, "%128s", fileArray[fileNum++])) {
|
||||||
if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) {
|
if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) {
|
||||||
fileNum--;
|
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);
|
fprintf(stderr, "ERROR: directory:%s changed while read\n", directoryName);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pclose(fp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void shellCheckTablesSQLFile(const char *directoryName)
|
static void shellCheckTablesSQLFile(const char *directoryName)
|
||||||
|
|
|
@ -388,7 +388,7 @@ int32_t shellReadCommand(TAOS *con, char *command) {
|
||||||
|
|
||||||
void *shellLoopQuery(void *arg) {
|
void *shellLoopQuery(void *arg) {
|
||||||
if (indicator) {
|
if (indicator) {
|
||||||
get_old_terminal_mode(&oldtio);
|
getOldTerminalMode();
|
||||||
indicator = 0;
|
indicator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,12 +409,12 @@ void *shellLoopQuery(void *arg) {
|
||||||
do {
|
do {
|
||||||
// Read command from shell.
|
// Read command from shell.
|
||||||
memset(command, 0, MAX_COMMAND_SIZE);
|
memset(command, 0, MAX_COMMAND_SIZE);
|
||||||
set_terminal_mode();
|
setTerminalMode();
|
||||||
err = shellReadCommand(con, command);
|
err = shellReadCommand(con, command);
|
||||||
if (err) {
|
if (err) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
reset_terminal_mode();
|
resetTerminalMode();
|
||||||
} while (shellRunCommand(con, command) == 0);
|
} while (shellRunCommand(con, command) == 0);
|
||||||
|
|
||||||
tfree(command);
|
tfree(command);
|
||||||
|
@ -425,56 +425,6 @@ void *shellLoopQuery(void *arg) {
|
||||||
return NULL;
|
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 get_history_path(char *_history) { snprintf(_history, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), HISTORY_FILE); }
|
||||||
|
|
||||||
void clearScreen(int ecmd_pos, int cursor_pos) {
|
void clearScreen(int ecmd_pos, int cursor_pos) {
|
||||||
|
@ -571,10 +521,10 @@ void showOnScreen(Command *cmd) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup_handler(void *arg) { tcsetattr(0, TCSANOW, &oldtio); }
|
void cleanup_handler(void *arg) { resetTerminalMode(); }
|
||||||
|
|
||||||
void exitShell() {
|
void exitShell() {
|
||||||
/*int32_t ret =*/ tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
|
/*int32_t ret =*/ resetTerminalMode();
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,11 +41,11 @@ void *cancelHandler(void *arg) {
|
||||||
taosReleaseRef(tscObjRef, rid);
|
taosReleaseRef(tscObjRef, rid);
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
reset_terminal_mode();
|
resetTerminalMode();
|
||||||
printf("\nReceive ctrl+c or other signal, quit shell.\n");
|
printf("\nReceive ctrl+c or other signal, quit shell.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
#endif
|
#endif
|
||||||
reset_terminal_mode();
|
resetTerminalMode();
|
||||||
printf("\nReceive ctrl+c or other signal, quit shell.\n");
|
printf("\nReceive ctrl+c or other signal, quit shell.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue