exapnd werkz
This commit is contained in:
parent
5bb0428c15
commit
2aa7167cd3
93
bish.cc
93
bish.cc
|
@ -69,58 +69,24 @@ int main(int argc, char **argv){
|
|||
done = 1;
|
||||
break;
|
||||
}
|
||||
if (cmd->cmds[0].vargs[0] == "cd") {
|
||||
if (cmd->cmds[0].vargs[1] == "") {
|
||||
if (chdir(homedir) < 0) perror("chdir");
|
||||
} else {
|
||||
if (chdir(cmd->cmds[0].vargs[1].c_str()) < 0) perror("chdir");
|
||||
|
||||
if (num_cmds > 1) {
|
||||
|
||||
int in = 0, fd[2];
|
||||
for (int i = 0; i < num_cmds-1; i++) {
|
||||
if (!pipe(fd)) perror("pipe");
|
||||
dup_io(in, fd[1]);
|
||||
bish_expandexec(&cmd->cmds[i]);
|
||||
// close(fd[1]);
|
||||
in = fd[0];
|
||||
}
|
||||
if (in != 0) {
|
||||
dup_io(in, 1);
|
||||
bish_expandexec(&cmd->cmds[num_cmds-1]);
|
||||
}
|
||||
}
|
||||
else bish_expandexec(&cmd->cmds[0]);
|
||||
|
||||
// int n;
|
||||
// bish_expandexec(&cmd->cmds[0]);
|
||||
int in = 0, fd[2];
|
||||
for (int i = 0; i < num_cmds-1; i++) {
|
||||
pipe(fd);
|
||||
dup_io(in, fd[1]);
|
||||
bish_expandexec(&cmd->cmds[i]);
|
||||
// close(fd[1]);
|
||||
in = fd[0];
|
||||
}
|
||||
if (in != 0) {
|
||||
dup_io(in, 1);
|
||||
bish_expandexec(&cmd->cmds[num_cmds]);
|
||||
}
|
||||
|
||||
// // else { // do fork/exec
|
||||
// pid_t kidpid = fork();
|
||||
// // fork error
|
||||
// if (kidpid < 0) {
|
||||
// perror("fork");
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
// // run it
|
||||
// // also check the path for things
|
||||
// else if (kidpid == 0){
|
||||
// int infd = 0, outfd = 1;
|
||||
|
||||
// if (num_cmds > 1) {
|
||||
// int pipefd[2];
|
||||
// pipe(pipefd);
|
||||
// pid_t nextkidpid = fork();
|
||||
// if (nextkidpid < 0) {
|
||||
// perror("fork");
|
||||
// return -1;
|
||||
// }
|
||||
// else if (nextkidpid == 0) {
|
||||
// close(pipefd[0]);
|
||||
// outfd = pipefd[1];
|
||||
// bishexec(&curr, infd, outfd);
|
||||
// }
|
||||
// }
|
||||
|
||||
// // io redirection
|
||||
// if (curr.outfile != "") {
|
||||
// outfd = open(curr.outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
// if (outfd < 0) {
|
||||
|
@ -136,35 +102,6 @@ int main(int argc, char **argv){
|
|||
// }
|
||||
// }
|
||||
|
||||
// bishexec(&curr, infd, outfd);
|
||||
// exit(1);
|
||||
|
||||
// } // end child
|
||||
// // parent waits for kid to die
|
||||
// else {
|
||||
|
||||
// int status;
|
||||
|
||||
// if (!cmd->background){
|
||||
// do {
|
||||
// if (waitpid(kidpid, &status, WUNTRACED | WCONTINUED) == -1) {
|
||||
// perror("waitpid");
|
||||
// exit(1);
|
||||
// }
|
||||
// if (WIFEXITED(status)) {
|
||||
// cout << "(" << WEXITSTATUS(status) << "):";
|
||||
// } else if (WIFSIGNALED(status)) {
|
||||
// cout << endl << "killed by signal " << WTERMSIG(status) << endl;
|
||||
// } else if (WIFSTOPPED(status)) {
|
||||
// cout << endl << "stopped by signal " << WSTOPSIG(status) << endl;
|
||||
// } else if (WIFCONTINUED(status)) {
|
||||
// cout << endl << "continued" << endl;
|
||||
// }
|
||||
// } while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
// }
|
||||
|
||||
// } // end parent
|
||||
|
||||
|
||||
// COMMANDS that do something with the line before fork/exec
|
||||
// if (strcmp(cmd->cmds[0]->args[0], "!") == 0) {
|
||||
|
|
22
bish.l
22
bish.l
|
@ -1,22 +0,0 @@
|
|||
%{
|
||||
#include <string.h>
|
||||
#include "y.tab.h"
|
||||
%}
|
||||
|
||||
%%
|
||||
|
||||
\n { return NEWLINE;}
|
||||
|
||||
[ \t] {}
|
||||
|
||||
">" { return GT; }
|
||||
"<" { return LT; }
|
||||
">>" { return GTGT; }
|
||||
"|" { return PIPE; }
|
||||
"&" { return AMPERSAND; }
|
||||
[^ \t\n][^ \t\n]* {
|
||||
yylval.string_val = strdup(yytext);
|
||||
return WORD;
|
||||
}
|
||||
|
||||
%%
|
6
parse.cc
6
parse.cc
|
@ -14,9 +14,9 @@ void print_cmd(command *cmd) {
|
|||
cout << "-----------------------------" << endl;
|
||||
for (auto cmd_iter: cmd->cmds) {
|
||||
if (cmd->background) cout << "backgroud: true" << endl;
|
||||
cout << "command " << i++ << endl;
|
||||
cout << "infile: " << cmd_iter.infile << endl;
|
||||
cout << "outfile: " << cmd_iter.outfile << endl << endl;
|
||||
cout << "command " << ++i << endl;
|
||||
if (cmd_iter.infile != "") cout << "infile: " << cmd_iter.infile << endl;
|
||||
if (cmd_iter.outfile != "") cout << "outfile: " << cmd_iter.outfile << endl << endl;
|
||||
cout << ">>\t";
|
||||
for (auto scmd_iter: cmd_iter.vargs) {
|
||||
cout << "\"" << scmd_iter << "\" ";
|
||||
|
|
46
util_fns.cc
46
util_fns.cc
|
@ -10,6 +10,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <sstream>
|
||||
#include <wordexp.h>
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/wait.h>
|
||||
#include "parse.h"
|
||||
#include "util_fns.h"
|
||||
|
@ -79,6 +81,27 @@ void bishexec(simple_command* cmd, int infd, int outfd) {
|
|||
cout << "that's not a command, bish" << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void check_cmd_io(simple_command *cmd, int *infd, int *outfd) {
|
||||
if (cmd->infile != "") {
|
||||
*infd = open(cmd->infile.c_str(), O_RDONLY);
|
||||
if (infd < 0) {
|
||||
perror("infile");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
if (cmd->outfile != "") {
|
||||
*outfd = open(cmd->outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
if (outfd < 0) {
|
||||
perror("outfile");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void dup_io(int infd, int outfd, bool ispipe) {
|
||||
cout << "in: " << infd << "out: " << outfd << endl;
|
||||
if (ispipe) {
|
||||
|
@ -103,6 +126,8 @@ void dup_io(int infd, int outfd, bool ispipe) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int bish_expandexec(simple_command* cmd) {
|
||||
return expand_and_execute(cmd->vargs[0].c_str(), v_to_cpp(cmd->vargs));
|
||||
}
|
||||
|
@ -113,6 +138,7 @@ int expand_and_execute (const char *program, char **options) {
|
|||
pid_t pid;
|
||||
int status, i;
|
||||
|
||||
|
||||
/* Expand the string for the program to run. */
|
||||
switch (wordexp (program, &result, 0)) {
|
||||
case 0: /* Successful. */
|
||||
|
@ -126,13 +152,27 @@ int expand_and_execute (const char *program, char **options) {
|
|||
}
|
||||
|
||||
/* Expand the strings specified for the arguments. */
|
||||
for (i = 0; options[i] != NULL; i++) {
|
||||
for (i = 1; options[i] != NULL; i++) {
|
||||
if (wordexp (options[i], &result, WRDE_APPEND)) {
|
||||
wordfree (&result);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
const char *homedir;
|
||||
if ((homedir = getenv("HOME")) == NULL) {
|
||||
homedir = getpwuid(getuid())->pw_dir;
|
||||
}
|
||||
|
||||
if (strcmp(program, "cd") == 0) {
|
||||
if (options[1]) {
|
||||
if (chdir(result.we_wordv[1]) < 0) perror("chdir");
|
||||
} else {
|
||||
if (chdir(homedir) < 0) perror("chdir");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) {
|
||||
/* This is the child process. Execute the command. */
|
||||
|
@ -155,6 +195,10 @@ int expand_and_execute (const char *program, char **options) {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define DUP2CLOSE(oldfd, newfd) (dup2(oldfd, newfd) == 0 && close(oldfd) == 0)
|
||||
// http://unixwiz.net/techtips/remap-pipe-fds.c.txt
|
||||
// !!!!!!!
|
||||
|
|
|
@ -13,9 +13,9 @@ 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);
|
||||
bool remap_pipe_stdin_stdout(int rpipe, int wpipe);
|
||||
int expand_and_execute (const char *program, char **options);
|
||||
int bish_expandexec(simple_command* cmd);
|
||||
void dup_io(int infd, int outfd, bool ispipe = false);
|
||||
int bish_expandexec(simple_command* cmd);
|
||||
int expand_and_execute (const char *program, char **options);
|
||||
bool remap_pipe_stdin_stdout(int rpipe, int wpipe);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue