shellEngine.c 28.4 KB
Newer Older
H
hzcheng 已提交
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
17 18
#define _BSD_SOURCE
#define _GNU_SOURCE
H
hzcheng 已提交
19
#define _XOPEN_SOURCE
S
slguan 已提交
20
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
21
#include "shellInt.h"
H
hzcheng 已提交
22

23 24 25 26 27 28 29 30 31 32
static bool    shellIsEmptyCommand(const char *cmd);
static int32_t shellRunSingleCommand(char *command);
static int32_t shellRunCommand(char *command);
static void    shellRunSingleCommandImp(char *command);
static char   *shellFormatTimestamp(char *buf, int64_t val, int32_t precision);
static void    shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length,
                                    int32_t precision);
static int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
static void    shellPrintNChar(const char *str, int32_t length, int32_t width);
static void    shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision);
S
Shengliang Guan 已提交
33
static int32_t shellVerticalPrintResult(TAOS_RES *tres, const char *sql);
34 35
static int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision);
static void    shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields);
S
Shengliang Guan 已提交
36 37
static int32_t shellHorizontalPrintResult(TAOS_RES *tres, const char *sql);
static int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql);
38 39 40 41 42 43 44 45 46 47 48 49
static void    shellReadHistory();
static void    shellWriteHistory();
static void    shellPrintError(TAOS_RES *tres, int64_t st);
static bool    shellIsCommentLine(char *line);
static void    shellSourceFile(const char *file);
static void    shellGetGrantInfo();
static void    shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context);
static void    shellCleanup(void *arg);
static void   *shellCancelHandler(void *arg);
static void   *shellThreadLoop(void *arg);

bool shellIsEmptyCommand(const char *cmd) {
50 51 52
  for (char c = *cmd++; c != 0; c = *cmd++) {
    if (c != ' ' && c != '\t' && c != ';') {
      return false;
H
hzcheng 已提交
53 54
    }
  }
55
  return true;
H
hzcheng 已提交
56 57
}

58 59
int32_t shellRunSingleCommand(char *command) {
  if (shellIsEmptyCommand(command)) {
60
    return 0;
H
hzcheng 已提交
61 62
  }

63 64
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
    shellWriteHistory();
65
    return -1;
66 67
  }

68
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
H
hzcheng 已提交
69
    system("clear");
70 71
    return 0;
  }
72

73
  if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$",
74
                      REG_EXTENDED | REG_ICASE)) {
75 76
    strtok(command, " \t");
    strtok(NULL, " \t");
S
Shengliang Guan 已提交
77
    char *p = strtok(NULL, " \t");
78
    if (strncasecmp(p, "default", 7) == 0) {
79
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
80
    } else {
81 82 83
      int32_t displayWidth = atoi(p);
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
      shell.args.displayWidth = displayWidth;
84
    }
85 86
    return 0;
  }
87

88
  if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
H
hzcheng 已提交
89 90 91 92 93
    /* If source file. */
    char *c_ptr = strtok(command, " ;");
    assert(c_ptr != NULL);
    c_ptr = strtok(NULL, " ;");
    assert(c_ptr != NULL);
94
    shellSourceFile(c_ptr);
95
    return 0;
H
hzcheng 已提交
96
  }
97

98
  shellRunSingleCommandImp(command);
99
  return 0;
H
hzcheng 已提交
100 101
}

102 103
int32_t shellRunCommand(char *command) {
  if (shellIsEmptyCommand(command)) {
104 105 106
    return 0;
  }

107 108 109 110 111 112
  SShellHistory *pHistory = &shell.history;
  if (pHistory->hstart == pHistory->hend ||
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
    if (pHistory->hist[pHistory->hend] != NULL) {
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
113
    }
114
    pHistory->hist[pHistory->hend] = strdup(command);
115

116 117 118
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
    if (pHistory->hend == pHistory->hstart) {
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
119 120 121
    }
  }

wmmhello's avatar
wmmhello 已提交
122
  char quote = 0, *cmd = command;
123
  for (char c = *command++; c != 0; c = *command++) {
wmmhello's avatar
wmmhello 已提交
124
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
S
Shengliang Guan 已提交
125
      command++;
126 127
      continue;
    }
128

129 130
    if (quote == c) {
      quote = 0;
wmmhello's avatar
wmmhello 已提交
131
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
132
      quote = c;
wmmhello's avatar
wmmhello 已提交
133 134 135
    } else if (c == ';' && quote == 0) {
      c = *command;
      *command = 0;
136
      if (shellRunSingleCommand(cmd) < 0) {
137 138
        return -1;
      }
wmmhello's avatar
wmmhello 已提交
139 140
      *command = c;
      cmd = command;
141 142
    }
  }
143
  return shellRunSingleCommand(cmd);
D
dapan1121 已提交
144 145
}

146
void shellRunSingleCommandImp(char *command) {
147 148 149 150 151
  int64_t st, et;
  char   *sptr = NULL;
  char   *cptr = NULL;
  char   *fname = NULL;
  bool    printMode = false;
H
hzcheng 已提交
152 153 154 155 156 157 158

  if ((sptr = strstr(command, ">>")) != NULL) {
    cptr = strstr(command, ";");
    if (cptr != NULL) {
      *cptr = '\0';
    }

159
    fname = sptr + 2;
wafwerar's avatar
wafwerar 已提交
160
    while (*fname == ' ') fname++;
H
hzcheng 已提交
161 162 163
    *sptr = '\0';
  }

164 165 166 167 168 169 170 171 172 173
  if ((sptr = strstr(command, "\\G")) != NULL) {
    cptr = strstr(command, ";");
    if (cptr != NULL) {
      *cptr = '\0';
    }

    *sptr = '\0';
    printMode = true;  // When output to a file, the switch does not work.
  }

H
hzcheng 已提交
174 175
  st = taosGetTimestampUs();

176
  TAOS_RES *pSql = taos_query(shell.conn, command);
H
Haojun Liao 已提交
177
  if (taos_errno(pSql)) {
178
    shellPrintError(pSql, st);
H
hzcheng 已提交
179 180 181
    return;
  }

182
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
wafwerar's avatar
wafwerar 已提交
183
    fprintf(stdout, "Database changed.\r\n\r\n");
H
hzcheng 已提交
184
    fflush(stdout);
185

S
Shengliang Guan 已提交
186 187
    taos_free_result(pSql);

H
hzcheng 已提交
188 189 190
    return;
  }

S
Shengliang Guan 已提交
191
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
H
Haojun Liao 已提交
192
  if (pFields != NULL) {  // select and show kinds of commands
193
    int32_t error_no = 0;
194

S
Shengliang Guan 已提交
195
    int32_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
196
    if (numOfRows < 0) return;
H
hzcheng 已提交
197 198 199

    et = taosGetTimestampUs();
    if (error_no == 0) {
wafwerar's avatar
wafwerar 已提交
200
      printf("Query OK, %d rows affected (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
H
hzcheng 已提交
201
    } else {
wafwerar's avatar
wafwerar 已提交
202
      printf("Query interrupted (%s), %d rows affected (%.6fs)\r\n", taos_errstr(pSql), numOfRows, (et - st) / 1E6);
H
hzcheng 已提交
203
    }
S
Shengliang Guan 已提交
204
    taos_free_result(pSql);
H
hzcheng 已提交
205
  } else {
206
    int32_t num_rows_affacted = taos_affected_rows(pSql);
207
    taos_free_result(pSql);
H
hzcheng 已提交
208
    et = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
209
    printf("Query OK, %d of %d rows affected (%.6fs)\r\n", num_rows_affacted, num_rows_affacted, (et - st) / 1E6);
H
hzcheng 已提交
210 211
  }

wafwerar's avatar
wafwerar 已提交
212
  printf("\r\n");
H
hzcheng 已提交
213 214
}

215 216
char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) {
  if (shell.args.is_raw_time) {
217 218 219
    sprintf(buf, "%" PRId64, val);
    return buf;
  }
H
hzcheng 已提交
220

S
Shengliang Guan 已提交
221
  time_t  tt;
D
fix bug  
dapan1121 已提交
222
  int32_t ms = 0;
223 224 225 226
  if (precision == TSDB_TIME_PRECISION_NANO) {
    tt = (time_t)(val / 1000000000);
    ms = val % 1000000000;
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
227
    tt = (time_t)(val / 1000000);
D
fix bug  
dapan1121 已提交
228
    ms = val % 1000000;
229 230
  } else {
    tt = (time_t)(val / 1000);
D
fix bug  
dapan1121 已提交
231
    ms = val % 1000;
232 233
  }

234 235
  /*
    comment out as it make testcases like select_with_tags.sim fail.
S
Shengliang Guan 已提交
236 237 238 239 240
    but in windows, this may cause the call to localtime crash if tt < 0,
    need to find a better solution.
    if (tt < 0) {
      tt = 0;
    }
241
  */
242

243 244 245
#ifdef WINDOWS
  if (tt < 0) tt = 0;
#endif
S
Shengliang Guan 已提交
246
  if (tt <= 0 && ms < 0) {
D
fix bug  
dapan1121 已提交
247
    tt--;
248 249 250
    if (precision == TSDB_TIME_PRECISION_NANO) {
      ms += 1000000000;
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
D
fix bug  
dapan1121 已提交
251 252 253 254 255
      ms += 1000000;
    } else {
      ms += 1000;
    }
  }
256

257
  struct tm *ptm = taosLocalTime(&tt, NULL);
S
Shengliang Guan 已提交
258
  size_t     pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm);
259

260 261 262
  if (precision == TSDB_TIME_PRECISION_NANO) {
    sprintf(buf + pos, ".%09d", ms);
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
D
fix bug  
dapan1121 已提交
263
    sprintf(buf + pos, ".%06d", ms);
264
  } else {
D
fix bug  
dapan1121 已提交
265
    sprintf(buf + pos, ".%03d", ms);
266 267 268 269 270
  }

  return buf;
}

271
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
272
  if (val == NULL) {
273
    taosFprintfFile(pFile, "%s", TSDB_DATA_NULL_STR);
274 275 276
    return;
  }

S
Shengliang Guan 已提交
277
  int  n;
278 279 280
  char buf[TSDB_MAX_BYTES_PER_ROW];
  switch (field->type) {
    case TSDB_DATA_TYPE_BOOL:
281
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
282 283
      break;
    case TSDB_DATA_TYPE_TINYINT:
284
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
285
      break;
S
Shengliang Guan 已提交
286 287 288
    case TSDB_DATA_TYPE_UTINYINT:
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
      break;
289
    case TSDB_DATA_TYPE_SMALLINT:
290
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
291
      break;
S
Shengliang Guan 已提交
292 293 294
    case TSDB_DATA_TYPE_USMALLINT:
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
      break;
295
    case TSDB_DATA_TYPE_INT:
296
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
297
      break;
S
Shengliang Guan 已提交
298 299 300
    case TSDB_DATA_TYPE_UINT:
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
      break;
301
    case TSDB_DATA_TYPE_BIGINT:
302
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
303
      break;
S
Shengliang Guan 已提交
304 305 306
    case TSDB_DATA_TYPE_UBIGINT:
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
      break;
307
    case TSDB_DATA_TYPE_FLOAT:
308
      taosFprintfFile(pFile, "%.5f", GET_FLOAT_VAL(val));
309 310
      break;
    case TSDB_DATA_TYPE_DOUBLE:
S
Shengliang Guan 已提交
311
      n = snprintf(buf, TSDB_MAX_BYTES_PER_ROW, "%*.9f", length, GET_DOUBLE_VAL(val));
312
      if (n > TMAX(25, length)) {
S
Shengliang Guan 已提交
313 314 315 316
        taosFprintfFile(pFile, "%*.15e", length, GET_DOUBLE_VAL(val));
      } else {
        taosFprintfFile(pFile, "%s", buf);
      }
317 318 319
      break;
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_NCHAR:
wmmhello's avatar
wmmhello 已提交
320
    case TSDB_DATA_TYPE_JSON:
321 322
      memcpy(buf, val, length);
      buf[length] = 0;
323
      taosFprintfFile(pFile, "\'%s\'", buf);
324 325
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
326
      shellFormatTimestamp(buf, *(int64_t *)val, precision);
327
      taosFprintfFile(pFile, "'%s'", buf);
328 329 330 331 332 333
      break;
    default:
      break;
  }
}

334
int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
335 336 337 338 339
  char fullname[PATH_MAX] = {0};
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
    tstrncpy(fullname, fname, PATH_MAX);
  }

340
  TAOS_ROW row = taos_fetch_row(tres);
341 342 343 344
  if (row == NULL) {
    return 0;
  }

345
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
346
  if (pFile == NULL) {
wafwerar's avatar
wafwerar 已提交
347
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
348 349 350
    return -1;
  }

351
  TAOS_FIELD *fields = taos_fetch_fields(tres);
352 353
  int32_t     num_fields = taos_num_fields(tres);
  int32_t     precision = taos_result_precision(tres);
354

355
  for (int32_t col = 0; col < num_fields; col++) {
356
    if (col > 0) {
357
      taosFprintfFile(pFile, ",");
358
    }
359
    taosFprintfFile(pFile, "%s", fields[col].name);
360
  }
wafwerar's avatar
wafwerar 已提交
361
  taosFprintfFile(pFile, "\r\n");
362

363
  int32_t numOfRows = 0;
364
  do {
S
Shengliang Guan 已提交
365
    int32_t *length = taos_fetch_lengths(tres);
366
    for (int32_t i = 0; i < num_fields; i++) {
367
      if (i > 0) {
X
Xiaoyu Wang 已提交
368
        taosFprintfFile(pFile, ",");
369
      }
370
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
H
hzcheng 已提交
371
    }
wafwerar's avatar
wafwerar 已提交
372
    taosFprintfFile(pFile, "\r\n");
373 374

    numOfRows++;
375
    row = taos_fetch_row(tres);
S
Shengliang Guan 已提交
376
  } while (row != NULL);
377

378
  taosCloseFile(&pFile);
379

380 381 382
  return numOfRows;
}

383
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
wafwerar's avatar
wafwerar 已提交
384
  TdWchar tail[3];
385
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
386

387
  while (pos < length) {
wafwerar's avatar
wafwerar 已提交
388
    TdWchar wc;
389
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
wmmhello's avatar
wmmhello 已提交
390
    if (bytes <= 0) {
391 392
      break;
    }
wmmhello's avatar
wmmhello 已提交
393 394

    if (pos + bytes > length) {
395 396
      break;
    }
wmmhello's avatar
wmmhello 已提交
397
    int w = 0;
X
Xiaoyu Wang 已提交
398
    if (*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r') {
wmmhello's avatar
wmmhello 已提交
399
      w = bytes;
X
Xiaoyu Wang 已提交
400
    } else {
wmmhello's avatar
wmmhello 已提交
401 402 403 404
      w = taosWcharWidth(wc);
    }
    pos += bytes;

405 406 407 408 409 410 411 412 413 414 415 416 417 418
    if (w <= 0) {
      continue;
    }

    if (width <= 0) {
      printf("%lc", wc);
      continue;
    }

    totalCols += w;
    if (totalCols > width) {
      break;
    }
    if (totalCols <= (width - 3)) {
419 420
      printf("%lc", wc);
      cols += w;
421 422 423
    } else {
      tail[tailLen] = wc;
      tailLen++;
424 425 426
    }
  }

427 428
  if (totalCols > width) {
    // width could be 1 or 2, so printf("...") cannot be used
429
    for (int32_t i = 0; i < 3; i++) {
430 431 432 433 434 435 436
      if (cols >= width) {
        break;
      }
      putchar('.');
      ++cols;
    }
  } else {
437
    for (int32_t i = 0; i < tailLen; i++) {
438 439 440 441 442
      printf("%lc", tail[i]);
    }
    cols = totalCols;
  }

443 444 445 446 447
  for (; cols < width; cols++) {
    putchar(' ');
  }
}

448
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
449
  if (val == NULL) {
450
    int32_t w = width;
451 452
    if (field->type < TSDB_DATA_TYPE_TINYINT || field->type > TSDB_DATA_TYPE_DOUBLE) {
      w = 0;
H
hzcheng 已提交
453
    }
454 455 456 457 458 459
    w = printf("%*s", w, TSDB_DATA_NULL_STR);
    for (; w < width; w++) {
      putchar(' ');
    }
    return;
  }
H
hzcheng 已提交
460

S
Shengliang Guan 已提交
461
  int  n;
462 463 464
  char buf[TSDB_MAX_BYTES_PER_ROW];
  switch (field->type) {
    case TSDB_DATA_TYPE_BOOL:
S
TD-1530  
Shengliang Guan 已提交
465
      printf("%*s", width, ((((int32_t)(*((char *)val))) == 1) ? "true" : "false"));
466 467
      break;
    case TSDB_DATA_TYPE_TINYINT:
S
TD-1530  
Shengliang Guan 已提交
468
      printf("%*d", width, *((int8_t *)val));
469
      break;
470 471 472
    case TSDB_DATA_TYPE_UTINYINT:
      printf("%*u", width, *((uint8_t *)val));
      break;
473
    case TSDB_DATA_TYPE_SMALLINT:
S
TD-1530  
Shengliang Guan 已提交
474
      printf("%*d", width, *((int16_t *)val));
475
      break;
476 477 478
    case TSDB_DATA_TYPE_USMALLINT:
      printf("%*u", width, *((uint16_t *)val));
      break;
479
    case TSDB_DATA_TYPE_INT:
S
TD-1530  
Shengliang Guan 已提交
480
      printf("%*d", width, *((int32_t *)val));
481
      break;
482 483 484
    case TSDB_DATA_TYPE_UINT:
      printf("%*u", width, *((uint32_t *)val));
      break;
485 486 487
    case TSDB_DATA_TYPE_BIGINT:
      printf("%*" PRId64, width, *((int64_t *)val));
      break;
488 489 490
    case TSDB_DATA_TYPE_UBIGINT:
      printf("%*" PRIu64, width, *((uint64_t *)val));
      break;
491 492 493 494
    case TSDB_DATA_TYPE_FLOAT:
      printf("%*.5f", width, GET_FLOAT_VAL(val));
      break;
    case TSDB_DATA_TYPE_DOUBLE:
S
Shengliang Guan 已提交
495
      n = snprintf(buf, TSDB_MAX_BYTES_PER_ROW, "%*.9f", width, GET_DOUBLE_VAL(val));
496
      if (n > TMAX(25, width)) {
S
Shengliang Guan 已提交
497 498 499 500
        printf("%*.15e", width, GET_DOUBLE_VAL(val));
      } else {
        printf("%s", buf);
      }
501 502 503
      break;
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_NCHAR:
wmmhello's avatar
wmmhello 已提交
504
    case TSDB_DATA_TYPE_JSON:
B
Bomin Zhang 已提交
505
      shellPrintNChar(val, length, width);
506 507
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
508
      shellFormatTimestamp(buf, *(int64_t *)val, precision);
509 510 511 512
      printf("%s", buf);
      break;
    default:
      break;
H
hzcheng 已提交
513
  }
514
}
H
hzcheng 已提交
515

S
Shengliang Guan 已提交
516
bool shellIsLimitQuery(const char *sql) {
X
Xiaoyu Wang 已提交
517
  // todo refactor
wafwerar's avatar
wafwerar 已提交
518
  if (taosStrCaseStr(sql, " limit ") != NULL) {
S
Shengliang Guan 已提交
519 520 521 522 523 524
    return true;
  }

  return false;
}

D
dapan1121 已提交
525
bool shellIsShowQuery(const char *sql) {
X
Xiaoyu Wang 已提交
526
  // todo refactor
D
dapan1121 已提交
527 528 529 530 531 532 533
  if (taosStrCaseStr(sql, "show ") != NULL) {
    return true;
  }

  return false;
}

S
Shengliang Guan 已提交
534
int32_t shellVerticalPrintResult(TAOS_RES *tres, const char *sql) {
H
Haojun Liao 已提交
535
  TAOS_ROW row = taos_fetch_row(tres);
536 537 538 539
  if (row == NULL) {
    return 0;
  }

540
  int32_t     num_fields = taos_num_fields(tres);
H
Haojun Liao 已提交
541
  TAOS_FIELD *fields = taos_fetch_fields(tres);
542
  int32_t     precision = taos_result_precision(tres);
543

544 545 546
  int32_t maxColNameLen = 0;
  for (int32_t col = 0; col < num_fields; col++) {
    int32_t len = (int32_t)strlen(fields[col].name);
547 548 549 550 551
    if (len > maxColNameLen) {
      maxColNameLen = len;
    }
  }

D
fix bug  
dapan1121 已提交
552 553
  uint64_t resShowMaxNum = UINT64_MAX;

S
Shengliang Guan 已提交
554
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsLimitQuery(sql)) {
555
    resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
D
fix bug  
dapan1121 已提交
556 557
  }

558 559
  int32_t numOfRows = 0;
  int32_t showMore = 1;
560
  do {
D
fix bug  
dapan1121 已提交
561
    if (numOfRows < resShowMaxNum) {
wafwerar's avatar
wafwerar 已提交
562
      printf("*************************** %d.row ***************************\r\n", numOfRows + 1);
D
fix bug  
dapan1121 已提交
563

S
Shengliang Guan 已提交
564
      int32_t *length = taos_fetch_lengths(tres);
D
fix bug  
dapan1121 已提交
565

566
      for (int32_t i = 0; i < num_fields; i++) {
S
Shengliang Guan 已提交
567
        TAOS_FIELD *field = fields + i;
568

569
        int32_t padding = (int32_t)(maxColNameLen - strlen(field->name));
D
fix bug  
dapan1121 已提交
570
        printf("%*.s%s: ", padding, " ", field->name);
571

572
        shellPrintField((const char *)row[i], field, 0, length[i], precision);
wafwerar's avatar
wafwerar 已提交
573
        putchar('\r');
D
fix bug  
dapan1121 已提交
574 575
        putchar('\n');
      }
D
fix bug  
dapan1121 已提交
576
    } else if (showMore) {
wafwerar's avatar
wafwerar 已提交
577 578 579 580 581 582 583
      printf("\r\n");
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
      printf("\r\n");
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
      printf("\r\n");
S
Shengliang Guan 已提交
584
      showMore = 0;
585 586 587
    }

    numOfRows++;
H
Haojun Liao 已提交
588
    row = taos_fetch_row(tres);
S
Shengliang Guan 已提交
589
  } while (row != NULL);
590 591 592 593

  return numOfRows;
}

594 595
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
  int32_t width = (int32_t)strlen(field->name);
596 597

  switch (field->type) {
D
dapan1121 已提交
598 599
    case TSDB_DATA_TYPE_NULL:
      return TMAX(4, width);  // null
600
    case TSDB_DATA_TYPE_BOOL:
dengyihao's avatar
dengyihao 已提交
601
      return TMAX(5, width);  // 'false'
602 603

    case TSDB_DATA_TYPE_TINYINT:
604
    case TSDB_DATA_TYPE_UTINYINT:
dengyihao's avatar
dengyihao 已提交
605
      return TMAX(4, width);  // '-127'
606 607

    case TSDB_DATA_TYPE_SMALLINT:
608
    case TSDB_DATA_TYPE_USMALLINT:
dengyihao's avatar
dengyihao 已提交
609
      return TMAX(6, width);  // '-32767'
610 611

    case TSDB_DATA_TYPE_INT:
612
    case TSDB_DATA_TYPE_UINT:
dengyihao's avatar
dengyihao 已提交
613
      return TMAX(11, width);  // '-2147483648'
614 615

    case TSDB_DATA_TYPE_BIGINT:
616
    case TSDB_DATA_TYPE_UBIGINT:
dengyihao's avatar
dengyihao 已提交
617
      return TMAX(21, width);  // '-9223372036854775807'
618 619

    case TSDB_DATA_TYPE_FLOAT:
dengyihao's avatar
dengyihao 已提交
620
      return TMAX(20, width);
621 622

    case TSDB_DATA_TYPE_DOUBLE:
dengyihao's avatar
dengyihao 已提交
623
      return TMAX(25, width);
624 625

    case TSDB_DATA_TYPE_BINARY:
626 627
      if (field->bytes > shell.args.displayWidth) {
        return TMAX(shell.args.displayWidth, width);
628
      } else {
dengyihao's avatar
dengyihao 已提交
629
        return TMAX(field->bytes, width);
630 631
      }

wmmhello's avatar
wmmhello 已提交
632 633
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_JSON: {
634
      int16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
635 636
      if (bytes > shell.args.displayWidth) {
        return TMAX(shell.args.displayWidth, width);
637
      } else {
dengyihao's avatar
dengyihao 已提交
638
        return TMAX(bytes, width);
639 640 641
      }
    }

642
    case TSDB_DATA_TYPE_TIMESTAMP:
643
      if (shell.args.is_raw_time) {
dengyihao's avatar
dengyihao 已提交
644
        return TMAX(14, width);
S
Shengliang Guan 已提交
645 646
      }
      if (precision == TSDB_TIME_PRECISION_NANO) {
dengyihao's avatar
dengyihao 已提交
647
        return TMAX(29, width);
648
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
dengyihao's avatar
dengyihao 已提交
649
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
650
      } else {
dengyihao's avatar
dengyihao 已提交
651
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
S
slguan 已提交
652
      }
H
hzcheng 已提交
653

654 655
    default:
      assert(false);
H
hzcheng 已提交
656 657
  }

658 659
  return 0;
}
H
hzcheng 已提交
660

661 662 663
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
  int32_t rowWidth = 0;
  for (int32_t col = 0; col < num_fields; col++) {
S
Shengliang Guan 已提交
664
    TAOS_FIELD *field = fields + col;
665 666
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
    int32_t     left = padding / 2;
667 668 669 670
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
    rowWidth += width[col] + 3;
  }

wafwerar's avatar
wafwerar 已提交
671
  putchar('\r');
672
  putchar('\n');
673
  for (int32_t i = 0; i < rowWidth; i++) {
674 675
    putchar('=');
  }
wafwerar's avatar
wafwerar 已提交
676
  putchar('\r');
677 678 679
  putchar('\n');
}

S
Shengliang Guan 已提交
680
int32_t shellHorizontalPrintResult(TAOS_RES *tres, const char *sql) {
H
Haojun Liao 已提交
681
  TAOS_ROW row = taos_fetch_row(tres);
682 683 684 685
  if (row == NULL) {
    return 0;
  }

686
  int32_t     num_fields = taos_num_fields(tres);
H
Haojun Liao 已提交
687
  TAOS_FIELD *fields = taos_fetch_fields(tres);
688
  int32_t     precision = taos_result_precision(tres);
689

690 691 692
  int32_t width[TSDB_MAX_COLUMNS];
  for (int32_t col = 0; col < num_fields; col++) {
    width[col] = shellCalcColWidth(fields + col, precision);
693 694
  }

695
  shellPrintHeader(fields, width, num_fields);
696

D
fix bug  
dapan1121 已提交
697 698
  uint64_t resShowMaxNum = UINT64_MAX;

D
dapan1121 已提交
699
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsLimitQuery(sql) && !shellIsShowQuery(sql)) {
700
    resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
D
fix bug  
dapan1121 已提交
701 702
  }

703 704
  int32_t numOfRows = 0;
  int32_t showMore = 1;
705

706
  do {
S
Shengliang Guan 已提交
707
    int32_t *length = taos_fetch_lengths(tres);
D
fix bug  
dapan1121 已提交
708
    if (numOfRows < resShowMaxNum) {
709
      for (int32_t i = 0; i < num_fields; i++) {
D
fix bug  
dapan1121 已提交
710
        putchar(' ');
711
        shellPrintField((const char *)row[i], fields + i, width[i], length[i], precision);
D
fix bug  
dapan1121 已提交
712 713 714
        putchar(' ');
        putchar('|');
      }
wafwerar's avatar
wafwerar 已提交
715
      putchar('\r');
D
fix bug  
dapan1121 已提交
716
      putchar('\n');
D
fix bug  
dapan1121 已提交
717
    } else if (showMore) {
wafwerar's avatar
wafwerar 已提交
718 719 720 721 722 723 724
      printf("\r\n");
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
      printf("\r\n");
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
      printf("\r\n");
S
Shengliang Guan 已提交
725
      showMore = 0;
726
    }
727

728
    numOfRows++;
H
Haojun Liao 已提交
729
    row = taos_fetch_row(tres);
S
Shengliang Guan 已提交
730
  } while (row != NULL);
731 732 733 734

  return numOfRows;
}

S
Shengliang Guan 已提交
735
int32_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
736
  int32_t numOfRows = 0;
H
hzcheng 已提交
737
  if (fname != NULL) {
738
    numOfRows = shellDumpResultToFile(fname, tres);
S
Shengliang Guan 已提交
739
  } else if (vertical) {
S
Shengliang Guan 已提交
740
    numOfRows = shellVerticalPrintResult(tres, sql);
741
  } else {
S
Shengliang Guan 已提交
742
    numOfRows = shellHorizontalPrintResult(tres, sql);
H
hzcheng 已提交
743 744
  }

H
Haojun Liao 已提交
745
  *error_no = taos_errno(tres);
H
hzcheng 已提交
746 747 748
  return numOfRows;
}

749
void shellReadHistory() {
750 751 752
  SShellHistory *pHistory = &shell.history;
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) return;
H
hzcheng 已提交
753

754 755
  char   *line = NULL;
  int32_t read_size = 0;
756
  while ((read_size = taosGetLineFile(pFile, &line)) != -1) {
H
hzcheng 已提交
757
    line[read_size - 1] = '\0';
758
    taosMemoryFree(pHistory->hist[pHistory->hend]);
759
    pHistory->hist[pHistory->hend] = strdup(line);
H
hzcheng 已提交
760

761
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
H
hzcheng 已提交
762

763 764
    if (pHistory->hend == pHistory->hstart) {
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
H
hzcheng 已提交
765 766 767
    }
  }

S
Shengliang Guan 已提交
768
  if (line != NULL) taosMemoryFree(line);
769
  taosCloseFile(&pFile);
H
hzcheng 已提交
770 771
}

772
void shellWriteHistory() {
773
  SShellHistory *pHistory = &shell.history;
S
Shengliang Guan 已提交
774
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
775
  if (pFile == NULL) return;
H
hzcheng 已提交
776

777 778 779
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
    if (pHistory->hist[i] != NULL) {
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
780 781
      taosMemoryFree(pHistory->hist[i]);
      pHistory->hist[i] = NULL;
H
hzcheng 已提交
782
    }
783
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
H
hzcheng 已提交
784
  }
785
  taosFsyncFile(pFile);
786
  taosCloseFile(&pFile);
H
hzcheng 已提交
787 788
}

789 790 791 792 793 794 795 796 797 798
void shellCleanupHistory() {
  SShellHistory *pHistory = &shell.history;
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
    if (pHistory->hist[i] != NULL) {
      taosMemoryFree(pHistory->hist[i]);
      pHistory->hist[i] = NULL;
    }
  }
}

799
void shellPrintError(TAOS_RES *tres, int64_t st) {
S
TD-1793  
Shengliang Guan 已提交
800
  int64_t et = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
801
  fprintf(stderr, "\r\nDB error: %s (%.6fs)\r\n", taos_errstr(tres), (et - st) / 1E6);
H
Haojun Liao 已提交
802
  taos_free_result(tres);
H
hzcheng 已提交
803 804
}

805 806
bool shellIsCommentLine(char *line) {
  if (line == NULL) return true;
807
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
H
hzcheng 已提交
808 809
}

810 811 812 813 814
void shellSourceFile(const char *file) {
  int32_t read_len = 0;
  char   *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1);
  size_t  cmd_len = 0;
  char   *line = NULL;
815
  char    fullname[PATH_MAX] = {0};
H
hzcheng 已提交
816

817 818
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
    tstrncpy(fullname, file, PATH_MAX);
H
hzcheng 已提交
819 820
  }

821
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
822
  if (pFile == NULL) {
wafwerar's avatar
wafwerar 已提交
823
    fprintf(stderr, "failed to open file %s\r\n", fullname);
wafwerar's avatar
wafwerar 已提交
824
    taosMemoryFree(cmd);
H
hzcheng 已提交
825 826 827
    return;
  }

828
  while ((read_len = taosGetLineFile(pFile, &line)) != -1) {
H
Haojun Liao 已提交
829
    if (read_len >= TSDB_MAX_ALLOWED_SQL_LEN) continue;
H
hzcheng 已提交
830 831
    line[--read_len] = '\0';

832
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
H
hzcheng 已提交
833 834 835 836 837 838 839 840 841 842 843
      continue;
    }

    if (line[read_len - 1] == '\\') {
      line[read_len - 1] = ' ';
      memcpy(cmd + cmd_len, line, read_len);
      cmd_len += read_len;
      continue;
    }

    memcpy(cmd + cmd_len, line, read_len);
wafwerar's avatar
wafwerar 已提交
844
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
845
    shellRunCommand(cmd);
H
Haojun Liao 已提交
846
    memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN);
H
hzcheng 已提交
847 848 849
    cmd_len = 0;
  }

wafwerar's avatar
wafwerar 已提交
850
  taosMemoryFree(cmd);
S
Shengliang Guan 已提交
851
  if (line != NULL) taosMemoryFree(line);
852
  taosCloseFile(&pFile);
H
hzcheng 已提交
853
}
S
slguan 已提交
854

855
void shellGetGrantInfo() {
856 857
  char sinfo[1024] = {0};
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
wafwerar's avatar
wafwerar 已提交
858
  strtok(sinfo, "\r\n");
859

S
slguan 已提交
860 861
  char sql[] = "show grants";

862
  TAOS_RES *tres = taos_query(shell.conn, sql);
H
Haojun Liao 已提交
863

864
  int32_t code = taos_errno(tres);
S
slguan 已提交
865
  if (code != TSDB_CODE_SUCCESS) {
866
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS) {
wafwerar's avatar
wafwerar 已提交
867
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
S
slguan 已提交
868
    }
S
slguan 已提交
869 870 871
    return;
  }

872
  int32_t num_fields = taos_field_count(tres);
S
slguan 已提交
873
  if (num_fields == 0) {
wafwerar's avatar
wafwerar 已提交
874
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
S
slguan 已提交
875 876
    exit(0);
  } else {
877
    if (tres == NULL) {
wafwerar's avatar
wafwerar 已提交
878
      fprintf(stderr, "\r\nGrant information is null.\r\n");
S
slguan 已提交
879 880 881
      exit(0);
    }

882
    TAOS_FIELD *fields = taos_fetch_fields(tres);
883
    TAOS_ROW    row = taos_fetch_row(tres);
S
slguan 已提交
884
    if (row == NULL) {
wafwerar's avatar
wafwerar 已提交
885
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
S
slguan 已提交
886 887 888
      exit(0);
    }

S
slguan 已提交
889
    char serverVersion[32] = {0};
S
slguan 已提交
890 891 892
    char expiretime[32] = {0};
    char expired[32] = {0};

S
slguan 已提交
893
    memcpy(serverVersion, row[0], fields[0].bytes);
S
slguan 已提交
894 895 896
    memcpy(expiretime, row[1], fields[1].bytes);
    memcpy(expired, row[2], fields[2].bytes);

897
    if (strcmp(serverVersion, "community") == 0) {
wafwerar's avatar
wafwerar 已提交
898
      fprintf(stdout, "Server is Community Edition.\r\n");
899
    } else if (strcmp(expiretime, "unlimited") == 0) {
wafwerar's avatar
wafwerar 已提交
900
      fprintf(stdout, "Server is Enterprise %s Edition, %s and will never expire.\r\n", serverVersion, sinfo);
S
slguan 已提交
901
    } else {
wafwerar's avatar
wafwerar 已提交
902
      fprintf(stdout, "Server is Enterprise %s Edition, %s and will expire at %s.\r\n", serverVersion, sinfo, expiretime);
S
slguan 已提交
903 904
    }

905
    taos_free_result(tres);
S
slguan 已提交
906 907
  }

wafwerar's avatar
wafwerar 已提交
908
  fprintf(stdout, "\r\n");
909 910 911 912
}

void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }

913 914 915 916
void shellSigintHandler(int32_t signum, void *sigInfo, void *context) {
  // do nothing
}

917 918 919 920 921 922 923 924 925 926 927
void shellCleanup(void *arg) { taosResetTerminalMode(); }

void *shellCancelHandler(void *arg) {
  setThreadName("shellCancelHandler");
  while (1) {
    if (tsem_wait(&shell.cancelSem) != 0) {
      taosMsleep(10);
      continue;
    }

    taosResetTerminalMode();
wafwerar's avatar
wafwerar 已提交
928
    printf("\r\nReceive SIGTERM or other signal, quit shell.\r\n");
929 930
    shellWriteHistory();
    shellExit();
931 932 933 934 935 936 937 938 939 940 941 942
  }

  return NULL;
}

void *shellThreadLoop(void *arg) {
  setThreadName("shellThreadLoop");
  taosGetOldTerminalMode();
  taosThreadCleanupPush(shellCleanup, NULL);

  char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
  if (command == NULL) {
wafwerar's avatar
wafwerar 已提交
943
    printf("failed to malloc command\r\n");
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    return NULL;
  }

  do {
    memset(command, 0, SHELL_MAX_COMMAND_SIZE);
    taosSetTerminalMode();

    if (shellReadCommand(command) != 0) {
      break;
    }

    taosResetTerminalMode();
  } while (shellRunCommand(command) == 0);

  taosMemoryFreeClear(command);
959 960
  shellWriteHistory();
  shellExit();
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  taosThreadCleanupPop(1);
  return NULL;
}

int32_t shellExecute() {
  printf(shell.info.clientVersion, shell.info.osname, taos_get_client_info());
  fflush(stdout);

  SShellArgs *pArgs = &shell.args;
  if (shell.args.auth == NULL) {
    shell.conn = taos_connect(pArgs->host, pArgs->user, pArgs->password, pArgs->database, pArgs->port);
  } else {
    shell.conn = taos_connect_auth(pArgs->host, pArgs->user, pArgs->auth, pArgs->database, pArgs->port);
  }

  if (shell.conn == NULL) {
    fflush(stdout);
    return -1;
  }

982 983
  shellReadHistory();

984
  if (pArgs->commands != NULL || pArgs->file[0] != 0) {
985
    if (pArgs->commands != NULL) {
wafwerar's avatar
wafwerar 已提交
986
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
987 988 989 990 991
      char *cmd = strdup(pArgs->commands);
      shellRunCommand(cmd);
      taosMemoryFree(cmd);
    }

992
    if (pArgs->file[0] != 0) {
993 994 995 996 997
      shellSourceFile(pArgs->file);
    }

    taos_close(shell.conn);
    shellWriteHistory();
998
    shellCleanupHistory();
999 1000 1001 1002
    return 0;
  }

  if (tsem_init(&shell.cancelSem, 0, 0) != 0) {
wafwerar's avatar
wafwerar 已提交
1003
    printf("failed to create cancel semphore\r\n");
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    return -1;
  }

  TdThread spid = {0};
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);

  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
  taosSetSignal(SIGABRT, shellQueryInterruptHandler);

1014 1015
  taosSetSignal(SIGINT, shellSigintHandler);

1016
  shellGetGrantInfo();
1017 1018 1019 1020

  while (1) {
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, shell.conn);
    taosThreadJoin(shell.pid, NULL);
1021
    taosThreadClear(&shell.pid);
1022 1023
  }

1024
  shellCleanupHistory();
1025
  return 0;
S
slguan 已提交
1026
}