Work on scan.
[egate.git] / www / z39util.tcl
1 #
2 # $Id: z39util.tcl,v 1.13 1995/12/21 15:49:53 adam Exp $
3 #
4 proc saveState {} {
5     uplevel #0 {
6     set f [open "tcl.state.${sessionId}" w]
7     foreach var [info globals] {
8         if {$var == "f"} continue
9         if {$var == "sessionId"} continue
10         if {$var == "errorInfo"} continue
11         if {[catch {set names [array names $var]}]} {
12             eval "set v \$${var}"
13             puts $f "set ${var} \{$v\}"
14         } else {
15             foreach n $names {
16                 eval "set v \$${var}(\$n)"
17                 puts $f "set ${var}($n) \{$v\}"
18             }
19             catch {
20                 eval "set v \$${var}"
21                 puts $f "set ${var} \{$v\}"
22             }
23         }
24     }
25     close $f
26     }
27 }
28
29 proc search-response {zz} {
30     global sessionWait
31
32     set status [$zz responseStatus]
33     if {[lindex $status 0] == "NSD"} {
34         $zz nextResultSetPosition 0
35         set code [lindex $status 1]
36         set msg [lindex $status 2]
37         set addinfo [lindex $status 3]
38         displayError "Diagnostic message" \
39                 "$msg: $addinfo<br>\n(error code $code)"
40         set sessionWait -2
41     } else {
42         set sessionWait 1
43     }
44 }
45
46 proc scan-response {zz} {
47     global sessionWait
48
49     set status [$zz scanStatus]
50     if {$status == 6} {
51         displayError "Scan fail" ""
52         set sessionWait -2
53     } else {
54         set sessionWait 1
55     }
56 }
57
58 proc ok-response {} {
59     global sessionWait
60     set sessionWait 1
61 }
62
63 proc fail-response {} {
64     global sessionWait
65     set sessionWait -1
66 }
67
68 proc display-brief {zset no tno} {
69     global env
70     global setNo
71     global sessionId
72
73     set type [$zset type $no]
74     if {$type == "SD"} {
75         set err [lindex [$zset diag $no] 1]
76         set add [lindex [$zset diag $no] 2]
77         if {$add != {}} {
78             set add " :${add}"
79         }
80         html "${no} Error ${err}${add} <br>\n"
81         return
82     }
83     if {$type != "DB"} {
84         return
85     }
86     html "${no}"
87     set rtype [$zset recordType $no]
88     if {$rtype == "SUTRS"} {
89         html [join [$zset getSutrs $no]]
90         html "<br>\n"
91         return
92     } 
93     if {![catch {
94         set title [lindex [$zset getMarc $no field 245 * a] 0]
95         set year [lindex [$zset getMarc $no field 260 * c] 0]
96     } ] } {
97         html {<a href="http:} $env(SCRIPT_NAME) /
98         html $sessionId {/showfull.egw/} $setNo + $tno + $no + full 
99         html {"> } $title {</a>} " <i> ${year} </i>"
100     }
101     html "<br>\n"
102 }
103
104 proc display-raw {zset no tno} {
105     set type [$zset type $no]
106     if {$type == "SD"} {
107         set err [lindex [$zset diag $no] 1]
108         set add [lindex [$zset diag $no] 2]
109         if {$add != {}} {
110             set add " :${add}"
111         }
112         html "<h3>${no}</h3>\n"
113         html "Error ${err}${add} <br>\n"
114         return
115     }
116     if {$type != "DB"} {
117         return
118     }
119     set rtype [$zset recordType $no]
120     if {$rtype == "SUTRS"} {
121         html [join [$zset getSutrs $no]] "<br>\n"
122         return
123     } 
124     if {[catch {set r [$zset getMarc $no line * * *]}]} {
125         html "Unknown record type: $rtype <br>\n"
126         return
127     }
128     foreach line $r {
129         set tag [lindex $line 0]
130         set indicator [lindex $line 1]
131         set fields [lindex $line 2]
132         set l [string length $indicator]
133         html "<tt>$tag "
134         if {$l > 0} {
135             for {set i 0} {$i < $l} {incr i} {
136                 if {[string index $indicator $i] == " "} {
137                     html "-"
138                 } else {
139                     html [string index $tag $i]
140                 }
141             }
142         }
143         html "</tt>"
144         foreach field $fields {
145             set id [lindex $field 0]
146             set data [lindex $field 1]
147             if {$id != ""} {
148                 html " <b>\$$id</b> "
149             }
150             html $data
151         }
152         htmlr {<br>}
153     }
154 }
155
156 proc put-marc-contents {cc} {
157     set ref ""
158     if {[string first :// $cc] > 0} {
159         foreach urltype {gopher http ftp mailto} {
160             if {[string first ${urltype}:// $cc] == 0} {
161                 set ref $urltype
162                 break
163             }
164         }
165     } 
166     if {$ref != ""} {
167         html {<a href="}
168     }
169     html $cc
170     if {$ref != ""} {
171         html {">} $cc {</a>}
172     }
173 }
174
175 proc dl-marc-field {zset no tag id la lb sep} {
176     set n 0
177     set c [$zset getMarc $no field $tag * $id]
178     set len [llength $c]
179     if {$len == 0} {
180         return 0
181     }
182     if {$len > 1 && "x$lb" != "x"} {
183         html "<dt><b>$lb</b>\n<dd>"
184     } else {
185         html "<dt><b>$la</b>\n<dd>"
186     }
187     foreach cc $c {
188         if {$n > 0} {
189             html $sep
190         }
191         put-marc-contents $cc
192         incr n
193     }
194     return $n
195 }
196
197 proc dd-marc-field {zset no tag id start stop} {
198     set n 0
199     set c [$zset getMarc $no field $tag * $id]
200     set len [llength $c]
201     if {$len == 0} {
202         return 0
203     }
204     foreach cc $c {
205         html $start
206         put-marc-contents $cc
207         html $stop
208         incr n
209     }
210     return $n
211 }
212
213 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
214     set n 0
215     set lines [$zset getMarc $no line $tag * *]
216     foreach line $lines {
217         foreach field [lindex $line 2] {
218             if {$n == 0} {
219                 html "<dt><b>$lead</b>"
220                 html "\n<dd>"
221             }
222             set id [lindex $field 0]
223             if {$id == $startid} {
224                 if {$n > 0} {
225                     html $stop
226                 }
227                 html $start
228                 incr n
229                 html [lindex $field 1]
230             } else {
231                 html $sep
232                 html [lindex $field 1]
233             }
234         }
235     }
236     if {$n > 0} {
237         html $stop
238     }
239 }
240
241 proc display-full {zset no tno} {
242     set type [$zset type $no]
243     if {$type == "SD"} {
244         set err [lindex [$zset diag $no] 1]
245         set add [lindex [$zset diag $no] 2]
246         if {$add != {}} {
247             set add " :${add}"
248         }
249         html "Error ${err}${add} <br>\n"
250         return
251     }
252     if {$type != "DB"} {
253         return
254     }
255     set rtype [$zset recordType $no]
256     if {$rtype == "SUTRS"} {
257         html [join [$zset getSutrs $no]] "<br>\n"
258         return
259     } 
260     if {[catch {set r [$zset getMarc $no line * * *]}]} {
261         html "Unknown record type: $rtype <br>\n"
262         return
263     }
264     html "<dl>\n"
265     set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
266     if {$n == 0} {
267         set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
268     }
269     set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
270     if {$n == 0} {
271         set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
272     }
273     set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
274     if {$n > 0} {
275         dd-marc-field $zset $no 245 b "<em>" "</em>"
276         dd-marc-field $zset $no 245 c " " ""
277     } else {
278         dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
279     }
280     dl-marc-field $zset $no 520 a "Abstract" {} ", "
281     dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
282     dl-marc-field $zset $no 650 * "Subject" {} ", "
283     dl-marc-field $zset $no 260 * "Publisher" {} " "
284     dl-marc-field $zset $no 300 * "Physical Description" {} " "
285
286     dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
287
288     dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
289
290     dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
291
292     dl-marc-field $zset $no 513 a "Report type" {} ", "
293     dl-marc-field $zset $no 513 b "Period covered" {} ", "
294     dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
295     dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
296     dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
297
298     dl-marc-field $zset $no 350 a "Price" {} ", "
299     dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
300     dl-marc-field $zset $no 850 a "Holdings" {} ", "
301
302     dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
303     if {0} {
304         set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
305         if {$n > 0} {
306             html "\n<dl>\n"
307             
308             if {0} {
309                 dl-marc-field $zset $no 270 a "Street" {} ", "
310                 dl-marc-field $zset $no 270 b "City" {} ", "
311                 dl-marc-field $zset $no 270 c "State" {} ", "
312                 dl-marc-field $zset $no 270 e "Zip code" {} ", "
313                 dl-marc-field $zset $no 270 d "Country" {} ", "
314                 dl-marc-field $zset $no 270 m "Network address" {} ", "
315                 dl-marc-field $zset $no 301 a "Service hours" {} ", "
316                 dl-marc-field $zset $no 270 k "Phone" {} ", "
317                 dl-marc-field $zset $no 270 l "Fax" {} ", "
318             } else {
319                 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
320             }
321             
322             html "\n</dl>\n"
323         }
324     }
325     dl-marc-field $zset $no 010 a "LC control number" {} ", "
326     dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
327     dl-marc-field $zset $no 020 a "ISBN" {} ", "
328     dl-marc-field $zset $no 022 a "ISSN" {} ", "
329     set url [$zset getMarc $no field 856 * u]
330     set sp [$zset getMarc $no field 856 * 3]
331     if {"x$url" != "x"} {
332         html "<dt><b>URL</b>\n"
333         if {"x$sp" == "x"} {
334             set sp reference
335         }
336         html {<dd><a href="} $url {">} [join $sp] "</a>\n"
337     }
338     dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
339     dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
340     dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
341     dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
342     dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
343     dl-marc-field $zset $no 001 * "Local control number" {} ", "
344     html "</dl>\n"
345 }
346
347
348 proc display-rec {from to dfunc tno} {
349     global setNo
350
351     if {$tno > 0} {
352         while {$from <= $to} { 
353             eval "$dfunc z39${tno}.${setNo} $from $tno"
354             incr from
355         }
356     } else {
357         while {$from <= $to} { 
358             eval "$dfunc z39.${setNo} $from 0"
359             incr from
360         }
361     }
362 }
363
364 proc build-scan {t ilines} {
365     global targets
366
367     for {set i 1} {$i <= $ilines} {incr i} {
368         set term [wform entry$i]
369         if {$term != ""} {
370             set field [wform menu$i]
371             foreach x [lindex $targets($t) 2] {
372                 if {[lindex $x 0] == $field} {
373                     set attr [lindex $x 1]
374                 }
375             }
376             return [list $term $attr]
377         }
378     }
379     return ""
380 }
381
382 proc build-query {t ilines} {
383     global targets
384
385     set op {}
386     set q {}
387     for {set i 1} {$i <= $ilines} {incr i} {
388         set term [wform entry$i]
389         if {$term != ""} {
390             set field [wform menu$i]
391             foreach x [lindex $targets($t) 2] {
392                 if {[lindex $x 0] == $field} {
393                     set attr [lindex $x 1]
394                 }
395             }
396             switch $op {
397             And
398                 { set q "@and $q ${attr} ${term}" }
399             Or
400                 { set q "@or $q ${attr} ${term}" }
401             {And not}
402                 { set q "@not $q ${attr} ${term}" }
403             {}
404                 { set q "${attr} ${term}" }
405             }
406             set op [wform logic$i]
407         }
408     }
409     return $q
410 }
411
412 proc z39scan {setNo scanNo tno scanLines scanPos} {
413     global hist
414     global sessionWait
415     global targets
416
417     if {$tno > 0} {
418         set zz z39$tno
419         set host $hist($setNo,$tno,host)
420         set idAuth $hist($setNo,$tno,idAuthentication)
421         set database $hist($setNo,$tno,database)
422         set scanAttr $hist($setNo,$tno,scanAttr)
423         set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
424     } else {
425         set zz z39
426         set host $hist($setNo,host)
427         set idAuth $hist($setNo,idAuthentication)
428         set database $hist($setNo,database)
429         set scanAttr $hist($setNo,scanAttr)
430         set scanTerm $hist($setNo,$scanNo,scanTerm)
431     }
432     if {[catch [list $zz failback fail-response]]} {
433         ir $zz
434     }
435     if {[catch [list set oldHost [$zz connect]]]} {
436         set oldHost ""
437     }
438     set zs $zz.s$scanNo.$setNo
439     $zz callback ok-response
440     $zz failback fail-response
441     if {$oldHost != $host} {
442         catch [list $zz disconnect]
443
444         set sessionWait 0
445         if {[catch [list $zz connect $host]]} {
446             displayError "Cannot connect to target" $host
447             return 0
448         } elseif {$sessionWait == 0} {
449             if {[catch {zwait sessionWait 300}]} {
450                 $zz disconnect
451                 displayError "Cannot connect to target" $host
452                 return 0
453             }
454             if {$sessionWait != 1} {
455                 displayError "Cannot connect to target" $host
456                 return 0
457             }
458         }
459         $zz idAuthentication $idAuth
460         set sessionWait 0
461         if {[catch {$zz init}]} {
462             displayError "Cannot initialize target" $host
463             $zz disconnect
464             return 0
465         }
466         if {[catch {zwait sessionWait 60}]} {
467             displayError "Cannot initialize target" $host
468             $zz disconnect
469             return 0
470         }
471         if {$sessionWait != "1"} {
472             displayError "Cannot initialize target" $host
473             $zz disconnect
474             return 0
475         }
476         if {![$zz initResult]} {
477             set u [$zz userInformationField]
478             $zz disconnect
479             displayError "Cannot initialize target $host" $u
480             return 0
481         }
482     } else {
483         if {![catch [list $zs numberOfTermsRequested 5]]} {
484             return 1
485         }
486     }
487     eval $zz databaseNames $database
488
489     ir-scan $zs $zz
490
491     $zs numberOfTermsRequested $scanLines
492     $zs preferredPositionInResponse $scanPos
493
494     $zz callback [list scan-response $zs]
495
496     set sessionWait 0
497     $zs scan "${scanAttr} ${scanTerm}"
498
499     if {[catch {zwait sessionWait 600}]} {
500         wlog debug "timeout/cancel in scan"
501         displayError "Timeout in scan" {}
502         html "</body></html>\n"
503         $zz disconnect
504         return 0
505     }
506     if {$sessionWait == -1} {
507         displayError "Scan fail" "Connection closed"
508         html "</body></html>\n"
509         $zz disconnect
510     }
511     if {$sessionWait != 1} {
512         return 0
513     }
514     return 1
515 }
516
517 proc display-scan {setNo scanNo tno} {
518     global hist
519     global targets
520
521     if {$tno > 0} {
522         set zz z39$tno
523     } else {
524         set zz z39
525     }
526     set zs $zz.s$scanNo.$setNo
527     set m [$zs numberOfEntriesReturned]
528
529     html "<dl>\n"
530     for {set i 0} {$i < $m} {incr i} {
531         html "<dt>"
532         html [lindex [$zs scanLine $i] 1]
533         html ": "
534         html [lindex [$zs scanLine $i] 2]
535         html "\n"
536     }
537     html "</dl>\n"
538 }
539
540 proc z39search {setNo piggy tno elements} {
541     global hist
542     global sessionWait
543     global targets
544
545     if {$tno > 0} {
546         set zz z39$tno
547         set host $hist($setNo,$tno,host)
548         set idAuth $hist($setNo,$tno,idAuthentication)
549         set database $hist($setNo,$tno,database)
550         set query $hist($setNo,$tno,query)
551     } else {
552         set zz z39
553         set host $hist($setNo,host)
554         set idAuth $hist($setNo,idAuthentication)
555         set database $hist($setNo,database)
556         set query $hist($setNo,query)
557     }
558     if {[catch [list $zz failback fail-response]]} {
559         ir $zz
560     }
561     if {[catch [list set oldHost [$zz connect]]]} {
562         set oldHost ""
563     }
564     $zz callback ok-response
565     $zz failback fail-response
566     if {$oldHost != $host} {
567         catch [list $zz disconnect]
568
569         set sessionWait 0
570         if {[catch [list $zz connect $host]]} {
571             displayError "Cannot connect to target" $host
572             return 0
573         } elseif {$sessionWait == 0} {
574             if {[catch {zwait sessionWait 300}]} {
575                 $zz disconnect
576                 displayError "Cannot connect to target" $host
577                 return 0
578             }
579             if {$sessionWait != 1} {
580                 displayError "Cannot connect to target" $host
581                 return 0
582             }
583         }
584         $zz idAuthentication $idAuth
585         set sessionWait 0
586         if {[catch {$zz init}]} {
587             displayError "Cannot initialize target" $host
588             $zz disconnect
589             return 0
590         }
591         if {[catch {zwait sessionWait 60}]} {
592             displayError "Cannot initialize target" $host
593             $zz disconnect
594             return 0
595         }
596         if {$sessionWait != "1"} {
597             displayError "Cannot initialize target" $host
598             $zz disconnect
599             return 0
600         }
601         if {![$zz initResult]} {
602             set u [$zz userInformationField]
603             $zz disconnect
604             displayError "Cannot initialize target $host" $u
605             return 0
606         }
607     } else {
608         if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
609             return 1
610         }
611     }
612     ir-set $zz.$setNo $zz
613     
614     if {![lindex $targets($host) 5]} {
615         set elements {}
616     }
617     $zz.$setNo smallSetElementSetNames $elements
618     $zz.$setNo mediumSetElementSetNames $elements
619     $zz.$setNo recordElements $elements
620
621     wlog debug "database=$database"
622     eval $zz.$setNo databaseNames $database
623
624     $zz.$setNo preferredRecordSyntax USMARC
625
626     $zz callback [list search-response $zz.$setNo]
627     if {$piggy} {
628         $zz.$setNo largeSetLowerBound 999999
629         $zz.$setNo smallSetUpperBound 0
630         $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
631     } else {
632         $zz.$setNo largeSetLowerBound 2
633         $zz.$setNo smallSetUpperBound 0
634         $zz.$setNo mediumSetPresentNumber 0
635     }
636     set sessionWait 0
637     $zz.$setNo search $query
638
639     if {[catch {zwait sessionWait 600}]} {
640         wlog debug "timeout/cancel in search"
641         displayError "Timeout in search" {}
642         html "</body></html>\n"
643         $zz disconnect
644         return 0
645     }
646         
647     if {$sessionWait == -1} {
648         displayError "Search fail" "Connection closed"
649         html "</body></html>\n"
650         $zz disconnect
651     }
652     if {$sessionWait != 1} {
653         return 0
654     }
655     set hist($setNo,hits) [$zz.$setNo resultCount]
656     return 1
657 }
658
659 proc init-m-response {i} {
660     global zstatus
661     global zleft
662
663     wlog debug "init-m-response"
664
665     set zstatus($i) 1
666     incr zleft -1
667 }
668
669 proc connect-m-response {i} {
670     global zstatus
671     global zleft
672
673     wlog debug "connect-m-response"
674     z39$i callback [list init-m-response $i]
675     if {[catch {z39$i init}]} {
676         set zstatus($i) -1
677         incr zleft -1
678     }
679 }
680
681 proc fail-m-response {i} {
682     global zstatus
683     global zleft
684     
685     wlog debug "fail-m-response"
686     set zstatus($i) -1
687     incr zleft -1
688 }
689
690 proc search-m-response {setNo i} {
691     global zleft
692     global zstatus
693
694     incr zleft -1
695     set zstatus($i) 2
696 }
697
698 proc z39msearch {setNo piggy elements} {
699     global zleft
700     global zstatus
701     global hist
702     global targets
703
704     set not $hist($setNo,0,host)
705
706     for {set i 1} {$i <= $not} {incr i} {
707         set host $hist($setNo,$i,host)
708         if {[catch {z39 failback fail-response}]} {
709             ir z39$i
710         }
711         if {[catch {set oldHost [z39$i connect]}]} {
712             set oldHost ""
713         }
714         if {$oldHost != $host} {
715             catch {z39$i disconnect}
716         }
717         z39$i callback [list connect-m-response $i]
718         z39$i failback [list fail-m-response $i]
719     }
720     set zleft 0
721     for {set i 1} {$i <= $not} {incr i} {
722         set oldHost [z39$i connect]
723         set host $hist($setNo,$i,host)
724         if {$oldHost == $host} {
725             set zstatus($i) 1
726             continue
727         }
728         z39$i idAuthentication $hist($setNo,$i,idAuthentication)
729         html "Connecting to target " $host " <br>\n"
730         set zstatus($i) -1
731         if {![catch {z39$i connect $host}]} {
732             incr zleft
733         } 
734     }
735     while {$zleft > 0} {
736         wlog debug "Waiting for init response"
737         if {[catch {zwait zleft 10}]} {
738             break
739         }
740     }
741     set zleft 0
742     for {set i 1} {$i <= $not} {incr i} {
743         html "host " $hist($setNo,$i,host) ": "
744         if {$zstatus($i) >= 1} {
745             html "ok <br>\n"
746             ir-set z39$i.$setNo z39$i
747             set hist($setNo,$i,offset) 0
748             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
749
750             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
751                 set thisElements {}
752             } else {
753                 set thisElements $elements
754             }
755             z39$i.$setNo smallSetElementSetNames $thisElements
756             z39$i.$setNo mediumSetElementSetNames $thisElements
757             z39$i.$setNo recordElements $thisElements
758
759             z39$i.$setNo preferredRecordSyntax USMARC
760             z39$i callback [list search-m-response $setNo $i]
761
762             if {$piggy} {
763                 z39$i.$setNo largeSetLowerBound 999999
764                 z39$i.$setNo smallSetUpperBound 0
765                 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
766             } else {
767                 z39$i.$setNo largeSetLowerBound 2
768                 z39$i.$setNo smallSetUpperBound 0
769                 z39$i.$setNo mediumSetPresentNumber 0
770             }
771             set zstatus($i) 1
772             wlog debug "search " $hist($setNo,$i,query)
773             z39$i.$setNo search $hist($setNo,$i,query)
774             incr zleft
775         } else {
776             html "fail <br>\n"
777         }
778     }
779     while {$zleft > 0} {
780         wlog debug "Waiting for search response"
781         if {[catch {zwait zleft 30}]} {
782             break
783         }
784     }
785     for {set i 1} {$i <= $not} {incr i} {
786         if {$zstatus($i) != 2} continue
787         set status [z39$i.$setNo responseStatus]
788         if {[lindex $status 0] != "NSD"} {
789             set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
790         }
791     }
792 }
793
794 proc z39present {setNo tno setOffset setMax dfunc elements} {
795     global hist
796     global sessionWait
797     global targets
798
799     if {$tno > 0} {
800         set zz z39$tno
801         set host $hist($setNo,$tno,host)
802     } else {
803         set zz z39
804         set host $hist($setNo,host)
805     }
806
807     if {![lindex $targets($host) 5]} {
808         set elements {}
809     }
810
811     $zz.$setNo elementSetNames $elements
812     $zz.$setNo recordElements $elements
813     set toGet [expr 1 + $setMax - $setOffset]
814
815     $zz callback [list search-response $zz.$setNo]
816
817     while {$setMax > 0 && $toGet > 0} {
818         for {set got 0} {$got < $toGet} {incr got} {
819             if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
820                 break
821             }
822         }
823         if {$got < $toGet} {
824             set sessionWait 0
825             $zz.$setNo present $setOffset $toGet
826             if {[catch {zwait sessionWait 300}]} {
827                 wlog debug "timeout/cancel in present"
828                 $zz disconnect
829                 break
830             }
831             if {$sessionWait == "0"} {
832                 $zz disconnect
833             }
834             if {$sessionWait != "1"} {
835                 break
836             }
837             set got [$zz.$setNo numberOfRecordsReturned]
838             if {$got <= 0} {
839                 break
840             }
841         }
842         display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
843         set setOffset [expr $got + $setOffset]
844         set toGet [expr 1 + $setMax - $setOffset]
845         wflush
846     }
847 }
848
849 proc z39history {} {
850     global nextSetNo
851     global hist
852     global env
853     global sessionId
854     global targets
855
856     if {![info exists nextSetNo]} {
857         return
858     }
859     html "<hr><h3>History</h3><dl>\n"
860     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
861         html {<dt> <a href="http:} $env(SCRIPT_NAME)
862         html / $sessionId {/search.egw/} $setNo + 1
863         html + $hist($setNo,maxPresent)
864         html {"> } [lindex $targets($hist($setNo,host)) 0]
865         if {[llength $hist($setNo,database)] > 1} {
866             html ": "
867             foreach b $hist($setNo,database) {
868                 html " $b"
869             }
870         }
871         html "</a>\n"
872         html "<dd> "
873         if {[info exists hist($setNo,hits)]} {
874             html $hist($setNo,hits) " hits"
875         } else {
876             html failed
877         }
878         html "\n"
879     }
880     html "</dl>\n"
881 }
882
883 proc displayError {msga msgb} {
884     html "<p><center>\n"
885     html {<img src="/gif/noway.gif">}
886     html "<h2>" $msga "</h2>\n"
887     if {$msgb != ""} {
888         html "<h3>" $msgb "</h3>\n"
889     }
890     html "</center><p>\n"
891 }
892
893 set useIcons 1