Detect the pipe for the pager being closed and exit gracefully.
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.