CS 326 Operating Systems

Project 2: Command Line Shell (v 1.0)

Starter repository on GitHub: https://classroom.github.com/a/Ot4_dFEK

The outermost layer of the operating system kernel is called the shell. In Unix-based systems, the shell is generally a command line interface. Most Linux distributions ship with bash as the default (there are several others: csh, ksh, sh, tcsh, zsh). In this project, we’ll be implementing a shell of our own – see, I told you that you’d come to love command line interfaces in this class!

You will need to come up with a name for your shell first. The only requirement is that the name ends in ‘sh’, which is tradition in the computing world. In the following examples, my shell is named crash (Cool Really Awesome Shell) because of its propensity to crash.

The Basics

Upon startup, your shell will print its prompt and wait for user input:

--[0|1:37pm|mmalensek@mmalensek-pi:~]--$

Your shell should be able to run commands in both the current directory and those in the PATH environment variable. To run a command in the current directory, you’ll need to prefix it with ./ as usual. If a command isn’t found, print an error message:

--[0|1:37pm|mmalensek@mmalensek-pi:~]--$ ./hello
Hello world!
--[1|1:37pm|mmalensek@mmalensek-pi:~]--$ ls /usr
bin  include  lib  local  sbin  share  src
--[2|1:37pm|mmalensek@mmalensek-pi:~]--$ echo hello there!
hello there!
--[3|1:38pm|mmalensek@mmalensek-pi:~]--$ ./blah
crash: no such file or directory: ./blah
--[4|1:38pm|mmalensek@mmalensek-pi:~]--$ 

Prompt

Our shell prompt displays some helpful information. From left to right, it includes:

In my example, these are separated by dashes and pipe characters (|) to make it a little easier to read. For this assignment, you are allowed to invent your own prompt format as long as it has the elements listed above. You can use colors, unicode characters, etc. if you’d like.

You will format the current working directory in the same way most shells do: if the CWD is the user’s home directory, then the entire path is replaced with ~. Subdirectories under the home directory are prefixed with ~; if I am in /home/mmalensek/test, the prompt will show ~/test. If you’re unsure how this works, bash behaves in the same way.

Here’s a test to make sure you’ve handled ~ correctly:

--[0|1:39pm|mmalensek@mmalensek-pi:~/shell]--$ whoami
mmalensek
# Create a directory with our full home directory in its path:
# **Must use the username outputted above from whoami)**
--[1|1:39pm|mmalensek@mmalensek-pi:~/shell]--$ mkdir -p /tmp/home/mmalensek/test
--[2|1:39pm|mmalensek@mmalensek-pi:~/shell]--$ cd /tmp/home/mmalensek/test
# Note that the FULL path is shown here (no ~):
--[3|1:40pm|mmalensek@mmalensek-pi:/tmp/home/mmalensek/test]--$ pwd
/tmp/home/mmalensek/test

Shell Features

Your shell must support the following functionality.

Built-In Commands

Most shells have built-in commands, including cd and time. Your shell must support:

Signal Handling

Your shell should gracefully handle the user pressing Ctrl+C:

--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ hi there oh wait nevermind^C
--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ ^C
--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ ^C
--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ ^C
--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ ^C
--[1|1:54pm|mmalensek@mmalensek-pi:~]--$ sleep 100
^C
--[2|1:54pm|mmalensek@mmalensek-pi:~]--$ sleep 100

The most important aspect of this is making sure ^C doesn’t terminate your shell. To make the output look like the example above, in your signal handler you can (1) print a newline character, (2) print the prompt only if no command is currently executing, and (3) fflush(stdout).

Timing

You will track how long each process runs and make this information available in the history. Here’s a demonstration of the history command:

--[142|7:42am|mmalensek@mmalensek-pi:~]--$ history
[42|6:45am|0.32] ls -l
[43|6:45am|6.10] top
[44|6:49am|0.11] echo "hi" # This prints out 'hi'

...

[140|7:35am|0.01] ls /bin
[141|7:38am|11.52] gcc -g crash.c

In this demo, the user has entered 141 commands. Only the last 100 are kept, so the list starts at command 42. The format is: [command-number|start-time|run-time-in-seconds] command-line. If the user enters a blank command, it should not be shown in the history.

Redirection

Your shell must support file output redirection and pipe redirection:

# Create/overwrite 'my_file.txt' and redirect the output of echo there:
--[3|9:38am|mmalensek@mmalensek-pi:~/dir]--$ echo "hello world!" > my_file.txt

# Pipe redirection:
--[4|9:38am|mmalensek@mmalensek-pi:~/dir]--$ cat other_file.txt | sort
--[5|9:38am|mmalensek@mmalensek-pi:~/dir]--$ seq 100000 | wc -l
10000
--[6|9:38am|mmalensek@mmalensek-pi:~/dir]--$ cat /etc/passwd | sort > sorted_pwd.txt

Background Jobs

If a command ends in &, then it should run in the background. In other words, don’t wait for the command to finish before prompting for the next command. If you enter jobs, your shell should print out a list of currently-running backgrounded processes.

Scripting

While your shell doesn’t need to explicitly support scripting, it should be able to handle commands piped in from stdin:

cat <<EOM | ./crash
ls /
echo "hi"
exit
EOM

# Which outputs (note how the prompt is not displayed):
bin  boot  dev  etc  home  lib  lost+found  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
hi

# Another option (assuming commands.txt contains shell commands):
./crash < commands.txt
(commands are executed line by line)

You should check and make sure you can run a large script with your shell. Note that the script should not have to end in exit.

Testing Your Code

You should make sure your code runs on the Raspberry Pi. We’ll have interactive grading for the project where you will demonstrate program functionality and walk through your logic.

Recommended: start with basic command execution, then add support for built-ins and history. Wrap up with redirection and pipes.

Submission: submit via GitHub by checking in your code before the project deadline. You must include a makefile with your project. As part of the testing process, we will check out your code and run make to build it.

Hints

Here’s some hints to guide your implementation:

To support scripting mode, you will need to determine whether stdin is connected to a terminal or not. Here’s some sample code that does this with isatty:

#include <stdio.h>
#include <unistd.h>

int main(void) {

    if (isatty(STDIN_FILENO)) {
        printf("stdin is a TTY; entering interactive mode\n");
    } else {
        printf("data piped in on stdin; entering script mode\n");
    }

    return 0;
}

Grading

Restrictions: you may use any standard C library functionality. External libraries are not allowed unless permission is granted in advance. In particular, you may not use the readline library for this project. Your shell may not call another shell (e.g., running commands via the system function or executing bash, sh, etc.). Your code must compile and run on your Raspberry Pi set up with Arch Linux as described in class. Failure to follow these guidelines will will result in a grade of 0.

Changelog