osSystem.c 6.7 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
int32_t taosSystem(const char *cmd, char *buf, int32_t bufSize) {
33
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
34 35 36 37 38
  FILE *fp;
  if (cmd == NULL) {
    // printf("taosSystem cmd is NULL!");
    return -1;
  }
S
Shengliang Guan 已提交
39

wafwerar's avatar
wafwerar 已提交
40 41 42
  if ((fp = _popen(cmd, "r")) == NULL) {
    // printf("popen cmd:%s error: %s", cmd, strerror(errno));
    return -1;
S
Shengliang Guan 已提交
43
  } else {
wafwerar's avatar
wafwerar 已提交
44 45 46
    while (fgets(buf, bufSize, fp)) {
      // printf("popen result:%s", buf);
    }
S
Shengliang Guan 已提交
47

wafwerar's avatar
wafwerar 已提交
48 49 50 51 52 53
    if (!_pclose(fp)) {
      // printf("close popen file pointer fp error!");
      return -1;
    } else {
      // printf("popen res is :%d", res);
    }
S
Shengliang Guan 已提交
54

wafwerar's avatar
wafwerar 已提交
55
    return 0;
56
  }
S
Shengliang Guan 已提交
57
#elif defined(_TD_DARWIN_64)
wafwerar's avatar
wafwerar 已提交
58 59 60 61 62 63 64
  printf("no support funtion");
  return -1;
#else
  FILE *fp;
  int32_t res;
  if (cmd == NULL) {
    // printf("taosSystem cmd is NULL!");
S
Shengliang Guan 已提交
65 66 67
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
68 69
  if ((fp = popen(cmd, "r")) == NULL) {
    // printf("popen cmd:%s error: %s", cmd, strerror(errno));
S
Shengliang Guan 已提交
70
    return -1;
wafwerar's avatar
wafwerar 已提交
71 72 73 74
  } else {
    while (fgets(buf, bufSize, fp)) {
      // printf("popen result:%s", buf);
    }
S
Shengliang Guan 已提交
75

wafwerar's avatar
wafwerar 已提交
76 77 78 79 80
    if ((res = pclose(fp)) == -1) {
      // printf("close popen file pointer fp error!");
    } else {
      // printf("popen res is :%d", res);
    }
S
Shengliang Guan 已提交
81

wafwerar's avatar
wafwerar 已提交
82 83 84 85
    return res;
  }
#endif
}
S
Shengliang Guan 已提交
86 87

void* taosLoadDll(const char* filename) {
88
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
89 90 91 92
  return NULL;
#elif defined(_TD_DARWIN_64)
  return NULL;
#else
S
Shengliang Guan 已提交
93 94
  void* handle = dlopen(filename, RTLD_LAZY);
  if (!handle) {
S
Shengliang Guan 已提交
95
    //printf("load dll:%s failed, error:%s", filename, dlerror());
S
Shengliang Guan 已提交
96 97 98
    return NULL;
  }

S
Shengliang Guan 已提交
99
  //printf("dll %s loaded", filename);
S
Shengliang Guan 已提交
100 101

  return handle;
wafwerar's avatar
wafwerar 已提交
102
#endif
S
Shengliang Guan 已提交
103 104 105
}

void* taosLoadSym(void* handle, char* name) {
wafwerar's avatar
wafwerar 已提交
106 107 108 109 110
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return NULL;
#elif defined(_TD_DARWIN_64)
  return NULL;
#else
S
Shengliang Guan 已提交
111 112 113 114
  void* sym = dlsym(handle, name);
  char* error = NULL;

  if ((error = dlerror()) != NULL) {
S
Shengliang Guan 已提交
115
    //printf("load sym:%s failed, error:%s", name, dlerror());
S
Shengliang Guan 已提交
116 117 118
    return NULL;
  }

S
Shengliang Guan 已提交
119
  //printf("sym %s loaded", name);
S
Shengliang Guan 已提交
120 121

  return sym;
wafwerar's avatar
wafwerar 已提交
122
#endif
S
Shengliang Guan 已提交
123 124
}

wafwerar's avatar
wafwerar 已提交
125 126 127 128 129 130
void  taosCloseDll(void* handle) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return;
#elif defined(_TD_DARWIN_64)
  return;
#else
S
Shengliang Guan 已提交
131 132 133
  if (handle) {
    dlclose(handle);
  }
wafwerar's avatar
wafwerar 已提交
134
#endif
S
Shengliang Guan 已提交
135 136 137
}

int taosSetConsoleEcho(bool on) {
wafwerar's avatar
wafwerar 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
#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
S
Shengliang Guan 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#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) {
S
Shengliang Guan 已提交
189
    //printf("Cannot set the attribution of the terminal");
S
Shengliang Guan 已提交
190 191 192 193
    return -1;
  }

  return 0;
wafwerar's avatar
wafwerar 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#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() {
258
#if defined(WINDOWS)
wafwerar's avatar
wafwerar 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

#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
S
Shengliang Guan 已提交
285 286
}

wafwerar's avatar
wafwerar 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
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);
  }
S
Shengliang Guan 已提交
300
#endif
301
}