osSystem.c 4.3 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) {
S
Shengliang Guan 已提交
42
    //printf("load dll:%s failed, error:%s", filename, dlerror());
S
Shengliang Guan 已提交
43 44 45
    return NULL;
  }

S
Shengliang Guan 已提交
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) {
S
Shengliang Guan 已提交
62
    //printf("load sym:%s failed, error:%s", name, dlerror());
S
Shengliang Guan 已提交
63 64 65
    return NULL;
  }

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

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

wafwerar's avatar
wafwerar 已提交
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 103 104 105 106 107 108 109 110 111 112 113
#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) {
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 122
#endif
}

void setTerminalMode() {
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 155

#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() {
156
#if defined(WINDOWS)
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
}

wafwerar's avatar
wafwerar 已提交
173
void resetTerminalMode() {
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

TdCmdPtr taosOpenCmd(const char *cmd) {
  if (cmd == NULL) return NULL;
  return (TdCmdPtr)popen(cmd, "r");
}

int64_t taosGetLineCmd(TdCmdPtr pCmd, char ** __restrict ptrBuf) {
  if (pCmd == NULL) {
    return -1;
  }

  size_t len = 0;
  return getline(ptrBuf, &len, (FILE*)pCmd);
}

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

int64_t taosCloseCmd(TdCmdPtr *ppCmd) {
  if (ppCmd == NULL || *ppCmd == NULL) {
    return 0;
  }
  pclose((FILE*)(*ppCmd));
  *ppCmd = NULL;
  return 0;
}