• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4935

22 Jan 2026 06:38AM UTC coverage: 66.708% (+0.02%) from 66.691%
#4935

push

travis-ci

web-flow
merge: from main to 3.0 #34371

121 of 271 new or added lines in 17 files covered. (44.65%)

9066 existing lines in 149 files now uncovered.

203884 of 305637 relevant lines covered (66.71%)

125811266.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.36
/tools/shell/src/shellEngine.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define ALLOW_FORBID_FUNC
17
#define _BSD_SOURCE
18
#define _GNU_SOURCE
19
#define _XOPEN_SOURCE
20
#define _DEFAULT_SOURCE
21
#include "../../inc/pub.h"
22
#include "geosWrapper.h"
23
#include "shellAuto.h"
24
#include "shellInt.h"
25

26
SShellObj shell = {0};
27

28
typedef struct {
29
  const char *sql;
30
  bool        vertical;
31
  tsem_t      sem;
32
  int64_t     numOfRows;  // the num of this batch
33
  int64_t     numOfAllRows;
34

35
  int32_t     numFields;
36
  TAOS_FIELD *fields;
37
  int32_t     precision;
38

39
  int32_t maxColNameLen;            // for vertical print
40
  int32_t width[TSDB_MAX_COLUMNS];  // for horizontal print
41

42
  uint64_t resShowMaxNum;
43
} tsDumpInfo;
44

45
static bool    shellIsEmptyCommand(const char *cmd);
46
static int32_t shellRunSingleCommand(char *command);
47
static void    shellRecordCommandToHistory(char *command);
48
static int32_t shellRunCommand(char *command, bool recordHistory);
49
static void    shellRunSingleCommandImp(char *command);
50
static char   *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision);
51
static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
52
static void    shellPrintNChar(const char *str, int32_t length, int32_t width);
53
static void    shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width);
54
static void    shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
55
static void    shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
56
static int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql);
57
static void    shellReadHistory();
58
static void    shellWriteHistory();
59
static void    shellPrintError(TAOS_RES *tres, int64_t st);
60
static bool    shellIsCommentLine(char *line);
61
static void    shellSourceFile(const char *file);
62
static int32_t shellGetGrantInfo(char *buf);
63

64
static void  shellCleanup(void *arg);
65
static void *shellCancelHandler(void *arg);
66
static void *shellThreadLoop(void *arg);
67

68
static bool shellCmdkilled = false;
69

70
bool shellIsEmptyCommand(const char *cmd) {
230,850,208✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
230,975,121✔
72
    if (c != ' ' && c != '\t' && c != ';') {
155,256,787✔
73
      return false;
155,131,874✔
74
    }
75
  }
76
  return true;
75,718,334✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
153,346,100✔
80
  shellCmdkilled = false;
153,346,100✔
81

82
  if (shellIsEmptyCommand(command)) {
153,346,100✔
83
    return 0;
75,718,334✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
77,627,766✔
87
    return -1;
129✔
88
  }
89

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
77,627,637✔
91
#pragma GCC diagnostic push
92
#pragma GCC diagnostic ignored "-Wunused-result"
93
#ifndef TD_ASTRA
94
    system("clear");
×
95
#else
96
    printf("\033[2J\033[H");
97
#endif
98
#pragma GCC diagnostic pop
99
    return 0;
×
100
  }
101

102
  if (shellRegexMatch(command, "^[\t ]*set[ \t]+max_binary_display_width[ \t]+(default|[1-9][0-9]*)[ \t;]*$",
77,627,637✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
1,949✔
105
    strtok(NULL, " \t");
1,949✔
106
    char *p = strtok(NULL, " \t");
1,949✔
107
    if (strncasecmp(p, "default", 7) == 0) {
1,949✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
1,949✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
1,949✔
112
      shell.args.displayWidth = displayWidth;
1,949✔
113
    }
114
    return 0;
1,949✔
115
  }
116

117
  if (shellRegexMatch(command, "^[ \t]*source[\t ]+[^ ]+[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
77,625,688✔
118
    /* If source file. */
119
    char *c_ptr = strtok(command, " ;");
123✔
120
    if (c_ptr == NULL) {
123✔
121
      shellRunSingleCommandImp(command);
×
122
      return 0;
×
123
    }
124
    c_ptr = strtok(NULL, " ;");
123✔
125
    if (c_ptr == NULL) {
123✔
126
      shellRunSingleCommandImp(command);
×
127
      return 0;
×
128
    }
129
    shellSourceFile(c_ptr);
123✔
130
    return 0;
123✔
131
  }
132
  shellRunSingleCommandImp(command);
77,625,565✔
133
  return 0;
77,625,565✔
134
}
135

136
void shellRecordCommandToHistory(char *command) {
855,289✔
137
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
855,289✔
138
    if (taosStrCaseStr(command, " pass ")) {
×
139
      // have password command forbid record to history because security
140
      return;
×
141
    }
142
  }
143

144
  SShellHistory *pHistory = &shell.history;
855,289✔
145
  if (pHistory->hstart == pHistory->hend ||
855,289✔
146
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
123✔
147
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
123✔
148
    if (pHistory->hist[pHistory->hend] != NULL) {
855,289✔
149
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
×
150
    }
151
    pHistory->hist[pHistory->hend] = taosStrdup(command);
855,289✔
152

153
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
855,289✔
154
    if (pHistory->hend == pHistory->hstart) {
855,289✔
155
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
156
    }
157
  }
158
}
159

160
int32_t shellRunCommand(char *command, bool recordHistory) {
77,504,108✔
161
  if (shellIsEmptyCommand(command)) {
77,504,108✔
162
    return 0;
×
163
  }
164

165
  // add help or help;
166
  if (strncasecmp(command, "help", 4) == 0) {
77,504,108✔
167
    if (command[4] == ';' || command[4] == ' ' || command[4] == 0) {
129✔
168
      showHelp();
129✔
169
      return 0;
129✔
170
    }
171
  }
172

173
  if (recordHistory) shellRecordCommandToHistory(command);
77,503,979✔
174

175
  char quote = 0, *cmd = command;
77,503,979✔
176
  for (char c = *command++; c != 0; c = *command++) {
2,147,483,647✔
177
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
2,147,483,647✔
178
      command++;
2,896✔
179
      continue;
2,896✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
5,137,712✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
5,137,712✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
75,842,250✔
188
      *command = 0;
75,842,250✔
189
      if (shellRunSingleCommand(cmd) < 0) {
75,842,250✔
190
        return -1;
129✔
191
      }
192
      *command = c;
75,842,121✔
193
      cmd = command;
75,842,121✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
77,503,850✔
197
}
198

199
char *strendG(const char *pstr) {
77,625,565✔
200
  if (pstr == NULL) {
77,625,565✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
77,625,565✔
205
  if (len < 4) {
77,625,565✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
77,625,565✔
210
  if (strcmp(p, "\\G") == 0) {
77,625,565✔
211
    return p;
5,743✔
212
  }
213

214
  p = (char *)pstr + len - 3;
77,619,822✔
215
  if (strcmp(p, "\\G;") == 0) {
77,619,822✔
216
    return p;
51,082,711✔
217
  }
218

219
  return NULL;
26,537,111✔
220
}
221

222
void shellRunSingleCommandImp(char *command) {
77,625,565✔
223
  int64_t st, et;
224
  char   *sptr = NULL;
77,625,565✔
225
  char   *cptr = NULL;
77,625,565✔
226
  char   *fname = NULL;
77,625,565✔
227
  bool    printMode = false;
77,625,565✔
228

229
  if ((sptr = strstr(command, ">>")) != NULL) {
77,625,565✔
230
    fname = sptr + 2;
596,447✔
231
    while (*fname == ' ') fname++;
1,192,639✔
232
    *sptr = '\0';
596,447✔
233

234
    cptr = strstr(fname, ";");
596,447✔
235
    if (cptr != NULL) {
596,447✔
236
      *cptr = '\0';
1,521✔
237
    }
238
  }
239

240
  if ((sptr = strendG(command)) != NULL) {
77,625,565✔
241
    *sptr = '\0';
51,088,454✔
242
    printMode = true;  // When output to a file, the switch does not work.
51,088,454✔
243
  }
244

245
  st = taosGetTimestampUs();
77,625,565✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
77,625,565✔
248
  if (taos_errno(pSql)) {
77,625,565✔
249
    shellPrintError(pSql, st);
46,408,840✔
250
    return;
46,408,840✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
31,216,725✔
254
    printf("Database changed.\r\n\r\n");
13,393✔
255

256
    // call back auto tab module
257
    callbackAutoTab(command, pSql, true);
13,393✔
258

259
    taos_free_result(pSql);
13,393✔
260

261
    return;
13,393✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
31,203,332✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,203,332✔
267
    pre = "Delete OK";
62✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,203,270✔
269
    pre = "Insert OK";
4,781✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,198,489✔
271
    pre = "Create OK";
4,350✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,194,139✔
273
    pre = "Drop OK";
937✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
31,203,332✔
277
  if (pFields != NULL) {  // select and show kinds of commands
31,203,332✔
278
    int32_t error_no = 0;
29,892,484✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
29,892,484✔
281
    if (numOfRows < 0) return;
29,892,484✔
282

283
    et = taosGetTimestampUs();
29,892,484✔
284
    if (error_no == 0) {
29,892,484✔
285
      printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
29,892,484✔
286
    } else {
287
      printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", tstrerror(error_no), numOfRows,
×
288
             (et - st) / 1E6);
×
289
    }
290
    taos_free_result(pSql);
29,892,484✔
291
  } else {
292
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,310,848✔
293
    taos_free_result(pSql);
1,310,848✔
294
    et = taosGetTimestampUs();
1,310,848✔
295
    printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,310,848✔
296

297
    // call auto tab
298
    callbackAutoTab(command, NULL, false);
1,310,848✔
299
  }
300

301
  printf("\r\n");
31,203,332✔
302
}
303

304
char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) {
684,397,298✔
305
  if (shell.args.is_raw_time) {
684,397,298✔
306
    sprintf(buf, "%" PRId64, val);
×
307
    return buf;
×
308
  }
309

310
  time_t  tt;
684,001,518✔
311
  int32_t ms = 0;
684,397,298✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
684,397,298✔
313
    tt = (time_t)(val / 1000000000);
1,230✔
314
    ms = val % 1000000000;
1,230✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
684,396,068✔
316
    tt = (time_t)(val / 1000000);
1,230✔
317
    ms = val % 1000000;
1,230✔
318
  } else {
319
    tt = (time_t)(val / 1000);
684,394,838✔
320
    ms = val % 1000;
684,394,838✔
321
  }
322

323
  if (tt <= 0 && ms < 0) {
684,397,298✔
324
    tt--;
9,273✔
325
    if (precision == TSDB_TIME_PRECISION_NANO) {
9,273✔
326
      ms += 1000000000;
×
327
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
9,273✔
328
      ms += 1000000;
×
329
    } else {
330
      ms += 1000;
9,273✔
331
    }
332
  }
333

334
  struct tm ptm = {0};
684,397,298✔
335
  if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) {
684,397,298✔
336
    return buf;
×
337
  }
338
  size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
684,397,298✔
339

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
684,397,298✔
341
    sprintf(buf + pos, ".%09d", ms);
1,230✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
684,396,068✔
343
    sprintf(buf + pos, ".%06d", ms);
1,230✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
684,394,838✔
346
  }
347

348
  return buf;
684,397,298✔
349
}
350

351
char *shellDumpHexValue(char *buf, const char *val, int32_t length) {
×
352
  for (int32_t i = 0; i < length; i++) {
×
353
    sprintf(buf + (i * 2), "%02X", val[i]);
×
354
  }
355
  buf[length * 2] = 0;
×
356

357
  return buf;
×
358
}
359

360
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
1,441,342,162✔
361
  if (val == NULL) {
1,441,342,162✔
362
    taosFprintfFile(pFile, "NULL");
13,226,976✔
363
    return;
13,226,976✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,428,115,186✔
367
  int32_t width;
368

369
  int n = 0;
1,428,115,186✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,428,115,186✔
372
  switch (field->type) {
1,428,115,186✔
373
    case TSDB_DATA_TYPE_BOOL:
276,604,491✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
276,604,491✔
375
      break;
276,604,491✔
376
    case TSDB_DATA_TYPE_TINYINT:
18,314,015✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
18,314,015✔
378
      break;
18,314,015✔
379
    case TSDB_DATA_TYPE_UTINYINT:
723,471✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
723,471✔
381
      break;
723,471✔
382
    case TSDB_DATA_TYPE_SMALLINT:
722,738✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
722,738✔
384
      break;
722,738✔
385
    case TSDB_DATA_TYPE_USMALLINT:
719,806✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
719,806✔
387
      break;
719,806✔
388
    case TSDB_DATA_TYPE_INT:
136,750,316✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
136,750,316✔
390
      break;
136,750,316✔
391
    case TSDB_DATA_TYPE_UINT:
2,166,015✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
2,166,015✔
393
      break;
2,166,015✔
394
    case TSDB_DATA_TYPE_BIGINT:
174,626,700✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
174,626,700✔
396
      break;
174,626,700✔
397
    case TSDB_DATA_TYPE_UBIGINT:
723,471✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
723,471✔
399
      break;
723,471✔
400
    case TSDB_DATA_TYPE_FLOAT:
16,149,230✔
401
      width = SHELL_FLOAT_WIDTH;
16,149,230✔
402
      if (tsEnableScience) {
16,149,230✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
16,149,230✔
406
        if (n > SHELL_FLOAT_WIDTH) {
16,149,230✔
UNCOV
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
16,149,230✔
410
        }
411
      }
412
      break;
16,149,230✔
413
    case TSDB_DATA_TYPE_DOUBLE:
228,332,540✔
414
      width = SHELL_DOUBLE_WIDTH;
228,332,540✔
415
      if (tsEnableScience) {
228,332,540✔
416
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
417
        taosFprintfFile(pFile, "%s", buf);
×
418
      } else {
419
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
228,332,540✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
228,332,540✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
120,545,492✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
107,787,048✔
424
        }
425
      }
426
      break;
228,332,540✔
427
    case TSDB_DATA_TYPE_BINARY:
211,211,590✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
211,211,590✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
211,211,590✔
432
      if (tmp == NULL) break;
211,211,590✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,085,420,259✔
435
        bufIndex++;
2,085,420,259✔
436
        if (val[i] == '\"') {
2,085,420,259✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
211,211,590✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
211,211,590✔
444
      taosMemoryFree(tmp);
211,211,590✔
445
    } break;
211,211,590✔
446
    case TSDB_DATA_TYPE_VARBINARY: {
×
447
      void    *tmp = NULL;
×
448
      uint32_t size = 0;
×
449
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
450
        break;
×
451
      }
452
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
453
      taosMemoryFree(tmp);
×
454
      break;
×
455
    }
456
    case TSDB_DATA_TYPE_GEOMETRY: {
×
457
      char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
×
458
      if (tmp == NULL) break;
×
459
      shellDumpHexValue(tmp, val, length);
×
460
      taosFprintfFile(pFile, "%s", buf);
×
461
      taosMemoryFree(tmp);
×
462
      break;
×
463
    }
464
    case TSDB_DATA_TYPE_TIMESTAMP:
162,047,359✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
162,047,359✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
162,047,359✔
467
      break;
162,047,359✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
199,023,444✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
199,023,444✔
471
      break;
199,023,444✔
472
    case TSDB_DATA_TYPE_BLOB:
×
473
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
474
      void    *tmp = NULL;
×
475
      uint32_t size = 0;
×
476
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
477
        break;
×
478
      }
479
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
480
      taosMemoryFree(tmp);
×
481

482
      break;
×
483
    }
484
    default:
×
485
      break;
×
486
  }
487
}
488

489
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
575,324✔
490
  char fullname[PATH_MAX] = {0};
575,324✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
575,324✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

495
  TAOS_ROW row = taos_fetch_row(tres);
575,324✔
496
  if (row == NULL) {
575,324✔
497
    return 0;
×
498
  }
499

500
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
575,324✔
501
  if (pFile == NULL) {
575,324✔
502
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
503
    return -1;
×
504
  }
505

506
  TAOS_FIELD *fields = taos_fetch_fields(tres);
575,324✔
507
  int32_t     num_fields = taos_num_fields(tres);
575,324✔
508
  int32_t     precision = taos_result_precision(tres);
575,324✔
509

510
  for (int32_t col = 0; col < num_fields; col++) {
1,394,836✔
511
    if (col > 0) {
819,512✔
512
      taosFprintfFile(pFile, ",");
244,188✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
819,512✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
575,324✔
517

518
  int64_t numOfRows = 0;
575,324✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
665,154,456✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,106,496,618✔
522
      if (i > 0) {
1,441,342,162✔
523
        taosFprintfFile(pFile, ",");
776,187,706✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,441,342,162✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
665,154,456✔
528

529
    numOfRows++;
665,154,456✔
530
    row = taos_fetch_row(tres);
665,154,456✔
531
  } while (row != NULL);
665,154,456✔
532

533
  taosCloseFile(&pFile);
575,324✔
534

535
  return numOfRows;
575,324✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
866,402,312✔
539
  TdWchar tail[3];
295,182,035✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
866,402,312✔
541

542
  while (pos < length) {
2,147,483,647✔
543
    TdWchar wc;
2,147,483,647✔
544
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
2,147,483,647✔
545
    if (bytes <= 0) {
2,147,483,647✔
546
      break;
697,381✔
547
    }
548

549
    if (pos + bytes > length) {
2,147,483,647✔
550
      break;
×
551
    }
552
    int w = 0;
2,147,483,647✔
553
    if (*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r') {
2,147,483,647✔
554
      w = bytes;
×
555
    } else {
556
      w = taosWcharWidth(wc);
2,147,483,647✔
557
    }
558
    pos += bytes;
2,147,483,647✔
559

560
    if (w <= 0) {
2,147,483,647✔
561
      continue;
2,147,483,647✔
562
    }
563

564
    if (width <= 0) {
2,147,483,647✔
565
      printf("%lc", wc);
2,147,483,647✔
566
      continue;
2,147,483,647✔
567
    }
568

569
    totalCols += w;
2,147,483,647✔
570
    if (totalCols > width) {
2,147,483,647✔
571
      break;
897,914✔
572
    }
573
    if (totalCols <= (width - 3)) {
2,147,483,647✔
574
      printf("%lc", wc);
2,147,483,647✔
575
      cols += w;
2,147,483,647✔
576
    } else {
577
      tail[tailLen] = wc;
2,844,029✔
578
      tailLen++;
2,844,029✔
579
    }
580
  }
581

582
  if (totalCols > width) {
866,402,312✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,591,656✔
585
      if (cols >= width) {
2,693,742✔
586
        break;
×
587
      }
588
      putchar('.');
2,693,742✔
589
      ++cols;
2,693,742✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
865,659,041✔
593
      printf("%lc", tail[i]);
154,643✔
594
    }
595
    cols = totalCols;
865,504,398✔
596
  }
597

598
  for (; cols < width; cols++) {
2,147,483,647✔
599
    putchar(' ');
2,147,483,647✔
600
  }
601
}
866,402,312✔
602

603
void shellPrintString(const char *str, int32_t width) {
1,290,990,825✔
604
  int32_t len = strlen(str);
1,290,990,825✔
605

606
  if (width == 0) {
1,290,990,825✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
1,290,990,825✔
609
    if (width <= 3) {
5,550✔
610
      printf("%.*s.", width - 1, str);
5,550✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
1,290,985,275✔
616
  }
617
}
1,290,990,825✔
618

619
void shellPrintGeometry(const unsigned char *val, int32_t length, int32_t width) {
×
620
  if (length == 0) {  // empty value
×
621
    shellPrintString("", width);
×
622
    return;
×
623
  }
624

625
  int32_t code = TSDB_CODE_FAILED;
×
626

627
  code = initCtxAsText();
×
628
  if (code != TSDB_CODE_SUCCESS) {
×
629
    shellPrintString(getGeosErrMsg(code), width);
×
630
    return;
×
631
  }
632

633
  char *outputWKT = NULL;
×
634
  code = doAsText(val, length, &outputWKT);
×
635
  if (code != TSDB_CODE_SUCCESS) {
×
636
    shellPrintString(getGeosErrMsg(code), width);  // should NOT happen
×
637
    return;
×
638
  }
639

640
  shellPrintString(outputWKT, width);
×
641

642
  geosFreeBuffer(outputWKT);
×
643
}
644

645
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
2,147,483,647✔
646
  if (val == NULL) {
2,147,483,647✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,102,545,134✔
648
    return;
1,102,545,134✔
649
  }
650

651
  int n = 0;
2,147,483,647✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
2,147,483,647✔
654
  switch (field->type) {
2,147,483,647✔
655
    case TSDB_DATA_TYPE_BOOL:
188,445,691✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
188,445,691✔
657
      break;
188,445,691✔
658
    case TSDB_DATA_TYPE_TINYINT:
58,402,822✔
659
      printf("%*d", width, *((int8_t *)val));
58,402,822✔
660
      break;
58,402,822✔
661
    case TSDB_DATA_TYPE_UTINYINT:
124,657,329✔
662
      printf("%*u", width, *((uint8_t *)val));
124,657,329✔
663
      break;
124,657,329✔
664
    case TSDB_DATA_TYPE_SMALLINT:
46,152,915✔
665
      printf("%*d", width, *((int16_t *)val));
46,152,915✔
666
      break;
46,152,915✔
667
    case TSDB_DATA_TYPE_USMALLINT:
113,655,628✔
668
      printf("%*u", width, *((uint16_t *)val));
113,655,628✔
669
      break;
113,655,628✔
670
    case TSDB_DATA_TYPE_INT:
145,420,030✔
671
      printf("%*d", width, *((int32_t *)val));
145,420,030✔
672
      break;
145,420,030✔
673
    case TSDB_DATA_TYPE_UINT:
135,566,736✔
674
      printf("%*u", width, *((uint32_t *)val));
135,566,736✔
675
      break;
135,566,736✔
676
    case TSDB_DATA_TYPE_BIGINT:
360,439,510✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
360,439,510✔
678
      break;
360,439,510✔
679
    case TSDB_DATA_TYPE_UBIGINT:
155,406,042✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
155,406,042✔
681
      break;
155,406,042✔
682
    case TSDB_DATA_TYPE_FLOAT:
73,203,736✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
73,203,736✔
684
      if (tsEnableScience) {
73,203,736✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
73,203,736✔
688
        printf("%s", buf);
73,203,736✔
689
      }
690
      break;
73,203,736✔
691
    case TSDB_DATA_TYPE_DOUBLE:
286,143,568✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
286,143,568✔
693
      if (tsEnableScience) {
286,143,568✔
694
        snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
695
        printf("%s", buf);
×
696
      } else {
697
        snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
286,143,568✔
698
        printf("%*s", width, buf);
286,143,568✔
699
      }
700
      break;
286,143,568✔
701
    case TSDB_DATA_TYPE_VARBINARY: {
×
702
      void    *data = NULL;
×
703
      uint32_t size = 0;
×
704
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
705
        break;
×
706
      }
707
      shellPrintNChar(data, size, width);
×
708
      taosMemoryFree(data);
×
709
      break;
×
710
    }
711
    case TSDB_DATA_TYPE_BINARY:
866,402,312✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
866,402,312✔
715
      break;
866,402,312✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
522,349,939✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
522,349,939✔
721
      printf("%s", buf);
522,349,939✔
722
      break;
522,349,939✔
723

724
    case TSDB_DATA_TYPE_BLOB:
×
725
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
726
      void    *data = NULL;
×
727
      uint32_t size = 0;
×
728
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
729
        break;
×
730
      }
731
      shellPrintNChar(data, size, width);
×
732
      taosMemoryFree(data);
×
733
      break;
×
734
    }
735
    case TSDB_DATA_TYPE_DECIMAL:
132✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
132✔
738
    default:
132✔
739
      break;
132✔
740
  }
741
}
742

743
// show whole result for this query return true, like limit or describe
744
bool shellIsShowWhole(const char *sql) {
×
745
  // limit
746
  if (taosStrCaseStr(sql, " limit ") != NULL) {
×
747
    return true;
×
748
  }
749
  // describe
750
  if (taosStrCaseStr(sql, "describe ") != NULL) {
×
751
    return true;
×
752
  }
753
  // desc
754
  if (taosStrCaseStr(sql, "desc ") != NULL) {
×
755
    return true;
×
756
  }
757
  // show
758
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
759
    return true;
×
760
  }
761
  // explain
762
  if (taosStrCaseStr(sql, "explain ") != NULL) {
×
763
    return true;
×
764
  }
765

766
  return false;
×
767
}
768

769
bool shellIsShowQuery(const char *sql) {
×
770
  // todo refactor
771
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
772
    return true;
×
773
  }
774

775
  return false;
×
776
}
777

778
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
29,317,160✔
779
  dump_info->sql = sql;
29,317,160✔
780
  dump_info->vertical = vertical;
29,317,160✔
781
  tsem_init(&dump_info->sem, 0, 0);
29,317,160✔
782
  dump_info->numOfAllRows = 0;
29,317,160✔
783

784
  dump_info->numFields = taos_num_fields(tres);
29,317,160✔
785
  dump_info->fields = taos_fetch_fields(tres);
29,317,160✔
786
  dump_info->precision = taos_result_precision(tres);
29,317,160✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
29,317,160✔
789

790
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
29,317,160✔
791
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
×
792
  }
793

794
  if (vertical) {
29,317,160✔
795
    dump_info->maxColNameLen = 0;
17,717,661✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
35,442,042✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
17,724,381✔
798
      if (len > dump_info->maxColNameLen) {
17,724,381✔
799
        dump_info->maxColNameLen = len;
17,719,679✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
56,664,169✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
45,064,670✔
805
    }
806
    // set an appropriate width for token and totp_secret display
807
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
11,599,499✔
808
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
809
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
11,599,499✔
810
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
811
    }
812
  }
813
}
29,317,160✔
814

815
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
17,717,661✔
816
  TAOS_ROW row = taos_fetch_row(tres);
17,717,661✔
817
  if (row == NULL) {
17,717,661✔
818
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
819
    return;
×
820
  }
821

822
  int64_t numOfPintRows = dump_info->numOfAllRows;
17,717,661✔
823
  int     numOfPrintRowsThisOne = 0;
17,717,661✔
824

825
  while (row != NULL) {
572,367,902✔
826
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
572,367,902✔
827

828
    int32_t *length = taos_fetch_lengths(tres);
572,367,902✔
829

830
    for (int32_t i = 0; i < dump_info->numFields; i++) {
1,144,749,554✔
831
      TAOS_FIELD *field = dump_info->fields + i;
572,381,652✔
832

833
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
572,381,652✔
834
      printf("%*.s%s: ", padding, " ", field->name);
572,381,652✔
835

836
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
572,381,652✔
837
      putchar('\r');
572,381,652✔
838
      putchar('\n');
572,381,652✔
839
    }
840

841
    numOfPintRows++;
572,367,902✔
842
    numOfPrintRowsThisOne++;
572,367,902✔
843

844
    if (numOfPintRows == dump_info->resShowMaxNum) {
572,367,902✔
845
      printf("\r\n");
×
846
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
847
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
848
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
849
      printf("\r\n");
×
850
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
851
      printf("\r\n");
×
852
      return;
×
853
    }
854

855
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
572,367,902✔
856
      return;
17,717,661✔
857
    }
858

859
    row = taos_fetch_row(tres);
554,650,241✔
860
  }
861
  return;
×
862
}
863

864
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
45,064,670✔
865
  int32_t width = (int32_t)strlen(field->name);
45,064,670✔
866

867
  switch (field->type) {
45,064,670✔
868
    case TSDB_DATA_TYPE_NULL:
×
869
      return TMAX(4, width);  // null
×
870
    case TSDB_DATA_TYPE_BOOL:
2,896,542✔
871
      return TMAX(5, width);  // 'false'
2,896,542✔
872

873
    case TSDB_DATA_TYPE_TINYINT:
3,936,028✔
874
    case TSDB_DATA_TYPE_UTINYINT:
875
      return TMAX(4, width);  // '-127'
3,936,028✔
876

877
    case TSDB_DATA_TYPE_SMALLINT:
3,202,804✔
878
    case TSDB_DATA_TYPE_USMALLINT:
879
      return TMAX(6, width);  // '-32767'
3,202,804✔
880

881
    case TSDB_DATA_TYPE_INT:
6,970,278✔
882
    case TSDB_DATA_TYPE_UINT:
883
      return TMAX(11, width);  // '-2147483648'
6,970,278✔
884

885
    case TSDB_DATA_TYPE_BIGINT:
6,819,810✔
886
    case TSDB_DATA_TYPE_UBIGINT:
887
      return TMAX(21, width);  // '-9223372036854775807'
6,819,810✔
888

889
    case TSDB_DATA_TYPE_FLOAT:
2,045,104✔
890
      return TMAX(SHELL_FLOAT_WIDTH, width);
2,045,104✔
891

892
    case TSDB_DATA_TYPE_DOUBLE:
6,760,558✔
893
      return TMAX(SHELL_DOUBLE_WIDTH, width);
6,760,558✔
894

895
    case TSDB_DATA_TYPE_BINARY:
4,735,777✔
896
    case TSDB_DATA_TYPE_GEOMETRY:
897
      if (field->bytes > shell.args.displayWidth) {
4,735,777✔
898
        return TMAX(shell.args.displayWidth, width);
3,095,719✔
899
      } else {
900
        return TMAX(field->bytes + 2, width);
1,640,058✔
901
      }
902
    case TSDB_DATA_TYPE_VARBINARY: {
804✔
903
      int32_t bytes = field->bytes * 2 + 2;
804✔
904
      if (bytes > shell.args.displayWidth) {
804✔
905
        return TMAX(shell.args.displayWidth, width);
×
906
      } else {
907
        return TMAX(bytes + 2, width);
804✔
908
      }
909
    }
910
    case TSDB_DATA_TYPE_NCHAR:
3,274,047✔
911
    case TSDB_DATA_TYPE_JSON: {
912
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
3,274,047✔
913
      if (bytes > shell.args.displayWidth) {
3,274,047✔
914
        return TMAX(shell.args.displayWidth, width);
3,274,047✔
915
      } else {
916
        return TMAX(bytes + 2, width);
×
917
      }
918
    }
919

920
    case TSDB_DATA_TYPE_TIMESTAMP:
4,422,786✔
921
      if (shell.args.is_raw_time) {
4,422,786✔
922
        return TMAX(14, width);
201✔
923
      }
924
      if (precision == TSDB_TIME_PRECISION_NANO) {
4,422,585✔
925
        return TMAX(29, width);
123✔
926
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,422,462✔
927
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
123✔
928
      } else {
929
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
4,422,339✔
930
      }
931
    case TSDB_DATA_TYPE_BLOB:
×
932
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
933
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
934
      if (bytes > shell.args.displayWidth) {
×
935
        return TMAX(shell.args.displayWidth, width);
×
936
      } else {
937
        return TMAX(bytes + 2, width);
×
938
      }
939
    } break;
940

941
    case TSDB_DATA_TYPE_DECIMAL64:
66✔
942
      return TMAX(width, 20);
66✔
943
    case TSDB_DATA_TYPE_DECIMAL:
66✔
944
      return TMAX(width, 40);
66✔
945
    default:
×
946
      ASSERT(false);
×
947
  }
948

949
  return 0;
×
950
}
951

952
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
9,021,593✔
953
  int32_t rowWidth = 0;
9,021,593✔
954
  for (int32_t col = 0; col < num_fields; col++) {
47,195,707✔
955
    TAOS_FIELD *field = fields + col;
38,174,114✔
956
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
38,174,114✔
957
    int32_t     left = padding / 2;
38,174,114✔
958
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
38,174,114✔
959
    rowWidth += width[col] + 3;
38,174,114✔
960
  }
961

962
  putchar('\r');
9,021,593✔
963
  putchar('\n');
9,021,593✔
964
  for (int32_t i = 0; i < rowWidth; i++) {
1,073,724,872✔
965
    putchar('=');
1,064,703,279✔
966
  }
967
  putchar('\r');
9,021,593✔
968
  putchar('\n');
9,021,593✔
969
}
9,021,593✔
970

971
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
10,225,374✔
972
  TAOS_ROW row = taos_fetch_row(tres);
10,225,374✔
973
  if (row == NULL) {
10,225,374✔
974
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
975
    return;
×
976
  }
977

978
  int64_t numOfPintRows = dump_info->numOfAllRows;
10,225,374✔
979
  int     numOfPrintRowsThisOne = 0;
10,225,374✔
980
  if (numOfPintRows == 0) {
10,225,374✔
981
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
9,021,593✔
982
  }
983

984
  while (row != NULL) {
465,549,783✔
985
    int32_t *length = taos_fetch_lengths(tres);
465,549,783✔
986
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,147,483,647✔
987
      putchar(' ');
2,147,483,647✔
988
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
2,147,483,647✔
989
                      dump_info->precision);
990
      putchar(' ');
2,147,483,647✔
991
      putchar('|');
2,147,483,647✔
992
    }
993
    putchar('\r');
465,549,783✔
994
    putchar('\n');
465,549,783✔
995

996
    numOfPintRows++;
465,549,783✔
997
    numOfPrintRowsThisOne++;
465,549,783✔
998

999
    if (numOfPintRows == dump_info->resShowMaxNum) {
465,549,783✔
1000
      printf("\r\n");
×
1001
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
1002
      if (shellIsShowQuery(dump_info->sql)) {
×
1003
        printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1004
      } else {
1005
        printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
1006
        printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1007
      }
1008
      printf("\r\n");
×
1009
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
1010
      printf("\r\n");
×
1011
      return;
×
1012
    }
1013

1014
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
465,549,783✔
1015
      return;
10,225,374✔
1016
    }
1017

1018
    row = taos_fetch_row(tres);
455,324,409✔
1019
  }
1020
  return;
×
1021
}
1022

1023
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
57,260,195✔
1024
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
57,260,195✔
1025
  if (num_of_rows > 0) {
57,260,195✔
1026
    dump_info->numOfRows = num_of_rows;
27,943,035✔
1027
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
27,943,035✔
1028
      if (dump_info->vertical) {
27,943,035✔
1029
        shellVerticalPrintResult(tres, dump_info);
17,717,661✔
1030
      } else {
1031
        shellHorizontalPrintResult(tres, dump_info);
10,225,374✔
1032
      }
1033
    }
1034
    dump_info->numOfAllRows += num_of_rows;
27,943,035✔
1035
    if (!shellCmdkilled) {
27,943,035✔
1036
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
27,943,035✔
1037
    } else {
1038
      tsem_post(&dump_info->sem);
×
1039
    }
1040
  } else {
1041
    if (num_of_rows < 0) {
29,317,160✔
1042
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1043
    }
1044
    tsem_post(&dump_info->sem);
29,317,160✔
1045
  }
1046
}
57,260,195✔
1047

1048
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
29,892,484✔
1049
  int64_t num_of_rows = 0;
29,892,484✔
1050
  if (fname != NULL) {
29,892,484✔
1051
    num_of_rows = shellDumpResultToFile(fname, tres);
575,324✔
1052
  } else {
1053
    tsDumpInfo dump_info;
8,145,942✔
1054
    if (!shellCmdkilled) {
29,317,160✔
1055
      init_dump_info(&dump_info, tres, sql, vertical);
29,317,160✔
1056
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
29,317,160✔
1057
      tsem_wait(&dump_info.sem);
29,317,160✔
1058
      num_of_rows = dump_info.numOfAllRows;
29,317,160✔
1059
    }
1060
  }
1061

1062
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
29,892,484✔
1063
  return num_of_rows;
29,892,484✔
1064
}
1065

1066
void shellReadHistory() {
855,295✔
1067
  SShellHistory *pHistory = &shell.history;
855,295✔
1068
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
855,295✔
1069
  if (pFile == NULL) return;
855,295✔
1070

1071
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
817,269✔
1072
  int32_t read_size = 0;
817,269✔
1073
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
162,265,921✔
1074
    line[read_size - 1] = '\0';
161,448,652✔
1075
    taosMemoryFree(pHistory->hist[pHistory->hend]);
161,448,652✔
1076
    pHistory->hist[pHistory->hend] = taosStrdup(line);
161,448,652✔
1077

1078
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
161,448,652✔
1079

1080
    if (pHistory->hend == pHistory->hstart) {
161,448,652✔
1081
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1082
    }
1083
  }
1084

1085
  taosMemoryFreeClear(line);
817,269✔
1086
  taosCloseFile(&pFile);
817,269✔
1087
  int64_t file_size;
815,516✔
1088
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
817,269✔
1089
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
×
1090
    if (pFile == NULL) return;
×
1091
    int32_t endIndex = pHistory->hstart;
×
1092
    if (endIndex != 0) {
×
1093
      endIndex = pHistory->hend;
×
1094
    }
1095
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
×
1096
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
×
1097
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
×
1098
    }
1099
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
×
1100

1101
    /* coverity[+retval] */
1102
    taosFsyncFile(pFile);
×
1103
    taosCloseFile(&pFile);
×
1104
  }
1105
  pHistory->hstart = pHistory->hend;
817,269✔
1106
}
1107

1108
void shellWriteHistory() {
855,295✔
1109
  SShellHistory *pHistory = &shell.history;
855,295✔
1110
  if (pHistory->hend == pHistory->hstart) return;
855,295✔
1111
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
855,166✔
1112
  if (pFile == NULL) return;
855,166✔
1113

1114
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
1,710,455✔
1115
    if (pHistory->hist[i] != NULL) {
855,289✔
1116
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
855,289✔
1117
      taosMemoryFree(pHistory->hist[i]);
855,289✔
1118
      pHistory->hist[i] = NULL;
855,289✔
1119
    }
1120
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
855,289✔
1121
  }
1122
  taosCloseFile(&pFile);
855,166✔
1123
}
1124

1125
void shellCleanupHistory() {
855,295✔
1126
  SShellHistory *pHistory = &shell.history;
855,295✔
1127
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
856,150,295✔
1128
    if (pHistory->hist[i] != NULL) {
855,295,000✔
1129
      taosMemoryFree(pHistory->hist[i]);
161,448,652✔
1130
      pHistory->hist[i] = NULL;
161,448,652✔
1131
    }
1132
  }
1133
}
855,295✔
1134

1135
void shellPrintError(TAOS_RES *tres, int64_t st) {
46,408,840✔
1136
  int code = taos_errno(tres);
46,408,840✔
1137
  int64_t et = taosGetTimestampUs();
46,408,840✔
1138
  printf("\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), code, (et - st) / 1E6);
46,408,840✔
1139
  taos_free_result(tres);
46,408,840✔
1140

1141
  // tip
1142
  if (code == TSDB_CODE_MND_USER_PASSWORD_EXPIRED) {
46,408,840✔
1143
    fprintf(stdout, "******************** TIPS ********************\n");
59✔
1144
    fprintf(stdout, "Please reset your password using the `ALTER USER <user_name> PASS 'new_password'` command.\n");
59✔
1145
    fprintf(stdout, "**********************************************\n");
59✔
1146
  }
1147
}
46,408,840✔
1148

1149
bool shellIsCommentLine(char *line) {
76,770,451✔
1150
  if (line == NULL) return true;
76,770,451✔
1151
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
76,770,451✔
1152
}
1153

1154
void shellSourceFile(const char *file) {
121,359✔
1155
  int32_t read_len = 0;
121,359✔
1156
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
121,359✔
1157
  size_t  cmd_len = 0;
121,359✔
1158
  char    fullname[PATH_MAX] = {0};
121,359✔
1159
  char    sourceFileCommand[PATH_MAX + 8] = {0};
121,359✔
1160

1161
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
121,359✔
1162
    tstrncpy(fullname, file, PATH_MAX);
×
1163
  }
1164

1165
  sprintf(sourceFileCommand, "source %s;", fullname);
121,359✔
1166
  shellRecordCommandToHistory(sourceFileCommand);
121,359✔
1167

1168
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
121,359✔
1169
  if (pFile == NULL) {
121,359✔
1170
    fprintf(stderr, "failed to open file %s\r\n", fullname);
20,191✔
1171
    taosMemoryFree(cmd);
20,191✔
1172
    return;
20,191✔
1173
  }
1174

1175
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
101,168✔
1176
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
77,689,174✔
1177
    if (cmd_len + read_len >= tsMaxSQLLength) {
77,588,006✔
1178
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1179
             read_len);
1180
      cmd_len = 0;
×
1181
      memset(line, 0, tsMaxSQLLength + 1);
×
1182
      continue;
×
1183
    }
1184
    line[--read_len] = '\0';
77,588,006✔
1185

1186
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
77,588,006✔
1187
      continue;
817,957✔
1188
    }
1189

1190
    if (line[read_len - 1] == '\\') {
76,770,049✔
1191
      line[read_len - 1] = ' ';
×
1192
      memcpy(cmd + cmd_len, line, read_len);
×
1193
      cmd_len += read_len;
×
1194
      continue;
×
1195
    }
1196

1197
    if (line[read_len - 1] == '\r') {
76,770,049✔
1198
      line[read_len - 1] = ' ';
×
1199
    }
1200

1201
    memcpy(cmd + cmd_len, line, read_len);
76,770,049✔
1202
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
76,770,049✔
1203
    shellRunCommand(cmd, false);
76,770,049✔
1204
    memset(cmd, 0, tsMaxSQLLength);
76,770,049✔
1205
    cmd_len = 0;
76,770,049✔
1206
  }
1207

1208
  taosMemoryFree(cmd);
101,168✔
1209
  taosMemoryFreeClear(line);
101,168✔
1210
  taosCloseFile(&pFile);
101,168✔
1211
}
1212

1213
int32_t shellGetGrantInfo(char *buf) {
347✔
1214
  int32_t verType = TSDB_VERSION_UNKNOWN;
347✔
1215
  char    sinfo[256] = {0};
347✔
1216
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
347✔
1217
  strtok(sinfo, "\r\n");
347✔
1218

1219
#ifndef TD_ASTRA
1220
  char sql[] = "show grants";
347✔
1221

1222
  TAOS_RES *tres = taos_query(shell.conn, sql);
347✔
1223

1224
  int32_t code = taos_errno(tres);
347✔
1225
  if (code != TSDB_CODE_SUCCESS) {
347✔
1226
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1227
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1228
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1229
    }
1230
    taos_free_result(tres);
×
1231
    return verType;
×
1232
  }
1233

1234
  int32_t num_fields = taos_field_count(tres);
347✔
1235
  if (num_fields == 0) {
347✔
1236
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1237
    exit(0);
×
1238
  } else {
1239
    if (tres == NULL) {
347✔
1240
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1241
      exit(0);
×
1242
    }
1243

1244
    TAOS_FIELD *fields = taos_fetch_fields(tres);
347✔
1245
    TAOS_ROW    row = taos_fetch_row(tres);
347✔
1246
    if (row == NULL) {
347✔
1247
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1248
      exit(0);
×
1249
    }
1250
    char serverVersion[64] = {0};
347✔
1251
    char expiretime[32] = {0};
347✔
1252
    char expired[32] = {0};
347✔
1253

1254
    tstrncpy(serverVersion, row[0], 64);
347✔
1255
    memcpy(expiretime, row[1], fields[1].bytes);
347✔
1256
    memcpy(expired, row[2], fields[2].bytes);
347✔
1257

1258
    trimStr(serverVersion, "trial");
347✔
1259

1260
    if (strcmp(serverVersion, "community") == 0) {
347✔
1261
      verType = TSDB_VERSION_OSS;
×
1262
    } else if (strcmp(expiretime, "unlimited") == 0) {
347✔
1263
      verType = TSDB_VERSION_ENTERPRISE;
×
1264
      sprintf(buf, "Server is %s %s. License will never expire.\r\n", serverVersion, sinfo);
×
1265
    } else {
1266
      verType = TSDB_VERSION_ENTERPRISE;
347✔
1267
      sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
347✔
1268
    }
1269

1270
    taos_free_result(tres);
347✔
1271
  }
1272

1273
  fprintf(stdout, "\r\n");
347✔
1274
#else
1275
  verType = TSDB_VERSION_ENTERPRISE;
1276
  sprintf(buf, "Server is %s %s. License will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1277
#endif
1278
  return verType;
347✔
1279
}
1280

1281
#ifdef WINDOWS
1282
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1283
  tsem_post(&shell.cancelSem);
1284
  return TRUE;
1285
}
1286
#else
1287
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
557✔
1288
#endif
1289

1290
void shellCleanup(void *arg) { taosResetTerminalMode(); }
347✔
1291

1292
void *shellCancelHandler(void *arg) {
347✔
1293
  setThreadName("shellCancelHandler");
347✔
1294
  while (1) {
1295
    if (shell.exit == true) {
971✔
1296
      break;
347✔
1297
    }
1298

1299
    if (tsem_wait(&shell.cancelSem) != 0) {
624✔
1300
      taosMsleep(10);
×
1301
      continue;
×
1302
    }
1303

1304
    if (shell.conn) {
624✔
1305
      shellCmdkilled = true;
277✔
1306
      taos_kill_query(shell.conn);
277✔
1307
    }
1308

1309
#ifdef WINDOWS
1310
    printf("\n%s", shell.info.promptHeader);
1311
#endif
1312
  }
1313

1314
  return NULL;
347✔
1315
}
1316

1317
#pragma GCC diagnostic push
1318
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1319

1320
void *shellThreadLoop(void *arg) {
347✔
1321
  setThreadName("shellThreadLoop");
347✔
1322
  taosGetOldTerminalMode();
347✔
1323
  taosThreadCleanupPush(shellCleanup, NULL);
347✔
1324

1325
  do {
1326
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
347✔
1327
    if (command == NULL) {
347✔
1328
      printf("failed to malloc command\r\n");
×
1329
      break;
×
1330
    }
1331

1332
    do {
1333
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
694✔
1334
      taosSetTerminalMode();
694✔
1335

1336
      if (shellReadCommand(command) != 0) {
694✔
1337
        break;
347✔
1338
      }
1339

1340
      taosResetTerminalMode();
347✔
1341
    } while (shellRunCommand(command, true) == 0);
347✔
1342

1343
    taosMemoryFreeClear(command);
347✔
1344
    shellWriteHistory();
347✔
1345
    shellExit();
347✔
1346
  } while (0);
1347

1348
  taosThreadCleanupPop(1);
347✔
1349
  return NULL;
347✔
1350
}
1351

1352
bool inputTotpCode(char *totpCode) {
2,207✔
1353
  bool ret = true;
2,207✔
1354
  printf("Please enter your TOTP code:");
2,207✔
1355
  if (scanf("%255s", totpCode) != 1) {
2,207✔
1356
    fprintf(stderr, "TOTP code reading error\n");
164✔
1357
    ret = false;
164✔
1358
  }
1359
  if (EOF == getchar()) {
2,207✔
1360
    // tip
1361
    fprintf(stdout, "getchar() return EOF\r\n");    
164✔
1362
  }
1363
  return ret;
2,207✔
1364
}
1365

1366
#pragma GCC diagnostic pop
1367

1368
TAOS *createConnect(SShellArgs *pArgs) {
859,786✔
1369
  char     show[256] = "\0";
859,786✔
1370
  char    *host = NULL;
859,786✔
1371
  uint16_t port = 0;
859,786✔
1372
  char    *user = NULL;
859,786✔
1373
  char    *pwd = NULL;
859,786✔
1374
  TAOS    *taos = NULL;
859,786✔
1375

1376
  // set mode
1377
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
859,786✔
1378
    // websocket
1379
    memcpy(show, pArgs->dsn, 20);
1,524✔
1380
    memcpy(show + 20, "...", 3);
1,524✔
1381
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
1,524✔
1382

1383
    // connect dsn
1384
    taos = taos_connect_with_dsn(pArgs->dsn);
1,524✔
1385
  } else {
1386
    host = (char *)pArgs->host;
858,262✔
1387
    user = (char *)pArgs->user;
858,262✔
1388
    pwd = pArgs->password;
858,262✔
1389

1390
    if (pArgs->port_inputted) {
858,262✔
1391
      port = pArgs->port;
806✔
1392
    } else {
1393
      port = defaultPort(pArgs->connMode, pArgs->dsn);
857,456✔
1394
    }
1395

1396
    sprintf(show, "host:%s port:%d ", host, port);
858,262✔
1397

1398
    // connect normal
1399
    if (pArgs->auth) {
858,262✔
1400
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
207✔
1401
    } else {
1402
#ifdef TD_ENTERPRISE 
1403
      if (strlen(pArgs->token) > 0) {
858,055✔
1404
        // token
1405
        printf("Connect with token ...");
2,407✔
1406
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,407✔
1407
        if (taos != NULL) {
2,407✔
1408
          printf("... [ OK ]\n");
1,121✔
1409
          return taos;
1,121✔
1410
        }
1411
        printf("... [ FAILED ]\n");
1,286✔
1412
        return NULL;
1,286✔
1413
      }
1414
#endif      
1415
      taos = taos_connect(host, user, pwd, pArgs->database, port);
855,648✔
1416
    }
1417

1418
    if (taos == NULL) {
855,855✔
1419
      // failed
1420
      int code = taos_errno(NULL);
4,347✔
1421
      if (code == TSDB_CODE_MND_WRONG_TOTP_CODE) {
4,347✔
1422
         // totp
1423
        char totpCode[TSDB_USER_PASSWORD_LONGLEN];
2,207✔
1424
        memset(totpCode, 0, sizeof(totpCode));  
2,207✔
1425
        if (inputTotpCode(totpCode)) {
2,207✔
1426
          printf("Connect with TOTP code:%s ...", totpCode);
2,043✔
1427
          taos = taos_connect_totp(host, user, pwd, totpCode, pArgs->database, port);
2,043✔
1428
          if (taos != NULL) {
2,043✔
1429
            printf("... [ OK ]\n");
1,386✔
1430
            return taos;
1,386✔
1431
          }
1432
          printf("... [ FAILED ]\n");
657✔
1433
          return NULL;
657✔
1434
        }
1435
      }
1436
      // token
1437
    }
1438
  }
1439

1440
  return taos;
855,336✔
1441
}
1442

1443
int32_t shellExecute(int argc, char *argv[]) {
859,786✔
1444
  int32_t code = 0;
859,786✔
1445
  printf(shell.info.clientVersion, shell.info.cusName,
1,719,572✔
1446
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
859,786✔
1447
         taos_get_client_info(), shell.info.cusName);
1448
  fflush(stdout);
859,786✔
1449

1450
  SShellArgs *pArgs = &shell.args;
859,786✔
1451
  shell.conn = createConnect(pArgs);
859,786✔
1452

1453
  if (shell.conn == NULL) {
859,786✔
1454
    printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
4,491✔
1455
           ERROR_CODE_DETAIL);
1456
    fflush(stdout);
4,491✔
1457
    return -1;
4,491✔
1458
  }
1459

1460
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
855,295✔
1461
  shellSetConn(shell.conn, runOnce);
855,295✔
1462
  shellReadHistory();
855,295✔
1463

1464
  if (shell.args.is_bi_mode) {
855,295✔
1465
    // need set bi mode
1466
    printf("Set BI mode is true.\n");
468✔
1467
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
468✔
1468
  }
1469

1470
  if (runOnce) {
855,295✔
1471
    if (pArgs->commands != NULL) {
854,948✔
1472
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
733,712✔
1473
      char *cmd = taosStrdup(pArgs->commands);
733,712✔
1474
      shellRunCommand(cmd, true);
733,712✔
1475
      taosMemoryFree(cmd);
733,712✔
1476
    }
1477

1478
    if (pArgs->file[0] != 0) {
854,948✔
1479
      shellSourceFile(pArgs->file);
121,236✔
1480
    }
1481

1482
    taos_close(shell.conn);
854,948✔
1483

1484
    shellWriteHistory();
854,948✔
1485
    shellCleanupHistory();
854,948✔
1486
    return 0;
854,948✔
1487
  }
1488

1489
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
347✔
1490
    printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1491
    return code;
×
1492
  }
1493

1494
  TdThread spid = {0};
347✔
1495
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
347✔
1496

1497
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
347✔
1498
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
347✔
1499
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
347✔
1500

1501
  char    buf[512] = {0};
347✔
1502
  int32_t verType = shellGetGrantInfo(buf);
347✔
1503
#ifndef WINDOWS
1504
  printfIntroduction(verType);
347✔
1505
#else
1506
  if (verType == TSDB_VERSION_OSS) {
1507
    showAD(false);
1508
  }
1509
#endif
1510
  // printf version
1511
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
347✔
1512
    printf("%s\n", buf);
347✔
1513
  }
1514

1515
  while (1) {
1516
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
347✔
1517
    taosThreadJoin(shell.pid, NULL);
347✔
1518
    taosThreadClear(&shell.pid);
347✔
1519
    if (shell.exit) {
347✔
1520
      tsem_post(&shell.cancelSem);
347✔
1521
      break;
347✔
1522
    }
1523
  }
1524

1525
  if (verType == TSDB_VERSION_OSS) {
347✔
1526
    showAD(true);
×
1527
  }
1528

1529
  taosThreadJoin(spid, NULL);
347✔
1530

1531
  shellCleanupHistory();
347✔
1532
  taos_kill_query(shell.conn);
347✔
1533
  taos_close(shell.conn);
347✔
1534

1535
  TAOS_RETURN(code);
347✔
1536
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc