Project 2: The Shell - Part 1


This project was adapted from CS354 at Purdue University

Introduction

The goal of this project is to build a shell interpreter like csh. The project has been divided in several parts. Some sources are being provided so you don't need to start from scratch.

Using the Debugger

Before you start, it is important that you learn how to use a debugger to debug your C and C++ programs. If you spend a while learning how to use gdb, it will save you a lot of hours of development in this lab. For more complete tutorials on gdb see:
GDB Tutorial 1
GDB Tutorial 2
GDB Tutorial 3

To start gdb type:

gdb <program>

For example, to debug your shell type:

csh> gdb shell

Then type

(gdb) break main

This will make the debugger stop your program before main is called. In general, to set a breakpoint in a given function type break <function-name>

To start running your program type:

(gdb)run

Your program will start running and then will stop at main.

Use "step"or "next" to execute your program a bit at a time. "step" will execute the following line and if it is a function, it will step into it. "next" will execute the following line and if it a function it will execute the function. Other useful commands are:

print  
- Prints a variable
where 
- Prints the stack trace
quit 
- Exits gdb

First part: Flex and Bison

In this part you will build the scanner and parser for your shell using flex and bison. Information about flex and bison can be found here and you can access the entire book through the library. You can also find information on Google.
  • Copy the tar file shell-src.tar.gz, that contains all the files in shell-src, to your home directory on aji.cs.georgetown.edu from my home directory using the following command:

     cp ~clay/shell-src.tar.gz ~/

    and untar it using the following command:

     gunzip shell-src.tar.gz
     tar -xvf shell-src.tar
    This will cause a directory named
    shell-src
    to appear in your home directory. Use cd to change to that directory.
  • Build the shell program by typing :
    make
    To run it type:
     shell
    Then type commands like
     ls -al ls -al aaa bbb > out
    Check the output printed to see what was recorded.
  • Read and understand the code (particularly shell.l and shell.y) to understand how the program works. Read the shell-src/Makefile to learn how the program is built. The file shell-src/command.h implements the data structure that represents a shell command. The struct SimpleCommand implements the list of arguments of a simple command. Usually a shell command can be represented by only one SimpleCommand. However, when pipes are used, a command will consist of more than one SimpleCommand. The struct Command represents a list of SimpleCommand structs. Other fields that the Command struct has are _outFile, _inputFile, and _errFile that represent input, output, and error redirection.
  • Currently the shell program implements a very simple grammar:
     cmd [arg]* [> filename]
    You will have to modify shell-src/shell.y to implement a more complex grammar
     cmd [arg]* [ | cmd [arg]* ]* 
    [< filename] 
    [ [> filename] [ >& filename] [>> filename] [>>& filename] ] 
    [&]
  • Insert the necessary actions in shell-src/shell.y to fill in the Command struct. Make sure that the Command struct is printed correctly.
  • Run your program against the following commands:
     ls
     ls -al
     ls -al aaa bbb cc
     ls -al aaa bbb cc > outfile
     ls | cat | grep
     ls | cat | grep > out < inp
     ls aaaa | grep cccc | grep jjjj ssss dfdffdf
     ls aaaa | grep cccc | grep jjjj ssss dfdffdf >& out < in
     httpd &
     ls aaaa | grep cccc | grep jjjj ssss dfdffdf >>& out < in

The deadline of this part of the project is February 9th. before class. Follow these instructions to turnin your part one.

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. Finally, upload the file to Canvas. We will not grade this one, but we need to do this so I have a record in case the machine crashes. This will also allow you to make a backup to your own system in accordance with the class policies.