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

#define __USE_XOPEN
S
Shengliang Guan 已提交
17
#include "shellInt.h"
H
hzcheng 已提交
18

19 20 21 22 23
#define LEFT  1
#define RIGHT 2
#define UP    3
#define DOWN  4
#define PSIZE shell.info.promptSize
S
Shengliang Guan 已提交
24

H
hzcheng 已提交
25
typedef struct {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  char    *buffer;
  char    *command;
  uint32_t commandSize;
  uint32_t bufferSize;
  uint32_t cursorOffset;
  uint32_t screenOffset;
  uint32_t endOffset;
} SShellCmd;

static int32_t shellCountPrefixOnes(uint8_t c);
static void    shellGetPrevCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width);
static void    shellGetNextCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width);
static void    shellInsertChar(SShellCmd *cmd, char *c, int size);
static void    shellBackspaceChar(SShellCmd *cmd);
static void    shellClearLineBefore(SShellCmd *cmd);
static void    shellClearLineAfter(SShellCmd *cmd);
static void    shellDeleteChar(SShellCmd *cmd);
static void    shellMoveCursorLeft(SShellCmd *cmd);
static void    shellMoveCursorRight(SShellCmd *cmd);
static void    shellPositionCursorHome(SShellCmd *cmd);
static void    shellPositionCursorEnd(SShellCmd *cmd);
static void    shellPrintChar(char c, int32_t times);
static void    shellPositionCursor(int32_t step, int32_t direction);
static void    shellUpdateBuffer(SShellCmd *cmd);
static int32_t shellIsReadyGo(SShellCmd *cmd);
static void    shellGetMbSizeInfo(const char *str, int32_t *size, int32_t *width);
static void    shellResetCommand(SShellCmd *cmd, const char s[]);
static void    shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos);
static void    shellShowOnScreen(SShellCmd *cmd);

#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
static void shellPrintContinuePrompt() { printf("%s", shell.args.promptContinue); }
static void shellPrintPrompt() { printf("%s", shell.args.promptHeader); }

void shellUpdateBuffer(SShellCmd *cmd) {
  if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " ");
  strcat(cmd->buffer, cmd->command);

  memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE);
  cmd->cursorOffset = 0;
}

int shellIsReadyGo(SShellCmd *cmd) {
  char *total = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
  memset(total, 0, SHELL_MAX_COMMAND_SIZE);
  sprintf(total, "%s%s", cmd->buffer, cmd->command);

  char *reg_str =
      "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^"
      "\\s*clear\\s*$)";
  if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) {
    taosMemoryFree(total);
    return 1;
  }

  taosMemoryFree(total);
  return 0;
}

void shellInsertChar(SShellCmd *cmd, char c) {
  if (cmd->cursorOffset >= SHELL_MAX_COMMAND_SIZE) {
    fprintf(stdout, "sql is larger than %d bytes", SHELL_MAX_COMMAND_SIZE);
    return;
  }
  cmd->command[cmd->cursorOffset++] = c;
}

int32_t shellReadCommand(char command[]) {
  SShellCmd cmd;
  memset(&cmd, 0, sizeof(cmd));
  cmd.buffer = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE);
  cmd.command = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE);

  // Read input.
  char c;
  while (1) {
    c = getchar();

    switch (c) {
      case '\n':
      case '\r':
        if (shellIsReadyGo(&cmd)) {
          sprintf(command, "%s%s", cmd.buffer, cmd.command);
          taosMemoryFree(cmd.buffer);
          cmd.buffer = NULL;
          taosMemoryFree(cmd.command);
          cmd.command = NULL;
          return 0;
        } else {
          shellPrintContinuePrompt();
          shellUpdateBuffer(&cmd);
        }
        break;
      default:
        shellInsertChar(&cmd, c);
    }
  }

  return 0;
}

#else
H
hzcheng 已提交
128

129 130
int32_t shellCountPrefixOnes(uint8_t c) {
  uint8_t mask = 127;
H
hzcheng 已提交
131
  mask = ~mask;
132
  int32_t ret = 0;
H
hzcheng 已提交
133 134 135 136 137 138 139 140
  while ((c & mask) != 0) {
    ret++;
    c <<= 1;
  }

  return ret;
}

141
void shellGetPrevCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width) {
H
hzcheng 已提交
142 143
  assert(pos > 0);

wafwerar's avatar
wafwerar 已提交
144
  TdWchar wc;
H
hzcheng 已提交
145 146 147 148 149 150
  *size = 0;
  *width = 0;

  while (--pos >= 0) {
    *size += 1;

151
    if (str[pos] > 0 || shellCountPrefixOnes((uint8_t)str[pos]) > 1) break;
H
hzcheng 已提交
152 153
  }

154
  int32_t rc = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
A
Amos Bird 已提交
155
  assert(rc == *size);
H
hzcheng 已提交
156

wafwerar's avatar
wafwerar 已提交
157
  *width = taosWcharWidth(wc);
H
hzcheng 已提交
158 159
}

160
void shellGetNextCharSize(const char *str, int32_t pos, int32_t *size, int32_t *width) {
H
hzcheng 已提交
161 162
  assert(pos >= 0);

wafwerar's avatar
wafwerar 已提交
163 164 165
  TdWchar wc;
  *size = taosMbToWchar(&wc, str + pos, MB_CUR_MAX);
  *width = taosWcharWidth(wc);
H
hzcheng 已提交
166 167
}

168
void shellInsertChar(SShellCmd *cmd, char *c, int32_t size) {
H
hzcheng 已提交
169 170
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

wafwerar's avatar
wafwerar 已提交
171 172
  TdWchar wc;
  if (taosMbToWchar(&wc, c, size) < 0) return;
H
hzcheng 已提交
173

174
  shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
H
hzcheng 已提交
175 176 177 178 179 180 181
  /* update the buffer */
  memmove(cmd->command + cmd->cursorOffset + size, cmd->command + cmd->cursorOffset,
          cmd->commandSize - cmd->cursorOffset);
  memcpy(cmd->command + cmd->cursorOffset, c, size);
  /* update the values */
  cmd->commandSize += size;
  cmd->cursorOffset += size;
wafwerar's avatar
wafwerar 已提交
182 183
  cmd->screenOffset += taosWcharWidth(wc);
  cmd->endOffset += taosWcharWidth(wc);
184
  shellShowOnScreen(cmd);
H
hzcheng 已提交
185 186
}

187
void shellBackspaceChar(SShellCmd *cmd) {
H
hzcheng 已提交
188 189 190
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset > 0) {
191 192 193 194
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
    int32_t size = 0;
    int32_t width = 0;
    shellGetPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width);
H
hzcheng 已提交
195 196 197 198 199 200
    memmove(cmd->command + cmd->cursorOffset - size, cmd->command + cmd->cursorOffset,
            cmd->commandSize - cmd->cursorOffset);
    cmd->commandSize -= size;
    cmd->cursorOffset -= size;
    cmd->screenOffset -= width;
    cmd->endOffset -= width;
201
    shellShowOnScreen(cmd);
H
hzcheng 已提交
202 203 204
  }
}

205
void shellClearLineBefore(SShellCmd *cmd) {
206 207
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

208
  shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
S
Shengliang Guan 已提交
209
  memmove(cmd->command, cmd->command + cmd->cursorOffset, cmd->commandSize - cmd->cursorOffset);
210 211 212 213
  cmd->commandSize -= cmd->cursorOffset;
  cmd->cursorOffset = 0;
  cmd->screenOffset = 0;
  cmd->endOffset = cmd->commandSize;
214
  shellShowOnScreen(cmd);
215 216
}

217
void shellClearLineAfter(SShellCmd *cmd) {
218 219
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

220
  shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
221 222
  cmd->commandSize -= cmd->endOffset - cmd->cursorOffset;
  cmd->endOffset = cmd->cursorOffset;
223
  shellShowOnScreen(cmd);
224 225
}

226
void shellDeleteChar(SShellCmd *cmd) {
H
hzcheng 已提交
227 228 229
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset < cmd->commandSize) {
230 231 232 233
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
    int32_t size = 0;
    int32_t width = 0;
    shellGetNextCharSize(cmd->command, cmd->cursorOffset, &size, &width);
H
hzcheng 已提交
234 235 236 237
    memmove(cmd->command + cmd->cursorOffset, cmd->command + cmd->cursorOffset + size,
            cmd->commandSize - cmd->cursorOffset - size);
    cmd->commandSize -= size;
    cmd->endOffset -= width;
238
    shellShowOnScreen(cmd);
H
hzcheng 已提交
239 240 241
  }
}

242
void shellMoveCursorLeft(SShellCmd *cmd) {
H
hzcheng 已提交
243 244 245
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset > 0) {
246 247 248 249
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
    int32_t size = 0;
    int32_t width = 0;
    shellGetPrevCharSize(cmd->command, cmd->cursorOffset, &size, &width);
H
hzcheng 已提交
250 251
    cmd->cursorOffset -= size;
    cmd->screenOffset -= width;
252
    shellShowOnScreen(cmd);
H
hzcheng 已提交
253 254 255
  }
}

256
void shellMoveCursorRight(SShellCmd *cmd) {
H
hzcheng 已提交
257 258 259
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset < cmd->commandSize) {
260 261 262 263
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
    int32_t size = 0;
    int32_t width = 0;
    shellGetNextCharSize(cmd->command, cmd->cursorOffset, &size, &width);
H
hzcheng 已提交
264 265
    cmd->cursorOffset += size;
    cmd->screenOffset += width;
266
    shellShowOnScreen(cmd);
H
hzcheng 已提交
267 268 269
  }
}

270
void shellPositionCursorHome(SShellCmd *cmd) {
H
hzcheng 已提交
271 272 273
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset > 0) {
274
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
H
hzcheng 已提交
275 276
    cmd->cursorOffset = 0;
    cmd->screenOffset = 0;
277
    shellShowOnScreen(cmd);
H
hzcheng 已提交
278 279 280
  }
}

281
void shellPositionCursorEnd(SShellCmd *cmd) {
H
hzcheng 已提交
282 283 284
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

  if (cmd->cursorOffset < cmd->commandSize) {
285
    shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
H
hzcheng 已提交
286 287
    cmd->cursorOffset = cmd->commandSize;
    cmd->screenOffset = cmd->endOffset;
288
    shellShowOnScreen(cmd);
H
hzcheng 已提交
289 290 291
  }
}

292 293
void shellPrintChar(char c, int32_t times) {
  for (int32_t i = 0; i < times; i++) {
H
hzcheng 已提交
294 295 296 297 298
    fprintf(stdout, "%c", c);
  }
  fflush(stdout);
}

299
void shellPositionCursor(int32_t step, int32_t direction) {
H
hzcheng 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
  if (step > 0) {
    if (direction == LEFT) {
      fprintf(stdout, "\033[%dD", step);
    } else if (direction == RIGHT) {
      fprintf(stdout, "\033[%dC", step);
    } else if (direction == UP) {
      fprintf(stdout, "\033[%dA", step);
    } else if (direction == DOWN) {
      fprintf(stdout, "\033[%dB", step);
    }
    fflush(stdout);
  }
}

314
void shellUpdateBuffer(SShellCmd *cmd) {
H
hzcheng 已提交
315 316
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

317
  if (shellRegexMatch(cmd->buffer, "(\\s+$)|(^$)", REG_EXTENDED)) strcat(cmd->command, " ");
H
hzcheng 已提交
318 319 320
  strcat(cmd->buffer, cmd->command);
  cmd->bufferSize += cmd->commandSize;

321
  memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE);
H
hzcheng 已提交
322 323 324 325
  cmd->cursorOffset = 0;
  cmd->screenOffset = 0;
  cmd->commandSize = 0;
  cmd->endOffset = 0;
326
  shellShowOnScreen(cmd);
H
hzcheng 已提交
327 328
}

329
int32_t shellIsReadyGo(SShellCmd *cmd) {
H
hzcheng 已提交
330 331
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

332 333
  char *total = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE);
  memset(cmd->command + cmd->commandSize, 0, SHELL_MAX_COMMAND_SIZE - cmd->commandSize);
H
hzcheng 已提交
334 335 336
  sprintf(total, "%s%s", cmd->buffer, cmd->command);

  char *reg_str =
S
Shengliang Guan 已提交
337 338
      "(^.*;\\s*$)|(^\\s*$)|(^\\s*exit\\s*$)|(^\\s*q\\s*$)|(^\\s*quit\\s*$)|(^"
      "\\s*clear\\s*$)";
339
  if (shellRegexMatch(total, reg_str, REG_EXTENDED | REG_ICASE)) {
wafwerar's avatar
wafwerar 已提交
340
    taosMemoryFree(total);
H
hzcheng 已提交
341 342 343
    return 1;
  }

wafwerar's avatar
wafwerar 已提交
344
  taosMemoryFree(total);
H
hzcheng 已提交
345 346 347
  return 0;
}

348 349
void shellGetMbSizeInfo(const char *str, int32_t *size, int32_t *width) {
  TdWchar *wc = (TdWchar *)taosMemoryCalloc(sizeof(TdWchar), SHELL_MAX_COMMAND_SIZE);
H
hzcheng 已提交
350
  *size = strlen(str);
351 352
  taosMbsToWchars(wc, str, SHELL_MAX_COMMAND_SIZE);
  *width = taosWcharsWidth(wc, SHELL_MAX_COMMAND_SIZE);
wafwerar's avatar
wafwerar 已提交
353
  taosMemoryFree(wc);
H
hzcheng 已提交
354 355
}

356
void shellResetCommand(SShellCmd *cmd, const char s[]) {
H
hzcheng 已提交
357 358
  assert(cmd->cursorOffset <= cmd->commandSize && cmd->endOffset >= cmd->screenOffset);

359 360 361 362 363 364 365
  shellClearScreen(cmd->endOffset + PSIZE, cmd->screenOffset + PSIZE);
  memset(cmd->buffer, 0, SHELL_MAX_COMMAND_SIZE);
  memset(cmd->command, 0, SHELL_MAX_COMMAND_SIZE);
  strncpy(cmd->command, s, SHELL_MAX_COMMAND_SIZE);
  int32_t size = 0;
  int32_t width = 0;
  shellGetMbSizeInfo(s, &size, &width);
H
hzcheng 已提交
366 367 368 369 370
  cmd->bufferSize = 0;
  cmd->commandSize = size;
  cmd->cursorOffset = size;
  cmd->screenOffset = width;
  cmd->endOffset = width;
371
  shellShowOnScreen(cmd);
H
hzcheng 已提交
372
}
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos) {
  struct winsize w;
  if (ioctl(0, TIOCGWINSZ, &w) < 0 || w.ws_col == 0 || w.ws_row == 0) {
    // fprintf(stderr, "No stream device, and use default value(col 120, row 30)\n");
    w.ws_col = 120;
    w.ws_row = 30;
  }

  int32_t cursor_x = cursor_pos / w.ws_col;
  int32_t cursor_y = cursor_pos % w.ws_col;
  int32_t command_x = ecmd_pos / w.ws_col;
  shellPositionCursor(cursor_y, LEFT);
  shellPositionCursor(command_x - cursor_x, DOWN);
  fprintf(stdout, "\033[2K");
  for (int32_t i = 0; i < command_x; i++) {
    shellPositionCursor(1, UP);
    fprintf(stdout, "\033[2K");
  }
  fflush(stdout);
}

void shellShowOnScreen(SShellCmd *cmd) {
  struct winsize w;
  if (ioctl(0, TIOCGWINSZ, &w) < 0 || w.ws_col == 0 || w.ws_row == 0) {
S
Shengliang Guan 已提交
398
    // fprintf(stderr, "No stream device\n");
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    w.ws_col = 120;
    w.ws_row = 30;
  }

  TdWchar wc;
  int32_t size = 0;

  // Print out the command.
  char *total_string = taosMemoryMalloc(SHELL_MAX_COMMAND_SIZE);
  memset(total_string, '\0', SHELL_MAX_COMMAND_SIZE);
  if (strcmp(cmd->buffer, "") == 0) {
    sprintf(total_string, "%s%s", shell.info.promptHeader, cmd->command);
  } else {
    sprintf(total_string, "%s%s", shell.info.promptContinue, cmd->command);
  }

  int32_t remain_column = w.ws_col;
  for (char *str = total_string; size < cmd->commandSize + PSIZE;) {
    int32_t ret = taosMbToWchar(&wc, str, MB_CUR_MAX);
    if (ret < 0) break;
    size += ret;
    /* assert(size >= 0); */
    int32_t width = taosWcharWidth(wc);
    if (remain_column > width) {
      printf("%lc", wc);
      remain_column -= width;
    } else {
      if (remain_column == width) {
        printf("%lc\n\r", wc);
        remain_column = w.ws_col;
      } else {
        printf("\n\r%lc", wc);
        remain_column = w.ws_col - width;
      }
    }

    str = total_string + size;
  }

  taosMemoryFree(total_string);

  // Position the cursor
  int32_t cursor_pos = cmd->screenOffset + PSIZE;
  int32_t ecmd_pos = cmd->endOffset + PSIZE;

  int32_t cursor_x = cursor_pos / w.ws_col;
  int32_t cursor_y = cursor_pos % w.ws_col;
  // int32_t cursor_y = cursor % w.ws_col;
  int32_t command_x = ecmd_pos / w.ws_col;
  int32_t command_y = ecmd_pos % w.ws_col;
  // int32_t command_y = (command.size() + PSIZE) % w.ws_col;
  shellPositionCursor(command_y, LEFT);
  shellPositionCursor(command_x, UP);
  shellPositionCursor(cursor_x, DOWN);
  shellPositionCursor(cursor_y, RIGHT);
  fflush(stdout);
}

int32_t shellReadCommand(char *command) {
  SShellHistory *pHistory = &shell.history;
  SShellCmd      cmd = {0};
  uint32_t       hist_counter = pHistory->hend;
  char           utf8_array[10] = "\0";

  cmd.buffer = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE);
  cmd.command = (char *)taosMemoryCalloc(1, SHELL_MAX_COMMAND_SIZE);
  shellShowOnScreen(&cmd);

  // Read input.
  char c;
  while (1) {
    c = (char)getchar();  // getchar() return an 'int32_t' value

    if (c == EOF) {
      return c;
    }

    if (c < 0) {  // For UTF-8
      int32_t count = shellCountPrefixOnes(c);
      utf8_array[0] = c;
      for (int32_t k = 1; k < count; k++) {
        c = (char)getchar();
        utf8_array[k] = c;
      }
      shellInsertChar(&cmd, utf8_array, count);
    } else if (c < '\033') {
      // Ctrl keys.  TODO: Implement ctrl combinations
      switch (c) {
        case 1:  // ctrl A
          shellPositionCursorHome(&cmd);
          break;
        case 3:
          printf("\n");
          shellResetCommand(&cmd, "");
          kill(0, SIGINT);
          break;
        case 4:  // EOF or Ctrl+D
          printf("\n");
          return -1;
        case 5:  // ctrl E
          shellPositionCursorEnd(&cmd);
          break;
        case 8:
          shellBackspaceChar(&cmd);
          break;
        case '\n':
        case '\r':
          printf("\n");
          if (shellIsReadyGo(&cmd)) {
            sprintf(command, "%s%s", cmd.buffer, cmd.command);
            taosMemoryFreeClear(cmd.buffer);
            taosMemoryFreeClear(cmd.command);
            return 0;
          } else {
            shellUpdateBuffer(&cmd);
          }
          break;
        case 11:  // Ctrl + K;
          shellClearLineAfter(&cmd);
          break;
        case 12:  // Ctrl + L;
          system("clear");
          shellShowOnScreen(&cmd);
          break;
        case 21:  // Ctrl + U;
          shellClearLineBefore(&cmd);
          break;
      }
    } else if (c == '\033') {
      c = (char)getchar();
      switch (c) {
        case '[':
          c = (char)getchar();
          switch (c) {
            case 'A':  // Up arrow
              if (hist_counter != pHistory->hstart) {
                hist_counter = (hist_counter + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE;
                shellResetCommand(&cmd, (pHistory->hist[hist_counter] == NULL) ? "" : pHistory->hist[hist_counter]);
              }
              break;
            case 'B':  // Down arrow
              if (hist_counter != pHistory->hend) {
                int32_t next_hist = (hist_counter + 1) % SHELL_MAX_HISTORY_SIZE;

                if (next_hist != pHistory->hend) {
                  shellResetCommand(&cmd, (pHistory->hist[next_hist] == NULL) ? "" : pHistory->hist[next_hist]);
                } else {
                  shellResetCommand(&cmd, "");
                }
                hist_counter = next_hist;
              }
              break;
            case 'C':  // Right arrow
              shellMoveCursorRight(&cmd);
              break;
            case 'D':  // Left arrow
              shellMoveCursorLeft(&cmd);
              break;
            case '1':
              if ((c = (char)getchar()) == '~') {
                // Home key
                shellPositionCursorHome(&cmd);
              }
              break;
            case '2':
              if ((c = (char)getchar()) == '~') {
                // Insert key
              }
              break;
            case '3':
              if ((c = (char)getchar()) == '~') {
                // Delete key
                shellDeleteChar(&cmd);
              }
              break;
            case '4':
              if ((c = (char)getchar()) == '~') {
                // End key
                shellPositionCursorEnd(&cmd);
              }
              break;
            case '5':
              if ((c = (char)getchar()) == '~') {
                // Page up key
              }
              break;
            case '6':
              if ((c = (char)getchar()) == '~') {
                // Page down key
              }
              break;
            case 72:
              // Home key
              shellPositionCursorHome(&cmd);
              break;
            case 70:
              // End key
              shellPositionCursorEnd(&cmd);
              break;
          }
          break;
      }
    } else if (c == 0x7f) {
      // press delete key
      shellBackspaceChar(&cmd);
    } else {
      shellInsertChar(&cmd, &c, 1);
    }
  }

  return 0;
}

#endif