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

[TD-13770]<fix>: redefine system api.

上级 94bc3a6e
......@@ -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);
......
......@@ -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
}
......
......@@ -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!");
......
......@@ -13,78 +13,80 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <dlfcn.h>
#include <termios.h>
#include <unistd.h>
#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;
while (fgets(buf, bufSize, fp)) {
// printf("popen result:%s", buf);
}
SetConsoleMode(hStdin, mode);
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;
} else {
while (fgets(buf, bufSize, fp)) {
// printf("popen result:%s", buf);
}
return 0;
}
#else
/*
* linux implementation
*/
if ((res = pclose(fp)) == -1) {
// printf("close popen file pointer fp error!");
} else {
// printf("popen res is :%d", res);
}
#include <dlfcn.h>
#include <termios.h>
#include <unistd.h>
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) {
#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
......@@ -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;
......
......@@ -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);
}
......@@ -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)
......
......@@ -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);
}
......@@ -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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册