Move all global state into a C++ class

This makes the code reentrant and easier to re-initialize.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Remove spurious debug message

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Move terminal output code back into main.cc

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Add some tests and fix bugs discovered by these

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>