Assignment 7 - A Simple Command Shell: Part 1

Due: Thursday, November 9, at 10:00pm

Starter code: look through the samples we’ve talked about in class. See below for suggestions on how to start.

Upload solutions via Moodle as either:

  • shell208.tar
  • shell208.c

See below for more details on what you should submit.

Goals

This assignment is designed to help you with the following:

  • manipulating Linux processes in C
  • demystifying command shells like bash, zsh, etc.
  • understanding how command-line arguments are arranged in memory

Rubric

This assignment is worth a total of 13 points. They are allocated as follows:

1 - author name (and collaborators) in a comment at the top of any `.c` files
1 - support "help"
3 - simplest mode: prompt-read-execute loop
3 - support command-line arguments
3 - support redirection of stdout via >
2 - code quality

You are expected to follow the submission instructions below; if your shell208.c or shell208.tar file cannot be used without modification, you will not receive any of the “code quality” credit for this assignment.

Typing the help command should result in printing a short summary of your shell’s current features.

For the “simplest mode” of a shell, you should print a prompt, get a one-word command from the user, and execute that command (or print an error message); these steps repeat forever (or until your program is terminated).

You will earn 2/3 of the points for command-line arguments by supporting a single (optional) argument, e.g., ls -lah instead of just ls. Note that your arguments should play nicely with redirecting stdout via >.

Background

When you interact with a Unix (or other similar) command prompt, you are interacting with a program known as a command shell. This might be bash in a WSL or Linux terminal, bash in ther terminal in VS Code, zsh in a macOS Terminal, PowerShell in Windows, MS-DOS in a Windows cmd window, etc.

Although command shells are typically sophisticated programs with many features and they often implement their own full-fledged programming languages (e.g., “bash scripting”), their flow of control is, at heart, conceptually very simple:

  1. Print a prompt for the user (e.g., $ or %).
  2. Read the user’s command.
  3. Execute the command.
  4. Wait for the command to finish.
  5. Go to step 1.

Your assignment

For this assignment, you will write a simple command shell, named shell208, that performs the most common operations that shells perform. For Part 1 of this project (i.e., this week’s assignment), you will implement the styles of operation listed in the rubric above.

For Part 1, your shell should be able to handle:

  • single-word commands (e.g., ls, pwd, help),
  • commands with command-line arguments (e.g., ls -l, echo dog), and
  • redirection of standard output (stdout, via >) to a file.

For example, each of the following should work as they do in bash on mantis:

shell208$ help
shell208$ ls
shell208$ ls -l mysubdirectory
shell208$ ls -l > listing.txt

Submitting your work

You should submit your program via Moodle in one of two forms:

  • Option 1: a single C source file named shell208.c. This program should compile without warnings or errors on mantis using:

    gcc -Wall -Werror -g -Og -o shell208 shell208.c
    
  • Option 2: a shell208.tar file containing a folder named shell208, which in turns contains any files your shell needs. This .tar file must include a Makefile that enables us to compile and run your shell on mantis like so:

    tar xvf shell208.tar
    cd shell208
    make shell208
    ./shell208
    

You should submit your single file (shell208.c or shell208.tar) to Moodle.

Getting started

There is no explicit starter code for this assignment, but there are a collection of sample programs that demonstrate all of the essential techniques you will need to implement your shell. You will mostly be repurposing the sample programs and making sure the logic fits together.

Have fun!

This program is pretty cool once you get it working. Take it slow, and handle only one part of the rubric at a time. You can do this!