Assignment 7: Implementing ISH

Please note that the requirement has changed, you are now required to interpret two environment variables: PATH and HOME

In this problem set, you will implement the shell ish, a command interpreter with a syntax and behavior similar to (but much simpler than) csh.

This problem set is divided into two parts (see Deadlines below). Here is the specification of the entire problem set; features marked with asterisks are to be implemented in the second part of the assignment.

Initialization and Termination

When first started, ish normally performs commands from the file ~/.ishrc, provided that it is readable. Typically, the ~/.ishrc file contains commands to specify the terminal type and environment. (Here is an example .ishrc file on which your shell will be testsed when you complete part I.)

Interactive Operation

After startup processing, an interactive ish shell begins reading commands from the terminal, prompting with ish>. The shell then repeatedly performs the following actions: a line of command input is read and broken into words; this sequence of words is parsed (as described under USAGE); and the shell executes the commands in the current line.
 

USAGE

Lexical Structure

The shell splits input lines into words separated by spaces or tabs, with the following exceptions:

Command Line Parsing

A simple command is a sequence of words, the first of which specifies the command to be executed. A pipeline is a sequence of one or more simple commands separated by '|' ;. With '|', the standard output of the preceding command is redirected to the standard input of the command that follows.

You may assume that each simple command is of the form
        'command arguments redirection'
and that the syntax
        'command redirection arguments'
is illegal.

In part 2 of this assignment, you will also handle '&' on the command line (see Background processes and job control).

I/O Redirection

The following metacharacters indicate that the subsequent word is the name of a file to which the command's standard input or standard output is redirected.

Command Execution

If the command is an ish shell built-in, the shell executes it directly. Otherwise, the shell searches for a file by that name with execute access. If the command-name contains a '/', the shell takes it as a pathname, and searches for it. If the command-name does not contain a '/', the shell attempts to resolve it to a pathname, searching each directory in the PATH variable for the command.

When a file is found that has proper execute permissions, the shell forks a new process and passes it, along with its arguments, to the OS using the execv or execl system call. (Do not use the execvp or execlp calls.) The OS then attempts to overlay the new process with the desired program. If the file is an executable binary the OS succeeds, and begins executing the new process. If the file does not have execute permissions, or if the pathname matches a directory, a "permission denied" message is displayed. If the pathname cannot be resolved a "command not found" message is displayed. If either of these errors occur with any component of a pipeline the entire pipeline is aborted, although some of the components of the pipeline may have already started running.

A pipeline is completed (i.e., returns to the prompt) only when all the commands that form a part of the pipeline and that are being executed in the foreground are completed.

Environment Variables

Environment variables may be accessed via the setenv and unsetenv built-in commands. Initially no environment variables are set, i.e. ish does not inherit environment variables from its parent. ish maintains environment variables internally, and may not use the C library routines putenv or getenv. When a program is exec'ed the environment variables are passed as parameters to execve. The only two environment variables that ish needs to interpret are PATH and HOME; all other environment variables can be set and unset in ish using the above built-in commands, but are not interpreted. 

* Background Processes and Job Control

When a command is started in the background using the '&' metacharacter, the shell displays a line with the job number in brackets, and a list of associated process numbers; e.g.,

        [1] 1234

Note that the special character & at the command line causes asynchronous execution of the preceding pipeline. The shell does not wait for the pipeline to finish; instead, it displays the job number and associated process IDs. The shell associates a numbered job with each command sequence, to keep track of those commands that are running in the background or have been stopped with TSTP signals (typically CTRL-Z). Jobs are put into the foreground using the tcsetpgrp system call.

To see the current list of jobs, use the jobs built-in command. The job most recently stopped (or put into the background if none are stopped) is referred to as the current job.

To manipulate jobs, refer to the built-in commands bg, fg, and kill. A reference to a job begins with a '%'. Refer to job number j as in: 'kill %j'. A job running in the background stops when it attempts to read from the terminal.

* Status Reporting

While running interactively, the shell tracks the status of each job and reports whenever it finishes or becomes blocked. Status only need be reported prior to printing the prompt; this means that job status may be polled via wait. Do not use signal handlers to track job status.

* Signal Handling

The shell normally ignores QUIT signals. Background jobs are immune to signals generated from the keyboard, including hangups (HUP). Other signals have the values that ish inherited from its environment.

Built-In Commands

Built-in commands are executed within ish. If a built-in command occurs as any component of a pipeline except the last, it is executed in a subshell.
 
* bg %job ...  Run the specified job in the background.
cd [ dir ] Change the shell's working directory to directory dir.If no argument is given, change to the home directory of the user (which is given by value of HOME).
exit Exit ish.
* fg %job Bring the specified job into the foreground.
* jobs List the active jobs under job control.
* kill %job ...  Send the TERM (terminate) signal to the job indicated. To insure termination, the job is also sent a CONT (continue) signal.
printenv Print the environment variables.
setenv [ VAR [ word ] ] 
 

 

With no arguments, setenv displays all environment variables. With the VAR argument, it sets the environment variable VAR to have an empty (null) value. (By convention, environment variables are normally given upper-case names.) With both VAR and word arguments, setenv sets the named environment variable to the value word, which must be either a single word or a quoted string.
unsetenv VAR Remove VAR from the environment.

 

FILES

~/.ishrc     Read at beginning of execution by each shell.
 

SUGGESTIONS

You may follow these suggested steps if you want.

LIMITATIONS

Words can be no longer than 1024 characters.
 

DEADLINES

Part 1 :

Part 2:

GRADING POLICY

This project is worth two assignments and you will receive a final grade based on the grades on the two parts. Part 1 is most likely to be weighted more than Part 2. You will not be provided with a working Part 1, so getting it to work reasonably is very important.

If you miss points on Part 1 you will have the opportunity to resubmit by the final due date and receive 70% credit for the problems fixed (i.e., you will have an opportunity to make up 70% of the points missed on Part 1). Late penalty points cannot be made up, but we'll take the larger grade of the one determined as above and 70% of your second submission.