mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 19:15:06 +00:00
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().
This commit is contained in:
parent
59dae9a766
commit
848044b74c
3 changed files with 19 additions and 18 deletions
|
@ -19,7 +19,7 @@ void Parser::commit_subcommand()
|
||||||
{
|
{
|
||||||
if (m_tokens.is_empty())
|
if (m_tokens.is_empty())
|
||||||
return;
|
return;
|
||||||
m_subcommands.append({ move(m_tokens), move(m_redirections) });
|
m_subcommands.append({ move(m_tokens), move(m_redirections), {} });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::do_pipe()
|
void Parser::do_pipe()
|
||||||
|
|
|
@ -10,7 +10,6 @@ struct Redirection {
|
||||||
FileWrite,
|
FileWrite,
|
||||||
FileWriteAppend,
|
FileWriteAppend,
|
||||||
FileRead,
|
FileRead,
|
||||||
Rewire
|
|
||||||
};
|
};
|
||||||
Type type;
|
Type type;
|
||||||
int fd { -1 };
|
int fd { -1 };
|
||||||
|
@ -18,9 +17,15 @@ struct Redirection {
|
||||||
String path {};
|
String path {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Rewiring {
|
||||||
|
int fd { -1 };
|
||||||
|
int rewire_fd { -1 };
|
||||||
|
};
|
||||||
|
|
||||||
struct Subcommand {
|
struct Subcommand {
|
||||||
Vector<String> args;
|
Vector<String> args;
|
||||||
Vector<Redirection> redirections;
|
Vector<Redirection> redirections;
|
||||||
|
Vector<Rewiring> rewirings;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Parser {
|
class Parser {
|
||||||
|
|
|
@ -315,9 +315,9 @@ static int run_command(const String& cmd)
|
||||||
perror("pipe");
|
perror("pipe");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
subcommand.redirections.append({ Redirection::Rewire, STDOUT_FILENO, pipefd[1] });
|
subcommand.rewirings.append({ STDOUT_FILENO, pipefd[1] });
|
||||||
auto& next_command = subcommands[i + 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[0]);
|
||||||
fds.add(pipefd[1]);
|
fds.add(pipefd[1]);
|
||||||
break;
|
break;
|
||||||
|
@ -328,7 +328,7 @@ static int run_command(const String& cmd)
|
||||||
perror("open");
|
perror("open");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
|
subcommand.rewirings.append({ redirection.fd, fd });
|
||||||
fds.add(fd);
|
fds.add(fd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -338,7 +338,7 @@ static int run_command(const String& cmd)
|
||||||
perror("open");
|
perror("open");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
|
subcommand.rewirings.append({ redirection.fd, fd });
|
||||||
fds.add(fd);
|
fds.add(fd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -348,12 +348,10 @@ static int run_command(const String& cmd)
|
||||||
perror("open");
|
perror("open");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
subcommand.redirections.append({ Redirection::Rewire, redirection.fd, fd });
|
subcommand.rewirings.append({ redirection.fd, fd });
|
||||||
fds.add(fd);
|
fds.add(fd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Redirection::Rewire:
|
|
||||||
break; // ignore
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -390,16 +388,14 @@ static int run_command(const String& cmd)
|
||||||
if (!child) {
|
if (!child) {
|
||||||
setpgid(0, 0);
|
setpgid(0, 0);
|
||||||
tcsetpgrp(0, getpid());
|
tcsetpgrp(0, getpid());
|
||||||
for (auto& redirection : subcommand.redirections) {
|
for (auto& rewiring : subcommand.rewirings) {
|
||||||
if (redirection.type == Redirection::Rewire) {
|
#ifdef SH_DEBUG
|
||||||
#ifdef SH_DEBUGsh
|
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(), redirection.rewire_fd, redirection.fd);
|
|
||||||
#endif
|
#endif
|
||||||
int rc = dup2(redirection.rewire_fd, redirection.fd);
|
int rc = dup2(rewiring.rewire_fd, rewiring.fd);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("dup2");
|
perror("dup2");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue