Be smarter about cleaning up the pager.

Register an atexit handler to make sure the pager gets cleaned up
correctly.  Also, attempt to preserve some mechanism for output by
duping the stdout and stderr, and restoring them in the atexit handler.
Finally, if stderr is also going to a terminal, route it through the
pager too.