Minor fix for anchor references.
[tclrobot.git] / robot.tcl
1 #!/usr/bin/tclsh 
2 # $Id: robot.tcl,v 1.12 2001/01/23 14:28:41 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         if {[info exist domains]} {
285             set ok 0
286             foreach domain $domains {
287                 if {[string match $domain $host]} {
288                     set ok 1
289                     break
290                  }
291             }
292             if {!$ok} {
293                 return 0
294             }
295         }
296     } else {
297         regexp {^([^\#]*)} $hpath x surl
298         set host $URL($url,hostport)
299     }
300     if {![string length $surl]} {
301         return 0
302     }
303     if {[string first / $surl]} {
304         # relative path
305         regexp {^([^\#?]*)} $URL($url,path) x dpart
306         set l [string last / $dpart]
307         if {[expr $l >= 0]} {
308             set surl [string range $dpart 0 $l]$surl
309         } else {
310             set surl $dpart/$surl
311         }
312     }
313     set c [split $surl /]
314     set i [llength $c]
315     incr i -1
316     set path [lindex $c $i]
317     incr i -1
318     while {$i >= 0} {
319         switch -- [lindex $c $i] {
320             .. {
321                 incr i -2
322                 if {$i < 0} {
323                     set i 0
324                 }
325             }
326             . {
327                 incr i -1
328             }
329             default {
330                 set path [lindex $c $i]/$path
331                 incr i -1
332             }
333         }
334     }
335     regsub -all {~} $path {%7E} path
336     set href "$method://$host$path"
337     puts "Ref href = $href"
338     return 1
339 }
340
341 proc RobotError {url code} {
342     global URL
343
344     puts "Bad URL $url, $code"
345     set fromurl {}
346     set distance -1
347     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
348         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
349         RobotReadRecord $inf fromurl distance
350         RobotFileClose $inf
351     }
352     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
353     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
354         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
355         RobotWriteRecord $outf $fromurl $distance
356         RobotFileClose $outf
357     }
358 }
359
360 proc RobotRedirect {url tourl code} {
361     global URL
362
363     puts "Redirecting from $url to $tourl"
364
365     set distance {}
366     set fromurl {}
367     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
368         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
369         RobotReadRecord $inf fromurl distance
370         RobotFileClose $inf
371     }
372     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
373         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
374         RobotWriteRecord $outf $fromurl $distance
375         RobotFileClose $outf
376     }
377     if {[RobotHref $url tourl host path]} {
378         if {![RobotFileExist visited $host $path]} {
379             if {![RobotFileExist unvisited $host $path]} {
380                 set outf [RobotFileOpen unvisited $host $path]
381                 RobotWriteRecord $outf $fromurl $distance
382                 RobotFileClose $outf
383             }
384         } else {
385             set olddistance {}
386             set inf [RobotFileOpen visited $host $path r]
387             RobotReadRecord $inf oldurl olddistance
388             RobotFileClose $inf
389             if {[string length $olddistance] == 0} {
390                 set olddistance 1000
391             }
392             if {[string length $distance] == 0} {
393                 set distance 1000
394             }
395             puts "distance=$distance olddistance=$olddistance"
396             if {[expr $distance < $olddistance]} {
397                 set outf [RobotFileOpen unvisited $host $path]
398                 RobotWriteRecord $outf $tourl $distance
399                 RobotFileClose $outf
400             }
401         }
402     }
403     if {[catch {RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)}]} {
404         puts "unlink failed"
405         exit 1
406     }
407 }
408
409 proc RobotTextHtml {url out} {
410     global URL maxDistance
411
412     set distance 0
413     if {$maxDistance < 1000 && [info exists URL($url,dist)]} {
414         set distance [expr $URL($url,dist) + 1]
415     }
416     htmlSwitch $URL($url,buf) \
417         title {
418             puts $out "<title>$body</title>"
419         } -nonest meta {
420             puts -nonewline $out "<meta"
421             foreach a [array names parm] {
422                 puts -nonewline $out " $a"
423                 puts -nonewline $out {="}
424                 puts -nonewline $out $parm($a)
425                 puts -nonewline $out {"}
426             }
427             puts $out {></meta>}
428         } body {
429             regsub -all -nocase {<script.*</script>} $body {} abody
430             regsub -all {<[^\>]+>} $abody {} nbody
431             puts $out "<documentcontent>"
432             puts $out $nbody
433             puts $out "</documentcontent>"
434         } -nonest a {
435             if {![info exists parm(href)]} {
436                 puts "no href"
437                 continue
438             }
439             if {[expr $distance <= $maxDistance]} {
440                 set href [string trim $parm(href)]
441                 if {![RobotHref $url href host path]} continue
442                 
443                 puts $out "<cr>"
444                 puts $out "<identifier>$href</identifier>"
445                 puts $out "<description>$body</description>"
446                 puts $out "</cr>"
447
448                 if {![RobotFileExist visited $host $path]} {
449                     set olddistance 1000
450                     if {![RobotFileExist bad $host $path]} {
451                         if {[RobotFileExist unvisited $host $path]} {
452                             set inf [RobotFileOpen unvisited $host $path r]
453                             RobotReadRecord $inf oldurl olddistance
454                             RobotFileClose $inf
455                         }
456                     } else {
457                         set olddistance 0
458                     }
459                     if {[string length $olddistance] == 0} {
460                         set olddistance 1000
461                     }
462                     if {[expr $distance < $olddistance]} {
463                         set outf [RobotFileOpen unvisited $host $path]
464                         RobotWriteRecord $outf $url $distance
465                         RobotFileClose $outf
466                     }
467                 } elseif {[string compare $href $url]} {
468                     set inf [RobotFileOpen visited $host $path r]
469                     RobotReadRecord $inf xurl olddistance
470                     close $inf
471                     if {[string length $olddistance] == 0} {
472                         set olddistance 1000
473                     }
474                     if {[expr $distance < $olddistance]} {
475                         puts "OK remarking url=$url href=$href"
476                         puts "olddistance = $olddistance"
477                         puts "newdistance = $distance"
478                         set outf [RobotFileOpen unvisited $host $path]
479                         RobotWriteRecord $outf $url $distance
480                         RobotFileClose $outf
481                     }
482                 }
483             }
484         }
485 }
486
487 proc RobotsTxt {url} {
488     global agent URL
489
490     RobotsTxt0 URL(URL($url,hostport),robots) $URL($url,buf)
491 }
492
493 proc RobotsTxt0 {v buf} {
494     global URL agent
495     set section 0
496     foreach l [split $buf \n] {
497         if {[regexp {([-A-Za-z]+):[ \t]*([^\#]+)} $l match cmd arg]} {
498             puts "cmd=$cmd arg=$arg"
499             switch $cmd {
500                 User-Agent {
501                     if {$section} break
502                     set pat [string tolower $arg]*
503                     set section [string match $pat $agent]
504                 }
505                 Disallow {
506                     if {$section} {
507                         puts "rule [list 0 $arg]"
508                         lappend $v [list 0 $arg]
509                     }
510                 }
511                 Allow {
512                     if {$section} {
513                         puts "rule [list 1 $arg]"
514                         lappend $v [list 1 $arg]
515                     }
516                 }
517             }
518         }
519     }
520 }
521
522 proc RobotTextPlain {url out} {
523     global URL
524
525     puts $out "<documentcontent>"
526     puts $out $URL($url,buf)
527     puts $out "</documentcontent>"
528
529     if {![string compare $URL($url,path) /robots.txt]} {
530         RobotsTxt $url
531     }
532 }
533
534 proc Robot200 {url} {
535     global URL domains
536     
537     set out [RobotFileOpen visited $URL($url,hostport) $URL($url,path)]
538     puts $out "<zmbot>"
539
540     set distance 1000
541     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
542         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
543         RobotReadRecord $inf fromurl distance
544         RobotFileClose $inf
545     }
546     set URL($url,dist) $distance
547     puts $out "<distance>"
548     puts $out "  $distance"
549     puts $out "</distance>"
550     headSave $url $out
551     puts "Parsing $url distance=$distance"
552     switch $URL($url,head,content-type) {
553         text/html {
554             if {[string length $distance]} {
555                 RobotTextHtml $url $out
556             }
557         }
558         text/plain {
559             RobotTextPlain $url $out
560         }
561         application/pdf {
562             set pdff [open test.pdf w]
563             puts -nonewline $pdff $URL($url,buf)
564             close $pdff
565         }
566     }
567     puts $out "</zmbot>"
568     RobotFileClose $out
569     # puts "Parsing done"
570     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
571 }
572
573 proc RobotReadContent {url sock binary} {
574     global URL
575
576     puts "RobotReadContent $url"
577     set buffer [read $sock 16384]
578     set readCount [string length $buffer]
579
580     if {$readCount <= 0} {
581         Robot200 $url
582         RobotRestart $url $sock
583     } elseif {!$binary && [string first \0 $buffer] >= 0} {
584         Robot200 $url
585         RobotRestart $url $sock
586     } else {
587         # puts "Got $readCount bytes"
588         set URL($url,buf) $URL($url,buf)$buffer
589     }
590 }
591
592 proc RobotReadHeader {url sock} {
593     global URL
594
595     puts "RobotReadHeader $url"
596     if {[catch {set buffer [read $sock 2148]}]} {
597         RobotError $url 404
598         RobotRestart $url $sock
599     }
600     set readCount [string length $buffer]
601     
602     if {$readCount <= 0} {
603         RobotError $url 404
604         RobotRestart $url $sock
605     } else {
606         # puts "Got $readCount bytes"
607         set URL($url,buf) $URL($url,buf)$buffer
608         
609         set n [string first \r\n\r\n $URL($url,buf)]
610         if {$n > 1} {
611             set code 0
612             set version {}
613             set headbuf [string range $URL($url,buf) 0 $n]
614             incr n 4
615             set URL($url,buf) [string range $URL($url,buf) $n end]
616             
617             regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
618             set lines [split $headbuf \n]
619             foreach line $lines {
620                 if {[regexp {^([^:]+):[ ]+(.*)} $line x name value]} {
621                     set URL($url,head,[string tolower $name]) [string trim $value]
622                 }
623             }
624             puts "code = $code"
625             set URL($url,state) skip
626             switch $code {
627                 301 {
628                     RobotRedirect $url $URL($url,head,location) 301
629                     RobotRestart $url $sock
630                 }
631                 302 {
632                     RobotRedirect $url $URL($url,head,location) 302
633                     RobotRestart $url $sock
634                 }
635                 200 {
636                     if {![info exists URL($url,head,content-type)]} {
637                         set URL($url,head,content-type) {}
638                     }
639                     set binary 0
640                     switch $URL($url,head,content-type) {
641                         application/pdf {
642                             set binary 1
643                         }
644                     }
645                     fileevent $sock readable [list RobotReadContent $url $sock $binary]
646                 }
647                 default {
648                     RobotError $url $code
649                     RobotRestart $url $sock
650                 }
651             }
652         }
653     }
654 }
655
656 proc RobotSockCancel {url sock} {
657
658     puts "RobotSockCancel sock=$sock url=$url"
659     RobotError $url 401
660     RobotRestart $url $sock
661 }
662
663 proc RobotConnect {url sock} {
664     global URL agent
665
666     fconfigure $sock -translation {lf crlf} -blocking 0
667     fileevent $sock readable [list RobotReadHeader $url $sock]
668     puts $sock "GET $URL($url,path) HTTP/1.0"
669     puts $sock "Host: $URL($url,host)"
670     puts $sock "User-Agent: $agent"
671     puts $sock ""
672     flush $sock
673     set URL($sock,cancel) [after 30000 [list RobotSockCancel $url $sock]]
674 }
675
676 proc RobotNop {} {
677
678 }
679
680 proc RobotGetUrl {url phost} {
681     global URL robotsRunning
682     flush stdout
683     puts "RobotGetUrl --------- robotsRunning=$robotsRunning url=$url"
684     if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
685         return -1
686     }
687     if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
688         set port 80
689         set host $hostport
690     }
691     set URL($url,method) $method
692     set URL($url,host) $host
693     set URL($url,hostport) $hostport
694     set URL($url,path) $path
695     set URL($url,state) head
696     set URL($url,buf) {}
697
698     if {[string compare $path /robots.txt]} {
699         set ok 1
700         if {![info exists URL($hostport,robots)]} {
701             puts "READING robots.txt for host $hostport"
702             if {[RobotFileExist visited $hostport /robots.txt]} {
703                 set inf [RobotFileOpen visited $hostport /robots.txt r]
704                 set buf [read $inf 32768]
705                 close $inf
706             } else {
707                 set buf "User-Agent: *\nAllow: /\n"
708             }
709             RobotsTxt0 URL($hostport,robots) $buf
710         }
711         if {[info exists URL($hostport,robots)]} {
712             foreach l $URL($hostport,robots) {
713                 if {[string first [lindex $l 1] $path] == 0} {
714                     set ok [lindex $l 0]
715                     break
716                 }
717             }
718         }
719         if {!$ok} {
720             return -1
721         }
722     }
723     if [catch {set sock [socket -async $host $port]}] {
724         return -1
725     }
726     RobotConnect $url $sock
727
728     return 0
729 }
730
731 if {![llength [info commands htmlSwitch]]} {
732     set e [info sharedlibextension]
733     if {[catch {load ./tclrobot$e}]} {
734         load tclrobot$e
735     }
736 }
737
738 set agent "zmbot/0.0"
739 if {![catch {set os [exec uname -s -r]}]} {
740     set agent "$agent ($os)"
741 }
742
743 puts "agent: $agent"
744
745 proc bgerror {m} {
746     global errorInfo
747     puts "BGERROR $m"
748     puts $errorInfo
749 }
750
751 set robotsRunning 0
752 set robotSeq 0
753 set workdir [pwd]
754 set idleTime 60000
755
756 set i 0
757 set l [llength $argv]
758
759 if {$l < 2} {
760     puts {tclrobot: usage [-j jobs] [-c count] [-d domain] [url ..]}
761     puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
762     exit 1
763 }
764
765 while  {$i < $l} {
766     set arg [lindex $argv $i]
767     switch -glob -- $arg {
768         -j* {
769             set robotsMax [string range $arg 2 end]
770             if {![string length $robotsMax]} {
771                 set robotsMax [lindex $argv [incr i]]
772             }
773         }
774         -c* {
775             set maxDistance [string range $arg 2 end]
776             if {![string length $maxDistance]} {
777                 set maxDistance [lindex $argv [incr i]]
778             }
779         }
780         -d* {
781             set dom [string range $arg 2 end]
782             if {![string length $dom]} {
783                 set dom [lindex $argv [incr i]]
784             }
785             lappend domains $dom
786         }
787         default {
788             set href $arg
789             if {[RobotHref http://www.indexdata.dk/ href host path]} {
790                 if {![RobotFileExist visited $host $path]} {
791                     set outf [RobotFileOpen unvisited $host $path]
792                     RobotWriteRecord $outf href 0
793                     RobotFileClose $outf
794                 }
795             }
796         }
797     }
798     incr i
799 }
800
801 if {![info exist domains]} {
802     set domains {*}
803 }
804 if {![info exist maxDistance]} {
805     set maxDistance 3
806 }
807 if {![info exist robotsMax]} {
808     set robotsMax 5
809 }
810
811 puts "domains=$domains"
812 puts "max distance=$maxDistance"
813 puts "max jobs=$robotsMax"
814
815 RobotStart
816
817 while {$robotsRunning} {
818     vwait robotsRunning
819 }