osSystem.c 4.8 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)
wafwerar's avatar
wafwerar 已提交
36 37 38 39
  return NULL;
#elif defined(_TD_DARWIN_64)
  return NULL;
#else
S
Shengliang Guan 已提交
40 41
  void* handle = dlopen(filename, RTLD_LAZY);
  if (!handle) {
L
Liu Jicong 已提交
42
    // printf("load dll:%s failed, error:%s", filename, dlerror());
S
Shengliang Guan 已提交
43 44 45
    return NULL;
  }

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

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

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

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

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

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

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

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

  if (tcgetattr(STDIN_FILENO, &term) == -1) {
L
Liu Jicong 已提交
103
    /*perror("Cannot get the attribution of the terminal");*/
S
Shengliang Guan 已提交
104 105 106 107 108 109 110 111 112 113
    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 已提交
114
    /*printf("Cannot set the attribution of the terminal");*/
S
Shengliang Guan 已提交
115 116 117 118
    return -1;
  }

  return 0;
wafwerar's avatar
wafwerar 已提交
119 120 121
#endif
}

122
void taosSetTerminalMode() {
123
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
124 125 126 127 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

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

155
int32_t taosGetOldTerminalMode() {
156
#if defined(WINDOWS)
L
Liu Jicong 已提交
157

wafwerar's avatar
wafwerar 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
#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 已提交
171 172
}

173
void taosResetTerminalMode() {
174
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
175 176 177 178 179 180

#else
  if (tcsetattr(0, TCSANOW, &oldtio) != 0) {
    fprintf(stderr, "Fail to reset the terminal properties!\n");
    exit(EXIT_FAILURE);
  }
S
Shengliang Guan 已提交
181
#endif
182
}
wafwerar's avatar
wafwerar 已提交
183

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

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

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

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