forked from facebook/phpsh. merged some patches that have not been taken up.