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

taosdata / TDengine / #4335

20 Jun 2025 05:45AM UTC coverage: 60.571% (-2.3%) from 62.916%
#4335

push

travis-ci

web-flow
fix: compatibility ci problems. (#31430)

149119 of 315107 branches covered (47.32%)

Branch coverage included in aggregate %.

231167 of 312731 relevant lines covered (73.92%)

6342953.77 hits per line

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

69.47
/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 "geosWrapper.h"
22
#include "shellAuto.h"
23
#include "shellInt.h"
24
#include "../../inc/pub.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) {
833,398✔
71
  for (char c = *cmd++; c != 0; c = *cmd++) {
1,013,988✔
72
    if (c != ' ' && c != '\t' && c != ';') {
630,184!
73
      return false;
449,594✔
74
    }
75
  }
76
  return true;
383,804✔
77
}
78

79
int32_t shellRunSingleCommand(char *command) {
609,209✔
80
  shellCmdkilled = false;
609,209✔
81

82
  if (shellIsEmptyCommand(command)) {
609,209✔
83
    return 0;
383,804✔
84
  }
85

86
  if (shellRegexMatch(command, "^[ \t]*(quit|q|exit)[ \t;]*$", REG_EXTENDED | REG_ICASE)) {
225,405!
87
    return -1;
×
88
  }
89

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

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

136
void shellRecordCommandToHistory(char *command) {
14,659✔
137
  if (strncasecmp(command, "create user ", 12) == 0 || strncasecmp(command, "alter user ", 11) == 0) {
14,659!
138
    if (taosStrCaseStr(command, " pass ")) {
6!
139
      // have password command forbid record to history because security
140
      return;
×
141
    }
142
  }
143

144
  SShellHistory *pHistory = &shell.history;
14,659✔
145
  if (pHistory->hstart == pHistory->hend ||
14,659✔
146
      pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
1!
147
      strcmp(command, pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE]) != 0) {
1!
148
    if (pHistory->hist[pHistory->hend] != NULL) {
14,659!
149
      taosMemoryFreeClear(pHistory->hist[pHistory->hend]);
14,659!
150
    }
151
    pHistory->hist[pHistory->hend] = taosStrdup(command);
14,659!
152

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

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

165
  // add help or help;
166
  if (strncasecmp(command, "help", 4) == 0) {
224,189!
167
    if (command[4] == ';' || command[4] == ' ' || command[4] == 0) {
×
168
      showHelp();
×
169
      return 0;
×
170
    }
171
  }
172

173
  if (recordHistory) shellRecordCommandToHistory(command);
224,189✔
174

175
  char quote = 0, *cmd = command;
224,189✔
176
  for (char c = *command++; c != 0; c = *command++) {
157,797,822✔
177
    if (c == '\\' && (*command == '\'' || *command == '"' || *command == '`')) {
157,573,633!
178
      command++;
×
179
      continue;
×
180
    }
181

182
    if (quote == c) {
157,573,633✔
183
      quote = 0;
2,189,086✔
184
    } else if (quote == 0 && (c == '\'' || c == '"' || c == '`')) {
155,384,547✔
185
      quote = c;
2,189,086✔
186
    } else if (c == ';' && quote == 0) {
153,195,461✔
187
      c = *command;
385,020✔
188
      *command = 0;
385,020✔
189
      if (shellRunSingleCommand(cmd) < 0) {
385,020!
190
        return -1;
×
191
      }
192
      *command = c;
385,020✔
193
      cmd = command;
385,020✔
194
    }
195
  }
196
  return shellRunSingleCommand(cmd);
224,189✔
197
}
198

199
char *strendG(const char *pstr) {
225,404✔
200
  if (pstr == NULL) {
225,404!
201
    return NULL;
×
202
  }
203

204
  size_t len = strlen(pstr);
225,404✔
205
  if (len < 4) {
225,404!
206
    return NULL;
×
207
  }
208

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

214
  p = (char *)pstr + len - 3;
225,400✔
215
  if (strcmp(p, "\\G;") == 0) {
225,400✔
216
    return p;
114✔
217
  }
218

219
  return NULL;
225,286✔
220
}
221

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

229
  if ((sptr = strstr(command, ">>")) != NULL) {
225,404✔
230
    fname = sptr + 2;
13,130✔
231
    while (*fname == ' ') fname++;
26,248✔
232
    *sptr = '\0';
13,130✔
233

234
    cptr = strstr(fname, ";");
13,130✔
235
    if (cptr != NULL) {
13,130✔
236
      *cptr = '\0';
12✔
237
    }
238
  }
239

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

245
  st = taosGetTimestampUs();
225,404✔
246

247
  TAOS_RES *pSql = taos_query(shell.conn, command);
225,404✔
248
  if (taos_errno(pSql)) {
225,404✔
249
    shellPrintError(pSql, st);
3,718✔
250
    return;
3,718✔
251
  }
252

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

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

259
    taos_free_result(pSql);
172✔
260

261
    return;
172✔
262
  }
263

264
  // pre string
265
  char *pre = "Query OK";
221,514✔
266
  if (shellRegexMatch(command, "^\\s*delete\\s*from\\s*.*", REG_EXTENDED | REG_ICASE)) {
221,514✔
267
    pre = "Delete OK";
25✔
268
  } else if (shellRegexMatch(command, "^\\s*insert\\s*into\\s*.*", REG_EXTENDED | REG_ICASE)) {
221,489✔
269
    pre = "Insert OK";
117,975✔
270
  } else if (shellRegexMatch(command, "^\\s*create\\s*.*", REG_EXTENDED | REG_ICASE)) {
103,514✔
271
    pre = "Create OK";
4,098✔
272
  } else if (shellRegexMatch(command, "^\\s*drop\\s*.*", REG_EXTENDED | REG_ICASE)) {
99,416✔
273
    pre = "Drop OK";
152✔
274
  }
275

276
  TAOS_FIELD *pFields = taos_fetch_fields(pSql);
221,514✔
277
  if (pFields != NULL) {  // select and show kinds of commands
221,514✔
278
    int32_t error_no = 0;
93,581✔
279

280
    int64_t numOfRows = shellDumpResult(pSql, fname, &error_no, printMode, command);
93,581✔
281
    if (numOfRows < 0) return;
93,581!
282

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

297
    // call auto tab
298
    callbackAutoTab(command, NULL, false);
127,933✔
299
  }
300

301
  printf("\r\n");
221,514✔
302
}
303

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

310
  time_t  tt;
311
  int32_t ms = 0;
4,650,590✔
312
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,650,590!
313
    tt = (time_t)(val / 1000000000);
×
314
    ms = val % 1000000000;
×
315
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,650,590!
316
    tt = (time_t)(val / 1000000);
×
317
    ms = val % 1000000;
×
318
  } else {
319
    tt = (time_t)(val / 1000);
4,650,590✔
320
    ms = val % 1000;
4,650,590✔
321
  }
322

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

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

340
  if (precision == TSDB_TIME_PRECISION_NANO) {
4,650,590!
341
    sprintf(buf + pos, ".%09d", ms);
×
342
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
4,650,590!
343
    sprintf(buf + pos, ".%06d", ms);
×
344
  } else {
345
    sprintf(buf + pos, ".%03d", ms);
4,650,590✔
346
  }
347

348
  return buf;
4,650,590✔
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) {
14,395,756✔
361
  if (val == NULL) {
14,395,756✔
362
    taosFprintfFile(pFile, "NULL");
58,988✔
363
    return;
58,988✔
364
  }
365

366
  char    quotationStr[2] = {'"', 0};
14,336,768✔
367
  int32_t width;
368

369
  int n = 0;
14,336,768✔
370
#define LENGTH 64
371
  char buf[LENGTH] = {0};
14,336,768✔
372
  switch (field->type) {
14,336,768!
373
    case TSDB_DATA_TYPE_BOOL:
3,608,878✔
374
      taosFprintfFile(pFile, "%d", ((((int32_t)(*((char *)val))) == 1) ? 1 : 0));
3,608,878✔
375
      break;
3,608,878✔
376
    case TSDB_DATA_TYPE_TINYINT:
124,792✔
377
      taosFprintfFile(pFile, "%d", *((int8_t *)val));
124,792✔
378
      break;
124,792✔
379
    case TSDB_DATA_TYPE_UTINYINT:
10,806✔
380
      taosFprintfFile(pFile, "%u", *((uint8_t *)val));
10,806✔
381
      break;
10,806✔
382
    case TSDB_DATA_TYPE_SMALLINT:
10,807✔
383
      taosFprintfFile(pFile, "%d", *((int16_t *)val));
10,807✔
384
      break;
10,807✔
385
    case TSDB_DATA_TYPE_USMALLINT:
5,897✔
386
      taosFprintfFile(pFile, "%u", *((uint16_t *)val));
5,897✔
387
      break;
5,897✔
388
    case TSDB_DATA_TYPE_INT:
430,688✔
389
      taosFprintfFile(pFile, "%d", *((int32_t *)val));
430,688✔
390
      break;
430,688✔
391
    case TSDB_DATA_TYPE_UINT:
9,823✔
392
      taosFprintfFile(pFile, "%u", *((uint32_t *)val));
9,823✔
393
      break;
9,823✔
394
    case TSDB_DATA_TYPE_BIGINT:
815,013✔
395
      taosFprintfFile(pFile, "%" PRId64, *((int64_t *)val));
815,013✔
396
      break;
815,013✔
397
    case TSDB_DATA_TYPE_UBIGINT:
12,791✔
398
      taosFprintfFile(pFile, "%" PRIu64, *((uint64_t *)val));
12,791✔
399
      break;
12,791✔
400
    case TSDB_DATA_TYPE_FLOAT:
120,822✔
401
      width = SHELL_FLOAT_WIDTH;
120,822✔
402
      if (tsEnableScience) {
120,822!
403
        taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
×
404
      } else {
405
        n = tsnprintf(buf, LENGTH, "%*.7f", width, GET_FLOAT_VAL(val));
120,822✔
406
        if (n > SHELL_FLOAT_WIDTH) {
120,822✔
407
          taosFprintfFile(pFile, "%*.7e", width, GET_FLOAT_VAL(val));
2,720✔
408
        } else {
409
          taosFprintfFile(pFile, "%s", buf);
118,102✔
410
        }
411
      }
412
      break;
120,822✔
413
    case TSDB_DATA_TYPE_DOUBLE:
1,463,045✔
414
      width = SHELL_DOUBLE_WIDTH;
1,463,045✔
415
      if (tsEnableScience) {
1,463,045!
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));
1,463,045✔
420
        if (n > SHELL_DOUBLE_WIDTH) {
1,463,045✔
421
          taosFprintfFile(pFile, "%*.15e", width, GET_DOUBLE_VAL(val));
1,091,079✔
422
        } else {
423
          taosFprintfFile(pFile, "%s", buf);
371,966✔
424
        }
425
      }
426
      break;
1,463,045✔
427
    case TSDB_DATA_TYPE_BINARY:
729,710✔
428
    case TSDB_DATA_TYPE_NCHAR:
429
    case TSDB_DATA_TYPE_JSON: {
430
      int32_t bufIndex = 0;
729,710✔
431
      char   *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
729,710!
432
      if (tmp == NULL) break;
729,710!
433
      for (int32_t i = 0; i < length; i++) {
8,821,197✔
434
        tmp[bufIndex] = val[i];
8,091,487✔
435
        bufIndex++;
8,091,487✔
436
        if (val[i] == '\"') {
8,091,487!
437
          tmp[bufIndex] = val[i];
×
438
          bufIndex++;
×
439
        }
440
      }
441
      tmp[bufIndex] = 0;
729,710✔
442

443
      taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
729,710✔
444
      taosMemoryFree(tmp);
729,710!
445
    } break;
729,710✔
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:
566,190✔
465
      shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision);
566,190✔
466
      taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr);
566,190✔
467
      break;
566,190✔
468
    case TSDB_DATA_TYPE_DECIMAL64:
6,427,506✔
469
    case TSDB_DATA_TYPE_DECIMAL:
470
      taosFprintfFile(pFile, "%s", val);
6,427,506✔
471
    default:
6,427,506✔
472
      break;
6,427,506✔
473
  }
474
}
475

476
int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres) {
12,270✔
477
  char fullname[PATH_MAX] = {0};
12,270✔
478
  if (taosExpandDir(fname, fullname, PATH_MAX) != 0) {
12,270!
479
    tstrncpy(fullname, fname, PATH_MAX);
×
480
  }
481

482
  TAOS_ROW row = taos_fetch_row(tres);
12,270✔
483
  if (row == NULL) {
12,270✔
484
    return 0;
1,038✔
485
  }
486

487
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_STREAM);
11,232✔
488
  if (pFile == NULL) {
11,232!
489
    fprintf(stderr, "failed to open file: %s\r\n", fullname);
×
490
    return -1;
×
491
  }
492

493
  TAOS_FIELD *fields = taos_fetch_fields(tres);
11,232✔
494
  int32_t     num_fields = taos_num_fields(tres);
11,232✔
495
  int32_t     precision = taos_result_precision(tres);
11,232✔
496

497
  for (int32_t col = 0; col < num_fields; col++) {
24,093✔
498
    if (col > 0) {
12,861✔
499
      taosFprintfFile(pFile, ",");
1,629✔
500
    }
501
    taosFprintfFile(pFile, "%s", fields[col].name);
12,861✔
502
  }
503
  taosFprintfFile(pFile, "\r\n");
11,232✔
504

505
  int64_t numOfRows = 0;
11,232✔
506
  do {
507
    int32_t *length = taos_fetch_lengths(tres);
11,314,739✔
508
    for (int32_t i = 0; i < num_fields; i++) {
25,710,495✔
509
      if (i > 0) {
14,395,756✔
510
        taosFprintfFile(pFile, ",");
3,081,017✔
511
      }
512
      shellDumpFieldToFile(pFile, (const char *)row[i], fields + i, length[i], precision);
14,395,756✔
513
    }
514
    taosFprintfFile(pFile, "\r\n");
11,314,739✔
515

516
    numOfRows++;
11,314,739✔
517
    row = taos_fetch_row(tres);
11,314,739✔
518
  } while (row != NULL);
11,314,739✔
519

520
  taosCloseFile(&pFile);
11,232✔
521

522
  return numOfRows;
11,232✔
523
}
524

525
void shellPrintNChar(const char *str, int32_t length, int32_t width) {
2,111,864✔
526
  TdWchar tail[3];
527
  int32_t pos = 0, cols = 0, totalCols = 0, tailLen = 0;
2,111,864✔
528

529
  while (pos < length) {
49,715,029✔
530
    TdWchar wc;
531
    int32_t bytes = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
48,339,760✔
532
    if (bytes <= 0) {
48,339,760✔
533
      break;
736,595✔
534
    }
535

536
    if (pos + bytes > length) {
48,339,634!
537
      break;
×
538
    }
539
    int w = 0;
48,339,634✔
540
    if (*(str + pos) == '\t' || *(str + pos) == '\n' || *(str + pos) == '\r') {
48,339,634!
541
      w = bytes;
×
542
    } else {
543
      w = taosWcharWidth(wc);
48,339,634✔
544
    }
545
    pos += bytes;
48,339,634✔
546

547
    if (w <= 0) {
48,339,634✔
548
      continue;
179,770✔
549
    }
550

551
    if (width <= 0) {
48,339,598✔
552
      printf("%lc", wc);
179,734✔
553
      continue;
179,734✔
554
    }
555

556
    totalCols += w;
48,159,864✔
557
    if (totalCols > width) {
48,159,864✔
558
      break;
736,469✔
559
    }
560
    if (totalCols <= (width - 3)) {
47,423,395✔
561
      printf("%lc", wc);
44,828,324✔
562
      cols += w;
44,828,324✔
563
    } else {
564
      tail[tailLen] = wc;
2,595,071✔
565
      tailLen++;
2,595,071✔
566
    }
567
  }
568

569
  if (totalCols > width) {
2,111,864✔
570
    // width could be 1 or 2, so printf("...") cannot be used
571
    for (int32_t i = 0; i < 3; i++) {
2,945,876✔
572
      if (cols >= width) {
2,209,407!
573
        break;
×
574
      }
575
      putchar('.');
2,209,407✔
576
      ++cols;
2,209,407✔
577
    }
578
  } else {
579
    for (int32_t i = 0; i < tailLen; i++) {
1,761,059✔
580
      printf("%lc", tail[i]);
385,664✔
581
    }
582
    cols = totalCols;
1,375,395✔
583
  }
584

585
  for (; cols < width; cols++) {
16,398,451✔
586
    putchar(' ');
14,286,587✔
587
  }
588
}
2,111,864✔
589

590
void shellPrintString(const char *str, int32_t width) {
3,374,413✔
591
  int32_t len = strlen(str);
3,374,413✔
592

593
  if (width == 0) {
3,374,413!
594
    printf("%s", str);
×
595
  } else if (len > width) {
3,374,413!
596
    if (width <= 3) {
×
597
      printf("%.*s.", width - 1, str);
×
598
    } else {
599
      printf("%.*s...", width - 3, str);
×
600
    }
601
  } else {
602
    printf("%s%*.s", str, width - len, "");
3,374,413✔
603
  }
604
}
3,374,413✔
605

606
void shellPrintGeometry(const unsigned char *val, int32_t length, int32_t width) {
×
607
  if (length == 0) {  // empty value
×
608
    shellPrintString("", width);
×
609
    return;
×
610
  }
611

612
  int32_t code = TSDB_CODE_FAILED;
×
613

614
  code = initCtxAsText();
×
615
  if (code != TSDB_CODE_SUCCESS) {
×
616
    shellPrintString(getGeosErrMsg(code), width);
×
617
    return;
×
618
  }
619

620
  char *outputWKT = NULL;
×
621
  code = doAsText(val, length, &outputWKT);
×
622
  if (code != TSDB_CODE_SUCCESS) {
×
623
    shellPrintString(getGeosErrMsg(code), width);  // should NOT happen
×
624
    return;
×
625
  }
626

627
  shellPrintString(outputWKT, width);
×
628

629
  geosFreeBuffer(outputWKT);
×
630
}
631

632
void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t length, int32_t precision) {
12,693,630✔
633
  if (val == NULL) {
12,693,630✔
634
    shellPrintString(TSDB_DATA_NULL_STR, width);
3,159,194✔
635
    return;
3,159,194✔
636
  }
637

638
  int n = 0;
9,534,436✔
639
#define LENGTH 64
640
  char buf[LENGTH] = {0};
9,534,436✔
641
  switch (field->type) {
9,534,436!
642
    case TSDB_DATA_TYPE_BOOL:
215,219✔
643
      shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
215,219✔
644
      break;
215,219✔
645
    case TSDB_DATA_TYPE_TINYINT:
215,437✔
646
      printf("%*d", width, *((int8_t *)val));
215,437✔
647
      break;
215,437✔
648
    case TSDB_DATA_TYPE_UTINYINT:
168,956✔
649
      printf("%*u", width, *((uint8_t *)val));
168,956✔
650
      break;
168,956✔
651
    case TSDB_DATA_TYPE_SMALLINT:
182,084✔
652
      printf("%*d", width, *((int16_t *)val));
182,084✔
653
      break;
182,084✔
654
    case TSDB_DATA_TYPE_USMALLINT:
175,268✔
655
      printf("%*u", width, *((uint16_t *)val));
175,268✔
656
      break;
175,268✔
657
    case TSDB_DATA_TYPE_INT:
242,700✔
658
      printf("%*d", width, *((int32_t *)val));
242,700✔
659
      break;
242,700✔
660
    case TSDB_DATA_TYPE_UINT:
110,106✔
661
      printf("%*u", width, *((uint32_t *)val));
110,106✔
662
      break;
110,106✔
663
    case TSDB_DATA_TYPE_BIGINT:
1,212,610✔
664
      printf("%*" PRId64, width, taosGetInt64Aligned((int64_t *)val));
1,212,610✔
665
      break;
1,212,610✔
666
    case TSDB_DATA_TYPE_UBIGINT:
77,454✔
667
      printf("%*" PRIu64, width, taosGetUInt64Aligned((uint64_t *)val));
77,454✔
668
      break;
77,454✔
669
    case TSDB_DATA_TYPE_FLOAT:
202,756✔
670
      width = width >= LENGTH ? LENGTH - 1 : width;
202,756✔
671
      if (tsEnableScience) {
202,756!
672
        printf("%*.7e", width, taosGetFloatAligned((float *)val));
×
673
      } else {
674
        snprintf(buf, LENGTH, "%*.*g", width, FLT_DIG, taosGetFloatAligned((float *)val));
202,756✔
675
        printf("%s", buf);
202,756✔
676
      }
677
      break;
202,756✔
678
    case TSDB_DATA_TYPE_DOUBLE:
535,582✔
679
      width = width >= LENGTH ? LENGTH - 1 : width;
535,582✔
680
      if (tsEnableScience) {
535,582!
681
        snprintf(buf, LENGTH, "%*.15e", width, taosGetDoubleAligned((double *)val));
×
682
        printf("%s", buf);
×
683
      } else {
684
        snprintf(buf, LENGTH, "%*.*g", width, DBL_DIG, taosGetDoubleAligned((double *)val));
535,582✔
685
        printf("%*s", width, buf);
535,582✔
686
      }
687
      break;
535,582✔
688
    case TSDB_DATA_TYPE_VARBINARY: {
×
689
      void    *data = NULL;
×
690
      uint32_t size = 0;
×
691
      if (taosAscii2Hex(val, length, &data, &size) < 0) {
×
692
        break;
×
693
      }
694
      shellPrintNChar(data, size, width);
×
695
      taosMemoryFree(data);
×
696
      break;
×
697
    }
698
    case TSDB_DATA_TYPE_BINARY:
2,111,864✔
699
    case TSDB_DATA_TYPE_NCHAR:
700
    case TSDB_DATA_TYPE_JSON:
701
      shellPrintNChar(val, length, width);
2,111,864✔
702
      break;
2,111,864✔
703
    case TSDB_DATA_TYPE_GEOMETRY:
×
704
      shellPrintGeometry(val, length, width);
×
705
      break;
×
706
    case TSDB_DATA_TYPE_TIMESTAMP:
4,084,400✔
707
      shellFormatTimestamp(buf, sizeof(buf), taosGetInt64Aligned((int64_t *)val), precision);
4,084,400✔
708
      printf("%s", buf);
4,084,400✔
709
      break;
4,084,400✔
710
    case TSDB_DATA_TYPE_DECIMAL:
×
711
    case TSDB_DATA_TYPE_DECIMAL64:
712
      printf("%*s", width, val);
×
713
    default:
×
714
      break;
×
715
  }
716
}
717

718
// show whole result for this query return true, like limit or describe
719
bool shellIsShowWhole(const char *sql) {
×
720
  // limit
721
  if (taosStrCaseStr(sql, " limit ") != NULL) {
×
722
    return true;
×
723
  }
724
  // describe
725
  if (taosStrCaseStr(sql, "describe ") != NULL) {
×
726
    return true;
×
727
  }
728
  // desc
729
  if (taosStrCaseStr(sql, "desc ") != NULL) {
×
730
    return true;
×
731
  }
732
  // show
733
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
734
    return true;
×
735
  }
736
  // explain
737
  if (taosStrCaseStr(sql, "explain ") != NULL) {
×
738
    return true;
×
739
  }
740

741
  return false;
×
742
}
743

744
bool shellIsShowQuery(const char *sql) {
×
745
  // todo refactor
746
  if (taosStrCaseStr(sql, "show ") != NULL) {
×
747
    return true;
×
748
  }
749

750
  return false;
×
751
}
752

753
void init_dump_info(tsDumpInfo *dump_info, TAOS_RES *tres, const char *sql, bool vertical) {
81,311✔
754
  dump_info->sql = sql;
81,311✔
755
  dump_info->vertical = vertical;
81,311✔
756
  tsem_init(&dump_info->sem, 0, 0);
81,311✔
757
  dump_info->numOfAllRows = 0;
81,311✔
758

759
  dump_info->numFields = taos_num_fields(tres);
81,311✔
760
  dump_info->fields = taos_fetch_fields(tres);
81,311✔
761
  dump_info->precision = taos_result_precision(tres);
81,311✔
762

763
  dump_info->resShowMaxNum = UINT64_MAX;
81,311✔
764

765
  if (shell.args.commands == NULL && shell.args.file[0] == 0 && !shellIsShowWhole(dump_info->sql)) {
81,311!
766
    dump_info->resShowMaxNum = SHELL_DEFAULT_RES_SHOW_NUM;
×
767
  }
768

769
  if (vertical) {
81,311✔
770
    dump_info->maxColNameLen = 0;
118✔
771
    for (int32_t col = 0; col < dump_info->numFields; col++) {
249✔
772
      int32_t len = (int32_t)strlen(dump_info->fields[col].name);
131✔
773
      if (len > dump_info->maxColNameLen) {
131✔
774
        dump_info->maxColNameLen = len;
122✔
775
      }
776
    }
777
  } else {
778
    for (int32_t col = 0; col < dump_info->numFields; col++) {
247,894✔
779
      dump_info->width[col] = shellCalcColWidth(dump_info->fields + col, dump_info->precision);
166,701✔
780
    }
781
  }
782
}
81,311✔
783

784
void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
118✔
785
  TAOS_ROW row = taos_fetch_row(tres);
118✔
786
  if (row == NULL) {
118!
787
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
788
    return;
×
789
  }
790

791
  int64_t numOfPintRows = dump_info->numOfAllRows;
118✔
792
  int     numOfPrintRowsThisOne = 0;
118✔
793

794
  while (row != NULL) {
3,056!
795
    printf("*************************** %" PRId64 ".row ***************************\r\n", numOfPintRows + 1);
3,056✔
796

797
    int32_t *length = taos_fetch_lengths(tres);
3,056✔
798

799
    for (int32_t i = 0; i < dump_info->numFields; i++) {
6,127✔
800
      TAOS_FIELD *field = dump_info->fields + i;
3,071✔
801

802
      int32_t padding = (int32_t)(dump_info->maxColNameLen - strlen(field->name));
3,071✔
803
      printf("%*.s%s: ", padding, " ", field->name);
3,071✔
804

805
      shellPrintField((const char *)row[i], field, 0, length[i], dump_info->precision);
3,071✔
806
      putchar('\r');
3,071✔
807
      putchar('\n');
3,071✔
808
    }
809

810
    numOfPintRows++;
3,056✔
811
    numOfPrintRowsThisOne++;
3,056✔
812

813
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,056!
814
      printf("\r\n");
×
815
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
816
      printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
817
      printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
818
      printf("\r\n");
×
819
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
820
      printf("\r\n");
×
821
      return;
×
822
    }
823

824
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
3,056✔
825
      return;
118✔
826
    }
827

828
    row = taos_fetch_row(tres);
2,938✔
829
  }
830
  return;
×
831
}
832

833
int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
166,701✔
834
  int32_t width = (int32_t)strlen(field->name);
166,701✔
835

836
  switch (field->type) {
166,701!
837
    case TSDB_DATA_TYPE_NULL:
×
838
      return TMAX(4, width);  // null
×
839
    case TSDB_DATA_TYPE_BOOL:
2,341✔
840
      return TMAX(5, width);  // 'false'
2,341✔
841

842
    case TSDB_DATA_TYPE_TINYINT:
4,407✔
843
    case TSDB_DATA_TYPE_UTINYINT:
844
      return TMAX(4, width);  // '-127'
4,407✔
845

846
    case TSDB_DATA_TYPE_SMALLINT:
5,695✔
847
    case TSDB_DATA_TYPE_USMALLINT:
848
      return TMAX(6, width);  // '-32767'
5,695✔
849

850
    case TSDB_DATA_TYPE_INT:
5,349✔
851
    case TSDB_DATA_TYPE_UINT:
852
      return TMAX(11, width);  // '-2147483648'
5,349✔
853

854
    case TSDB_DATA_TYPE_BIGINT:
32,461✔
855
    case TSDB_DATA_TYPE_UBIGINT:
856
      return TMAX(21, width);  // '-9223372036854775807'
32,461✔
857

858
    case TSDB_DATA_TYPE_FLOAT:
2,377✔
859
      return TMAX(SHELL_FLOAT_WIDTH, width);
2,377✔
860

861
    case TSDB_DATA_TYPE_DOUBLE:
20,491✔
862
      return TMAX(SHELL_DOUBLE_WIDTH, width);
20,491✔
863

864
    case TSDB_DATA_TYPE_BINARY:
47,344✔
865
    case TSDB_DATA_TYPE_GEOMETRY:
866
      if (field->bytes > shell.args.displayWidth) {
47,344✔
867
        return TMAX(shell.args.displayWidth, width);
39,854✔
868
      } else {
869
        return TMAX(field->bytes + 2, width);
7,490✔
870
      }
871
    case TSDB_DATA_TYPE_VARBINARY: {
×
872
      int32_t bytes = field->bytes * 2 + 2;
×
873
      if (bytes > shell.args.displayWidth) {
×
874
        return TMAX(shell.args.displayWidth, width);
×
875
      } else {
876
        return TMAX(bytes + 2, width);
×
877
      }
878
    }
879
    case TSDB_DATA_TYPE_NCHAR:
11,246✔
880
    case TSDB_DATA_TYPE_JSON: {
881
      uint16_t bytes = field->bytes * TSDB_NCHAR_SIZE;
11,246✔
882
      if (bytes > shell.args.displayWidth) {
11,246!
883
        return TMAX(shell.args.displayWidth, width);
11,246✔
884
      } else {
885
        return TMAX(bytes + 2, width);
×
886
      }
887
    }
888

889
    case TSDB_DATA_TYPE_TIMESTAMP:
34,990✔
890
      if (shell.args.is_raw_time) {
34,990!
891
        return TMAX(14, width);
×
892
      }
893
      if (precision == TSDB_TIME_PRECISION_NANO) {
34,990!
894
        return TMAX(29, width);
×
895
      } else if (precision == TSDB_TIME_PRECISION_MICRO) {
34,990!
896
        return TMAX(26, width);  // '2020-01-01 00:00:00.000000'
×
897
      } else {
898
        return TMAX(23, width);  // '2020-01-01 00:00:00.000'
34,990✔
899
      }
900
    case TSDB_DATA_TYPE_DECIMAL64:
×
901
      return TMAX(width, 20);
×
902
    case TSDB_DATA_TYPE_DECIMAL:
×
903
      return TMAX(width, 40);
×
904
    default:
×
905
      ASSERT(false);
×
906
  }
907

908
  return 0;
×
909
}
910

911
void shellPrintHeader(TAOS_FIELD *fields, int32_t *width, int32_t num_fields) {
75,788✔
912
  int32_t rowWidth = 0;
75,788✔
913
  for (int32_t col = 0; col < num_fields; col++) {
224,494✔
914
    TAOS_FIELD *field = fields + col;
148,706✔
915
    int32_t     padding = (int32_t)(width[col] - strlen(field->name));
148,706✔
916
    int32_t     left = padding / 2;
148,706✔
917
    printf(" %*.s%s%*.s |", left, " ", field->name, padding - left, " ");
148,706✔
918
    rowWidth += width[col] + 3;
148,706✔
919
  }
920

921
  putchar('\r');
75,788✔
922
  putchar('\n');
75,788✔
923
  for (int32_t i = 0; i < rowWidth; i++) {
4,238,021✔
924
    putchar('=');
4,162,233✔
925
  }
926
  putchar('\r');
75,788✔
927
  putchar('\n');
75,788✔
928
}
75,788✔
929

930
void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info) {
80,208✔
931
  TAOS_ROW row = taos_fetch_row(tres);
80,208✔
932
  if (row == NULL) {
80,208!
933
    printf("\033[31mtaos_fetch_row failed.\033[0m\n");
×
934
    return;
×
935
  }
936

937
  int64_t numOfPintRows = dump_info->numOfAllRows;
80,208✔
938
  int     numOfPrintRowsThisOne = 0;
80,208✔
939
  if (numOfPintRows == 0) {
80,208✔
940
    shellPrintHeader(dump_info->fields, dump_info->width, dump_info->numFields);
75,788✔
941
  }
942

943
  while (row != NULL) {
3,320,471!
944
    int32_t *length = taos_fetch_lengths(tres);
3,320,471✔
945
    for (int32_t i = 0; i < dump_info->numFields; i++) {
16,011,030✔
946
      putchar(' ');
12,690,559✔
947
      shellPrintField((const char *)row[i], dump_info->fields + i, dump_info->width[i], length[i],
12,690,559✔
948
                      dump_info->precision);
949
      putchar(' ');
12,690,559✔
950
      putchar('|');
12,690,559✔
951
    }
952
    putchar('\r');
3,320,471✔
953
    putchar('\n');
3,320,471✔
954

955
    numOfPintRows++;
3,320,471✔
956
    numOfPrintRowsThisOne++;
3,320,471✔
957

958
    if (numOfPintRows == dump_info->resShowMaxNum) {
3,320,471!
959
      printf("\r\n");
×
960
      printf(" Notice: The result shows only the first %d rows.\r\n", SHELL_DEFAULT_RES_SHOW_NUM);
×
961
      if (shellIsShowQuery(dump_info->sql)) {
×
962
        printf("         You can use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
963
      } else {
964
        printf("         You can use the `LIMIT` clause to get fewer result to show.\r\n");
×
965
        printf("           Or use '>>' to redirect the whole set of the result to a specified file.\r\n");
×
966
      }
967
      printf("\r\n");
×
968
      printf("         You can use Ctrl+C to stop the underway fetching.\r\n");
×
969
      printf("\r\n");
×
970
      return;
×
971
    }
972

973
    if (numOfPrintRowsThisOne == dump_info->numOfRows) {
3,320,471✔
974
      return;
80,208✔
975
    }
976

977
    row = taos_fetch_row(tres);
3,240,263✔
978
  }
979
  return;
×
980
}
981

982
void shellDumpResultCallback(void *param, TAOS_RES *tres, int num_of_rows) {
161,637✔
983
  tsDumpInfo *dump_info = (tsDumpInfo *)param;
161,637✔
984
  if (num_of_rows > 0) {
161,637✔
985
    dump_info->numOfRows = num_of_rows;
80,326✔
986
    if (dump_info->numOfAllRows < dump_info->resShowMaxNum) {
80,326!
987
      if (dump_info->vertical) {
80,326✔
988
        shellVerticalPrintResult(tres, dump_info);
118✔
989
      } else {
990
        shellHorizontalPrintResult(tres, dump_info);
80,208✔
991
      }
992
    }
993
    dump_info->numOfAllRows += num_of_rows;
80,326✔
994
    if (!shellCmdkilled) {
80,326!
995
      taos_fetch_rows_a(tres, shellDumpResultCallback, param);
80,326✔
996
    } else {
997
      tsem_post(&dump_info->sem);
×
998
    }
999
  } else {
1000
    if (num_of_rows < 0) {
81,311!
1001
      printf("\033[31masync retrieve failed, code: %d\033, %s[0m\n", num_of_rows, tstrerror(num_of_rows));
×
1002
    }
1003
    tsem_post(&dump_info->sem);
81,311✔
1004
  }
1005
}
161,637✔
1006

1007
int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql) {
93,581✔
1008
  int64_t num_of_rows = 0;
93,581✔
1009
  if (fname != NULL) {
93,581✔
1010
    num_of_rows = shellDumpResultToFile(fname, tres);
12,270✔
1011
  } else {
1012
    tsDumpInfo dump_info;
1013
    if (!shellCmdkilled) {
81,311!
1014
      init_dump_info(&dump_info, tres, sql, vertical);
81,311✔
1015
      taos_fetch_rows_a(tres, shellDumpResultCallback, &dump_info);
81,311✔
1016
      tsem_wait(&dump_info.sem);
81,311✔
1017
      num_of_rows = dump_info.numOfAllRows;
81,311✔
1018
    }
1019
  }
1020

1021
  *error_no = shellCmdkilled ? TSDB_CODE_TSC_QUERY_KILLED : taos_errno(tres);
93,581!
1022
  return num_of_rows;
93,581✔
1023
}
1024

1025
void shellReadHistory() {
14,658✔
1026
  SShellHistory *pHistory = &shell.history;
14,658✔
1027
  TdFilePtr      pFile = taosOpenFile(pHistory->file, TD_FILE_READ | TD_FILE_STREAM);
14,658✔
1028
  if (pFile == NULL) return;
14,658!
1029

1030
  char   *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
14,658!
1031
  int32_t read_size = 0;
14,658✔
1032
  while ((read_size = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
101,922,404✔
1033
    line[read_size - 1] = '\0';
101,907,746✔
1034
    taosMemoryFree(pHistory->hist[pHistory->hend]);
101,907,746!
1035
    pHistory->hist[pHistory->hend] = taosStrdup(line);
101,907,746!
1036

1037
    pHistory->hend = (pHistory->hend + 1) % SHELL_MAX_HISTORY_SIZE;
101,907,746✔
1038

1039
    if (pHistory->hend == pHistory->hstart) {
101,907,746✔
1040
      pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
87,264,404✔
1041
    }
1042
  }
1043

1044
  taosMemoryFreeClear(line);
14,658!
1045
  taosCloseFile(&pFile);
14,658✔
1046
  int64_t file_size;
1047
  if (taosStatFile(pHistory->file, &file_size, NULL, NULL) == 0 && file_size > SHELL_MAX_COMMAND_SIZE) {
14,658!
1048
    TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_TRUNC);
1✔
1049
    if (pFile == NULL) return;
1!
1050
    int32_t endIndex = pHistory->hstart;
1✔
1051
    if (endIndex != 0) {
1!
1052
      endIndex = pHistory->hend;
1✔
1053
    }
1054
    for (int32_t i = (pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE; i != endIndex;) {
1,000✔
1055
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
999✔
1056
      i = (i + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
999✔
1057
    }
1058
    taosFprintfFile(pFile, "%s\n", pHistory->hist[endIndex]);
1✔
1059

1060
    /* coverity[+retval] */
1061
    taosFsyncFile(pFile);
1✔
1062
    taosCloseFile(&pFile);
1✔
1063
  }
1064
  pHistory->hstart = pHistory->hend;
14,658✔
1065
}
1066

1067
void shellWriteHistory() {
14,658✔
1068
  SShellHistory *pHistory = &shell.history;
14,658✔
1069
  if (pHistory->hend == pHistory->hstart) return;
14,658!
1070
  TdFilePtr pFile = taosOpenFile(pHistory->file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_STREAM | TD_FILE_APPEND);
14,658✔
1071
  if (pFile == NULL) return;
14,658!
1072

1073
  for (int32_t i = pHistory->hstart; i != pHistory->hend;) {
29,317✔
1074
    if (pHistory->hist[i] != NULL) {
14,659!
1075
      taosFprintfFile(pFile, "%s\n", pHistory->hist[i]);
14,659✔
1076
      taosMemoryFree(pHistory->hist[i]);
14,659!
1077
      pHistory->hist[i] = NULL;
14,659✔
1078
    }
1079
    i = (i + 1) % SHELL_MAX_HISTORY_SIZE;
14,659✔
1080
  }
1081
  taosCloseFile(&pFile);
14,658✔
1082
}
1083

1084
void shellCleanupHistory() {
14,658✔
1085
  SShellHistory *pHistory = &shell.history;
14,658✔
1086
  for (int32_t i = 0; i < SHELL_MAX_HISTORY_SIZE; ++i) {
14,672,658✔
1087
    if (pHistory->hist[i] != NULL) {
14,658,000✔
1088
      taosMemoryFree(pHistory->hist[i]);
14,643,341!
1089
      pHistory->hist[i] = NULL;
14,643,341✔
1090
    }
1091
  }
1092
}
14,658✔
1093

1094
void shellPrintError(TAOS_RES *tres, int64_t st) {
3,718✔
1095
  int64_t et = taosGetTimestampUs();
3,718✔
1096
  fprintf(stderr, "\r\nDB error: %s [0x%08X] (%.6fs)\r\n", taos_errstr(tres), taos_errno(tres), (et - st) / 1E6);
3,718✔
1097
  taos_free_result(tres);
3,718✔
1098
}
3,718✔
1099

1100
bool shellIsCommentLine(char *line) {
209,753✔
1101
  if (line == NULL) return true;
209,753!
1102
  return shellRegexMatch(line, "^\\s*#.*", REG_EXTENDED);
209,753✔
1103
}
1104

1105
void shellSourceFile(const char *file) {
223✔
1106
  int32_t read_len = 0;
223✔
1107
  char   *cmd = taosMemoryCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN + 1);
223!
1108
  size_t  cmd_len = 0;
223✔
1109
  char    fullname[PATH_MAX] = {0};
223✔
1110
  char    sourceFileCommand[PATH_MAX + 8] = {0};
223✔
1111

1112
  if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
223!
1113
    tstrncpy(fullname, file, PATH_MAX);
×
1114
  }
1115

1116
  sprintf(sourceFileCommand, "source %s;", fullname);
223✔
1117
  shellRecordCommandToHistory(sourceFileCommand);
223✔
1118

1119
  TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
223✔
1120
  if (pFile == NULL) {
223!
1121
    fprintf(stderr, "failed to open file %s\r\n", fullname);
×
1122
    taosMemoryFree(cmd);
×
1123
    return;
×
1124
  }
1125

1126
  char *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
223!
1127
  while ((read_len = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
210,399✔
1128
    if (cmd_len + read_len >= TSDB_MAX_ALLOWED_SQL_LEN) {
210,176!
1129
      printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
×
1130
             read_len);
1131
      cmd_len = 0;
×
1132
      memset(line, 0, TSDB_MAX_ALLOWED_SQL_LEN + 1);
×
1133
      continue;
×
1134
    }
1135
    line[--read_len] = '\0';
210,176✔
1136

1137
    if (read_len == 0 || shellIsCommentLine(line)) {  // line starts with #
210,176!
1138
      continue;
423✔
1139
    }
1140

1141
    if (line[read_len - 1] == '\\') {
209,753!
1142
      line[read_len - 1] = ' ';
×
1143
      memcpy(cmd + cmd_len, line, read_len);
×
1144
      cmd_len += read_len;
×
1145
      continue;
×
1146
    }
1147

1148
    if (line[read_len - 1] == '\r') {
209,753!
1149
      line[read_len - 1] = ' ';
×
1150
    }
1151

1152
    memcpy(cmd + cmd_len, line, read_len);
209,753✔
1153
    printf("%s%s\r\n", shell.info.promptHeader, cmd);
209,753✔
1154
    shellRunCommand(cmd, false);
209,753✔
1155
    memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN);
209,753✔
1156
    cmd_len = 0;
209,753✔
1157
  }
1158

1159
  taosMemoryFree(cmd);
223!
1160
  taosMemoryFreeClear(line);
223!
1161
  taosCloseFile(&pFile);
223✔
1162
}
1163

1164
int32_t shellGetGrantInfo(char *buf) {
5✔
1165
  int32_t verType = TSDB_VERSION_UNKNOWN;
5✔
1166
  char    sinfo[256] = {0};
5✔
1167
  tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
5✔
1168
  strtok(sinfo, "\r\n");
5✔
1169

1170
#ifndef TD_ASTRA
1171
  char sql[] = "show grants";
5✔
1172

1173
  TAOS_RES *tres = taos_query(shell.conn, sql);
5✔
1174

1175
  int32_t code = taos_errno(tres);
5✔
1176
  if (code != TSDB_CODE_SUCCESS) {
5!
1177
    if (code != TSDB_CODE_OPS_NOT_SUPPORT && code != TSDB_CODE_MND_NO_RIGHTS &&
×
1178
        code != TSDB_CODE_PAR_PERMISSION_DENIED) {
1179
      fprintf(stderr, "Failed to check Server Edition, Reason:0x%04x:%s\r\n\r\n", code, taos_errstr(tres));
×
1180
    }
1181
    taos_free_result(tres);
×
1182
    return verType;
×
1183
  }
1184

1185
  int32_t num_fields = taos_field_count(tres);
5✔
1186
  if (num_fields == 0) {
5!
1187
    fprintf(stderr, "\r\nInvalid grant information.\r\n");
×
1188
    exit(0);
×
1189
  } else {
1190
    if (tres == NULL) {
5!
1191
      fprintf(stderr, "\r\nGrant information is null.\r\n");
×
1192
      exit(0);
×
1193
    }
1194

1195
    TAOS_FIELD *fields = taos_fetch_fields(tres);
5✔
1196
    TAOS_ROW    row = taos_fetch_row(tres);
5✔
1197
    if (row == NULL) {
5!
1198
      fprintf(stderr, "\r\nFailed to get grant information from server. Abort.\r\n");
×
1199
      exit(0);
×
1200
    }
1201
    char serverVersion[64] = {0};
5✔
1202
    char expiretime[32] = {0};
5✔
1203
    char expired[32] = {0};
5✔
1204

1205
    tstrncpy(serverVersion, row[0], 64);
5✔
1206
    memcpy(expiretime, row[1], fields[1].bytes);
5✔
1207
    memcpy(expired, row[2], fields[2].bytes);
5✔
1208

1209
    if (strcmp(serverVersion, "community") == 0) {
5!
1210
      verType = TSDB_VERSION_OSS;
×
1211
    } else if (strcmp(expiretime, "unlimited") == 0) {
5!
1212
      verType = TSDB_VERSION_ENTERPRISE;
×
1213
      sprintf(buf, "Server is %s, %s and will never expire.\r\n", serverVersion, sinfo);
×
1214
    } else {
1215
      verType = TSDB_VERSION_ENTERPRISE;
5✔
1216
      sprintf(buf, "Server is %s, %s and will expire at %s.\r\n", serverVersion, sinfo, expiretime);
5✔
1217
    }
1218

1219
    taos_free_result(tres);
5✔
1220
  }
1221

1222
  fprintf(stdout, "\r\n");
5✔
1223
#else
1224
  verType = TSDB_VERSION_ENTERPRISE;
1225
  sprintf(buf, "Server is %s, %s and will never expire.\r\n", TD_PRODUCT_NAME, sinfo);
1226
#endif
1227
  return verType;
5✔
1228
}
1229

1230
#ifdef WINDOWS
1231
BOOL shellQueryInterruptHandler(DWORD fdwCtrlType) {
1232
  tsem_post(&shell.cancelSem);
1233
  return TRUE;
1234
}
1235
#else
1236
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) { tsem_post(&shell.cancelSem); }
9✔
1237
#endif
1238

1239
void shellCleanup(void *arg) { taosResetTerminalMode(); }
5✔
1240

1241
void *shellCancelHandler(void *arg) {
5✔
1242
  setThreadName("shellCancelHandler");
5✔
1243
  while (1) {
1244
    if (shell.exit == true) {
15✔
1245
      break;
5✔
1246
    }
1247

1248
    if (tsem_wait(&shell.cancelSem) != 0) {
10!
1249
      taosMsleep(10);
×
1250
      continue;
×
1251
    }
1252

1253
    if (shell.conn) {
10✔
1254
      shellCmdkilled = true;
5✔
1255
      taos_kill_query(shell.conn);
5✔
1256
    }
1257

1258
#ifdef WINDOWS
1259
    printf("\n%s", shell.info.promptHeader);
1260
#endif
1261
  }
1262

1263
  return NULL;
5✔
1264
}
1265

1266
#pragma GCC diagnostic push
1267
#pragma GCC diagnostic ignored "-Wstringop-overflow"
1268

1269
void *shellThreadLoop(void *arg) {
5✔
1270
  setThreadName("shellThreadLoop");
5✔
1271
  taosGetOldTerminalMode();
5✔
1272
  taosThreadCleanupPush(shellCleanup, NULL);
5!
1273

1274
  do {
1275
    char *command = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
5!
1276
    if (command == NULL) {
5!
1277
      printf("failed to malloc command\r\n");
×
1278
      break;
×
1279
    }
1280

1281
    do {
1282
      memset(command, 0, SHELL_MAX_COMMAND_SIZE);
10✔
1283
      taosSetTerminalMode();
10✔
1284

1285
      if (shellReadCommand(command) != 0) {
10✔
1286
        break;
5✔
1287
      }
1288

1289
      taosResetTerminalMode();
5✔
1290
    } while (shellRunCommand(command, true) == 0);
5!
1291

1292
    taosMemoryFreeClear(command);
5!
1293
    shellWriteHistory();
5✔
1294
    shellExit();
5✔
1295
  } while (0);
1296

1297
  taosThreadCleanupPop(1);
5✔
1298
  return NULL;
5✔
1299
}
1300
#pragma GCC diagnostic pop
1301

1302
TAOS* createConnect(SShellArgs *pArgs) {
14,682✔
1303
  char     show[256] = "\0";
14,682✔
1304
  char *   host = NULL;
14,682✔
1305
  uint16_t port = 0;
14,682✔
1306
  char *   user = NULL;
14,682✔
1307
  char *   pwd  = NULL;
14,682✔
1308
  int32_t  code = 0;
14,682✔
1309
  char *   dsnc = NULL;
14,682✔
1310

1311
  // set mode
1312
  if (pArgs->connMode != CONN_MODE_NATIVE && pArgs->dsn) {
14,682!
1313
      dsnc = strToLowerCopy(pArgs->dsn);
×
1314
      if (dsnc == NULL) {
×
1315
          return NULL;
×
1316
      }
1317

1318
      char *cport = NULL;
×
1319
      char error[512] = "\0";
×
1320
      code = parseDsn(dsnc, &host, &cport, &user, &pwd, error);
×
1321
      if (code) {
×
1322
          printf("%s dsn=%s\n", error, dsnc);
×
1323
          free(dsnc);
×
1324
          return NULL;
×
1325
      }
1326

1327
      // default ws port
1328
      if (cport == NULL) {
×
1329
          if (user)
×
1330
              port = DEFAULT_PORT_WS_CLOUD;
×
1331
          else
1332
              port = DEFAULT_PORT_WS_LOCAL;
×
1333
      } else {
1334
          port = atoi(cport);
×
1335
      }
1336

1337
      // websocket
1338
      memcpy(show, pArgs->dsn, 20);
×
1339
      memcpy(show + 20, "...", 3);
×
1340
      memcpy(show + 23, pArgs->dsn + strlen(pArgs->dsn) - 10, 10);
×
1341

1342
  } else {
1343

1344
      host = (char *)pArgs->host;
14,682✔
1345
      user = (char *)pArgs->user;
14,682✔
1346
      pwd  = pArgs->password;
14,682✔
1347

1348
      if (pArgs->port_inputted) {
14,682✔
1349
          port = pArgs->port;
75✔
1350
      } else {
1351
          port = defaultPort(pArgs->connMode, pArgs->dsn);
14,607✔
1352
      }
1353

1354
      sprintf(show, "host:%s port:%d ", host, port);
14,682✔
1355
  }
1356

1357
  // connect main
1358
  TAOS * taos = NULL;
14,682✔
1359
  if (pArgs->auth) {
14,682✔
1360
    taos = taos_connect_auth(host, user, pArgs->auth, pArgs->database, port);
2✔
1361
  } else {
1362
    taos = taos_connect(host, user, pwd, pArgs->database, port);
14,680✔
1363
  }
1364

1365
  // host user pointer in dsnc address
1366
  free(dsnc);
14,682✔
1367
  return taos;
14,682✔
1368
}
1369

1370
int32_t shellExecute(int argc, char *argv[]) {
14,682✔
1371
  int32_t code = 0;
14,682✔
1372
  printf(shell.info.clientVersion, shell.info.cusName, 
29,364!
1373
             workingMode(shell.args.connMode, shell.args.dsn) == CONN_MODE_NATIVE ? STR_NATIVE : STR_WEBSOCKET,
14,682✔
1374
             taos_get_client_info(), shell.info.cusName);
1375
  fflush(stdout);
14,682✔
1376

1377
  SShellArgs *pArgs = &shell.args;
14,682✔
1378
  shell.conn = createConnect(pArgs);
14,682✔
1379

1380
  if (shell.conn == NULL) {
14,682✔
1381
    printf("failed to connect to server, reason: %s [0x%08X]\n%s", taos_errstr(NULL), taos_errno(NULL),
24✔
1382
           ERROR_CODE_DETAIL);
1383
    fflush(stdout);
24✔
1384
    return -1;
24✔
1385
  }
1386

1387
  bool runOnce = pArgs->commands != NULL || pArgs->file[0] != 0;
14,658✔
1388
  shellSetConn(shell.conn, runOnce);
14,658✔
1389
  shellReadHistory();
14,658✔
1390

1391
  if (shell.args.is_bi_mode) {
14,658!
1392
    // need set bi mode
1393
    printf("Set BI mode is true.\n");
×
1394
    taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
×
1395
  }
1396

1397
  if (runOnce) {
14,658✔
1398
    if (pArgs->commands != NULL) {
14,653✔
1399
      printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
14,431✔
1400
      char *cmd = taosStrdup(pArgs->commands);
14,431!
1401
      shellRunCommand(cmd, true);
14,431✔
1402
      taosMemoryFree(cmd);
14,431!
1403
    }
1404

1405
    if (pArgs->file[0] != 0) {
14,653✔
1406
      shellSourceFile(pArgs->file);
222✔
1407
    }
1408

1409
    taos_close(shell.conn);
14,653✔
1410

1411
    shellWriteHistory();
14,653✔
1412
    shellCleanupHistory();
14,653✔
1413
    return 0;
14,653✔
1414
  }
1415

1416
  if ((code = tsem_init(&shell.cancelSem, 0, 0)) != 0) {
5!
1417
    printf("failed to create cancel semaphore since %s\r\n", tstrerror(code));
×
1418
    return code;
×
1419
  }
1420

1421
  TdThread spid = {0};
5✔
1422
  taosThreadCreate(&spid, NULL, shellCancelHandler, NULL);
5✔
1423

1424
  taosSetSignal(SIGTERM, shellQueryInterruptHandler);
5✔
1425
  taosSetSignal(SIGHUP, shellQueryInterruptHandler);
5✔
1426
  taosSetSignal(SIGINT, shellQueryInterruptHandler);
5✔
1427

1428
  char    buf[512] = {0};
5✔
1429
  int32_t verType = shellGetGrantInfo(buf);
5✔
1430
#ifndef WINDOWS
1431
  printfIntroduction(verType);
5✔
1432
#else
1433
  if (verType == TSDB_VERSION_OSS) {
1434
    showAD(false);
1435
  }
1436
#endif
1437
  // printf version
1438
  if (verType == TSDB_VERSION_ENTERPRISE || verType == TSDB_VERSION_CLOUD) {
5!
1439
    printf("%s\n", buf);
5✔
1440
  }
1441

1442
  while (1) {
1443
    taosThreadCreate(&shell.pid, NULL, shellThreadLoop, NULL);
5✔
1444
    taosThreadJoin(shell.pid, NULL);
5✔
1445
    taosThreadClear(&shell.pid);
5✔
1446
    if (shell.exit) {
5!
1447
      tsem_post(&shell.cancelSem);
5✔
1448
      break;
5✔
1449
    }
1450
  }
1451

1452
  if (verType == TSDB_VERSION_OSS) {
5!
1453
    showAD(true);
×
1454
  }
1455

1456
  taosThreadJoin(spid, NULL);
5✔
1457

1458
  shellCleanupHistory();
5✔
1459
  taos_kill_query(shell.conn);
5✔
1460
  taos_close(shell.conn);
5✔
1461

1462
  TAOS_RETURN(code);
5✔
1463
}
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