Survive the Terminal
Before a shell can run a command it has to measure a string, compare one, copy one without running off the end, split a line into arguments, and loop until the user or the input stops. Three chapters, each one primitive built rather than imported. The parts that need a real kernel, fork, exec and pipes, are in the operating system section already.
strlenis a walk, every time you call it. That cost is the whole argument for a slice that already knows its length.- A buffer for a 5-byte string needs 6 bytes. The version of the bounds check that forgets the terminator passes a casual test and writes one byte past the end.
- Trimming returns a view, not a copy. Moving bytes to remove spaces is work you never needed to do.
- A REPL has two exits, not one. A loop that only watches for
quitspins forever the first time someone presses Ctrl-D.
3 chapters.
- String Primitivesstrlen, compare, copy and trim, written out. The off-by-one lives here.
- Tokenizing a Command LineTurning "ls -la /tmp" into the argv a shell needs, without copying anything.
- The Read-Eval-Print LoopFour steps and two ways to stop, which is the part people get wrong.