Restore terminal mode on exit

This restores the terminal mode when the program exits normally or because of a SIGINT.