osSystem.c 4.9 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
Shengliang Guan 已提交
17 18 19
#define _DEFAULT_SOURCE
#include "os.h"

20
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
21 22 23 24 25 26
#elif defined(_TD_DARWIN_64)
#else
#include <dlfcn.h>
#include <termios.h>
#include <unistd.h>
#endif
S
Shengliang Guan 已提交
27

28
#if !defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
29
struct termios oldtio;
30
#endif
S
Shengliang Guan 已提交
31

wafwerar's avatar
wafwerar 已提交
32 33
typedef struct FILE TdCmd;

S
Shengliang Guan 已提交
34
void* taosLoadDll(const char* filename) {
35
#if defined(WINDOWS)
36
  assert(0);
wafwerar's avatar
wafwerar 已提交
37 38 39 40
  return NULL;
#elif defined(_TD_DARWIN_64)
  return NULL;
#else
S
Shengliang Guan 已提交
41 42
  void* handle = dlopen(filename, RTLD_LAZY);
  if (!handle) {
L
Liu Jicong 已提交
43
    // printf("load dll:%s failed, error:%s", filename, dlerror());
S
Shengliang Guan 已提交
44 45 46
    return NULL;
  }

L
Liu Jicong 已提交
47
  // printf("dll %s loaded", filename);
S
Shengliang Guan 已提交
48 49

  return handle;
wafwerar's avatar
wafwerar 已提交
50
#endif
S
Shengliang Guan 已提交
51 52 53
}

void* taosLoadSym(void* handle, char* name) {
54
#if defined(WINDOWS)
55
  assert(0);
wafwerar's avatar
wafwerar 已提交
56 57 58 59
  return NULL;
#elif defined(_TD_DARWIN_64)
  return NULL;
#else
S
Shengliang Guan 已提交
60 61 62 63
  void* sym = dlsym(handle, name);
  char* error = NULL;

  if ((error = dlerror()) != NULL) {
L
Liu Jicong 已提交
64
    // printf("load sym:%s failed, error:%s", name, dlerror());
S
Shengliang Guan 已提交
65 66 67
    return NULL;
  }

L
Liu Jicong 已提交
68
  // printf("sym %s loaded", name);
S
Shengliang Guan 已提交
69 70

  return sym;
wafwerar's avatar
wafwerar 已提交
71
#endif
S
Shengliang Guan 已提交
72 73
}

L
Liu Jicong 已提交
74
void taosCloseDll(void* handle) {
75
#if defined(WINDOWS)
76
  assert(0);
wafwerar's avatar
wafwerar 已提交
77 78 79 80
  return;
#elif defined(_TD_DARWIN_64)
  return;
#else
S
Shengliang Guan 已提交
81 82 83
  if (handle) {
    dlclose(handle);
  }
wafwerar's avatar
wafwerar 已提交
84
#endif
S
Shengliang Guan 已提交
85 86 87
}

int taosSetConsoleEcho(bool on) {
88
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
89 90 91 92 93 94 95 96 97 98 99 100
  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
S
Shengliang Guan 已提交
101 102 103 104 105
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
  int            err;
  struct termios term;

  if (tcgetattr(STDIN_FILENO, &term) == -1) {
L
Liu Jicong 已提交
106
    /*perror("Cannot get the attribution of the terminal");*/
S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114 115 116
    return -1;
  }

  if (on)
    term.c_lflag |= ECHOFLAGS;
  else
    term.c_lflag &= ~ECHOFLAGS;

  err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
  if (err == -1 || err == EINTR) {
L
Liu Jicong 已提交
117
    /*printf("Cannot set the attribution of the terminal");*/
S
Shengliang Guan 已提交
118 119 120 121
    return -1;
  }

  return 0;
wafwerar's avatar
wafwerar 已提交
122 123 124
#endif
}

125
void taosSetTerminalMode() {
126
#if defined(WINDOWS)
127
  // assert(0);
wafwerar's avatar
wafwerar 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

#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
}

159
int32_t taosGetOldTerminalMode() {
160
#if defined(WINDOWS)
161
  // assert(0);
wafwerar's avatar
wafwerar 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
#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
S
Shengliang Guan 已提交
175 176
}

177
void taosResetTerminalMode() {
178
#if defined(WINDOWS)
179
  // assert(0);
wafwerar's avatar
wafwerar 已提交
180 181 182 183 184
#else
  if (tcsetattr(0, TCSANOW, &oldtio) != 0) {
    fprintf(stderr, "Fail to reset the terminal properties!\n");
    exit(EXIT_FAILURE);
  }
S
Shengliang Guan 已提交
185
#endif
186
}
wafwerar's avatar
wafwerar 已提交
187

L
Liu Jicong 已提交
188
TdCmdPtr taosOpenCmd(const char* cmd) {
wafwerar's avatar
wafwerar 已提交
189
  if (cmd == NULL) return NULL;
wafwerar's avatar
wafwerar 已提交
190 191 192
#ifdef WINDOWS
  return (TdCmdPtr)_popen(cmd, "r");
#else
wafwerar's avatar
wafwerar 已提交
193
  return (TdCmdPtr)popen(cmd, "r");
wafwerar's avatar
wafwerar 已提交
194
#endif
wafwerar's avatar
wafwerar 已提交
195 196
}

L
Liu Jicong 已提交
197 198
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
  if (pCmd == NULL || ptrBuf == NULL) {
wafwerar's avatar
wafwerar 已提交
199 200
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213
  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
wafwerar's avatar
wafwerar 已提交
214 215
  size_t len = 0;
  return getline(ptrBuf, &len, (FILE*)pCmd);
wafwerar's avatar
wafwerar 已提交
216
#endif
wafwerar's avatar
wafwerar 已提交
217 218 219 220 221 222 223 224 225
}

int32_t taosEOFCmd(TdCmdPtr pCmd) {
  if (pCmd == NULL) {
    return 0;
  }
  return feof((FILE*)pCmd);
}

L
Liu Jicong 已提交
226
int64_t taosCloseCmd(TdCmdPtr* ppCmd) {
wafwerar's avatar
wafwerar 已提交
227 228 229
  if (ppCmd == NULL || *ppCmd == NULL) {
    return 0;
  }
wafwerar's avatar
wafwerar 已提交
230 231 232
#ifdef WINDOWS
  _pclose((FILE*)(*ppCmd));
#else
wafwerar's avatar
wafwerar 已提交
233
  pclose((FILE*)(*ppCmd));
wafwerar's avatar
wafwerar 已提交
234
#endif
wafwerar's avatar
wafwerar 已提交
235 236 237
  *ppCmd = NULL;
  return 0;
}