TDengine/source/os/src/osSystem.c

234 lines
4.8 KiB
C
Raw Normal View History

2021-09-30 13:21:51 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-03-10 10:06:20 +00:00
#define ALLOW_FORBID_FUNC
2021-09-30 13:21:51 +00:00
#define _DEFAULT_SOURCE
#include "os.h"
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
#elif defined(_TD_DARWIN_64)
#else
#include <dlfcn.h>
#include <termios.h>
#include <unistd.h>
#endif
2021-09-30 13:21:51 +00:00
#if !defined(WINDOWS)
2022-03-10 10:06:20 +00:00
struct termios oldtio;
#endif
2021-09-30 13:21:51 +00:00
2022-04-13 08:48:29 +00:00
typedef struct FILE TdCmd;
2021-09-30 13:21:51 +00:00
void* taosLoadDll(const char* filename) {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
return NULL;
#elif defined(_TD_DARWIN_64)
return NULL;
#else
2021-09-30 13:21:51 +00:00
void* handle = dlopen(filename, RTLD_LAZY);
if (!handle) {
2022-04-26 06:32:37 +00:00
// printf("load dll:%s failed, error:%s", filename, dlerror());
2021-09-30 13:21:51 +00:00
return NULL;
}
2022-04-26 06:32:37 +00:00
// printf("dll %s loaded", filename);
2021-09-30 13:21:51 +00:00
return handle;
2022-03-10 10:06:20 +00:00
#endif
2021-09-30 13:21:51 +00:00
}
void* taosLoadSym(void* handle, char* name) {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
return NULL;
#elif defined(_TD_DARWIN_64)
return NULL;
#else
2021-09-30 13:21:51 +00:00
void* sym = dlsym(handle, name);
char* error = NULL;
if ((error = dlerror()) != NULL) {
2022-04-26 06:32:37 +00:00
// printf("load sym:%s failed, error:%s", name, dlerror());
2021-09-30 13:21:51 +00:00
return NULL;
}
2022-04-26 06:32:37 +00:00
// printf("sym %s loaded", name);
2021-09-30 13:21:51 +00:00
return sym;
2022-03-10 10:06:20 +00:00
#endif
2021-09-30 13:21:51 +00:00
}
2022-04-26 06:32:37 +00:00
void taosCloseDll(void* handle) {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
return;
#elif defined(_TD_DARWIN_64)
return;
#else
2021-09-30 13:21:51 +00:00
if (handle) {
dlclose(handle);
}
2022-03-10 10:06:20 +00:00
#endif
2021-09-30 13:21:51 +00:00
}
int taosSetConsoleEcho(bool on) {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
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;
#else
2021-09-30 13:21:51 +00:00
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
int err;
struct termios term;
if (tcgetattr(STDIN_FILENO, &term) == -1) {
2022-04-26 06:32:37 +00:00
/*perror("Cannot get the attribution of the terminal");*/
2021-09-30 13:21:51 +00:00
return -1;
}
if (on)
term.c_lflag |= ECHOFLAGS;
else
term.c_lflag &= ~ECHOFLAGS;
err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
if (err == -1 || err == EINTR) {
2022-04-26 06:32:37 +00:00
/*printf("Cannot set the attribution of the terminal");*/
2021-09-30 13:21:51 +00:00
return -1;
}
return 0;
2022-03-10 10:06:20 +00:00
#endif
}
2022-04-21 13:09:43 +00:00
void taosSetTerminalMode() {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
#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
}
2022-04-21 13:09:43 +00:00
int32_t taosGetOldTerminalMode() {
#if defined(WINDOWS)
2022-04-26 06:32:37 +00:00
2022-03-10 10:06:20 +00:00
#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
2021-09-30 13:21:51 +00:00
}
2022-04-21 13:09:43 +00:00
void taosResetTerminalMode() {
#if defined(WINDOWS)
2022-03-10 10:06:20 +00:00
#else
if (tcsetattr(0, TCSANOW, &oldtio) != 0) {
fprintf(stderr, "Fail to reset the terminal properties!\n");
exit(EXIT_FAILURE);
}
2021-09-30 13:21:51 +00:00
#endif
}
2022-04-13 08:48:29 +00:00
2022-04-26 06:32:37 +00:00
TdCmdPtr taosOpenCmd(const char* cmd) {
2022-04-13 08:48:29 +00:00
if (cmd == NULL) return NULL;
2022-04-22 01:54:27 +00:00
#ifdef WINDOWS
return (TdCmdPtr)_popen(cmd, "r");
#else
2022-04-13 08:48:29 +00:00
return (TdCmdPtr)popen(cmd, "r");
2022-04-22 01:54:27 +00:00
#endif
2022-04-13 08:48:29 +00:00
}
2022-04-26 06:32:37 +00:00
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
if (pCmd == NULL || ptrBuf == NULL) {
2022-04-13 08:48:29 +00:00
return -1;
}
2022-04-22 01:54:27 +00:00
if (*ptrBuf != NULL) {
taosMemoryFreeClear(*ptrBuf);
}
#ifdef WINDOWS
*ptrBuf = taosMemoryMalloc(1024);
if (*ptrBuf == NULL) return -1;
if (fgets(*ptrBuf, 1023, (FILE*)pCmd) == NULL) {
taosMemoryFreeClear(*ptrBuf);
return -1;
}
(*ptrBuf)[1023] = 0;
return strlen(*ptrBuf);
#else
2022-04-13 08:48:29 +00:00
size_t len = 0;
return getline(ptrBuf, &len, (FILE*)pCmd);
2022-04-22 01:54:27 +00:00
#endif
2022-04-13 08:48:29 +00:00
}
int32_t taosEOFCmd(TdCmdPtr pCmd) {
if (pCmd == NULL) {
return 0;
}
return feof((FILE*)pCmd);
}
2022-04-26 06:32:37 +00:00
int64_t taosCloseCmd(TdCmdPtr* ppCmd) {
2022-04-13 08:48:29 +00:00
if (ppCmd == NULL || *ppCmd == NULL) {
return 0;
}
2022-04-22 01:54:27 +00:00
#ifdef WINDOWS
_pclose((FILE*)(*ppCmd));
#else
2022-04-13 08:48:29 +00:00
pclose((FILE*)(*ppCmd));
2022-04-22 01:54:27 +00:00
#endif
2022-04-13 08:48:29 +00:00
*ppCmd = NULL;
return 0;
}