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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

81.16
/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) {
233,822,530✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
233,961,181✔
72
    if (c != ' ' && c != '\t' && c != ';') {
157,385,737✔
73
      return false;
157,247,086✔
74
    }
75
  }
76
  return true;
76,575,444✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
155,266,520✔
80
  shellCmdkilled = false;
155,266,520✔
81

82
  if (shellIsEmptyCommand(command)) {
155,266,520✔
83
    return 0;
76,575,444✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
78,691,076✔
87
    return -1;
564✔
88
  }
89

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
78,690,512✔
91
#pragma GCC diagnostic push
92
#pragma GCC diagnostic ignored "-Wunused-result"
93
#ifndef TD_ASTRA
94
    (void)system("clear");
×
95
#else
96
    (void)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;]*$",
78,690,512✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
7,332✔
105
    strtok(NULL, " \t");
7,332✔
106
    char *p = strtok(NULL, " \t");
7,332✔
107
    if (strncasecmp(p, "default", 7) == 0) {
7,332✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
7,332✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
7,332✔
112
      shell.args.displayWidth = displayWidth;
7,332✔
113
    }
114
    return 0;
7,332✔
115
  }
116

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

136
void shellRecordCommandToHistory(char *command) {
977,127✔
137
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
977,127✔
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;
977,127✔
145
  if (pHistory->hstart == pHistory->hend ||
977,127✔
146
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
120✔
147
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
120✔
148
    if (pHistory->hist[pHistory->hend] != NULL) {
977,127✔
149
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
×
150
    }
151
    pHistory->hist[pHistory->hend] = taosStrdup(command);
977,127✔
152

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
78,555,879✔
174

175
  char quote = 0, *cmd = command;
78,555,879✔
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++;
1,052✔
179
      continue;
1,052✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
14,248,797✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
14,248,797✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
76,711,205✔
188
      *command = 0;
76,711,205✔
189
      if (shellRunSingleCommand(cmd) < 0) {
76,711,205✔
190
        return -1;
564✔
191
      }
192
      *command = c;
76,710,641✔
193
      cmd = command;
76,710,641✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
78,555,315✔
197
}
198

199
char *strendG(const char *pstr) {
78,683,060✔
200
  if (pstr == NULL) {
78,683,060✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
78,683,060✔
205
  if (len < 4) {
78,683,060✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
78,683,060✔
210
  if (strcmp(p, "\\G") == 0) {
78,683,060✔
211
    return p;
6,256✔
212
  }
213

214
  p = (char *)pstr + len - 3;
78,676,804✔
215
  if (strcmp(p, "\\G;") == 0) {
78,676,804✔
216
    return p;
123,242✔
217
  }
218

219
  return NULL;
78,553,562✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
78,683,060✔
230
    fname = sptr + 2;
685,563✔
231
    while (*fname == ' ') fname++;
1,370,873✔
232
    *sptr = '\0';
685,563✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
78,683,060✔
241
    *sptr = '\0';
129,498✔
242
    printMode = true;  // When output to a file, the switch does not work.
129,498✔
243
  }
244

245
  st = taosGetTimestampUs();
78,683,060✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
78,683,060✔
248
  if (taos_errno(pSql)) {
78,683,060✔
249
    shellPrintError(pSql, st);
28,805,137✔
250
    return;
28,805,137✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
49,877,923✔
254
    (void)printf("Database changed.\r\n\r\n");
17,339✔
255

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

259
    taos_free_result(pSql);
17,339✔
260

261
    return;
17,339✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
49,860,584✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,860,584✔
267
    pre = "Delete OK";
59✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,860,525✔
269
    pre = "Insert OK";
6,115✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,854,410✔
271
    pre = "Create OK";
5,783✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,848,627✔
273
    pre = "Drop OK";
1,050✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
49,860,584✔
277
  if (pFields != NULL) {  // select and show kinds of commands
49,860,584✔
278
    int32_t error_no = 0;
48,452,383✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
48,452,383✔
281
    if (numOfRows < 0) {
48,452,383✔
282
      taos_free_result(pSql);
×
283
      return;
×
284
    }
285

286
    et = taosGetTimestampUs();
48,452,383✔
287
    if (error_no == 0) {
48,452,383✔
288
      (void)printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
48,452,383✔
289
    } else {
290
      (void)printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", tstrerror(error_no), numOfRows,
×
291
             (et - st) / 1E6);
×
292
    }
293
    taos_free_result(pSql);
48,452,383✔
294
  } else {
295
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,408,201✔
296
    taos_free_result(pSql);
1,408,201✔
297
    et = taosGetTimestampUs();
1,408,201✔
298
    (void)printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,408,201✔
299

300
    // call auto tab
301
    callbackAutoTab(command, NULL, false);
1,408,201✔
302
  }
303

304
  (void)printf("\r\n");
49,860,584✔
305
}
306

307
char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) {
795,352,406✔
308
  if (shell.args.is_raw_time) {
795,352,406✔
309
    (void)sprintf(buf, "%" PRId64, val);
21,960✔
310
    return buf;
21,960✔
311
  }
312

313
  time_t  tt;
794,846,935✔
314
  int32_t ms = 0;
795,330,446✔
315
  if (precision == TSDB_TIME_PRECISION_NANO) {
795,330,446✔
316
    tt = (time_t)(val / 1000000000);
1,200✔
317
    ms = val % 1000000000;
1,200✔
318
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
795,329,246✔
319
    tt = (time_t)(val / 1000000);
1,200✔
320
    ms = val % 1000000;
1,200✔
321
  } else {
322
    tt = (time_t)(val / 1000);
795,328,046✔
323
    ms = val % 1000;
795,328,046✔
324
  }
325

326
  if (tt <= 0 && ms < 0) {
795,330,446✔
327
    tt--;
10,032✔
328
    if (precision == TSDB_TIME_PRECISION_NANO) {
10,032✔
329
      ms += 1000000000;
×
330
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
10,032✔
331
      ms += 1000000;
×
332
    } else {
333
      ms += 1000;
10,032✔
334
    }
335
  }
336

337
  struct tm ptm = {0};
795,330,446✔
338
  if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) {
795,330,446✔
339
    return buf;
×
340
  }
341
  size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
795,330,446✔
342

343
  if (precision == TSDB_TIME_PRECISION_NANO) {
795,330,446✔
344
    (void)sprintf(buf + pos, ".%09d", ms);
1,200✔
345
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
795,329,246✔
346
    (void)sprintf(buf + pos, ".%06d", ms);
1,200✔
347
  } else {
348
    (void)sprintf(buf + pos, ".%03d", ms);
795,328,046✔
349
  }
350

351
  return buf;
795,330,446✔
352
}
353

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

360
  return buf;
×
361
}
362

363
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
1,570,813,257✔
364
  if (val == NULL) {
1,570,813,257✔
365
    taosFprintfFile(pFile, "NULL");
14,638,678✔
366
    return;
14,638,678✔
367
  }
368

369
  char    quotationStr[2] = {'"', 0};
1,556,174,579✔
370
  int32_t width;
371

372
  int n = 0;
1,556,174,579✔
373
#define LENGTH 64
374
  char buf[LENGTH] = {0};
1,556,174,579✔
375
  switch (field->type) {
1,556,174,579✔
376
    case TSDB_DATA_TYPE_BOOL:
296,458,996✔
377
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
296,458,996✔
378
      break;
296,458,996✔
379
    case TSDB_DATA_TYPE_TINYINT:
18,940,250✔
380
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
18,940,250✔
381
      break;
18,940,250✔
382
    case TSDB_DATA_TYPE_UTINYINT:
1,563,410✔
383
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
1,563,410✔
384
      break;
1,563,410✔
385
    case TSDB_DATA_TYPE_SMALLINT:
2,315,932✔
386
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
2,315,932✔
387
      break;
2,315,932✔
388
    case TSDB_DATA_TYPE_USMALLINT:
3,099,021✔
389
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
3,099,021✔
390
      break;
3,099,021✔
391
    case TSDB_DATA_TYPE_INT:
153,228,617✔
392
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
153,228,617✔
393
      break;
153,228,617✔
394
    case TSDB_DATA_TYPE_UINT:
1,562,421✔
395
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,562,421✔
396
      break;
1,562,421✔
397
    case TSDB_DATA_TYPE_BIGINT:
183,701,257✔
398
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
183,701,257✔
399
      break;
183,701,257✔
400
    case TSDB_DATA_TYPE_UBIGINT:
1,536,793✔
401
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
1,536,793✔
402
      break;
1,536,793✔
403
    case TSDB_DATA_TYPE_FLOAT:
17,381,190✔
404
      width = SHELL_FLOAT_WIDTH;
17,381,190✔
405
      if (tsEnableScience) {
17,381,190✔
406
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
407
      } else {
408
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
17,381,190✔
409
        if (n > SHELL_FLOAT_WIDTH) {
17,381,190✔
410
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
411
        } else {
412
          taosFprintfFile(pFile, "%s", buf);
17,381,190✔
413
        }
414
      }
415
      break;
17,381,190✔
416
    case TSDB_DATA_TYPE_DOUBLE:
248,408,322✔
417
      width = SHELL_DOUBLE_WIDTH;
248,408,322✔
418
      if (tsEnableScience) {
248,408,322✔
419
        (void)snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
420
        taosFprintfFile(pFile, "%s", buf);
×
421
      } else {
422
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
248,408,322✔
423
        if (n > SHELL_DOUBLE_WIDTH) {
248,408,322✔
424
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
129,136,750✔
425
        } else {
426
          taosFprintfFile(pFile, "%s", buf);
119,271,572✔
427
        }
428
      }
429
      break;
248,408,322✔
430
    case TSDB_DATA_TYPE_BINARY:
236,421,640✔
431
    case TSDB_DATA_TYPE_NCHAR:
432
    case TSDB_DATA_TYPE_JSON: {
433
      int32_t bufIndex = 0;
236,421,640✔
434
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
236,421,640✔
435
      if (tmp == NULL) break;
236,421,640✔
436
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
437
        tmp[bufIndex] = val[i];
2,147,483,647✔
438
        bufIndex++;
2,147,483,647✔
439
        if (val[i] == '\"') {
2,147,483,647✔
440
          tmp[bufIndex] = val[i];
×
441
          bufIndex++;
×
442
        }
443
      }
444
      tmp[bufIndex] = 0;
236,421,640✔
445

446
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
236,421,640✔
447
      taosMemoryFree(tmp);
236,421,640✔
448
    } break;
236,421,640✔
449
    case TSDB_DATA_TYPE_VARBINARY: {
×
450
      void    *tmp = NULL;
×
451
      uint32_t size = 0;
×
452
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
453
        break;
×
454
      }
455
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
456
      taosMemoryFree(tmp);
×
457
      break;
×
458
    }
459
    case TSDB_DATA_TYPE_GEOMETRY: {
×
460
      char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
×
461
      if (tmp == NULL) break;
×
462
      shellDumpHexValue(tmp, val, length);
×
463
      taosFprintfFile(pFile, "%s", buf);
×
464
      taosMemoryFree(tmp);
×
465
      break;
×
466
    }
467
    case TSDB_DATA_TYPE_TIMESTAMP:
176,005,351✔
468
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
176,005,351✔
469
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
176,005,351✔
470
      break;
176,005,351✔
471
    case TSDB_DATA_TYPE_DECIMAL64:
215,551,379✔
472
    case TSDB_DATA_TYPE_DECIMAL:
473
      taosFprintfFile(pFile, "%s", val);
215,551,379✔
474
      break;
215,551,379✔
475
    case TSDB_DATA_TYPE_BLOB:
×
476
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
477
      void    *tmp = NULL;
×
478
      uint32_t size = 0;
×
479
      if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
×
480
        break;
×
481
      }
482
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
×
483
      taosMemoryFree(tmp);
×
484

485
      break;
×
486
    }
487
    default:
×
488
      break;
×
489
  }
490
}
491

492
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
664,612✔
493
  char fullname[PATH_MAX] = {0};
664,612✔
494
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
664,612✔
495
    tstrncpy(fullname, fname, PATH_MAX);
×
496
  }
497

498
  TAOS_ROW row = taos_fetch_row(tres);
664,612✔
499
  if (row == NULL) {
664,612✔
500
    return 0;
×
501
  }
502

503
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
664,612✔
504
  if (pFile == NULL) {
664,612✔
505
    (void)fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
506
    return -1;
×
507
  }
508

509
  TAOS_FIELD *fields = taos_fetch_fields(tres);
664,612✔
510
  int32_t     num_fields = taos_num_fields(tres);
664,612✔
511
  int32_t     precision = taos_result_precision(tres);
664,612✔
512

513
  for (int32_t col = 0; col < num_fields; col++) {
1,632,145✔
514
    if (col > 0) {
967,533✔
515
      taosFprintfFile(pFile, ",");
302,921✔
516
    }
517
    taosFprintfFile(pFile, "%s", fields[col].name);
967,533✔
518
  }
519
  taosFprintfFile(pFile, "\r\n");
664,612✔
520

521
  int64_t numOfRows = 0;
664,612✔
522
  do {
523
    int32_t *length = taos_fetch_lengths(tres);
722,277,729✔
524
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
525
      if (i > 0) {
1,570,813,257✔
526
        taosFprintfFile(pFile, ",");
848,535,528✔
527
      }
528
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,570,813,257✔
529
    }
530
    taosFprintfFile(pFile, "\r\n");
722,277,729✔
531

532
    numOfRows++;
722,277,729✔
533
    row = taos_fetch_row(tres);
722,277,729✔
534
  } while (row != NULL);
722,277,729✔
535

536
  taosCloseFile(&pFile);
664,612✔
537

538
  return numOfRows;
664,612✔
539
}
540

541
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
323,405,040✔
542
  TdWchar tail[3];
323,195,872✔
543
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
323,405,040✔
544

545
  while (pos < length) {
2,147,483,647✔
546
    TdWchar wc;
2,147,483,647✔
547
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
2,147,483,647✔
548
    if (bytes <= 0) {
2,147,483,647✔
549
      break;
144,432✔
550
    }
551

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

563
    if (w <= 0) {
2,147,483,647✔
564
      continue;
6,020,527✔
565
    }
566

567
    if (width <= 0) {
2,147,483,647✔
568
      (void)printf("%lc", wc);
220,824,364✔
569
      continue;
220,824,364✔
570
    }
571

572
    totalCols += w;
2,147,483,647✔
573
    if (totalCols > width) {
2,147,483,647✔
574
      break;
1,043,529✔
575
    }
576
    if (totalCols <= (width - 3)) {
2,147,483,647✔
577
      (void)printf("%lc", wc);
2,147,483,647✔
578
      cols += w;
2,147,483,647✔
579
    } else {
580
      tail[tailLen] = wc;
3,365,029✔
581
      tailLen++;
3,365,029✔
582
    }
583
  }
584

585
  if (totalCols > width) {
323,405,040✔
586
    // width could be 1 or 2, so printf("...") cannot be used
587
    for (int32_t i = 0; i < 3; i++) {
4,174,116✔
588
      if (cols >= width) {
3,130,587✔
589
        break;
×
590
      }
591
      putchar('.');
3,130,587✔
592
      ++cols;
3,130,587✔
593
    }
594
  } else {
595
    for (int32_t i = 0; i < tailLen; i++) {
322,600,678✔
596
      (void)printf("%lc", tail[i]);
239,167✔
597
    }
598
    cols = totalCols;
322,361,511✔
599
  }
600

601
  for (; cols < width; cols++) {
2,147,483,647✔
602
    putchar(' ');
2,147,483,647✔
603
  }
604
}
323,405,040✔
605

606
void shellPrintString(const char *str, int32_t width) {
1,527,395,099✔
607
  int32_t len = strlen(str);
1,527,395,099✔
608

609
  if (width == 0) {
1,527,395,099✔
610
    (void)printf("%s", str);
×
611
  } else if (len > width) {
1,527,395,099✔
612
    if (width <= 3) {
7,056✔
613
      (void)printf("%.*s.", width - 1, str);
7,056✔
614
    } else {
615
      (void)printf("%.*s...", width - 3, str);
×
616
    }
617
  } else {
618
    (void)printf("%s%*.s", str, width - len, "");
1,527,388,043✔
619
  }
620
}
1,527,395,099✔
621

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

628
  int32_t code = TSDB_CODE_FAILED;
×
629

630
  code = initCtxAsText();
×
631
  if (code != TSDB_CODE_SUCCESS) {
×
632
    shellPrintString(getGeosErrMsg(code), width);
×
633
    return;
×
634
  }
635

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

643
  shellPrintString(outputWKT, width);
×
644

645
  geosFreeBuffer(outputWKT);
×
646
}
647

648
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
2,147,483,647✔
649
  if (val == NULL) {
2,147,483,647✔
650
    shellPrintString(TSDB_DATA_NULL_STR, width);
1,292,783,145✔
651
    return;
1,292,783,145✔
652
  }
653

654
  int n = 0;
2,147,483,647✔
655
#define LENGTH 64
656
  char buf[LENGTH] = {0};
2,147,483,647✔
657
  switch (field->type) {
2,147,483,647✔
658
    case TSDB_DATA_TYPE_BOOL:
234,611,954✔
659
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
234,611,954✔
660
      break;
234,611,954✔
661
    case TSDB_DATA_TYPE_TINYINT:
63,236,205✔
662
      (void)printf("%*d", width, *((int8_t *)val));
63,236,205✔
663
      break;
63,236,205✔
664
    case TSDB_DATA_TYPE_UTINYINT:
135,972,239✔
665
      (void)printf("%*u", width, *((uint8_t *)val));
135,972,239✔
666
      break;
135,972,239✔
667
    case TSDB_DATA_TYPE_SMALLINT:
49,647,247✔
668
      (void)printf("%*d", width, *((int16_t *)val));
49,647,247✔
669
      break;
49,647,247✔
670
    case TSDB_DATA_TYPE_USMALLINT:
120,204,444✔
671
      (void)printf("%*u", width, *((uint16_t *)val));
120,204,444✔
672
      break;
120,204,444✔
673
    case TSDB_DATA_TYPE_INT:
160,645,101✔
674
      (void)printf("%*d", width, *((int32_t *)val));
160,645,101✔
675
      break;
160,645,101✔
676
    case TSDB_DATA_TYPE_UINT:
170,017,058✔
677
      (void)printf("%*u", width, *((uint32_t *)val));
170,017,058✔
678
      break;
170,017,058✔
679
    case TSDB_DATA_TYPE_BIGINT:
433,027,250✔
680
      (void)printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
433,027,250✔
681
      break;
433,027,250✔
682
    case TSDB_DATA_TYPE_UBIGINT:
177,156,160✔
683
      (void)printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
177,156,160✔
684
      break;
177,156,160✔
685
    case TSDB_DATA_TYPE_FLOAT:
78,793,226✔
686
      width = width >= LENGTH ? LENGTH - 1 : width;
78,793,226✔
687
      if (tsEnableScience) {
78,793,226✔
688
        (void)printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
689
      } else {
690
        (void)snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
78,793,226✔
691
        (void)printf("%s", buf);
78,793,226✔
692
      }
693
      break;
78,793,226✔
694
    case TSDB_DATA_TYPE_DOUBLE:
339,310,673✔
695
      width = width >= LENGTH ? LENGTH - 1 : width;
339,310,673✔
696
      if (tsEnableScience) {
339,310,673✔
697
        (void)snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
698
        (void)printf("%s", buf);
×
699
      } else {
700
        (void)snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
339,310,673✔
701
        (void)printf("%*s", width, buf);
339,310,673✔
702
      }
703
      break;
339,310,673✔
704
    case TSDB_DATA_TYPE_VARBINARY: {
×
705
      void    *data = NULL;
×
706
      uint32_t size = 0;
×
707
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
708
        break;
×
709
      }
710
      shellPrintNChar(data, size, width);
×
711
      taosMemoryFree(data);
×
712
      break;
×
713
    }
714
    case TSDB_DATA_TYPE_BINARY:
323,405,040✔
715
    case TSDB_DATA_TYPE_NCHAR:
716
    case TSDB_DATA_TYPE_JSON:
717
      shellPrintNChar(val, length, width);
323,405,040✔
718
      break;
323,405,040✔
719
    case TSDB_DATA_TYPE_GEOMETRY:
×
720
      shellPrintGeometry(val, length, width);
×
721
      break;
×
722
    case TSDB_DATA_TYPE_TIMESTAMP:
619,347,055✔
723
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
619,347,055✔
724
      (void)printf("%s", buf);
619,347,055✔
725
      break;
619,347,055✔
726

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

746
// show whole result for this query return true, like limit or describe
747
bool shellIsShowWhole(const char *sql) {
432✔
748
  // limit
749
  char * p = taosStrCaseStr(sql, " limit ");
432✔
750
  if (p != NULL) {
432✔
751
    // except subquery, like "select * from (select * from t limit 10) limit 3", only the last limit is valid
752
    char * p1 = taosStrCaseStr(p + 7, ")");
216✔
753
    if (p1 == NULL) {
216✔
754
      return true;
72✔
755
    }
756
    if (taosStrCaseStr(p1 + 1, " limit ")) {
144✔
757
      return true;
72✔
758
    }
759
  }
760
  // describe
761
  if (taosStrCaseStr(sql, "describe ") != NULL) {
288✔
762
    return true;
72✔
763
  }
764
  // desc
765
  if (taosStrCaseStr(sql, "desc ") != NULL) {
216✔
766
    return true;
72✔
767
  }
768
  // show
769
  if (taosStrCaseStr(sql, "show ") != NULL) {
144✔
770
    return true;
×
771
  }
772
  // explain
773
  if (taosStrCaseStr(sql, "explain ") != NULL) {
144✔
774
    return true;
72✔
775
  }
776

777
  return false;
72✔
778
}
779

780
bool shellIsShowQuery(const char *sql) {
72✔
781
  // todo refactor
782
  if (taosStrCaseStr(sql, "show ") != NULL) {
72✔
783
    return true;
×
784
  }
785

786
  return false;
72✔
787
}
788

789
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
47,787,771✔
790
  dump_info->sql = sql;
47,787,771✔
791
  dump_info->vertical = vertical;
47,787,771✔
792
  tsem_init(&dump_info->sem, 0, 0);
47,787,771✔
793
  dump_info->numOfAllRows = 0;
47,787,771✔
794

795
  dump_info->numFields = taos_num_fields(tres);
47,787,771✔
796
  dump_info->fields = taos_fetch_fields(tres);
47,787,771✔
797
  dump_info->precision = taos_result_precision(tres);
47,787,771✔
798

799
  dump_info->resShowMaxNum = UINT64_MAX;
47,787,771✔
800

801
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
47,787,771✔
802
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
72✔
803
  }
804

805
  if (vertical) {
47,787,771✔
806
    dump_info->maxColNameLen = 0;
120,318✔
807
    for (int32_t col = 0; col < dump_info->numFields; col++) {
247,765✔
808
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
127,447✔
809
      if (len > dump_info->maxColNameLen) {
127,447✔
810
        dump_info->maxColNameLen = len;
122,466✔
811
      }
812
    }
813
  } else {
814
    for (int32_t col = 0; col < dump_info->numFields; col++) {
137,040,387✔
815
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
89,372,934✔
816
    }
817
    // set an appropriate width for token and totp_secret display
818
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
47,667,453✔
819
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
820
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
47,667,453✔
821
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
822
    }
823
  }
824
}
47,787,771✔
825

826
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
120,318✔
827
  TAOS_ROW row = taos_fetch_row(tres);
120,318✔
828
  if (row == NULL) {
120,318✔
829
    (void)printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
830
    return;
×
831
  }
832

833
  int64_t numOfPintRows = dump_info->numOfAllRows;
120,318✔
834
  int     numOfPrintRowsThisOne = 0;
120,318✔
835

836
  while (row != NULL) {
3,037,373✔
837
    (void)printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
3,037,373✔
838

839
    int32_t *length = taos_fetch_lengths(tres);
3,037,373✔
840

841
    for (int32_t i = 0; i < dump_info->numFields; i++) {
6,088,819✔
842
      TAOS_FIELD *field = dump_info->fields + i;
3,051,446✔
843

844
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
3,051,446✔
845
      (void)printf("%*.s%s: ", padding, " ", field->name);
3,051,446✔
846

847
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
3,051,446✔
848
      putchar('\r');
3,051,446✔
849
      putchar('\n');
3,051,446✔
850
    }
851

852
    numOfPintRows++;
3,037,373✔
853
    numOfPrintRowsThisOne++;
3,037,373✔
854

855
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,037,373✔
856
      (void)printf("\r\n");
×
857
      (void)printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
858
      (void)printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
859
      (void)printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
860
      (void)printf("\r\n");
×
861
      (void)printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
862
      (void)printf("\r\n");
×
863
      return;
×
864
    }
865

866
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
3,037,373✔
867
      return;
120,318✔
868
    }
869

870
    row = taos_fetch_row(tres);
2,917,055✔
871
  }
872
  return;
×
873
}
874

875
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
89,372,934✔
876
  int32_t width = (int32_t)strlen(field->name);
89,372,934✔
877

878
  switch (field->type) {
89,372,934✔
879
    case TSDB_DATA_TYPE_NULL:
×
880
      return TMAX(4, width);  // null
×
881
    case TSDB_DATA_TYPE_BOOL:
20,255,160✔
882
      return TMAX(5, width);  // 'false'
20,255,160✔
883

884
    case TSDB_DATA_TYPE_TINYINT:
4,856,807✔
885
    case TSDB_DATA_TYPE_UTINYINT:
886
      return TMAX(4, width);  // '-127'
4,856,807✔
887

888
    case TSDB_DATA_TYPE_SMALLINT:
3,795,129✔
889
    case TSDB_DATA_TYPE_USMALLINT:
890
      return TMAX(6, width);  // '-32767'
3,795,129✔
891

892
    case TSDB_DATA_TYPE_INT:
8,971,654✔
893
    case TSDB_DATA_TYPE_UINT:
894
      return TMAX(11, width);  // '-2147483648'
8,971,654✔
895

896
    case TSDB_DATA_TYPE_BIGINT:
8,572,503✔
897
    case TSDB_DATA_TYPE_UBIGINT:
898
      return TMAX(21, width);  // '-9223372036854775807'
8,572,503✔
899

900
    case TSDB_DATA_TYPE_FLOAT:
2,581,170✔
901
      return TMAX(SHELL_FLOAT_WIDTH, width);
2,581,170✔
902

903
    case TSDB_DATA_TYPE_DOUBLE:
8,754,348✔
904
      return TMAX(SHELL_DOUBLE_WIDTH, width);
8,754,348✔
905

906
    case TSDB_DATA_TYPE_BINARY:
6,125,677✔
907
    case TSDB_DATA_TYPE_GEOMETRY:
908
      if (field->bytes > shell.args.displayWidth) {
6,125,677✔
909
        return TMAX(shell.args.displayWidth, width);
4,149,281✔
910
      } else {
911
        return TMAX(field->bytes + 2, width);
1,976,396✔
912
      }
913
    case TSDB_DATA_TYPE_VARBINARY: {
806✔
914
      int32_t bytes = field->bytes * 2 + 2;
806✔
915
      if (bytes > shell.args.displayWidth) {
806✔
916
        return TMAX(shell.args.displayWidth, width);
×
917
      } else {
918
        return TMAX(bytes + 2, width);
806✔
919
      }
920
    }
921
    case TSDB_DATA_TYPE_NCHAR:
4,097,354✔
922
    case TSDB_DATA_TYPE_JSON: {
923
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
4,097,354✔
924
      if (bytes > shell.args.displayWidth) {
4,097,354✔
925
        return TMAX(shell.args.displayWidth, width);
4,097,354✔
926
      } else {
927
        return TMAX(bytes + 2, width);
×
928
      }
929
    }
930

931
    case TSDB_DATA_TYPE_TIMESTAMP:
21,362,192✔
932
      if (shell.args.is_raw_time) {
21,362,192✔
933
        return TMAX(14, width);
417✔
934
      }
935
      if (precision == TSDB_TIME_PRECISION_NANO) {
21,361,775✔
936
        return TMAX(29, width);
120✔
937
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
21,361,655✔
938
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
120✔
939
      } else {
940
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
21,361,535✔
941
      }
942
    case TSDB_DATA_TYPE_BLOB:
×
943
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
944
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
945
      if (bytes > shell.args.displayWidth) {
×
946
        return TMAX(shell.args.displayWidth, width);
×
947
      } else {
948
        return TMAX(bytes + 2, width);
×
949
      }
950
    } break;
951

952
    case TSDB_DATA_TYPE_DECIMAL64:
67✔
953
      return TMAX(width, 20);
67✔
954
    case TSDB_DATA_TYPE_DECIMAL:
67✔
955
      return TMAX(width, 40);
67✔
956
    default:
×
957
      ASSERT(false);
×
958
  }
959

960
  return 0;
×
961
}
962

963
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
33,923,060✔
964
  int32_t rowWidth = 0;
33,923,060✔
965
  for (int32_t col = 0; col < num_fields; col++) {
104,696,507✔
966
    TAOS_FIELD *field = fields + col;
70,773,447✔
967
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
70,773,447✔
968
    int32_t     left = padding / 2;
70,773,447✔
969
    (void)printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
70,773,447✔
970
    rowWidth += width[col] + 3;
70,773,447✔
971
  }
972

973
  putchar('\r');
33,923,060✔
974
  putchar('\n');
33,923,060✔
975
  for (int32_t i = 0; i < rowWidth; i++) {
2,147,483,647✔
976
    putchar('=');
2,147,483,647✔
977
  }
978
  putchar('\r');
33,923,060✔
979
  putchar('\n');
33,923,060✔
980
}
33,923,060✔
981

982
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
35,355,175✔
983
  TAOS_ROW row = taos_fetch_row(tres);
35,355,175✔
984
  if (row == NULL) {
35,355,175✔
985
    (void)printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
986
    return;
×
987
  }
988

989
  int64_t numOfPintRows = dump_info->numOfAllRows;
35,355,175✔
990
  int     numOfPrintRowsThisOne = 0;
35,355,175✔
991
  if (numOfPintRows == 0) {
35,355,175✔
992
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
33,923,060✔
993
  }
994

995
  while (row != NULL) {
599,191,410✔
996
    int32_t *length = taos_fetch_lengths(tres);
599,191,410✔
997
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,147,483,647✔
998
      putchar(' ');
2,147,483,647✔
999
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
2,147,483,647✔
1000
                      dump_info->precision);
1001
      putchar(' ');
2,147,483,647✔
1002
      putchar('|');
2,147,483,647✔
1003
    }
1004
    putchar('\r');
599,191,410✔
1005
    putchar('\n');
599,191,410✔
1006

1007
    numOfPintRows++;
599,191,410✔
1008
    numOfPrintRowsThisOne++;
599,191,410✔
1009

1010
    if (numOfPintRows == dump_info->resShowMaxNum) {
599,191,410✔
1011
      (void)printf("\r\n");
72✔
1012
      (void)printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
72✔
1013
      if (shellIsShowQuery(dump_info->sql)) {
72✔
1014
        (void)printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
1015
      } else {
1016
        (void)printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
72✔
1017
        (void)printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
72✔
1018
      }
1019
      (void)printf("\r\n");
72✔
1020
      (void)printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
72✔
1021
      (void)printf("\r\n");
72✔
1022
      return;
72✔
1023
    }
1024

1025
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
599,191,338✔
1026
      return;
35,355,103✔
1027
    }
1028

1029
    row = taos_fetch_row(tres);
563,836,235✔
1030
  }
1031
  return;
×
1032
}
1033

1034
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
83,263,264✔
1035
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
83,263,264✔
1036
  if (num_of_rows > 0) {
83,263,264✔
1037
    dump_info->numOfRows = num_of_rows;
35,475,493✔
1038
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
35,475,493✔
1039
      if (dump_info->vertical) {
35,475,493✔
1040
        shellVerticalPrintResult(tres, dump_info);
120,318✔
1041
      } else {
1042
        shellHorizontalPrintResult(tres, dump_info);
35,355,175✔
1043
      }
1044
    }
1045
    dump_info->numOfAllRows += num_of_rows;
35,475,493✔
1046
    if (!shellCmdkilled) {
35,475,493✔
1047
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
35,475,493✔
1048
    } else {
1049
      tsem_post(&dump_info->sem);
×
1050
    }
1051
  } else {
1052
    if (num_of_rows < 0) {
47,787,771✔
1053
      (void)printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1054
    }
1055
    tsem_post(&dump_info->sem);
47,787,771✔
1056
  }
1057
}
83,263,197✔
1058

1059
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
48,452,383✔
1060
  int64_t num_of_rows = 0;
48,452,383✔
1061
  if (fname != NULL) {
48,452,383✔
1062
    num_of_rows = shellDumpResultToFile(fname, tres);
664,612✔
1063
  } else {
1064
    tsDumpInfo dump_info;
43,088,566✔
1065
    if (!shellCmdkilled) {
47,787,771✔
1066
      init_dump_info(&dump_info, tres, sql, vertical);
47,787,771✔
1067
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
47,787,771✔
1068
      tsem_wait(&dump_info.sem);
47,787,771✔
1069
      num_of_rows = dump_info.numOfAllRows;
47,787,771✔
1070
    }
1071
  }
1072

1073
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
48,452,383✔
1074
  return num_of_rows;
48,452,383✔
1075
}
1076

1077
void shellReadHistory() {
977,138✔
1078
  SShellHistory *pHistory = &shell.history;
977,138✔
1079
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
977,138✔
1080
  if (pFile == NULL) return;
977,138✔
1081

1082
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
930,714✔
1083
  int32_t read_size = 0;
930,714✔
1084
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
180,572,275✔
1085
    line[read_size - 1] = '\0';
179,641,561✔
1086
    taosMemoryFree(pHistory->hist[pHistory->hend]);
179,641,561✔
1087
    pHistory->hist[pHistory->hend] = taosStrdup(line);
179,641,561✔
1088

1089
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
179,641,561✔
1090

1091
    if (pHistory->hend == pHistory->hstart) {
179,641,561✔
1092
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1093
    }
1094
  }
1095

1096
  taosMemoryFreeClear(line);
930,714✔
1097
  taosCloseFile(&pFile);
930,714✔
1098
  int64_t file_size;
928,784✔
1099
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
930,714✔
1100
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
×
1101
    if (pFile == NULL) return;
×
1102
    int32_t endIndex = pHistory->hstart;
×
1103
    if (endIndex != 0) {
×
1104
      endIndex = pHistory->hend;
×
1105
    }
1106
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
×
1107
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
×
1108
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
×
1109
    }
1110
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
×
1111

1112
    /* coverity[+retval] */
1113
    taosFsyncFile(pFile);
×
1114
    taosCloseFile(&pFile);
×
1115
  }
1116
  pHistory->hstart = pHistory->hend;
930,714✔
1117
}
1118

1119
void shellWriteHistory() {
977,138✔
1120
  SShellHistory *pHistory = &shell.history;
977,138✔
1121
  if (pHistory->hend == pHistory->hstart) return;
977,138✔
1122
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
977,007✔
1123
  if (pFile == NULL) return;
977,007✔
1124

1125
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
1,954,134✔
1126
    if (pHistory->hist[i] != NULL) {
977,127✔
1127
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
977,127✔
1128
      taosMemoryFree(pHistory->hist[i]);
977,127✔
1129
      pHistory->hist[i] = NULL;
977,127✔
1130
    }
1131
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
977,127✔
1132
  }
1133
  taosCloseFile(&pFile);
977,007✔
1134
}
1135

1136
void shellCleanupHistory() {
977,138✔
1137
  SShellHistory *pHistory = &shell.history;
977,138✔
1138
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
978,115,138✔
1139
    if (pHistory->hist[i] != NULL) {
977,138,000✔
1140
      taosMemoryFree(pHistory->hist[i]);
179,641,561✔
1141
      pHistory->hist[i] = NULL;
179,641,561✔
1142
    }
1143
  }
1144
}
977,138✔
1145

1146
void shellPrintError(TAOS_RES *tres, int64_t st) {
28,805,137✔
1147
  int code = taos_errno(tres);
28,805,137✔
1148
  int64_t et = taosGetTimestampUs();
28,805,137✔
1149
  (void)printf("\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), code, (et - st) / 1E6);
28,805,137✔
1150
  taos_free_result(tres);
28,805,137✔
1151

1152
  // tip
1153
  if (code == TSDB_CODE_MND_USER_PASSWORD_EXPIRED) {
28,805,137✔
1154
    (void)fprintf(stdout, "******************** TIPS ********************\n");
53✔
1155
    (void)fprintf(stdout, "Please reset your password using the `ALTER USER <user_name> PASS 'new_password'` command.\n");
53✔
1156
    (void)fprintf(stdout, "**********************************************\n");
53✔
1157
  }
1158
}
28,805,137✔
1159

1160
bool shellIsCommentLine(char *line) {
77,721,194✔
1161
  if (line == NULL) return true;
77,721,194✔
1162
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
77,721,194✔
1163
}
1164

1165
void shellSourceFile(const char *file) {
142,003✔
1166
  int32_t read_len = 0;
142,003✔
1167
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
142,003✔
1168
  size_t  cmd_len = 0;
142,003✔
1169
  char    fullname[PATH_MAX] = {0};
142,003✔
1170
  char    sourceFileCommand[PATH_MAX + 8] = {0};
142,003✔
1171

1172
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
142,003✔
1173
    tstrncpy(fullname, file, PATH_MAX);
×
1174
  }
1175

1176
  (void)sprintf(sourceFileCommand, "source %s;", fullname);
142,003✔
1177
  shellRecordCommandToHistory(sourceFileCommand);
142,003✔
1178

1179
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
142,003✔
1180
  if (pFile == NULL) {
142,003✔
1181
    (void)fprintf(stderr, "failed to open file %s\r\n", fullname);
20,708✔
1182
    taosMemoryFree(cmd);
20,708✔
1183
    return;
20,708✔
1184
  }
1185

1186
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
121,295✔
1187
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
78,933,792✔
1188
    if (cmd_len + read_len >= tsMaxSQLLength) {
78,812,497✔
1189
      (void)printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1190
             read_len);
1191
      cmd_len = 0;
×
1192
      memset(line, 0, tsMaxSQLLength + 1);
×
1193
      continue;
×
1194
    }
1195
    line[--read_len] = '\0';
78,812,497✔
1196

1197
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
78,812,497✔
1198
      continue;
1,091,742✔
1199
    }
1200

1201
    if (line[read_len - 1] == '\\') {
77,720,755✔
1202
      line[read_len - 1] = ' ';
×
1203
      memcpy(cmd + cmd_len, line, read_len);
×
1204
      cmd_len += read_len;
×
1205
      continue;
×
1206
    }
1207

1208
    if (line[read_len - 1] == '\r') {
77,720,755✔
1209
      line[read_len - 1] = ' ';
116,696✔
1210
    }
1211

1212
    memcpy(cmd + cmd_len, line, read_len);
77,720,755✔
1213
    (void)printf("%s%s\r\n", shell.info.promptHeader, cmd);
77,720,755✔
1214
    shellRunCommand(cmd, false);
77,720,755✔
1215
    memset(cmd, 0, tsMaxSQLLength);
77,720,755✔
1216
    cmd_len = 0;
77,720,755✔
1217
  }
1218

1219
  taosMemoryFree(cmd);
121,295✔
1220
  taosMemoryFreeClear(line);
121,295✔
1221
  taosCloseFile(&pFile);
121,295✔
1222
}
1223

1224
int32_t shellGetGrantInfo(char *buf) {
779✔
1225
  int32_t verType = TSDB_VERSION_UNKNOWN;
779✔
1226
  char    sinfo[256] = {0};
779✔
1227
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
779✔
1228
  strtok(sinfo, "\r\n");
779✔
1229

1230
#ifndef TD_ASTRA
1231
  char sql[] = "show grants";
779✔
1232

1233
  TAOS_RES *tres = taos_query(shell.conn, sql);
779✔
1234

1235
  int32_t code = taos_errno(tres);
779✔
1236
  if (code != TSDB_CODE_SUCCESS) {
779✔
1237
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1238
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1239
      (void)fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1240
    }
1241
    taos_free_result(tres);
×
1242
    return verType;
×
1243
  }
1244

1245
  int32_t num_fields = taos_field_count(tres);
779✔
1246
  if (num_fields == 0) {
779✔
1247
    (void)fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1248
    exit(0);
×
1249
  } else {
1250
    if (tres == NULL) {
779✔
1251
      (void)fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1252
      exit(0);
×
1253
    }
1254

1255
    TAOS_FIELD *fields = taos_fetch_fields(tres);
779✔
1256
    TAOS_ROW    row = taos_fetch_row(tres);
779✔
1257
    if (row == NULL) {
779✔
1258
      (void)fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1259
      exit(0);
×
1260
    }
1261
    char serverVersion[64] = {0};
779✔
1262
    char expiretime[32] = {0};
779✔
1263
    char expired[32] = {0};
779✔
1264

1265
    tstrncpy(serverVersion, row[0], 64);
779✔
1266
    memcpy(expiretime, row[1], fields[1].bytes);
779✔
1267
    memcpy(expired, row[2], fields[2].bytes);
779✔
1268

1269
    trimStr(serverVersion, "trial");
779✔
1270

1271
    if (strcmp(serverVersion, "community") == 0) {
779✔
1272
      verType = TSDB_VERSION_OSS;
×
1273
    } else if (strcmp(expiretime, "unlimited") == 0) {
779✔
1274
      verType = TSDB_VERSION_ENTERPRISE;
×
1275
      (void)sprintf(buf, "Server is %s %s. License will never expire.\r\n", serverVersion, sinfo);
×
1276
    } else {
1277
      verType = TSDB_VERSION_ENTERPRISE;
779✔
1278
      (void)sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
779✔
1279
    }
1280

1281
    taos_free_result(tres);
779✔
1282
  }
1283

1284
  (void)fprintf(stdout, "\r\n");
779✔
1285
#else
1286
  verType = TSDB_VERSION_ENTERPRISE;
1287
  (void)sprintf(buf, "Server is %s %s. License will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1288
#endif
1289
  return verType;
779✔
1290
}
1291

1292
#ifdef WINDOWS
1293
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1294
  tsem_post(&shell.cancelSem);
1295
  return TRUE;
1296
}
1297
#else
1298
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
624✔
1299
#endif
1300

1301
void shellCleanup(void *arg) { taosResetTerminalMode(); }
779✔
1302

1303
void *shellCancelHandler(void *arg) {
779✔
1304
  setThreadName("shellCancelHandler");
779✔
1305
  while (1) {
1306
    if (shell.exit == true) {
1,905✔
1307
      break;
779✔
1308
    }
1309

1310
    if (tsem_wait(&shell.cancelSem) != 0) {
1,126✔
1311
      taosMsleep(10);
×
1312
      continue;
×
1313
    }
1314

1315
    if (shell.conn) {
1,126✔
1316
      shellCmdkilled = true;
347✔
1317
      taos_kill_query(shell.conn);
347✔
1318
    }
1319

1320
#ifdef WINDOWS
1321
    (void)printf("\n%s", shell.info.promptHeader);
1322
#endif
1323
  }
1324

1325
  return NULL;
779✔
1326
}
1327

1328
#pragma GCC diagnostic push
1329
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1330

1331
void *shellThreadLoop(void *arg) {
779✔
1332
  setThreadName("shellThreadLoop");
779✔
1333
  taosGetOldTerminalMode();
779✔
1334
  taosThreadCleanupPush(shellCleanup, NULL);
779✔
1335

1336
  do {
1337
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
779✔
1338
    if (command == NULL) {
779✔
1339
      (void)printf("failed to malloc command\r\n");
×
1340
      break;
×
1341
    }
1342

1343
    do {
1344
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
1,126✔
1345
      taosSetTerminalMode();
1,126✔
1346

1347
      if (shellReadCommand(command) != 0) {
1,126✔
1348
        break;
347✔
1349
      }
1350

1351
      taosResetTerminalMode();
779✔
1352
    } while (shellRunCommand(command, true) == 0);
779✔
1353

1354
    taosMemoryFreeClear(command);
779✔
1355
    shellWriteHistory();
779✔
1356
    shellExit();
779✔
1357
  } while (0);
1358

1359
  taosThreadCleanupPop(1);
779✔
1360
  return NULL;
779✔
1361
}
1362

1363
bool inputTotpCode(char *totpCode) {
2,380✔
1364
  bool ret = true;
2,380✔
1365
  (void)printf("Please enter your TOTP code:");
2,380✔
1366
  if (scanf("%255s", totpCode) != 1) {
2,380✔
1367
    (void)fprintf(stderr, "TOTP code reading error\n");
177✔
1368
    ret = false;
177✔
1369
  }
1370
  if (EOF == getchar()) {
2,380✔
1371
    // tip
1372
    (void)fprintf(stdout, "getchar() return EOF\r\n");    
177✔
1373
  }
1374
  return ret;
2,380✔
1375
}
1376

1377
#pragma GCC diagnostic pop
1378

1379
TAOS *createConnect(SShellArgs *pArgs) {
981,872✔
1380
  char     show[256] = "\0";
981,872✔
1381
  char    *host = NULL;
981,872✔
1382
  uint16_t port = 0;
981,872✔
1383
  char    *user = NULL;
981,872✔
1384
  char    *pwd = NULL;
981,872✔
1385
  TAOS    *taos = NULL;
981,872✔
1386

1387
  // set mode
1388
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
981,872✔
1389
    // websocket
1390
    memcpy(show, pArgs->dsn, 20);
1,484✔
1391
    memcpy(show + 20, "...", 3);
1,484✔
1392
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
1,484✔
1393

1394
    // connect dsn
1395
    taos = taos_connect_with_dsn(pArgs->dsn);
1,484✔
1396
  } else {
1397
    host = (char *)pArgs->host;
980,388✔
1398
    user = (char *)pArgs->user;
980,388✔
1399
    pwd = pArgs->password;
980,388✔
1400

1401
    if (pArgs->port_inputted) {
980,388✔
1402
      port = pArgs->port;
819✔
1403
    } else {
1404
      port = defaultPort(pArgs->connMode, pArgs->dsn);
979,569✔
1405
    }
1406

1407
    (void)sprintf(show, "host:%s port:%d ", host, port);
980,388✔
1408

1409
    // connect normal
1410
    if (pArgs->auth) {
980,388✔
1411
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
206✔
1412
    } else {
1413
#ifdef TD_ENTERPRISE 
1414
      if (strlen(pArgs->token) > 0) {
980,182✔
1415
        // token
1416
        (void)printf("Connect with token ...");
2,591✔
1417
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,591✔
1418
        if (taos != NULL) {
2,591✔
1419
          (void)printf("... [ OK ]\n");
1,204✔
1420
          return taos;
1,204✔
1421
        }
1422
        (void)printf("... [ FAILED ]\n");
1,387✔
1423
        return NULL;
1,387✔
1424
      }
1425
#endif      
1426
      taos = taos_connect(host, user, pwd, pArgs->database, port);
977,591✔
1427
    }
1428

1429
    if (taos == NULL) {
977,797✔
1430
      // failed
1431
      int code = taos_errno(NULL);
4,612✔
1432
      if (code == TSDB_CODE_MND_WRONG_TOTP_CODE) {
4,612✔
1433
         // totp
1434
        char totpCode[TSDB_USER_PASSWORD_LONGLEN];
2,380✔
1435
        memset(totpCode, 0, sizeof(totpCode));  
2,380✔
1436
        if (inputTotpCode(totpCode)) {
2,380✔
1437
          (void)printf("Connect with TOTP code:%s ...", totpCode);
2,203✔
1438
          taos = taos_connect_totp(host, user, pwd, totpCode, pArgs->database, port);
2,203✔
1439
          if (taos != NULL) {
2,203✔
1440
            (void)printf("... [ OK ]\n");
1,491✔
1441
            return taos;
1,491✔
1442
          }
1443
          (void)printf("... [ FAILED ]\n");
712✔
1444
          return NULL;
712✔
1445
        }
1446
      }
1447
      // token
1448
    }
1449
  }
1450

1451
  return taos;
977,078✔
1452
}
1453

1454
int32_t shellExecute(int argc, char *argv[]) {
981,872✔
1455
  int32_t code = 0;
981,872✔
1456
  (void)printf(shell.info.clientVersion, shell.info.cusName,
1,963,744✔
1457
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
981,872✔
1458
         taos_get_client_info(), shell.info.cusName);
1459
  fflush(stdout);
981,872✔
1460

1461
  SShellArgs *pArgs = &shell.args;
981,872✔
1462
  shell.conn = createConnect(pArgs);
981,872✔
1463

1464
  if (shell.conn == NULL) {
981,872✔
1465
    (void)printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
4,734✔
1466
           ERROR_CODE_DETAIL);
1467
    fflush(stdout);
4,734✔
1468
    return -1;
4,734✔
1469
  }
1470

1471
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
977,138✔
1472
  shellSetConn(shell.conn, runOnce);
977,138✔
1473
  shellReadHistory();
977,138✔
1474

1475
  if (shell.args.is_bi_mode) {
977,138✔
1476
    // need set bi mode
1477
    (void)printf("Set BI mode is true.\n");
467✔
1478
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
467✔
1479
  }
1480

1481
  if (runOnce) {
977,138✔
1482
    if (pArgs->commands != NULL) {
976,359✔
1483
      (void)printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
834,476✔
1484
      char *cmd = taosStrdup(pArgs->commands);
834,476✔
1485
      shellRunCommand(cmd, true);
834,476✔
1486
      taosMemoryFree(cmd);
834,476✔
1487
    }
1488

1489
    if (pArgs->file[0] != 0) {
976,359✔
1490
      shellSourceFile(pArgs->file);
141,883✔
1491
    }
1492

1493
    taos_close(shell.conn);
976,359✔
1494

1495
    shellWriteHistory();
976,359✔
1496
    shellCleanupHistory();
976,359✔
1497
    return 0;
976,359✔
1498
  }
1499

1500
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
779✔
1501
    (void)printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1502
    return code;
×
1503
  }
1504

1505
  TdThread spid = {0};
779✔
1506
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
779✔
1507

1508
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
779✔
1509
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
779✔
1510
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
779✔
1511

1512
  char    buf[512] = {0};
779✔
1513
  int32_t verType = shellGetGrantInfo(buf);
779✔
1514
#ifndef WINDOWS
1515
  printfIntroduction(verType);
779✔
1516
#else
1517
  if (verType == TSDB_VERSION_OSS) {
1518
    showAD(false);
1519
  }
1520
#endif
1521
  // printf version
1522
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
779✔
1523
    (void)printf("%s\n", buf);
779✔
1524
  }
1525

1526
  while (1) {
1527
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
779✔
1528
    taosThreadJoin(shell.pid, NULL);
779✔
1529
    taosThreadClear(&shell.pid);
779✔
1530
    if (shell.exit) {
779✔
1531
      tsem_post(&shell.cancelSem);
779✔
1532
      break;
779✔
1533
    }
1534
  }
1535

1536
  if (verType == TSDB_VERSION_OSS) {
779✔
1537
    showAD(true);
×
1538
  }
1539

1540
  taosThreadJoin(spid, NULL);
779✔
1541

1542
  shellCleanupHistory();
779✔
1543
  taos_kill_query(shell.conn);
779✔
1544
  taos_close(shell.conn);
779✔
1545

1546
  TAOS_RETURN(code);
779✔
1547
}
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