shellUtil.c 4.0 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
  }

  /* 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));
wafwerar's avatar
wafwerar 已提交
43
    fprintf(stderr, "Regex match failed: %s\r\n", msgbuf);
44
    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
  }

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

wafwerar's avatar
wafwerar 已提交
71
void shellPrintVersion() { printf("version: %s\r\n", version); }
72

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

void shellDumpConfig() {
  SConfig *pCfg = taosGetCfg();
  if (pCfg == NULL) {
wafwerar's avatar
wafwerar 已提交
83
    printf("TDengine read global config failed!\r\n");
84
  } else {
85
    cfgDumpCfg(pCfg, 1, true);
86
  }
87
  fflush(stdout);
88 89 90 91 92 93 94 95 96 97
}

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:
wafwerar's avatar
wafwerar 已提交
98
        printf("0: unavailable\r\n");
99 100
        break;
      case TSDB_SRV_STATUS_NETWORK_OK:
wafwerar's avatar
wafwerar 已提交
101
        printf("1: network ok\r\n");
102 103
        break;
      case TSDB_SRV_STATUS_SERVICE_OK:
wafwerar's avatar
wafwerar 已提交
104
        printf("2: service ok\r\n");
105 106
        break;
      case TSDB_SRV_STATUS_SERVICE_DEGRADED:
wafwerar's avatar
wafwerar 已提交
107
        printf("3: service degraded\r\n");
108 109
        break;
      case TSDB_SRV_STATUS_EXTING:
wafwerar's avatar
wafwerar 已提交
110
        printf("4: exiting\r\n");
111 112 113
        break;
    }
    if (strlen(details) != 0) {
wafwerar's avatar
wafwerar 已提交
114
      printf("%s\r\n\r\n", details);
115
    }
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);
}
Y
Yang Zhao 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#ifdef WEBSOCKET
void shellCheckConnectMode() {
	if (shell.args.dsn) {
		shell.args.cloud = true;
		shell.args.restful = false;
		return;
	}
	if (shell.args.cloud) {
		shell.args.dsn = getenv("TDENGINE_CLOUD_DSN");
		if (shell.args.dsn) {
			shell.args.cloud = true;
			shell.args.restful = false;
			return;
		}
		if (shell.args.restful) {
			if (!shell.args.host) {
				shell.args.host = "localhost";
			}
			if (!shell.args.port) {
				shell.args.port = 6041;
			}
			shell.args.dsn = taosMemoryCalloc(1, 1024);
146
			snprintf(shell.args.dsn, 1024, "ws://%s:%d",
Y
Yang Zhao 已提交
147 148 149 150 151 152 153
					shell.args.host, shell.args.port);
		}
		shell.args.cloud = false;
		return;
	}
}
#endif
154

155 156 157 158 159
void shellExit() {
  if (shell.conn != NULL) {
    taos_close(shell.conn);
    shell.conn = NULL;
  }
160
  shell.exit = true;
161
  taos_cleanup();
Y
Yang Zhao 已提交
162
}