Program 1 - A Simple Shell

A Simple Shell
Prof. Darrell Long
Computer Science Department
Jack Baskin School of Engineering
University of California, Santa Cruz

Due: October 8, 23:59

Updates

9/26/02 - There is a new lex.l file that will now compile. I have also posted a Makefile to get you started. You need to run flex lex.l only once before you type make.

1 Purpose

The primary goal of this assignment is to warm up your programming skills that may have atrophied over the summer, and to gain some familiarity with the system call interface. A secondary goal is to use some of the programming tools provided in the UNIX environment. In this assignment you are to implement a UNIX shell program. A shell is simply a program that conveniently allows you to run other programs.

2 Basics

You are provided with the files lex.l and myshell.c that contain some code that calls getline(), a function provided by lex.l to read and parse a line of input. The getline() function returns an array of pointers to character strings. Each string is either a word containing the letters, numbers, period (.), and forward slash (/), or a character string containing one of the special characters: (, ), <, >, |, &, or ; (which have syntactical meaning to the shell). To compile lex.l, you have to use the flex command. This will produce a file called lex.yy.c. You must them compile and link lex.yy.c and myshell.c in order to get a running program. In the link step you also have to use -lfl to get everything to work properly. Use gcc for the compilation and linking.

3 Details

Your shell must support the following:

1. The internal shell command "exit" which terminates the shell.
Concepts: shell commands, exiting the shell
System calls: exit()

2. A command with no arguments.

Example: ls
Details: Your shell must block until the command completes and, if the return code is abnormal, print out a message to that effect. This holds for all command strings in this assignment.
Concepts: Forking a child process, waiting for it to complete, synchronous execution.
System calls: fork(), execvp(), exit(), wait()

3. A command with arguments.

Example: ls -l
Details: Argument zero is the name of the command other arguments follow in sequence.
Concepts: Command-line parameters.

4. A command, with or without arguments, whose output is redirected to a file.

Example: ls -l > file
Details: This takes the output of the command and put it in the named file.
Concepts: File operations, output redirection.
System calls: close() and dup()

5. A command, with or without arguments, whose input is redirected from a file.

Example: sort < scores
Details: This takes the named file as input to the command.
Concepts: Input redirection, file operations.
System calls: close() and dup()

6. A command, with or without arguments, whose output is piped to the input of another command.

Example: ls -l | more
Details: This takes the output of the first command and makes it the input to the second command.
Concepts: Pipes, synchronous operation
System calls: pipe(), close() and dup()

n.b. You must check and correctly handle all return values. This means that you need to read the manual pages for each function and system call to figure out what the possible return values are, what errors they indicate, and what you must do when you get that error.

4 Deliverables

A compressed tar file of your project directory, including your design document. You must do "make clean" before creating the tar file. In addition, include a README file to explain anything unusual to the teaching assistant. Your code and other associated files must be in a single directory so they will build properly in the submit directory.

Do not submit object files, assembler files, or executables. Every file in the submit directory that could be generated automatically by the compiler or assembler will result in a 5 point deduction from your programming assignment grade.

Your design document should be called DESIGN, and should reside in the project directory with the rest of your code. Your design should describe the design of your assignment in enough detail that a knowledgeable programmer could duplicate your work. This includes descriptions of the data structures you use, all non-trivial algorithms and formulas, and a description of each function including its purpose, inputs, outputs, and assumptions it makes about the inputs or outputs. A sample design document will be available on the course web page.


Although the idea of writing a shell as the first assignment in the operating systems course is an ancient one, dating at least to the venerable Prof. Jehan-Francois Paris, I have borrowed liberally from the assignment designed by Prof. Scott Brandt. Even so, it differs in key aspects which will be observed by the discerning student.
Narayan Brooks
Last modified: Thu Sep 26 11:52:23 PDT 2002