shellArguments.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "shellInt.h"

18 19 20 21 22 23 24 25 26 27 28 29 30 31
#define SHELL_HOST     "The auth string to use when connecting to the server."
#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."
32
#define SHELL_WIDTH    "Set the default binary display width, default is 30."
33
#define SHELL_NET_ROLE "Net role when network connectivity test, options: client|server."
34
#define SHELL_PKG_LEN  "Packet length used for net test, default is 1024 bytes."
35 36 37
#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>"
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void shellPrintHelp() {
  char indent[] = "  ";
  printf("Usage: taos [OPTION...] \n\n");
  printf("%s%s%s%s\n", indent, "-a,", indent, SHELL_AUTH);
  printf("%s%s%s%s\n", indent, "-A,", indent, SHELL_GEN_AUTH);
  printf("%s%s%s%s\n", indent, "-c,", indent, SHELL_CFG_DIR);
  printf("%s%s%s%s\n", indent, "-C,", indent, SHELL_DMP_CFG);
  printf("%s%s%s%s\n", indent, "-d,", indent, SHELL_DB);
  printf("%s%s%s%s\n", indent, "-f,", indent, SHELL_FILE);
  printf("%s%s%s%s\n", indent, "-h,", indent, SHELL_HOST);
  printf("%s%s%s%s\n", indent, "-k,", indent, SHELL_CHECK);
  printf("%s%s%s%s\n", indent, "-l,", indent, SHELL_PKG_LEN);
  printf("%s%s%s%s\n", indent, "-n,", indent, SHELL_NET_ROLE);
  printf("%s%s%s%s\n", indent, "-N,", indent, SHELL_PKT_NUM);
  printf("%s%s%s%s\n", indent, "-p,", indent, SHELL_PASSWORD);
  printf("%s%s%s%s\n", indent, "-P,", indent, SHELL_PORT);
  printf("%s%s%s%s\n", indent, "-r,", indent, SHELL_RAW_TIME);
  printf("%s%s%s%s\n", indent, "-s,", indent, SHELL_CMD);
  printf("%s%s%s%s\n", indent, "-t,", indent, SHELL_STARTUP);
  printf("%s%s%s%s\n", indent, "-u,", indent, SHELL_USER);
  printf("%s%s%s%s\n", indent, "-w,", indent, SHELL_WIDTH);
  printf("%s%s%s%s\n", indent, "-V,", indent, SHELL_VERSION);
  printf("\n\nReport bugs to %s.\n", SHELL_EMAIL);
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#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},
    {"pktlen", 'l', "PKTLEN", 0, SHELL_PKG_LEN},
    {"pktnum", 'N', "PKTNUM", 0, SHELL_PKT_NUM},
    {0},
};

93 94
static int32_t shellParseSingleOpt(int32_t key, char *arg) {
  SShellArgs *pArgs = &shell.args;
95 96 97

  switch (key) {
    case 'h':
98
      pArgs->host = arg;
99 100
      break;
    case 'P':
101
      pArgs->port = atoi(arg);
102
      if (pArgs->port == 0) pArgs->port = -1;
103 104
      break;
    case 'u':
105
      pArgs->user = arg;
106 107 108 109
      break;
    case 'p':
      break;
    case 'a':
110
      pArgs->auth = arg;
111 112
      break;
    case 'A':
113
      pArgs->is_gen_auth = true;
114 115
      break;
    case 'c':
116
      pArgs->cfgdir = arg;
117 118
      break;
    case 'C':
119
      pArgs->is_dump_config = true;
120 121
      break;
    case 's':
122
      pArgs->commands = arg;
123 124
      break;
    case 'r':
125
      pArgs->is_raw_time = true;
126 127
      break;
    case 'f':
128
      tstrncpy(pArgs->file, arg, sizeof(pArgs->file));
129 130
      break;
    case 'd':
131
      pArgs->database = arg;
132 133
      break;
    case 'k':
134
      pArgs->is_check = true;
135 136
      break;
    case 't':
137
      pArgs->is_startup = true;
138 139
      break;
    case 'w':
140
      pArgs->displayWidth = atoi(arg);
141 142
      break;
    case 'n':
143
      pArgs->netrole = arg;
144 145
      break;
    case 'l':
146
      pArgs->pktLen = atoi(arg);
147 148
      break;
    case 'N':
149
      pArgs->pktNum = atoi(arg);
150 151
      break;
    case 'V':
152 153 154 155
      pArgs->is_version = true;
      break;
    case '?':
      pArgs->is_help = true;
156 157
      break;
    case 1:
158
      pArgs->abort = 1;
159 160 161 162 163 164 165
      break;
    default:
      return ARGP_ERR_UNKNOWN;
  }
  return 0;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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
int32_t shellParseArgsWithoutArgp(int argc, char *argv[]) {
  SShellArgs *pArgs = &shell.args;

  for (int i = 1; i < argc; i++) {
    if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 || strcmp(argv[i], "-?") == 0) {
      shellParseSingleOpt('?', NULL);
      return 0;
    }

    char   *key = argv[i];
    int32_t keyLen = strlen(key);
    if (keyLen != 2) {
      fprintf(stderr, "invalid option %s\n", key);
      return -1;
    }
    if (key[0] != '-') {
      fprintf(stderr, "invalid option %s\n", key);
      return -1;
    }

    if (key[1] == 'h' || key[1] == 'P' || key[1] == 'u' || key[1] == 'a' || key[1] == 'c' || key[1] == 's' ||
        key[1] == 'f' || key[1] == 'd' || key[1] == 'w' || key[1] == 'n' || key[1] == 'l' || key[1] == 'N') {
      if (i + 1 >= argc) {
        fprintf(stderr, "option %s requires an argument\n", key);
        return -1;
      }
      char *val = argv[i + 1];
      if (val[0] == '-') {
        fprintf(stderr, "option %s requires an argument\n", key);
        return -1;
      }
      shellParseSingleOpt(key[1], val);
      i++;
    } else if (key[1] == 'p' || key[1] == 'A' || key[1] == 'c' || key[1] == 'r' || key[1] == 'k' || key[1] == 't' ||
               key[1] == 'V') {
      shellParseSingleOpt(key[1], NULL);
    } else {
      fprintf(stderr, "invalid option %s\n", key);
      return -1;
    }
  }

  return 0;
}

static error_t shellParseOpt(int32_t key, char *arg, struct argp_state *state) { return shellParseSingleOpt(key, arg); }

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

215
static void shellParseArgsUseArgp(int argc, char *argv[]) {
216
  argp_program_version = shell.info.programVersion;
217
  argp_parse(&shellArgp, argc, argv, 0, 0, &shell.args);
218 219 220 221 222 223
}

#endif

static void shellInitArgs(int argc, char *argv[]) {
  for (int i = 1; i < argc; i++) {
224
    if (strncmp(argv[i], "-p", 2) == 0) {
225
      printf(shell.info.clientVersion, tsOsName, taos_get_client_info());
226
      if (strlen(argv[i]) == 2) {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        printf("Enter password: ");
        taosSetConsoleEcho(false);
        if (scanf("%20s", shell.args.password) > 1) {
          fprintf(stderr, "password reading error\n");
        }
        taosSetConsoleEcho(true);
        if (EOF == getchar()) {
          fprintf(stderr, "getchar() return EOF\n");
        }
      } 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));
  }

246 247 248 249 250
  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;
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
static int32_t shellCheckArgs() {
  SShellArgs *pArgs = &shell.args;
  if (pArgs->host != NULL && (strlen(pArgs->host) <= 0 || strlen(pArgs->host) > TSDB_FQDN_LEN)) {
    printf("Invalid host:%s\n", pArgs->host);
    return -1;
  }

  if (pArgs->user != NULL && (strlen(pArgs->user) <= 0 || strlen(pArgs->user) > TSDB_USER_LEN)) {
    printf("Invalid user:%s\n", pArgs->user);
    return -1;
  }

  if (pArgs->auth != NULL && (strlen(pArgs->auth) <= 0 || strlen(pArgs->auth) > TSDB_PASSWORD_LEN)) {
    printf("Invalid auth:%s\n", pArgs->auth);
    return -1;
  }

  if (pArgs->database != NULL && (strlen(pArgs->database) <= 0 || strlen(pArgs->database) > TSDB_DB_NAME_LEN)) {
    printf("Invalid database:%s\n", pArgs->database);
    return -1;
  }

275 276 277 278 279
  if (pArgs->file[0] != 0) {
    char fullname[PATH_MAX] = {0};
    if (taosExpandDir(pArgs->file, fullname, PATH_MAX) == 0) {
      tstrncpy(pArgs->file, fullname, PATH_MAX);
    }
280 281 282 283 284 285 286
  }

  if (pArgs->cfgdir != NULL) {
    if (strlen(pArgs->cfgdir) <= 0 || strlen(pArgs->cfgdir) >= PATH_MAX) {
      printf("Invalid cfgdir:%s\n", pArgs->cfgdir);
      return -1;
    } else {
287 288 289
      if (taosExpandDir(pArgs->cfgdir, configDir, PATH_MAX) != 0) {
        tstrncpy(configDir, pArgs->cfgdir, PATH_MAX);
      }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    }
  }

  if (pArgs->commands != NULL && (strlen(pArgs->commands) <= 0)) {
    printf("Invalid commands:%s\n", pArgs->commands);
    return -1;
  }

  if (pArgs->netrole != NULL && !(strcmp(pArgs->netrole, "client") == 0 || strcmp(pArgs->netrole, "server") == 0)) {
    printf("Invalid netrole:%s\n", pArgs->netrole);
    return -1;
  }

  if (pArgs->password != NULL && (strlen(pArgs->password) <= 0)) {
    printf("Invalid password\n");
    return -1;
  }

308 309 310 311 312
  if (pArgs->port < 0 || pArgs->port > 65535) {
    printf("Invalid port\n");
    return -1;
  }

313 314
  if (pArgs->pktLen < SHELL_MIN_PKG_LEN || pArgs->pktLen > SHELL_MAX_PKG_LEN) {
    printf("Invalid pktLen:%d, range:[%d, %d]\n", pArgs->pktLen, SHELL_MIN_PKG_LEN, SHELL_MAX_PKG_LEN);
315 316 317
    return -1;
  }

318 319
  if (pArgs->pktNum < SHELL_MIN_PKG_NUM || pArgs->pktNum > SHELL_MAX_PKG_NUM) {
    printf("Invalid pktNum:%d, range:[%d, %d]\n", pArgs->pktNum, SHELL_MIN_PKG_NUM, SHELL_MAX_PKG_NUM);
320 321 322 323 324 325 326 327 328 329
    return -1;
  }

  if (pArgs->displayWidth <= 0 || pArgs->displayWidth > 10 * 1024) {
    printf("Invalid displayWidth:%d, range:[1, 10 * 1024]\n", pArgs->displayWidth);
    return -1;
  }

  return 0;
}
330 331 332 333 334 335 336 337 338

int32_t shellParseArgs(int32_t argc, char *argv[]) {
  shellInitArgs(argc, argv);
  shell.info.clientVersion =
      "Welcome to the TDengine shell from %s, Client Version:%s\n"
      "Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.\n\n";
  shell.info.promptHeader = "taos> ";
  shell.info.promptContinue = "   -> ";
  shell.info.promptSize = 6;
339
  snprintf(shell.info.programVersion, sizeof(shell.info.programVersion), "version: %s", version);
340 341 342 343

#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);
344
  // if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
345 346 347
#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);
348
  if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
349 350 351
#else
  shell.info.osname = "Linux";
  snprintf(shell.history.file, TSDB_FILENAME_LEN, "%s/%s", getenv("HOME"), SHELL_HISTORY_FILE);
352
  shellParseArgsUseArgp(argc, argv);
353
  // if (shellParseArgsWithoutArgp(argc, argv) != 0) return -1;
354 355 356
  if (shell.args.abort) {
    return -1;
  }
357
#endif
358 359 360

  return shellCheckArgs();
}