Move terminal handling into terminal module.

Handling terminal control flags is now done in the terminal module
instead of the main module.