diff --git a/bish.cc b/bish.cc index 22728b3..de1b6c4 100644 --- a/bish.cc +++ b/bish.cc @@ -59,9 +59,9 @@ int main(int argc, char **argv){ command *cmd = parse(split(it.c_str())); int num_cmds = cmd->cmds.size(); // debug info - print_cmd(cmd); + // print_cmd(cmd); - // clear line var + // clear line var cause we already parsed it free(line); line = (char*)NULL; @@ -74,8 +74,8 @@ int main(int argc, char **argv){ int in = 0, fd[2]; for (int i = 0; i < num_cmds-1; i++) { - if (!pipe(fd)) perror("pipe"); - // dup_io(in, fd[1]); + if (pipe(fd)) perror("pipe"); + cmd->cmds[i].ispipe = true; cmd->cmds[i].infd = in; cmd->cmds[i].outfd = fd[1]; expand_and_execute(&cmd->cmds[i]); @@ -83,13 +83,14 @@ int main(int argc, char **argv){ in = fd[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].outfd = 1; expand_and_execute(&cmd->cmds[num_cmds-1]); } } else { + cmd->cmds[0].ispipe = false; expand_and_execute(&cmd->cmds[0]); } diff --git a/parse.h b/parse.h index a2d0290..6834600 100644 --- a/parse.h +++ b/parse.h @@ -14,6 +14,7 @@ struct simple_command { string outfile; int infd; int outfd; + bool ispipe; }; // command struct struct command { diff --git a/util_fns.cc b/util_fns.cc index a2fdb47..acebdb0 100644 --- a/util_fns.cc +++ b/util_fns.cc @@ -46,91 +46,49 @@ char** v_to_cpp(vector 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 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) { - if (cmd->infile != "") { - cmd->infd = open(cmd->infile.c_str(), O_RDONLY); - if (cmd->infd < 0) { - 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; + if (cmd->ispipe) { + if (!remap_pipe_stdin_stdout(cmd->infd, cmd->outfd)) { + // perror("dup2-remap_pipe_stdin_stdout"); } + return; } else { - if (outfd != 1) { - if (dup2(outfd, 1) == -1) { - perror("dup2 outfile"); - exit(0); + if (cmd->infile != "") { + cmd->infd = open(cmd->infile.c_str(), O_RDONLY); + if (cmd->infd < 0) { + perror("infile"); + return; } - } - if (infd != 0) { - if (dup2(infd, 0) == -1) { + if (dup2(cmd->infd, 0) == -1) { perror("dup2 infile"); 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 int expand_and_execute (simple_command *cmd) { 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. */ // int in = 0, out = 1; check_cmd_io(cmd); - dup_io(cmd->infd, cmd->outfd); execvp (result.we_wordv[0], result.we_wordv); 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) // http://unixwiz.net/techtips/remap-pipe-fds.c.txt // !!!!!!! diff --git a/util_fns.h b/util_fns.h index f2f4048..104f92b 100644 --- a/util_fns.h +++ b/util_fns.h @@ -12,9 +12,8 @@ using namespace std; void ctrlCHandler(int sig); vector split(const char *str, char c = ' '); char** v_to_cpp(vector vargs); -void bishexec(simple_command* cmd, int infd, int outfd); -void check_cmd_io(simple_command *cmd, int *infd, int *outfd); -void dup_io(int infd, int outfd, bool ispipe = false); + +void check_cmd_io(simple_command *cmd); int expand_and_execute (simple_command *cmd); bool remap_pipe_stdin_stdout(int rpipe, int wpipe);