Assignment 9OS - Simple Command Shell

This assignment is one of two options. See Assignment 9 for more information.

Due: Wednesday, November 20, at 10:00pm

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

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
  • learning how redirection is implemented
  • learning how to use pipes for inter-process communication
  • learning how to handle signals

Collaboration policy

For this assignment, you may work alone or with a partner, but you must type up all of the answers yourself. (It is therefore unexpected for two submissions to be completely identical.)

You may also discuss the assignment at a high level with other students.

You should list any student with whom you discussed the assignment, and the manner of discussion (high level, partner, etc.) in comments at the top of your .c source file(s).

If you work alone, you should say so instead.

Assessment

To demonstrate proficiency, your submission needs to:

  • support the basic prompt-read-execute loop
  • support a single command-line argument
  • support redirection of stdout via >

To demonstrate mastery, your submission needs to:

  • be able to run without modifications
  • support multiple command-line arguments
  • support redirection of stdin via <
  • support a single pipe (|) or handle SIGINT signals

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.

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).

Your assignment

For this assignment, you will write a simple command shell, named shell208, that performs the most common operations that shells perform.

Proficiency

To demonstrate Proficiency, your shell should be able to handle:

  • single-word commands (e.g., ls, pwd),
  • commands with a single command-line argument (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$ pwd
shell208$ ls
shell208$ ls -l mysubdirectory
shell208$ ls -l > listing.txt

Mastery

To demonstrate mastery, your shell will additionally need to be able to handle:

  • multiple command-line arguments,
  • redirection of standard input (stdin, via <) from a file, and
  • piping commands via | or capturing SIGINT signals.

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

shell208$ ls
shell208$ ls -l mysubdirectory
shell208$ ls -l > listing.txt
shell208$ wc < myfile.txt
shell208$ ls -lah | wc
shell208$ ls -lah | grep .txt | wc -l          /* this isn't necessary, but it's a nice stretch */
shell208$ ^C                                   /* user pressed ctrl-c */
shell208$
shell208$ exit

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.

Take a look at Lab 6 for some guidance on what programs to read through. Ask questions if you have them!

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!