Terminal-emulator State Machine