This is a fully functional UNIX shell implemented in minimal bytes. You may have to disable some compiler warnings to compile it.
- Execution of single commands as expected
- Piping commands arbitrarily with
|
- Redirecting both standard input and output with
<
and>
respectively - Semicolons to terminate commands and join multiple commands on a single line
- Shell builtins
cd
andexit
- Anything that might make this turing complete
- Any variety of feedback in the form of errors
It's fun to test the limits of C programming. This could likely be smaller, but a functional shell program that could fit on a business card or be encoded into a mid-size QR code is quite a feat.
The core of this shell is simplistic interpret rolled into a heavily stateful parsing state machine. There is string processing and a few global variables to manage special cases for the command as well as parser state.
The core is the string processing. There is a main buffer of chars
that holds
the input text, then a buffer of char*
that points into the text buffer, and
then a further char**
buffer that points into the previous buffer. Appropriate
state transitions are made for each character of the input buffer such that this
array of array of array of chars is a list of commands comprised of a list of
strings.
Finally, with all this information, the execution of the command is not difficult. The execution steps are not difficult to read, though trying to reason about the program as whole is not easy, even with this information.