Previus/next scan buttons in scan.
[egate.git] / www / z39util.tcl
1 #
2 # $Id: z39util.tcl,v 1.16 1996/01/03 08:59:45 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 $url
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 i} {
365     global targets
366
367     set term [wform entry$i]
368     if {$term != ""} {
369         set field [wform menu$i]
370         foreach x [lindex $targets($t) 2] {
371             if {[lindex $x 0] == $field} {
372                 set attr [lindex $x 1]
373             }
374         }
375         return [list $term $attr]
376     }
377     return ""
378 }
379
380 proc build-query {t ilines} {
381     global targets
382
383     set op {}
384     set q {}
385     for {set i 1} {$i <= $ilines} {incr i} {
386         set term [wform entry$i]
387         if {$term != ""} {
388             set field [wform menu$i]
389             foreach x [lindex $targets($t) 2] {
390                 if {[lindex $x 0] == $field} {
391                     set attr [lindex $x 1]
392                 }
393             }
394             switch $op {
395             And
396                 { set q "@and $q ${attr} ${term}" }
397             Or
398                 { set q "@or $q ${attr} ${term}" }
399             {And not}
400                 { set q "@not $q ${attr} ${term}" }
401             {}
402                 { set q "${attr} ${term}" }
403             }
404             set op [wform logic$i]
405         }
406     }
407     return $q
408 }
409
410 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
411     global hist
412     global sessionWait
413     global targets
414
415     if {$tno > 0} {
416         set zz z39$tno
417         set host $hist($setNo,$tno,host)
418         set idAuth $hist($setNo,$tno,idAuthentication)
419         set database $hist($setNo,$tno,database)
420         set scanAttr $hist($setNo,$tno,scanAttr)
421         set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
422     } else {
423         set zz z39
424         set host $hist($setNo,host)
425         set idAuth $hist($setNo,idAuthentication)
426         set database $hist($setNo,database)
427         set scanAttr $hist($setNo,scanAttr)
428         set scanTerm $hist($setNo,$scanNo,scanTerm)
429     }
430     if {[catch [list $zz failback fail-response]]} {
431         ir $zz
432     }
433     if {[catch [list set oldHost [$zz connect]]]} {
434         set oldHost ""
435     }
436     set zs $zz.s$scanNo.$setNo
437     $zz callback ok-response
438     $zz failback fail-response
439     if {$oldHost != $host} {
440         catch [list $zz disconnect]
441
442         set sessionWait 0
443         if {[catch [list $zz connect $host]]} {
444             displayError "Cannot connect to target" $host
445             return 0
446         } elseif {$sessionWait == 0} {
447             if {[catch {zwait sessionWait 300}]} {
448                 $zz disconnect
449                 displayError "Cannot connect to target" $host
450                 return 0
451             }
452             if {$sessionWait != 1} {
453                 displayError "Cannot connect to target" $host
454                 return 0
455             }
456         }
457         $zz idAuthentication $idAuth
458         set sessionWait 0
459         if {[catch {$zz init}]} {
460             displayError "Cannot initialize target" $host
461             $zz disconnect
462             return 0
463         }
464         if {[catch {zwait sessionWait 60}]} {
465             displayError "Cannot initialize target" $host
466             $zz disconnect
467             return 0
468         }
469         if {$sessionWait != "1"} {
470             displayError "Cannot initialize target" $host
471             $zz disconnect
472             return 0
473         }
474         if {![$zz initResult]} {
475             set u [$zz userInformationField]
476             $zz disconnect
477             displayError "Cannot initialize target $host" $u
478             return 0
479         }
480     } else {
481         if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
482             return 1
483         }
484     }
485     eval $zz databaseNames $database
486
487     ir-scan $zs $zz
488
489     $zs numberOfTermsRequested $scanLines
490     $zs preferredPositionInResponse $scanPos
491
492     $zz callback [list scan-response $zs]
493
494     set sessionWait 0
495     $zs scan "${scanAttr} ${scanTerm}"
496
497     if {[catch {zwait sessionWait 600}]} {
498         wlog debug "timeout/cancel in scan"
499         displayError "Timeout in scan" {}
500         html "</body></html>\n"
501         $zz disconnect
502         return 0
503     }
504     if {$sessionWait == -1} {
505         displayError "Scan fail" "Connection closed"
506         html "</body></html>\n"
507         $zz disconnect
508     }
509     if {$sessionWait != 1} {
510         return 0
511     }
512     return 1
513 }
514
515 proc display-scan {setNo scanNo tno} {
516     global hist
517     global targets
518     global env
519     global sessionId
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     if {$m > 0} {
530         set t [lindex [$zs scanLine 0] 1]
531         if {$tno > 0} {
532             set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
533         } else {
534             set hist($setNo,[expr $scanNo - 1],scanTerm) $t
535         }
536         set t [lindex [$zs scanLine [expr $m - 1]] 1]
537         if {$tno > 0} {
538             set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
539         } else {
540             set hist($setNo,[expr $scanNo + 1],scanTerm) $t
541         }
542     }
543     for {set i 0} {$i < $m} {incr i} {
544         html {<a href="http:} $env(SCRIPT_NAME)
545         html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
546         html $hist($setNo,scan) + [lindex [$zs scanLine $i] 1] {">}
547         html [lindex [$zs scanLine $i] 1]
548         html {</a>: <em>}
549         html [lindex [$zs scanLine $i] 2]
550         html "</em><br>\n"
551     }
552 }
553
554 proc z39search {setNo piggy tno elements} {
555     global hist
556     global sessionWait
557     global targets
558
559     if {$tno > 0} {
560         set zz z39$tno
561         set host $hist($setNo,$tno,host)
562         set idAuth $hist($setNo,$tno,idAuthentication)
563         set database $hist($setNo,$tno,database)
564         set query $hist($setNo,$tno,query)
565     } else {
566         set zz z39
567         set host $hist($setNo,host)
568         set idAuth $hist($setNo,idAuthentication)
569         set database $hist($setNo,database)
570         set query $hist($setNo,query)
571     }
572     if {[catch [list $zz failback fail-response]]} {
573         ir $zz
574     }
575     if {[catch [list set oldHost [$zz connect]]]} {
576         set oldHost ""
577     }
578     $zz callback ok-response
579     $zz failback fail-response
580     if {$oldHost != $host} {
581         catch [list $zz disconnect]
582
583         set sessionWait 0
584         if {[catch [list $zz connect $host]]} {
585             displayError "Cannot connect to target" $host
586             return 0
587         } elseif {$sessionWait == 0} {
588             if {[catch {zwait sessionWait 300}]} {
589                 $zz disconnect
590                 displayError "Cannot connect to target" $host
591                 return 0
592             }
593             if {$sessionWait != 1} {
594                 displayError "Cannot connect to target" $host
595                 return 0
596             }
597         }
598         $zz idAuthentication $idAuth
599         set sessionWait 0
600         if {[catch {$zz init}]} {
601             displayError "Cannot initialize target" $host
602             $zz disconnect
603             return 0
604         }
605         if {[catch {zwait sessionWait 60}]} {
606             displayError "Cannot initialize target" $host
607             $zz disconnect
608             return 0
609         }
610         if {$sessionWait != "1"} {
611             displayError "Cannot initialize target" $host
612             $zz disconnect
613             return 0
614         }
615         if {![$zz initResult]} {
616             set u [$zz userInformationField]
617             $zz disconnect
618             displayError "Cannot initialize target $host" $u
619             return 0
620         }
621     } else {
622         if {[info exists hist($setNo,hits)] && \
623                 ![catch [list $zz.$setNo smallSetUpperBound 0]]} {
624             return 1
625         }
626         
627     }
628     ir-set $zz.$setNo $zz
629     
630     if {![lindex $targets($host) 5]} {
631         set elements {}
632     }
633     $zz.$setNo smallSetElementSetNames $elements
634     $zz.$setNo mediumSetElementSetNames $elements
635     $zz.$setNo recordElements $elements
636
637     wlog debug "database=$database"
638     eval $zz.$setNo databaseNames $database
639
640     $zz.$setNo preferredRecordSyntax USMARC
641
642     $zz callback [list search-response $zz.$setNo]
643     if {$piggy} {
644         $zz.$setNo largeSetLowerBound 999999
645         $zz.$setNo smallSetUpperBound 0
646         $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
647     } else {
648         $zz.$setNo largeSetLowerBound 2
649         $zz.$setNo smallSetUpperBound 0
650         $zz.$setNo mediumSetPresentNumber 0
651     }
652     set sessionWait 0
653     $zz.$setNo search $query
654
655     if {[catch {zwait sessionWait 600}]} {
656         wlog debug "timeout/cancel in search"
657         displayError "Timeout in search" {}
658         html "</body></html>\n"
659         $zz disconnect
660         return 0
661     }
662         
663     if {$sessionWait == -1} {
664         displayError "Search fail" "Connection closed"
665         html "</body></html>\n"
666         $zz disconnect
667     }
668     if {$sessionWait != 1} {
669         return 0
670     }
671     set hist($setNo,hits) [$zz.$setNo resultCount]
672     return 1
673 }
674
675 proc init-m-response {i} {
676     global zstatus
677     global zleft
678
679     wlog debug "init-m-response"
680
681     set zstatus($i) 1
682     incr zleft -1
683 }
684
685 proc connect-m-response {i} {
686     global zstatus
687     global zleft
688
689     wlog debug "connect-m-response"
690     z39$i callback [list init-m-response $i]
691     if {[catch {z39$i init}]} {
692         set zstatus($i) -1
693         incr zleft -1
694     }
695 }
696
697 proc fail-m-response {i} {
698     global zstatus
699     global zleft
700     
701     wlog debug "fail-m-response"
702     set zstatus($i) -1
703     incr zleft -1
704 }
705
706 proc search-m-response {setNo i} {
707     global zleft
708     global zstatus
709
710     incr zleft -1
711     set zstatus($i) 2
712 }
713
714 proc z39msearch {setNo piggy elements} {
715     global zleft
716     global zstatus
717     global hist
718     global targets
719
720     set not $hist($setNo,0,host)
721
722     for {set i 1} {$i <= $not} {incr i} {
723         set host $hist($setNo,$i,host)
724         if {[catch {z39 failback fail-response}]} {
725             ir z39$i
726         }
727         if {[catch {set oldHost [z39$i connect]}]} {
728             set oldHost ""
729         }
730         if {$oldHost != $host} {
731             catch {z39$i disconnect}
732         }
733         z39$i callback [list connect-m-response $i]
734         z39$i failback [list fail-m-response $i]
735     }
736     set zleft 0
737     for {set i 1} {$i <= $not} {incr i} {
738         set oldHost [z39$i connect]
739         set host $hist($setNo,$i,host)
740         if {$oldHost == $host} {
741             set zstatus($i) 1
742             continue
743         }
744         z39$i idAuthentication $hist($setNo,$i,idAuthentication)
745         html "Connecting to target " $host " <br>\n"
746         set zstatus($i) -1
747         if {![catch {z39$i connect $host}]} {
748             incr zleft
749         } 
750     }
751     while {$zleft > 0} {
752         wlog debug "Waiting for init response"
753         if {[catch {zwait zleft 10}]} {
754             break
755         }
756     }
757     set zleft 0
758     for {set i 1} {$i <= $not} {incr i} {
759         html "host " $hist($setNo,$i,host) ": "
760         if {$zstatus($i) >= 1} {
761             html "ok <br>\n"
762             ir-set z39$i.$setNo z39$i
763             set hist($setNo,$i,offset) 0
764             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
765
766             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
767                 set thisElements {}
768             } else {
769                 set thisElements $elements
770             }
771             z39$i.$setNo smallSetElementSetNames $thisElements
772             z39$i.$setNo mediumSetElementSetNames $thisElements
773             z39$i.$setNo recordElements $thisElements
774
775             z39$i.$setNo preferredRecordSyntax USMARC
776             z39$i callback [list search-m-response $setNo $i]
777
778             if {$piggy} {
779                 z39$i.$setNo largeSetLowerBound 999999
780                 z39$i.$setNo smallSetUpperBound 0
781                 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
782             } else {
783                 z39$i.$setNo largeSetLowerBound 2
784                 z39$i.$setNo smallSetUpperBound 0
785                 z39$i.$setNo mediumSetPresentNumber 0
786             }
787             set zstatus($i) 1
788             wlog debug "search " $hist($setNo,$i,query)
789             z39$i.$setNo search $hist($setNo,$i,query)
790             incr zleft
791         } else {
792             html "fail <br>\n"
793         }
794     }
795     while {$zleft > 0} {
796         wlog debug "Waiting for search response"
797         if {[catch {zwait zleft 30}]} {
798             break
799         }
800     }
801     for {set i 1} {$i <= $not} {incr i} {
802         if {$zstatus($i) != 2} continue
803         set status [z39$i.$setNo responseStatus]
804         if {[lindex $status 0] != "NSD"} {
805             set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
806         }
807     }
808 }
809
810 proc z39present {setNo tno setOffset setMax dfunc elements} {
811     global hist
812     global sessionWait
813     global targets
814
815     if {$tno > 0} {
816         set zz z39$tno
817         set host $hist($setNo,$tno,host)
818     } else {
819         set zz z39
820         set host $hist($setNo,host)
821     }
822
823     if {![lindex $targets($host) 5]} {
824         set elements {}
825     }
826
827     $zz.$setNo elementSetNames $elements
828     $zz.$setNo recordElements $elements
829     set toGet [expr 1 + $setMax - $setOffset]
830
831     $zz callback [list search-response $zz.$setNo]
832
833     while {$setMax > 0 && $toGet > 0} {
834         for {set got 0} {$got < $toGet} {incr got} {
835             if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
836                 break
837             }
838         }
839         if {$got < $toGet} {
840             set sessionWait 0
841             $zz.$setNo present $setOffset $toGet
842             if {[catch {zwait sessionWait 300}]} {
843                 wlog debug "timeout/cancel in present"
844                 $zz disconnect
845                 break
846             }
847             if {$sessionWait == "0"} {
848                 $zz disconnect
849             }
850             if {$sessionWait != "1"} {
851                 break
852             }
853             set got [$zz.$setNo numberOfRecordsReturned]
854             if {$got <= 0} {
855                 break
856             }
857         }
858         display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
859         set setOffset [expr $got + $setOffset]
860         set toGet [expr 1 + $setMax - $setOffset]
861         wflush
862     }
863 }
864
865 proc z39history {} {
866     global nextSetNo
867     global hist
868     global env
869     global sessionId
870     global targets
871
872     if {![info exists nextSetNo]} {
873         return
874     }
875     html "<hr><h2>History</h2><dl><br>\n"
876     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
877         html {<dt> <a href="http:} $env(SCRIPT_NAME)
878         html / $sessionId {/search.egw/} $setNo + 1
879         html + $hist($setNo,maxPresent)
880         html {"> } [lindex $targets($hist($setNo,host)) 0]
881         if {[llength $hist($setNo,database)] > 1} {
882             html ": "
883             foreach b $hist($setNo,database) {
884                 html " $b"
885             }
886         }
887         html "</a>\n"
888         html "<dd> "
889         if {[info exists hist($setNo,hits)]} {
890             html $hist($setNo,hits) " hits"
891         } else {
892             html failed
893         }
894         html "\n"
895     }
896     html "</dl>\n"
897 }
898
899 proc displayError {msga msgb} {
900     html "<p><center>\n"
901     html {<img src="/gif/noway.gif" alt="Error">}
902     html "<h2>" $msga "</h2>\n"
903     if {$msgb != ""} {
904         html "<h3>" $msgb "</h3>\n"
905     }
906     html "</center><p>\n"
907 }
908
909 set useIcons 1