pipiinggnggggjgjgjgjgjjgjjgjjggjgj
This commit is contained in:
parent
f069874359
commit
e2514a1d9d
11
bish.cc
11
bish.cc
|
@ -59,9 +59,9 @@ int main(int argc, char **argv){
|
||||||
command *cmd = parse(split(it.c_str()));
|
command *cmd = parse(split(it.c_str()));
|
||||||
int num_cmds = cmd->cmds.size();
|
int num_cmds = cmd->cmds.size();
|
||||||
// debug info
|
// debug info
|
||||||
print_cmd(cmd);
|
// print_cmd(cmd);
|
||||||
|
|
||||||
// clear line var
|
// clear line var cause we already parsed it
|
||||||
free(line);
|
free(line);
|
||||||
line = (char*)NULL;
|
line = (char*)NULL;
|
||||||
|
|
||||||
|
@ -74,8 +74,8 @@ int main(int argc, char **argv){
|
||||||
|
|
||||||
int in = 0, fd[2];
|
int in = 0, fd[2];
|
||||||
for (int i = 0; i < num_cmds-1; i++) {
|
for (int i = 0; i < num_cmds-1; i++) {
|
||||||
if (!pipe(fd)) perror("pipe");
|
if (pipe(fd)) perror("pipe");
|
||||||
// dup_io(in, fd[1]);
|
cmd->cmds[i].ispipe = true;
|
||||||
cmd->cmds[i].infd = in;
|
cmd->cmds[i].infd = in;
|
||||||
cmd->cmds[i].outfd = fd[1];
|
cmd->cmds[i].outfd = fd[1];
|
||||||
expand_and_execute(&cmd->cmds[i]);
|
expand_and_execute(&cmd->cmds[i]);
|
||||||
|
@ -83,13 +83,14 @@ int main(int argc, char **argv){
|
||||||
in = fd[0];
|
in = fd[0];
|
||||||
}
|
}
|
||||||
if (in != 0) {
|
if (in != 0) {
|
||||||
// dup_io(in, 1);
|
cmd->cmds[num_cmds-1].ispipe = true;
|
||||||
cmd->cmds[num_cmds-1].infd = in;
|
cmd->cmds[num_cmds-1].infd = in;
|
||||||
cmd->cmds[num_cmds-1].outfd = 1;
|
cmd->cmds[num_cmds-1].outfd = 1;
|
||||||
expand_and_execute(&cmd->cmds[num_cmds-1]);
|
expand_and_execute(&cmd->cmds[num_cmds-1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
cmd->cmds[0].ispipe = false;
|
||||||
expand_and_execute(&cmd->cmds[0]);
|
expand_and_execute(&cmd->cmds[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
parse.h
1
parse.h
|
@ -14,6 +14,7 @@ struct simple_command {
|
||||||
string outfile;
|
string outfile;
|
||||||
int infd;
|
int infd;
|
||||||
int outfd;
|
int outfd;
|
||||||
|
bool ispipe;
|
||||||
};
|
};
|
||||||
// command struct
|
// command struct
|
||||||
struct command {
|
struct command {
|
||||||
|
|
98
util_fns.cc
98
util_fns.cc
|
@ -46,91 +46,49 @@ char** v_to_cpp(vector<string> vargs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void bishexec(simple_command* cmd, int infd, int outfd) {
|
|
||||||
|
|
||||||
// if (!remap_pipe_stdin_stdout(infd, outfd)) {
|
|
||||||
// perror("dup2");
|
|
||||||
// // return;
|
|
||||||
// }
|
|
||||||
if (outfd != 1) {
|
|
||||||
if (dup2(outfd, 1) == -1) {
|
|
||||||
perror("dup2 outfile");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (infd != 0) {
|
|
||||||
if (dup2(infd, 0) == -1) {
|
|
||||||
perror("dup2 infile");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to run it as is
|
|
||||||
execvp(cmd->vargs[0].c_str(), v_to_cpp(cmd->vargs));
|
|
||||||
|
|
||||||
// search the path
|
|
||||||
// vector<string> path = split(getenv("PATH"), ':');
|
|
||||||
// stringstream curr;
|
|
||||||
|
|
||||||
// for (auto iter: path) {
|
|
||||||
// curr.str("");
|
|
||||||
// curr << iter << "/" << cmd->vargs[0];
|
|
||||||
// execv(curr.str().c_str(), v_to_cpp(cmd->vargs));
|
|
||||||
// }
|
|
||||||
// nothing found here...
|
|
||||||
cout << "that's not a command, bish" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void check_cmd_io(simple_command *cmd) {
|
void check_cmd_io(simple_command *cmd) {
|
||||||
if (cmd->infile != "") {
|
if (cmd->ispipe) {
|
||||||
cmd->infd = open(cmd->infile.c_str(), O_RDONLY);
|
if (!remap_pipe_stdin_stdout(cmd->infd, cmd->outfd)) {
|
||||||
if (cmd->infd < 0) {
|
// perror("dup2-remap_pipe_stdin_stdout");
|
||||||
perror("infile");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else cmd->infd = 0;
|
|
||||||
|
|
||||||
if (cmd->outfile != "") {
|
|
||||||
cmd->outfd = open(cmd->outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
|
|
||||||
if (cmd->outfd < 0) {
|
|
||||||
perror("outfile");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else cmd->outfd = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void dup_io(int infd, int outfd, bool ispipe) {
|
|
||||||
cout << "in: " << infd << "out: " << outfd << endl;
|
|
||||||
if (ispipe) {
|
|
||||||
if (!remap_pipe_stdin_stdout(infd, outfd)) {
|
|
||||||
perror("dup2");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (outfd != 1) {
|
if (cmd->infile != "") {
|
||||||
if (dup2(outfd, 1) == -1) {
|
cmd->infd = open(cmd->infile.c_str(), O_RDONLY);
|
||||||
perror("dup2 outfile");
|
if (cmd->infd < 0) {
|
||||||
exit(0);
|
perror("infile");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
if (dup2(cmd->infd, 0) == -1) {
|
||||||
if (infd != 0) {
|
|
||||||
if (dup2(infd, 0) == -1) {
|
|
||||||
perror("dup2 infile");
|
perror("dup2 infile");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else cmd->infd = 0;
|
||||||
|
|
||||||
|
if (cmd->outfile != "") {
|
||||||
|
cmd->outfd = open(cmd->outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||||
|
if (cmd->outfd < 0) {
|
||||||
|
perror("outfile");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (dup2(cmd->outfd, 1) == -1) {
|
||||||
|
perror("dup2 outfile");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else cmd->outfd = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// http://www.gnu.org/software/libc/manual/html_node/Wordexp-Example.html
|
// http://www.gnu.org/software/libc/manual/html_node/Wordexp-Example.html
|
||||||
int expand_and_execute (simple_command *cmd) {
|
int expand_and_execute (simple_command *cmd) {
|
||||||
const char *program = cmd->vargs[0].c_str();
|
const char *program = cmd->vargs[0].c_str();
|
||||||
|
@ -179,7 +137,6 @@ int expand_and_execute (simple_command *cmd) {
|
||||||
/* This is the child process. Execute the command. */
|
/* This is the child process. Execute the command. */
|
||||||
// int in = 0, out = 1;
|
// int in = 0, out = 1;
|
||||||
check_cmd_io(cmd);
|
check_cmd_io(cmd);
|
||||||
dup_io(cmd->infd, cmd->outfd);
|
|
||||||
|
|
||||||
execvp (result.we_wordv[0], result.we_wordv);
|
execvp (result.we_wordv[0], result.we_wordv);
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
|
@ -203,7 +160,6 @@ int expand_and_execute (simple_command *cmd) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define DUP2CLOSE(oldfd, newfd) (dup2(oldfd, newfd) == 0 && close(oldfd) == 0)
|
#define DUP2CLOSE(oldfd, newfd) (dup2(oldfd, newfd) == 0 && close(oldfd) == 0)
|
||||||
// http://unixwiz.net/techtips/remap-pipe-fds.c.txt
|
// http://unixwiz.net/techtips/remap-pipe-fds.c.txt
|
||||||
// !!!!!!!
|
// !!!!!!!
|
||||||
|
|
|
@ -12,9 +12,8 @@ using namespace std;
|
||||||
void ctrlCHandler(int sig);
|
void ctrlCHandler(int sig);
|
||||||
vector<string> split(const char *str, char c = ' ');
|
vector<string> split(const char *str, char c = ' ');
|
||||||
char** v_to_cpp(vector<string> vargs);
|
char** v_to_cpp(vector<string> vargs);
|
||||||
void bishexec(simple_command* cmd, int infd, int outfd);
|
|
||||||
void check_cmd_io(simple_command *cmd, int *infd, int *outfd);
|
void check_cmd_io(simple_command *cmd);
|
||||||
void dup_io(int infd, int outfd, bool ispipe = false);
|
|
||||||
int expand_and_execute (simple_command *cmd);
|
int expand_and_execute (simple_command *cmd);
|
||||||
bool remap_pipe_stdin_stdout(int rpipe, int wpipe);
|
bool remap_pipe_stdin_stdout(int rpipe, int wpipe);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue