Multiple-target searches handles cancel operation now.
[egate.git] / www / z39util.tcl
1 #
2 # $Id: z39util.tcl,v 1.42 1996/03/15 14:43: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-medium {zset no setNo targetNo} {
69     global env
70     global sessionId
71
72     html {<li>}
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     set rtype [$zset recordType $no]
87     switch $rtype {
88         SUTRS {
89             html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
90             html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
91             html [join [$zset getSutrs $no]]
92             html "</a><br>\n"
93             return
94         }
95         WAIS {
96             html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
97             html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
98             html [join [$zset getWAIS $no headline]]
99             html {</a>}
100             html "<br>\n"
101             html {Score: } [$zset getWAIS $no score]
102             set lines [$zset getWAIS $no lines]
103             if {$lines > 0} {
104                 html {, } $lines { lines}
105             }
106             html "<br>\n"
107             return
108         }
109     }
110     if {![catch {
111         set author [$zset getMarc $no field 100 * a]
112         set corp [$zset getMarc $no field 110 * a]
113         set meet [$zset getMarc $no field 111 * a]
114         set title [$zset getMarc $no field 245 * a]
115         if {[llength $author] == 0} {
116             set cover [$zset getMarc $no field 245 * {[bc]}]
117         } else {
118             set cover [$zset getMarc $no field 245 * b]
119         }
120         set location [$zset getMarc $no field 260 * a] 
121         set publisher [$zset getMarc $no field 260 * b]
122         set year [$zset getMarc $no field 260 * c]
123         set score [$zset getMarc $no field 999 * r]
124     } dispError ] } {
125         html { <a href="http:} $env(SCRIPT_NAME) /
126         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
127         set p 0
128         foreach a $author {
129             if {$p} {
130                 html ", "
131             }
132             html $a
133             set p 1
134         }
135         foreach a $corp {
136             if {$p} {
137                 html ", "
138             }
139             html $a
140             set p 1
141         }
142         foreach a $meet {
143             if {$p} {
144                 html ", "
145             }
146             html $a
147             set p 1
148         }
149         if {$p} {
150             html ": "
151         }
152         set nope 1
153         foreach v $title {
154             html $v
155             set nope 0
156         }
157         set v [join $cover ""]
158         if {[string length $v] > 0} {
159             set nope 0
160             html $v
161         } elseif {$nope} {
162             html "No Title"
163         }
164         html {</a> }
165         if {[scan $score %d nscore]} {
166             html "; Score " $nscore
167         }
168     } else {
169         html { <a href="http:} $env(SCRIPT_NAME) /
170         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
171         html {No Title}
172         html {</a> }
173         html "Error: " $dispError "\n"
174     }
175     html "<br>\n"
176 }
177
178 proc display-brief {zset no setNo targetNo} {
179     global env
180     global sessionId
181
182     html {<li>}
183     set type [$zset type $no]
184     if {$type == "SD"} {
185         set err [lindex [$zset diag $no] 1]
186         set add [lindex [$zset diag $no] 2]
187         if {$add != {}} {
188             set add " :${add}"
189         }
190         html "${no} Error ${err}${add} <br>\n"
191         return
192     }
193     if {$type != "DB"} {
194         return
195     }
196     set rtype [$zset recordType $no]
197     switch $rtype {
198         SUTRS {
199             html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
200             html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
201             html [string range [join [$zset getSutrs $no]] 0 70]
202             html "</a><br>\n"
203             return
204         }
205         WAIS {
206             html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
207             html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
208             html [string range [join [$zset getWAIS $no headline]] 0 70]
209             
210             html {</a>}
211             set score [$zset getWAIS $no score]
212             html { Score } $score
213             html "<br>\n"
214             return
215         }
216     }
217     if {![catch {
218         set author [$zset getMarc $no field 100 * a]
219         set corp [$zset getMarc $no field 110 * a]
220         set meet [$zset getMarc $no field 111 * a]
221         set title [$zset getMarc $no field 245 * a]
222         if {[llength $author] == 0} {
223             set cover [$zset getMarc $no field 245 * {[bc]}]
224         } else {
225             set cover [$zset getMarc $no field 245 * b]
226         }
227         set location [$zset getMarc $no field 260 * a] 
228         set publisher [$zset getMarc $no field 260 * b]
229         set year [$zset getMarc $no field 260 * c]
230     } dispError ] } {
231         html { <a href="http:} $env(SCRIPT_NAME) /
232         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
233         set p 0
234         foreach a $author {
235             if {$p} {
236                 html ", "
237             }
238             html $a
239             set p 1
240         }
241         foreach a $corp {
242             if {$p} {
243                 html ", "
244             }
245             html $a
246             set p 1
247         }
248         foreach a $meet {
249             if {$p} {
250                 html ", "
251             }
252             html $a
253             set p 1
254         }
255         if {$p} {
256             html ": "
257         }
258         html {<it>}
259         set nope 1
260         foreach v $title {
261             html $v
262             set nope 0
263         }
264         html {</it>}
265         if {$nope} {
266             set v [join $cover ""]
267             if {[string length $v] > 40} {
268                 set nope 0
269                 html [string range $v 0 38] "..."
270             } elseif {[string length $v] > 0} {
271                 set nope 0
272                 html $v
273             } else {
274                 html "No Title"
275             }
276         }
277         html {</a> }
278     } else {
279         html { <a href="http:} $env(SCRIPT_NAME) /
280         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
281         html {No Title}
282         html {</a> }
283         html "Error: " $dispError "\n"
284     }
285     html "<br>\n"
286 }
287
288 proc display-raw {zset no setNo targetNo} {
289     set type [$zset type $no]
290     switch $type {
291         SD {
292             set err [lindex [$zset diag $no] 1]
293             set add [lindex [$zset diag $no] 2]
294             if {$add != {}} {
295                 set add " :${add}"
296             }
297             html "<h3>${no}</h3>\n"
298             html "Error ${err}${add} <br>\n"
299             return
300         }
301         DB {
302         }
303         default {
304             return
305         }
306     }
307     set rtype [$zset recordType $no]
308     switch $rtype {
309         SUTRS {
310             html "<xmp>\n" [join [$zset getSutrs $no]] "\n</xmp>\n"
311             return
312         } 
313         WAIS {
314             html "<xmp>\n" [join [$zset getWAIS $no text]] "\n</xmp>\n"
315             return
316         }
317     }
318     if {[catch {set r [$zset getMarc $no line * * *]}]} {
319         html "Unknown record type: $rtype <br>\n"
320         return
321     }
322     foreach line $r {
323         set tag [lindex $line 0]
324         set indicator [lindex $line 1]
325         set fields [lindex $line 2]
326         set l [string length $indicator]
327         html "<tt>$tag "
328         if {$l > 0} {
329             for {set i 0} {$i < $l} {incr i} {
330                 if {[string index $indicator $i] == " "} {
331                     html "-"
332                 } else {
333                     html [string index $tag $i]
334                 }
335             }
336         }
337         html "</tt>"
338         foreach field $fields {
339             set id [lindex $field 0]
340             set data [lindex $field 1]
341             if {$id != ""} {
342                 html " <b>\$$id</b> "
343             }
344             html $data
345         }
346         html "<br>\n"
347     }
348 }
349
350 proc put-marc-contents {cc} {
351     set ref ""
352     if {[string first :// $cc] > 0} {
353         foreach urltype {gopher http ftp mailto} {
354             if {[string first ${urltype}:// $cc] == 0} {
355                 set ref $urltype
356                 break
357             }
358         }
359     } 
360     if {$ref != ""} {
361         html {<a href="}
362     }
363     html $cc
364     if {$ref != ""} {
365         html {">} $cc {</a>}
366     }
367 }
368
369 proc dl-marc-field {zset no tag id la lb sep} {
370     set n 0
371     set c [$zset getMarc $no field $tag * $id]
372     set len [llength $c]
373     if {$len == 0} {
374         return 0
375     }
376     if {$len > 1 && "x$lb" != "x"} {
377         html "<dt><b>$lb</b>\n<dd>"
378     } else {
379         html "<dt><b>$la</b>\n<dd>"
380     }
381     foreach cc $c {
382         if {$n > 0} {
383             html $sep
384         }
385         put-marc-contents $cc
386         incr n
387     }
388     return $n
389 }
390
391 proc dd-marc-field {zset no tag id start stop} {
392     set n 0
393     set c [$zset getMarc $no field $tag * $id]
394     set len [llength $c]
395     if {$len == 0} {
396         return 0
397     }
398     foreach cc $c {
399         html $start
400         put-marc-contents $cc
401         html $stop
402         incr n
403     }
404     return $n
405 }
406
407 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
408     set n 0
409     set lines [$zset getMarc $no line $tag * *]
410     foreach line $lines {
411         foreach field [lindex $line 2] {
412             if {$n == 0} {
413                 html "<dt><b>$lead</b>"
414                 html "\n<dd>"
415             }
416             set id [lindex $field 0]
417             if {$id == $startid} {
418                 if {$n > 0} {
419                     html $stop
420                 }
421                 html $start
422                 incr n
423                 html [lindex $field 1]
424             } else {
425                 html $sep
426                 html [lindex $field 1]
427             }
428         }
429     }
430     if {$n > 0} {
431         html $stop
432     }
433 }
434
435 proc display-full-marc {zset no setNo targetNo} {
436     global env
437     global hist
438     global sessionId
439
440     html "<dl>\n"
441     set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
442     if {$n == 0} {
443         set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
444     }
445     set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
446     if {$n == 0} {
447         set n [dl-marc-field $zset $no 110 a "Corporate Name" {} ", "]
448     }
449     set n [dl-marc-field $zset $no 711 a "Meeting Name" {} ", "]
450     if {$n > 0} {
451         dd-marc-field $zset $no 711 {[bndc]} " " ""
452     } else {
453         set n [dl-marc-field $zset $no 111 a "Meeting Name" {} ", "]
454         if {$n > 0} {
455             dd-marc-field $zset $no 111 {[bndc]} " " " "
456         }
457     } 
458     set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
459     if {$n > 0} {
460         dd-marc-field $zset $no 245 b "<em>" "</em>"
461         dd-marc-field $zset $no 245 c " " ""
462     } else {
463         dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
464     }
465     dl-marc-field $zset $no 520 a "Abstract" {} ", "
466     dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
467     dl-marc-field $zset $no 650 * "Subject" {} ", "
468     dl-marc-field $zset $no 260 * "Publisher" {} " "
469     dl-marc-field $zset $no 300 * "Physical Description" {} " "
470
471     dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
472
473     dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
474
475     dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
476
477     dl-marc-field $zset $no 513 a "Report type" {} ", "
478     dl-marc-field $zset $no 513 b "Period covered" {} ", "
479     dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
480     dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
481     dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
482
483     dl-marc-field $zset $no 350 a "Price" {} ", "
484     dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
485     dl-marc-field $zset $no 850 a "Holdings" {} ", "
486
487     dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
488     if {0} {
489         set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
490         if {$n > 0} {
491             html "\n<dl>\n"
492             
493             if {0} {
494                 dl-marc-field $zset $no 270 a "Street" {} ", "
495                 dl-marc-field $zset $no 270 b "City" {} ", "
496                 dl-marc-field $zset $no 270 c "State" {} ", "
497                 dl-marc-field $zset $no 270 e "Zip code" {} ", "
498                 dl-marc-field $zset $no 270 d "Country" {} ", "
499                 dl-marc-field $zset $no 270 m "Network address" {} ", "
500                 dl-marc-field $zset $no 301 a "Service hours" {} ", "
501                 dl-marc-field $zset $no 270 k "Phone" {} ", "
502                 dl-marc-field $zset $no 270 l "Fax" {} ", "
503             } else {
504                 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
505             }
506             
507             html "\n</dl>\n"
508         }
509     }
510     dl-marc-field $zset $no 010 a "LC control number" {} ", "
511     dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
512     dl-marc-field $zset $no 020 a "ISBN" {} ", "
513     dl-marc-field $zset $no 022 a "ISSN" {} ", "
514     set url [$zset getMarc $no field 856 * u]
515     set sp [$zset getMarc $no field 856 * 3]
516     if {"x$url" != "x"} {
517         html "<dt><b>URL</b>\n"
518         if {"x$sp" == "x"} {
519             set sp $url
520         }
521         html {<dd><a href="} [join $url] {">} [join $sp] "</a>\n"
522     }
523     dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
524     dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
525     dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
526     dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
527     dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
528     dl-marc-field $zset $no 999 r "Score" {} ", "
529     dl-marc-field $zset $no 001 * "Local control number" {} ", "
530     html "</dl>\n"
531 }
532
533 proc display-full-wais {zset no setNo targetNo} {
534     global env
535     global hist
536     global sessionId
537
538     set i 0    
539     set element junk
540     htmlToken l [join [$zset getWAIS $no text]] {
541         if {[string compare [string index $l 0] {<}]} {
542             if {[info exist data($element)]} {
543                 set data($element) $data($element)$l
544             } else {
545                 set data($element) $l
546             }
547             continue
548         }
549         switch -- $l {
550             <ti> {
551                 set element title 
552             }
553             <dm> {
554                 set element dateOfLastModification
555             }
556             <ci> {
557                 set element controlIdentifier
558             }
559             <lc> {
560                 set element lastChecked
561             }
562             <by> {
563                 set element bytes
564             }
565             <avli> {
566                 set element linkage
567             }
568             <cr> {
569                 incr i
570             }
571             <li> {
572                 set element "$i,linkage"
573             }
574             <cp> {
575                 set element "$i,title"
576             }
577             <ip> {
578                 set element ip
579             }
580             default {
581                 set element junk
582             }
583         }
584     }
585     if {![info exists data(title)] || ![info exists data(linkage)]} {
586         set nwi 0
587     } else {
588         set nwi 1
589     }
590     html "<dl>\n"
591     html {<dt><b>Title</b>}
592     if {$nwi} {
593         html {<dd><a href="} $data(linkage) {">} $data(title) "</a>"
594         html {<dt><b>URL</b>}
595         html {<dd>} $data(linkage) "<br>\n"
596     } else {
597         html {<dd>} [join [$zset getWAIS $no headline]]
598     }
599     html {<dt><b>Score</b><dd>} [$zset getWAIS $no score]
600     set lines [$zset getWAIS $no lines]
601     if {$lines > 0} {
602         html {<dt><b>Lines</b><dd>} $lines "<br>\n"
603     }
604     if {!$nwi} {
605         html "</dl><pre>\n" [join [$zset getWAIS $no text]] "\n</pre>\n"
606         return
607     }
608     if {[info exists data(bytes)]} {
609         html {<dt><b>Bytes</b><dd>} $data(bytes)
610     }
611     if {[info exists data(dateOfLastModification)]} {
612         html {<dt><b>Last modified</b><dd>} $data(dateOfLastModification)
613     }
614     if {[info exists data(lastChecked)]} {
615         html {<dt><b>Last checked</b><dd>} $data(lastChecked) "<br>\n"
616     }
617     if {[info exists data(ip)]} {
618         html {<dt><b>Initial text</b><dd>} $data(ip) "<br>\n"
619     }
620     if {0} {
621         html {<a href="} $env(SCRIPT_NAME) / $sessionId {/sameas.egw/}
622         html $setNo + $targetNo + 1 + $hist($setNo,maxPresent) +
623         html [egw_enc [$zset getWAIS $no documentID]] {">}
624         html {Similar WAIS record</a><br>}
625     }
626     if {[info exists data($i,linkage)]} {
627         html "<dt><b>References</b>\n"
628     }
629     for {set i 1} {[info exists data($i,linkage)]} {incr i} {
630         html {<dd><a href="} $data($i,linkage) {">}
631         if {[info exists data($i,title)]} {
632             html $data($i,title)
633         } else {
634             html Untitled
635         }
636         html "</a><br>\n"
637     }
638     html "</dl>\n"
639 }
640
641 proc display-full {zset no setNo targetNo} {
642     set type [$zset type $no]
643     switch $type {
644         SD {
645             set err [lindex [$zset diag $no] 1]
646             set add [lindex [$zset diag $no] 2]
647             if {$add != {}} {
648                 set add " :${add}"
649             }
650             html "Error ${err}${add} <br>\n"
651             return
652         }
653         DB {
654         }
655         default {
656             return
657         }
658     }
659     set rtype [$zset recordType $no]
660     switch $rtype {
661         SUTRS {
662             html "<pre>" [join [$zset getSutrs $no]] "</pre><br>\n"
663             return
664         }
665         WAIS {
666             display-full-wais $zset $no $setNo $targetNo
667             return
668         }
669     }
670     if {[catch {set r [$zset getMarc $no line * * *]}]} {
671         html "Unknown record type: $rtype <br>\n"
672         return
673     }
674     display-full-marc $zset $no $setNo $targetNo
675 }
676
677
678 proc display-rec {from to dfunc setNo targetNo} {
679     while {$from <= $to} { 
680         eval "$dfunc z39${targetNo}.${setNo} $from $setNo $targetNo"
681         incr from
682     }
683 }
684
685 proc build-scan {t i} {
686     global targets
687
688     set term [egw_form entry$i]
689     if {$term != ""} {
690         set field [join [egw_form menu$i]]
691         set attr {Title}
692         foreach x [lindex $targets($t) 2] {
693             if {[lindex $x 0] == $field} {
694                 set attr [lindex $x 1]
695             }
696         }
697         return [list $term $attr]
698     }
699     return ""
700 }
701
702 proc build-query {t ilines} {
703     global targets
704
705     set op {}
706     set q {}
707     for {set i 1} {$i <= $ilines} {incr i} {
708         set term [join [egw_form entry$i]]
709         if {[string length $term] == 0} continue
710         if {![string compare [lindex $targets($t) 1] WAIS]} {
711             if {[string length $q] == 0} {
712                 set q $term
713             } else {
714                 set q "$term $q"
715             }
716             set op [egw_form logic$i]
717             continue
718         } else {
719             set field [join [egw_form menu$i]]
720             catch {unset attr}
721             foreach x [lindex $targets($t) 2] {
722                 if {![string compare [lindex $x 0] $field]} {
723                     set attr [lindex $x 1]
724                 }
725             }
726             if {![info exists attr]} {
727                 egw_log debug "attr failed for $t"
728                 set attr [lindex [lindex [lindex $targets($t) 2] 0] 1]
729             }
730             switch $op {
731                 And
732                 { set q "@and $q ${attr} \"${term}\"" }
733                 Or
734                 { set q "@or $q ${attr} \"${term}\"" }
735                 {And not}
736                 { set q "@not $q ${attr} \"${term}\"" }
737                 {}
738                 { set q "${attr} \"${term}\"" }
739             }
740             set op [egw_form logic$i]
741         }
742     }
743     return $q
744 }
745
746 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
747     global hist
748     global sessionWait
749     global targets
750
751     set zz z39$tno
752     set host $hist($setNo,$tno,host)
753     set idAuth $hist($setNo,$tno,idAuthentication)
754     set database $hist($setNo,$tno,database)
755     set scanAttr $hist($setNo,scanAttr)
756     set scanTerm $hist($setNo,$scanNo,scanTerm)
757
758     mkAssoc $zz $host
759     if {[catch [list set oldHost [$zz connect]]]} {
760         set oldHost ""
761     }
762     set zs $zz.s$scanNo.$setNo
763     $zz callback ok-response
764     $zz failback fail-response
765     set thisHost [splitHostSpec $host]
766     if {[string compare $oldHost $thisHost]} {
767         catch [list $zz disconnect]
768
769         set sessionWait 0
770         if {[catch [list $zz connect $thisHost]]} {
771             displayError "Cannot connect to target" $thisHost
772             return 0
773         } elseif {$sessionWait == 0} {
774             if {[catch {egw_wait sessionWait 300}]} {
775                 $zz disconnect
776                 displayError "Cannot connect to target" $thisHost
777                 return 0
778             }
779             if {$sessionWait != 1} {
780                 displayError "Cannot connect to target" $thisHost
781                 return 0
782             }
783         }
784         $zz idAuthentication $idAuth
785         set sessionWait 0
786         if {[catch {$zz init}]} {
787             displayError "Cannot initialize target" $thisHost
788             $zz disconnect
789             return 0
790         }
791         if {[catch {egw_wait sessionWait 60}]} {
792             displayError "Cannot initialize target" $thisHost
793             $zz disconnect
794             return 0
795         }
796         if {$sessionWait != "1"} {
797             displayError "Cannot initialize target" $thisHost
798             $zz disconnect
799             return 0
800         }
801         if {![$zz initResult]} {
802             set u [$zz userInformationField]
803             $zz disconnect
804             displayError "Cannot initialize target $thisHost" $u
805             return 0
806         }
807     } else {
808         if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
809             return 1
810         }
811     }
812     eval $zz databaseNames $database
813
814     ir-scan $zs $zz
815
816     $zs numberOfTermsRequested $scanLines
817     $zs preferredPositionInResponse $scanPos
818
819     $zz callback [list scan-response $zs]
820
821     egw_log debug "scan: ${scanAttr} ${scanTerm}"
822     set sessionWait 0
823     $zs scan "${scanAttr} ${scanTerm}"
824
825     if {[catch {egw_wait sessionWait 60}]} {
826         egw_log debug "timeout/cancel in scan"
827         displayError "Timeout in scan" {}
828         html "</body></html>\n"
829         $zz disconnect
830         return 0
831     }
832     if {$sessionWait == -1} {
833         displayError "Scan fail" "Connection closed"
834         html "</body></html>\n"
835         $zz disconnect
836     }
837     if {$sessionWait != 1} {
838         return 0
839     }
840     return 1
841 }
842
843 proc display-scan {setNo scanNo tno} {
844     global hist
845     global targets
846     global env
847     global sessionId
848     global scriptQuery
849
850     set zz z39$tno
851     set zs $zz.s$scanNo.$setNo
852     set m [$zs numberOfEntriesReturned]
853         
854     if {$m > 0} {
855         set t [lindex [$zs scanLine 0] 1]
856         if {$tno > 0} {
857             set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
858         } else {
859             set hist($setNo,[expr $scanNo - 1],scanTerm) $t
860         }
861         set t [lindex [$zs scanLine [expr $m - 1]] 1]
862         if {$tno > 0} {
863             set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
864         } else {
865             set hist($setNo,[expr $scanNo + 1],scanTerm) $t
866         }
867     }
868     html {<table width=500 border=0><tr>}
869     html {<td align=left><b>Scan term</b>}
870     html {<td align=right><b>Hits</b>}
871     html {<tr>} \n
872
873     for {set i 0} {$i < $m} {incr i} {
874         html {<td align=left>}
875         if {0} {
876             regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
877             html {<a href="http:} $env(SCRIPT_NAME)
878             html / $sessionId / $scriptQuery {;/} $setNo + $hist($setNo,host) +
879             html $hist($setNo,scan) +  $tterm {">}
880         } else {
881             regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
882             html {<a href="http:} $env(SCRIPT_NAME)
883             html / $sessionId {/search.egw/} $setNo + $tno +
884             html hyper + $tterm {">}
885         }
886         html [lindex [$zs scanLine $i] 1]
887         html {</a>} 
888         html {<td align=right>}
889         html [lindex [$zs scanLine $i] 2]
890         html {<tr>} \n
891     }
892     html {</table} \n
893 }
894
895 proc z39search {setNo piggy tno elements} {
896     global hist
897     global sessionWait
898     global targets
899
900     set zz z39$tno
901     set host $hist($setNo,$tno,host)
902     set idAuth $hist($setNo,$tno,idAuthentication)
903     set database $hist($setNo,$tno,database)
904     set query $hist($setNo,$tno,query)
905     catch {set docId $hist($setNo,$tno,queryId)}
906
907     mkAssoc $zz $host
908     if {[catch [list set oldHost [$zz connect]]]} {
909         set oldHost ""
910     }
911     $zz callback ok-response
912     $zz failback fail-response
913     set thisHost [splitHostSpec $host]
914     if {[string compare $oldHost $thisHost]} {
915         catch [list $zz disconnect]
916
917         set sessionWait 0
918         if {[catch [list $zz connect $thisHost]]} {
919             displayError "Cannot connect to target" $thisHost
920             return 0
921         } elseif {$sessionWait == 0} {
922             if {[catch {egw_wait sessionWait 300}]} {
923                 $zz disconnect
924                 displayError "Cannot connect to target" $thisHost
925                 return 0
926             }
927             if {$sessionWait != 1} {
928                 displayError "Cannot connect to target" $thisHost
929                 return 0
930             }
931         }
932         $zz idAuthentication $idAuth
933         set sessionWait 0
934         if {[catch {$zz init}]} {
935             displayError "Cannot initialize target" $thisHost
936             $zz disconnect
937             return 0
938         }
939         if {$sessionWait == 0 && [catch {egw_wait sessionWait 60}]} {
940             displayError "Cannot initialize target" $thisHost
941             $zz disconnect
942             return 0
943         }
944         if {$sessionWait != "1"} {
945             displayError "Cannot initialize target" $thisHost
946             $zz disconnect
947             return 0
948         }
949         if {![$zz initResult]} {
950             set u [$zz userInformationField]
951             $zz disconnect
952             displayError "Cannot initialize target $thisHost" $u
953             return 0
954         }
955     } elseif {![catch  [list $zz.$setNo smallSetUpperBound 0]]} {
956         if {[info exists hist($setNo,$tno,hits)]} {
957             return 1
958         }
959     }
960     
961     if {![string compare [lindex $targets($host) 1] WAIS]} {
962         wais-set $zz.$setNo $zz
963     } else {
964         ir-set $zz.$setNo $zz
965         $zz.$setNo preferredRecordSyntax [lindex $targets($host) 1]
966         egw_log debug "set syntax to [lindex $targets($host) 1]"
967     }
968     if {![lindex $targets($host) 5]} {
969         set elements {}
970     }
971     $zz.$setNo smallSetElementSetNames $elements
972     $zz.$setNo mediumSetElementSetNames $elements
973     $zz.$setNo recordElements $elements
974
975     egw_log debug "database=$database"
976     eval $zz.$setNo databaseNames $database
977
978     $zz callback [list search-response $zz.$setNo]
979     if {$piggy} {
980         $zz.$setNo largeSetLowerBound 999999
981         $zz.$setNo smallSetUpperBound 0
982         $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
983     } else {
984         $zz.$setNo largeSetLowerBound 2
985         $zz.$setNo smallSetUpperBound 0
986         $zz.$setNo mediumSetPresentNumber 0
987     }
988     set sessionWait 0
989     egw_log debug "search: $query"
990
991     if {[info exists docId]} {
992         $zz.$setNo search $query $docId
993     } else {
994         $zz.$setNo search $query
995     }
996
997     if {!$sessionWait && [catch {egw_wait sessionWait 60}]} {
998         egw_log debug "timeout/cancel in search"
999         displayError "Timeout in search" {}
1000         html "</body></html>\n"
1001         $zz disconnect
1002         return 0
1003     }
1004         
1005     if {$sessionWait == -1} {
1006         displayError "Search fail" "Connection closed"
1007         html "</body></html>\n"
1008         $zz disconnect
1009     }
1010     if {$sessionWait != 1} {
1011         return 0
1012     }
1013     set hist($setNo,$tno,hits) [$zz.$setNo resultCount]
1014     return 1
1015 }
1016
1017 proc init-m-response {i} {
1018     global zstatus
1019     global zleft
1020
1021     egw_log debug "init-m-response"
1022
1023     incr zleft -1
1024     if {![z39$i initResult]} {
1025         set zstatus($i) -1
1026         z39$i disconnect
1027         return
1028     }
1029     set zstatus($i) 1
1030 }
1031
1032 proc connect-m-response {i} {
1033     global zstatus
1034     global zleft
1035
1036     egw_log debug "connect-m-response"
1037     z39$i callback [list init-m-response $i]
1038     if {[catch {z39$i init}]} {
1039         set zstatus($i) -1
1040         incr zleft -1
1041     }
1042 }
1043
1044 proc fail-m-response {i} {
1045     global zstatus
1046     global zleft
1047     
1048     egw_log debug "fail-m-response"
1049     set zstatus($i) -1
1050     incr zleft -1
1051 }
1052
1053 proc search-m-response {setNo i start number} {
1054     global zleft
1055     global zstatus
1056     global hist
1057
1058     egw_log debug "search-m-response"
1059     set status [z39$i.$setNo responseStatus]
1060     egw_log debug "search-m-response1"
1061     if {[lindex $status 0] == "OK"} {
1062         set nor 0
1063     } elseif {[lindex $status 0] == "DBOSD"} {
1064         set nor [z39$i.$setNo numberOfRecordsReturned]
1065     } else {
1066         egw_log debug "search-m-response2"
1067         incr zleft -1
1068         set zstatus($i) 2
1069         return
1070     }
1071     set hist($setNo,$i,hits) [z39$i.$setNo resultCount]
1072     egw_log debug "search-m-response3"
1073     set hist($setNo,$i,offset) [expr $start + $nor -1]
1074     if {[expr $nor + $start] > [z39$i.$setNo resultCount]} {
1075         egw_log debug "search-m-response4"
1076         incr zleft -1
1077         set zstatus($i) 2
1078         return
1079     }
1080     egw_log debug "search-m-response5"
1081     if {$nor >= $number} {
1082         egw_log debug "search-m-response6 nor=$nor number=$number"
1083         incr zleft -1
1084         set zstatus($i) 2
1085         return
1086     }
1087     egw_log debug "search-m-response7"
1088     set start [expr $start + $nor]
1089     set number [expr $number - $nor]
1090     if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
1091         set number [expr [z39$i.$setNo resultCount] - $start + 1]
1092     }
1093     z39$i callback [list search-m-response $setNo $i $start $number]
1094     egw_log debug "mpresent start=$number number=$number"
1095     z39$i.$setNo present $start $number
1096 }
1097
1098 proc z39msearch {setNo elements start number cache} {
1099     global zleft
1100     global zstatus
1101     global hist
1102     global targets
1103     global debug
1104
1105     set not $hist($setNo,0,host)
1106
1107     egw_log debug "z39msearch start=$start number=$number elements=$elements"
1108     for {set i 1} {$i <= $not} {incr i} {
1109         set host $hist($setNo,$i,host)
1110         mkAssoc z39$i $host
1111         set oldHost [z39$i connect]
1112         set thisHost [splitHostSpec $host]
1113         if {[string compare $oldHost $thisHost]} {
1114             catch {z39$i disconnect}
1115         }
1116         z39$i callback [list connect-m-response $i]
1117         z39$i failback [list fail-m-response $i]
1118     }
1119     set zleft 0
1120     for {set i 1} {$i <= $not} {incr i} {
1121         set oldHost [z39$i connect]
1122         set host $hist($setNo,$i,host)
1123         set thisHost [splitHostSpec $host]
1124         if {![string compare $oldHost $thisHost]} {
1125             continue
1126         }
1127         egw_log debug "old=$oldHost this=$thisHost"
1128         z39$i idAuthentication $hist($setNo,$i,idAuthentication)
1129         html "Connecting to target " $thisHost " <br>\n"
1130         set zstatus($i) -1
1131         if {![catch {z39$i connect $thisHost}]} {
1132             incr zleft
1133         } 
1134     }
1135     while {$zleft > 0} {
1136         egw_log debug "Waiting for init response"
1137         if {[catch {egw_wait zleft 20} reason]} {
1138             if {![string compare $reason cancel]} {
1139                 for {set i 1} {$i <= $not} {incr i} {
1140                     set zstatus($i) -1
1141                     catch {z39$i disconnect}
1142                 }
1143                 return
1144             } else {
1145                 for {set i 1} {$i <= $not} {incr i} {
1146                     if {$zstatus($i) == -1} {
1147                         catch {z39$i disconnect}
1148                     }
1149                 }
1150             }
1151             break
1152         }
1153     }
1154     set zleft 0
1155     for {set i 1} {$i <= $not} {incr i} {
1156         set host $hist($setNo,$i,host)
1157         if {$debug} {
1158             html "host " [splitHostSpec $host] ": "
1159         }
1160         egw_log debug "i=$i zstatus=$zstatus($i)"
1161         if {$zstatus($i) < 1} {
1162             if {$debug} {
1163                 html "fail<br>\n"
1164             }
1165             continue
1166         }
1167         if {[catch [list z39$i.$setNo preferredRecordSyntax]]} {
1168             if {$debug} {
1169                 html "ok<br>\n"
1170             }
1171
1172             if {![string compare [lindex $targets($host) 1] WAIS]} {
1173                 wais-set z39$i.$setNo z39$i
1174             } else {
1175                 ir-set z39$i.$setNo z39$i
1176                 z39$i.$setNo preferredRecordSyntax [lindex $targets($host) 1]
1177                 egw_log debug "set syntax to [lindex $targets($host) 1]"
1178             }
1179             set hist($setNo,$i,offset) 0
1180             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
1181
1182             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
1183                 set thisElements {}
1184             } else {
1185                 set thisElements $elements
1186             }
1187             z39$i.$setNo smallSetElementSetNames $thisElements
1188             z39$i.$setNo mediumSetElementSetNames $thisElements
1189             z39$i.$setNo elementSetNames $thisElements
1190             z39$i.$setNo recordElements $thisElements
1191
1192             z39$i callback [list search-m-response $setNo $i $start $number]
1193
1194             if {$start == 1} {
1195                 z39$i.$setNo largeSetLowerBound 999999
1196                 z39$i.$setNo smallSetUpperBound 0
1197                 z39$i.$setNo mediumSetPresentNumber $number
1198             } else {
1199                 z39$i.$setNo largeSetLowerBound 2
1200                 z39$i.$setNo smallSetUpperBound 0
1201                 z39$i.$setNo mediumSetPresentNumber 0
1202             }
1203             set zstatus($i) 1
1204             incr zleft
1205             egw_log debug "msearch host=" $hist($setNo,$i,host)
1206             egw_log debug "setNo=$setNo query=" $hist($setNo,$i,query) "="
1207             if {[catch {z39$i.$setNo search $hist($setNo,$i,query)}]} {
1208                 set zstatus($i) -1
1209                 incr zleft -1
1210             }
1211         } elseif {[z39$i.$setNo resultCount] >= $start} {
1212             if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
1213                 set tnumber [expr [z39$i.$setNo resultCount] - $start + 1]
1214             } else {
1215                 set tnumber $number
1216             }
1217             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
1218                 set thisElements {}
1219             } else {
1220                 set thisElements $elements
1221             }
1222             z39$i.$setNo smallSetElementSetNames $thisElements
1223             z39$i.$setNo mediumSetElementSetNames $thisElements
1224             z39$i.$setNo elementSetNames $thisElements
1225             z39$i.$setNo recordElements $thisElements
1226
1227             for {set n 0} {$n < $tnumber} {incr n} {
1228                 if {[z39$i.$setNo recordType [expr $start + $n]] == ""} {
1229                     if {$n > 0} {
1230                         egw_log debug "failed on $n"
1231                     }
1232                     if {$debug} {
1233                         html "no record at #" [expr $start + $n]
1234                         html " el=-" $thisElements "-"
1235                     }
1236                     break
1237                 }
1238             }
1239             if {$n == $tnumber} {
1240                 if {$debug} {
1241                     html "cached<br>\n"
1242                 }
1243                 continue
1244             }
1245             
1246             html "present<br>\n"
1247             z39$i callback [list search-m-response $setNo $i $start $tnumber]
1248             incr zleft
1249             egw_log debug "mpresent start=$start number=$tnumber"
1250             z39$i.$setNo present $start $tnumber
1251         } else {
1252             if {$debug} {
1253                 html "ok<br>\n"
1254             }
1255         }
1256     }
1257
1258
1259     while {$zleft > 0} {
1260         egw_log debug "Waiting for search/present response"
1261         if {[catch {egw_wait zleft 60} reason]} {
1262             if {![string compare $reason cancel]} {
1263                 for {set i 1} {$i <= $not} {incr i} {
1264                     catch {z39$i disconnect}
1265                     set zstatus($i) -1
1266                 }
1267                 return
1268             } else {
1269                 for {set i 1} {$i <= $not} {incr i} {
1270                     if {$zstatus($i) != 2} {
1271                         catch {z39$i disconnect}
1272                     }
1273                 }
1274             }
1275             break
1276         }
1277     }
1278     for {set i 1} {$i <= $not} {incr i} {
1279         if {$zstatus($i) != 2} continue
1280         set status [z39$i.$setNo responseStatus]
1281         if {0 && [lindex $status 0] != "NSD"} {
1282             set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
1283         }
1284     }
1285 }
1286
1287 proc z39present {setNo tno setOffset setMax dfunc elements} {
1288     global hist
1289     global sessionWait
1290     global targets
1291
1292     set zz z39$tno
1293     set host $hist($setNo,$tno,host)
1294
1295     if {![lindex $targets($host) 5]} {
1296         set elements {}
1297     }
1298
1299     $zz.$setNo elementSetNames $elements
1300     $zz.$setNo recordElements $elements
1301     set toGet [expr 1 + $setMax - $setOffset]
1302
1303     $zz callback [list search-response $zz.$setNo]
1304
1305     while {$setMax > 0 && $toGet > 0} {
1306         for {set got 0} {$got < $toGet} {incr got} {
1307             if {[$zz.$setNo recordType [expr $setOffset + $got]] == ""} {
1308                 break
1309             }
1310         }
1311         if {$got < $toGet} {
1312             set sessionWait 0
1313             $zz.$setNo present $setOffset $toGet
1314             if {[catch {egw_wait sessionWait 300}]} {
1315                 egw_log debug "timeout/cancel in present"
1316                 $zz disconnect
1317                 break
1318             }
1319             if {$sessionWait == "0"} {
1320                 $zz disconnect
1321             }
1322             if {$sessionWait != "1"} {
1323                 break
1324             }
1325             set got [$zz.$setNo numberOfRecordsReturned]
1326             if {$got <= 0} {
1327                 break
1328             }
1329         }
1330         display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $setNo $tno
1331         set setOffset [expr $got + $setOffset]
1332         set toGet [expr 1 + $setMax - $setOffset]
1333         egw_flush
1334     }
1335 }
1336
1337 proc buttons-result-set-s {setNo targetNo setMax startPos after} {
1338     global sessionId
1339     global useIcons
1340     global env
1341     global hist
1342
1343     set zz z39$targetNo
1344     html "<p>\n"
1345     button-main
1346     if {$setMax > 0 && $setMax < [$zz.$setNo resultCount]} {
1347         if {!$useIcons} {
1348             html "\n | "
1349         }
1350         html {<a href="http:} $env(SCRIPT_NAME)
1351         html / $sessionId {/search.egw/} $setNo + $targetNo + 
1352         html [expr $setMax + 1] + [expr $setMax + $hist($setNo,maxPresent)]
1353         if {$useIcons} {
1354             html {"><img src="/egwgif/button-next-records.gif" }
1355             html {alt="Next Records" border=0></a>}
1356         } else {
1357             html {">Next Records</a>}
1358         }
1359     }
1360     if {$setMax > 0 && $startPos != "" && $startPos != "1"} {
1361         if {!$useIcons} {
1362             html "\n | "
1363         }
1364         html {<a href="http:} $env(SCRIPT_NAME)
1365         html / $sessionId {/search.egw/} $setNo + $targetNo
1366         html + [expr $startPos - $hist($setNo,maxPresent)]
1367         html + [expr $startPos - 1]
1368         if {$useIcons} {
1369             html {"><img src="/egwgif/button-previous-records.gif" }
1370             html {alt="Previous Records" border=0></a>}
1371         } else {
1372             html {">Previous Records</a>} 
1373         }
1374     }
1375     if {$targetNo > 0} {
1376         button-result-set $setNo $targetNo
1377     }
1378     button-new-query $setNo
1379     button-new-target
1380     button-view-history
1381
1382     html "<p>\n"
1383 }
1384
1385 proc score-sort {l r} {
1386     return [expr [lindex $r 0] - [lindex $l 0]]
1387 }
1388
1389 proc display-result-set-m-score {setNo} {
1390     global hist
1391     global useIcons
1392     global zstatus
1393     global targets
1394
1395     set not $hist($setNo,0,host)
1396     for {set i 1} {$i <= $not} {incr i} {
1397         if {$zstatus($i) != 2} continue
1398         set status [z39$i.$setNo responseStatus]
1399         if {[lindex $status 0] != "DBOSD"} continue        
1400         set nor $hist($setNo,$i,offset)
1401         for {set j 1} {$j <= $nor} {incr j} {
1402             if {![string compare [z39$i.$setNo recordType $j] WAIS]} {
1403                 set score [z39$i.$setNo getWAIS $j score]
1404             } elseif {![string compare [z39$i.$setNo recordType $j] USmarc]} {
1405                 set score [z39$i.$setNo getMarc $j field 999 * r]
1406                 if {[scan $score %d score] != 1} {
1407                     set score 10
1408                 }
1409             } else {
1410                 set score 10
1411             }
1412             if {$score > 0} {
1413                 lappend scoreArray [list $score $i $j]
1414             }
1415         }
1416     }
1417     if {![info exists scoreArray]} {
1418         html "<br><h3>Search produced no result</h3><br>\n"
1419     } else {
1420         html "<ul>\n"
1421         set scoreSorted [lsort -command score-sort $scoreArray]
1422         foreach r $scoreSorted {
1423             set i [lindex $r 1]
1424             set j [lindex $r 2]
1425             display-$hist($setNo,format) z39$i.$setNo $j $setNo $i
1426         }
1427         html "<br></ul>\n"
1428     }
1429     for {set i 1} {$i <= $not} {incr i} {
1430         if {$zstatus($i) != 2} continue
1431         set status [z39$i.$setNo responseStatus]
1432         if {[lindex $status 0] == "NSD"} {
1433             z39$i.$setNo nextResultSetPosition 0
1434             set code [lindex $status 1]
1435             set msg [lindex $status 2]
1436             set addinfo [lindex $status 3]
1437             html {<dt>} [lindex $targets($hist($setNo,$i,host)) 0] 
1438             html "<dd>Error: $msg: $addinfo (code $code)<br>\n"
1439         }
1440     }
1441     html "\n<br>"
1442 }
1443
1444 proc display-result-set-m-server {setNo} {
1445     global hist
1446     global useIcons
1447     global zstatus
1448     global targets
1449     global env
1450     global sessionId
1451
1452     set not $hist($setNo,0,host)
1453     html "<dl>\n"
1454     for {set i 1} {$i <= $not} {incr i} {
1455         if {$zstatus($i) != 2} continue
1456         set status [z39$i.$setNo responseStatus]
1457         if {[lindex $status 0] == "NSD"} {
1458             html "<dt><h3>" [lindex $targets($hist($setNo,$i,host)) 0] ": "
1459             z39$i.$setNo nextResultSetPosition 0
1460             set code [lindex $status 1]
1461             set msg [lindex $status 2]
1462             set addinfo [lindex $status 3]
1463             html "Error</h3>\n<dd>NSD$code: $msg: $addinfo"
1464         } else {
1465             html {<dt><a href="http:} $env(SCRIPT_NAME)
1466             html / $sessionId {/search.egw/} $setNo + $i + 1
1467             html + $hist($setNo,maxPresent) {">}
1468             html "<h3>" [lindex $targets($hist($setNo,$i,host)) 0] "</a>: "
1469             set r [z39$i.$setNo resultCount]
1470             html "$r hits</h3>\n<dd>\n"
1471             
1472             if {$hist($setNo,$i,offset) > $hist($setNo,maxPresent)} {
1473                 set nor $hist($setNo,maxPresent)
1474             } else {
1475                 set nor $hist($setNo,$i,offset)
1476             }
1477             display-rec 1 $nor display-$hist($setNo,format) $setNo $i
1478         }
1479         html "\n"
1480     }
1481     html "</dl>\n"
1482 }
1483
1484 proc display-result-set-m {setNo} {
1485     global hist
1486     global useIcons
1487     global zstatus
1488     global targets
1489
1490     egw_log debug "sort=$hist($setNo,sort)"
1491     switch $hist($setNo,sort) {
1492         score {
1493             display-result-set-m-score $setNo
1494         }
1495         default {
1496             display-result-set-m-server $setNo
1497         }
1498     }
1499 }
1500
1501 proc display-result-set-s {setNo targetNo startPos endPos} {
1502     global hist
1503     global useIcons
1504
1505     set zz z39$targetNo
1506     set host $hist($setNo,$targetNo,host)
1507     set idAuth $hist($setNo,$targetNo,idAuthentication)
1508     set database $hist($setNo,$targetNo,database)
1509     set query $hist($setNo,$targetNo,query)
1510
1511     set useIcons 1
1512
1513     if {$startPos == ""} {
1514         if {[z39search $setNo 1 $targetNo B] != "1"} {
1515             return
1516         }
1517         set r [$zz.$setNo resultCount]
1518
1519         set setMax [$zz.$setNo resultCount]
1520         if {$setMax > $hist($setNo,maxPresent)} {
1521             set setMax $hist($setNo,maxPresent)
1522         }
1523         buttons-result-set-s $setNo $targetNo $setMax $startPos 0
1524
1525         set setOffset [$zz.$setNo numberOfRecordsReturned]
1526         if {$setMax > 0} {
1527             html {<h3> Records 1-} $setMax " out of $r</h3>\n"
1528         } else {
1529             html "<h3> No hits</h3>\n"
1530         }
1531         egw_flush
1532         html "<ul>\n"
1533         display-rec 1 $setMax display-brief $setNo $targetNo
1534         incr setOffset
1535
1536     } else {
1537         if {[z39search $setNo 0 $targetNo B] != "1"} {
1538             return 
1539         }
1540         set r [$zz.$setNo resultCount]
1541         set setOffset $startPos
1542         set setMax [$zz.$setNo resultCount]
1543         if {$setMax > $endPos} {
1544             set setMax $endPos
1545         }
1546         buttons-result-set-s $setNo $targetNo $setMax $startPos 0
1547         if {$setMax > 0} {
1548             html {<h3> Records } $startPos {-} $setMax " out of $r</h3>\n"
1549         } else {
1550             html "<h3> No hits</h3>\n"
1551         }
1552         egw_flush
1553         html "<ul>\n"
1554     }
1555     if {$setMax > 0} {
1556         z39present $setNo $targetNo $setOffset $setMax display-brief B
1557     }
1558     html "</ul>\n"
1559     set useIcons 0
1560     buttons-result-set-s $setNo $targetNo $setMax $startPos 1
1561 }
1562
1563 proc z39history {} {
1564     global nextSetNo
1565     global hist
1566     global env
1567     global sessionId
1568     global targets
1569     global html3
1570     global scriptQuery
1571
1572     if {![info exists nextSetNo]} {
1573         return
1574     }
1575     html "<h2>History</h2><br>\n"
1576     if {$html3} {
1577         html {<table width=500 border=1><tr>}
1578         html {<td align=center><b>Target</b>}
1579         html {<td align=center><b>Database</b>}
1580         html {<td align=center><b>Hits</b>}
1581         html {<td align=center><b>Query</b>}
1582         html {<tr>} "\n"
1583     } else {
1584         html {<dl>} "\n"
1585     }
1586     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
1587         if {[info exists hist($setNo,scan)]} {
1588             if {$hist($setNo,scan) > 0} continue
1589         }
1590         if {[info exists hist($setNo,1,host)]} {
1591             set start 1
1592             set end $hist($setNo,0,host)
1593         } else {
1594             set start 0
1595             set end 0
1596         }
1597         for {set i $start} {$i <= $end} {incr i} {
1598             if {$html3} {
1599                 html {<td align=left>}
1600             } else {
1601                 html {<dt> }
1602             }
1603             set host $hist($setNo,$i,host)
1604             html [lindex $targets($host) 0]
1605             if {$html3} {
1606                 html {<td align=left>} [join $hist($setNo,$i,database)]
1607             } else {
1608                 if {[llength [lindex $targets($host) 1]] > 1} {
1609                     html ": "
1610                     foreach b $hist($setNo,$i,database) {
1611                         html " $b"
1612                     }
1613                 }
1614                 html {. }
1615             }
1616             if {$html3} {
1617                 html {<td align=right>}
1618             }
1619             if {[info exists hist($setNo,$i,hits)]} {
1620                 html { <a href="http:} $env(SCRIPT_NAME)
1621                 html / $sessionId {/search.egw/} $setNo + $i + 1
1622                 html + $hist($setNo,maxPresent)
1623                 html {">} $hist($setNo,$i,hits) {</a>}
1624             } else {
1625                 if {$html3} {
1626                     html {Failed}
1627                 } else {
1628                     html {Search failed.}
1629                 }
1630             }
1631             if {$html3} {
1632                 html {<td align=left>}
1633             } else {
1634                 html "<dd>\n"
1635             }
1636             html { <a href="http:} $env(SCRIPT_NAME)
1637             html / $sessionId / $scriptQuery {;/} $setNo + $host
1638             if {$html3} {
1639                 html {">}
1640             } else {
1641                 html {">Query</a>: }
1642             }
1643             set op {}
1644             for {set j 1} {$j <= 3} {incr j} {
1645                 if {[string length $hist($setNo,form,entry$j)] > 0} {
1646                     html " <b>" [join $op " "] "</b> "
1647                 set pre [join $hist($setNo,form,menu$j)]
1648                     if {[string length $pre] > 0} {
1649                         html $pre "="
1650                     }
1651                     html $hist($setNo,form,entry$j)
1652                     set op $hist($setNo,form,logic$j)
1653                 }
1654             }
1655             if {$html3} {
1656                 html {</a><tr>} "\n"
1657             }
1658         }
1659     }
1660     if {$html3} {
1661         html {</table><p>}
1662     } else {
1663         html {</dl>}
1664     }
1665     html "\n"
1666 }
1667
1668 proc displayError {msga msgb} {
1669     html "<p><center>\n"
1670     html {<img src="/egwgif/noway.gif" alt="Error">}
1671     html "<h2>" $msga "</h2>\n"
1672     if {[string length $msgb] > 0} {
1673         html "<h3>" $msgb "</h3>\n"
1674     }
1675     html "</center><p>\n"
1676 }
1677
1678 proc button-main {} {
1679     global useIcons
1680     html {<a href="http://europagate.dtv.dk/">}
1681     if {$useIcons} {
1682         html {<img src="/egwgif/button-egw.gif" alt="Europagate" border=0></a>}
1683     } else {
1684         html {Europagate</a>}
1685     }
1686 }
1687
1688 proc button-define-target {} {
1689     global useIcons
1690     global env
1691     global sessionId
1692
1693     if {!$useIcons} {
1694         html "\n | "
1695     }
1696     html {<a href="http:} $env(SCRIPT_NAME)
1697     html / $sessionId {/tform.egw}
1698     if {$useIcons} {
1699         html {"><img src="/egwgif/button-define-target.gif" }
1700         html {alt="Define Target" border=0></a>}
1701     } else {
1702         html {">Define Target</a>}
1703     }
1704 }
1705
1706 proc button-new-target {} {
1707     global useIcons
1708     global env
1709     global sessionId
1710     global scriptTarget
1711
1712     if {[string length $scriptTarget] == 0} return
1713
1714     if {!$useIcons} {
1715         html "\n | "
1716     }
1717     html {<a href="http:} $env(SCRIPT_NAME)
1718     html / $sessionId / $scriptTarget
1719     if {$useIcons} {
1720         html {"><img src="/egwgif/button-new-target.gif" }
1721         html {alt="New Target" border=0></a>}
1722     } else {
1723         html {">New Target</a>}
1724     }
1725 }
1726
1727 proc button-view-history {} {
1728     global useIcons
1729     global env
1730     global sessionId
1731     global nextSetNo
1732
1733     if {!$useIcons} {
1734         html "\n | "
1735     }
1736     html {<a href="http:} $env(SCRIPT_NAME)
1737     html / $sessionId {/history.egw;}
1738     catch { html "/" $nextSetNo}
1739     if {$useIcons} {
1740         html {"><img src="/egwgif/button-view-history.gif" alt="View History" }
1741         html {border=0></a>}
1742     } else {
1743         html {">View History</a>}
1744     }
1745 }
1746
1747 proc button-new-query {setNo} {
1748     global useIcons
1749     global env
1750     global sessionId
1751     global hist
1752     global scriptQuery
1753
1754     if {!$useIcons} {
1755         html "\n | "
1756     }
1757     html {<a href="http:} $env(SCRIPT_NAME)
1758     html / $sessionId / $scriptQuery {;/} $setNo + $hist($setNo,0,host) {">}
1759
1760     if {$useIcons} {
1761         html {<img src="/egwgif/button-new-query.gif" }
1762         html {alt="New Query" border=0></a>}
1763     } else {
1764         html {New Query</a>}
1765     }
1766 }
1767
1768 proc button-result-set {setNo tno} {
1769     global useIcons
1770     global env
1771     global sessionId
1772     global hist
1773
1774     if {!$useIcons} {
1775         html "\n | "
1776     }
1777     html {<a href="http:} $env(SCRIPT_NAME) / $sessionId 
1778     if {$tno > 0} {
1779         html {/msearch.egw/} $setNo
1780     } else {
1781         html {/search.egw/} $setNo + $tno
1782     }
1783     html + 1 + $hist($setNo,maxPresent)
1784     if {$useIcons} {
1785         html {"><img src="/egwgif/button-result-set.gif" }
1786         html {alt="Result Set" border=0></a>}
1787     } else {
1788         html {">Result Set</a>}
1789     }
1790 }
1791
1792 proc button-scan-window {setNo} {
1793     global useIcons
1794     global env
1795     global sessionId
1796     global hist
1797
1798     if {!$useIcons} {
1799         html "\n | "
1800     }
1801     set targetNo 0
1802     html {<a href="http:} $env(SCRIPT_NAME)
1803     html / $sessionId {/search.egw/} $setNo + $targetNo + {scan} {">}
1804     if {$useIcons} {
1805         html {<img src="/egwgif/button-scan-window.gif" }
1806         html {alt="Scan" border=0></a>}
1807     } else {
1808         html {Scan</a>}
1809     }
1810 }
1811
1812 proc maintenance {} {
1813     html {<hr>This page is maintained by }
1814     html {<a href="mailto:pwh@dtv.dk"> Peter Wad Hansen </a>.}
1815     html {Last modified March 15, 1996. <br>}
1816 }
1817
1818 proc splitHostSpec {host} {
1819     set i [string first / $host]
1820     if {$i > 1} {
1821         incr i -1
1822         return [string range $host 0 $i]
1823     }
1824     return $host
1825 }
1826
1827 proc splitDatabaseSpec {host} {
1828     set i [string first / $host]
1829     if {$i > 1} {
1830         incr i
1831         regsub -all -- - [string range $host $i end] { } res
1832         return $res
1833     }
1834     regsub -all -- - $host {} res
1835     return $res
1836 }
1837
1838 proc mergeHostSpec {host databases} {
1839     return ${host}/[join $databases -]
1840 }
1841
1842 proc mkAssoc {assoc host} {
1843     global targets
1844
1845     if {[catch {$assoc failback fail-response}]} {
1846         if {![string compare [lindex $targets($host) 1] WAIS]} {
1847             wais $assoc
1848         } else {
1849             ir $assoc
1850         }
1851     } else {
1852         if {![string compare [lindex $targets($host) 1] WAIS]} {
1853             if {[$assoc comstack] == "wais"} return
1854             wais $assoc
1855         } else {
1856             if {[$assoc comstack] == "tcpip"} return
1857             ir $assoc
1858         }
1859     }
1860 }
1861
1862 proc serverList {headlineProc targetProc} {
1863     global targets
1864     global groupsDescription
1865    
1866     proc targetsCmp {l r} {
1867         global targets
1868         return [string compare [string tolower [lindex $targets($l) 0]] \
1869                                [string tolower [lindex $targets($r) 0]]]
1870     }
1871     proc groupCmp {l r} {
1872         global groupsOrder
1873         if {[catch {set lo $groupsOrder($l)}]} {
1874             set lo 10
1875         }
1876         if {[catch {set ro $groupsOrder($r)}]} {
1877             set ro 10
1878         }
1879         return [expr $lo - $ro]
1880     }
1881     
1882     foreach tt [array names targets] {
1883         lappend groupsTmp([lindex $targets($tt) 6]) $tt
1884     }
1885     set gts [lsort -command groupCmp [array names groupsTmp]]
1886     foreach gt $gts {
1887         if {[info exists groupsDescription($gt)]} {
1888             eval $headlineProc [list $groupsDescription($gt)]
1889         } else {
1890             eval $headlineProc $gt
1891         }
1892         set tn [lsort -command targetsCmp $groupsTmp($gt)]
1893         foreach t $tn {
1894             eval $targetProc $t
1895         }
1896     }
1897
1898     rename targetsCmp {}
1899 }
1900
1901 proc session-lost {} {
1902     global useIcons
1903
1904     html {<head><title>WWW/Z39.50 Gateway: Session Expired</title></head>}
1905     html \n {<body>}
1906     set useIcons 1
1907     button-main
1908     html {<h1>Session Expired</h1>}
1909     html {Your session has expired. Please reload the gateways' }
1910     html {front page.<br><br>} \n
1911     set useIcons 0
1912     button-main
1913     html {</body></html>}
1914 }
1915
1916 if {[info exists utilExtension]} {
1917     source $utilExtension
1918 }
1919