提交 f03055c3 编写于 作者: wafwerar's avatar wafwerar

shell: change shell record source file command

上级 e99782f4
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
static bool shellIsEmptyCommand(const char *cmd); static bool shellIsEmptyCommand(const char *cmd);
static int32_t shellRunSingleCommand(char *command); static int32_t shellRunSingleCommand(char *command);
static int32_t shellRunCommand(char *command); static void shellRecordCommandToHistory(char *command);
static int32_t shellRunCommand(char *command, bool recordHistory);
static void shellRunSingleCommandImp(char *command); static void shellRunSingleCommandImp(char *command);
static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision); static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision);
static int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres); static int32_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
...@@ -101,11 +102,7 @@ int32_t shellRunSingleCommand(char *command) { ...@@ -101,11 +102,7 @@ int32_t shellRunSingleCommand(char *command) {
return 0; return 0;
} }
int32_t shellRunCommand(char *command) { void shellRecordCommandToHistory(char *command) {
if (shellIsEmptyCommand(command)) {
return 0;
}
SShellHistory *pHistory = &shell.history; SShellHistory *pHistory = &shell.history;
if (pHistory->hstart == pHistory->hend || if (pHistory->hstart == pHistory->hend ||
pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL || pHistory->hist[(pHistory->hend + SHELL_MAX_HISTORY_SIZE - 1) % SHELL_MAX_HISTORY_SIZE] == NULL ||
...@@ -120,6 +117,14 @@ int32_t shellRunCommand(char *command) { ...@@ -120,6 +117,14 @@ int32_t shellRunCommand(char *command) {
pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE; pHistory->hstart = (pHistory->hstart + 1) % SHELL_MAX_HISTORY_SIZE;
} }
} }
}
int32_t shellRunCommand(char *command, bool recordHistory) {
if (shellIsEmptyCommand(command)) {
return 0;
}
if (recordHistory) shellRecordCommandToHistory(command);
char quote = 0, *cmd = command; char quote = 0, *cmd = command;
for (char c = *command++; c != 0; c = *command++) { for (char c = *command++; c != 0; c = *command++) {
...@@ -826,11 +831,15 @@ void shellSourceFile(const char *file) { ...@@ -826,11 +831,15 @@ void shellSourceFile(const char *file) {
size_t cmd_len = 0; size_t cmd_len = 0;
char *line = NULL; char *line = NULL;
char fullname[PATH_MAX] = {0}; char fullname[PATH_MAX] = {0};
char sourceFileCommand[PATH_MAX + 8] = {0};
if (taosExpandDir(file, fullname, PATH_MAX) != 0) { if (taosExpandDir(file, fullname, PATH_MAX) != 0) {
tstrncpy(fullname, file, PATH_MAX); tstrncpy(fullname, file, PATH_MAX);
} }
sprintf(sourceFileCommand, "source %s;",fullname);
shellRecordCommandToHistory(sourceFileCommand);
TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM); TdFilePtr pFile = taosOpenFile(fullname, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) { if (pFile == NULL) {
fprintf(stderr, "failed to open file %s\r\n", fullname); fprintf(stderr, "failed to open file %s\r\n", fullname);
...@@ -853,9 +862,13 @@ void shellSourceFile(const char *file) { ...@@ -853,9 +862,13 @@ void shellSourceFile(const char *file) {
continue; continue;
} }
if (line[read_len - 1] == '\r') {
line[read_len - 1] = ' ';
}
memcpy(cmd + cmd_len, line, read_len); memcpy(cmd + cmd_len, line, read_len);
printf("%s%s\r\n", shell.info.promptHeader, cmd); printf("%s%s\r\n", shell.info.promptHeader, cmd);
shellRunCommand(cmd); shellRunCommand(cmd, false);
memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN); memset(cmd, 0, TSDB_MAX_ALLOWED_SQL_LEN);
cmd_len = 0; cmd_len = 0;
} }
...@@ -977,7 +990,7 @@ void *shellThreadLoop(void *arg) { ...@@ -977,7 +990,7 @@ void *shellThreadLoop(void *arg) {
} }
taosResetTerminalMode(); taosResetTerminalMode();
} while (shellRunCommand(command) == 0); } while (shellRunCommand(command, true) == 0);
taosMemoryFreeClear(command); taosMemoryFreeClear(command);
shellWriteHistory(); shellWriteHistory();
...@@ -1019,7 +1032,7 @@ int32_t shellExecute() { ...@@ -1019,7 +1032,7 @@ int32_t shellExecute() {
if (pArgs->commands != NULL) { if (pArgs->commands != NULL) {
printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands); printf("%s%s\r\n", shell.info.promptHeader, pArgs->commands);
char *cmd = strdup(pArgs->commands); char *cmd = strdup(pArgs->commands);
shellRunCommand(cmd); shellRunCommand(cmd, true);
taosMemoryFree(cmd); taosMemoryFree(cmd);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册