summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-04 20:36:08 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-04 20:36:08 +0200
commit848044b74c8e31f810ac9af4623f0bf0b75b5c8a (patch)
tree5198c0a7752d864a0a2e3122fcd9a61b77b2252e
parent59dae9a76607b2d5a983ff11df743e2e54f1965a (diff)
downloadserenity-848044b74c8e31f810ac9af4623f0bf0b75b5c8a.zip
Shell: Separate fd rewirings from redirections.
This was unnecessarily confusing. When we build up a chain of commands connected by pipes, we now store the file descriptors of each end of these pipes as rewirings in a vector. The rewirings are then put into effect by calls to dup2().
-rw-r--r--Shell/Parser.cpp2
-rw-r--r--Shell/Parser.h7
-rw-r--r--Shell/main.cpp28
3 files changed, 19 insertions, 18 deletions
diff --git a/Shell/Parser.cpp b/Shell/Parser.cpp
index d49027714b..422b3feeb3 100644
--- a/Shell/Parser.cpp
+++ b/Shell/Parser.cpp
@@ -19,7 +19,7 @@ void Parser::commit_subcommand()
{
if (m_tokens.is_empty())
return;
- m_subcommands.append({ move(m_tokens), move(m_redirections) });
+ m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
}
void Parser::do_pipe()
diff --git a/Shell/Parser.h b/Shell/Parser.h
index 4f3bde4a7f..bbd30599d7 100644
--- a/Shell/Parser.h
+++ b/Shell/Parser.h
@@ -10,7 +10,6 @@ struct Redirection {
FileWrite,
FileWriteAppend,
FileRead,
- Rewire
};
Type type;
int fd { -1 };
@@ -18,9 +17,15 @@ struct Redirection {
String path {};
};
+struct Rewiring {
+ int fd { -1 };
+ int rewire_fd { -1 };
+};
+
struct Subcommand {
Vector<String> args;
Vector<Redirection> redirections;
+ Vector<Rewiring> rewirings;
};
class Parser {
diff --git a/Shell/main.cpp b/Shell/main.cpp
index bd5e9858dd..35ffae7951 100644
--- a/Shell/main.cpp
+++ b/Shell/main.cpp
@@ -315,9 +315,9 @@ static int run_command(const String& cmd)
perror("pipe");
return 1;
}
- subcommand.redirections.append({ Redirection::Rewire, STDOUT_FILENO, pipefd[1] });
+ subcommand.rewirings.append({ STDOUT_FILENO, pipefd[1] });
auto& next_command = subcommands[i + 1];
- next_command.redirections.append({ Redirection::Rewire, STDIN_FILENO, pipefd[0] });
+ next_command.rewirings.append({ STDIN_FILENO, pipefd[0] });
fds.add(pipefd[0]);
fds.add(pipefd[1]);
break;
@@ -328,7 +328,7 @@ static int run_command(const String& cmd)
perror("open");
return 1;
}
- subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
+ subcommand.rewirings.append({ redirection.fd, fd });
fds.add(fd);
break;
}
@@ -338,7 +338,7 @@ static int run_command(const String& cmd)
perror("open");
return 1;
}
- subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
+ subcommand.rewirings.append({ redirection.fd, fd });
fds.add(fd);
break;
}
@@ -348,12 +348,10 @@ static int run_command(const String& cmd)
perror("open");
return 1;
}
- subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
+ subcommand.rewirings.append({ redirection.fd, fd });
fds.add(fd);
break;
}
- case Redirection::Rewire:
- break; // ignore
}
}
}
@@ -390,16 +388,14 @@ static int run_command(const String& cmd)
if (!child) {
setpgid(0, 0);
tcsetpgrp(0, getpid());
- for (auto& redirection : subcommand.redirections) {
- if (redirection.type == Redirection::Rewire) {
-#ifdef SH_DEBUGsh
- dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), redirection.rewire_fd, redirection.fd);
+ for (auto& rewiring : subcommand.rewirings) {
+#ifdef SH_DEBUG
+ dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), redirection.rewire_fd, redirection.fd);
#endif
- int rc = dup2(redirection.rewire_fd, redirection.fd);
- if (rc < 0) {
- perror("dup2");
- return 1;
- }
+ int rc = dup2(rewiring.rewire_fd, rewiring.fd);
+ if (rc < 0) {
+ perror("dup2");
+ return 1;
}
}