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

taosdata / TDengine / #4925

12 Jan 2026 09:34AM UTC coverage: 66.107% (+0.8%) from 65.354%
#4925

push

travis-ci

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

103 of 129 new or added lines in 9 files covered. (79.84%)

891 existing lines in 139 files now uncovered.

200488 of 303278 relevant lines covered (66.11%)

129810096.48 hits per line

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

77.83
/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) {
336,719,640✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
336,841,350✔
72
    if (c != ' ' && c != '\t' && c != ';') {
226,185,463✔
73
      return false;
226,063,753✔
74
    }
75
  }
76
  return true;
110,655,887✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
223,747,919✔
80
  shellCmdkilled = false;
223,747,919✔
81

82
  if (shellIsEmptyCommand(command)) {
223,747,919✔
83
    return 0;
110,655,887✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
113,092,032✔
87
    return -1;
119✔
88
  }
89

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
112,971,605✔
174

175
  char quote = 0, *cmd = command;
112,971,605✔
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,430✔
179
      continue;
5,430✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
5,414,950✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
5,414,950✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
110,776,433✔
188
      *command = 0;
110,776,433✔
189
      if (shellRunSingleCommand(cmd) < 0) {
110,776,433✔
190
        return -1;
119✔
191
      }
192
      *command = c;
110,776,314✔
193
      cmd = command;
110,776,314✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
112,971,486✔
197
}
198

199
char *strendG(const char *pstr) {
113,091,114✔
200
  if (pstr == NULL) {
113,091,114✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
113,091,114✔
205
  if (len < 4) {
113,091,114✔
206
    return NULL;
×
207
  }
208

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

214
  p = (char *)pstr + len - 3;
113,085,409✔
215
  if (strcmp(p, "\\G;") == 0) {
113,085,409✔
216
    return p;
30,975,125✔
217
  }
218

219
  return NULL;
82,110,284✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
113,091,114✔
230
    fname = sptr + 2;
587,312✔
231
    while (*fname == ' ') fname++;
1,174,402✔
232
    *sptr = '\0';
587,312✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
113,091,114✔
241
    *sptr = '\0';
30,980,830✔
242
    printMode = true;  // When output to a file, the switch does not work.
30,980,830✔
243
  }
244

245
  st = taosGetTimestampUs();
113,091,114✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
113,091,114✔
248
  if (taos_errno(pSql)) {
113,091,114✔
249
    shellPrintError(pSql, st);
79,626,040✔
250
    return;
79,626,040✔
251
  }
252

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

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

259
    taos_free_result(pSql);
13,122✔
260

261
    return;
13,122✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
33,451,952✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
33,451,952✔
267
    pre = "Delete OK";
51✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
33,451,901✔
269
    pre = "Insert OK";
4,173✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
33,447,728✔
271
    pre = "Create OK";
4,110✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
33,443,618✔
273
    pre = "Drop OK";
838✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
33,451,952✔
277
  if (pFields != NULL) {  // select and show kinds of commands
33,451,952✔
278
    int32_t error_no = 0;
32,164,889✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
32,164,889✔
281
    if (numOfRows < 0) return;
32,164,889✔
282

283
    et = taosGetTimestampUs();
32,164,889✔
284
    if (error_no == 0) {
32,164,889✔
285
      printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
32,164,889✔
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);
32,164,889✔
291
  } else {
292
    int64_t num_rows_affacted = taos_affected_rows64(pSql);
1,287,063✔
293
    taos_free_result(pSql);
1,287,063✔
294
    et = taosGetTimestampUs();
1,287,063✔
295
    printf("%s, %" PRId64 " row(s) affected (%.6fs)\r\n", pre, num_rows_affacted, (et - st) / 1E6);
1,287,063✔
296

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

301
  printf("\r\n");
33,451,952✔
302
}
303

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

310
  time_t  tt;
736,292,002✔
311
  int32_t ms = 0;
736,304,402✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
736,304,402✔
313
    tt = (time_t)(val / 1000000000);
990✔
314
    ms = val % 1000000000;
990✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
736,303,412✔
316
    tt = (time_t)(val / 1000000);
990✔
317
    ms = val % 1000000;
990✔
318
  } else {
319
    tt = (time_t)(val / 1000);
736,302,422✔
320
    ms = val % 1000;
736,302,422✔
321
  }
322

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

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
736,304,402✔
341
    sprintf(buf + pos, ".%09d", ms);
990✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
736,303,412✔
343
    sprintf(buf + pos, ".%06d", ms);
990✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
736,302,422✔
346
  }
347

348
  return buf;
736,304,402✔
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,407,704,830✔
361
  if (val == NULL) {
1,407,704,830✔
362
    taosFprintfFile(pFile, "NULL");
8,683,565✔
363
    return;
8,683,565✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,399,021,265✔
367
  int32_t width;
368

369
  int n = 0;
1,399,021,265✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,399,021,265✔
372
  switch (field->type) {
1,399,021,265✔
373
    case TSDB_DATA_TYPE_BOOL:
278,520,862✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
278,520,862✔
375
      break;
278,520,862✔
376
    case TSDB_DATA_TYPE_TINYINT:
17,626,212✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
17,626,212✔
378
      break;
17,626,212✔
379
    case TSDB_DATA_TYPE_UTINYINT:
1,426,333✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
1,426,333✔
381
      break;
1,426,333✔
382
    case TSDB_DATA_TYPE_SMALLINT:
707,760✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
707,760✔
384
      break;
707,760✔
385
    case TSDB_DATA_TYPE_USMALLINT:
2,140,540✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
2,140,540✔
387
      break;
2,140,540✔
388
    case TSDB_DATA_TYPE_INT:
128,943,563✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
128,943,563✔
390
      break;
128,943,563✔
391
    case TSDB_DATA_TYPE_UINT:
1,415,520✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,415,520✔
393
      break;
1,415,520✔
394
    case TSDB_DATA_TYPE_BIGINT:
166,808,567✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
166,808,567✔
396
      break;
166,808,567✔
397
    case TSDB_DATA_TYPE_UBIGINT:
2,126,778✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
2,126,778✔
399
      break;
2,126,778✔
400
    case TSDB_DATA_TYPE_FLOAT:
16,907,646✔
401
      width = SHELL_FLOAT_WIDTH;
16,907,646✔
402
      if (tsEnableScience) {
16,907,646✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
16,907,646✔
406
        if (n > SHELL_FLOAT_WIDTH) {
16,907,646✔
UNCOV
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
16,907,646✔
410
        }
411
      }
412
      break;
16,907,646✔
413
    case TSDB_DATA_TYPE_DOUBLE:
224,802,776✔
414
      width = SHELL_DOUBLE_WIDTH;
224,802,776✔
415
      if (tsEnableScience) {
224,802,776✔
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));
224,802,776✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
224,802,776✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
119,670,886✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
105,131,890✔
424
        }
425
      }
426
      break;
224,802,776✔
427
    case TSDB_DATA_TYPE_BINARY:
203,498,292✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
203,498,292✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
203,498,292✔
432
      if (tmp == NULL) break;
203,498,292✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,039,154,716✔
435
        bufIndex++;
2,039,154,716✔
436
        if (val[i] == '\"') {
2,039,154,716✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
203,498,292✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
203,498,292✔
444
      taosMemoryFree(tmp);
203,498,292✔
445
    } break;
203,498,292✔
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,662,793✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
154,662,793✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
154,662,793✔
467
      break;
154,662,793✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
199,433,623✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
199,433,623✔
471
      break;
199,433,623✔
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) {
565,776✔
490
  char fullname[PATH_MAX] = {0};
565,776✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
565,776✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
1,367,090✔
511
    if (col > 0) {
801,314✔
512
      taosFprintfFile(pFile, ",");
235,538✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
801,314✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
565,776✔
517

518
  int64_t numOfRows = 0;
565,776✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
656,490,984✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,064,195,814✔
522
      if (i > 0) {
1,407,704,830✔
523
        taosFprintfFile(pFile, ",");
751,213,846✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,407,704,830✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
656,490,984✔
528

529
    numOfRows++;
656,490,984✔
530
    row = taos_fetch_row(tres);
656,490,984✔
531
  } while (row != NULL);
656,490,984✔
532

533
  taosCloseFile(&pFile);
565,776✔
534

535
  return numOfRows;
565,776✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
550,969,993✔
539
  TdWchar tail[3];
550,958,833✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
550,969,993✔
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;
398,141✔
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;
25,524✔
562
    }
563

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

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

582
  if (totalCols > width) {
550,969,993✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,679,156✔
585
      if (cols >= width) {
2,759,367✔
586
        break;
×
587
      }
588
      putchar('.');
2,759,367✔
589
      ++cols;
2,759,367✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
550,249,306✔
593
      printf("%lc", tail[i]);
199,102✔
594
    }
595
    cols = totalCols;
550,050,204✔
596
  }
597

598
  for (; cols < width; cols++) {
2,147,483,647✔
599
    putchar(' ');
2,147,483,647✔
600
  }
601
}
550,969,993✔
602

603
void shellPrintString(const char *str, int32_t width) {
1,432,338,003✔
604
  int32_t len = strlen(str);
1,432,338,003✔
605

606
  if (width == 0) {
1,432,338,003✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
1,432,338,003✔
609
    if (width <= 3) {
11,835✔
610
      printf("%.*s.", width - 1, str);
11,835✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
1,432,326,168✔
616
  }
617
}
1,432,338,003✔
618

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

625
  int32_t code = TSDB_CODE_FAILED;
×
626

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

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

640
  shellPrintString(outputWKT, width);
×
641

642
  geosFreeBuffer(outputWKT);
×
643
}
644

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

651
  int n = 0;
2,147,483,647✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
2,147,483,647✔
654
  switch (field->type) {
2,147,483,647✔
655
    case TSDB_DATA_TYPE_BOOL:
228,629,345✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
228,629,345✔
657
      break;
228,629,345✔
658
    case TSDB_DATA_TYPE_TINYINT:
65,271,237✔
659
      printf("%*d", width, *((int8_t *)val));
65,271,237✔
660
      break;
65,271,237✔
661
    case TSDB_DATA_TYPE_UTINYINT:
129,235,198✔
662
      printf("%*u", width, *((uint8_t *)val));
129,235,198✔
663
      break;
129,235,198✔
664
    case TSDB_DATA_TYPE_SMALLINT:
49,946,424✔
665
      printf("%*d", width, *((int16_t *)val));
49,946,424✔
666
      break;
49,946,424✔
667
    case TSDB_DATA_TYPE_USMALLINT:
122,750,517✔
668
      printf("%*u", width, *((uint16_t *)val));
122,750,517✔
669
      break;
122,750,517✔
670
    case TSDB_DATA_TYPE_INT:
168,472,874✔
671
      printf("%*d", width, *((int32_t *)val));
168,472,874✔
672
      break;
168,472,874✔
673
    case TSDB_DATA_TYPE_UINT:
157,912,781✔
674
      printf("%*u", width, *((uint32_t *)val));
157,912,781✔
675
      break;
157,912,781✔
676
    case TSDB_DATA_TYPE_BIGINT:
420,632,068✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
420,632,068✔
678
      break;
420,632,068✔
679
    case TSDB_DATA_TYPE_UBIGINT:
190,948,369✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
190,948,369✔
681
      break;
190,948,369✔
682
    case TSDB_DATA_TYPE_FLOAT:
76,937,266✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
76,937,266✔
684
      if (tsEnableScience) {
76,937,266✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
76,937,266✔
688
        printf("%s", buf);
76,937,266✔
689
      }
690
      break;
76,937,266✔
691
    case TSDB_DATA_TYPE_DOUBLE:
356,811,995✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
356,811,995✔
693
      if (tsEnableScience) {
356,811,995✔
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));
356,811,995✔
698
        printf("%*s", width, buf);
356,811,995✔
699
      }
700
      break;
356,811,995✔
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:
550,969,993✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
550,969,993✔
715
      break;
550,969,993✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
581,641,609✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
581,641,609✔
721
      printf("%s", buf);
581,641,609✔
722
      break;
581,641,609✔
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:
123✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
123✔
738
    default:
123✔
739
      break;
123✔
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) {
31,599,113✔
779
  dump_info->sql = sql;
31,599,113✔
780
  dump_info->vertical = vertical;
31,599,113✔
781
  tsem_init(&dump_info->sem, 0, 0);
31,599,113✔
782
  dump_info->numOfAllRows = 0;
31,599,113✔
783

784
  dump_info->numFields = taos_num_fields(tres);
31,599,113✔
785
  dump_info->fields = taos_fetch_fields(tres);
31,599,113✔
786
  dump_info->precision = taos_result_precision(tres);
31,599,113✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
31,599,113✔
789

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

794
  if (vertical) {
31,599,113✔
795
    dump_info->maxColNameLen = 0;
8,235,038✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
16,476,467✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
8,241,429✔
798
      if (len > dump_info->maxColNameLen) {
8,241,429✔
799
        dump_info->maxColNameLen = len;
8,236,948✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
88,925,704✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
65,561,629✔
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)) {
23,364,075✔
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)) {
23,364,075✔
810
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
811
    }
812
  }
813
}
31,599,113✔
814

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

822
  int64_t numOfPintRows = dump_info->numOfAllRows;
8,235,038✔
823
  int     numOfPrintRowsThisOne = 0;
8,235,038✔
824

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

828
    int32_t *length = taos_fetch_lengths(tres);
235,798,108✔
829

830
    for (int32_t i = 0; i < dump_info->numFields; i++) {
471,608,541✔
831
      TAOS_FIELD *field = dump_info->fields + i;
235,810,433✔
832

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

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

841
    numOfPintRows++;
235,798,108✔
842
    numOfPrintRowsThisOne++;
235,798,108✔
843

844
    if (numOfPintRows == dump_info->resShowMaxNum) {
235,798,108✔
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) {
235,798,108✔
856
      return;
8,235,038✔
857
    }
858

859
    row = taos_fetch_row(tres);
227,563,070✔
860
  }
861
  return;
×
862
}
863

864
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
65,561,629✔
865
  int32_t width = (int32_t)strlen(field->name);
65,561,629✔
866

867
  switch (field->type) {
65,561,629✔
868
    case TSDB_DATA_TYPE_NULL:
×
869
      return TMAX(4, width);  // null
×
870
    case TSDB_DATA_TYPE_BOOL:
3,275,469✔
871
      return TMAX(5, width);  // 'false'
3,275,469✔
872

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

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

881
    case TSDB_DATA_TYPE_INT:
12,381,957✔
882
    case TSDB_DATA_TYPE_UINT:
883
      return TMAX(11, width);  // '-2147483648'
12,381,957✔
884

885
    case TSDB_DATA_TYPE_BIGINT:
11,625,144✔
886
    case TSDB_DATA_TYPE_UBIGINT:
887
      return TMAX(21, width);  // '-9223372036854775807'
11,625,144✔
888

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

892
    case TSDB_DATA_TYPE_DOUBLE:
13,813,968✔
893
      return TMAX(SHELL_DOUBLE_WIDTH, width);
13,813,968✔
894

895
    case TSDB_DATA_TYPE_BINARY:
5,631,809✔
896
    case TSDB_DATA_TYPE_GEOMETRY:
897
      if (field->bytes > shell.args.displayWidth) {
5,631,809✔
898
        return TMAX(shell.args.displayWidth, width);
3,657,677✔
899
      } else {
900
        return TMAX(field->bytes + 2, width);
1,974,132✔
901
      }
902
    case TSDB_DATA_TYPE_VARBINARY: {
748✔
903
      int32_t bytes = field->bytes * 2 + 2;
748✔
904
      if (bytes > shell.args.displayWidth) {
748✔
905
        return TMAX(shell.args.displayWidth, width);
×
906
      } else {
907
        return TMAX(bytes + 2, width);
748✔
908
      }
909
    }
910
    case TSDB_DATA_TYPE_NCHAR:
3,476,619✔
911
    case TSDB_DATA_TYPE_JSON: {
912
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
3,476,619✔
913
      if (bytes > shell.args.displayWidth) {
3,476,619✔
914
        return TMAX(shell.args.displayWidth, width);
3,476,619✔
915
      } else {
916
        return TMAX(bytes + 2, width);
×
917
      }
918
    }
919

920
    case TSDB_DATA_TYPE_TIMESTAMP:
5,762,772✔
921
      if (shell.args.is_raw_time) {
5,762,772✔
922
        return TMAX(14, width);
187✔
923
      }
924
      if (precision == TSDB_TIME_PRECISION_NANO) {
5,762,585✔
925
        return TMAX(29, width);
99✔
926
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
5,762,486✔
927
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
99✔
928
      } else {
929
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
5,762,387✔
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:
62✔
942
      return TMAX(width, 20);
62✔
943
    case TSDB_DATA_TYPE_DECIMAL:
61✔
944
      return TMAX(width, 40);
61✔
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) {
14,935,216✔
953
  int32_t rowWidth = 0;
14,935,216✔
954
  for (int32_t col = 0; col < num_fields; col++) {
63,720,125✔
955
    TAOS_FIELD *field = fields + col;
48,784,909✔
956
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
48,784,909✔
957
    int32_t     left = padding / 2;
48,784,909✔
958
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
48,784,909✔
959
    rowWidth += width[col] + 3;
48,784,909✔
960
  }
961

962
  putchar('\r');
14,935,216✔
963
  putchar('\n');
14,935,216✔
964
  for (int32_t i = 0; i < rowWidth; i++) {
1,516,725,858✔
965
    putchar('=');
1,501,790,642✔
966
  }
967
  putchar('\r');
14,935,216✔
968
  putchar('\n');
14,935,216✔
969
}
14,935,216✔
970

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

978
  int64_t numOfPintRows = dump_info->numOfAllRows;
16,464,332✔
979
  int     numOfPrintRowsThisOne = 0;
16,464,332✔
980
  if (numOfPintRows == 0) {
16,464,332✔
981
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
14,935,216✔
982
  }
983

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

996
    numOfPintRows++;
527,868,965✔
997
    numOfPrintRowsThisOne++;
527,868,965✔
998

999
    if (numOfPintRows == dump_info->resShowMaxNum) {
527,868,965✔
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) {
527,868,965✔
1015
      return;
16,464,332✔
1016
    }
1017

1018
    row = taos_fetch_row(tres);
511,404,633✔
1019
  }
1020
  return;
×
1021
}
1022

1023
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
56,298,483✔
1024
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
56,298,483✔
1025
  if (num_of_rows > 0) {
56,298,483✔
1026
    dump_info->numOfRows = num_of_rows;
24,699,370✔
1027
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
24,699,370✔
1028
      if (dump_info->vertical) {
24,699,370✔
1029
        shellVerticalPrintResult(tres, dump_info);
8,235,038✔
1030
      } else {
1031
        shellHorizontalPrintResult(tres, dump_info);
16,464,332✔
1032
      }
1033
    }
1034
    dump_info->numOfAllRows += num_of_rows;
24,699,370✔
1035
    if (!shellCmdkilled) {
24,699,370✔
1036
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
24,699,370✔
1037
    } else {
1038
      tsem_post(&dump_info->sem);
×
1039
    }
1040
  } else {
1041
    if (num_of_rows < 0) {
31,599,113✔
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);
31,599,113✔
1045
  }
1046
}
56,298,483✔
1047

1048
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
32,164,889✔
1049
  int64_t num_of_rows = 0;
32,164,889✔
1050
  if (fname != NULL) {
32,164,889✔
1051
    num_of_rows = shellDumpResultToFile(fname, tres);
565,776✔
1052
  } else {
1053
    tsDumpInfo dump_info;
31,598,391✔
1054
    if (!shellCmdkilled) {
31,599,113✔
1055
      init_dump_info(&dump_info, tres, sql, vertical);
31,599,113✔
1056
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
31,599,113✔
1057
      tsem_wait(&dump_info.sem);
31,599,113✔
1058
      num_of_rows = dump_info.numOfAllRows;
31,599,113✔
1059
    }
1060
  }
1061

1062
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
32,164,889✔
1063
  return num_of_rows;
32,164,889✔
1064
}
1065

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

1071
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
812,814✔
1072
  int32_t read_size = 0;
812,814✔
1073
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
160,064,814✔
1074
    line[read_size - 1] = '\0';
159,252,000✔
1075
    taosMemoryFree(pHistory->hist[pHistory->hend]);
159,252,000✔
1076
    pHistory->hist[pHistory->hend] = taosStrdup(line);
159,252,000✔
1077

1078
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
159,252,000✔
1079

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

1085
  taosMemoryFreeClear(line);
812,814✔
1086
  taosCloseFile(&pFile);
812,814✔
1087
  int64_t file_size;
811,544✔
1088
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
812,814✔
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;
812,814✔
1106
}
1107

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

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

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

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

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

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

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

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

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

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

1175
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
112,343✔
1176
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
113,181,045✔
1177
    if (cmd_len + read_len >= tsMaxSQLLength) {
113,068,702✔
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';
113,068,702✔
1185

1186
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
113,068,702✔
1187
      continue;
815,396✔
1188
    }
1189

1190
    if (line[read_len - 1] == '\\') {
112,253,306✔
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') {
112,253,306✔
1198
      line[read_len - 1] = ' ';
×
1199
    }
1200

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

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

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

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

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

1224
  int32_t code = taos_errno(tres);
318✔
1225
  if (code != TSDB_CODE_SUCCESS) {
318✔
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);
318✔
1235
  if (num_fields == 0) {
318✔
1236
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1237
    exit(0);
×
1238
  } else {
1239
    if (tres == NULL) {
318✔
1240
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1241
      exit(0);
×
1242
    }
1243

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

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

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

1260
    if (strcmp(serverVersion, "community") == 0) {
318✔
1261
      verType = TSDB_VERSION_OSS;
×
1262
    } else if (strcmp(expiretime, "unlimited") == 0) {
318✔
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;
318✔
1267
      sprintf(buf, "Server is %s %s. License will expire at %s.\r\n", serverVersion, sinfo, expiretime);
318✔
1268
    }
1269

1270
    taos_free_result(tres);
318✔
1271
  }
1272

1273
  fprintf(stdout, "\r\n");
318✔
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;
318✔
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); }
636✔
1288
#endif
1289

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

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

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

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

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

1314
  return NULL;
318✔
1315
}
1316

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

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

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

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

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

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

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

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

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

1366
#pragma GCC diagnostic pop
1367

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

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

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

1390
    if (pArgs->port_inputted) {
850,211✔
1391
      port = pArgs->port;
761✔
1392
    } else {
1393
      port = defaultPort(pArgs->connMode, pArgs->dsn);
849,450✔
1394
    }
1395

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

1398
    // connect normal
1399
    if (pArgs->auth) {
850,211✔
1400
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
178✔
1401
    } else {
1402
#ifdef TD_ENTERPRISE 
1403
      if (strlen(pArgs->token) > 0) {
850,033✔
1404
        // token
1405
        printf("Connect with token ...");
176✔
1406
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
176✔
1407
        if (taos != NULL) {
176✔
1408
          printf("... [ OK ]\n");
88✔
1409
          return taos;
88✔
1410
        }
1411
        printf("... [ FAILED ]\n");
88✔
1412
        return NULL;
88✔
1413
      }
1414
#endif      
1415
      taos = taos_connect(host, user, pwd, pArgs->database, port);
849,857✔
1416
    }
1417

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

1440
  return taos;
851,260✔
1441
}
1442

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

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

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

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

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

1470
  if (runOnce) {
850,014✔
1471
    if (pArgs->commands != NULL) {
849,696✔
1472
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
718,097✔
1473
      char *cmd = taosStrdup(pArgs->commands);
718,097✔
1474
      shellRunCommand(cmd, true);
718,097✔
1475
      taosMemoryFree(cmd);
718,097✔
1476
    }
1477

1478
    if (pArgs->file[0] != 0) {
849,696✔
1479
      shellSourceFile(pArgs->file);
131,599✔
1480
    }
1481

1482
    taos_close(shell.conn);
849,696✔
1483

1484
    shellWriteHistory();
849,696✔
1485
    shellCleanupHistory();
849,696✔
1486
    return 0;
849,696✔
1487
  }
1488

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

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

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

1501
  char    buf[512] = {0};
318✔
1502
  int32_t verType = shellGetGrantInfo(buf);
318✔
1503
#ifndef WINDOWS
1504
  printfIntroduction(verType);
318✔
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) {
318✔
1512
    printf("%s\n", buf);
318✔
1513
  }
1514

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

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

1529
  taosThreadJoin(spid, NULL);
318✔
1530

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

1535
  TAOS_RETURN(code);
318✔
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