This commit is contained in:
Ben Harris 2016-11-16 12:52:14 -05:00
parent 2aa7167cd3
commit f069874359
5 changed files with 38 additions and 40 deletions

View File

@ -31,13 +31,13 @@ The Shell Assignment (total 42 points)
+ `cat < filename | sort > sortedFile.txt`
+ 2 Can set enviornment variables
+ `PATH=:/bin:/sbin:/usr/sbin:/usr/bin`
+ 2 Expands enviornment variables on the command line
+ 2 ~~Expands enviornment variables on the command line~~
+ `ls $HOME`
+ 2 Does filename expansion "glob" (Hint: Use the built in glob.)
+ 2 ~~Does filename expansion "glob" (Hint: Use the built in glob.)~~
+ `ls a*b`
+ 1 ~~Knows how to change directory~~
+ `cd /fred`
+ 1 ~~Bang last command~~
+ 1 Bang last command
+ `!l` runs ls
+ 1 Bang # command
+ `!4` runs 4th command from history
@ -54,7 +54,7 @@ The Shell Assignment (total 42 points)
+ `cd /home/rappleto & rm fred.txt`
+ 1 ~~Catch Keyboard interrupt~~
+ `ctrl + c` = back to prompt
+ 1 Replace "~" with the home directory
+ 1 ~~Replace "~" with the home directory~~
+ `rm ~/junkfile`
+ 1 ~~Control-L clears the screen~~
+ `ctrl-l` = clear screen

33
bish.cc
View File

@ -75,32 +75,23 @@ 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]);
bish_expandexec(&cmd->cmds[i]);
// close(fd[1]);
// dup_io(in, fd[1]);
cmd->cmds[i].infd = in;
cmd->cmds[i].outfd = fd[1];
expand_and_execute(&cmd->cmds[i]);
close(fd[1]);
in = fd[0];
}
if (in != 0) {
dup_io(in, 1);
bish_expandexec(&cmd->cmds[num_cmds-1]);
// dup_io(in, 1);
cmd->cmds[num_cmds-1].infd = in;
cmd->cmds[num_cmds-1].outfd = 1;
expand_and_execute(&cmd->cmds[num_cmds-1]);
}
}
else bish_expandexec(&cmd->cmds[0]);
// if (curr.outfile != "") {
// outfd = open(curr.outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
// if (outfd < 0) {
// perror("outfile");
// exit(0);
// }
// }
// if (curr.infile != "") {
// infd = open(curr.infile.c_str(), O_RDONLY);
// if (infd < 0) {
// perror("infile");
// exit(0);
// }
// }
else {
expand_and_execute(&cmd->cmds[0]);
}
// COMMANDS that do something with the line before fork/exec

View File

@ -12,6 +12,8 @@ struct simple_command {
vector<string> vargs;
string infile;
string outfile;
int infd;
int outfd;
};
// command struct
struct command {

View File

@ -83,21 +83,24 @@ 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) {
if (cmd->infile != "") {
*infd = open(cmd->infile.c_str(), O_RDONLY);
if (infd < 0) {
cmd->infd = open(cmd->infile.c_str(), O_RDONLY);
if (cmd->infd < 0) {
perror("infile");
exit(0);
return;
}
}
else cmd->infd = 0;
if (cmd->outfile != "") {
*outfd = open(cmd->outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
if (outfd < 0) {
cmd->outfd = open(cmd->outfile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
if (cmd->outfd < 0) {
perror("outfile");
exit(0);
return;
}
}
else cmd->outfd = 1;
}
@ -128,12 +131,10 @@ 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));
}
// http://www.gnu.org/software/libc/manual/html_node/Wordexp-Example.html
int expand_and_execute (const char *program, char **options) {
int expand_and_execute (simple_command *cmd) {
const char *program = cmd->vargs[0].c_str();
char **options = v_to_cpp(cmd->vargs);
wordexp_t result;
pid_t pid;
int status, i;
@ -170,12 +171,16 @@ int expand_and_execute (const char *program, char **options) {
} else {
if (chdir(homedir) < 0) perror("chdir");
}
return 0;
}
return 0;
pid = fork();
if (pid == 0) {
/* 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);
}

View File

@ -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);
void check_cmd_io(simple_command *cmd, int *infd, int *outfd);
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);
int expand_and_execute (simple_command *cmd);
bool remap_pipe_stdin_stdout(int rpipe, int wpipe);
#endif