This is an absolutely classic case for a “finite-state machine (FSM)” algorithm. (I’ll leave that for you to
Google WikiPedia.) There are four different “kinds” of input-lines:
- PCName: $1
- Command$1: $2
- blank line
... and a suitable but arbitrary number of “states.”
The logic of an FSM is very powerful because it moves from one “state” to another based on the current input (one of the four types listed above) and the current state. At each state, it can call a particular subroutine or piece of logic. This approach is also very flexible, easily modified, and it’s also clear because the logic of deciding what kind of input you have is clearly separated from the state-driven logic that does something about it.