extract wait_for_input, handle EINTR better (I think)