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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

136170362.0 hits per line

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

80.91
/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) {
214,731,031✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
214,856,695✔
72
    if (c != ' ' && c != '\t' && c != ';') {
144,550,741✔
73
      return false;
144,425,077✔
74
    }
75
  }
76
  return true;
70,305,954✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
142,580,879✔
80
  shellCmdkilled = false;
142,580,879✔
81

82
  if (shellIsEmptyCommand(command)) {
142,580,879✔
83
    return 0;
70,305,954✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
72,274,925✔
87
    return -1;
550✔
88
  }
89

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
72,150,022✔
174

175
  char quote = 0, *cmd = command;
72,150,022✔
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++;
2,974✔
179
      continue;
2,974✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
13,853,999✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
13,853,999✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
70,431,407✔
188
      *command = 0;
70,431,407✔
189
      if (shellRunSingleCommand(cmd) < 0) {
70,431,407✔
190
        return -1;
550✔
191
      }
192
      *command = c;
70,430,857✔
193
      cmd = command;
70,430,857✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
72,149,472✔
197
}
198

199
char *strendG(const char *pstr) {
72,270,385✔
200
  if (pstr == NULL) {
72,270,385✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
72,270,385✔
205
  if (len < 4) {
72,270,385✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
72,270,385✔
210
  if (strcmp(p, "\\G") == 0) {
72,270,385✔
211
    return p;
6,010✔
212
  }
213

214
  p = (char *)pstr + len - 3;
72,264,375✔
215
  if (strcmp(p, "\\G;") == 0) {
72,264,375✔
216
    return p;
79,480✔
217
  }
218

219
  return NULL;
72,184,895✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
72,270,385✔
230
    fname = sptr + 2;
604,983✔
231
    while (*fname == ' ') fname++;
1,209,711✔
232
    *sptr = '\0';
604,983✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
72,270,385✔
241
    *sptr = '\0';
85,490✔
242
    printMode = true;  // When output to a file, the switch does not work.
85,490✔
243
  }
244

245
  st = taosGetTimestampUs();
72,270,385✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
72,270,385✔
248
  if (taos_errno(pSql)) {
72,270,385✔
249
    shellPrintError(pSql, st);
25,629,946✔
250
    return;
25,629,946✔
251
  }
252

253
  if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$", REG_EXTENDED | REG_ICASE)) {
46,640,439✔
254
    printf("Database changed.\r\n\r\n");
15,932✔
255

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

259
    taos_free_result(pSql);
15,932✔
260

261
    return;
15,932✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
46,624,507✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,624,507✔
267
    pre = "Delete OK";
60✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,624,447✔
269
    pre = "Insert OK";
5,950✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,618,497✔
271
    pre = "Create OK";
4,416✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,614,081✔
273
    pre = "Drop OK";
924✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
46,624,507✔
277
  if (pFields != NULL) {  // select and show kinds of commands
46,624,507✔
278
    int32_t error_no = 0;
45,289,630✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
45,289,630✔
281
    if (numOfRows < 0) return;
45,289,630✔
282

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

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

301
  printf("\r\n");
46,624,507✔
302
}
303

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

310
  time_t  tt;
987,989,647✔
311
  int32_t ms = 0;
988,413,194✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
988,413,194✔
313
    tt = (time_t)(val / 1000000000);
1,200✔
314
    ms = val % 1000000000;
1,200✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
988,411,994✔
316
    tt = (time_t)(val / 1000000);
1,200✔
317
    ms = val % 1000000;
1,200✔
318
  } else {
319
    tt = (time_t)(val / 1000);
988,410,794✔
320
    ms = val % 1000;
988,410,794✔
321
  }
322

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

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
988,413,194✔
341
    sprintf(buf + pos, ".%09d", ms);
1,200✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
988,411,994✔
343
    sprintf(buf + pos, ".%06d", ms);
1,200✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
988,410,794✔
346
  }
347

348
  return buf;
988,413,194✔
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,480,619,496✔
361
  if (val == NULL) {
1,480,619,496✔
362
    taosFprintfFile(pFile, "NULL");
12,903,958✔
363
    return;
12,903,958✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,467,715,538✔
367
  int32_t width;
368

369
  int n = 0;
1,467,715,538✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,467,715,538✔
372
  switch (field->type) {
1,467,715,538✔
373
    case TSDB_DATA_TYPE_BOOL:
281,834,720✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
281,834,720✔
375
      break;
281,834,720✔
376
    case TSDB_DATA_TYPE_TINYINT:
18,908,570✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
18,908,570✔
378
      break;
18,908,570✔
379
    case TSDB_DATA_TYPE_UTINYINT:
1,472,020✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
1,472,020✔
381
      break;
1,472,020✔
382
    case TSDB_DATA_TYPE_SMALLINT:
2,934,306✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
2,934,306✔
384
      break;
2,934,306✔
385
    case TSDB_DATA_TYPE_USMALLINT:
2,217,448✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
2,217,448✔
387
      break;
2,217,448✔
388
    case TSDB_DATA_TYPE_INT:
141,657,006✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
141,657,006✔
390
      break;
141,657,006✔
UNCOV
391
    case TSDB_DATA_TYPE_UINT:
×
UNCOV
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
×
UNCOV
393
      break;
×
394
    case TSDB_DATA_TYPE_BIGINT:
172,974,005✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
172,974,005✔
396
      break;
172,974,005✔
397
    case TSDB_DATA_TYPE_UBIGINT:
2,202,714✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
2,202,714✔
399
      break;
2,202,714✔
400
    case TSDB_DATA_TYPE_FLOAT:
16,699,190✔
401
      width = SHELL_FLOAT_WIDTH;
16,699,190✔
402
      if (tsEnableScience) {
16,699,190✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
16,699,190✔
406
        if (n > SHELL_FLOAT_WIDTH) {
16,699,190✔
UNCOV
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
16,699,190✔
410
        }
411
      }
412
      break;
16,699,190✔
413
    case TSDB_DATA_TYPE_DOUBLE:
231,797,651✔
414
      width = SHELL_DOUBLE_WIDTH;
231,797,651✔
415
      if (tsEnableScience) {
231,797,651✔
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));
231,797,651✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
231,797,651✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
122,396,591✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
109,401,060✔
424
        }
425
      }
426
      break;
231,797,651✔
427
    case TSDB_DATA_TYPE_BINARY:
223,616,743✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
223,616,743✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
223,616,743✔
432
      if (tmp == NULL) break;
223,616,743✔
433
      for (int32_t i = 0; i < length; i++) {
2,147,483,647✔
434
        tmp[bufIndex] = val[i];
2,147,483,647✔
435
        bufIndex++;
2,147,483,647✔
436
        if (val[i] == '\"') {
2,147,483,647✔
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
223,616,743✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
223,616,743✔
444
      taosMemoryFree(tmp);
223,616,743✔
445
    } break;
223,616,743✔
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:
164,853,801✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
164,853,801✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
164,853,801✔
467
      break;
164,853,801✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
206,547,364✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
206,547,364✔
471
      break;
206,547,364✔
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) {
584,993✔
490
  char fullname[PATH_MAX] = {0};
584,993✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
584,993✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
1,413,317✔
511
    if (col > 0) {
828,324✔
512
      taosFprintfFile(pFile, ",");
243,331✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
828,324✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
584,993✔
517

518
  int64_t numOfRows = 0;
584,993✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
684,328,702✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
522
      if (i > 0) {
1,480,619,496✔
523
        taosFprintfFile(pFile, ",");
796,290,794✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,480,619,496✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
684,328,702✔
528

529
    numOfRows++;
684,328,702✔
530
    row = taos_fetch_row(tres);
684,328,702✔
531
  } while (row != NULL);
684,328,702✔
532

533
  taosCloseFile(&pFile);
584,993✔
534

535
  return numOfRows;
584,993✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
510,620,375✔
539
  TdWchar tail[3];
510,435,959✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
510,620,375✔
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;
150,012✔
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;
4,336,003✔
562
    }
563

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

569
    totalCols += w;
2,147,483,647✔
570
    if (totalCols > width) {
2,147,483,647✔
571
      break;
1,000,526✔
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;
3,248,499✔
578
      tailLen++;
3,248,499✔
579
    }
580
  }
581

582
  if (totalCols > width) {
510,620,375✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
4,002,104✔
585
      if (cols >= width) {
3,001,578✔
586
        break;
×
587
      }
588
      putchar('.');
3,001,578✔
589
      ++cols;
3,001,578✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
509,871,261✔
593
      printf("%lc", tail[i]);
251,412✔
594
    }
595
    cols = totalCols;
509,619,849✔
596
  }
597

598
  for (; cols < width; cols++) {
2,147,483,647✔
599
    putchar(' ');
2,147,483,647✔
600
  }
601
}
510,620,375✔
602

603
void shellPrintString(const char *str, int32_t width) {
2,147,483,647✔
604
  int32_t len = strlen(str);
2,147,483,647✔
605

606
  if (width == 0) {
2,147,483,647✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
2,147,483,647✔
609
    if (width <= 3) {
6,561✔
610
      printf("%.*s.", width - 1, str);
6,561✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
2,147,483,647✔
616
  }
617
}
2,147,483,647✔
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);
2,051,406,005✔
648
    return;
2,051,406,005✔
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:
324,683,982✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
324,683,982✔
657
      break;
324,683,982✔
658
    case TSDB_DATA_TYPE_TINYINT:
105,248,378✔
659
      printf("%*d", width, *((int8_t *)val));
105,248,378✔
660
      break;
105,248,378✔
661
    case TSDB_DATA_TYPE_UTINYINT:
194,320,611✔
662
      printf("%*u", width, *((uint8_t *)val));
194,320,611✔
663
      break;
194,320,611✔
664
    case TSDB_DATA_TYPE_SMALLINT:
81,330,695✔
665
      printf("%*d", width, *((int16_t *)val));
81,330,695✔
666
      break;
81,330,695✔
667
    case TSDB_DATA_TYPE_USMALLINT:
194,223,369✔
668
      printf("%*u", width, *((uint16_t *)val));
194,223,369✔
669
      break;
194,223,369✔
670
    case TSDB_DATA_TYPE_INT:
231,875,670✔
671
      printf("%*d", width, *((int32_t *)val));
231,875,670✔
672
      break;
231,875,670✔
673
    case TSDB_DATA_TYPE_UINT:
213,191,941✔
674
      printf("%*u", width, *((uint32_t *)val));
213,191,941✔
675
      break;
213,191,941✔
676
    case TSDB_DATA_TYPE_BIGINT:
521,003,565✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
521,003,565✔
678
      break;
521,003,565✔
679
    case TSDB_DATA_TYPE_UBIGINT:
240,744,264✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
240,744,264✔
681
      break;
240,744,264✔
682
    case TSDB_DATA_TYPE_FLOAT:
126,077,263✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
126,077,263✔
684
      if (tsEnableScience) {
126,077,263✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
126,077,263✔
688
        printf("%s", buf);
126,077,263✔
689
      }
690
      break;
126,077,263✔
691
    case TSDB_DATA_TYPE_DOUBLE:
423,636,371✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
423,636,371✔
693
      if (tsEnableScience) {
423,636,371✔
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));
423,636,371✔
698
        printf("%*s", width, buf);
423,636,371✔
699
      }
700
      break;
423,636,371✔
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:
510,620,375✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
510,620,375✔
715
      break;
510,620,375✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
823,580,540✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
823,580,540✔
721
      printf("%s", buf);
823,580,540✔
722
      break;
823,580,540✔
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:
136✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
136✔
738
    default:
136✔
739
      break;
136✔
740
  }
741
}
742

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

774
  return false;
69✔
775
}
776

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

783
  return false;
69✔
784
}
785

786
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
44,704,637✔
787
  dump_info->sql = sql;
44,704,637✔
788
  dump_info->vertical = vertical;
44,704,637✔
789
  tsem_init(&dump_info->sem, 0, 0);
44,704,637✔
790
  dump_info->numOfAllRows = 0;
44,704,637✔
791

792
  dump_info->numFields = taos_num_fields(tres);
44,704,637✔
793
  dump_info->fields = taos_fetch_fields(tres);
44,704,637✔
794
  dump_info->precision = taos_result_precision(tres);
44,704,637✔
795

796
  dump_info->resShowMaxNum = UINT64_MAX;
44,704,637✔
797

798
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
44,704,637✔
799
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
69✔
800
  }
801

802
  if (vertical) {
44,704,637✔
803
    dump_info->maxColNameLen = 0;
83,374✔
804
    for (int32_t col = 0; col < dump_info->numFields; col++) {
173,569✔
805
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
90,195✔
806
      if (len > dump_info->maxColNameLen) {
90,195✔
807
        dump_info->maxColNameLen = len;
85,434✔
808
      }
809
    }
810
  } else {
811
    for (int32_t col = 0; col < dump_info->numFields; col++) {
136,947,604✔
812
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
92,326,341✔
813
    }
814
    // set an appropriate width for token and totp_secret display
815
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
44,621,263✔
816
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
817
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
44,621,263✔
818
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
819
    }
820
  }
821
}
44,704,637✔
822

823
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
83,374✔
824
  TAOS_ROW row = taos_fetch_row(tres);
83,374✔
825
  if (row == NULL) {
83,374✔
826
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
827
    return;
×
828
  }
829

830
  int64_t numOfPintRows = dump_info->numOfAllRows;
83,374✔
831
  int     numOfPrintRowsThisOne = 0;
83,374✔
832

833
  while (row != NULL) {
1,608,399✔
834
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
1,608,399✔
835

836
    int32_t *length = taos_fetch_lengths(tres);
1,608,399✔
837

838
    for (int32_t i = 0; i < dump_info->numFields; i++) {
3,230,578✔
839
      TAOS_FIELD *field = dump_info->fields + i;
1,622,179✔
840

841
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
1,622,179✔
842
      printf("%*.s%s: ", padding, " ", field->name);
1,622,179✔
843

844
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
1,622,179✔
845
      putchar('\r');
1,622,179✔
846
      putchar('\n');
1,622,179✔
847
    }
848

849
    numOfPintRows++;
1,608,399✔
850
    numOfPrintRowsThisOne++;
1,608,399✔
851

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

863
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,608,399✔
864
      return;
83,374✔
865
    }
866

867
    row = taos_fetch_row(tres);
1,525,025✔
868
  }
869
  return;
×
870
}
871

872
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
92,326,341✔
873
  int32_t width = (int32_t)strlen(field->name);
92,326,341✔
874

875
  switch (field->type) {
92,326,341✔
876
    case TSDB_DATA_TYPE_NULL:
×
877
      return TMAX(4, width);  // null
×
878
    case TSDB_DATA_TYPE_BOOL:
19,194,425✔
879
      return TMAX(5, width);  // 'false'
19,194,425✔
880

881
    case TSDB_DATA_TYPE_TINYINT:
5,668,450✔
882
    case TSDB_DATA_TYPE_UTINYINT:
883
      return TMAX(4, width);  // '-127'
5,668,450✔
884

885
    case TSDB_DATA_TYPE_SMALLINT:
4,761,621✔
886
    case TSDB_DATA_TYPE_USMALLINT:
887
      return TMAX(6, width);  // '-32767'
4,761,621✔
888

889
    case TSDB_DATA_TYPE_INT:
9,509,107✔
890
    case TSDB_DATA_TYPE_UINT:
891
      return TMAX(11, width);  // '-2147483648'
9,509,107✔
892

893
    case TSDB_DATA_TYPE_BIGINT:
9,699,287✔
894
    case TSDB_DATA_TYPE_UBIGINT:
895
      return TMAX(21, width);  // '-9223372036854775807'
9,699,287✔
896

897
    case TSDB_DATA_TYPE_FLOAT:
2,866,878✔
898
      return TMAX(SHELL_FLOAT_WIDTH, width);
2,866,878✔
899

900
    case TSDB_DATA_TYPE_DOUBLE:
8,725,947✔
901
      return TMAX(SHELL_DOUBLE_WIDTH, width);
8,725,947✔
902

903
    case TSDB_DATA_TYPE_BINARY:
6,396,946✔
904
    case TSDB_DATA_TYPE_GEOMETRY:
905
      if (field->bytes > shell.args.displayWidth) {
6,396,946✔
906
        return TMAX(shell.args.displayWidth, width);
4,083,223✔
907
      } else {
908
        return TMAX(field->bytes + 2, width);
2,313,723✔
909
      }
910
    case TSDB_DATA_TYPE_VARBINARY: {
828✔
911
      int32_t bytes = field->bytes * 2 + 2;
828✔
912
      if (bytes > shell.args.displayWidth) {
828✔
913
        return TMAX(shell.args.displayWidth, width);
×
914
      } else {
915
        return TMAX(bytes + 2, width);
828✔
916
      }
917
    }
918
    case TSDB_DATA_TYPE_NCHAR:
4,717,185✔
919
    case TSDB_DATA_TYPE_JSON: {
920
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
4,717,185✔
921
      if (bytes > shell.args.displayWidth) {
4,717,185✔
922
        return TMAX(shell.args.displayWidth, width);
4,717,185✔
923
      } else {
924
        return TMAX(bytes + 2, width);
×
925
      }
926
    }
927

928
    case TSDB_DATA_TYPE_TIMESTAMP:
20,785,531✔
929
      if (shell.args.is_raw_time) {
20,785,531✔
930
        return TMAX(14, width);
415✔
931
      }
932
      if (precision == TSDB_TIME_PRECISION_NANO) {
20,785,116✔
933
        return TMAX(29, width);
120✔
934
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
20,784,996✔
935
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
120✔
936
      } else {
937
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
20,784,876✔
938
      }
939
    case TSDB_DATA_TYPE_BLOB:
×
940
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
941
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
942
      if (bytes > shell.args.displayWidth) {
×
943
        return TMAX(shell.args.displayWidth, width);
×
944
      } else {
945
        return TMAX(bytes + 2, width);
×
946
      }
947
    } break;
948

949
    case TSDB_DATA_TYPE_DECIMAL64:
68✔
950
      return TMAX(width, 20);
68✔
951
    case TSDB_DATA_TYPE_DECIMAL:
68✔
952
      return TMAX(width, 40);
68✔
953
    default:
×
954
      ASSERT(false);
×
955
  }
956

957
  return 0;
×
958
}
959

960
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
31,927,815✔
961
  int32_t rowWidth = 0;
31,927,815✔
962
  for (int32_t col = 0; col < num_fields; col++) {
105,300,189✔
963
    TAOS_FIELD *field = fields + col;
73,372,374✔
964
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
73,372,374✔
965
    int32_t     left = padding / 2;
73,372,374✔
966
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
73,372,374✔
967
    rowWidth += width[col] + 3;
73,372,374✔
968
  }
969

970
  putchar('\r');
31,927,815✔
971
  putchar('\n');
31,927,815✔
972
  for (int32_t i = 0; i < rowWidth; i++) {
2,147,483,647✔
973
    putchar('=');
2,147,483,647✔
974
  }
975
  putchar('\r');
31,927,815✔
976
  putchar('\n');
31,927,815✔
977
}
31,927,815✔
978

979
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
34,284,928✔
980
  TAOS_ROW row = taos_fetch_row(tres);
34,284,928✔
981
  if (row == NULL) {
34,284,928✔
982
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
983
    return;
×
984
  }
985

986
  int64_t numOfPintRows = dump_info->numOfAllRows;
34,284,928✔
987
  int     numOfPrintRowsThisOne = 0;
34,284,928✔
988
  if (numOfPintRows == 0) {
34,284,928✔
989
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
31,927,815✔
990
  }
991

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

1004
    numOfPintRows++;
825,029,254✔
1005
    numOfPrintRowsThisOne++;
825,029,254✔
1006

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

1022
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
825,029,185✔
1023
      return;
34,284,859✔
1024
    }
1025

1026
    row = taos_fetch_row(tres);
790,744,326✔
1027
  }
1028
  return;
×
1029
}
1030

1031
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
79,072,939✔
1032
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
79,072,939✔
1033
  if (num_of_rows > 0) {
79,072,939✔
1034
    dump_info->numOfRows = num_of_rows;
34,368,302✔
1035
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
34,368,302✔
1036
      if (dump_info->vertical) {
34,368,302✔
1037
        shellVerticalPrintResult(tres, dump_info);
83,374✔
1038
      } else {
1039
        shellHorizontalPrintResult(tres, dump_info);
34,284,928✔
1040
      }
1041
    }
1042
    dump_info->numOfAllRows += num_of_rows;
34,368,302✔
1043
    if (!shellCmdkilled) {
34,368,302✔
1044
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
34,368,302✔
1045
    } else {
1046
      tsem_post(&dump_info->sem);
×
1047
    }
1048
  } else {
1049
    if (num_of_rows < 0) {
44,704,637✔
1050
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1051
    }
1052
    tsem_post(&dump_info->sem);
44,704,637✔
1053
  }
1054
}
79,072,939✔
1055

1056
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
45,289,630✔
1057
  int64_t num_of_rows = 0;
45,289,630✔
1058
  if (fname != NULL) {
45,289,630✔
1059
    num_of_rows = shellDumpResultToFile(fname, tres);
584,993✔
1060
  } else {
1061
    tsDumpInfo dump_info;
40,567,187✔
1062
    if (!shellCmdkilled) {
44,704,637✔
1063
      init_dump_info(&dump_info, tres, sql, vertical);
44,704,637✔
1064
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
44,704,637✔
1065
      tsem_wait(&dump_info.sem);
44,704,637✔
1066
      num_of_rows = dump_info.numOfAllRows;
44,704,637✔
1067
    }
1068
  }
1069

1070
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
45,289,630✔
1071
  return num_of_rows;
45,289,630✔
1072
}
1073

1074
void shellReadHistory() {
902,369✔
1075
  SShellHistory *pHistory = &shell.history;
902,369✔
1076
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
902,369✔
1077
  if (pFile == NULL) return;
902,369✔
1078

1079
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
857,307✔
1080
  int32_t read_size = 0;
857,307✔
1081
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
165,393,567✔
1082
    line[read_size - 1] = '\0';
164,536,260✔
1083
    taosMemoryFree(pHistory->hist[pHistory->hend]);
164,536,260✔
1084
    pHistory->hist[pHistory->hend] = taosStrdup(line);
164,536,260✔
1085

1086
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
164,536,260✔
1087

1088
    if (pHistory->hend == pHistory->hstart) {
164,536,260✔
1089
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1090
    }
1091
  }
1092

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

1109
    /* coverity[+retval] */
1110
    taosFsyncFile(pFile);
×
1111
    taosCloseFile(&pFile);
×
1112
  }
1113
  pHistory->hstart = pHistory->hend;
857,307✔
1114
}
1115

1116
void shellWriteHistory() {
902,369✔
1117
  SShellHistory *pHistory = &shell.history;
902,369✔
1118
  if (pHistory->hend == pHistory->hstart) return;
902,369✔
1119
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
902,239✔
1120
  if (pFile == NULL) return;
902,239✔
1121

1122
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
1,804,598✔
1123
    if (pHistory->hist[i] != NULL) {
902,359✔
1124
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
902,359✔
1125
      taosMemoryFree(pHistory->hist[i]);
902,359✔
1126
      pHistory->hist[i] = NULL;
902,359✔
1127
    }
1128
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
902,359✔
1129
  }
1130
  taosCloseFile(&pFile);
902,239✔
1131
}
1132

1133
void shellCleanupHistory() {
902,369✔
1134
  SShellHistory *pHistory = &shell.history;
902,369✔
1135
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
903,271,369✔
1136
    if (pHistory->hist[i] != NULL) {
902,369,000✔
1137
      taosMemoryFree(pHistory->hist[i]);
164,536,260✔
1138
      pHistory->hist[i] = NULL;
164,536,260✔
1139
    }
1140
  }
1141
}
902,369✔
1142

1143
void shellPrintError(TAOS_RES *tres, int64_t st) {
25,629,946✔
1144
  int code = taos_errno(tres);
25,629,946✔
1145
  int64_t et = taosGetTimestampUs();
25,629,946✔
1146
  printf("\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), code, (et - st) / 1E6);
25,629,946✔
1147
  taos_free_result(tres);
25,629,946✔
1148

1149
  // tip
1150
  if (code == TSDB_CODE_MND_USER_PASSWORD_EXPIRED) {
25,629,946✔
1151
    fprintf(stdout, "******************** TIPS ********************\n");
49✔
1152
    fprintf(stdout, "Please reset your password using the `ALTER USER <user_name> PASS 'new_password'` command.\n");
49✔
1153
    fprintf(stdout, "**********************************************\n");
49✔
1154
  }
1155
}
25,629,946✔
1156

1157
bool shellIsCommentLine(char *line) {
71,407,045✔
1158
  if (line == NULL) return true;
71,407,045✔
1159
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
71,407,045✔
1160
}
1161

1162
void shellSourceFile(const char *file) {
158,968✔
1163
  int32_t read_len = 0;
158,968✔
1164
  char   *cmd = taosMemoryCalloc(1, tsMaxSQLLength + 1);
158,968✔
1165
  size_t  cmd_len = 0;
158,968✔
1166
  char    fullname[PATH_MAX] = {0};
158,968✔
1167
  char    sourceFileCommand[PATH_MAX + 8] = {0};
158,968✔
1168

1169
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
158,968✔
1170
    tstrncpy(fullname, file, PATH_MAX);
×
1171
  }
1172

1173
  sprintf(sourceFileCommand, "source %s;", fullname);
158,968✔
1174
  shellRecordCommandToHistory(sourceFileCommand);
158,968✔
1175

1176
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
158,968✔
1177
  if (pFile == NULL) {
158,968✔
1178
    fprintf(stderr, "failed to open file %s\r\n", fullname);
19,777✔
1179
    taosMemoryFree(cmd);
19,777✔
1180
    return;
19,777✔
1181
  }
1182

1183
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
139,191✔
1184
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
72,541,672✔
1185
    if (cmd_len + read_len >= tsMaxSQLLength) {
72,402,481✔
1186
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1187
             read_len);
1188
      cmd_len = 0;
×
1189
      memset(line, 0, tsMaxSQLLength + 1);
×
1190
      continue;
×
1191
    }
1192
    line[--read_len] = '\0';
72,402,481✔
1193

1194
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
72,402,481✔
1195
      continue;
995,850✔
1196
    }
1197

1198
    if (line[read_len - 1] == '\\') {
71,406,631✔
1199
      line[read_len - 1] = ' ';
×
1200
      memcpy(cmd + cmd_len, line, read_len);
×
1201
      cmd_len += read_len;
×
1202
      continue;
×
1203
    }
1204

1205
    if (line[read_len - 1] == '\r') {
71,406,631✔
1206
      line[read_len - 1] = ' ';
×
1207
    }
1208

1209
    memcpy(cmd + cmd_len, line, read_len);
71,406,631✔
1210
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
71,406,631✔
1211
    shellRunCommand(cmd, false);
71,406,631✔
1212
    memset(cmd, 0, tsMaxSQLLength);
71,406,631✔
1213
    cmd_len = 0;
71,406,631✔
1214
  }
1215

1216
  taosMemoryFree(cmd);
139,191✔
1217
  taosMemoryFreeClear(line);
139,191✔
1218
  taosCloseFile(&pFile);
139,191✔
1219
}
1220

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

1227
#ifndef TD_ASTRA
1228
  char sql[] = "show grants";
762✔
1229

1230
  TAOS_RES *tres = taos_query(shell.conn, sql);
762✔
1231

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

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

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

1262
    tstrncpy(serverVersion, row[0], 64);
762✔
1263
    memcpy(expiretime, row[1], fields[1].bytes);
762✔
1264
    memcpy(expired, row[2], fields[2].bytes);
762✔
1265

1266
    trimStr(serverVersion, "trial");
762✔
1267

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

1278
    taos_free_result(tres);
762✔
1279
  }
1280

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

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

1298
void shellCleanup(void *arg) { taosResetTerminalMode(); }
762✔
1299

1300
void *shellCancelHandler(void *arg) {
762✔
1301
  setThreadName("shellCancelHandler");
762✔
1302
  while (1) {
1303
    if (shell.exit == true) {
1,869✔
1304
      break;
762✔
1305
    }
1306

1307
    if (tsem_wait(&shell.cancelSem) != 0) {
1,107✔
1308
      taosMsleep(10);
×
1309
      continue;
×
1310
    }
1311

1312
    if (shell.conn) {
1,107✔
1313
      shellCmdkilled = true;
345✔
1314
      taos_kill_query(shell.conn);
345✔
1315
    }
1316

1317
#ifdef WINDOWS
1318
    printf("\n%s", shell.info.promptHeader);
1319
#endif
1320
  }
1321

1322
  return NULL;
762✔
1323
}
1324

1325
#pragma GCC diagnostic push
1326
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1327

1328
void *shellThreadLoop(void *arg) {
762✔
1329
  setThreadName("shellThreadLoop");
762✔
1330
  taosGetOldTerminalMode();
762✔
1331
  taosThreadCleanupPush(shellCleanup, NULL);
762✔
1332

1333
  do {
1334
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
762✔
1335
    if (command == NULL) {
762✔
1336
      printf("failed to malloc command\r\n");
×
1337
      break;
×
1338
    }
1339

1340
    do {
1341
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
1,107✔
1342
      taosSetTerminalMode();
1,107✔
1343

1344
      if (shellReadCommand(command) != 0) {
1,107✔
1345
        break;
345✔
1346
      }
1347

1348
      taosResetTerminalMode();
762✔
1349
    } while (shellRunCommand(command, true) == 0);
762✔
1350

1351
    taosMemoryFreeClear(command);
762✔
1352
    shellWriteHistory();
762✔
1353
    shellExit();
762✔
1354
  } while (0);
1355

1356
  taosThreadCleanupPop(1);
762✔
1357
  return NULL;
762✔
1358
}
1359

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

1374
#pragma GCC diagnostic pop
1375

1376
TAOS *createConnect(SShellArgs *pArgs) {
906,955✔
1377
  char     show[256] = "\0";
906,955✔
1378
  char    *host = NULL;
906,955✔
1379
  uint16_t port = 0;
906,955✔
1380
  char    *user = NULL;
906,955✔
1381
  char    *pwd = NULL;
906,955✔
1382
  TAOS    *taos = NULL;
906,955✔
1383

1384
  // set mode
1385
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
906,955✔
1386
    // websocket
1387
    memcpy(show, pArgs->dsn, 20);
1,471✔
1388
    memcpy(show + 20, "...", 3);
1,471✔
1389
    memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
1,471✔
1390

1391
    // connect dsn
1392
    taos = taos_connect_with_dsn(pArgs->dsn);
1,471✔
1393
  } else {
1394
    host = (char *)pArgs->host;
905,484✔
1395
    user = (char *)pArgs->user;
905,484✔
1396
    pwd = pArgs->password;
905,484✔
1397

1398
    if (pArgs->port_inputted) {
905,484✔
1399
      port = pArgs->port;
781✔
1400
    } else {
1401
      port = defaultPort(pArgs->connMode, pArgs->dsn);
904,703✔
1402
    }
1403

1404
    sprintf(show, "host:%s port:%d ", host, port);
905,484✔
1405

1406
    // connect normal
1407
    if (pArgs->auth) {
905,484✔
1408
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
206✔
1409
    } else {
1410
#ifdef TD_ENTERPRISE 
1411
      if (strlen(pArgs->token) > 0) {
905,278✔
1412
        // token
1413
        printf("Connect with token ...");
2,447✔
1414
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,447✔
1415
        if (taos != NULL) {
2,447✔
1416
          printf("... [ OK ]\n");
1,138✔
1417
          return taos;
1,138✔
1418
        }
1419
        printf("... [ FAILED ]\n");
1,309✔
1420
        return NULL;
1,309✔
1421
      }
1422
#endif      
1423
      taos = taos_connect(host, user, pwd, pArgs->database, port);
902,831✔
1424
    }
1425

1426
    if (taos == NULL) {
903,037✔
1427
      // failed
1428
      int code = taos_errno(NULL);
4,494✔
1429
      if (code == TSDB_CODE_MND_WRONG_TOTP_CODE) {
4,494✔
1430
         // totp
1431
        char totpCode[TSDB_USER_PASSWORD_LONGLEN];
2,290✔
1432
        memset(totpCode, 0, sizeof(totpCode));  
2,290✔
1433
        if (inputTotpCode(totpCode)) {
2,290✔
1434
          printf("Connect with TOTP code:%s ...", totpCode);
2,118✔
1435
          taos = taos_connect_totp(host, user, pwd, totpCode, pArgs->database, port);
2,118✔
1436
          if (taos != NULL) {
2,118✔
1437
            printf("... [ OK ]\n");
1,429✔
1438
            return taos;
1,429✔
1439
          }
1440
          printf("... [ FAILED ]\n");
689✔
1441
          return NULL;
689✔
1442
        }
1443
      }
1444
      // token
1445
    }
1446
  }
1447

1448
  return taos;
902,390✔
1449
}
1450

1451
int32_t shellExecute(int argc, char *argv[]) {
906,955✔
1452
  int32_t code = 0;
906,955✔
1453
  printf(shell.info.clientVersion, shell.info.cusName,
1,813,910✔
1454
         workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
906,955✔
1455
         taos_get_client_info(), shell.info.cusName);
1456
  fflush(stdout);
906,955✔
1457

1458
  SShellArgs *pArgs = &shell.args;
906,955✔
1459
  shell.conn = createConnect(pArgs);
906,955✔
1460

1461
  if (shell.conn == NULL) {
906,955✔
1462
    printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
4,586✔
1463
           ERROR_CODE_DETAIL);
1464
    fflush(stdout);
4,586✔
1465
    return -1;
4,586✔
1466
  }
1467

1468
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
902,369✔
1469
  shellSetConn(shell.conn, runOnce);
902,369✔
1470
  shellReadHistory();
902,369✔
1471

1472
  if (shell.args.is_bi_mode) {
902,369✔
1473
    // need set bi mode
1474
    printf("Set BI mode is true.\n");
469✔
1475
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
469✔
1476
  }
1477

1478
  if (runOnce) {
902,369✔
1479
    if (pArgs->commands != NULL) {
901,607✔
1480
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
742,759✔
1481
      char *cmd = taosStrdup(pArgs->commands);
742,759✔
1482
      shellRunCommand(cmd, true);
742,759✔
1483
      taosMemoryFree(cmd);
742,759✔
1484
    }
1485

1486
    if (pArgs->file[0] != 0) {
901,607✔
1487
      shellSourceFile(pArgs->file);
158,848✔
1488
    }
1489

1490
    taos_close(shell.conn);
901,607✔
1491

1492
    shellWriteHistory();
901,607✔
1493
    shellCleanupHistory();
901,607✔
1494
    return 0;
901,607✔
1495
  }
1496

1497
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
762✔
1498
    printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1499
    return code;
×
1500
  }
1501

1502
  TdThread spid = {0};
762✔
1503
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
762✔
1504

1505
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
762✔
1506
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
762✔
1507
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
762✔
1508

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

1523
  while (1) {
1524
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
762✔
1525
    taosThreadJoin(shell.pid, NULL);
762✔
1526
    taosThreadClear(&shell.pid);
762✔
1527
    if (shell.exit) {
762✔
1528
      tsem_post(&shell.cancelSem);
762✔
1529
      break;
762✔
1530
    }
1531
  }
1532

1533
  if (verType == TSDB_VERSION_OSS) {
762✔
1534
    showAD(true);
×
1535
  }
1536

1537
  taosThreadJoin(spid, NULL);
762✔
1538

1539
  shellCleanupHistory();
762✔
1540
  taos_kill_query(shell.conn);
762✔
1541
  taos_close(shell.conn);
762✔
1542

1543
  TAOS_RETURN(code);
762✔
1544
}
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