The shell sits in front of a lot of my work, but I mostly use it for the outcome: running unix commands and scripts, creating branches and making commits. Unlike when I'm writing code, I'm rarely thinking about how the shell itself works under the hood.
So, to dig a bit deeper into shells, I'm going to build a toy one until I run out of time. I have a fresh pot of filter coffee, and I'm awake three hours before everyone else.
A quick look ahead to everything I'm able to support by the end:
./andsh andsh$ cd / andsh$ pwd / andsh$ echo $HOME /Users/andrew andsh$ nosuchcommand nosuchcommand: No such file or directory andsh$ echo $? 127 andsh$ printf abc
| tr a-z A-Z | rev CBA andsh$ ec<Tab> hello andsh$ echo hello hello andsh$ <Up> andsh$ echo hello hello andsh$ ^D
If you prefer reading C over prose, head straight to healeycodes/andsh.
REPL
A shell is an interactive program before it's a language implementation, and the user experience starts at the prompt. This first step is about building the interactive skeleton: print a prompt, read a line, keep a little state, and leave a clean place to plug executio logic into.
// repl.h typedef struct { int last_status ; int running ; int interactive ; } Shell ;
We also need the classic read-eval-print loop:
... continue reading