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

taosdata / TDengine / #4914

06 Jan 2026 01:30AM UTC coverage: 64.876% (-0.008%) from 64.884%
#4914

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

3475 existing lines in 124 files now uncovered.

194993 of 300563 relevant lines covered (64.88%)

116239151.85 hits per line

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

77.83
/tools/shell/src/shellEngine.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define ALLOW_FORBID_FUNC
17
#define _BSD_SOURCE
18
#define _GNU_SOURCE
19
#define _XOPEN_SOURCE
20
#define _DEFAULT_SOURCE
21
#include "../../inc/pub.h"
22
#include "geosWrapper.h"
23
#include "shellAuto.h"
24
#include "shellInt.h"
25

26
SShellObj shell = {0};
27

28
typedef struct {
29
  const char *sql;
30
  bool        vertical;
31
  tsem_t      sem;
32
  int64_t     numOfRows;  // the num of this batch
33
  int64_t     numOfAllRows;
34

35
  int32_t     numFields;
36
  TAOS_FIELD *fields;
37
  int32_t     precision;
38

39
  int32_t maxColNameLen;            // for vertical print
40
  int32_t width[TSDB_MAX_COLUMNS];  // for horizontal print
41

42
  uint64_t resShowMaxNum;
43
} tsDumpInfo;
44

45
static bool    shellIsEmptyCommand(const char *cmd);
46
static int32_t shellRunSingleCommand(char *command);
47
static void    shellRecordCommandToHistory(char *command);
48
static int32_t shellRunCommand(char *command, bool recordHistory);
49
static void    shellRunSingleCommandImp(char *command);
50
static char   *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision);
51
static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
52
static void    shellPrintNChar(const char *str, int32_t length, int32_t width);
53
static void    shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width);
54
static void    shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
55
static void    shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
56
static int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql);
57
static void    shellReadHistory();
58
static void    shellWriteHistory();
59
static void    shellPrintError(TAOS_RES *tres, int64_t st);
60
static bool    shellIsCommentLine(char *line);
61
static void    shellSourceFile(const char *file);
62
static int32_t shellGetGrantInfo(char *buf);
63

64
static void  shellCleanup(void *arg);
65
static void *shellCancelHandler(void *arg);
66
static void *shellThreadLoop(void *arg);
67

68
static bool shellCmdkilled = false;
69

70
bool shellIsEmptyCommand(const char *cmd) {
205,208,058✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
205,321,446✔
72
    if (c != ' ' && c != '\t' && c != ';') {
138,214,389✔
73
      return false;
138,101,001✔
74
    }
75
  }
76
  return true;
67,107,057✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
136,213,946✔
80
  shellCmdkilled = false;
136,213,946✔
81

82
  if (shellIsEmptyCommand(command)) {
136,213,946✔
83
    return 0;
67,107,057✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
69,106,889✔
87
    return -1;
80✔
88
  }
89

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

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

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

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

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

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

173
  if (recordHistory) shellRecordCommandToHistory(command);
68,994,035✔
174

175
  char quote = 0, *cmd = command;
68,994,035✔
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,751✔
179
      continue;
2,751✔
180
    }
181

182
    if (quote == c) {
2,147,483,647✔
183
      quote = 0;
1,734,268✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
2,147,483,647✔
185
      quote = c;
1,734,268✔
186
    } else if (c == ';' && quote == 0) {
2,147,483,647✔
187
      c = *command;
67,219,991✔
188
      *command = 0;
67,219,991✔
189
      if (shellRunSingleCommand(cmd) < 0) {
67,219,991✔
190
        return -1;
80✔
191
      }
192
      *command = c;
67,219,911✔
193
      cmd = command;
67,219,911✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
68,993,955✔
197
}
198

199
char *strendG(const char *pstr) {
69,106,661✔
200
  if (pstr == NULL) {
69,106,661✔
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
69,106,661✔
205
  if (len < 4) {
69,106,661✔
206
    return NULL;
×
207
  }
208

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

214
  p = (char *)pstr + len - 3;
69,101,416✔
215
  if (strcmp(p, "\\G;") == 0) {
69,101,416✔
216
    return p;
57,814✔
217
  }
218

219
  return NULL;
69,043,602✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
69,106,661✔
230
    fname = sptr + 2;
910,390✔
231
    while (*fname == ' ') fname++;
1,820,622✔
232
    *sptr = '\0';
910,390✔
233

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

240
  if ((sptr = strendG(command)) != NULL) {
69,106,661✔
241
    *sptr = '\0';
63,059✔
242
    printMode = true;  // When output to a file, the switch does not work.
63,059✔
243
  }
244

245
  st = taosGetTimestampUs();
69,106,661✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
69,106,661✔
248
  if (taos_errno(pSql)) {
69,106,661✔
249
    shellPrintError(pSql, st);
52,350,411✔
250
    return;
52,350,411✔
251
  }
252

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

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

259
    taos_free_result(pSql);
12,309✔
260

261
    return;
12,309✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
16,743,941✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
16,743,941✔
267
    pre = "Delete OK";
37✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
16,743,904✔
269
    pre = "Insert OK";
3,134✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
16,740,770✔
271
    pre = "Create OK";
3,449✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
16,737,321✔
273
    pre = "Drop OK";
603✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
16,743,941✔
277
  if (pFields != NULL) {  // select and show kinds of commands
16,743,941✔
278
    int32_t error_no = 0;
15,515,067✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
15,515,067✔
281
    if (numOfRows < 0) return;
15,515,067✔
282

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

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

301
  printf("\r\n");
16,743,941✔
302
}
303

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

310
  time_t  tt;
163,243,245✔
311
  int32_t ms = 0;
163,255,470✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
163,255,470✔
313
    tt = (time_t)(val / 1000000000);
740✔
314
    ms = val % 1000000000;
740✔
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
163,254,730✔
316
    tt = (time_t)(val / 1000000);
740✔
317
    ms = val % 1000000;
740✔
318
  } else {
319
    tt = (time_t)(val / 1000);
163,253,990✔
320
    ms = val % 1000;
163,253,990✔
321
  }
322

323
  if (tt <= 0 && ms < 0) {
163,255,470✔
324
    tt--;
8,459✔
325
    if (precision == TSDB_TIME_PRECISION_NANO) {
8,459✔
326
      ms += 1000000000;
×
327
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
8,459✔
328
      ms += 1000000;
×
329
    } else {
330
      ms += 1000;
8,459✔
331
    }
332
  }
333

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
163,255,470✔
341
    sprintf(buf + pos, ".%09d", ms);
740✔
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
163,254,730✔
343
    sprintf(buf + pos, ".%06d", ms);
740✔
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
163,253,990✔
346
  }
347

348
  return buf;
163,255,470✔
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,840,998,554✔
361
  if (val == NULL) {
1,840,998,554✔
362
    taosFprintfFile(pFile, "NULL");
10,022,894✔
363
    return;
10,022,894✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
1,830,975,660✔
367
  int32_t width;
368

369
  int n = 0;
1,830,975,660✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
1,830,975,660✔
372
  switch (field->type) {
1,830,975,660✔
373
    case TSDB_DATA_TYPE_BOOL:
522,983,395✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
522,983,395✔
375
      break;
522,983,395✔
376
    case TSDB_DATA_TYPE_TINYINT:
31,549,379✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
31,549,379✔
378
      break;
31,549,379✔
379
    case TSDB_DATA_TYPE_UTINYINT:
692,736✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
692,736✔
381
      break;
692,736✔
382
    case TSDB_DATA_TYPE_SMALLINT:
686,831✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
686,831✔
384
      break;
686,831✔
385
    case TSDB_DATA_TYPE_USMALLINT:
2,068,929✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
2,068,929✔
387
      break;
2,068,929✔
388
    case TSDB_DATA_TYPE_INT:
121,003,096✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
121,003,096✔
390
      break;
121,003,096✔
391
    case TSDB_DATA_TYPE_UINT:
2,068,929✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
2,068,929✔
393
      break;
2,068,929✔
394
    case TSDB_DATA_TYPE_BIGINT:
172,674,436✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
172,674,436✔
396
      break;
172,674,436✔
397
    case TSDB_DATA_TYPE_UBIGINT:
1,379,286✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
1,379,286✔
399
      break;
1,379,286✔
400
    case TSDB_DATA_TYPE_FLOAT:
31,549,135✔
401
      width = SHELL_FLOAT_WIDTH;
31,549,135✔
402
      if (tsEnableScience) {
31,549,135✔
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
31,549,135✔
406
        if (n > SHELL_FLOAT_WIDTH) {
31,549,135✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
170,829✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
31,378,306✔
410
        }
411
      }
412
      break;
31,549,135✔
413
    case TSDB_DATA_TYPE_DOUBLE:
268,294,471✔
414
      width = SHELL_DOUBLE_WIDTH;
268,294,471✔
415
      if (tsEnableScience) {
268,294,471✔
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));
268,294,471✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
268,294,471✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
167,772,093✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
100,522,378✔
424
        }
425
      }
426
      break;
268,294,471✔
427
    case TSDB_DATA_TYPE_BINARY:
207,518,252✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
207,518,252✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
207,518,252✔
432
      if (tmp == NULL) break;
207,518,252✔
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;
207,518,252✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
207,518,252✔
444
      taosMemoryFree(tmp);
207,518,252✔
445
    } break;
207,518,252✔
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:
159,804,460✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
159,804,460✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
159,804,460✔
467
      break;
159,804,460✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
308,702,325✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
308,702,325✔
471
      break;
308,702,325✔
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) {
891,794✔
490
  char fullname[PATH_MAX] = {0};
891,794✔
491
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
891,794✔
492
    tstrncpy(fullname, fname, PATH_MAX);
×
493
  }
494

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

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

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

510
  for (int32_t col = 0; col < num_fields; col++) {
2,174,714✔
511
    if (col > 0) {
1,282,920✔
512
      taosFprintfFile(pFile, ",");
391,126✔
513
    }
514
    taosFprintfFile(pFile, "%s", fields[col].name);
1,282,920✔
515
  }
516
  taosFprintfFile(pFile, "\r\n");
891,794✔
517

518
  int64_t numOfRows = 0;
891,794✔
519
  do {
520
    int32_t *length = taos_fetch_lengths(tres);
972,422,337✔
521
    for (int32_t i = 0; i < num_fields; i++) {
2,147,483,647✔
522
      if (i > 0) {
1,840,998,554✔
523
        taosFprintfFile(pFile, ",");
868,576,217✔
524
      }
525
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
1,840,998,554✔
526
    }
527
    taosFprintfFile(pFile, "\r\n");
972,422,337✔
528

529
    numOfRows++;
972,422,337✔
530
    row = taos_fetch_row(tres);
972,422,337✔
531
  } while (row != NULL);
972,422,337✔
532

533
  taosCloseFile(&pFile);
891,794✔
534

535
  return numOfRows;
891,794✔
536
}
537

538
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
10,417,373✔
539
  TdWchar tail[3];
10,406,357✔
540
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
10,417,373✔
541

542
  while (pos < length) {
191,558,639✔
543
    TdWchar wc;
181,812,275✔
544
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
181,869,344✔
545
    if (bytes <= 0) {
181,869,344✔
546
      break;
4,343✔
547
    }
548

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

560
    if (w <= 0) {
181,865,001✔
561
      continue;
×
562
    }
563

564
    if (width <= 0) {
181,865,001✔
565
      printf("%lc", wc);
72,095,653✔
566
      continue;
72,095,653✔
567
    }
568

569
    totalCols += w;
109,769,348✔
570
    if (totalCols > width) {
109,769,348✔
571
      break;
723,735✔
572
    }
573
    if (totalCols <= (width - 3)) {
109,045,613✔
574
      printf("%lc", wc);
106,856,623✔
575
      cols += w;
106,856,623✔
576
    } else {
577
      tail[tailLen] = wc;
2,188,990✔
578
      tailLen++;
2,188,990✔
579
    }
580
  }
581

582
  if (totalCols > width) {
10,417,373✔
583
    // width could be 1 or 2, so printf("...") cannot be used
584
    for (int32_t i = 0; i < 3; i++) {
2,894,940✔
585
      if (cols >= width) {
2,171,205✔
586
        break;
×
587
      }
588
      putchar('.');
2,171,205✔
589
      ++cols;
2,171,205✔
590
    }
591
  } else {
592
    for (int32_t i = 0; i < tailLen; i++) {
9,715,572✔
593
      printf("%lc", tail[i]);
21,934✔
594
    }
595
    cols = totalCols;
9,693,638✔
596
  }
597

598
  for (; cols < width; cols++) {
189,024,228✔
599
    putchar(' ');
178,606,855✔
600
  }
601
}
10,417,373✔
602

603
void shellPrintString(const char *str, int32_t width) {
5,947,909✔
604
  int32_t len = strlen(str);
5,947,909✔
605

606
  if (width == 0) {
5,947,909✔
607
    printf("%s", str);
×
608
  } else if (len > width) {
5,947,909✔
609
    if (width <= 3) {
12,114✔
610
      printf("%.*s.", width - 1, str);
12,114✔
611
    } else {
612
      printf("%.*s...", width - 3, str);
×
613
    }
614
  } else {
615
    printf("%s%*.s", str, width - len, "");
5,935,795✔
616
  }
617
}
5,947,909✔
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) {
41,400,846✔
646
  if (val == NULL) {
41,400,846✔
647
    shellPrintString(TSDB_DATA_NULL_STR, width);
4,956,420✔
648
    return;
4,956,420✔
649
  }
650

651
  int n = 0;
36,444,426✔
652
#define LENGTH 64
653
  char buf[LENGTH] = {0};
36,444,426✔
654
  switch (field->type) {
36,444,426✔
655
    case TSDB_DATA_TYPE_BOOL:
991,489✔
656
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
991,489✔
657
      break;
991,489✔
658
    case TSDB_DATA_TYPE_TINYINT:
140,418✔
659
      printf("%*d", width, *((int8_t *)val));
140,418✔
660
      break;
140,418✔
661
    case TSDB_DATA_TYPE_UTINYINT:
54,668✔
662
      printf("%*u", width, *((uint8_t *)val));
54,668✔
663
      break;
54,668✔
664
    case TSDB_DATA_TYPE_SMALLINT:
501,958✔
665
      printf("%*d", width, *((int16_t *)val));
501,958✔
666
      break;
501,958✔
667
    case TSDB_DATA_TYPE_USMALLINT:
2,559✔
668
      printf("%*u", width, *((uint16_t *)val));
2,559✔
669
      break;
2,559✔
670
    case TSDB_DATA_TYPE_INT:
6,029,567✔
671
      printf("%*d", width, *((int32_t *)val));
6,029,567✔
672
      break;
6,029,567✔
673
    case TSDB_DATA_TYPE_UINT:
10,588✔
674
      printf("%*u", width, *((uint32_t *)val));
10,588✔
675
      break;
10,588✔
676
    case TSDB_DATA_TYPE_BIGINT:
4,925,834✔
677
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
4,925,834✔
678
      break;
4,925,834✔
679
    case TSDB_DATA_TYPE_UBIGINT:
4,207✔
680
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
4,207✔
681
      break;
4,207✔
682
    case TSDB_DATA_TYPE_FLOAT:
414,924✔
683
      width = width >= LENGTH ? LENGTH - 1 : width;
414,924✔
684
      if (tsEnableScience) {
414,924✔
685
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
686
      } else {
687
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
414,924✔
688
        printf("%s", buf);
414,924✔
689
      }
690
      break;
414,924✔
691
    case TSDB_DATA_TYPE_DOUBLE:
9,499,747✔
692
      width = width >= LENGTH ? LENGTH - 1 : width;
9,499,747✔
693
      if (tsEnableScience) {
9,499,747✔
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));
9,499,747✔
698
        printf("%*s", width, buf);
9,499,747✔
699
      }
700
      break;
9,499,747✔
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:
10,417,373✔
712
    case TSDB_DATA_TYPE_NCHAR:
713
    case TSDB_DATA_TYPE_JSON:
714
      shellPrintNChar(val, length, width);
10,417,373✔
715
      break;
10,417,373✔
716
    case TSDB_DATA_TYPE_GEOMETRY:
×
717
      shellPrintGeometry(val, length, width);
×
718
      break;
×
719
    case TSDB_DATA_TYPE_TIMESTAMP:
3,451,010✔
720
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
3,451,010✔
721
      printf("%s", buf);
3,451,010✔
722
      break;
3,451,010✔
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:
84✔
736
    case TSDB_DATA_TYPE_DECIMAL64:
737
      printf("%*s", width, val);
84✔
738
    default:
84✔
739
      break;
84✔
740
  }
741
}
742

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

766
  return false;
×
767
}
768

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

775
  return false;
×
776
}
777

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

784
  dump_info->numFields = taos_num_fields(tres);
14,623,273✔
785
  dump_info->fields = taos_fetch_fields(tres);
14,623,273✔
786
  dump_info->precision = taos_result_precision(tres);
14,623,273✔
787

788
  dump_info->resShowMaxNum = UINT64_MAX;
14,623,273✔
789

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

794
  if (vertical) {
14,623,273✔
795
    dump_info->maxColNameLen = 0;
63,059✔
796
    for (int32_t col = 0; col < dump_info->numFields; col++) {
131,772✔
797
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
68,713✔
798
      if (len > dump_info->maxColNameLen) {
68,713✔
799
        dump_info->maxColNameLen = len;
64,777✔
800
      }
801
    }
802
  } else {
803
    for (int32_t col = 0; col < dump_info->numFields; col++) {
38,619,875✔
804
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
24,059,661✔
805
    }
806
    // set an appropriate width for token and totp_secret display
807
    if (shellRegexMatch(sql, "^[\t ]*create[ \t]+token[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
14,560,214✔
808
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOKEN_DISPLAY_WIDTH);
×
809
    } else if (shellRegexMatch(sql, "^[\t ]*create[ \t]+totp_secret[ \t]+.*", REG_EXTENDED | REG_ICASE)) {
14,560,214✔
NEW
810
      dump_info->width[0] = TMAX(dump_info->width[0], SHELL_SHOW_TOTP_SECRET_DISPLAY_WIDTH);
×
811
    }
812
  }
813
}
14,623,273✔
814

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

822
  int64_t numOfPintRows = dump_info->numOfAllRows;
63,059✔
823
  int     numOfPrintRowsThisOne = 0;
63,059✔
824

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

828
    int32_t *length = taos_fetch_lengths(tres);
1,242,232✔
829

830
    for (int32_t i = 0; i < dump_info->numFields; i++) {
2,494,456✔
831
      TAOS_FIELD *field = dump_info->fields + i;
1,252,224✔
832

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

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

841
    numOfPintRows++;
1,242,232✔
842
    numOfPrintRowsThisOne++;
1,242,232✔
843

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

855
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
1,242,232✔
856
      return;
63,059✔
857
    }
858

859
    row = taos_fetch_row(tres);
1,179,173✔
860
  }
UNCOV
861
  return;
×
862
}
863

864
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
24,059,661✔
865
  int32_t width = (int32_t)strlen(field->name);
24,059,661✔
866

867
  switch (field->type) {
24,059,661✔
UNCOV
868
    case TSDB_DATA_TYPE_NULL:
×
UNCOV
869
      return TMAX(4, width);  // null
×
870
    case TSDB_DATA_TYPE_BOOL:
784,523✔
871
      return TMAX(5, width);  // 'false'
784,523✔
872

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

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

881
    case TSDB_DATA_TYPE_INT:
6,773,444✔
882
    case TSDB_DATA_TYPE_UINT:
883
      return TMAX(11, width);  // '-2147483648'
6,773,444✔
884

885
    case TSDB_DATA_TYPE_BIGINT:
5,101,409✔
886
    case TSDB_DATA_TYPE_UBIGINT:
887
      return TMAX(21, width);  // '-9223372036854775807'
5,101,409✔
888

889
    case TSDB_DATA_TYPE_FLOAT:
69,166✔
890
      return TMAX(SHELL_FLOAT_WIDTH, width);
69,166✔
891

892
    case TSDB_DATA_TYPE_DOUBLE:
7,765,502✔
893
      return TMAX(SHELL_DOUBLE_WIDTH, width);
7,765,502✔
894

895
    case TSDB_DATA_TYPE_BINARY:
1,265,540✔
896
    case TSDB_DATA_TYPE_GEOMETRY:
897
      if (field->bytes > shell.args.displayWidth) {
1,265,540✔
898
        return TMAX(shell.args.displayWidth, width);
821,103✔
899
      } else {
900
        return TMAX(field->bytes + 2, width);
444,437✔
901
      }
902
    case TSDB_DATA_TYPE_VARBINARY: {
504✔
903
      int32_t bytes = field->bytes * 2 + 2;
504✔
904
      if (bytes > shell.args.displayWidth) {
504✔
UNCOV
905
        return TMAX(shell.args.displayWidth, width);
×
906
      } else {
907
        return TMAX(bytes + 2, width);
504✔
908
      }
909
    }
910
    case TSDB_DATA_TYPE_NCHAR:
93,688✔
911
    case TSDB_DATA_TYPE_JSON: {
912
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
93,688✔
913
      if (bytes > shell.args.displayWidth) {
93,688✔
914
        return TMAX(shell.args.displayWidth, width);
93,688✔
915
      } else {
UNCOV
916
        return TMAX(bytes + 2, width);
×
917
      }
918
    }
919

920
    case TSDB_DATA_TYPE_TIMESTAMP:
2,115,266✔
921
      if (shell.args.is_raw_time) {
2,115,266✔
922
        return TMAX(14, width);
126✔
923
      }
924
      if (precision == TSDB_TIME_PRECISION_NANO) {
2,115,140✔
925
        return TMAX(29, width);
74✔
926
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
2,115,066✔
927
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
74✔
928
      } else {
929
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
2,114,992✔
930
      }
UNCOV
931
    case TSDB_DATA_TYPE_BLOB:
×
932
    case TSDB_DATA_TYPE_MEDIUMBLOB: {
933
      int32_t bytes = TSDB_MAX_BLOB_LEN;
×
UNCOV
934
      if (bytes > shell.args.displayWidth) {
×
935
        return TMAX(shell.args.displayWidth, width);
×
936
      } else {
937
        return TMAX(bytes + 2, width);
×
938
      }
939
    } break;
940

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

UNCOV
949
  return 0;
×
950
}
951

952
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
7,948,050✔
953
  int32_t rowWidth = 0;
7,948,050✔
954
  for (int32_t col = 0; col < num_fields; col++) {
20,957,489✔
955
    TAOS_FIELD *field = fields + col;
13,009,439✔
956
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
13,009,439✔
957
    int32_t     left = padding / 2;
13,009,439✔
958
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
13,009,439✔
959
    rowWidth += width[col] + 3;
13,009,439✔
960
  }
961

962
  putchar('\r');
7,948,050✔
963
  putchar('\n');
7,948,050✔
964
  for (int32_t i = 0; i < rowWidth; i++) {
542,194,601✔
965
    putchar('=');
534,246,551✔
966
  }
967
  putchar('\r');
7,948,050✔
968
  putchar('\n');
7,948,050✔
969
}
7,948,050✔
970

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

978
  int64_t numOfPintRows = dump_info->numOfAllRows;
8,073,355✔
979
  int     numOfPrintRowsThisOne = 0;
8,073,355✔
980
  if (numOfPintRows == 0) {
8,073,355✔
981
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
7,948,050✔
982
  }
983

984
  while (row != NULL) {
21,837,333✔
985
    int32_t *length = taos_fetch_lengths(tres);
21,837,333✔
986
    for (int32_t i = 0; i < dump_info->numFields; i++) {
61,985,955✔
987
      putchar(' ');
40,148,622✔
988
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
40,148,622✔
989
                      dump_info->precision);
990
      putchar(' ');
40,148,622✔
991
      putchar('|');
40,148,622✔
992
    }
993
    putchar('\r');
21,837,333✔
994
    putchar('\n');
21,837,333✔
995

996
    numOfPintRows++;
21,837,333✔
997
    numOfPrintRowsThisOne++;
21,837,333✔
998

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

1014
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
21,837,333✔
1015
      return;
8,073,355✔
1016
    }
1017

1018
    row = taos_fetch_row(tres);
13,763,978✔
1019
  }
UNCOV
1020
  return;
×
1021
}
1022

1023
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
22,759,687✔
1024
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
22,759,687✔
1025
  if (num_of_rows > 0) {
22,759,687✔
1026
    dump_info->numOfRows = num_of_rows;
8,136,414✔
1027
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
8,136,414✔
1028
      if (dump_info->vertical) {
8,136,414✔
1029
        shellVerticalPrintResult(tres, dump_info);
63,059✔
1030
      } else {
1031
        shellHorizontalPrintResult(tres, dump_info);
8,073,355✔
1032
      }
1033
    }
1034
    dump_info->numOfAllRows += num_of_rows;
8,136,414✔
1035
    if (!shellCmdkilled) {
8,136,414✔
1036
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
8,136,414✔
1037
    } else {
UNCOV
1038
      tsem_post(&dump_info->sem);
×
1039
    }
1040
  } else {
1041
    if (num_of_rows < 0) {
14,623,273✔
UNCOV
1042
      printf("\033[31masync retrieve failed, code: %d, %s\033[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1043
    }
1044
    tsem_post(&dump_info->sem);
14,623,273✔
1045
  }
1046
}
22,759,650✔
1047

1048
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
15,515,067✔
1049
  int64_t num_of_rows = 0;
15,515,067✔
1050
  if (fname != NULL) {
15,515,067✔
1051
    num_of_rows = shellDumpResultToFile(fname, tres);
891,794✔
1052
  } else {
1053
    tsDumpInfo dump_info;
14,622,671✔
1054
    if (!shellCmdkilled) {
14,623,273✔
1055
      init_dump_info(&dump_info, tres, sql, vertical);
14,623,273✔
1056
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
14,623,273✔
1057
      tsem_wait(&dump_info.sem);
14,623,273✔
1058
      num_of_rows = dump_info.numOfAllRows;
14,623,273✔
1059
    }
1060
  }
1061

1062
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
15,515,067✔
1063
  return num_of_rows;
15,515,067✔
1064
}
1065

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

1071
  char   *line = taosMemoryMalloc(tsMaxSQLLength + 1);
1,060,789✔
1072
  int32_t read_size = 0;
1,060,789✔
1073
  while ((read_size = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
240,759,172✔
1074
    line[read_size - 1] = '\0';
239,698,383✔
1075
    taosMemoryFree(pHistory->hist[pHistory->hend]);
239,698,383✔
1076
    pHistory->hist[pHistory->hend] = taosStrdup(line);
239,698,383✔
1077

1078
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
239,698,383✔
1079

1080
    if (pHistory->hend == pHistory->hstart) {
239,698,383✔
UNCOV
1081
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
×
1082
    }
1083
  }
1084

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

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

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

1114
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
2,169,950✔
1115
    if (pHistory->hist[i] != NULL) {
1,085,012✔
1116
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
1,085,012✔
1117
      taosMemoryFree(pHistory->hist[i]);
1,085,012✔
1118
      pHistory->hist[i] = NULL;
1,085,012✔
1119
    }
1120
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
1,085,012✔
1121
  }
1122
  taosCloseFile(&pFile);
1,084,938✔
1123
}
1124

1125
void shellCleanupHistory() {
1,085,015✔
1126
  SShellHistory *pHistory = &shell.history;
1,085,015✔
1127
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
1,086,100,015✔
1128
    if (pHistory->hist[i] != NULL) {
1,085,015,000✔
1129
      taosMemoryFree(pHistory->hist[i]);
239,698,383✔
1130
      pHistory->hist[i] = NULL;
239,698,383✔
1131
    }
1132
  }
1133
}
1,085,015✔
1134

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

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

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

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

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

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

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

1175
  char *line = taosMemoryMalloc(tsMaxSQLLength + 1);
34,872✔
1176
  while ((read_len = taosGetsFile(pFile, tsMaxSQLLength, line)) > 0) {
68,029,079✔
1177
    if (cmd_len + read_len >= tsMaxSQLLength) {
67,994,207✔
UNCOV
1178
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1179
             read_len);
1180
      cmd_len = 0;
×
UNCOV
1181
      memset(line, 0, tsMaxSQLLength + 1);
×
1182
      continue;
×
1183
    }
1184
    line[--read_len] = '\0';
67,994,207✔
1185

1186
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
67,994,207✔
1187
      continue;
32,011✔
1188
    }
1189

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

1197
    if (line[read_len - 1] == '\r') {
67,962,196✔
UNCOV
1198
      line[read_len - 1] = ' ';
×
1199
    }
1200

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

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

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

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

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

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

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

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

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

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

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

1270
    taos_free_result(tres);
210✔
1271
  }
1272

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

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

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

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

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

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

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

1314
  return NULL;
210✔
1315
}
1316

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

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

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

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

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

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

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

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

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

1366
#pragma GCC diagnostic pop
1367

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

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

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

1390
    if (pArgs->port_inputted) {
1,085,132✔
1391
      port = pArgs->port;
574✔
1392
    } else {
1393
      port = defaultPort(pArgs->connMode, pArgs->dsn);
1,084,558✔
1394
    }
1395

1396
    sprintf(show, "host:%s port:%d ", host, port);
1,085,132✔
1397

1398
    // connect normal
1399
    if (pArgs->auth) {
1,085,132✔
1400
      taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
126✔
1401
    } else {
1402
#ifdef TD_ENTERPRISE 
1403
      if (strlen(pArgs->token) > 0) {
1,085,006✔
1404
        // token
1405
        printf("Connect with token ...");
133✔
1406
        taos = taos_connect_token(host, pArgs->token, pArgs->database, port);
133✔
1407
        if (taos != NULL) {
133✔
1408
          printf("... [ OK ]\n");
68✔
1409
          return taos;
68✔
1410
        }
1411
        printf("... [ FAILED ]\n");
65✔
1412
        return NULL;
65✔
1413
      }
1414
#endif      
1415
      taos = taos_connect(host, user, pwd, pArgs->database, port);
1,084,873✔
1416
    }
1417

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

1440
  return taos;
1,085,902✔
1441
}
1442

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

1450
  SShellArgs *pArgs = &shell.args;
1,086,069✔
1451
  shell.conn = createConnect(pArgs);
1,086,069✔
1452

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

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

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

1470
  if (runOnce) {
1,085,015✔
1471
    if (pArgs->commands != NULL) {
1,084,805✔
1472
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
1,031,706✔
1473
      char *cmd = taosStrdup(pArgs->commands);
1,031,706✔
1474
      shellRunCommand(cmd, true);
1,031,706✔
1475
      taosMemoryFree(cmd);
1,031,706✔
1476
    }
1477

1478
    if (pArgs->file[0] != 0) {
1,084,805✔
1479
      shellSourceFile(pArgs->file);
53,099✔
1480
    }
1481

1482
    taos_close(shell.conn);
1,084,805✔
1483

1484
    shellWriteHistory();
1,084,805✔
1485
    shellCleanupHistory();
1,084,805✔
1486
    return 0;
1,084,805✔
1487
  }
1488

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

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

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

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

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

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

1529
  taosThreadJoin(spid, NULL);
210✔
1530

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

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

© 2026 Coveralls, Inc