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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

77.61
/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) {
309,313,874✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
309,435,030✔
72
    if (c != ' ' && c != '\t' && c != ';') {
207,902,448✔
73
      return false;
207,781,292✔
74
    }
75
  }
76
  return true;
101,532,582✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
205,483,103✔
80
  shellCmdkilled = false;
205,483,103✔
81

82
  if (shellIsEmptyCommand(command)) {
205,483,103✔
83
    return 0;
101,532,582✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
103,950,521✔
87
    return -1;
114✔
88
  }
89

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

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
103,830,659✔
174

175
  char quote = 0, *cmd = command;
103,830,659✔
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++;
5,446✔
179
      continue;
5,446✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
2,813,054✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
2,813,054✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
101,652,558✔
188
      *command = 0;
101,652,558✔
189
      if (shellRunSingleCommand(cmd) < 0) {
101,652,558✔
190
        return -1;
114✔
191
      }
192
      *command = c;
101,652,444✔
193
      cmd = command;
101,652,444✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
103,830,545✔
197
}
198

199
char *strendG(const char *pstr) {
103,950,213✔
200
  if (pstr == NULL) {
103,950,213✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
103,950,213✔
205
  if (len < 4) {
103,950,213✔
206
    return NULL;
×
207
  }
208

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

214
  p = (char *)pstr + len - 3;
103,944,511✔
215
  if (strcmp(p, "\\G;") == 0) {
103,944,511✔
216
    return p;
28,615,303✔
217
  }
218

219
  return NULL;
75,329,208✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
103,950,213✔
230
    fname = sptr + 2;
584,037✔
231
    while (*fname == ' ') fname++;
1,167,858✔
232
    *sptr = '\0';
584,037✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
103,950,213✔
241
    *sptr = '\0';
28,621,005✔
242
    printMode = true;  // When output to a file, the switch does not work.
28,621,005✔
243
  }
244

245
  st = taosGetTimestampUs();
103,950,213✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
103,950,213✔
248
  if (taos_errno(pSql)) {
103,950,213✔
249
    shellPrintError(pSql, st);
72,657,936✔
250
    return;
72,657,936✔
251
  }
252

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

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

259
    taos_free_result(pSql);
13,135✔
260

261
    return;
13,135✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
31,279,142✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,279,142✔
267
    pre = "Delete OK";
48✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,279,094✔
269
    pre = "Insert OK";
4,132✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,274,962✔
271
    pre = "Create OK";
4,050✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
31,270,912✔
273
    pre = "Drop OK";
827✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
31,279,142✔
277
  if (pFields != NULL) {  // select and show kinds of commands
31,279,142✔
278
    int32_t error_no = 0;
30,005,553✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
30,005,553✔
281
    if (numOfRows < 0) return;
30,005,553✔
282

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

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

301
  printf("\r\n");
31,279,142✔
302
}
303

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

310
  time_t  tt;
185,730,157✔
311
  int32_t ms = 0;
185,742,557✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
185,742,557✔
313
    tt = (time_t)(val / 1000000000);
970✔
314
    ms = val % 1000000000;
970✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
185,741,587✔
316
    tt = (time_t)(val / 1000000);
970✔
317
    ms = val % 1000000;
970✔
318
  } else {
319
    tt = (time_t)(val / 1000);
185,740,617✔
320
    ms = val % 1000;
185,740,617✔
321
  }
322

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

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
185,742,557✔
341
    sprintf(buf + pos, ".%09d", ms);
970✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
185,741,587✔
343
    sprintf(buf + pos, ".%06d", ms);
970✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
185,740,617✔
346
  }
347

348
  return buf;
185,742,557✔
349
}
350

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

357
  return buf;
×
358
}
359

360
void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, int32_t length, int32_t precision) {
1,409,491,146✔
361
  if (val == NULL) {
1,409,491,146✔
362
    taosFprintfFile(pFile, "NULL");
7,712,897✔
363
    return;
7,712,897✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,401,778,249✔
367
  int32_t width;
368

369
  int n = 0;
1,401,778,249✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,401,778,249✔
372
  switch (field->type) {
1,401,778,249✔
373
    case TSDB_DATA_TYPE_BOOL:
278,391,336✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
278,391,336✔
375
      break;
278,391,336✔
376
    case TSDB_DATA_TYPE_TINYINT:
17,585,172✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
17,585,172✔
378
      break;
17,585,172✔
379
    case TSDB_DATA_TYPE_UTINYINT:
2,139,916✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,139,916✔
381
      break;
2,139,916✔
382
    case TSDB_DATA_TYPE_SMALLINT:
1,420,596✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,420,596✔
384
      break;
1,420,596✔
385
    case TSDB_DATA_TYPE_USMALLINT:
721,272✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
721,272✔
387
      break;
721,272✔
388
    case TSDB_DATA_TYPE_INT:
128,015,434✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
128,015,434✔
390
      break;
128,015,434✔
UNCOV
391
    case TSDB_DATA_TYPE_UINT:
×
UNCOV
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
×
UNCOV
393
      break;
×
394
    case TSDB_DATA_TYPE_BIGINT:
167,454,186✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
167,454,186✔
396
      break;
167,454,186✔
397
    case TSDB_DATA_TYPE_UBIGINT:
2,151,556✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
2,151,556✔
399
      break;
2,151,556✔
400
    case TSDB_DATA_TYPE_FLOAT:
18,287,724✔
401
      width = SHELL_FLOAT_WIDTH;
18,287,724✔
402
      if (tsEnableScience) {
18,287,724✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
18,287,724✔
406
        if (n > SHELL_FLOAT_WIDTH) {
18,287,724✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
505,902✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
17,781,822✔
410
        }
411
      }
412
      break;
18,287,724✔
413
    case TSDB_DATA_TYPE_DOUBLE:
225,274,259✔
414
      width = SHELL_DOUBLE_WIDTH;
225,274,259✔
415
      if (tsEnableScience) {
225,274,259✔
416
        snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
×
417
        taosFprintfFile(pFile, "%s", buf);
×
418
      } else {
419
        n = tsnprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
225,274,259✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
225,274,259✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
119,445,411✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
105,828,848✔
424
        }
425
      }
426
      break;
225,274,259✔
427
    case TSDB_DATA_TYPE_BINARY:
205,186,085✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
205,186,085✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
205,186,085✔
432
      if (tmp == NULL) break;
205,186,085✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,069,634,653✔
435
        bufIndex++;
2,069,634,653✔
436
        if (val[i] == '\"') {
2,069,634,653✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
205,186,085✔
442

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

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

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

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
1,358,980✔
511
    if (col > 0) {
794,995✔
512
      taosFprintfFile(pFile, ",");
231,010✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
794,995✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
563,985✔
517

518
  int64_t numOfRows = 0;
563,985✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
658,063,176✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,067,554,322✔
522
      if (i > 0) {
1,409,491,146✔
523
        taosFprintfFile(pFile, ",");
751,427,970✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,409,491,146✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
658,063,176✔
528

529
    numOfRows++;
658,063,176✔
530
    row = taos_fetch_row(tres);
658,063,176✔
531
  } while (row != NULL);
658,063,176✔
532

533
  taosCloseFile(&pFile);
563,985✔
534

535
  return numOfRows;
563,985✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
409,491,672✔
539
  TdWchar tail[3];
409,480,512✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
409,491,672✔
541

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

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

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

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

569
    totalCols += w;
272,806,554✔
570
    if (totalCols > width) {
272,806,554✔
571
      break;
801,760✔
572
    }
573
    if (totalCols <= (width - 3)) {
272,004,794✔
574
      printf("%lc", wc);
269,536,723✔
575
      cols += w;
269,536,723✔
576
    } else {
577
      tail[tailLen] = wc;
2,468,071✔
578
      tailLen++;
2,468,071✔
579
    }
580
  }
581

582
  if (totalCols > width) {
409,491,672✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,207,040✔
585
      if (cols >= width) {
2,405,280✔
586
        break;
×
587
      }
588
      putchar('.');
2,405,280✔
589
      ++cols;
2,405,280✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
408,760,956✔
593
      printf("%lc", tail[i]);
71,044✔
594
    }
595
    cols = totalCols;
408,689,912✔
596
  }
597

598
  for (; cols < width; cols++) {
758,627,300✔
599
    putchar(' ');
349,135,628✔
600
  }
601
}
409,491,672✔
602

603
void shellPrintString(const char *str, int32_t width) {
18,520,039✔
604
  int32_t len = strlen(str);
18,520,039✔
605

606
  if (width == 0) {
18,520,039✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
18,520,039✔
609
    if (width <= 3) {
12,870✔
610
      printf("%.*s.", width - 1, str);
12,870✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
18,507,169✔
616
  }
617
}
18,520,039✔
618

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

625
  int32_t code = TSDB_CODE_FAILED;
×
626

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

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

640
  shellPrintString(outputWKT, width);
×
641

642
  geosFreeBuffer(outputWKT);
×
643
}
644

645
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
543,834,340✔
646
  if (val == NULL) {
543,834,340✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
15,442,852✔
648
    return;
15,442,852✔
649
  }
650

651
  int n = 0;
528,391,488✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
528,391,488✔
654
  switch (field->type) {
528,391,488✔
655
    case TSDB_DATA_TYPE_BOOL:
3,077,187✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
3,077,187✔
657
      break;
3,077,187✔
658
    case TSDB_DATA_TYPE_TINYINT:
2,089,690✔
659
      printf("%*d", width, *((int8_t *)val));
2,089,690✔
660
      break;
2,089,690✔
661
    case TSDB_DATA_TYPE_UTINYINT:
12,428,598✔
662
      printf("%*u", width, *((uint8_t *)val));
12,428,598✔
663
      break;
12,428,598✔
664
    case TSDB_DATA_TYPE_SMALLINT:
2,266,793✔
665
      printf("%*d", width, *((int16_t *)val));
2,266,793✔
666
      break;
2,266,793✔
667
    case TSDB_DATA_TYPE_USMALLINT:
6,958,371✔
668
      printf("%*u", width, *((uint16_t *)val));
6,958,371✔
669
      break;
6,958,371✔
670
    case TSDB_DATA_TYPE_INT:
10,187,245✔
671
      printf("%*d", width, *((int32_t *)val));
10,187,245✔
672
      break;
10,187,245✔
673
    case TSDB_DATA_TYPE_UINT:
4,548,850✔
674
      printf("%*u", width, *((uint32_t *)val));
4,548,850✔
675
      break;
4,548,850✔
676
    case TSDB_DATA_TYPE_BIGINT:
26,888,950✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
26,888,950✔
678
      break;
26,888,950✔
679
    case TSDB_DATA_TYPE_UBIGINT:
1,716,173✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
1,716,173✔
681
      break;
1,716,173✔
682
    case TSDB_DATA_TYPE_FLOAT:
3,235,393✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
3,235,393✔
684
      if (tsEnableScience) {
3,235,393✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
3,235,393✔
688
        printf("%s", buf);
3,235,393✔
689
      }
690
      break;
3,235,393✔
691
    case TSDB_DATA_TYPE_DOUBLE:
14,288,466✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
14,288,466✔
693
      if (tsEnableScience) {
14,288,466✔
694
        snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
695
        printf("%s", buf);
×
696
      } else {
697
        snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
14,288,466✔
698
        printf("%*s", width, buf);
14,288,466✔
699
      }
700
      break;
14,288,466✔
701
    case TSDB_DATA_TYPE_VARBINARY: {
×
702
      void    *data = NULL;
×
703
      uint32_t size = 0;
×
704
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
705
        break;
×
706
      }
707
      shellPrintNChar(data, size, width);
×
708
      taosMemoryFree(data);
×
709
      break;
×
710
    }
711
    case TSDB_DATA_TYPE_BINARY:
409,491,672✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
409,491,672✔
715
      break;
409,491,672✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
31,213,979✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
31,213,979✔
721
      printf("%s", buf);
31,213,979✔
722
      break;
31,213,979✔
723

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

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

766
  return false;
×
767
}
768

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

775
  return false;
×
776
}
777

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

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

788
  dump_info->resShowMaxNum = UINT64_MAX;
29,441,568✔
789

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

794
  if (vertical) {
29,441,568✔
795
    dump_info->maxColNameLen = 0;
10,663,395✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
21,333,154✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
10,669,759✔
798
      if (len > dump_info->maxColNameLen) {
10,669,759✔
799
        dump_info->maxColNameLen = len;
10,665,299✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
52,836,264✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
34,058,091✔
805
    }
806
    // set an appropriate width for token and totp_secret display
807
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
18,778,173✔
808
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
809
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
18,778,173✔
810
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
811
    }
812
  }
813
}
29,441,568✔
814

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

822
  int64_t numOfPintRows = dump_info->numOfAllRows;
10,663,395✔
823
  int     numOfPrintRowsThisOne = 0;
10,663,395✔
824

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

828
    int32_t *length = taos_fetch_lengths(tres);
388,339,766✔
829

830
    for (int32_t i = 0; i < dump_info->numFields; i++) {
776,691,750✔
831
      TAOS_FIELD *field = dump_info->fields + i;
388,351,984✔
832

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

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

841
    numOfPintRows++;
388,339,766✔
842
    numOfPrintRowsThisOne++;
388,339,766✔
843

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

855
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
388,339,766✔
856
      return;
10,663,395✔
857
    }
858

859
    row = taos_fetch_row(tres);
377,676,371✔
860
  }
861
  return;
×
862
}
863

864
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
34,058,091✔
865
  int32_t width = (int32_t)strlen(field->name);
34,058,091✔
866

867
  switch (field->type) {
34,058,091✔
868
    case TSDB_DATA_TYPE_NULL:
×
869
      return TMAX(4, width);  // null
×
870
    case TSDB_DATA_TYPE_BOOL:
1,165,913✔
871
      return TMAX(5, width);  // 'false'
1,165,913✔
872

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

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

881
    case TSDB_DATA_TYPE_INT:
8,608,377✔
882
    case TSDB_DATA_TYPE_UINT:
883
      return TMAX(11, width);  // '-2147483648'
8,608,377✔
884

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

889
    case TSDB_DATA_TYPE_FLOAT:
405,895✔
890
      return TMAX(SHELL_FLOAT_WIDTH, width);
405,895✔
891

892
    case TSDB_DATA_TYPE_DOUBLE:
10,107,070✔
893
      return TMAX(SHELL_DOUBLE_WIDTH, width);
10,107,070✔
894

895
    case TSDB_DATA_TYPE_BINARY:
2,282,908✔
896
    case TSDB_DATA_TYPE_GEOMETRY:
897
      if (field->bytes > shell.args.displayWidth) {
2,282,908✔
898
        return TMAX(shell.args.displayWidth, width);
1,498,053✔
899
      } else {
900
        return TMAX(field->bytes + 2, width);
784,855✔
901
      }
902
    case TSDB_DATA_TYPE_VARBINARY: {
752✔
903
      int32_t bytes = field->bytes * 2 + 2;
752✔
904
      if (bytes > shell.args.displayWidth) {
752✔
905
        return TMAX(shell.args.displayWidth, width);
×
906
      } else {
907
        return TMAX(bytes + 2, width);
752✔
908
      }
909
    }
910
    case TSDB_DATA_TYPE_NCHAR:
618,976✔
911
    case TSDB_DATA_TYPE_JSON: {
912
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
618,976✔
913
      if (bytes > shell.args.displayWidth) {
618,976✔
914
        return TMAX(shell.args.displayWidth, width);
618,976✔
915
      } else {
916
        return TMAX(bytes + 2, width);
×
917
      }
918
    }
919

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

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

949
  return 0;
×
950
}
951

952
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
10,925,712✔
953
  int32_t rowWidth = 0;
10,925,712✔
954
  for (int32_t col = 0; col < num_fields; col++) {
31,696,909✔
955
    TAOS_FIELD *field = fields + col;
20,771,197✔
956
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
20,771,197✔
957
    int32_t     left = padding / 2;
20,771,197✔
958
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
20,771,197✔
959
    rowWidth += width[col] + 3;
20,771,197✔
960
  }
961

962
  putchar('\r');
10,925,712✔
963
  putchar('\n');
10,925,712✔
964
  for (int32_t i = 0; i < rowWidth; i++) {
790,329,688✔
965
    putchar('=');
779,403,976✔
966
  }
967
  putchar('\r');
10,925,712✔
968
  putchar('\n');
10,925,712✔
969
}
10,925,712✔
970

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

978
  int64_t numOfPintRows = dump_info->numOfAllRows;
11,126,667✔
979
  int     numOfPrintRowsThisOne = 0;
11,126,667✔
980
  if (numOfPintRows == 0) {
11,126,667✔
981
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
10,925,712✔
982
  }
983

984
  while (row != NULL) {
45,395,188✔
985
    int32_t *length = taos_fetch_lengths(tres);
45,395,188✔
986
    for (int32_t i = 0; i < dump_info->numFields; i++) {
200,877,544✔
987
      putchar(' ');
155,482,356✔
988
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
155,482,356✔
989
                      dump_info->precision);
990
      putchar(' ');
155,482,356✔
991
      putchar('|');
155,482,356✔
992
    }
993
    putchar('\r');
45,395,188✔
994
    putchar('\n');
45,395,188✔
995

996
    numOfPintRows++;
45,395,188✔
997
    numOfPrintRowsThisOne++;
45,395,188✔
998

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

1014
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
45,395,188✔
1015
      return;
11,126,667✔
1016
    }
1017

1018
    row = taos_fetch_row(tres);
34,268,521✔
1019
  }
1020
  return;
×
1021
}
1022

1023
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
51,231,630✔
1024
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
51,231,630✔
1025
  if (num_of_rows > 0) {
51,231,630✔
1026
    dump_info->numOfRows = num_of_rows;
21,790,062✔
1027
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
21,790,062✔
1028
      if (dump_info->vertical) {
21,790,062✔
1029
        shellVerticalPrintResult(tres, dump_info);
10,663,395✔
1030
      } else {
1031
        shellHorizontalPrintResult(tres, dump_info);
11,126,667✔
1032
      }
1033
    }
1034
    dump_info->numOfAllRows += num_of_rows;
21,790,062✔
1035
    if (!shellCmdkilled) {
21,790,062✔
1036
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
21,790,062✔
1037
    } else {
1038
      tsem_post(&dump_info->sem);
×
1039
    }
1040
  } else {
1041
    if (num_of_rows < 0) {
29,441,568✔
1042
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1043
    }
1044
    tsem_post(&dump_info->sem);
29,441,568✔
1045
  }
1046
}
51,231,630✔
1047

1048
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
30,005,553✔
1049
  int64_t num_of_rows = 0;
30,005,553✔
1050
  if (fname != NULL) {
30,005,553✔
1051
    num_of_rows = shellDumpResultToFile(fname, tres);
563,985✔
1052
  } else {
1053
    tsDumpInfo dump_info;
29,440,850✔
1054
    if (!shellCmdkilled) {
29,441,568✔
1055
      init_dump_info(&dump_info, tres, sql, vertical);
29,441,568✔
1056
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
29,441,568✔
1057
      tsem_wait(&dump_info.sem);
29,441,568✔
1058
      num_of_rows = dump_info.numOfAllRows;
29,441,568✔
1059
    }
1060
  }
1061

1062
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
30,005,553✔
1063
  return num_of_rows;
30,005,553✔
1064
}
1065

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

1071
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
760,217✔
1072
  int32_t read_size = 0;
760,217✔
1073
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
159,421,930✔
1074
    line[read_size - 1] = '\0';
158,661,713✔
1075
    taosMemoryFree(pHistory->hist[pHistory->hend]);
158,661,713✔
1076
    pHistory->hist[pHistory->hend] = taosStrdup(line);
158,661,713✔
1077

1078
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
158,661,713✔
1079

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

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

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

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

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

1125
void shellCleanupHistory() {
788,160✔
1126
  SShellHistory *pHistory = &shell.history;
788,160✔
1127
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
788,948,160✔
1128
    if (pHistory->hist[i] != NULL) {
788,160,000✔
1129
      taosMemoryFree(pHistory->hist[i]);
158,661,713✔
1130
      pHistory->hist[i] = NULL;
158,661,713✔
1131
    }
1132
  }
1133
}
788,160✔
1134

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

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

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

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

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

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

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

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

1186
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
103,297,012✔
1187
      continue;
180,736✔
1188
    }
1189

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

1197
    if (line[read_len - 1] == '\r') {
103,116,276✔
1198
      line[read_len - 1] = ' ';
×
1199
    }
1200

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

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

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

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

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

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

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

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

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

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

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

1270
    taos_free_result(tres);
327✔
1271
  }
1272

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

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

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

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

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

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

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

1314
  return NULL;
327✔
1315
}
1316

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

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

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

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

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

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

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

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

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

1366
#pragma GCC diagnostic pop
1367

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

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

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

1390
    if (pArgs->port_inputted) {
788,348✔
1391
      port = pArgs->port;
752✔
1392
    } else {
1393
      port = defaultPort(pArgs->connMode, pArgs->dsn);
787,596✔
1394
    }
1395

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

1398
    // connect normal
1399
    if (pArgs->auth) {
788,348✔
1400
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
181✔
1401
    } else {
1402
#ifdef TD_ENTERPRISE 
1403
      if (strlen(pArgs->token) > 0) {
788,167✔
1404
        // token
1405
        printf("Connect with token ...");
183✔
1406
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
183✔
1407
        if (taos != NULL) {
183✔
1408
          printf("... [ OK ]\n");
92✔
1409
          return taos;
92✔
1410
        }
1411
        printf("... [ FAILED ]\n");
91✔
1412
        return NULL;
91✔
1413
      }
1414
#endif      
1415
      taos = taos_connect(host, user, pwd, pArgs->database, port);
787,984✔
1416
    }
1417

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

1440
  return taos;
789,391✔
1441
}
1442

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

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

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

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

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

1470
  if (runOnce) {
788,160✔
1471
    if (pArgs->commands != NULL) {
787,833✔
1472
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
714,168✔
1473
      char *cmd = taosStrdup(pArgs->commands);
714,168✔
1474
      shellRunCommand(cmd, true);
714,168✔
1475
      taosMemoryFree(cmd);
714,168✔
1476
    }
1477

1478
    if (pArgs->file[0] != 0) {
787,833✔
1479
      shellSourceFile(pArgs->file);
73,665✔
1480
    }
1481

1482
    taos_close(shell.conn);
787,833✔
1483

1484
    shellWriteHistory();
787,833✔
1485
    shellCleanupHistory();
787,833✔
1486
    return 0;
787,833✔
1487
  }
1488

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

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

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

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

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

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

1529
  taosThreadJoin(spid, NULL);
327✔
1530

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

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

© 2026 Coveralls, Inc