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

taosdata / TDengine / #4981

11 Mar 2026 06:29AM UTC coverage: 68.488% (-0.004%) from 68.492%
#4981

push

travis-ci

web-flow
enh: [6548485194] Support push down ts condition in vtable query. (#34718)

62 of 76 new or added lines in 2 files covered. (81.58%)

859 existing lines in 123 files now uncovered.

211924 of 309434 relevant lines covered (68.49%)

135102255.25 hits per line

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

81.02
/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) {
217,670,472✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
217,799,015✔
72
    if (c != ' ' && c != '\t' && c != ';') {
146,517,947✔
73
      return false;
146,389,404✔
74
    }
75
  }
76
  return true;
71,281,068✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
144,539,588✔
80
  shellCmdkilled = false;
144,539,588✔
81

82
  if (shellIsEmptyCommand(command)) {
144,539,588✔
83
    return 0;
71,281,068✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
73,258,520✔
87
    return -1;
549✔
88
  }
89

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

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

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

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

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

165
  // add help or help;
166
  if (strncasecmp(command, "help", 4) == 0) {
73,130,884✔
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);
73,130,754✔
174

175
  char quote = 0, *cmd = command;
73,130,754✔
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,993✔
179
      continue;
2,993✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
13,082,212✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
13,082,212✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
71,409,383✔
188
      *command = 0;
71,409,383✔
189
      if (shellRunSingleCommand(cmd) < 0) {
71,409,383✔
190
        return -1;
549✔
191
      }
192
      *command = c;
71,408,834✔
193
      cmd = command;
71,408,834✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
73,130,205✔
197
}
198

199
char *strendG(const char *pstr) {
73,251,359✔
200
  if (pstr == NULL) {
73,251,359✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
73,251,359✔
205
  if (len < 4) {
73,251,359✔
206
    return NULL;
×
207
  }
208

209
  char *p = (char *)pstr + len - 2;
73,251,359✔
210
  if (strcmp(p, "\\G") == 0) {
73,251,359✔
211
    return p;
6,059✔
212
  }
213

214
  p = (char *)pstr + len - 3;
73,245,300✔
215
  if (strcmp(p, "\\G;") == 0) {
73,245,300✔
216
    return p;
79,981✔
217
  }
218

219
  return NULL;
73,165,319✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
73,251,359✔
230
    fname = sptr + 2;
610,777✔
231
    while (*fname == ' ') fname++;
1,221,297✔
232
    *sptr = '\0';
610,777✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
73,251,359✔
241
    *sptr = '\0';
86,040✔
242
    printMode = true;  // When output to a file, the switch does not work.
86,040✔
243
  }
244

245
  st = taosGetTimestampUs();
73,251,359✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
73,251,359✔
248
  if (taos_errno(pSql)) {
73,251,359✔
249
    shellPrintError(pSql, st);
26,799,761✔
250
    return;
26,799,761✔
251
  }
252

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

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

259
    taos_free_result(pSql);
16,197✔
260

261
    return;
16,197✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
46,435,401✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,435,401✔
267
    pre = "Delete OK";
64✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,435,337✔
269
    pre = "Insert OK";
6,081✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,429,256✔
271
    pre = "Create OK";
4,480✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
46,424,776✔
273
    pre = "Drop OK";
983✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
46,435,401✔
277
  if (pFields != NULL) {  // select and show kinds of commands
46,435,401✔
278
    int32_t error_no = 0;
45,072,749✔
279

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

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

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

301
  printf("\r\n");
46,435,401✔
302
}
303

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

310
  time_t  tt;
762,798,130✔
311
  int32_t ms = 0;
763,248,069✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
763,248,069✔
313
    tt = (time_t)(val / 1000000000);
1,270✔
314
    ms = val % 1000000000;
1,270✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
763,246,799✔
316
    tt = (time_t)(val / 1000000);
1,270✔
317
    ms = val % 1000000;
1,270✔
318
  } else {
319
    tt = (time_t)(val / 1000);
763,245,529✔
320
    ms = val % 1000;
763,245,529✔
321
  }
322

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

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
763,248,069✔
341
    sprintf(buf + pos, ".%09d", ms);
1,270✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
763,246,799✔
343
    sprintf(buf + pos, ".%06d", ms);
1,270✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
763,245,529✔
346
  }
347

348
  return buf;
763,248,069✔
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,502,102,404✔
361
  if (val == NULL) {
1,502,102,404✔
362
    taosFprintfFile(pFile, "NULL");
12,880,768✔
363
    return;
12,880,768✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,489,221,636✔
367
  int32_t width;
368

369
  int n = 0;
1,489,221,636✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,489,221,636✔
372
  switch (field->type) {
1,489,221,636✔
373
    case TSDB_DATA_TYPE_BOOL:
287,110,732✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
287,110,732✔
375
      break;
287,110,732✔
376
    case TSDB_DATA_TYPE_TINYINT:
18,238,126✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
18,238,126✔
378
      break;
18,238,126✔
379
    case TSDB_DATA_TYPE_UTINYINT:
2,990,730✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
2,990,730✔
381
      break;
2,990,730✔
382
    case TSDB_DATA_TYPE_SMALLINT:
1,499,170✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
1,499,170✔
384
      break;
1,499,170✔
385
    case TSDB_DATA_TYPE_USMALLINT:
1,491,560✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
1,491,560✔
387
      break;
1,491,560✔
388
    case TSDB_DATA_TYPE_INT:
144,135,869✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
144,135,869✔
390
      break;
144,135,869✔
391
    case TSDB_DATA_TYPE_UINT:
1,494,604✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
1,494,604✔
393
      break;
1,494,604✔
394
    case TSDB_DATA_TYPE_BIGINT:
176,542,650✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
176,542,650✔
396
      break;
176,542,650✔
UNCOV
397
    case TSDB_DATA_TYPE_UBIGINT:
×
UNCOV
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
×
UNCOV
399
      break;
×
400
    case TSDB_DATA_TYPE_FLOAT:
19,734,000✔
401
      width = SHELL_FLOAT_WIDTH;
19,734,000✔
402
      if (tsEnableScience) {
19,734,000✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
19,734,000✔
406
        if (n > SHELL_FLOAT_WIDTH) {
19,734,000✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
929,942✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
18,804,058✔
410
        }
411
      }
412
      break;
19,734,000✔
413
    case TSDB_DATA_TYPE_DOUBLE:
235,657,290✔
414
      width = SHELL_DOUBLE_WIDTH;
235,657,290✔
415
      if (tsEnableScience) {
235,657,290✔
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));
235,657,290✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
235,657,290✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
122,817,778✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
112,839,512✔
424
        }
425
      }
426
      break;
235,657,290✔
427
    case TSDB_DATA_TYPE_BINARY:
224,892,609✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
224,892,609✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
224,892,609✔
432
      if (tmp == NULL) break;
224,892,609✔
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;
224,892,609✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
224,892,609✔
444
      taosMemoryFree(tmp);
224,892,609✔
445
    } break;
224,892,609✔
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:
167,986,067✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
167,986,067✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
167,986,067✔
467
      break;
167,986,067✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
207,448,229✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
207,448,229✔
471
      break;
207,448,229✔
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) {
590,359✔
490
  char fullname[PATH_MAX] = {0};
590,359✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
590,359✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
1,427,330✔
511
    if (col > 0) {
836,971✔
512
      taosFprintfFile(pFile, ",");
246,612✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
836,971✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
590,359✔
517

518
  int64_t numOfRows = 0;
590,359✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
690,021,407✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
522
      if (i > 0) {
1,502,102,404✔
523
        taosFprintfFile(pFile, ",");
812,080,997✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,502,102,404✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
690,021,407✔
528

529
    numOfRows++;
690,021,407✔
530
    row = taos_fetch_row(tres);
690,021,407✔
531
  } while (row != NULL);
690,021,407✔
532

533
  taosCloseFile(&pFile);
590,359✔
534

535
  return numOfRows;
590,359✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
306,922,880✔
539
  TdWchar tail[3];
306,731,085✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
306,922,880✔
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;
139,001✔
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,382,612✔
562
    }
563

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

569
    totalCols += w;
2,147,483,647✔
570
    if (totalCols > width) {
2,147,483,647✔
571
      break;
994,873✔
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,209,497✔
578
      tailLen++;
3,209,497✔
579
    }
580
  }
581

582
  if (totalCols > width) {
306,922,880✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
3,979,492✔
585
      if (cols >= width) {
2,984,619✔
586
        break;
×
587
      }
588
      putchar('.');
2,984,619✔
589
      ++cols;
2,984,619✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
306,157,385✔
593
      printf("%lc", tail[i]);
229,378✔
594
    }
595
    cols = totalCols;
305,928,007✔
596
  }
597

598
  for (; cols < width; cols++) {
2,147,483,647✔
599
    putchar(' ');
2,147,483,647✔
600
  }
601
}
306,922,880✔
602

603
void shellPrintString(const char *str, int32_t width) {
1,466,993,149✔
604
  int32_t len = strlen(str);
1,466,993,149✔
605

606
  if (width == 0) {
1,466,993,149✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
1,466,993,149✔
609
    if (width <= 3) {
6,888✔
610
      printf("%.*s.", width - 1, str);
6,888✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
1,466,986,261✔
616
  }
617
}
1,466,993,149✔
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,242,228,896✔
648
    return;
1,242,228,896✔
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:
224,764,253✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
224,764,253✔
657
      break;
224,764,253✔
658
    case TSDB_DATA_TYPE_TINYINT:
60,576,447✔
659
      printf("%*d", width, *((int8_t *)val));
60,576,447✔
660
      break;
60,576,447✔
661
    case TSDB_DATA_TYPE_UTINYINT:
128,324,412✔
662
      printf("%*u", width, *((uint8_t *)val));
128,324,412✔
663
      break;
128,324,412✔
664
    case TSDB_DATA_TYPE_SMALLINT:
47,511,880✔
665
      printf("%*d", width, *((int16_t *)val));
47,511,880✔
666
      break;
47,511,880✔
667
    case TSDB_DATA_TYPE_USMALLINT:
115,403,602✔
668
      printf("%*u", width, *((uint16_t *)val));
115,403,602✔
669
      break;
115,403,602✔
670
    case TSDB_DATA_TYPE_INT:
153,629,234✔
671
      printf("%*d", width, *((int32_t *)val));
153,629,234✔
672
      break;
153,629,234✔
673
    case TSDB_DATA_TYPE_UINT:
163,227,346✔
674
      printf("%*u", width, *((uint32_t *)val));
163,227,346✔
675
      break;
163,227,346✔
676
    case TSDB_DATA_TYPE_BIGINT:
416,036,414✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
416,036,414✔
678
      break;
416,036,414✔
679
    case TSDB_DATA_TYPE_UBIGINT:
170,191,435✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
170,191,435✔
681
      break;
170,191,435✔
682
    case TSDB_DATA_TYPE_FLOAT:
75,207,971✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
75,207,971✔
684
      if (tsEnableScience) {
75,207,971✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
75,207,971✔
688
        printf("%s", buf);
75,207,971✔
689
      }
690
      break;
75,207,971✔
691
    case TSDB_DATA_TYPE_DOUBLE:
325,147,926✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
325,147,926✔
693
      if (tsEnableScience) {
325,147,926✔
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));
325,147,926✔
698
        printf("%*s", width, buf);
325,147,926✔
699
      }
700
      break;
325,147,926✔
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:
306,922,880✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
306,922,880✔
715
      break;
306,922,880✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
595,283,149✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
595,283,149✔
721
      printf("%s", buf);
595,283,149✔
722
      break;
595,283,149✔
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:
130✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
130✔
738
    default:
130✔
739
      break;
130✔
740
  }
741
}
742

743
// show whole result for this query return true, like limit or describe
744
bool shellIsShowWhole(const char *sql) {
419✔
745
  // limit
746
  char * p = taosStrCaseStr(sql, " limit ");
419✔
747
  if (p != NULL) {
419✔
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) {
280✔
759
    return true;
71✔
760
  }
761
  // desc
762
  if (taosStrCaseStr(sql, "desc ") != NULL) {
209✔
763
    return true;
71✔
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,482,390✔
787
  dump_info->sql = sql;
44,482,390✔
788
  dump_info->vertical = vertical;
44,482,390✔
789
  tsem_init(&dump_info->sem, 0, 0);
44,482,390✔
790
  dump_info->numOfAllRows = 0;
44,482,390✔
791

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

796
  dump_info->resShowMaxNum = UINT64_MAX;
44,482,390✔
797

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

802
  if (vertical) {
44,482,390✔
803
    dump_info->maxColNameLen = 0;
83,904✔
804
    for (int32_t col = 0; col < dump_info->numFields; col++) {
174,748✔
805
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
90,844✔
806
      if (len > dump_info->maxColNameLen) {
90,844✔
807
        dump_info->maxColNameLen = len;
86,002✔
808
      }
809
    }
810
  } else {
811
    for (int32_t col = 0; col < dump_info->numFields; col++) {
125,494,890✔
812
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
81,096,404✔
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,398,486✔
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,398,486✔
818
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
819
    }
820
  }
821
}
44,482,390✔
822

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

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

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

836
    int32_t *length = taos_fetch_lengths(tres);
1,618,152✔
837

838
    for (int32_t i = 0; i < dump_info->numFields; i++) {
3,250,429✔
839
      TAOS_FIELD *field = dump_info->fields + i;
1,632,277✔
840

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

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

849
    numOfPintRows++;
1,618,152✔
850
    numOfPrintRowsThisOne++;
1,618,152✔
851

852
    if (numOfPintRows == dump_info->resShowMaxNum) {
1,618,152✔
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,618,152✔
864
      return;
83,904✔
865
    }
866

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

872
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
81,096,404✔
873
  int32_t width = (int32_t)strlen(field->name);
81,096,404✔
874

875
  switch (field->type) {
81,096,404✔
876
    case TSDB_DATA_TYPE_NULL:
×
877
      return TMAX(4, width);  // null
×
878
    case TSDB_DATA_TYPE_BOOL:
18,884,904✔
879
      return TMAX(5, width);  // 'false'
18,884,904✔
880

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

885
    case TSDB_DATA_TYPE_SMALLINT:
3,348,428✔
886
    case TSDB_DATA_TYPE_USMALLINT:
887
      return TMAX(6, width);  // '-32767'
3,348,428✔
888

889
    case TSDB_DATA_TYPE_INT:
8,132,572✔
890
    case TSDB_DATA_TYPE_UINT:
891
      return TMAX(11, width);  // '-2147483648'
8,132,572✔
892

893
    case TSDB_DATA_TYPE_BIGINT:
7,845,757✔
894
    case TSDB_DATA_TYPE_UBIGINT:
895
      return TMAX(21, width);  // '-9223372036854775807'
7,845,757✔
896

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

900
    case TSDB_DATA_TYPE_DOUBLE:
7,724,555✔
901
      return TMAX(SHELL_DOUBLE_WIDTH, width);
7,724,555✔
902

903
    case TSDB_DATA_TYPE_BINARY:
5,214,472✔
904
    case TSDB_DATA_TYPE_GEOMETRY:
905
      if (field->bytes > shell.args.displayWidth) {
5,214,472✔
906
        return TMAX(shell.args.displayWidth, width);
3,465,681✔
907
      } else {
908
        return TMAX(field->bytes + 2, width);
1,748,791✔
909
      }
910
    case TSDB_DATA_TYPE_VARBINARY: {
804✔
911
      int32_t bytes = field->bytes * 2 + 2;
804✔
912
      if (bytes > shell.args.displayWidth) {
804✔
913
        return TMAX(shell.args.displayWidth, width);
×
914
      } else {
915
        return TMAX(bytes + 2, width);
804✔
916
      }
917
    }
918
    case TSDB_DATA_TYPE_NCHAR:
3,498,842✔
919
    case TSDB_DATA_TYPE_JSON: {
920
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
3,498,842✔
921
      if (bytes > shell.args.displayWidth) {
3,498,842✔
922
        return TMAX(shell.args.displayWidth, width);
3,498,842✔
923
      } else {
924
        return TMAX(bytes + 2, width);
×
925
      }
926
    }
927

928
    case TSDB_DATA_TYPE_TIMESTAMP:
20,079,528✔
929
      if (shell.args.is_raw_time) {
20,079,528✔
930
        return TMAX(14, width);
409✔
931
      }
932
      if (precision == TSDB_TIME_PRECISION_NANO) {
20,079,119✔
933
        return TMAX(29, width);
127✔
934
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
20,078,992✔
935
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
127✔
936
      } else {
937
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
20,078,865✔
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:
65✔
950
      return TMAX(width, 20);
65✔
951
    case TSDB_DATA_TYPE_DECIMAL:
65✔
952
      return TMAX(width, 40);
65✔
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,467,077✔
961
  int32_t rowWidth = 0;
31,467,077✔
962
  for (int32_t col = 0; col < num_fields; col++) {
95,012,686✔
963
    TAOS_FIELD *field = fields + col;
63,545,609✔
964
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
63,545,609✔
965
    int32_t     left = padding / 2;
63,545,609✔
966
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
63,545,609✔
967
    rowWidth += width[col] + 3;
63,545,609✔
968
  }
969

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

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

986
  int64_t numOfPintRows = dump_info->numOfAllRows;
32,834,100✔
987
  int     numOfPrintRowsThisOne = 0;
32,834,100✔
988
  if (numOfPintRows == 0) {
32,834,100✔
989
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
31,467,077✔
990
  }
991

992
  while (row != NULL) {
570,458,191✔
993
    int32_t *length = taos_fetch_lengths(tres);
570,458,191✔
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');
570,458,191✔
1002
    putchar('\n');
570,458,191✔
1003

1004
    numOfPintRows++;
570,458,191✔
1005
    numOfPrintRowsThisOne++;
570,458,191✔
1006

1007
    if (numOfPintRows == dump_info->resShowMaxNum) {
570,458,191✔
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) {
570,458,122✔
1023
      return;
32,834,031✔
1024
    }
1025

1026
    row = taos_fetch_row(tres);
537,624,091✔
1027
  }
1028
  return;
×
1029
}
1030

1031
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
77,400,394✔
1032
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
77,400,394✔
1033
  if (num_of_rows > 0) {
77,400,394✔
1034
    dump_info->numOfRows = num_of_rows;
32,918,004✔
1035
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
32,918,004✔
1036
      if (dump_info->vertical) {
32,918,004✔
1037
        shellVerticalPrintResult(tres, dump_info);
83,904✔
1038
      } else {
1039
        shellHorizontalPrintResult(tres, dump_info);
32,834,100✔
1040
      }
1041
    }
1042
    dump_info->numOfAllRows += num_of_rows;
32,918,004✔
1043
    if (!shellCmdkilled) {
32,918,004✔
1044
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
32,918,004✔
1045
    } else {
1046
      tsem_post(&dump_info->sem);
×
1047
    }
1048
  } else {
1049
    if (num_of_rows < 0) {
44,482,390✔
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,482,390✔
1053
  }
1054
}
77,400,327✔
1055

1056
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
45,072,749✔
1057
  int64_t num_of_rows = 0;
45,072,749✔
1058
  if (fname != NULL) {
45,072,749✔
1059
    num_of_rows = shellDumpResultToFile(fname, tres);
590,359✔
1060
  } else {
1061
    tsDumpInfo dump_info;
40,063,012✔
1062
    if (!shellCmdkilled) {
44,482,390✔
1063
      init_dump_info(&dump_info, tres, sql, vertical);
44,482,390✔
1064
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
44,482,390✔
1065
      tsem_wait(&dump_info.sem);
44,482,390✔
1066
      num_of_rows = dump_info.numOfAllRows;
44,482,390✔
1067
    }
1068
  }
1069

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

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

1079
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
841,907✔
1080
  int32_t read_size = 0;
841,907✔
1081
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
167,198,449✔
1082
    line[read_size - 1] = '\0';
166,356,542✔
1083
    taosMemoryFree(pHistory->hist[pHistory->hend]);
166,356,542✔
1084
    pHistory->hist[pHistory->hend] = taosStrdup(line);
166,356,542✔
1085

1086
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
166,356,542✔
1087

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

1093
  taosMemoryFreeClear(line);
841,907✔
1094
  taosCloseFile(&pFile);
841,907✔
1095
  int64_t file_size;
840,139✔
1096
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
841,907✔
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;
841,907✔
1114
}
1115

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

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

1133
void shellCleanupHistory() {
885,994✔
1134
  SShellHistory *pHistory = &shell.history;
885,994✔
1135
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
886,879,994✔
1136
    if (pHistory->hist[i] != NULL) {
885,994,000✔
1137
      taosMemoryFree(pHistory->hist[i]);
166,356,542✔
1138
      pHistory->hist[i] = NULL;
166,356,542✔
1139
    }
1140
  }
1141
}
885,994✔
1142

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

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

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

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

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

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

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

1183
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
113,567✔
1184
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
73,359,199✔
1185
    if (cmd_len + read_len >= tsMaxSQLLength) {
73,245,632✔
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';
73,245,632✔
1193

1194
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
73,245,632✔
1195
      continue;
867,343✔
1196
    }
1197

1198
    if (line[read_len - 1] == '\\') {
72,378,289✔
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') {
72,378,289✔
1206
      line[read_len - 1] = ' ';
×
1207
    }
1208

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

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

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

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

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

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

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

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

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

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

1278
    taos_free_result(tres);
758✔
1279
  }
1280

1281
  fprintf(stdout, "\r\n");
758✔
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;
758✔
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); }
678✔
1296
#endif
1297

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

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

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

1312
    if (shell.conn) {
961✔
1313
      shellCmdkilled = true;
203✔
1314
      taos_kill_query(shell.conn);
203✔
1315
    }
1316

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

1322
  return NULL;
758✔
1323
}
1324

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

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

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

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

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

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

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

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

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

1374
#pragma GCC diagnostic pop
1375

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

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

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

1398
    if (pArgs->port_inputted) {
889,148✔
1399
      port = pArgs->port;
779✔
1400
    } else {
1401
      port = defaultPort(pArgs->connMode, pArgs->dsn);
888,369✔
1402
    }
1403

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

1406
    // connect normal
1407
    if (pArgs->auth) {
889,148✔
1408
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
211✔
1409
    } else {
1410
#ifdef TD_ENTERPRISE 
1411
      if (strlen(pArgs->token) > 0) {
888,937✔
1412
        // token
1413
        printf("Connect with token ...");
2,513✔
1414
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
2,513✔
1415
        if (taos != NULL) {
2,513✔
1416
          printf("... [ OK ]\n");
1,172✔
1417
          return taos;
1,172✔
1418
        }
1419
        printf("... [ FAILED ]\n");
1,341✔
1420
        return NULL;
1,341✔
1421
      }
1422
#endif      
1423
      taos = taos_connect(host, user, pwd, pArgs->database, port);
886,424✔
1424
    }
1425

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

1448
  return taos;
886,037✔
1449
}
1450

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

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

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

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

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

1478
  if (runOnce) {
885,994✔
1479
    if (pArgs->commands != NULL) {
885,236✔
1480
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
751,837✔
1481
      char *cmd = taosStrdup(pArgs->commands);
751,837✔
1482
      shellRunCommand(cmd, true);
751,837✔
1483
      taosMemoryFree(cmd);
751,837✔
1484
    }
1485

1486
    if (pArgs->file[0] != 0) {
885,236✔
1487
      shellSourceFile(pArgs->file);
133,399✔
1488
    }
1489

1490
    taos_close(shell.conn);
885,236✔
1491

1492
    shellWriteHistory();
885,236✔
1493
    shellCleanupHistory();
885,236✔
1494
    return 0;
885,236✔
1495
  }
1496

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

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

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

1509
  char    buf[512] = {0};
758✔
1510
  int32_t verType = shellGetGrantInfo(buf);
758✔
1511
#ifndef WINDOWS
1512
  printfIntroduction(verType);
758✔
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) {
758✔
1520
    printf("%s\n", buf);
758✔
1521
  }
1522

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

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

1537
  taosThreadJoin(spid, NULL);
758✔
1538

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

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