shellUtil.c 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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/>.
 */

#define _BSD_SOURCE
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE

#include "shellInt.h"

bool shellRegexMatch(const char *s, const char *reg, int32_t cflags) {
24
  regex_t regex = {0};
25 26 27 28 29
  char    msgbuf[100] = {0};

  /* Compile regular expression */
  if (regcomp(&regex, reg, cflags) != 0) {
    fprintf(stderr, "Fail to compile regex");
30
    shellExit();
31 32 33 34 35 36 37 38 39 40 41 42 43 44
  }

  /* Execute regular expression */
  int32_t reti = regexec(&regex, s, 0, NULL, 0);
  if (!reti) {
    regfree(&regex);
    return true;
  } else if (reti == REG_NOMATCH) {
    regfree(&regex);
    return false;
  } else {
    regerror(reti, &regex, msgbuf, sizeof(msgbuf));
    fprintf(stderr, "Regex match failed: %s\n", msgbuf);
    regfree(&regex);
45
    shellExit();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  }

  return false;
}

int32_t shellCheckIntSize() {
  if (sizeof(int8_t) != 1) {
    printf("taos int8 size is %d(!= 1)", (int)sizeof(int8_t));
    return -1;
  }
  if (sizeof(int16_t) != 2) {
    printf("taos int16 size is %d(!= 2)", (int)sizeof(int16_t));
    return -1;
  }
  if (sizeof(int32_t) != 4) {
    printf("taos int32 size is %d(!= 4)", (int)sizeof(int32_t));
    return -1;
  }
  if (sizeof(int64_t) != 8) {
    printf("taos int64 size is %d(!= 8)", (int)sizeof(int64_t));
    return -1;
  }
  return 0;
}

void shellPrintVersion() { printf("version: %s\n", version); }

73 74 75 76 77 78
void shellGenerateAuth() {
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
  taosEncryptPass_c((uint8_t *)shell.args.password, strlen(shell.args.password), secretEncrypt);
  printf("%s\n", secretEncrypt);
  fflush(stdout);
}
79 80 81 82 83 84

void shellDumpConfig() {
  SConfig *pCfg = taosGetCfg();
  if (pCfg == NULL) {
    printf("TDengine read global config failed!\n");
  } else {
85
    cfgDumpCfg(pCfg, 1, true);
86
  }
87
  fflush(stdout);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

void shellCheckServerStatus() {
  TSDB_SERVER_STATUS code;

  do {
    char details[1024] = {0};
    code = taos_check_server_status(shell.args.host, shell.args.port, details, 1024);
    switch (code) {
      case TSDB_SRV_STATUS_UNAVAILABLE:
        printf("0: unavailable\n");
        break;
      case TSDB_SRV_STATUS_NETWORK_OK:
        printf("1: network ok\n");
        break;
      case TSDB_SRV_STATUS_SERVICE_OK:
        printf("2: service ok\n");
        break;
      case TSDB_SRV_STATUS_SERVICE_DEGRADED:
        printf("3: service degraded\n");
        break;
      case TSDB_SRV_STATUS_EXTING:
        printf("4: exiting\n");
        break;
    }
    if (strlen(details) != 0) {
      printf("%s\n\n", details);
    }
116
    fflush(stdout);
117 118 119 120 121 122 123
    if (code == TSDB_SRV_STATUS_NETWORK_OK && shell.args.is_startup) {
      taosMsleep(1000);
    } else {
      break;
    }
  } while (1);
}
124

125 126 127 128 129 130 131 132
void shellExit() {
  if (shell.conn != NULL) {
    taos_close(shell.conn);
    shell.conn = NULL;
  }
  taos_cleanup();
  exit(EXIT_FAILURE);
}