Restore terminal mode on exit

This restores the terminal mode when the program exits normally or by SIGINT.