Skip to content

Latest commit

 

History

History
80 lines (69 loc) · 2.23 KB

README.md

File metadata and controls

80 lines (69 loc) · 2.23 KB

Unix shell in C (MacOs)

An educational project based on the curriculum of ucode.world. Uses Meta-circular interpreter under the hood. It is written in C for MacOs.

This shell implements:

  1. execution of external commands (bin files),
  2. execution of builtins,
  3. meta-circular interpreter,
  4. interpretation of complex grammar of input line including command separator ( ; ), logic operators ( && || ), pipeline ( | ), command substitution, redirections,
  5. expansions of tilda (~),
  6. variables ( $name, ${name}),
  7. escape working accordingly in no quote, quote, double quote expressions,
  8. Ctrl+C, Ctrl+D, Ctrl+Z,
  9. command history,
  10. backspace key while inputting line.

1. Execution external commands (bin files)

Bin files are executed in a separate process. e.g. ls ls -la /bin/ls -l

2. Builtins implemented in the shell:

  1. Echo e.g. echo "CBL World" echo CBL World echo 'CBL World' echo -n "\a"
  2. Exit
  3. cd
  4. pwd
  5. env
  6. set
  7. unset
  8. export
  9. fg
  10. path
  11. yes
  12. true
  13. false
  14. jobs
  15. which

3. Meta-circular interpreter

Parsing and execution of the input line is done by implementation of meta-circular interpreter described in the book Structure and Interpretation of Computer Programs.

4. Shell Grammar

4.1 Grammar

Details of grammar are here

4.2 Operator precedence

  1. ;
  2. || &&
  3. |
  4. =
  5. Expansions (see below)
    5.1. "..." '...'
    5.2. $... $(...)
    5.3. `...` ${...}
    5.4. ~
  6. Redirections
    6.1. >
    6.2. >>
    6.3. <

4.3 Order of expansion performance

The following types of expansions are performed in the indicated order in five steps:

  1. Alias Expansion (not implemented)
  2. Parameter Expansion (only $name and ${name})
  3. Command Substitution
  4. These are performed in left-to-right fashion. On each argument, any of the five steps that are needed are performed one after the other. Hence, for example, all the parts of parameter expansion are completed before command substitution is started. After these expansions, all unquoted occurrences of the characters \ and " are removed.
  5. Filename Expansion