main loop exits on eofs