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

taosdata / TDengine / #5005

26 Mar 2026 12:51PM UTC coverage: 72.152% (-0.2%) from 72.338%
#5005

push

travis-ci

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

512 of 851 new or added lines in 47 files covered. (60.16%)

6189 existing lines in 147 files now uncovered.

253282 of 351039 relevant lines covered (72.15%)

132156710.33 hits per line

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

80.95
/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) {
224,568,409✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
224,704,118✔
72
    if (c != ' ' && c != '\t' && c != ';') {
151,189,206✔
73
      return false;
151,053,497✔
74
    }
75
  }
76
  return true;
73,514,912✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
149,107,712✔
80
  shellCmdkilled = false;
149,107,712✔
81

82
  if (shellIsEmptyCommand(command)) {
149,107,712✔
83
    return 0;
73,514,912✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
75,592,800✔
87
    return -1;
526✔
88
  }
89

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
75,460,590✔
174

175
  char quote = 0, *cmd = command;
75,460,590✔
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,048✔
179
      continue;
1,048✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
13,357,130✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
13,357,130✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
73,647,648✔
188
      *command = 0;
73,647,648✔
189
      if (shellRunSingleCommand(cmd) < 0) {
73,647,648✔
190
        return -1;
526✔
191
      }
192
      *command = c;
73,647,122✔
193
      cmd = command;
73,647,122✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
75,460,064✔
197
}
198

199
char *strendG(const char *pstr) {
75,584,932✔
200
  if (pstr == NULL) {
75,584,932✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
75,584,932✔
205
  if (len < 4) {
75,584,932✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
75,584,932✔
210
  if (strcmp(p, "\\G") == 0) {
75,584,932✔
211
    return p;
6,201✔
212
  }
213

214
  p = (char *)pstr + len - 3;
75,578,731✔
215
  if (strcmp(p, "\\G;") == 0) {
75,578,731✔
216
    return p;
123,412✔
217
  }
218

219
  return NULL;
75,455,319✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
75,584,932✔
230
    fname = sptr + 2;
660,163✔
231
    while (*fname == ' ') fname++;
1,320,103✔
232
    *sptr = '\0';
660,163✔
233

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

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

245
  st = taosGetTimestampUs();
75,584,932✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
75,584,932✔
248
  if (taos_errno(pSql)) {
75,584,932✔
249
    shellPrintError(pSql, st);
27,861,922✔
250
    return;
27,861,922✔
251
  }
252

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

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

259
    taos_free_result(pSql);
17,152✔
260

261
    return;
17,152✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
47,705,858✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
47,705,858✔
267
    pre = "Delete OK";
52✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
47,705,806✔
269
    pre = "Insert OK";
5,838✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
47,699,968✔
271
    pre = "Create OK";
5,559✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
47,694,409✔
273
    pre = "Drop OK";
947✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
47,705,858✔
277
  if (pFields != NULL) {  // select and show kinds of commands
47,705,858✔
278
    int32_t error_no = 0;
46,302,460✔
279

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

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

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

304
  (void)printf("\r\n");
47,705,858✔
305
}
306

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

313
  time_t  tt;
791,071,356✔
314
  int32_t ms = 0;
791,544,712✔
315
  if (precision == TSDB_TIME_PRECISION_NANO) {
791,544,712✔
316
    tt = (time_t)(val / 1000000000);
1,040✔
317
    ms = val % 1000000000;
1,040✔
318
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
791,543,672✔
319
    tt = (time_t)(val / 1000000);
1,040✔
320
    ms = val % 1000000;
1,040✔
321
  } else {
322
    tt = (time_t)(val / 1000);
791,542,632✔
323
    ms = val % 1000;
791,542,632✔
324
  }
325

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

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

343
  if (precision == TSDB_TIME_PRECISION_NANO) {
791,544,712✔
344
    (void)sprintf(buf + pos, ".%09d", ms);
1,040✔
345
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
791,543,672✔
346
    (void)sprintf(buf + pos, ".%06d", ms);
1,040✔
347
  } else {
348
    (void)sprintf(buf + pos, ".%03d", ms);
791,542,632✔
349
  }
350

351
  return buf;
791,544,712✔
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,554,639,305✔
364
  if (val == NULL) {
1,554,639,305✔
365
    taosFprintfFile(pFile, "NULL");
13,658,770✔
366
    return;
13,658,770✔
367
  }
368

369
  char    quotationStr[2] = {'"', 0};
1,540,980,535✔
370
  int32_t width;
371

372
  int n = 0;
1,540,980,535✔
373
#define LENGTH 64
374
  char buf[LENGTH] = {0};
1,540,980,535✔
375
  switch (field->type) {
1,540,980,535✔
376
    case TSDB_DATA_TYPE_BOOL:
294,095,035✔
377
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
294,095,035✔
378
      break;
294,095,035✔
379
    case TSDB_DATA_TYPE_TINYINT:
19,636,854✔
380
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
19,636,854✔
381
      break;
19,636,854✔
382
    case TSDB_DATA_TYPE_UTINYINT:
2,258,814✔
383
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,258,814✔
384
      break;
2,258,814✔
UNCOV
385
    case TSDB_DATA_TYPE_SMALLINT:
×
UNCOV
386
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
×
UNCOV
387
      break;
×
388
    case TSDB_DATA_TYPE_USMALLINT:
2,255,746✔
389
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
2,255,746✔
390
      break;
2,255,746✔
391
    case TSDB_DATA_TYPE_INT:
151,408,031✔
392
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
151,408,031✔
393
      break;
151,408,031✔
394
    case TSDB_DATA_TYPE_UINT:
751,660✔
395
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
751,660✔
396
      break;
751,660✔
397
    case TSDB_DATA_TYPE_BIGINT:
180,716,680✔
398
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
180,716,680✔
399
      break;
180,716,680✔
400
    case TSDB_DATA_TYPE_UBIGINT:
2,256,854✔
401
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
2,256,854✔
402
      break;
2,256,854✔
403
    case TSDB_DATA_TYPE_FLOAT:
18,887,418✔
404
      width = SHELL_FLOAT_WIDTH;
18,887,418✔
405
      if (tsEnableScience) {
18,887,418✔
406
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
407
      } else {
408
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
18,887,418✔
409
        if (n > SHELL_FLOAT_WIDTH) {
18,887,418✔
410
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
174,876✔
411
        } else {
412
          taosFprintfFile(pFile, "%s", buf);
18,712,542✔
413
        }
414
      }
415
      break;
18,887,418✔
416
    case TSDB_DATA_TYPE_DOUBLE:
244,462,873✔
417
      width = SHELL_DOUBLE_WIDTH;
244,462,873✔
418
      if (tsEnableScience) {
244,462,873✔
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));
244,462,873✔
423
        if (n > SHELL_DOUBLE_WIDTH) {
244,462,873✔
424
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
127,764,461✔
425
        } else {
426
          taosFprintfFile(pFile, "%s", buf);
116,698,412✔
427
        }
428
      }
429
      break;
244,462,873✔
430
    case TSDB_DATA_TYPE_BINARY:
234,974,680✔
431
    case TSDB_DATA_TYPE_NCHAR:
432
    case TSDB_DATA_TYPE_JSON: {
433
      int32_t bufIndex = 0;
234,974,680✔
434
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
234,974,680✔
435
      if (tmp == NULL) break;
234,974,680✔
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;
234,974,680✔
445

446
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
234,974,680✔
447
      taosMemoryFree(tmp);
234,974,680✔
448
    } break;
234,974,680✔
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:
174,295,064✔
468
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
174,295,064✔
469
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
174,295,064✔
470
      break;
174,295,064✔
471
    case TSDB_DATA_TYPE_DECIMAL64:
214,980,826✔
472
    case TSDB_DATA_TYPE_DECIMAL:
473
      taosFprintfFile(pFile, "%s", val);
214,980,826✔
474
      break;
214,980,826✔
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) {
638,613✔
493
  char fullname[PATH_MAX] = {0};
638,613✔
494
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
638,613✔
495
    tstrncpy(fullname, fname, PATH_MAX);
×
496
  }
497

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

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

513
  for (int32_t col = 0; col < num_fields; col++) {
1,560,944✔
514
    if (col > 0) {
922,331✔
515
      taosFprintfFile(pFile, ",");
283,718✔
516
    }
517
    taosFprintfFile(pFile, "%s", fields[col].name);
922,331✔
518
  }
519
  taosFprintfFile(pFile, "\r\n");
638,613✔
520

521
  int64_t numOfRows = 0;
638,613✔
522
  do {
523
    int32_t *length = taos_fetch_lengths(tres);
714,124,839✔
524
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
525
      if (i > 0) {
1,554,639,305✔
526
        taosFprintfFile(pFile, ",");
840,514,466✔
527
      }
528
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,554,639,305✔
529
    }
530
    taosFprintfFile(pFile, "\r\n");
714,124,839✔
531

532
    numOfRows++;
714,124,839✔
533
    row = taos_fetch_row(tres);
714,124,839✔
534
  } while (row != NULL);
714,124,839✔
535

536
  taosCloseFile(&pFile);
638,613✔
537

538
  return numOfRows;
638,613✔
539
}
540

541
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
321,134,556✔
542
  TdWchar tail[3];
320,929,345✔
543
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
321,134,556✔
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;
143,502✔
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,019,058✔
565
    }
566

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

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

585
  if (totalCols > width) {
321,134,556✔
586
    // width could be 1 or 2, so printf("...") cannot be used
587
    for (int32_t i = 0; i < 3; i++) {
4,122,172✔
588
      if (cols >= width) {
3,091,629✔
589
        break;
×
590
      }
591
      putchar('.');
3,091,629✔
592
      ++cols;
3,091,629✔
593
    }
594
  } else {
595
    for (int32_t i = 0; i < tailLen; i++) {
320,341,894✔
596
      (void)printf("%lc", tail[i]);
237,881✔
597
    }
598
    cols = totalCols;
320,104,013✔
599
  }
600

601
  for (; cols < width; cols++) {
2,147,483,647✔
602
    putchar(' ');
2,147,483,647✔
603
  }
604
}
321,134,556✔
605

606
void shellPrintString(const char *str, int32_t width) {
1,522,899,405✔
607
  int32_t len = strlen(str);
1,522,899,405✔
608

609
  if (width == 0) {
1,522,899,405✔
610
    (void)printf("%s", str);
×
611
  } else if (len > width) {
1,522,899,405✔
612
    if (width <= 3) {
7,020✔
613
      (void)printf("%.*s.", width - 1, str);
7,020✔
614
    } else {
615
      (void)printf("%.*s...", width - 3, str);
×
616
    }
617
  } else {
618
    (void)printf("%s%*.s", str, width - len, "");
1,522,892,385✔
619
  }
620
}
1,522,899,405✔
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,290,487,810✔
651
    return;
1,290,487,810✔
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:
232,411,595✔
659
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
232,411,595✔
660
      break;
232,411,595✔
661
    case TSDB_DATA_TYPE_TINYINT:
62,940,949✔
662
      (void)printf("%*d", width, *((int8_t *)val));
62,940,949✔
663
      break;
62,940,949✔
664
    case TSDB_DATA_TYPE_UTINYINT:
132,795,387✔
665
      (void)printf("%*u", width, *((uint8_t *)val));
132,795,387✔
666
      break;
132,795,387✔
667
    case TSDB_DATA_TYPE_SMALLINT:
49,375,959✔
668
      (void)printf("%*d", width, *((int16_t *)val));
49,375,959✔
669
      break;
49,375,959✔
670
    case TSDB_DATA_TYPE_USMALLINT:
119,880,079✔
671
      (void)printf("%*u", width, *((uint16_t *)val));
119,880,079✔
672
      break;
119,880,079✔
673
    case TSDB_DATA_TYPE_INT:
159,475,027✔
674
      (void)printf("%*d", width, *((int32_t *)val));
159,475,027✔
675
      break;
159,475,027✔
676
    case TSDB_DATA_TYPE_UINT:
169,316,182✔
677
      (void)printf("%*u", width, *((uint32_t *)val));
169,316,182✔
678
      break;
169,316,182✔
679
    case TSDB_DATA_TYPE_BIGINT:
431,281,405✔
680
      (void)printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
431,281,405✔
681
      break;
431,281,405✔
682
    case TSDB_DATA_TYPE_UBIGINT:
176,408,138✔
683
      (void)printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
176,408,138✔
684
      break;
176,408,138✔
685
    case TSDB_DATA_TYPE_FLOAT:
78,123,457✔
686
      width = width >= LENGTH ? LENGTH - 1 : width;
78,123,457✔
687
      if (tsEnableScience) {
78,123,457✔
688
        (void)printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
689
      } else {
690
        (void)snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
78,123,457✔
691
        (void)printf("%s", buf);
78,123,457✔
692
      }
693
      break;
78,123,457✔
694
    case TSDB_DATA_TYPE_DOUBLE:
336,911,343✔
695
      width = width >= LENGTH ? LENGTH - 1 : width;
336,911,343✔
696
      if (tsEnableScience) {
336,911,343✔
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));
336,911,343✔
701
        (void)printf("%*s", width, buf);
336,911,343✔
702
      }
703
      break;
336,911,343✔
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:
321,134,556✔
715
    case TSDB_DATA_TYPE_NCHAR:
716
    case TSDB_DATA_TYPE_JSON:
717
      shellPrintNChar(val, length, width);
321,134,556✔
718
      break;
321,134,556✔
719
    case TSDB_DATA_TYPE_GEOMETRY:
×
720
      shellPrintGeometry(val, length, width);
×
721
      break;
×
722
    case TSDB_DATA_TYPE_TIMESTAMP:
617,270,693✔
723
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
617,270,693✔
724
      (void)printf("%s", buf);
617,270,693✔
725
      break;
617,270,693✔
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:
122✔
739
    case TSDB_DATA_TYPE_DECIMAL64:
740
      (void)printf("%*s", width, val);
122✔
741
    default:
122✔
742
      break;
122✔
743
  }
744
}
745

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

777
  return false;
69✔
778
}
779

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

786
  return false;
69✔
787
}
788

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

795
  dump_info->numFields = taos_num_fields(tres);
45,663,847✔
796
  dump_info->fields = taos_fetch_fields(tres);
45,663,847✔
797
  dump_info->precision = taos_result_precision(tres);
45,663,847✔
798

799
  dump_info->resShowMaxNum = UINT64_MAX;
45,663,847✔
800

801
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
45,663,847✔
802
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
69✔
803
  }
804

805
  if (vertical) {
45,663,847✔
806
    dump_info->maxColNameLen = 0;
120,423✔
807
    for (int32_t col = 0; col < dump_info->numFields; col++) {
247,799✔
808
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
127,376✔
809
      if (len > dump_info->maxColNameLen) {
127,376✔
810
        dump_info->maxColNameLen = len;
122,501✔
811
      }
812
    }
813
  } else {
814
    for (int32_t col = 0; col < dump_info->numFields; col++) {
129,366,408✔
815
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
83,822,984✔
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)) {
45,543,424✔
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)) {
45,543,424✔
821
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
822
    }
823
  }
824
}
45,663,847✔
825

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

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

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

839
    int32_t *length = taos_fetch_lengths(tres);
3,042,768✔
840

841
    for (int32_t i = 0; i < dump_info->numFields; i++) {
6,098,699✔
842
      TAOS_FIELD *field = dump_info->fields + i;
3,055,931✔
843

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

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

852
    numOfPintRows++;
3,042,768✔
853
    numOfPrintRowsThisOne++;
3,042,768✔
854

855
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,042,768✔
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,042,768✔
867
      return;
120,423✔
868
    }
869

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

875
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
83,822,984✔
876
  int32_t width = (int32_t)strlen(field->name);
83,822,984✔
877

878
  switch (field->type) {
83,822,984✔
879
    case TSDB_DATA_TYPE_NULL:
×
880
      return TMAX(4, width);  // null
×
881
    case TSDB_DATA_TYPE_BOOL:
19,269,913✔
882
      return TMAX(5, width);  // 'false'
19,269,913✔
883

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

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

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

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

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

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

906
    case TSDB_DATA_TYPE_BINARY:
5,539,645✔
907
    case TSDB_DATA_TYPE_GEOMETRY:
908
      if (field->bytes > shell.args.displayWidth) {
5,539,645✔
909
        return TMAX(shell.args.displayWidth, width);
3,715,615✔
910
      } else {
911
        return TMAX(field->bytes + 2, width);
1,824,030✔
912
      }
913
    case TSDB_DATA_TYPE_VARBINARY: {
754✔
914
      int32_t bytes = field->bytes * 2 + 2;
754✔
915
      if (bytes > shell.args.displayWidth) {
754✔
916
        return TMAX(shell.args.displayWidth, width);
×
917
      } else {
918
        return TMAX(bytes + 2, width);
754✔
919
      }
920
    }
921
    case TSDB_DATA_TYPE_NCHAR:
3,659,072✔
922
    case TSDB_DATA_TYPE_JSON: {
923
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
3,659,072✔
924
      if (bytes > shell.args.displayWidth) {
3,659,072✔
925
        return TMAX(shell.args.displayWidth, width);
3,659,072✔
926
      } else {
927
        return TMAX(bytes + 2, width);
×
928
      }
929
    }
930

931
    case TSDB_DATA_TYPE_TIMESTAMP:
20,488,697✔
932
      if (shell.args.is_raw_time) {
20,488,697✔
933
        return TMAX(14, width);
394✔
934
      }
935
      if (precision == TSDB_TIME_PRECISION_NANO) {
20,488,303✔
936
        return TMAX(29, width);
104✔
937
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
20,488,199✔
938
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
104✔
939
      } else {
940
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
20,488,095✔
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:
61✔
953
      return TMAX(width, 20);
61✔
954
    case TSDB_DATA_TYPE_DECIMAL:
61✔
955
      return TMAX(width, 40);
61✔
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) {
32,343,315✔
964
  int32_t rowWidth = 0;
32,343,315✔
965
  for (int32_t col = 0; col < num_fields; col++) {
98,170,099✔
966
    TAOS_FIELD *field = fields + col;
65,826,784✔
967
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
65,826,784✔
968
    int32_t     left = padding / 2;
65,826,784✔
969
    (void)printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
65,826,784✔
970
    rowWidth += width[col] + 3;
65,826,784✔
971
  }
972

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

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

989
  int64_t numOfPintRows = dump_info->numOfAllRows;
33,765,210✔
990
  int     numOfPrintRowsThisOne = 0;
33,765,210✔
991
  if (numOfPintRows == 0) {
33,765,210✔
992
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
32,343,315✔
993
  }
994

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

1007
    numOfPintRows++;
591,499,015✔
1008
    numOfPrintRowsThisOne++;
591,499,015✔
1009

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

1025
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
591,498,946✔
1026
      return;
33,765,141✔
1027
    }
1028

1029
    row = taos_fetch_row(tres);
557,733,805✔
1030
  }
1031
  return;
×
1032
}
1033

1034
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
79,549,480✔
1035
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
79,549,480✔
1036
  if (num_of_rows > 0) {
79,549,480✔
1037
    dump_info->numOfRows = num_of_rows;
33,885,633✔
1038
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
33,885,633✔
1039
      if (dump_info->vertical) {
33,885,633✔
1040
        shellVerticalPrintResult(tres, dump_info);
120,423✔
1041
      } else {
1042
        shellHorizontalPrintResult(tres, dump_info);
33,765,210✔
1043
      }
1044
    }
1045
    dump_info->numOfAllRows += num_of_rows;
33,885,633✔
1046
    if (!shellCmdkilled) {
33,885,633✔
1047
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
33,885,633✔
1048
    } else {
1049
      tsem_post(&dump_info->sem);
×
1050
    }
1051
  } else {
1052
    if (num_of_rows < 0) {
45,663,847✔
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);
45,663,847✔
1056
  }
1057
}
79,549,480✔
1058

1059
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
46,302,460✔
1060
  int64_t num_of_rows = 0;
46,302,460✔
1061
  if (fname != NULL) {
46,302,460✔
1062
    num_of_rows = shellDumpResultToFile(fname, tres);
638,613✔
1063
  } else {
1064
    tsDumpInfo dump_info;
41,058,815✔
1065
    if (!shellCmdkilled) {
45,663,847✔
1066
      init_dump_info(&dump_info, tres, sql, vertical);
45,663,847✔
1067
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
45,663,847✔
1068
      tsem_wait(&dump_info.sem);
45,663,847✔
1069
      num_of_rows = dump_info.numOfAllRows;
45,663,847✔
1070
    }
1071
  }
1072

1073
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
46,302,460✔
1074
  return num_of_rows;
46,302,460✔
1075
}
1076

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

1082
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
900,146✔
1083
  int32_t read_size = 0;
900,146✔
1084
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
175,366,269✔
1085
    line[read_size - 1] = '\0';
174,466,123✔
1086
    taosMemoryFree(pHistory->hist[pHistory->hend]);
174,466,123✔
1087
    pHistory->hist[pHistory->hend] = taosStrdup(line);
174,466,123✔
1088

1089
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
174,466,123✔
1090

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

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

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

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

1136
void shellCleanupHistory() {
945,739✔
1137
  SShellHistory *pHistory = &shell.history;
945,739✔
1138
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
946,684,739✔
1139
    if (pHistory->hist[i] != NULL) {
945,739,000✔
1140
      taosMemoryFree(pHistory->hist[i]);
174,466,123✔
1141
      pHistory->hist[i] = NULL;
174,466,123✔
1142
    }
1143
  }
1144
}
945,739✔
1145

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

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

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

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

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

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

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

1186
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
118,915✔
1187
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
75,724,582✔
1188
    if (cmd_len + read_len >= tsMaxSQLLength) {
75,605,667✔
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';
75,605,667✔
1196

1197
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
75,605,667✔
1198
      continue;
951,279✔
1199
    }
1200

1201
    if (line[read_len - 1] == '\\') {
74,654,388✔
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') {
74,654,388✔
1209
      line[read_len - 1] = ' ';
116,412✔
1210
    }
1211

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

1219
  taosMemoryFree(cmd);
118,915✔
1220
  taosMemoryFreeClear(line);
118,915✔
1221
  taosCloseFile(&pFile);
118,915✔
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); }
686✔
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,723✔
1307
      break;
758✔
1308
    }
1309

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

1315
    if (shell.conn) {
965✔
1316
      shellCmdkilled = true;
207✔
1317
      taos_kill_query(shell.conn);
207✔
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,101✔
1345
      taosSetTerminalMode();
1,101✔
1346

1347
      if (shellReadCommand(command) != 0) {
1,101✔
1348
        break;
343✔
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,337✔
1364
  bool ret = true;
2,337✔
1365
  (void)printf("Please enter your TOTP code:");
2,337✔
1366
  if (scanf("%255s", totpCode) != 1) {
2,337✔
1367
    (void)fprintf(stderr, "TOTP code reading error\n");
171✔
1368
    ret = false;
171✔
1369
  }
1370
  if (EOF == getchar()) {
2,337✔
1371
    // tip
1372
    (void)fprintf(stdout, "getchar() return EOF\r\n");    
171✔
1373
  }
1374
  return ret;
2,337✔
1375
}
1376

1377
#pragma GCC diagnostic pop
1378

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

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

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

1401
    if (pArgs->port_inputted) {
948,950✔
1402
      port = pArgs->port;
779✔
1403
    } else {
1404
      port = defaultPort(pArgs->connMode, pArgs->dsn);
948,171✔
1405
    }
1406

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

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

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

1451
  return taos;
945,536✔
1452
}
1453

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

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

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

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

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

1481
  if (runOnce) {
945,739✔
1482
    if (pArgs->commands != NULL) {
944,981✔
1483
      (void)printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
805,551✔
1484
      char *cmd = taosStrdup(pArgs->commands);
805,551✔
1485
      shellRunCommand(cmd, true);
805,551✔
1486
      taosMemoryFree(cmd);
805,551✔
1487
    }
1488

1489
    if (pArgs->file[0] != 0) {
944,981✔
1490
      shellSourceFile(pArgs->file);
139,430✔
1491
    }
1492

1493
    taos_close(shell.conn);
944,981✔
1494

1495
    shellWriteHistory();
944,981✔
1496
    shellCleanupHistory();
944,981✔
1497
    return 0;
944,981✔
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