COSC 3346, Operating Systems

Project #1: (guish) "Gnu" Island Shell

Date Assigned: Wednesday, June 7, 2017
Due Date: Monday, June 21, 2017 by 11:59 p.m.

This is a fun project that bring together many concepts already presented in class. Enjoy this project and have fun!


The purpose of this assignment is to get everyone up to speed on system programming 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 CLI program that conveniently allows you to run other programs.  Read up on your favorite shell (such as bash, csh, tcsh, sh, ...) to see what it does..



Basic Commands:  The input to guish is a sequence of commands, each provided on a separate line of input text (or typed interactively at the keyboard).  The following command must be supported:

Runs the program progname with the given, possibly optional, arguments.

The exit internal command causes the shell to exit.  This is the *only* way your shell can exit.  Just prior to your shell exiting, you should print out the number of times each interrupt was detected.

The hist history internal shell command will display the (up to) 10 most recent commands entered from the user.  See details below how this should work.

The r n internal shell command will execute a command from the history list.  See details below how this should work.


I/O Redirection: Your shell must support I/O redirection.  By default, guish runs programs so that input data is read from standard input and output data is written to standard output.  However, this behavior can be changed using I/O redirection.  I/O redirection is specified using the <, > operators.  For example:

Write the standard output of progname to the file file.out.

Uses the contents of the file as the standard input to program progname.

Both input and output redirection may be specified for a single command so your shell will have to check for both and operate accordingly.  The redirections can be specified in any order.


Handling Erroneous Programs:  Finally, when programs terminate, they return an integer exit code to your shell.  If this code is non-zero, your shell should print the returned value.  For example

guish: cp foo bar

    cp: cannot access foo

    [ Program returned exit code 1 ]



Getting Started

Make sure you understand the assignment before beginning any work.  Now, a guish shell template, parser, and makefile are provided for you.

The name of our shell will be called New Island Shell (guish).  You are provided with the files guish.l and guish.cpp which contain some source code that uses gettoks(), a function provided by guish.l to get and parse a line of input.  The function gettoks() returns an array of pointers to character strings (i.e. NULL terminated C style strings).  Each string is either a word containing the letters, numbers, ., and /, or a single character string containing one of the special characters: ( ) < > | & ;.

To compile guish.l, you have to use flex: "flex guish.l".  This will produce a file called (by default) lex.yy.c which is compiled with gcc.  The file guish.cpp will be compiled with g++.   The final step is to link the object files lex.yy.o and guish.o with the flex library -lfl.  If this is all confusing, here is a  Makefile to simplify the entire process. (with a few modifications)


Other Shell Details

Your shell must support the following:

  1. The command exit is an internal shell command.  It will cause your shell program to quit and print the interrupt counts.
  2. The ONLY way your program is to exit is via the 'exit' command at the prompt.  Therefore your shell will have to trap the appropriate signals.  When your shell 'exits' display a list of the signals your shell traps and the number of interrupts of each type. (Ctrl C, Ctrl \, Ctrl Z)
  3. The internal shell command "hist" which displays (up to) the ten most recent commands typed into your shell. The hist command will be added to the history.  The commands will always be numbered from 1 to 10.
  4. All commands (internal or external, successful or unsuccessful, except r and exit) are added to the history list.
  5. The internal shell command "r n" where 'n' is the history number of that command in the history list. Also, the user should be able to run the most recent command again by just entering 'r'. You can assume that one space will separate the the 'r' and the number of the command. Any command that is executed in this fashion should be echoed on the user's screen and the command is also placed in the history buffer as the next command.  The r command is not added to the history list but the referenced command is added to the history list.
  6. Take some creative liberty is designing your shell prompt.  You could include the name of the shell, current working subdirectory, command number, etc. Static prompts will result in loss of points.
  7. You must check and correctly handle all return values.  This means that you need to read the man pages for each function to understand what the possible return values are, what errors they indicate, and what you must do when you get that error.
  8. You may not use the "system()" system function in your shell.  However, this function call is good for prototyping and debugging.


Getting Help

You are expected to do your own work and research regarding the usage of various system calls, header files, and libraries.  Information is readily available in the man pages, UNIX reference books, or on the web.  Otherwise, do not hesitate to ask a question if you are unclear about how some part of the assignment is supposed to work.


Extra Credit

For 5 points extra added to this project, have your shell process a command, with or without arguments, whose output is piped to the input of another command.


Submitting Your Project

Please see the project submission guidelines for detailing how your project should be formatted for submission.  In addition, please turn in a printed listing of guish.cpp on the due date.


Assessment and Grading

This is an individual assignment.  Write a well structured, well documented, computer program using C++ and the supporting files provided to you.  Your project must following the documentation standards defined here.  Use procedures and functions.  You may use the STL in your solution.  This project is worth 100 points.