+# Match LINE against regexp OUTPUT_LINES[IDX].
+proc match_line { line output_lines idx_name } {
+ upvar $idx_name idx
+
+ while { 1 } {
+ if { $idx == [llength $output_lines] } {
+ # Ran out of regexps, bail out.
+ return -1
+ }
+
+ set re [lindex $output_lines $idx]
+ if { $re == "--optional" } {
+ # Optional, get actual regexp.
+ set opt 1
+ incr idx
+ set re [lindex $output_lines $idx]
+ } else {
+ # Not optional.
+ set opt 0
+ }
+
+ if { [regexp $re $line] } {
+ # Match.
+ incr idx
+ if { $idx == [llength $output_lines] } {
+ # Last match, we're done.
+ return 1
+ }
+ # Match found, keep looking for next match.
+ return 0
+ } else {
+ # No match.
+ if { $idx == 0 } {
+ # First match not found, just keep looking for first match.
+ return 0
+ } elseif { $opt } {
+ # Try next regexp on same line.
+ incr idx
+ continue
+ } else {
+ # Mismatch, bail out.
+ return -1
+ }
+ }
+ break
+ }
+
+ # Keep going.
+ return 0
+}
+