next up previous
Next: About this document

Comp 242 Spring 2002

The Nutshell (Due Tue April 9)

In this project, you will write a basic shell, called the ``Nutshell''. It will execute programmer-defined commands and the following built-in commands:

setenv <var> <value>
printenv <var>
printall
cd <dir>
hm
pwd
exit
Both programmer-defined commands and built-in commands can be connected by pipes:
pwd | cat
You do not have to implement redirection of I/O to files.

The setenv command will change the value of an environment variable:

setenv DISPLAY jeeves.cs.unc.edu:0.0
The printenv command will display the value of an environment variable:
printenv DISPLAY
The printall command will print the values of all variables in the environment. You must substitute environment variables (starting with a $) with their values:
setenv PRINTER atlw146
lpq -P$PRINTER
In addition, you must support the semantics of the predefined PATH and HOME variables. The PATH variable defines the list of directories searched when looking for a program. The HOME variable defines the home directory - the hm command changes the working directory of the nutshell to the home directory specified by this variable.

The cd command changes the current working directory to the directory specified by the user. The pwd command prints the current working directory. The exit command quits the shell. This command is related but not the same as the exit system call. The interrupt signal (SIGINT) should not kill the Nutshell-- it should be interpreted as a signal for the child processes of the Nutshell rather than the Nutshell itself.

You do not have to do Unix tilde expansion, that is, you can assume no tildes appear in directory names. In general, you are not required to implement any more functionality than what has been specified above. I have kept the syntax simple and rigid to make the parsing task easy. You can assume the user will never make an error. Hopefully, these assumptions will allow you to focus on the semantics.

Following are some questions that you should answer:

What is the process tree created when processing the command?

cat /usr/dict/words | fgrep cat | head -2

In the example above, what happens if the ``fgrep'' or ``head'' process finishes before the other processes? It is upto you how early termination of a piped process is handled - simply describe here what happens on early termination. (Check the pipe man page to see what happens when a process tries to read/write a closed pipe descriptor.)

In the example above, what happens when the user sends the interrupt signal to the (top-level) shell?

What is the process tree created by the following command?

pwd | cat

Even though your nutshell does not support I/O redirection of the commands it invokes, the I/O of nutshell can be redirected by the invoking Unix shell. Assuming that the output of nutshell is redirected

nutshell > script
what is the standard output of your shell before, during, and after the execution of the piped command above?

Does your shell ever need to explicitly close pipe descriptors?

How does your implementation handle built-in commands in pipes that have side effects?

setenv PRINTER atlw146 | printenv PRINTER
It is upto you how you handle this issue - that is, it is up to you whether these commands affect state seen by subsequent commands. (How does the Unix shell you use handle side effects?)

You should look at the man pages of the following:

gets, strchr and strtok
open and close
dup and dup2
access
fork
execl (and other execs)
getwd
chdir (2V)
pipe
exit (2) and _exit
wait (2V)
environ (5V)
getenv
putenv
signal and kill
In general, to lookup the man page for an item, type the optional number and then the item name:
man 5V environ
Good Luck!



next up previous
Next: About this document



Prasun Dewan
Tue Mar 26 16:27:07 EST 2002