Add select, two global variables to simplify code.

Add select to avoid polling
Add global ufile and escape_char to simplify code structure