Added options for the robot.
[tclrobot.git] / robot.tcl
1 #!/usr/bin/tclsh 
2 # $Id: robot.tcl,v 1.11 2001/01/23 11:26:43 adam Exp $
3 #
4 proc RobotFileNext1 {area lead} {
5     puts "RobotFileNext1 area=$area lead=$lead"
6     if {[catch {set ns [glob ${area}/*]}]} {
7         return {}
8     }
9     foreach n $ns {
10         if {[file isfile $n]} {
11             set off [string last / $n]
12             incr off 2
13             return $lead/[string range $n $off end]
14         }
15     }
16     foreach n $ns {
17         if {[file isdirectory $n]} {
18             set off [string last / $n]
19             incr off 2
20             set sb [RobotFileNext1 $n $lead/[string range $n $off end]]
21             if {[string length $sb]} {
22                 return $sb
23             }
24         }
25     }
26     return {}
27 }
28
29 proc RobotWriteRecord {outf fromurl distance} {
30     puts $outf "<zmbot>"
31     puts $outf "<distance>"
32     puts $outf $distance
33     puts $outf "</distance>"
34     puts $outf "<fromurl>"
35     puts $outf $fromurl
36     puts $outf "</fromurl>"
37     puts $outf "</zmbot>"
38 }
39
40 proc RobotReadRecord {inf fromurlx distancex} {
41     upvar $fromurlx fromurl
42     upvar $distancex distance
43     gets $inf
44     gets $inf
45     set distance [string trim [gets $inf]]
46     puts "got distance = $distance"
47     gets $inf
48     gets $inf
49     set fromurl [string trim [gets $inf]]
50 }
51
52 proc RobotFileNext {area} {
53     global robotSeq global idleTime ns
54
55     puts "RobotFileNext robotSeq=$robotSeq"
56     if {$robotSeq < 0} {
57         return {}
58     }
59     if {$robotSeq == 0} {
60         if {[catch {set ns [glob ${area}/*]}]} {
61             return {}
62         }
63     }
64     set off [string length $area]
65     incr off
66     set n [lindex $ns $robotSeq]
67     if {![string length $n]} {
68         set robotSeq -1
69         flush stdout
70         puts "------------ N E X T  R O U N D --------"
71         return wait
72     }
73     incr robotSeq
74     if {[file isfile $n/frobots.txt]} {
75         puts "ok returning http://[string range $n $off end]/robots.txt"
76         return http://[string range $n $off end]/robots.txt
77     } elseif {[file isdirectory $n]} {
78         set sb [RobotFileNext1 $n http://[string range $n $off end]]
79         if {[string length $sb]} {
80             return $sb
81         }
82     }
83     puts "no more work at end of RobotFileNext n=$n"
84     puts "ns=$ns"
85     return {}
86 }
87
88
89 proc RobotFileExist {area host path} {
90     puts "RobotFileExist begin area=$area host=$host path=$path"
91     set lpath [split $path /]
92     set l [llength $lpath]
93     incr l -1
94     set t [lindex $lpath $l]
95     incr l -1
96     set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
97     puts "RobotFileExist end npath=$npath"
98     return [file exists $npath]
99 }
100
101 proc RobotFileUnlink {area host path} {
102     puts "RobotFileUnlink begin"
103     puts "area=$area host=$host path=$path"
104     set lpath [split $path /]
105     set l [llength $lpath]
106     incr l -1
107     set t [lindex $lpath $l]
108     incr l -1
109     set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
110     puts "npath=$npath"
111     set comp [split $npath /]
112     set l [llength $comp]
113     incr l -1
114     if {[catch {exec rm [join $comp /]}]} return
115     incr l -1
116     for {set i $l} {$i > 0} {incr i -1} {
117         set path [join [lrange $comp 0 $i] /]
118         if {![catch {glob $path/*}]} return
119         exec rmdir ./$path
120     }
121     puts "RobotFileUnlink end"
122 }
123
124 proc RobotFileClose {out} {
125     if [string compare $out stdout] {
126         close $out
127     }
128 }
129
130 proc RobotFileOpen {area host path {mode w}} {
131     set orgPwd [pwd]
132     global workdir
133
134     if {![info exists workdir]} {
135         return stdout
136     }
137     puts "RobotFileOpen orgPwd=$orgPwd area=$area host=$host path=$path mode=$mode"
138     if {[string compare $orgPwd $workdir]} {
139         puts "ooops. RobotFileOpen failed"
140         puts "workdir = $workdir"
141         puts "pwd = $orgPwd"
142         exit 1
143     }
144     set comp [split $area/$host$path /]
145     set len [llength $comp]
146     incr len -1
147     for {set i 0} {$i < $len} {incr i} {
148         if {$i > 1} {
149             set d "d[lindex $comp $i]" 
150         } else {
151             set d [lindex $comp $i]
152         }
153         if {[catch {cd ./$d}]} {
154             exec mkdir $d
155             cd ./$d
156             if {![string compare $area unvisited] && $i == 1 && $mode == "w"} {
157                 set out [open frobots.txt w]
158                 puts "creating robots.txt in $d"
159                 close $out
160             }
161         }
162     }
163     set d [lindex $comp $len]
164     if {[string length $d]} {
165         if {[file isdirectory $d]} {
166             set out [open $d/f $mode]
167         } else {
168             set out [open f$d $mode]
169         }
170     } else {
171         set out [open f $mode]
172     }
173     cd $orgPwd
174     return $out
175 }
176
177 proc RobotRR {} {
178     global robotSeq robotsRunning
179
180     incr robotsRunning -1
181     while {$robotsRunning} {
182         vwait robotsRunning
183     }
184     set robotSeq 0
185     RobotStart
186 }
187
188 proc RobotRestart {url sock} {
189     global URL robotsRunning
190
191     close $sock
192     after cancel $URL($sock,cancel) 
193
194     foreach v [array names URL $url,*] {
195         unset URL($v)
196     }
197
198     incr robotsRunning -1
199     RobotStart
200 }
201
202 proc RobotStart {} {
203     global URL
204     global robotsRunning robotsMax idleTime
205   
206     puts "RobotStart"
207     while {1} {
208         set url [RobotFileNext unvisited]
209         if {![string length $url]} {
210             return
211         }
212         incr robotsRunning
213         if {[string compare $url wait] == 0} {
214             after $idleTime RobotRR
215             return
216         }
217         set r [RobotGetUrl $url {}]
218         if {!$r} {
219             if {$robotsRunning >= $robotsMax} return
220         } else {
221             incr robotsRunning -1
222             if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
223                 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
224                 RobotFileClose $outf
225             }
226             RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
227         }
228     }
229 }
230
231 proc headSave {url out} {
232     global URL
233     
234     if {[info exists URL($url,head,last-modified)]} {
235         puts $out "<lastmodified>$URL($url,head,last-modified)</lastmodified>"
236     }
237     puts $out {<si>}
238     if {[info exists URL($url,head,date)]} {
239         puts $out " <date>$URL($url,head,date)</date>"
240     }
241     if {[info exists URL($url,head,content-length)]} {
242         puts $out " <by>$URL($url,head,content-length)</by>"
243     }
244     if {[info exists URL($url,head,server)]} {
245         puts $out " <format>$URL($url,head,server)</format>"
246     }
247     puts $out {</si>}
248     puts $out {<publisher>}
249     puts $out " <identifier>$url</identifier>"
250     if {[info exists URL($url,head,content-type)]} {
251         puts $out " <type>$URL($url,head,content-type)</type>"
252     }
253     puts $out {</publisher>}
254 }
255
256 proc RobotHref {url hrefx hostx pathx} {
257     global URL domains
258     upvar $hrefx href
259     upvar $hostx host
260     upvar $pathx path
261
262     puts "Ref url = $url href=$href"
263
264     if {[string first { } $href] >= 0} {
265         return 0
266     }
267     if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
268         return 0
269     }
270     # get method (if any)
271     if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
272         set hpath $href
273         set method http
274     } else {
275         if {[string compare $method http]} {
276             return 0
277         }
278     }
279     # get host (if any)
280     if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
281         if {![string length $surl]} {
282             set surl /
283         }
284         set ok 0
285         foreach domain $domains {
286             if {[string match $domain $host]} {
287                 set ok 1
288                 break
289             }
290         }
291         if {!$ok} {
292             return 0
293         }
294     } else {
295         regexp {^([^\#]*)} $hpath x surl
296         set host $URL($url,hostport)
297     }
298     if {![string length $surl]} {
299         return 0
300     }
301     if {[string first / $surl]} {
302         # relative path
303         regexp {^([^\#?]*)} $URL($url,path) x dpart
304         set l [string last / $dpart]
305         if {[expr $l >= 0]} {
306             set surl [string range $dpart 0 $l]$surl
307         } else {
308             set surl $dpart/$surl
309         }
310     }
311     set c [split $surl /]
312     set i [llength $c]
313     incr i -1
314     set path [lindex $c $i]
315     incr i -1
316     while {$i >= 0} {
317         switch -- [lindex $c $i] {
318             .. {
319                 incr i -2
320                 if {$i < 0} {
321                     set i 0
322                 }
323             }
324             . {
325                 incr i -1
326             }
327             default {
328                 set path [lindex $c $i]/$path
329                 incr i -1
330             }
331         }
332     }
333     regsub -all {~} $path {%7E} path
334     set href "$method://$host$path"
335     puts "Ref href = $href"
336     return 1
337 }
338
339 proc RobotError {url code} {
340     global URL
341
342     puts "Bad URL $url, $code"
343     set fromurl {}
344     set distance -1
345     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
346         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
347         RobotReadRecord $inf fromurl distance
348         RobotFileClose $inf
349     }
350     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
351     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
352         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
353         RobotWriteRecord $outf $fromurl $distance
354         RobotFileClose $outf
355     }
356 }
357
358 proc RobotRedirect {url tourl code} {
359     global URL
360
361     puts "Redirecting from $url to $tourl"
362
363     set distance {}
364     set fromurl {}
365     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
366         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
367         RobotReadRecord $inf fromurl distance
368         RobotFileClose $inf
369     }
370     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
371         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
372         RobotWriteRecord $outf $fromurl $distance
373         RobotFileClose $outf
374     }
375     if {[RobotHref $url tourl host path]} {
376         if {![RobotFileExist visited $host $path]} {
377             if {![RobotFileExist unvisited $host $path]} {
378                 set outf [RobotFileOpen unvisited $host $path]
379                 RobotWriteRecord $outf $fromurl $distance
380                 RobotFileClose $outf
381             }
382         } else {
383             set olddistance {}
384             set inf [RobotFileOpen visited $host $path r]
385             RobotReadRecord $inf oldurl olddistance
386             RobotFileClose $inf
387             if {[string length $olddistance] == 0} {
388                 set olddistance 1000
389             }
390             if {[string length $distance] == 0} {
391                 set distance 1000
392             }
393             puts "distance=$distance olddistance=$olddistance"
394             if {[expr $distance < $olddistance]} {
395                 set outf [RobotFileOpen unvisited $host $path]
396                 RobotWriteRecord $outf $tourl $distance
397                 RobotFileClose $outf
398             }
399         }
400     }
401     if {[catch {RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)}]} {
402         puts "unlink failed"
403         exit 1
404     }
405 }
406
407 proc RobotTextHtml {url out} {
408     global URL maxDistance
409
410     set distance 0
411     if {$maxDistance < 1000 && [info exists URL($url,dist)]} {
412         set distance [expr $URL($url,dist) + 1]
413     }
414     htmlSwitch $URL($url,buf) \
415         title {
416             puts $out "<title>$body</title>"
417         } -nonest meta {
418             puts -nonewline $out "<meta"
419             foreach a [array names parm] {
420                 puts -nonewline $out " $a"
421                 puts -nonewline $out {="}
422                 puts -nonewline $out $parm($a)
423                 puts -nonewline $out {"}
424             }
425             puts $out {></meta>}
426         } body {
427             regsub -all -nocase {<script.*</script>} $body {} abody
428             regsub -all {<[^\>]+>} $abody {} nbody
429             puts $out "<documentcontent>"
430             puts $out $nbody
431             puts $out "</documentcontent>"
432         } a {
433             if {![info exists parm(href)]} {
434                 puts "no href"
435                 continue
436             }
437             if {[expr $distance <= $maxDistance]} {
438                 set href [string trim $parm(href)]
439                 if {![RobotHref $url href host path]} continue
440                 
441                 puts $out "<cr>"
442                 puts $out "<identifier>$href</identifier>"
443                 puts $out "<description>$body</description>"
444                 puts $out "</cr>"
445
446                 if {![RobotFileExist visited $host $path]} {
447                     set olddistance 1000
448                     if {![RobotFileExist bad $host $path]} {
449                         if {[RobotFileExist unvisited $host $path]} {
450                             set inf [RobotFileOpen unvisited $host $path r]
451                             RobotReadRecord $inf oldurl olddistance
452                             RobotFileClose $inf
453                         }
454                     } else {
455                         set olddistance 0
456                     }
457                     if {[string length $olddistance] == 0} {
458                         set olddistance 1000
459                     }
460                     if {[expr $distance < $olddistance]} {
461                         set outf [RobotFileOpen unvisited $host $path]
462                         RobotWriteRecord $outf $url $distance
463                         RobotFileClose $outf
464                     }
465                 } elseif {[string compare $href $url]} {
466                     set inf [RobotFileOpen visited $host $path r]
467                     RobotReadRecord $inf xurl olddistance
468                     close $inf
469                     if {[string length $olddistance] == 0} {
470                         set olddistance 1000
471                     }
472                     if {[expr $distance < $olddistance]} {
473                         puts "OK remarking url=$url href=$href"
474                         puts "olddistance = $olddistance"
475                         puts "newdistance = $distance"
476                         set outf [RobotFileOpen unvisited $host $path]
477                         RobotWriteRecord $outf $url $distance
478                         RobotFileClose $outf
479                     }
480                 }
481             }
482         }
483 }
484
485 proc RobotsTxt {url} {
486     global agent URL
487
488     RobotsTxt0 URL(URL($url,hostport),robots) $URL($url,buf)
489 }
490
491 proc RobotsTxt0 {v buf} {
492     global URL agent
493     set section 0
494     foreach l [split $buf \n] {
495         if {[regexp {([-A-Za-z]+):[ \t]*([^\#]+)} $l match cmd arg]} {
496             puts "cmd=$cmd arg=$arg"
497             switch $cmd {
498                 User-Agent {
499                     if {$section} break
500                     set pat [string tolower $arg]*
501                     set section [string match $pat $agent]
502                 }
503                 Disallow {
504                     if {$section} {
505                         puts "rule [list 0 $arg]"
506                         lappend $v [list 0 $arg]
507                     }
508                 }
509                 Allow {
510                     if {$section} {
511                         puts "rule [list 1 $arg]"
512                         lappend $v [list 1 $arg]
513                     }
514                 }
515             }
516         }
517     }
518 }
519
520 proc RobotTextPlain {url out} {
521     global URL
522
523     puts $out "<documentcontent>"
524     puts $out $URL($url,buf)
525     puts $out "</documentcontent>"
526
527     if {![string compare $URL($url,path) /robots.txt]} {
528         RobotsTxt $url
529     }
530 }
531
532 proc Robot200 {url} {
533     global URL domains
534     
535     set out [RobotFileOpen visited $URL($url,hostport) $URL($url,path)]
536     puts $out "<zmbot>"
537
538     set distance 1000
539     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
540         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
541         RobotReadRecord $inf fromurl distance
542         RobotFileClose $inf
543     }
544     set URL($url,dist) $distance
545     puts $out "<distance>"
546     puts $out "  $distance"
547     puts $out "</distance>"
548     headSave $url $out
549     puts "Parsing $url distance=$distance"
550     switch $URL($url,head,content-type) {
551         text/html {
552             if {[string length $distance]} {
553                 RobotTextHtml $url $out
554             }
555         }
556         text/plain {
557             RobotTextPlain $url $out
558         }
559         application/pdf {
560             set pdff [open test.pdf w]
561             puts -nonewline $pdff $URL($url,buf)
562             close $pdff
563         }
564     }
565     puts $out "</zmbot>"
566     RobotFileClose $out
567     # puts "Parsing done"
568     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
569 }
570
571 proc RobotReadContent {url sock binary} {
572     global URL
573
574     puts "RobotReadContent $url"
575     set buffer [read $sock 16384]
576     set readCount [string length $buffer]
577
578     if {$readCount <= 0} {
579         Robot200 $url
580         RobotRestart $url $sock
581     } elseif {!$binary && [string first \0 $buffer] >= 0} {
582         Robot200 $url
583         RobotRestart $url $sock
584     } else {
585         # puts "Got $readCount bytes"
586         set URL($url,buf) $URL($url,buf)$buffer
587     }
588 }
589
590 proc RobotReadHeader {url sock} {
591     global URL
592
593     puts "RobotReadHeader $url"
594     if {[catch {set buffer [read $sock 2148]}]} {
595         RobotError $url 404
596         RobotRestart $url $sock
597     }
598     set readCount [string length $buffer]
599     
600     if {$readCount <= 0} {
601         RobotError $url 404
602         RobotRestart $url $sock
603     } else {
604         # puts "Got $readCount bytes"
605         set URL($url,buf) $URL($url,buf)$buffer
606         
607         set n [string first \r\n\r\n $URL($url,buf)]
608         if {$n > 1} {
609             set code 0
610             set version {}
611             set headbuf [string range $URL($url,buf) 0 $n]
612             incr n 4
613             set URL($url,buf) [string range $URL($url,buf) $n end]
614             
615             regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
616             set lines [split $headbuf \n]
617             foreach line $lines {
618                 if {[regexp {^([^:]+):[ ]+(.*)} $line x name value]} {
619                     set URL($url,head,[string tolower $name]) [string trim $value]
620                 }
621             }
622             puts "code = $code"
623             set URL($url,state) skip
624             switch $code {
625                 301 {
626                     RobotRedirect $url $URL($url,head,location) 301
627                     RobotRestart $url $sock
628                 }
629                 302 {
630                     RobotRedirect $url $URL($url,head,location) 302
631                     RobotRestart $url $sock
632                 }
633                 200 {
634                     if {![info exists URL($url,head,content-type)]} {
635                         set URL($url,head,content-type) {}
636                     }
637                     set binary 0
638                     switch $URL($url,head,content-type) {
639                         application/pdf {
640                             set binary 1
641                         }
642                     }
643                     fileevent $sock readable [list RobotReadContent $url $sock $binary]
644                 }
645                 default {
646                     RobotError $url $code
647                     RobotRestart $url $sock
648                 }
649             }
650         }
651     }
652 }
653
654 proc RobotSockCancel {url sock} {
655
656     puts "RobotSockCancel sock=$sock url=$url"
657     RobotError $url 401
658     RobotRestart $url $sock
659 }
660
661 proc RobotConnect {url sock} {
662     global URL agent
663
664     fconfigure $sock -translation {lf crlf} -blocking 0
665     fileevent $sock readable [list RobotReadHeader $url $sock]
666     puts $sock "GET $URL($url,path) HTTP/1.0"
667     puts $sock "Host: $URL($url,host)"
668     puts $sock "User-Agent: $agent"
669     puts $sock ""
670     flush $sock
671     set URL($sock,cancel) [after 30000 [list RobotSockCancel $url $sock]]
672 }
673
674 proc RobotNop {} {
675
676 }
677
678 proc RobotGetUrl {url phost} {
679     global URL robotsRunning
680     flush stdout
681     puts "RobotGetUrl --------- robotsRunning=$robotsRunning url=$url"
682     if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
683         return -1
684     }
685     if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
686         set port 80
687         set host $hostport
688     }
689     set URL($url,method) $method
690     set URL($url,host) $host
691     set URL($url,hostport) $hostport
692     set URL($url,path) $path
693     set URL($url,state) head
694     set URL($url,buf) {}
695
696     if {[string compare $path /robots.txt]} {
697         set ok 1
698         if {![info exists URL($hostport,robots)]} {
699             puts "READING robots.txt for host $hostport"
700             if {[RobotFileExist visited $hostport /robots.txt]} {
701                 set inf [RobotFileOpen visited $hostport /robots.txt r]
702                 set buf [read $inf 32768]
703                 close $inf
704             } else {
705                 set buf "User-Agent: *\nAllow: /\n"
706             }
707             RobotsTxt0 URL($hostport,robots) $buf
708         }
709         if {[info exists URL($hostport,robots)]} {
710             foreach l $URL($hostport,robots) {
711                 if {[string first [lindex $l 1] $path] == 0} {
712                     set ok [lindex $l 0]
713                     break
714                 }
715             }
716         }
717         if {!$ok} {
718             return -1
719         }
720     }
721     if [catch {set sock [socket -async $host $port]}] {
722         return -1
723     }
724     RobotConnect $url $sock
725
726     return 0
727 }
728
729 if {![llength [info commands htmlSwitch]]} {
730     set e [info sharedlibextension]
731     if {[catch {load ./tclrobot$e}]} {
732         load tclrobot$e
733     }
734 }
735
736 set agent "zmbot/0.0"
737 if {![catch {set os [exec uname -s -r]}]} {
738     set agent "$agent ($os)"
739 }
740
741 puts "agent: $agent"
742
743 proc bgerror {m} {
744     global errorInfo
745     puts "BGERROR $m"
746     puts $errorInfo
747 }
748
749 set robotsRunning 0
750 set robotSeq 0
751 set workdir [pwd]
752 set idleTime 60000
753
754 set i 0
755 set l [llength $argv]
756
757 if {$l < 2} {
758     puts {tclrobot: usage [-j jobs] [-c count] [-d domain] [url ..]}
759     puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
760     exit 1
761 }
762
763 while  {$i < $l} {
764     set arg [lindex $argv $i]
765     switch -glob -- $arg {
766         -j* {
767             set robotsMax [string range $arg 2 end]
768             if {![string length $robotsMax]} {
769                 set robotsMax [lindex $argv [incr i]]
770             }
771         }
772         -c* {
773             set maxDistance [string range $arg 2 end]
774             if {![string length $maxDistance]} {
775                 set maxDistance [lindex $argv [incr i]]
776             }
777         }
778         -d* {
779             set dom [string range $arg 2 end]
780             if {![string length $dom]} {
781                 set dom [lindex $argv [incr i]]
782             }
783             lappend domains $dom
784         }
785         default {
786             set href $arg
787             if {[RobotHref http://www.indexdata.dk/ href host path]} {
788                 if {![RobotFileExist visited $host $path]} {
789                     set outf [RobotFileOpen unvisited $host $path]
790                     RobotWriteRecord $outf href 0
791                     RobotFileClose $outf
792                 }
793             }
794         }
795     }
796     incr i
797 }
798
799 if {![info exist domains]} {
800     set domains {*}
801 }
802 if {![info exist maxDistance]} {
803     set maxDistance 3
804 }
805 if {![info exist robotsMax]} {
806     set robotsMax 5
807 }
808
809 puts "domains=$domains"
810 puts "max distance=$maxDistance"
811 puts "max jobs=$robotsMax"
812
813 RobotStart
814
815 while {$robotsRunning} {
816     vwait robotsRunning
817 }