Project 2: The Shell - Part 2


Starting from the command table produced in Part 1, in this part you will execute the commands, do the file redirection, piping and if necessary wait for the commands to end. Note that this description uses the old code base. See the README file in the C++ code to see how to translate to the C++ code base; the Command stuct will instead refer to the Command class.

1. Do the file redirection in the shell. If any of the input/output/error is different than 0 in the Command struct, then create the files, and use dup2() to redirect file descriptors 0, 1, or 2 to the new files. Make sure to save the old file descriptors to restore later! See the example cat_grep.cc to see how to do redirection, recalling that your shell will not call execvp, so it is slightly different than this example; your shell needs to be able to recover and execute another command. After this part you have to be able to execute commands like:

ls -al > out
cat out
ls /tttt >& err
cat err
cat < out
cat < out > out2
cat out2
ls /tt >>& out2 Recall:

  • ">& file" redirects both stdout and stderr to file.
  • ">>& file" append both stdout and stderr to file.
  • ">> file" appends stdout to file.
2. Loop over all commands and do the pipes. Use the call pipe() to create pipes that will interconnect the output of one simple command to the input of the next simple command. use dup2() to do the redirection. See the example cat_grep.cc to see how to construct pipes and do redirection. 3. For every command create a new process using fork() and call execvp() to execute the corresponding executable. If the -background flag in the Command struct is not set then your shell has to wait for the last simple command to finish using waitpid(). Check the manual pages of fork(), execvp(), and waitpid(). Also there is an example file that executes processes and does redirection in cat_grep.cc. After this part you have to be able to execute commands like:
ls -al | grep command
ls -al | grep command | grep command.o
ls -al | grep command
ls -al | grep command | grep command.o > out
cat out

4. Your shell has to ignore ctrl-c. When ctrl-c is typed, a signal SIGINT is generated that kills the program. There is an example program in ctrl-c.cc that tells you how to ignore SIGINT. Check the man pages for sigset.

5. You will also have to implement also an internal command called exit that will exit the shell when you type it. Remember that the exit command has to be executed by the shell itself without forking another process. myshell> exit
   Good bye!!

csh>

6. Implement the cd [ dir ] command. This command changes the current directory to dir. When dir is not specified, the current directory is changed to the home directory. Check "man 2 chdir".

7. When your shell uses a file as standard input your shell should not print a prompt. This is important because your shell will be graded by redirecting small scripts into your shell and comparing the output. Use the function isatty() to find out if the input comes from a file or from a terminal.

Submission Instructions

The deadline of this part of the project is February 15th, 2017, before class. Follow these instructions to submit.

1. Login to aji.cs.georgetown.edu.

2. cd to shell-src and type make clean

3. Type make to make sure that your shell is build correctly.

4. Type make clean again.

5. cd one directory above shell-src by typing "cd .."

6. Create a tar file named <user_name>.tar, where <user_name> is your aji.cs.georgetown.edu login, by typing

tar -cf <user_name>.tar shell-src

7. Gzip the tar file by typing

gzip <user_name>.tar

8. Since this timestamp will be used to verify whether the work was completed on time or not, you should set the permissions on the file you submitted to make sure that the file timestamp is not changed. So this by typing:

chmod a-w <user_name>.tar.gz

9. Upload the gzipped tar file to Canvas