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

Shell: Support semicolons for separating commands

This commit is contained in:
Conrad Pankoff 2019-08-30 14:54:05 +10:00 committed by Andreas Kling
parent 3e6a0a0533
commit 6bb6176762
3 changed files with 201 additions and 175 deletions

View file

@ -22,6 +22,13 @@ void Parser::commit_subcommand()
m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
}
void Parser::commit_command()
{
if (m_subcommands.is_empty())
return;
m_commands.append({ move(m_subcommands) });
}
void Parser::do_pipe()
{
m_redirections.append({ Redirection::Pipe, STDOUT_FILENO });
@ -38,7 +45,7 @@ void Parser::begin_redirect_write(int fd)
m_redirections.append({ Redirection::FileWrite, fd });
}
Vector<Subcommand> Parser::parse()
Vector<Command> Parser::parse()
{
for (int i = 0; i < m_input.length(); ++i) {
char ch = m_input.characters()[i];
@ -48,6 +55,12 @@ Vector<Subcommand> Parser::parse()
commit_token();
break;
}
if (ch == ';') {
commit_token();
commit_subcommand();
commit_command();
break;
}
if (ch == '|') {
commit_token();
if (m_tokens.is_empty()) {
@ -140,6 +153,7 @@ Vector<Subcommand> Parser::parse()
}
commit_token();
commit_subcommand();
commit_command();
if (!m_subcommands.is_empty()) {
for (auto& redirection : m_subcommands.last().redirections) {
@ -150,5 +164,5 @@ Vector<Subcommand> Parser::parse()
}
}
return move(m_subcommands);
return move(m_commands);
}

View file

@ -27,6 +27,10 @@ struct Subcommand {
Vector<Rewiring> rewirings;
};
struct Command {
Vector<Subcommand> subcommands;
};
class Parser {
public:
explicit Parser(const String& input)
@ -34,11 +38,12 @@ public:
{
}
Vector<Subcommand> parse();
Vector<Command> parse();
private:
void commit_token();
void commit_subcommand();
void commit_command();
void do_pipe();
void begin_redirect_read(int fd);
void begin_redirect_write(int fd);
@ -53,6 +58,7 @@ private:
State m_state { Free };
String m_input;
Vector<Command> m_commands;
Vector<Subcommand> m_subcommands;
Vector<String> m_tokens;
Vector<Redirection> m_redirections;

View file

@ -20,7 +20,7 @@
#include <termios.h>
#include <unistd.h>
//#define SH_DEBUG
#define SH_DEBUG
GlobalState g;
static LineEditor editor;
@ -320,17 +320,18 @@ static int run_command(const String& cmd)
if (cmd.is_empty())
return 0;
auto subcommands = Parser(cmd).parse();
auto commands = Parser(cmd).parse();
#ifdef SH_DEBUG
for (int i = 0; i < subcommands.size(); ++i) {
for (auto& command : commands) {
for (int i = 0; i < command.subcommands.size(); ++i) {
for (int j = 0; j < i; ++j)
dbgprintf(" ");
for (auto& arg : subcommands[i].args) {
for (auto& arg : command.subcommands[i].args) {
dbgprintf("<%s> ", arg.characters());
}
dbgprintf("\n");
for (auto& redirecton : subcommands[i].redirections) {
for (auto& redirecton : command.subcommands[i].redirections) {
for (int j = 0; j < i; ++j)
dbgprintf(" ");
dbgprintf(" ");
@ -352,15 +353,28 @@ static int run_command(const String& cmd)
}
}
}
dbgprintf("\n");
}
#endif
if (subcommands.is_empty())
return 0;
struct termios trm;
tcgetattr(0, &trm);
struct SpawnedProcess {
String name;
pid_t pid;
};
int return_value = 0;
for (auto& command : commands) {
if (command.subcommands.is_empty())
continue;
FileDescriptionCollector fds;
for (int i = 0; i < subcommands.size(); ++i) {
auto& subcommand = subcommands[i];
for (int i = 0; i < command.subcommands.size(); ++i) {
auto& subcommand = command.subcommands[i];
for (auto& redirection : subcommand.redirections) {
switch (redirection.type) {
case Redirection::Pipe: {
@ -371,7 +385,7 @@ static int run_command(const String& cmd)
return 1;
}
subcommand.rewirings.append({ STDOUT_FILENO, pipefd[1] });
auto& next_command = subcommands[i + 1];
auto& next_command = command.subcommands[i + 1];
next_command.rewirings.append({ STDIN_FILENO, pipefd[0] });
fds.add(pipefd[0]);
fds.add(pipefd[1]);
@ -411,20 +425,12 @@ static int run_command(const String& cmd)
}
}
struct termios trm;
tcgetattr(0, &trm);
struct SpawnedProcess {
String name;
pid_t pid;
};
Vector<SpawnedProcess> children;
CommandTimer timer;
for (int i = 0; i < subcommands.size(); ++i) {
auto& subcommand = subcommands[i];
for (int i = 0; i < command.subcommands.size(); ++i) {
auto& subcommand = command.subcommands[i];
Vector<String> argv_string = process_arguments(subcommand.args);
Vector<const char*> argv;
argv.ensure_capacity(argv_string.size());
@ -450,7 +456,7 @@ static int run_command(const String& cmd)
tcsetpgrp(0, getpid());
for (auto& rewiring : subcommand.rewirings) {
#ifdef SH_DEBUG
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), redirection.rewire_fd, redirection.fd);
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), rewiring.rewire_fd, rewiring.fd);
#endif
int rc = dup2(rewiring.rewire_fd, rewiring.fd);
if (rc < 0) {
@ -486,7 +492,6 @@ static int run_command(const String& cmd)
#endif
int wstatus = 0;
int return_value = 0;
for (int i = 0; i < children.size(); ++i) {
auto& child = children[i];
@ -513,6 +518,7 @@ static int run_command(const String& cmd)
}
} while (errno == EINTR);
}
}
// FIXME: Should I really have to tcsetpgrp() after my child has exited?
// Is the terminal controlling pgrp really still the PGID of the dead process?