Move the tty-related stuff out to tty.c as far as possible.