prefix all debug output with "D ", add a missing break; and tidy indenting on break statements.
bug fixes
Read the first line at startup (which is probably partial). Give up reading a line after reading too many debug lines.