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

taosdata / TDengine / #5013

03 Apr 2026 03:59PM UTC coverage: 72.317% (+0.01%) from 72.305%
#5013

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

13131 existing lines in 160 files now uncovered.

257489 of 356056 relevant lines covered (72.32%)

129893134.08 hits per line

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

82.65
/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,075,085✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
230,209,903✔
72
    if (c != ' ' && c != '\t' && c != ';') {
154,889,188✔
73
      return false;
154,754,370✔
74
    }
75
  }
76
  return true;
75,320,715✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
152,764,792✔
80
  shellCmdkilled = false;
152,764,792✔
81

82
  if (shellIsEmptyCommand(command)) {
152,764,792✔
83
    return 0;
75,320,715✔
84
  }
85

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

90
  if (shellRegexMatch(command, "^[\t ]*clear[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
77,443,532✔
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;]*$",
77,443,532✔
103
                      REG_EXTENDED | REG_ICASE)) {
104
    strtok(command, " \t");
7,331✔
105
    strtok(NULL, " \t");
7,331✔
106
    char *p = strtok(NULL, " \t");
7,331✔
107
    if (strncasecmp(p, "default", 7) == 0) {
7,331✔
108
      shell.args.displayWidth = SHELL_DEFAULT_MAX_BINARY_DISPLAY_WIDTH;
×
109
    } else {
110
      int32_t displayWidth = atoi(p);
7,331✔
111
      displayWidth = TRANGE(displayWidth, 1, 10 * 1024);
7,331✔
112
      shell.args.displayWidth = displayWidth;
7,331✔
113
    }
114
    return 0;
7,331✔
115
  }
116

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
77,310,175✔
174

175
  char quote = 0, *cmd = command;
77,310,175✔
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,058✔
179
      continue;
1,058✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
14,044,079✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
14,044,079✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
75,455,162✔
188
      *command = 0;
75,455,162✔
189
      if (shellRunSingleCommand(cmd) < 0) {
75,455,162✔
190
        return -1;
545✔
191
      }
192
      *command = c;
75,454,617✔
193
      cmd = command;
75,454,617✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
77,309,630✔
197
}
198

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

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

209
  char *p = (char *)pstr + len - 2;
77,436,096✔
210
  if (strcmp(p, "\\G") == 0) {
77,436,096✔
211
    return p;
6,182✔
212
  }
213

214
  p = (char *)pstr + len - 3;
77,429,914✔
215
  if (strcmp(p, "\\G;") == 0) {
77,429,914✔
216
    return p;
124,402✔
217
  }
218

219
  return NULL;
77,305,512✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
77,436,096✔
230
    fname = sptr + 2;
691,693✔
231
    while (*fname == ' ') fname++;
1,383,151✔
232
    *sptr = '\0';
691,693✔
233

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

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

245
  st = taosGetTimestampUs();
77,436,096✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
77,436,096✔
248
  if (taos_errno(pSql)) {
77,436,096✔
249
    shellPrintError(pSql, st);
28,208,306✔
250
    return;
28,208,306✔
251
  }
252

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

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

259
    taos_free_result(pSql);
21,483✔
260

261
    return;
21,483✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
49,206,307✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,206,307✔
267
    pre = "Delete OK";
55✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,206,252✔
269
    pre = "Insert OK";
5,903✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,200,349✔
271
    pre = "Create OK";
5,606✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
49,194,743✔
273
    pre = "Drop OK";
995✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
49,206,307✔
277
  if (pFields != NULL) {  // select and show kinds of commands
49,206,307✔
278
    int32_t error_no = 0;
47,805,402✔
279

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

286
    et = taosGetTimestampUs();
47,805,402✔
287
    if (error_no == 0) {
47,805,402✔
288
      (void)printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
47,805,402✔
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);
47,805,402✔
294
  } else {
295
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,400,905✔
296
    taos_free_result(pSql);
1,400,905✔
297
    et = taosGetTimestampUs();
1,400,905✔
298
    (void)printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,400,905✔
299

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

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

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

313
  time_t  tt;
794,311,511✔
314
  int32_t ms = 0;
794,783,698✔
315
  if (precision == TSDB_TIME_PRECISION_NANO) {
794,783,698✔
316
    tt = (time_t)(val / 1000000000);
1,050✔
317
    ms = val % 1000000000;
1,050✔
318
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
794,782,648✔
319
    tt = (time_t)(val / 1000000);
1,050✔
320
    ms = val % 1000000;
1,050✔
321
  } else {
322
    tt = (time_t)(val / 1000);
794,781,598✔
323
    ms = val % 1000;
794,781,598✔
324
  }
325

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

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

343
  if (precision == TSDB_TIME_PRECISION_NANO) {
794,783,698✔
344
    (void)sprintf(buf + pos, ".%09d", ms);
1,050✔
345
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
794,782,648✔
346
    (void)sprintf(buf + pos, ".%06d", ms);
1,050✔
347
  } else {
348
    (void)sprintf(buf + pos, ".%03d", ms);
794,781,598✔
349
  }
350

351
  return buf;
794,783,698✔
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,565,525,177✔
364
  if (val == NULL) {
1,565,525,177✔
365
    taosFprintfFile(pFile, "NULL");
13,420,711✔
366
    return;
13,420,711✔
367
  }
368

369
  char    quotationStr[2] = {'"', 0};
1,552,104,466✔
370
  int32_t width;
371

372
  int n = 0;
1,552,104,466✔
373
#define LENGTH 64
374
  char buf[LENGTH] = {0};
1,552,104,466✔
375
  switch (field->type) {
1,552,104,466✔
376
    case TSDB_DATA_TYPE_BOOL:
297,142,433✔
377
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
297,142,433✔
378
      break;
297,142,433✔
379
    case TSDB_DATA_TYPE_TINYINT:
19,794,032✔
380
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
19,794,032✔
381
      break;
19,794,032✔
382
    case TSDB_DATA_TYPE_UTINYINT:
761,964✔
383
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
761,964✔
384
      break;
761,964✔
385
    case TSDB_DATA_TYPE_SMALLINT:
763,508✔
386
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
763,508✔
387
      break;
763,508✔
388
    case TSDB_DATA_TYPE_USMALLINT:
763,724✔
389
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
763,724✔
390
      break;
763,724✔
391
    case TSDB_DATA_TYPE_INT:
150,238,276✔
392
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
150,238,276✔
393
      break;
150,238,276✔
394
    case TSDB_DATA_TYPE_UINT:
2,283,792✔
395
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
2,283,792✔
396
      break;
2,283,792✔
397
    case TSDB_DATA_TYPE_BIGINT:
183,869,888✔
398
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
183,869,888✔
399
      break;
183,869,888✔
400
    case TSDB_DATA_TYPE_UBIGINT:
1,520,284✔
401
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
1,520,284✔
402
      break;
1,520,284✔
403
    case TSDB_DATA_TYPE_FLOAT:
18,276,774✔
404
      width = SHELL_FLOAT_WIDTH;
18,276,774✔
405
      if (tsEnableScience) {
18,276,774✔
406
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
407
      } else {
408
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
18,276,774✔
409
        if (n > SHELL_FLOAT_WIDTH) {
18,276,774✔
UNCOV
410
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
411
        } else {
412
          taosFprintfFile(pFile, "%s", buf);
18,276,774✔
413
        }
414
      }
415
      break;
18,276,774✔
416
    case TSDB_DATA_TYPE_DOUBLE:
248,458,085✔
417
      width = SHELL_DOUBLE_WIDTH;
248,458,085✔
418
      if (tsEnableScience) {
248,458,085✔
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,458,085✔
423
        if (n > SHELL_DOUBLE_WIDTH) {
248,458,085✔
424
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
129,110,448✔
425
        } else {
426
          taosFprintfFile(pFile, "%s", buf);
119,347,637✔
427
        }
428
      }
429
      break;
248,458,085✔
430
    case TSDB_DATA_TYPE_BINARY:
237,105,921✔
431
    case TSDB_DATA_TYPE_NCHAR:
432
    case TSDB_DATA_TYPE_JSON: {
433
      int32_t bufIndex = 0;
237,105,921✔
434
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
237,105,921✔
435
      if (tmp == NULL) break;
237,105,921✔
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;
237,105,921✔
445

446
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
237,105,921✔
447
      taosMemoryFree(tmp);
237,105,921✔
448
    } break;
237,105,921✔
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:
175,932,381✔
468
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
175,932,381✔
469
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
175,932,381✔
470
      break;
175,932,381✔
471
    case TSDB_DATA_TYPE_DECIMAL64:
215,193,404✔
472
    case TSDB_DATA_TYPE_DECIMAL:
473
      taosFprintfFile(pFile, "%s", val);
215,193,404✔
474
      break;
215,193,404✔
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) {
666,920✔
493
  char fullname[PATH_MAX] = {0};
666,920✔
494
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
666,920✔
495
    tstrncpy(fullname, fname, PATH_MAX);
×
496
  }
497

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

503
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
666,920✔
504
  if (pFile == NULL) {
666,920✔
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);
666,920✔
510
  int32_t     num_fields = taos_num_fields(tres);
666,920✔
511
  int32_t     precision = taos_result_precision(tres);
666,920✔
512

513
  for (int32_t col = 0; col < num_fields; col++) {
1,645,486✔
514
    if (col > 0) {
978,566✔
515
      taosFprintfFile(pFile, ",");
311,646✔
516
    }
517
    taosFprintfFile(pFile, "%s", fields[col].name);
978,566✔
518
  }
519
  taosFprintfFile(pFile, "\r\n");
666,920✔
520

521
  int64_t numOfRows = 0;
666,920✔
522
  do {
523
    int32_t *length = taos_fetch_lengths(tres);
717,317,608✔
524
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
525
      if (i > 0) {
1,565,525,177✔
526
        taosFprintfFile(pFile, ",");
848,207,569✔
527
      }
528
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,565,525,177✔
529
    }
530
    taosFprintfFile(pFile, "\r\n");
717,317,608✔
531

532
    numOfRows++;
717,317,608✔
533
    row = taos_fetch_row(tres);
717,317,608✔
534
  } while (row != NULL);
717,317,608✔
535

536
  taosCloseFile(&pFile);
666,920✔
537

538
  return numOfRows;
666,920✔
539
}
540

541
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
323,919,853✔
542
  TdWchar tail[3];
323,715,098✔
543
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
323,919,853✔
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;
145,108✔
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,021,753✔
565
    }
566

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

572
    totalCols += w;
2,147,483,647✔
573
    if (totalCols > width) {
2,147,483,647✔
574
      break;
1,042,006✔
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,362,343✔
581
      tailLen++;
3,362,343✔
582
    }
583
  }
584

585
  if (totalCols > width) {
323,919,853✔
586
    // width could be 1 or 2, so printf("...") cannot be used
587
    for (int32_t i = 0; i < 3; i++) {
4,168,024✔
588
      if (cols >= width) {
3,126,018✔
589
        break;
×
590
      }
591
      putchar('.');
3,126,018✔
592
      ++cols;
3,126,018✔
593
    }
594
  } else {
595
    for (int32_t i = 0; i < tailLen; i++) {
323,118,924✔
596
      (void)printf("%lc", tail[i]);
241,077✔
597
    }
598
    cols = totalCols;
322,877,847✔
599
  }
600

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

606
void shellPrintString(const char *str, int32_t width) {
1,530,439,914✔
607
  int32_t len = strlen(str);
1,530,439,914✔
608

609
  if (width == 0) {
1,530,439,914✔
610
    (void)printf("%s", str);
×
611
  } else if (len > width) {
1,530,439,914✔
612
    if (width <= 3) {
7,414✔
613
      (void)printf("%.*s.", width - 1, str);
6,984✔
614
    } else {
615
      (void)printf("%.*s...", width - 3, str);
430✔
616
    }
617
  } else {
618
    (void)printf("%s%*.s", str, width - len, "");
1,530,432,500✔
619
  }
620
}
1,530,439,914✔
621

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

628
  int32_t code = TSDB_CODE_FAILED;
860✔
629

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

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

643
  shellPrintString(outputWKT, width);
860✔
644

645
  geosFreeBuffer(outputWKT);
860✔
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,296,827,738✔
651
    return;
1,296,827,738✔
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:
233,611,316✔
659
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
233,611,316✔
660
      break;
233,611,316✔
661
    case TSDB_DATA_TYPE_TINYINT:
63,418,417✔
662
      (void)printf("%*d", width, *((int8_t *)val));
63,418,417✔
663
      break;
63,418,417✔
664
    case TSDB_DATA_TYPE_UTINYINT:
136,689,095✔
665
      (void)printf("%*u", width, *((uint8_t *)val));
136,689,095✔
666
      break;
136,689,095✔
667
    case TSDB_DATA_TYPE_SMALLINT:
49,789,006✔
668
      (void)printf("%*d", width, *((int16_t *)val));
49,789,006✔
669
      break;
49,789,006✔
670
    case TSDB_DATA_TYPE_USMALLINT:
120,542,720✔
671
      (void)printf("%*u", width, *((uint16_t *)val));
120,542,720✔
672
      break;
120,542,720✔
673
    case TSDB_DATA_TYPE_INT:
160,480,033✔
674
      (void)printf("%*d", width, *((int32_t *)val));
160,480,033✔
675
      break;
160,480,033✔
676
    case TSDB_DATA_TYPE_UINT:
169,627,313✔
677
      (void)printf("%*u", width, *((uint32_t *)val));
169,627,313✔
678
      break;
169,627,313✔
679
    case TSDB_DATA_TYPE_BIGINT:
432,150,544✔
680
      (void)printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
432,150,544✔
681
      break;
432,150,544✔
682
    case TSDB_DATA_TYPE_UBIGINT:
176,659,042✔
683
      (void)printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
176,659,042✔
684
      break;
176,659,042✔
685
    case TSDB_DATA_TYPE_FLOAT:
78,853,045✔
686
      width = width >= LENGTH ? LENGTH - 1 : width;
78,853,045✔
687
      if (tsEnableScience) {
78,853,045✔
688
        (void)printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
689
      } else {
690
        (void)snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
78,853,045✔
691
        (void)printf("%s", buf);
78,853,045✔
692
      }
693
      break;
78,853,045✔
694
    case TSDB_DATA_TYPE_DOUBLE:
337,733,080✔
695
      width = width >= LENGTH ? LENGTH - 1 : width;
337,733,080✔
696
      if (tsEnableScience) {
337,733,080✔
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));
337,733,080✔
701
        (void)printf("%*s", width, buf);
337,733,080✔
702
      }
703
      break;
337,733,080✔
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,919,853✔
715
    case TSDB_DATA_TYPE_NCHAR:
716
    case TSDB_DATA_TYPE_JSON:
717
      shellPrintNChar(val, length, width);
323,919,853✔
718
      break;
323,919,853✔
719
    case TSDB_DATA_TYPE_GEOMETRY:
860✔
720
      shellPrintGeometry(val, length, width);
860✔
721
      break;
860✔
722
    case TSDB_DATA_TYPE_TIMESTAMP:
618,872,667✔
723
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
618,872,667✔
724
      (void)printf("%s", buf);
618,872,667✔
725
      break;
618,872,667✔
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:
130✔
739
    case TSDB_DATA_TYPE_DECIMAL64:
740
      (void)printf("%*s", width, val);
130✔
741
    default:
130✔
742
      break;
130✔
743
  }
744
}
745

746
// show whole result for this query return true, like limit or describe
747
bool shellIsShowWhole(const char *sql) {
420✔
748
  // limit
749
  char * p = taosStrCaseStr(sql, " limit ");
420✔
750
  if (p != NULL) {
420✔
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, ")");
210✔
753
    if (p1 == NULL) {
210✔
754
      return true;
70✔
755
    }
756
    if (taosStrCaseStr(p1 + 1, " limit ")) {
140✔
757
      return true;
70✔
758
    }
759
  }
760
  // describe
761
  if (taosStrCaseStr(sql, "describe ") != NULL) {
280✔
762
    return true;
70✔
763
  }
764
  // desc
765
  if (taosStrCaseStr(sql, "desc ") != NULL) {
210✔
766
    return true;
70✔
767
  }
768
  // show
769
  if (taosStrCaseStr(sql, "show ") != NULL) {
140✔
770
    return true;
×
771
  }
772
  // explain
773
  if (taosStrCaseStr(sql, "explain ") != NULL) {
140✔
774
    return true;
70✔
775
  }
776

777
  return false;
70✔
778
}
779

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

786
  return false;
70✔
787
}
788

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

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

799
  dump_info->resShowMaxNum = UINT64_MAX;
47,138,482✔
800

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

805
  if (vertical) {
47,138,482✔
806
    dump_info->maxColNameLen = 0;
121,314✔
807
    for (int32_t col = 0; col < dump_info->numFields; col++) {
249,622✔
808
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
128,308✔
809
      if (len > dump_info->maxColNameLen) {
128,308✔
810
        dump_info->maxColNameLen = len;
123,403✔
811
      }
812
    }
813
  } else {
814
    for (int32_t col = 0; col < dump_info->numFields; col++) {
135,864,962✔
815
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
88,847,794✔
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,017,168✔
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,017,168✔
821
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
822
    }
823
  }
824
}
47,138,482✔
825

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

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

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

839
    int32_t *length = taos_fetch_lengths(tres);
3,067,149✔
840

841
    for (int32_t i = 0; i < dump_info->numFields; i++) {
6,147,593✔
842
      TAOS_FIELD *field = dump_info->fields + i;
3,080,444✔
843

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

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

852
    numOfPintRows++;
3,067,149✔
853
    numOfPrintRowsThisOne++;
3,067,149✔
854

855
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,067,149✔
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,067,149✔
867
      return;
121,314✔
868
    }
869

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

875
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
88,847,794✔
876
  int32_t width = (int32_t)strlen(field->name);
88,847,794✔
877

878
  switch (field->type) {
88,847,794✔
879
    case TSDB_DATA_TYPE_NULL:
×
880
      return TMAX(4, width);  // null
×
881
    case TSDB_DATA_TYPE_BOOL:
19,948,151✔
882
      return TMAX(5, width);  // 'false'
19,948,151✔
883

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

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

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

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

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

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

906
    case TSDB_DATA_TYPE_BINARY:
6,151,634✔
907
    case TSDB_DATA_TYPE_GEOMETRY:
908
      if (field->bytes > shell.args.displayWidth) {
6,151,634✔
909
        return TMAX(shell.args.displayWidth, width);
4,162,905✔
910
      } else {
911
        return TMAX(field->bytes + 2, width);
1,988,729✔
912
      }
913
    case TSDB_DATA_TYPE_VARBINARY: {
792✔
914
      int32_t bytes = field->bytes * 2 + 2;
792✔
915
      if (bytes > shell.args.displayWidth) {
792✔
916
        return TMAX(shell.args.displayWidth, width);
×
917
      } else {
918
        return TMAX(bytes + 2, width);
792✔
919
      }
920
    }
921
    case TSDB_DATA_TYPE_NCHAR:
4,102,762✔
922
    case TSDB_DATA_TYPE_JSON: {
923
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
4,102,762✔
924
      if (bytes > shell.args.displayWidth) {
4,102,762✔
925
        return TMAX(shell.args.displayWidth, width);
4,102,762✔
926
      } else {
927
        return TMAX(bytes + 2, width);
×
928
      }
929
    }
930

931
    case TSDB_DATA_TYPE_TIMESTAMP:
20,988,039✔
932
      if (shell.args.is_raw_time) {
20,988,039✔
933
        return TMAX(14, width);
408✔
934
      }
935
      if (precision == TSDB_TIME_PRECISION_NANO) {
20,987,631✔
936
        return TMAX(29, width);
105✔
937
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
20,987,526✔
938
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
105✔
939
      } else {
940
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
20,987,421✔
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:
65✔
953
      return TMAX(width, 20);
65✔
954
    case TSDB_DATA_TYPE_DECIMAL:
65✔
955
      return TMAX(width, 40);
65✔
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,555,027✔
964
  int32_t rowWidth = 0;
33,555,027✔
965
  for (int32_t col = 0; col < num_fields; col++) {
104,117,578✔
966
    TAOS_FIELD *field = fields + col;
70,562,551✔
967
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
70,562,551✔
968
    int32_t     left = padding / 2;
70,562,551✔
969
    (void)printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
70,562,551✔
970
    rowWidth += width[col] + 3;
70,562,551✔
971
  }
972

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

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

989
  int64_t numOfPintRows = dump_info->numOfAllRows;
34,992,606✔
990
  int     numOfPrintRowsThisOne = 0;
34,992,606✔
991
  if (numOfPintRows == 0) {
34,992,606✔
992
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
33,555,027✔
993
  }
994

995
  while (row != NULL) {
599,401,392✔
996
    int32_t *length = taos_fetch_lengths(tres);
599,401,392✔
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,401,392✔
1005
    putchar('\n');
599,401,392✔
1006

1007
    numOfPintRows++;
599,401,392✔
1008
    numOfPrintRowsThisOne++;
599,401,392✔
1009

1010
    if (numOfPintRows == dump_info->resShowMaxNum) {
599,401,392✔
1011
      (void)printf("\r\n");
70✔
1012
      (void)printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
70✔
1013
      if (shellIsShowQuery(dump_info->sql)) {
70✔
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");
70✔
1017
        (void)printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
70✔
1018
      }
1019
      (void)printf("\r\n");
70✔
1020
      (void)printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
70✔
1021
      (void)printf("\r\n");
70✔
1022
      return;
70✔
1023
    }
1024

1025
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
599,401,322✔
1026
      return;
34,992,536✔
1027
    }
1028

1029
    row = taos_fetch_row(tres);
564,408,786✔
1030
  }
1031
  return;
×
1032
}
1033

1034
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
82,252,402✔
1035
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
82,252,402✔
1036
  if (num_of_rows > 0) {
82,252,402✔
1037
    dump_info->numOfRows = num_of_rows;
35,113,920✔
1038
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
35,113,920✔
1039
      if (dump_info->vertical) {
35,113,920✔
1040
        shellVerticalPrintResult(tres, dump_info);
121,314✔
1041
      } else {
1042
        shellHorizontalPrintResult(tres, dump_info);
34,992,606✔
1043
      }
1044
    }
1045
    dump_info->numOfAllRows += num_of_rows;
35,113,920✔
1046
    if (!shellCmdkilled) {
35,113,920✔
1047
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
35,113,920✔
1048
    } else {
1049
      tsem_post(&dump_info->sem);
×
1050
    }
1051
  } else {
1052
    if (num_of_rows < 0) {
47,138,482✔
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,138,482✔
1056
  }
1057
}
82,252,336✔
1058

1059
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
47,805,402✔
1060
  int64_t num_of_rows = 0;
47,805,402✔
1061
  if (fname != NULL) {
47,805,402✔
1062
    num_of_rows = shellDumpResultToFile(fname, tres);
666,920✔
1063
  } else {
1064
    tsDumpInfo dump_info;
42,482,966✔
1065
    if (!shellCmdkilled) {
47,138,482✔
1066
      init_dump_info(&dump_info, tres, sql, vertical);
47,138,482✔
1067
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
47,138,482✔
1068
      tsem_wait(&dump_info.sem);
47,138,482✔
1069
      num_of_rows = dump_info.numOfAllRows;
47,138,482✔
1070
    }
1071
  }
1072

1073
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
47,805,402✔
1074
  return num_of_rows;
47,805,402✔
1075
}
1076

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

1082
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
996,632✔
1083
  int32_t read_size = 0;
996,632✔
1084
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
183,344,833✔
1085
    line[read_size - 1] = '\0';
182,348,201✔
1086
    taosMemoryFree(pHistory->hist[pHistory->hend]);
182,348,201✔
1087
    pHistory->hist[pHistory->hend] = taosStrdup(line);
182,348,201✔
1088

1089
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
182,348,201✔
1090

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

1096
  taosMemoryFreeClear(line);
996,632✔
1097
  taosCloseFile(&pFile);
996,632✔
1098
  int64_t file_size;
994,715✔
1099
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
996,632✔
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;
996,632✔
1117
}
1118

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

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

1136
void shellCleanupHistory() {
1,043,889✔
1137
  SShellHistory *pHistory = &shell.history;
1,043,889✔
1138
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,044,932,889✔
1139
    if (pHistory->hist[i] != NULL) {
1,043,889,000✔
1140
      taosMemoryFree(pHistory->hist[i]);
182,348,201✔
1141
      pHistory->hist[i] = NULL;
182,348,201✔
1142
    }
1143
  }
1144
}
1,043,889✔
1145

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

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

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

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

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

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

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

1186
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
125,669✔
1187
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
77,653,722✔
1188
    if (cmd_len + read_len >= tsMaxSQLLength) {
77,528,053✔
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';
77,528,053✔
1196

1197
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
77,528,053✔
1198
      continue;
1,115,310✔
1199
    }
1200

1201
    if (line[read_len - 1] == '\\') {
76,412,743✔
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') {
76,412,743✔
1209
      line[--read_len] = '\0';
117,277✔
1210
    }
1211

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

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

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

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

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

1235
  int32_t code = taos_errno(tres);
758✔
1236
  if (code != TSDB_CODE_SUCCESS) {
758✔
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);
758✔
1246
  if (num_fields == 0) {
758✔
1247
    (void)fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1248
    exit(0);
×
1249
  } else {
1250
    if (tres == NULL) {
758✔
1251
      (void)fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1252
      exit(0);
×
1253
    }
1254

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

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

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

1271
    if (strcmp(serverVersion, "community") == 0) {
758✔
1272
      verType = TSDB_VERSION_OSS;
×
1273
    } else if (strcmp(expiretime, "unlimited") == 0) {
758✔
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;
758✔
1278
      (void)sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
758✔
1279
    }
1280

1281
    taos_free_result(tres);
758✔
1282
  }
1283

1284
  (void)fprintf(stdout, "\r\n");
758✔
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;
758✔
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); }
676✔
1299
#endif
1300

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

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

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

1315
    if (shell.conn) {
962✔
1316
      shellCmdkilled = true;
204✔
1317
      taos_kill_query(shell.conn);
204✔
1318
    }
1319

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

1325
  return NULL;
758✔
1326
}
1327

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

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

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

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

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

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

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

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

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

1377
#pragma GCC diagnostic pop
1378

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

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

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

1401
    if (pArgs->port_inputted) {
1,047,118✔
1402
      port = pArgs->port;
792✔
1403
    } else {
1404
      port = defaultPort(pArgs->connMode, pArgs->dsn);
1,046,326✔
1405
    }
1406

1407
    (void)sprintf(show, "host:%s port:%d ", host, port);
1,047,118✔
1408

1409
    // connect normal
1410
    if (pArgs->auth) {
1,047,118✔
1411
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
192✔
1412
    } else {
1413
#ifdef TD_ENTERPRISE 
1414
      if (strlen(pArgs->token) > 0) {
1,046,926✔
1415
        // token
1416
        (void)printf("Connect with token ...");
2,584✔
1417
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,584✔
1418
        if (taos != NULL) {
2,584✔
1419
          (void)printf("... [ OK ]\n");
1,200✔
1420
          return taos;
1,200✔
1421
        }
1422
        (void)printf("... [ FAILED ]\n");
1,384✔
1423
        return NULL;
1,384✔
1424
      }
1425
#endif      
1426
      taos = taos_connect(host, user, pwd, pArgs->database, port);
1,044,342✔
1427
    }
1428

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

1451
  return taos;
1,043,743✔
1452
}
1453

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

1461
  SShellArgs *pArgs = &shell.args;
1,048,474✔
1462
  shell.conn = createConnect(pArgs);
1,048,474✔
1463

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

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

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

1481
  if (runOnce) {
1,043,889✔
1482
    if (pArgs->commands != NULL) {
1,043,131✔
1483
      (void)printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
896,792✔
1484
      char *cmd = taosStrdup(pArgs->commands);
896,792✔
1485
      shellRunCommand(cmd, true);
896,792✔
1486
      taosMemoryFree(cmd);
896,792✔
1487
    }
1488

1489
    if (pArgs->file[0] != 0) {
1,043,131✔
1490
      shellSourceFile(pArgs->file);
146,339✔
1491
    }
1492

1493
    taos_close(shell.conn);
1,043,131✔
1494

1495
    shellWriteHistory();
1,043,131✔
1496
    shellCleanupHistory();
1,043,131✔
1497
    return 0;
1,043,131✔
1498
  }
1499

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

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

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

1512
  char    buf[512] = {0};
758✔
1513
  int32_t verType = shellGetGrantInfo(buf);
758✔
1514
#ifndef WINDOWS
1515
  printfIntroduction(verType);
758✔
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) {
758✔
1523
    (void)printf("%s\n", buf);
758✔
1524
  }
1525

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

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

1540
  taosThreadJoin(spid, NULL);
758✔
1541

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

1546
  TAOS_RETURN(code);
758✔
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