DEV Community

harsh banthiya
harsh banthiya

Posted on

Writing my own minimal shell in C - Part 1 (The constraints)

Day 1 - Understanding the Problem.

This is my attempt to replicate the behavior of BASH or (Bourne again shell) command line interpreter for a school project. Before we get into how to get into the solution and the general approach of making the shell. Let's first look at all the constraints and what is expected from this minimal shell.

First and foremost, standard library functions I am allowed to use.

Below is the list of the external functions / standard library functions that I am allowed to use, if I need any another function I must code it myself. Please see the man pages of these functions if you do not recognize them.

readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay, add_history, printf, malloc, free, write, access, open, read, close, fork, wait, waitpid, wait3, wait4, signal, sigaction, kill, exit, getcwd, chdir, stat, lstat, fstat, unlink, execve, dup, dup2, pipe, opendir, readdir, closedir, strerror, perror, isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr, tgetflag, tgetnum, tgetstr, tgoto, tputs

Expectations from the shell, what must this minimal shell do

  1. It must implement the builtin shell functions

    • echo with the option -n
    • cd with only a relative or absolute path
    • pwd with no options
    • export with no options
    • unset with no options
    • env with no options or arguments
    • exit with no options
  2. Redirections

    • < should redirect input
    • > should redirect output
    • << read input from the current source until a line containing only the delimiter is even.
    • >> should redirect output in append mode.
  3. Pipes

    • | The output of each command in the pipeline is connected via a pipe to the input of the next command
  4. Environment Variables ($ followed by characters ) should expand to their values.

  5. $? should expand to the exit status of the most recently executed foreground pipeline

  6. For some simplification, the shell must not interpret unclosed quotes or unspecified special characters like '\' or ';' etc

  7. The shell must show a prompt waiting for next command, have a working history and must only use one global variable.

  8. ctrl-C ctrl-D ctrl-\ should work like bash. When interactive:

    • ctrl-C print a new prompt on a newline
    • ctrl-D exit the shell.

Lets begin researching the problems and the areas of Operating System I am less familiar with, in the next part of this series I will break down the general overview of the design and I will try to make it super modular.

Top comments (0)