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()));
|
||||
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]);
|
||||
}
|
||||
|
||||
|
|
1
parse.h
1
parse.h
|
@ -14,6 +14,7 @@ struct simple_command {
|
|||
string outfile;
|
||||
int infd;
|
||||
int outfd;
|
||||
bool ispipe;
|
||||
};
|
||||
// command struct
|
||||
struct command {
|
||||
|
|
76
util_fns.cc
76
util_fns.cc
|
@ -46,50 +46,26 @@ 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) {
|
||||
if (cmd->ispipe) {
|
||||
if (!remap_pipe_stdin_stdout(cmd->infd, cmd->outfd)) {
|
||||
// perror("dup2-remap_pipe_stdin_stdout");
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
if (cmd->infile != "") {
|
||||
cmd->infd = open(cmd->infile.c_str(), O_RDONLY);
|
||||
if (cmd->infd < 0) {
|
||||
perror("infile");
|
||||
return;
|
||||
}
|
||||
if (dup2(cmd->infd, 0) == -1) {
|
||||
perror("dup2 infile");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
else cmd->infd = 0;
|
||||
|
||||
|
@ -99,38 +75,20 @@ void check_cmd_io(simple_command *cmd) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (outfd != 1) {
|
||||
if (dup2(outfd, 1) == -1) {
|
||||
if (dup2(cmd->outfd, 1) == -1) {
|
||||
perror("dup2 outfile");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
if (infd != 0) {
|
||||
if (dup2(infd, 0) == -1) {
|
||||
perror("dup2 infile");
|
||||
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
|
||||
// !!!!!!!
|
||||
|
|
|
@ -12,9 +12,8 @@ using namespace std;
|
|||
void ctrlCHandler(int sig);
|
||||
vector<string> split(const char *str, char c = ' ');
|
||||
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 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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue