1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

Shell: Use a Vector<char> for the main input buffer.

This commit is contained in:
Andreas Kling 2019-05-07 01:01:50 +02:00
parent e66f88ed60
commit e63cc38861

View file

@ -352,12 +352,10 @@ struct CommandTimer {
CElapsedTimer timer; CElapsedTimer timer;
}; };
static int runcmd(char* cmd) static int run_command(const String& cmd)
{ {
if (cmd[0] == 0) if (cmd.is_empty())
return 0; return 0;
char buf[128];
memcpy(buf, cmd, 128);
auto subcommands = Parser(cmd).parse(); auto subcommands = Parser(cmd).parse();
@ -571,14 +569,12 @@ int main(int argc, char** argv)
return 1; return 1;
} }
char linebuf[128]; Vector<char, 256> line_buffer;
int linedx = 0;
linebuf[0] = '\0';
{ {
char cwdbuf[1024]; auto* cwd = getcwd(nullptr, 0);
getcwd(cwdbuf, sizeof(cwdbuf)); g->cwd = cwd;
g->cwd = cwdbuf; free(cwd);
} }
prompt(); prompt();
for (;;) { for (;;) {
@ -589,12 +585,11 @@ int main(int argc, char** argv)
if (nread < 0) { if (nread < 0) {
if (errno == EINTR) { if (errno == EINTR) {
if (g->was_interrupted) { if (g->was_interrupted) {
if (linedx != 0) if (!line_buffer.is_empty())
printf("^C"); printf("^C");
} }
g->was_interrupted = false; g->was_interrupted = false;
linebuf[0] = '\0'; line_buffer.clear();
linedx = 0;
putchar('\n'); putchar('\n');
prompt(); prompt();
continue; continue;
@ -608,46 +603,44 @@ int main(int argc, char** argv)
if (ch == 0) if (ch == 0)
continue; continue;
if (ch == 8 || ch == g->termios.c_cc[VERASE]) { if (ch == 8 || ch == g->termios.c_cc[VERASE]) {
if (linedx == 0) if (line_buffer.is_empty())
continue; continue;
linebuf[--linedx] = '\0'; line_buffer.take_last();
putchar(8); putchar(8);
fflush(stdout); fflush(stdout);
continue; continue;
} }
if (ch == g->termios.c_cc[VWERASE]) { if (ch == g->termios.c_cc[VWERASE]) {
bool has_seen_nonspace = false; bool has_seen_nonspace = false;
while (linedx > 0) { while (!line_buffer.is_empty()) {
if (isspace(linebuf[linedx - 1])) { if (isspace(line_buffer.last())) {
if (has_seen_nonspace) if (has_seen_nonspace)
break; break;
} else { } else {
has_seen_nonspace = true; has_seen_nonspace = true;
} }
putchar(0x8); putchar(0x8);
--linedx; line_buffer.take_last();
} }
fflush(stdout); fflush(stdout);
continue; continue;
} }
if (ch == g->termios.c_cc[VKILL]) { if (ch == g->termios.c_cc[VKILL]) {
if (linedx == 0) if (line_buffer.is_empty())
continue; continue;
for (; linedx; --linedx) for (int i = 0; i < line_buffer.size(); ++i)
putchar(0x8); putchar(0x8);
linebuf[0] = '\0'; line_buffer.clear();
fflush(stdout); fflush(stdout);
continue; continue;
} }
putchar(ch); putchar(ch);
fflush(stdout); fflush(stdout);
if (ch != '\n') { if (ch != '\n') {
linebuf[linedx++] = ch; line_buffer.append(ch);
linebuf[linedx] = '\0';
} else { } else {
runcmd(linebuf); run_command(String::copy(line_buffer));
linebuf[0] = '\0'; line_buffer.clear();
linedx = 0;
prompt(); prompt();
} }
} }