aboutsummaryrefslogtreecommitdiffstats

For this assignment, you will be writing a program called hush, the HU Shell. This program will read commands from stdin and execute them, much like the bash shell that you've been using on the server so far. Of course, your shell will be a lot simpler than bash and we'll make several assumptions to ease your implementation.

Required Features

The required features of your shell are as follows,

  1. Your shell should be interactive. When launched, present the user with a prompt of the form,

    and all them to type a command immediately following the sigil,
    $ ls
  2. When the user hits enter, parse the command they entered and execute it. Fork a process to do so.
  3. Your shell should support redirecting stdout and stdin. If the user doesn't specify an input or output file, the forked process should inherit them from the shell itself. However, the user can specify an output file like,
    $ ls > output.txt
    or an input file like
    $ cat < input.txt
    and your shell should create/open these files and set up stdin/stdout for the new process accordingly.
  4. Your shell should support basic variables. 
    1. Allow the user to create a variable using the following syntax
      $ myvar=test
      Note the lack of spaces.
    2. In a command string, a $ preceding a name should cause the shell to replace that name with the value associated with that name, or with an empty string if it isn't been defined. For example,
      $ myvar=test.txt
      $ cat $myvar
      should execute the command
      $ cat test.txt
  5. Your shell should have the following built-in commands
    1. export varname
      1. If varname is a variable, remove it from the local variable store and create an environment variable with the same name and value
    2. cd dirname
      1. Update the shell's working directory to dirname
    3. pwd
      1. Write the current working directory of the shell to stdout
    4. exit
      1. Calls exit(EXIT_SUCCESS) to close the shell session
  6. Pipes
    1. Your shell should have support for command pipelines, using the character |
      $ command1 | command2 | command 3
      should execute all three commands, with pipes created between them.

Simplifying Assumptions

You may make the following simplifications to standard POSIX shell semantics

  1. Assume that all tokens are whitespace delimited. You do not need to handle quoting or escaping spaces. This means that it will be impossible to interact with files or commands that have spaces in their names, and variable values cannot contain spaces either.
  2. A space is required before and after a pipe or I/O redirection operator. In other words, it will always look like,
    $ ls > test.txt
    and never
    $ ls >test.txt
    etc.
  3. You do not need to implement or handling globbing/wildcard expansion.
  4. Your shell shouldn't crash when an invalid command is provided, but you do not need to provide detailed error messages about what went wrong.
  5. All commands a newline terminated--you do not need to handle multiple commands on a single line, or spreading a single command over multiple lines
  6. You do not need to support redirection of stderr.
  7. A variable cannot be used as a command. In other words, the following is invalid
    $ cmd=ls
    $ $cmd
  8. You do not need to implement multi-tasking. There are no background jobs--all processes run in the foreground. 
  9. You may assume that a command will have no more than 8 arguments.
  10. You do not need to support executing commands that are not on the PATH. In other words, you don't need to support running a command like,
    $ ./a.out
    where a.out is in the working directory, but not on the path.
  11. You may assume that commands, arguments, filenames, etc., are purely alphanumeric. Thus, if you see an = it will always mean variable assignment, and <, >, | will always be I/O redirections, etc.
  12. You may assume that commands are no longer than 1023 characters.

Hints

  • Consider using the strsep(3) function for parsing command strings. 
  • When reading the man page for strsep, look for a "SEE ALSO" section. This may contain other functions you may find useful for this project.
  • The hash table you wrote in Project 1 might be useful here