Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 948 #1018

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions tools/shell/embedded_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// prompt for user input
const char* PROMPT = "kuzu> ";
const char* ALTPROMPT = "..> ";
// file to read/write shell history
const char* HISTORY_PATH = "history.txt";

Expand Down Expand Up @@ -48,6 +49,9 @@ const regex specialChars{R"([-[\]{}()*+?.,\^$|#\s])"};
vector<string> nodeTableNames;
vector<string> relTableNames;

bool continueLine = false;
string currLine;

void EmbeddedShell::updateTableNames() {
nodeTableNames.clear();
relTableNames.clear();
Expand Down Expand Up @@ -171,8 +175,15 @@ EmbeddedShell::EmbeddedShell(

void EmbeddedShell::run() {
char* line;
while ((line = linenoise(PROMPT)) != nullptr) {
string query;
stringstream ss;
while ((line = linenoise(continueLine ? ALTPROMPT : PROMPT)) != nullptr) {
auto lineStr = string(line);
if (continueLine) {
lineStr = currLine + lineStr;
currLine = "";
continueLine = false;
}
if (line == shellCommand.HELP) {
printHelp();
} else if (line == shellCommand.CLEAR) {
Expand All @@ -198,11 +209,20 @@ void EmbeddedShell::run() {
} else if (lineStr.rfind(shellCommand.SHOW_REL) == 0) {
printRelSchema(lineStr.substr(shellCommand.SHOW_REL.length()));
} else if (!lineStr.empty()) {
auto queryResult = conn->query(lineStr);
if (queryResult->isSuccess()) {
printExecutionResult(*queryResult);
} else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
ss.clear();
ss.str(lineStr);
while (getline(ss, query, ';')) {
if (ss.eof() && ss.peek() == -1) {
continueLine = true;
currLine += query + " ";
} else {
auto queryResult = conn->query(query);
if (queryResult->isSuccess()) {
printExecutionResult(*queryResult);
} else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
}
}
}
}
updateTableNames();
Expand Down
42 changes: 33 additions & 9 deletions tools/shell/linenoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
#include <unistd.h>

#include <cstddef>
#include <string>

#include <sys/ioctl.h>

Expand Down Expand Up @@ -181,6 +182,9 @@ static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char* line);
static void refreshLine(struct linenoiseState* l);

std::string oldInput = "";
bool inputLeft;

/* Debugging macro. */
#if 0
FILE *lndebug_fp = NULL;
Expand Down Expand Up @@ -680,6 +684,14 @@ static void refreshLine(struct linenoiseState* l) {
refreshSingleLine(l);
}

bool pastedInput(int ifd) {
struct pollfd fd {
ifd, POLLIN, 0
};
int isPasted = poll(&fd, 1, 0);
return (isPasted != 0);
}

/* Insert the character 'c' at cursor current position.
*
* On error writing to the terminal -1 is returned, otherwise 0. */
Expand All @@ -695,11 +707,7 @@ int linenoiseEditInsert(struct linenoiseState* l, char c) {
if (write(l->ofd, &d, 1) == -1)
return -1;
}
struct pollfd fd {
l->ifd, POLLIN, 0
};
int pastedInput = poll(&fd, 1, 0);
if (pastedInput == 0) {
if (!pastedInput(l->ifd)) {
refreshLine(l);
}
} else {
Expand Down Expand Up @@ -878,9 +886,18 @@ static int linenoiseEdit(
int nread;
char seq[3];

nread = read(l.ifd, &c, 1);
if (nread <= 0)
return l.len;
if (inputLeft) {
c = oldInput[0];
oldInput.erase(0, 1);
if (!c) {
inputLeft = false;
}
}
if (!inputLeft) {
nread = read(l.ifd, &c, 1);
if (nread <= 0)
return l.len;
}

/* Only autocomplete when the callback is set. It returns < 0 when
* there was an error reading from fd. Otherwise it will return the
Expand All @@ -894,9 +911,16 @@ static int linenoiseEdit(
if (c == 0)
continue;
}

switch (c) {
case ENTER: /* enter */
if (pastedInput(l.ifd)) {
linenoiseEditInsert(&l, ' ');
inputLeft = true;
while (pastedInput(l.ifd)) {
read(l.ifd, &c, 1);
oldInput += c;
}
}
history_len--;
free(history[history_len]);
if (mlmode)
Expand Down