shellArguments.c 13.6 KB
Newer Older
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 17 18 19
#ifdef _TD_DARWIN_64
#include <pwd.h>
#endif

20 21
#include "shellInt.h"

sangshuduo's avatar
sangshuduo 已提交
22
#ifndef CUS_NAME
sangshuduo's avatar
sangshuduo 已提交
23
    char cusName[] = "TDengine";
sangshuduo's avatar
sangshuduo 已提交
24
#else
sangshuduo's avatar
sangshuduo 已提交
25
    char cusName[] = CUS_NAME;
sangshuduo's avatar
sangshuduo 已提交
26 27
#endif

sangshuduo's avatar
sangshuduo 已提交
28 29
#ifndef CUS_PROMPT
    char cusPrompt[] = "taos";
sangshuduo's avatar
sangshuduo 已提交
30
#else
sangshuduo's avatar
sangshuduo 已提交
31
    char cusPrompt[] = CUS_PROMPT;
sangshuduo's avatar
sangshuduo 已提交
32 33
#endif

34 35
#define TAOS_CONSOLE_PROMPT_CONTINUE "   -> "

sangshuduo's avatar
sangshuduo 已提交
36
#define SHELL_HOST     "The server FQDN to connect. The default host is localhost."
37 38 39 40 41 42 43 44 45 46 47 48 49
#define SHELL_PORT     "The TCP/IP port number to use for the connection."
#define SHELL_USER     "The user name to use when connecting to the server."
#define SHELL_PASSWORD "The password to use when connecting to the server."
#define SHELL_AUTH     "The auth string to use when connecting to the server."
#define SHELL_GEN_AUTH "Generate auth string from password."
#define SHELL_CFG_DIR  "Configuration directory."
#define SHELL_DMP_CFG  "Dump configuration."
#define SHELL_CMD      "Commands to run without enter the shell."
#define SHELL_RAW_TIME "Output time as uint64_t."
#define SHELL_FILE     "Script to run without enter the shell."
#define SHELL_DB       "Database to use when connecting to the server."
#define SHELL_CHECK    "Check the service status."
#define SHELL_STARTUP  "Check the details of the service status."
50
#define SHELL_WIDTH    "Set the default binary display width, default is 30."
51
#define SHELL_NET_ROLE "Net role when network connectivity test, options: client|server."
wafwerar's avatar
wafwerar 已提交
52
#define SHELL_PKT_LEN  "Packet length used for net test, default is 1024 bytes."
53 54 55
#define SHELL_PKT_NUM  "Packet numbers used for net test, default is 100."
#define SHELL_VERSION  "Print program version."
#define SHELL_EMAIL    "<support@taosdata.com>"
56

Y
Yang Zhao 已提交
57 58 59 60 61 62
#ifdef WEBSOCKET
#define SHELL_DSN      "The dsn to use when connecting to cloud server."
#define SHELL_REST     "Use restful mode when connecting."
#define SHELL_TIMEOUT  "Set the timeout for websocket query in seconds, default is 10."
#endif

wafwerar's avatar
wafwerar 已提交
63 64
static int32_t shellParseSingleOpt(int32_t key, char *arg);

65 66
void shellPrintHelp() {
  char indent[] = "  ";
wafwerar's avatar
wafwerar 已提交
67 68 69 70 71 72 73 74 75
  printf("Usage: taos [OPTION...] \r\n\r\n");
  printf("%s%s%s%s\r\n", indent, "-a,", indent, SHELL_AUTH);
  printf("%s%s%s%s\r\n", indent, "-A,", indent, SHELL_GEN_AUTH);
  printf("%s%s%s%s\r\n", indent, "-c,", indent, SHELL_CFG_DIR);
  printf("%s%s%s%s\r\n", indent, "-C,", indent, SHELL_DMP_CFG);
  printf("%s%s%s%s\r\n", indent, "-d,", indent, SHELL_DB);
  printf("%s%s%s%s\r\n", indent, "-f,", indent, SHELL_FILE);
  printf("%s%s%s%s\r\n", indent, "-h,", indent, SHELL_HOST);
  printf("%s%s%s%s\r\n", indent, "-k,", indent, SHELL_CHECK);
wafwerar's avatar
wafwerar 已提交
76
  printf("%s%s%s%s\r\n", indent, "-l,", indent, SHELL_PKT_LEN);
wafwerar's avatar
wafwerar 已提交
77 78 79 80 81 82 83 84
  printf("%s%s%s%s\r\n", indent, "-n,", indent, SHELL_NET_ROLE);
  printf("%s%s%s%s\r\n", indent, "-N,", indent, SHELL_PKT_NUM);
  printf("%s%s%s%s\r\n", indent, "-p,", indent, SHELL_PASSWORD);
  printf("%s%s%s%s\r\n", indent, "-P,", indent, SHELL_PORT);
  printf("%s%s%s%s\r\n", indent, "-r,", indent, SHELL_RAW_TIME);
  printf("%s%s%s%s\r\n", indent, "-s,", indent, SHELL_CMD);
  printf("%s%s%s%s\r\n", indent, "-t,", indent, SHELL_STARTUP);
  printf("%s%s%s%s\r\n", indent, "-u,", indent, SHELL_USER);
Y
Yang Zhao 已提交
85 86 87 88 89
#ifdef WEBSOCKET
  printf("%s%s%s%s\r\n", indent, "-E,", indent, SHELL_DSN);
  printf("%s%s%s%s\r\n", indent, "-R,", indent, SHELL_REST);
  printf("%s%s%s%s\r\n", indent, "-T,", indent, SHELL_TIMEOUT);
#endif
wafwerar's avatar
wafwerar 已提交
90 91 92
  printf("%s%s%s%s\r\n", indent, "-w,", indent, SHELL_WIDTH);
  printf("%s%s%s%s\r\n", indent, "-V,", indent, SHELL_VERSION);
  printf("\r\n\r\nReport bugs to %s.\r\n", SHELL_EMAIL);
93 94
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#ifdef LINUX
#include <argp.h>
#include <termio.h>

const char *argp_program_version = version;
const char *argp_program_bug_address = SHELL_EMAIL;

static struct argp_option shellOptions[] = {
    {"host", 'h', "HOST", 0, SHELL_HOST},
    {"port", 'P', "PORT", 0, SHELL_PORT},
    {"user", 'u', "USER", 0, SHELL_USER},
    {0, 'p', 0, 0, SHELL_PASSWORD},
    {"auth", 'a', "AUTH", 0, SHELL_AUTH},
    {"generate-auth", 'A', 0, 0, SHELL_GEN_AUTH},
    {"config-dir", 'c', "DIR", 0, SHELL_CFG_DIR},
    {"dump-config", 'C', 0, 0, SHELL_DMP_CFG},
    {"commands", 's', "COMMANDS", 0, SHELL_CMD},
    {"raw-time", 'r', 0, 0, SHELL_RAW_TIME},
    {"file", 'f', "FILE", 0, SHELL_FILE},
    {"database", 'd', "DATABASE", 0, SHELL_DB},
    {"check", 'k', 0, 0, SHELL_CHECK},
    {"startup", 't', 0, 0, SHELL_STARTUP},
    {"display-width", 'w', "WIDTH", 0, SHELL_WIDTH},
    {"netrole", 'n', "NETROLE", 0, SHELL_NET_ROLE},
wafwerar's avatar
wafwerar 已提交
119
    {"pktlen", 'l', "PKTLEN", 0, SHELL_PKT_LEN},
Y
Yang Zhao 已提交
120 121 122
#ifdef WEBSOCKET
    {"dsn", 'E', "DSN", 0, SHELL_DSN},
    {"restful", 'R', 0, 0, SHELL_REST},
wafwerar's avatar
wafwerar 已提交
123
	  {"timeout", 'T', "SECONDS", 0, SHELL_TIMEOUT},
Y
Yang Zhao 已提交
124
#endif
125 126 127 128
    {"pktnum", 'N', "PKTNUM", 0, SHELL_PKT_NUM},
    {0},
};

wafwerar's avatar
wafwerar 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static error_t shellParseOpt(int32_t key, char *arg, struct argp_state *state) { return shellParseSingleOpt(key, arg); }

static struct argp shellArgp = {shellOptions, shellParseOpt, "", ""};

static void shellParseArgsUseArgp(int argc, char *argv[]) {
  argp_program_version = shell.info.programVersion;
  argp_parse(&shellArgp, argc, argv, 0, 0, &shell.args);
}

#endif

#ifndef ARGP_ERR_UNKNOWN
  #define ARGP_ERR_UNKNOWN E2BIG
#endif

144 145
static int32_t shellParseSingleOpt(int32_t key, char *arg) {
  SShellArgs *pArgs = &shell.args;
146 147 148

  switch (key) {
    case 'h':
149
      pArgs->host = arg;
Y
Yang Zhao 已提交
150 151 152
#ifdef WEBSOCKET
      pArgs->cloud = false;
#endif
153 154
      break;
    case 'P':
155
      pArgs->port = atoi(arg);
Y
Yang Zhao 已提交
156 157 158
#ifdef WEBSOCKET
      pArgs->cloud = false;
#endif
159
      if (pArgs->port == 0) pArgs->port = -1;
160 161
      break;
    case 'u':
162
      pArgs->user = arg;
163 164 165 166
      break;
    case 'p':
      break;
    case 'a':
167
      pArgs->auth = arg;
168 169
      break;
    case 'A':
170
      pArgs->is_gen_auth = true;
171 172
      break;
    case 'c':
Y
Yang Zhao 已提交
173 174 175
#ifdef WEBSOCKET
      pArgs->cloud = false;
#endif
176
      pArgs->cfgdir = arg;
177 178
      break;
    case 'C':
179
      pArgs->is_dump_config = true;
180 181
      break;
    case 's':
182
      pArgs->commands = arg;
183 184
      break;
    case 'r':
185
      pArgs->is_raw_time = true;
186 187
      break;
    case 'f':
188
      tstrncpy(pArgs->file, arg, sizeof(pArgs->file));
189 190
      break;
    case 'd':
191
      pArgs->database = arg;
192 193
      break;
    case 'k':
194
      pArgs->is_check = true;
195 196
      break;
    case 't':
197
      pArgs->is_startup = true;
198 199
      break;
    case 'w':
200
      pArgs->displayWidth = atoi(arg);
201 202
      break;
    case 'n':
203
      pArgs->netrole = arg;
204 205
      break;
    case 'l':
206
      pArgs->pktLen = atoi(arg);
207 208
      break;
    case 'N':
209
      pArgs->pktNum = atoi(arg);
210
      break;
Y
Yang Zhao 已提交
211 212 213 214 215 216 217 218 219 220 221 222
#ifdef WEBSOCKET
    case 'R':
      pArgs->restful = true;
      break;
    case 'E':
      pArgs->dsn = arg;
      pArgs->cloud = true;
      break;
	case 'T':
	  pArgs->timeout = atoi(arg);
	  break;
#endif
223
    case 'V':
224 225 226 227
      pArgs->is_version = true;
      break;
    case '?':
      pArgs->is_help = true;
228 229
      break;
    case 1:
230
      pArgs->abort = 1;
231 232 233 234 235 236 237
      break;
    default:
      return ARGP_ERR_UNKNOWN;
  }
  return 0;
}

238 239 240 241
int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) {
  SShellArgs *pArgs = &shell.args;

  for (int i = 1; i < argc; i++) {
wafwerar's avatar
wafwerar 已提交
242
    if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 || strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "/?") == 0) {
243 244 245 246 247 248 249
      shellParseSingleOpt('?', NULL);
      return 0;
    }

    char   *key = argv[i];
    int32_t keyLen = strlen(key);
    if (keyLen != 2) {
wafwerar's avatar
wafwerar 已提交
250
      fprintf(stderr, "invalid option %s\r\n", key);
251 252 253
      return -1;
    }
    if (key[0] != '-') {
wafwerar's avatar
wafwerar 已提交
254
      fprintf(stderr, "invalid option %s\r\n", key);
255 256 257 258
      return -1;
    }

    if (key[1] == 'h' || key[1] == 'P' || key[1] == 'u' || key[1] == 'a' || key[1] == 'c' || key[1] == 's' ||
Y
Yang Zhao 已提交
259 260 261 262 263
        key[1] == 'f' || key[1] == 'd' || key[1] == 'w' || key[1] == 'n' || key[1] == 'l' || key[1] == 'N' 
#ifdef WEBSOCKET
	   || key[1] == 'E' || key[1] == 'T'
#endif
		) {
264
      if (i + 1 >= argc) {
wafwerar's avatar
wafwerar 已提交
265
        fprintf(stderr, "option %s requires an argument\r\n", key);
266 267 268 269
        return -1;
      }
      char *val = argv[i + 1];
      if (val[0] == '-') {
wafwerar's avatar
wafwerar 已提交
270
        fprintf(stderr, "option %s requires an argument\r\n", key);
271 272 273 274
        return -1;
      }
      shellParseSingleOpt(key[1], val);
      i++;
wafwerar's avatar
wafwerar 已提交
275
    } else if (key[1] == 'p' || key[1] == 'A' || key[1] == 'C' || key[1] == 'r' || key[1] == 'k' || 
Y
Yang Zhao 已提交
276 277 278 279 280
               key[1] == 't' || key[1] == 'V' || key[1] == '?' || key[1] == 1
#ifdef WEBSOCKET
			   ||key[1] == 'R'
#endif
			   ) {
281 282
      shellParseSingleOpt(key[1], NULL);
    } else {
wafwerar's avatar
wafwerar 已提交
283
      fprintf(stderr, "invalid option %s\r\n", key);
284 285 286 287 288 289 290
      return -1;
    }
  }

  return 0;
}

291 292
static void shellInitArgs(int argc, char *argv[]) {
  for (int i = 1; i < argc; i++) {
293
    if (strncmp(argv[i], "-p", 2) == 0) {
wafwerar's avatar
wafwerar 已提交
294
      // printf(shell.info.clientVersion, taos_get_client_info());
295
      if (strlen(argv[i]) == 2) {
296 297 298 299 300 301 302
        printf("Enter password: ");
        taosSetConsoleEcho(false);
        if (scanf("%20s", shell.args.password) > 1) {
          fprintf(stderr, "password reading error\n");
        }
        taosSetConsoleEcho(true);
        if (EOF == getchar()) {
wafwerar's avatar
wafwerar 已提交
303
          fprintf(stderr, "getchar() return EOF\r\n");
304 305 306 307 308 309 310 311 312 313 314
        }
      } else {
        tstrncpy(shell.args.password, (char *)(argv[i] + 2), sizeof(shell.args.password));
        strcpy(argv[i], "-p");
      }
    }
  }
  if (strlen(shell.args.password) == 0) {
    tstrncpy(shell.args.password, TSDB_DEFAULT_PASS, sizeof(shell.args.password));
  }

315 316 317 318 319
  SShellArgs *pArgs = &shell.args;
  pArgs->user = TSDB_DEFAULT_USER;
  pArgs->pktLen = SHELL_DEF_PKG_LEN;
  pArgs->pktNum = SHELL_DEF_PKG_NUM;
  pArgs->displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
320 321
}

322 323 324
static int32_t shellCheckArgs() {
  SShellArgs *pArgs = &shell.args;
  if (pArgs->host != NULL && (strlen(pArgs->host) <= 0 || strlen(pArgs->host) > TSDB_FQDN_LEN)) {
wafwerar's avatar
wafwerar 已提交
325
    printf("Invalid host:%s\r\n", pArgs->host);
326 327 328 329
    return -1;
  }

  if (pArgs->user != NULL && (strlen(pArgs->user) <= 0 || strlen(pArgs->user) > TSDB_USER_LEN)) {
wafwerar's avatar
wafwerar 已提交
330
    printf("Invalid user:%s\r\n", pArgs->user);
331 332 333 334
    return -1;
  }

  if (pArgs->auth != NULL && (strlen(pArgs->auth) <= 0 || strlen(pArgs->auth) > TSDB_PASSWORD_LEN)) {
wafwerar's avatar
wafwerar 已提交
335
    printf("Invalid auth:%s\r\n", pArgs->auth);
336 337 338 339
    return -1;
  }

  if (pArgs->database != NULL && (strlen(pArgs->database) <= 0 || strlen(pArgs->database) > TSDB_DB_NAME_LEN)) {
wafwerar's avatar
wafwerar 已提交
340
    printf("Invalid database:%s\r\n", pArgs->database);
341 342 343
    return -1;
  }

344 345 346 347 348
  if (pArgs->file[0] != 0) {
    char fullname[PATH_MAX] = {0};
    if (taosExpandDir(pArgs->file, fullname, PATH_MAX) == 0) {
      tstrncpy(pArgs->file, fullname, PATH_MAX);
    }
349 350 351 352
  }

  if (pArgs->cfgdir != NULL) {
    if (strlen(pArgs->cfgdir) <= 0 || strlen(pArgs->cfgdir) >= PATH_MAX) {
wafwerar's avatar
wafwerar 已提交
353
      printf("Invalid cfgdir:%s\r\n", pArgs->cfgdir);
354 355
      return -1;
    } else {
356 357 358
      if (taosExpandDir(pArgs->cfgdir, configDir, PATH_MAX) != 0) {
        tstrncpy(configDir, pArgs->cfgdir, PATH_MAX);
      }
359 360 361 362
    }
  }

  if (pArgs->commands != NULL && (strlen(pArgs->commands) <= 0)) {
wafwerar's avatar
wafwerar 已提交
363
    printf("Invalid commands:%s\r\n", pArgs->commands);
364 365 366 367
    return -1;
  }

  if (pArgs->netrole != NULL && !(strcmp(pArgs->netrole, "client") == 0 || strcmp(pArgs->netrole, "server") == 0)) {
wafwerar's avatar
wafwerar 已提交
368
    printf("Invalid netrole:%s\r\n", pArgs->netrole);
369 370 371
    return -1;
  }

S
Shengliang Guan 已提交
372
  if (/*pArgs->password != NULL && */ (strlen(pArgs->password) <= 0)) {
wafwerar's avatar
wafwerar 已提交
373
    printf("Invalid password\r\n");
374 375 376
    return -1;
  }

377
  if (pArgs->port < 0 || pArgs->port > 65535) {
wafwerar's avatar
wafwerar 已提交
378
    printf("Invalid port\r\n");
379 380 381
    return -1;
  }

382
  if (pArgs->pktLen < SHELL_MIN_PKG_LEN || pArgs->pktLen > SHELL_MAX_PKG_LEN) {
wafwerar's avatar
wafwerar 已提交
383
    printf("Invalid pktLen:%d, range:[%d, %d]\r\n", pArgs->pktLen, SHELL_MIN_PKG_LEN, SHELL_MAX_PKG_LEN);
384 385 386
    return -1;
  }

387
  if (pArgs->pktNum < SHELL_MIN_PKG_NUM || pArgs->pktNum > SHELL_MAX_PKG_NUM) {
wafwerar's avatar
wafwerar 已提交
388
    printf("Invalid pktNum:%d, range:[%d, %d]\r\n", pArgs->pktNum, SHELL_MIN_PKG_NUM, SHELL_MAX_PKG_NUM);
389 390 391 392
    return -1;
  }

  if (pArgs->displayWidth <= 0 || pArgs->displayWidth > 10 * 1024) {
wafwerar's avatar
wafwerar 已提交
393
    printf("Invalid displayWidth:%d, range:[1, 10 * 1024]\r\n", pArgs->displayWidth);
394 395 396 397 398
    return -1;
  }

  return 0;
}
399 400 401

int32_t shellParseArgs(int32_t argc, char *argv[]) {
  shellInitArgs(argc, argv);
sangshuduo's avatar
sangshuduo 已提交
402 403 404
  shell.info.clientVersion = 
      "Welcome to the %s Command Line Interface, Client Version:%s\r\n"
      "Copyright (c) 2022 by %s, all rights reserved.\r\n\r\n";
sangshuduo's avatar
sangshuduo 已提交
405 406
  strcpy(shell.info.cusName, cusName);
  sprintf(shell.info.promptHeader, "%s> ", cusPrompt);
wafwerar's avatar
wafwerar 已提交
407
  shell.info.promptContinue = TAOS_CONSOLE_PROMPT_CONTINUE;
sangshuduo's avatar
sangshuduo 已提交
408
  shell.info.promptSize = strlen(shell.info.promptHeader);
409
  snprintf(shell.info.programVersion, sizeof(shell.info.programVersion), "version: %s", version);
410 411 412 413

#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  shell.info.osname = "Windows";
  snprintf(shell.history.file, TSDB_FILENAME_LEN, "C:/TDengine/%s", SHELL_HISTORY_FILE);
wafwerar's avatar
wafwerar 已提交
414
  if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
415 416 417
#elif defined(_TD_DARWIN_64)
  shell.info.osname = "Darwin";
  snprintf(shell.history.file, TSDB_FILENAME_LEN, "%s/%s", getpwuid(getuid())->pw_dir, SHELL_HISTORY_FILE);
418
  if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
419 420 421
#else
  shell.info.osname = "Linux";
  snprintf(shell.history.file, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), SHELL_HISTORY_FILE);
422
  shellParseArgsUseArgp(argc, argv);
423
  // if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
424 425 426
  if (shell.args.abort) {
    return -1;
  }
427
#endif
428 429 430

  return shellCheckArgs();
}