Added feedback button.
[egate.git] / www / z39util.tcl
1 #
2 # $Id: z39util.tcl,v 1.45 1996/05/24 11:16:02 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] == 1} {
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             egw_log debug "op=${op}"
731             switch $op {
732                 And
733                 { set q "@and $q ${attr} \"${term}\""; egw_log debug AND }
734                 Or
735                 { set q "@or $q ${attr} \"${term}\""; egw_log debug OR }
736                 {}
737                 { set q "${attr} \"${term}\""; egw_log debug NOPE }
738                 default
739                 { set q "@not $q ${attr} \"${term}\""; egw_log debug ANDNOT }
740             }
741             set op [egw_form logic$i]
742         }
743     }
744     return $q
745 }
746
747 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
748     global hist
749     global sessionWait
750     global targets
751
752     set zz z39$tno
753     set host $hist($setNo,$tno,host)
754     set idAuth $hist($setNo,$tno,idAuthentication)
755     set database $hist($setNo,$tno,database)
756     set scanAttr $hist($setNo,scanAttr)
757     set scanTerm $hist($setNo,$scanNo,scanTerm)
758
759     mkAssoc $zz $host
760     if {[catch [list set oldHost [$zz connect]]]} {
761         set oldHost ""
762     }
763     set zs $zz.s$scanNo.$setNo
764     $zz callback ok-response
765     $zz failback fail-response
766     set thisHost [splitHostSpec $host]
767     if {[string compare $oldHost $thisHost]} {
768         catch [list $zz disconnect]
769
770         set sessionWait 0
771         if {[catch [list $zz connect $thisHost]]} {
772             displayError "Cannot connect to target" $thisHost
773             return 0
774         } elseif {$sessionWait == 0} {
775             if {[catch {egw_wait sessionWait 300}]} {
776                 $zz disconnect
777                 displayError "Cannot connect to target" $thisHost
778                 return 0
779             }
780             if {$sessionWait != 1} {
781                 displayError "Cannot connect to target" $thisHost
782                 return 0
783             }
784         }
785         $zz idAuthentication $idAuth
786         set sessionWait 0
787         if {[catch {$zz init}]} {
788             displayError "Cannot initialize target" $thisHost
789             $zz disconnect
790             return 0
791         }
792         if {[catch {egw_wait sessionWait 60}]} {
793             displayError "Cannot initialize target" $thisHost
794             $zz disconnect
795             return 0
796         }
797         if {$sessionWait != "1"} {
798             displayError "Cannot initialize target" $thisHost
799             $zz disconnect
800             return 0
801         }
802         if {![$zz initResult]} {
803             set u [$zz userInformationField]
804             $zz disconnect
805             displayError "Cannot initialize target $thisHost" $u
806             return 0
807         }
808     } else {
809         if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
810             return 1
811         }
812     }
813     eval $zz databaseNames $database
814
815     ir-scan $zs $zz
816
817     $zs numberOfTermsRequested $scanLines
818     $zs preferredPositionInResponse $scanPos
819
820     $zz callback [list scan-response $zs]
821
822     egw_log debug "scan: ${scanAttr} ${scanTerm}"
823     set sessionWait 0
824     $zs scan "${scanAttr} ${scanTerm}"
825
826     if {[catch {egw_wait sessionWait 60}]} {
827         egw_log debug "timeout/cancel in scan"
828         displayError "Timeout in scan" {}
829         html "</body></html>\n"
830         $zz disconnect
831         return 0
832     }
833     if {$sessionWait == -1} {
834         displayError "Scan fail" "Connection closed"
835         html "</body></html>\n"
836         $zz disconnect
837     }
838     if {$sessionWait != 1} {
839         return 0
840     }
841     return 1
842 }
843
844 proc display-scan {setNo scanNo tno} {
845     global hist
846     global targets
847     global env
848     global sessionId
849     global scriptQuery
850
851     set zz z39$tno
852     set zs $zz.s$scanNo.$setNo
853     set m [$zs numberOfEntriesReturned]
854         
855     if {$m > 0} {
856         set t [lindex [$zs scanLine 0] 1]
857         if {$tno > 0} {
858             set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
859         } else {
860             set hist($setNo,[expr $scanNo - 1],scanTerm) $t
861         }
862         set t [lindex [$zs scanLine [expr $m - 1]] 1]
863         if {$tno > 0} {
864             set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
865         } else {
866             set hist($setNo,[expr $scanNo + 1],scanTerm) $t
867         }
868     }
869     html {<table width=500 border=0><tr>}
870     html {<td align=left><b>Scan term</b>}
871     html {<td align=right><b>Hits</b>}
872     html {<tr>} \n
873
874     for {set i 0} {$i < $m} {incr i} {
875         html {<td align=left>}
876         if {0} {
877             regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
878             html {<a href="http:} $env(SCRIPT_NAME)
879             html / $sessionId / $scriptQuery {;/} $setNo + $hist($setNo,host) +
880             html $hist($setNo,scan) +  $tterm {">}
881         } else {
882             regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
883             html {<a href="http:} $env(SCRIPT_NAME)
884             html / $sessionId {/search.egw/} $setNo + $tno +
885             html hyper + $tterm {">}
886         }
887         html [lindex [$zs scanLine $i] 1]
888         html {</a>} 
889         html {<td align=right>}
890         html [lindex [$zs scanLine $i] 2]
891         html {<tr>} \n
892     }
893     html {</table} \n
894 }
895
896 proc z39search {setNo piggy tno elements} {
897     global hist
898     global sessionWait
899     global targets
900
901     set zz z39$tno
902     set host $hist($setNo,$tno,host)
903     set idAuth $hist($setNo,$tno,idAuthentication)
904     set database $hist($setNo,$tno,database)
905     set query $hist($setNo,$tno,query)
906     catch {set docId $hist($setNo,$tno,queryId)}
907
908     mkAssoc $zz $host
909     if {[catch [list set oldHost [$zz connect]]]} {
910         set oldHost ""
911     }
912     $zz callback ok-response
913     $zz failback fail-response
914     set thisHost [splitHostSpec $host]
915     if {[string compare $oldHost $thisHost]} {
916         catch [list $zz disconnect]
917
918         set sessionWait 0
919         if {[catch [list $zz connect $thisHost]]} {
920             displayError "Cannot connect to target" $thisHost
921             return 0
922         } elseif {$sessionWait == 0} {
923             if {[catch {egw_wait sessionWait 300}]} {
924                 $zz disconnect
925                 displayError "Cannot connect to target" $thisHost
926                 return 0
927             }
928             if {$sessionWait != 1} {
929                 displayError "Cannot connect to target" $thisHost
930                 return 0
931             }
932         }
933         $zz idAuthentication $idAuth
934         set sessionWait 0
935         if {[catch {$zz init}]} {
936             displayError "Cannot initialize target" $thisHost
937             $zz disconnect
938             return 0
939         }
940         if {$sessionWait == 0 && [catch {egw_wait sessionWait 60}]} {
941             displayError "Cannot initialize target" $thisHost
942             $zz disconnect
943             return 0
944         }
945         if {$sessionWait != "1"} {
946             displayError "Cannot initialize target" $thisHost
947             $zz disconnect
948             return 0
949         }
950         if {![$zz initResult]} {
951             set u [$zz userInformationField]
952             $zz disconnect
953             displayError "Cannot initialize target $thisHost" $u
954             return 0
955         }
956     } elseif {![catch  [list $zz.$setNo smallSetUpperBound 0]]} {
957         if {[info exists hist($setNo,$tno,hits)]} {
958             return 1
959         }
960     }
961     
962     if {![string compare [lindex $targets($host) 1] WAIS]} {
963         wais-set $zz.$setNo $zz
964     } else {
965         ir-set $zz.$setNo $zz
966         $zz.$setNo preferredRecordSyntax [lindex $targets($host) 1]
967         egw_log debug "set syntax to [lindex $targets($host) 1]"
968     }
969     if {![lindex $targets($host) 5]} {
970         set elements {}
971     }
972     $zz.$setNo smallSetElementSetNames $elements
973     $zz.$setNo mediumSetElementSetNames $elements
974     $zz.$setNo recordElements $elements
975
976     egw_log debug "database=$database"
977     eval $zz.$setNo databaseNames $database
978
979     $zz callback [list search-response $zz.$setNo]
980     if {$piggy} {
981         $zz.$setNo largeSetLowerBound 999999
982         $zz.$setNo smallSetUpperBound 0
983         $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
984     } else {
985         $zz.$setNo largeSetLowerBound 2
986         $zz.$setNo smallSetUpperBound 0
987         $zz.$setNo mediumSetPresentNumber 0
988     }
989     set sessionWait 0
990     egw_log debug "search: $query"
991
992     if {[info exists docId]} {
993         $zz.$setNo search $query $docId
994     } else {
995         $zz.$setNo search $query
996     }
997
998     if {!$sessionWait && [catch {egw_wait sessionWait 60}]} {
999         egw_log debug "timeout/cancel in search"
1000         displayError "Timeout in search" {}
1001         html "</body></html>\n"
1002         $zz disconnect
1003         return 0
1004     }
1005         
1006     if {$sessionWait == -1} {
1007         displayError "Search fail" "Connection closed"
1008         html "</body></html>\n"
1009         $zz disconnect
1010     }
1011     if {$sessionWait != 1} {
1012         return 0
1013     }
1014     set hist($setNo,$tno,hits) [$zz.$setNo resultCount]
1015     return 1
1016 }
1017
1018 proc init-m-response {i} {
1019     global zstatus
1020     global zleft
1021
1022     egw_log debug "init-m-response"
1023
1024     incr zleft -1
1025     if {![z39$i initResult]} {
1026         set zstatus($i) -1
1027         z39$i disconnect
1028         return
1029     }
1030     set zstatus($i) 1
1031 }
1032
1033 proc connect-m-response {i} {
1034     global zstatus
1035     global zleft
1036
1037     egw_log debug "connect-m-response"
1038     z39$i callback [list init-m-response $i]
1039     if {[catch {z39$i init}]} {
1040         set zstatus($i) -1
1041         incr zleft -1
1042     }
1043 }
1044
1045 proc fail-m-response {i} {
1046     global zstatus
1047     global zleft
1048     
1049     egw_log debug "fail-m-response"
1050     set zstatus($i) -1
1051     incr zleft -1
1052 }
1053
1054 proc search-m-response {setNo i start number} {
1055     global zleft
1056     global zstatus
1057     global hist
1058
1059     egw_log debug "search-m-response"
1060     set status [z39$i.$setNo responseStatus]
1061     egw_log debug "search-m-response1"
1062     if {[lindex $status 0] == "OK"} {
1063         set nor 0
1064     } elseif {[lindex $status 0] == "DBOSD"} {
1065         set nor [z39$i.$setNo numberOfRecordsReturned]
1066     } else {
1067         egw_log debug "search-m-response2"
1068         incr zleft -1
1069         set zstatus($i) 2
1070         return
1071     }
1072     set hist($setNo,$i,hits) [z39$i.$setNo resultCount]
1073     egw_log debug "search-m-response3"
1074     set hist($setNo,$i,offset) [expr $start + $nor -1]
1075     if {[expr $nor + $start] > [z39$i.$setNo resultCount]} {
1076         egw_log debug "search-m-response4"
1077         incr zleft -1
1078         set zstatus($i) 2
1079         return
1080     }
1081     egw_log debug "search-m-response5"
1082     if {$nor >= $number} {
1083         egw_log debug "search-m-response6 nor=$nor number=$number"
1084         incr zleft -1
1085         set zstatus($i) 2
1086         return
1087     }
1088     egw_log debug "search-m-response7"
1089     set start [expr $start + $nor]
1090     set number [expr $number - $nor]
1091     if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
1092         set number [expr [z39$i.$setNo resultCount] - $start + 1]
1093     }
1094     z39$i callback [list search-m-response $setNo $i $start $number]
1095     egw_log debug "mpresent start=$number number=$number"
1096     z39$i.$setNo present $start $number
1097 }
1098
1099 proc z39msearch {setNo elements start number cache} {
1100     global zleft
1101     global zstatus
1102     global hist
1103     global targets
1104     global debug
1105
1106     set not $hist($setNo,0,host)
1107
1108     egw_log debug "z39msearch start=$start number=$number elements=$elements"
1109     for {set i 1} {$i <= $not} {incr i} {
1110         set host $hist($setNo,$i,host)
1111         mkAssoc z39$i $host
1112         set oldHost [z39$i connect]
1113         set thisHost [splitHostSpec $host]
1114         if {[string compare $oldHost $thisHost]} {
1115             catch {z39$i disconnect}
1116         }
1117         z39$i callback [list connect-m-response $i]
1118         z39$i failback [list fail-m-response $i]
1119     }
1120     set zleft 0
1121     for {set i 1} {$i <= $not} {incr i} {
1122         set oldHost [z39$i connect]
1123         set host $hist($setNo,$i,host)
1124         set thisHost [splitHostSpec $host]
1125         if {![string compare $oldHost $thisHost]} {
1126             continue
1127         }
1128         egw_log debug "old=$oldHost this=$thisHost"
1129         z39$i idAuthentication $hist($setNo,$i,idAuthentication)
1130         html "Connecting to target " $thisHost " <br>\n"
1131         set zstatus($i) -1
1132         if {![catch {z39$i connect $thisHost}]} {
1133             incr zleft
1134         } 
1135     }
1136     while {$zleft > 0} {
1137         egw_log debug "Waiting for init response"
1138         if {[catch {egw_wait zleft 20} reason]} {
1139             if {![string compare $reason cancel]} {
1140                 for {set i 1} {$i <= $not} {incr i} {
1141                     set zstatus($i) -1
1142                     catch {z39$i disconnect}
1143                 }
1144                 return
1145             } else {
1146                 for {set i 1} {$i <= $not} {incr i} {
1147                     if {$zstatus($i) == -1} {
1148                         catch {z39$i disconnect}
1149                     }
1150                 }
1151             }
1152             break
1153         }
1154     }
1155     set zleft 0
1156     for {set i 1} {$i <= $not} {incr i} {
1157         set host $hist($setNo,$i,host)
1158         if {$debug} {
1159             html "host " [splitHostSpec $host] ": "
1160         }
1161         egw_log debug "i=$i zstatus=$zstatus($i)"
1162         if {$zstatus($i) < 1} {
1163             if {$debug} {
1164                 html "fail<br>\n"
1165             }
1166             continue
1167         }
1168         if {[catch [list z39$i.$setNo preferredRecordSyntax]]} {
1169             if {$debug} {
1170                 html "ok<br>\n"
1171             }
1172
1173             if {![string compare [lindex $targets($host) 1] WAIS]} {
1174                 wais-set z39$i.$setNo z39$i
1175             } else {
1176                 ir-set z39$i.$setNo z39$i
1177                 z39$i.$setNo preferredRecordSyntax [lindex $targets($host) 1]
1178                 egw_log debug "set syntax to [lindex $targets($host) 1]"
1179             }
1180             set hist($setNo,$i,offset) 0
1181             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
1182
1183             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
1184                 set thisElements {}
1185             } else {
1186                 set thisElements $elements
1187             }
1188             z39$i.$setNo smallSetElementSetNames $thisElements
1189             z39$i.$setNo mediumSetElementSetNames $thisElements
1190             z39$i.$setNo elementSetNames $thisElements
1191             z39$i.$setNo recordElements $thisElements
1192
1193             z39$i callback [list search-m-response $setNo $i $start $number]
1194
1195             if {$start == 1} {
1196                 z39$i.$setNo largeSetLowerBound 999999
1197                 z39$i.$setNo smallSetUpperBound 0
1198                 z39$i.$setNo mediumSetPresentNumber $number
1199             } else {
1200                 z39$i.$setNo largeSetLowerBound 2
1201                 z39$i.$setNo smallSetUpperBound 0
1202                 z39$i.$setNo mediumSetPresentNumber 0
1203             }
1204             set zstatus($i) 1
1205             incr zleft
1206             egw_log debug "msearch host=" $hist($setNo,$i,host)
1207             egw_log debug "setNo=$setNo query=" $hist($setNo,$i,query) "="
1208             if {[catch {z39$i.$setNo search $hist($setNo,$i,query)}]} {
1209                 set zstatus($i) -1
1210                 incr zleft -1
1211             }
1212         } elseif {[z39$i.$setNo resultCount] >= $start} {
1213             if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
1214                 set tnumber [expr [z39$i.$setNo resultCount] - $start + 1]
1215             } else {
1216                 set tnumber $number
1217             }
1218             if {![lindex $targets($hist($setNo,$i,host)) 5]} {
1219                 set thisElements {}
1220             } else {
1221                 set thisElements $elements
1222             }
1223             z39$i.$setNo smallSetElementSetNames $thisElements
1224             z39$i.$setNo mediumSetElementSetNames $thisElements
1225             z39$i.$setNo elementSetNames $thisElements
1226             z39$i.$setNo recordElements $thisElements
1227
1228             for {set n 0} {$n < $tnumber} {incr n} {
1229                 if {[z39$i.$setNo recordType [expr $start + $n]] == ""} {
1230                     if {$n > 0} {
1231                         egw_log debug "failed on $n"
1232                     }
1233                     if {$debug} {
1234                         html "no record at #" [expr $start + $n]
1235                         html " el=-" $thisElements "-"
1236                     }
1237                     break
1238                 }
1239             }
1240             if {$n == $tnumber} {
1241                 if {$debug} {
1242                     html "cached<br>\n"
1243                 }
1244                 continue
1245             }
1246             
1247             html "present<br>\n"
1248             z39$i callback [list search-m-response $setNo $i $start $tnumber]
1249             incr zleft
1250             egw_log debug "mpresent start=$start number=$tnumber"
1251             z39$i.$setNo present $start $tnumber
1252         } else {
1253             if {$debug} {
1254                 html "ok<br>\n"
1255             }
1256         }
1257     }
1258
1259
1260     while {$zleft > 0} {
1261         egw_log debug "Waiting for search/present response"
1262         if {[catch {egw_wait zleft 60} reason]} {
1263             if {![string compare $reason cancel]} {
1264                 for {set i 1} {$i <= $not} {incr i} {
1265                     catch {z39$i disconnect}
1266                     set zstatus($i) -1
1267                 }
1268                 return
1269             } else {
1270                 for {set i 1} {$i <= $not} {incr i} {
1271                     if {$zstatus($i) != 2} {
1272                         catch {z39$i disconnect}
1273                     }
1274                 }
1275             }
1276             break
1277         }
1278     }
1279     for {set i 1} {$i <= $not} {incr i} {
1280         if {$zstatus($i) != 2} continue
1281         set status [z39$i.$setNo responseStatus]
1282         if {0 && [lindex $status 0] != "NSD"} {
1283             set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
1284         }
1285     }
1286 }
1287
1288 proc z39present {setNo tno setOffset setMax dfunc elements} {
1289     global hist
1290     global sessionWait
1291     global targets
1292
1293     set zz z39$tno
1294     set host $hist($setNo,$tno,host)
1295
1296     if {![lindex $targets($host) 5]} {
1297         set elements {}
1298     }
1299
1300     $zz.$setNo elementSetNames $elements
1301     $zz.$setNo recordElements $elements
1302     set toGet [expr 1 + $setMax - $setOffset]
1303
1304     $zz callback [list search-response $zz.$setNo]
1305
1306     while {$setMax > 0 && $toGet > 0} {
1307         for {set got 0} {$got < $toGet} {incr got} {
1308             if {[$zz.$setNo recordType [expr $setOffset + $got]] == ""} {
1309                 break
1310             }
1311         }
1312         if {$got < $toGet} {
1313             set sessionWait 0
1314             $zz.$setNo present $setOffset $toGet
1315             if {[catch {egw_wait sessionWait 300}]} {
1316                 egw_log debug "timeout/cancel in present"
1317                 $zz disconnect
1318                 break
1319             }
1320             if {$sessionWait == "0"} {
1321                 $zz disconnect
1322             }
1323             if {$sessionWait != "1"} {
1324                 break
1325             }
1326             set got [$zz.$setNo numberOfRecordsReturned]
1327             if {$got <= 0} {
1328                 break
1329             }
1330         }
1331         display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $setNo $tno
1332         set setOffset [expr $got + $setOffset]
1333         set toGet [expr 1 + $setMax - $setOffset]
1334         egw_flush
1335     }
1336 }
1337
1338 proc buttons-result-set-s {setNo targetNo setMax startPos after} {
1339     global sessionId
1340     global useIcons
1341     global env
1342     global hist
1343
1344     set zz z39$targetNo
1345     html "<p>\n"
1346     button-main
1347     if {$setMax > 0 && $setMax < [$zz.$setNo resultCount]} {
1348         if {!$useIcons} {
1349             html "\n | "
1350         }
1351         html {<a href="http:} $env(SCRIPT_NAME)
1352         html / $sessionId {/search.egw/} $setNo + $targetNo + 
1353         html [expr $setMax + 1] + [expr $setMax + $hist($setNo,maxPresent)]
1354         if {$useIcons} {
1355             html {"><img src="/egwgif/button-next-records.gif" }
1356             html {alt="Next Records" border=0></a>}
1357         } else {
1358             html {">Next Records</a>}
1359         }
1360     }
1361     if {$setMax > 0 && $startPos != "" && $startPos != "1"} {
1362         if {!$useIcons} {
1363             html "\n | "
1364         }
1365         html {<a href="http:} $env(SCRIPT_NAME)
1366         html / $sessionId {/search.egw/} $setNo + $targetNo
1367         html + [expr $startPos - $hist($setNo,maxPresent)]
1368         html + [expr $startPos - 1]
1369         if {$useIcons} {
1370             html {"><img src="/egwgif/button-previous-records.gif" }
1371             html {alt="Previous Records" border=0></a>}
1372         } else {
1373             html {">Previous Records</a>} 
1374         }
1375     }
1376     if {$targetNo > 0} {
1377         button-result-set $setNo $targetNo
1378     }
1379     button-new-query $setNo
1380     button-new-target
1381     button-view-history
1382
1383     html "<p>\n"
1384 }
1385
1386 proc score-sort {l r} {
1387     return [expr [lindex $r 0] - [lindex $l 0]]
1388 }
1389
1390 proc display-result-set-m-score {setNo} {
1391     global hist
1392     global useIcons
1393     global zstatus
1394     global targets
1395
1396     set not $hist($setNo,0,host)
1397     for {set i 1} {$i <= $not} {incr i} {
1398         if {$zstatus($i) != 2} continue
1399         set status [z39$i.$setNo responseStatus]
1400         if {[lindex $status 0] != "DBOSD"} continue        
1401         set nor $hist($setNo,$i,offset)
1402         for {set j 1} {$j <= $nor} {incr j} {
1403             if {![string compare [z39$i.$setNo recordType $j] WAIS]} {
1404                 set score [z39$i.$setNo getWAIS $j score]
1405             } elseif {![string compare [z39$i.$setNo recordType $j] USmarc]} {
1406                 set score [z39$i.$setNo getMarc $j field 999 * r]
1407                 if {[scan $score %d score] != 1} {
1408                     set score 10
1409                 }
1410             } else {
1411                 set score 10
1412             }
1413             if {$score > 0} {
1414                 lappend scoreArray [list $score $i $j]
1415             }
1416         }
1417     }
1418     if {![info exists scoreArray]} {
1419         html "<br><h3>Search produced no result</h3><br>\n"
1420     } else {
1421         html "<ul>\n"
1422         set scoreSorted [lsort -command score-sort $scoreArray]
1423         foreach r $scoreSorted {
1424             set i [lindex $r 1]
1425             set j [lindex $r 2]
1426             display-$hist($setNo,format) z39$i.$setNo $j $setNo $i
1427         }
1428         html "<br></ul>\n"
1429     }
1430     for {set i 1} {$i <= $not} {incr i} {
1431         if {$zstatus($i) != 2} continue
1432         set status [z39$i.$setNo responseStatus]
1433         if {[lindex $status 0] == "NSD"} {
1434             z39$i.$setNo nextResultSetPosition 0
1435             set code [lindex $status 1]
1436             set msg [lindex $status 2]
1437             set addinfo [lindex $status 3]
1438             html {<dt>} [lindex $targets($hist($setNo,$i,host)) 0] 
1439             html "<dd>Error: $msg: $addinfo (code $code)<br>\n"
1440         }
1441     }
1442     html "\n<br>"
1443 }
1444
1445 proc display-result-set-m-server {setNo} {
1446     global hist
1447     global useIcons
1448     global zstatus
1449     global targets
1450     global env
1451     global sessionId
1452
1453     set not $hist($setNo,0,host)
1454     html "<dl>\n"
1455     for {set i 1} {$i <= $not} {incr i} {
1456         if {$zstatus($i) != 2} continue
1457         set status [z39$i.$setNo responseStatus]
1458         if {[lindex $status 0] == "NSD"} {
1459             html "<dt><h3>" [lindex $targets($hist($setNo,$i,host)) 0] ": "
1460             z39$i.$setNo nextResultSetPosition 0
1461             set code [lindex $status 1]
1462             set msg [lindex $status 2]
1463             set addinfo [lindex $status 3]
1464             html "Error</h3>\n<dd>NSD$code: $msg: $addinfo"
1465         } else {
1466             html {<dt><a href="http:} $env(SCRIPT_NAME)
1467             html / $sessionId {/search.egw/} $setNo + $i + 1
1468             html + $hist($setNo,maxPresent) {">}
1469             html "<h3>" [lindex $targets($hist($setNo,$i,host)) 0] "</a>: "
1470             set r [z39$i.$setNo resultCount]
1471             html "$r hits</h3>\n<dd>\n"
1472             
1473             if {$hist($setNo,$i,offset) > $hist($setNo,maxPresent)} {
1474                 set nor $hist($setNo,maxPresent)
1475             } else {
1476                 set nor $hist($setNo,$i,offset)
1477             }
1478             display-rec 1 $nor display-$hist($setNo,format) $setNo $i
1479         }
1480         html "\n"
1481     }
1482     html "</dl>\n"
1483 }
1484
1485 proc display-result-set-m {setNo} {
1486     global hist
1487     global useIcons
1488     global zstatus
1489     global targets
1490
1491     egw_log debug "sort=$hist($setNo,sort)"
1492     switch $hist($setNo,sort) {
1493         score {
1494             display-result-set-m-score $setNo
1495         }
1496         default {
1497             display-result-set-m-server $setNo
1498         }
1499     }
1500 }
1501
1502 proc display-result-set-s {setNo targetNo startPos endPos} {
1503     global hist
1504     global useIcons
1505
1506     set zz z39$targetNo
1507     set host $hist($setNo,$targetNo,host)
1508     set idAuth $hist($setNo,$targetNo,idAuthentication)
1509     set database $hist($setNo,$targetNo,database)
1510     set query $hist($setNo,$targetNo,query)
1511
1512     set useIcons 1
1513
1514     if {$startPos == ""} {
1515         if {[z39search $setNo 1 $targetNo B] != "1"} {
1516             return
1517         }
1518         set r [$zz.$setNo resultCount]
1519
1520         set setMax [$zz.$setNo resultCount]
1521         if {$setMax > $hist($setNo,maxPresent)} {
1522             set setMax $hist($setNo,maxPresent)
1523         }
1524         buttons-result-set-s $setNo $targetNo $setMax $startPos 0
1525
1526         set setOffset [$zz.$setNo numberOfRecordsReturned]
1527         if {$setMax > 0} {
1528             html {<h3> Records 1-} $setMax " out of $r</h3>\n"
1529         } else {
1530             html "<h3> No hits</h3>\n"
1531         }
1532         egw_flush
1533         html "<ul>\n"
1534         display-rec 1 $setMax display-brief $setNo $targetNo
1535         incr setOffset
1536
1537     } else {
1538         if {[z39search $setNo 0 $targetNo B] != "1"} {
1539             return 
1540         }
1541         set r [$zz.$setNo resultCount]
1542         set setOffset $startPos
1543         set setMax [$zz.$setNo resultCount]
1544         if {$setMax > $endPos} {
1545             set setMax $endPos
1546         }
1547         buttons-result-set-s $setNo $targetNo $setMax $startPos 0
1548         if {$setMax > 0} {
1549             html {<h3> Records } $startPos {-} $setMax " out of $r</h3>\n"
1550         } else {
1551             html "<h3> No hits</h3>\n"
1552         }
1553         egw_flush
1554         html "<ul>\n"
1555     }
1556     if {$setMax > 0} {
1557         z39present $setNo $targetNo $setOffset $setMax display-brief B
1558     }
1559     html "</ul>\n"
1560     set useIcons 0
1561     buttons-result-set-s $setNo $targetNo $setMax $startPos 1
1562 }
1563
1564 proc z39history {} {
1565     global nextSetNo
1566     global hist
1567     global env
1568     global sessionId
1569     global targets
1570     global html3
1571     global scriptQuery
1572
1573     if {![info exists nextSetNo]} {
1574         return
1575     }
1576     html "<h2>History</h2><br>\n"
1577     if {$html3} {
1578         html {<table width=500 border=1><tr>}
1579         html {<td align=center><b>Target</b>}
1580         html {<td align=center><b>Database</b>}
1581         html {<td align=center><b>Hits</b>}
1582         html {<td align=center><b>Query</b>}
1583         html {<tr>} "\n"
1584     } else {
1585         html {<dl>} "\n"
1586     }
1587     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
1588         if {[info exists hist($setNo,scan)]} {
1589             if {$hist($setNo,scan) > 0} continue
1590         }
1591         if {[info exists hist($setNo,1,host)]} {
1592             set start 1
1593             set end $hist($setNo,0,host)
1594         } else {
1595             set start 0
1596             set end 0
1597         }
1598         for {set i $start} {$i <= $end} {incr i} {
1599             if {$html3} {
1600                 html {<td align=left>}
1601             } else {
1602                 html {<dt> }
1603             }
1604             set host $hist($setNo,$i,host)
1605             html [lindex $targets($host) 0]
1606             if {$html3} {
1607                 html {<td align=left>} [join $hist($setNo,$i,database)]
1608             } else {
1609                 if {[llength [lindex $targets($host) 1]] > 1} {
1610                     html ": "
1611                     foreach b $hist($setNo,$i,database) {
1612                         html " $b"
1613                     }
1614                 }
1615                 html {. }
1616             }
1617             if {$html3} {
1618                 html {<td align=right>}
1619             }
1620             if {[info exists hist($setNo,$i,hits)]} {
1621                 html { <a href="http:} $env(SCRIPT_NAME)
1622                 html / $sessionId {/search.egw/} $setNo + $i + 1
1623                 html + $hist($setNo,maxPresent)
1624                 html {">} $hist($setNo,$i,hits) {</a>}
1625             } else {
1626                 if {$html3} {
1627                     html {Failed}
1628                 } else {
1629                     html {Search failed.}
1630                 }
1631             }
1632             if {$html3} {
1633                 html {<td align=left>}
1634             } else {
1635                 html "<dd>\n"
1636             }
1637             html { <a href="http:} $env(SCRIPT_NAME)
1638             html / $sessionId / $scriptQuery {;/} $setNo + $host
1639             if {$html3} {
1640                 html {">}
1641             } else {
1642                 html {">Query</a>: }
1643             }
1644             set op {}
1645             for {set j 1} {$j <= 10} {incr j} {
1646                 if {![info exists hist($setNo,form,entry$j)]} {
1647                     break
1648                 }
1649                 if {[string length $hist($setNo,form,entry$j)] > 0} {
1650                     html " <b>" [join $op " "] "</b> "
1651                 set pre [join $hist($setNo,form,menu$j)]
1652                     if {[string length $pre] > 0} {
1653                         html $pre "="
1654                     }
1655                     html $hist($setNo,form,entry$j)
1656                     set op $hist($setNo,form,logic$j)
1657                 }
1658             }
1659             if {$html3} {
1660                 html {</a><tr>} "\n"
1661             }
1662         }
1663     }
1664     if {$html3} {
1665         html {</table><p>}
1666     } else {
1667         html {</dl>}
1668     }
1669     html "\n"
1670 }
1671
1672 proc displayError {msga msgb} {
1673     html "<p><center>\n"
1674     html {<img src="/egwgif/noway.gif" alt="Error">}
1675     html "<h2>" $msga "</h2>\n"
1676     if {[string length $msgb] > 0} {
1677         html "<h3>" $msgb "</h3>\n"
1678     }
1679     html "</center><p>\n"
1680 }
1681
1682 proc button-main {} {
1683     global useIcons
1684     html {<a href="http://europagate.dtv.dk/">}
1685     if {$useIcons} {
1686         html {<img src="/egwgif/button-egw.gif" alt="Europagate" border=0></a>}
1687     } else {
1688         html {Europagate</a>}
1689     }
1690 }
1691
1692 proc button-feedback {} {
1693     global useIcons
1694
1695     html {<a href="http://europagate.dtv.dk/wwwquest.html">}
1696     if {$useIcons} {
1697         html {<img src="/egwgif/button-user-feedback.gif" alt="User Feedback"
1698         border=0></a>}
1699     } else {
1700         html {User Feedback</a>}
1701     }    
1702 }
1703
1704 proc button-define-target {} {
1705     global useIcons
1706     global env
1707     global sessionId
1708
1709     if {!$useIcons} {
1710         html "\n | "
1711     }
1712     html {<a href="http:} $env(SCRIPT_NAME)
1713     html / $sessionId {/tform.egw}
1714     if {$useIcons} {
1715         html {"><img src="/egwgif/button-define-target.gif" }
1716         html {alt="Define Target" border=0></a>}
1717     } else {
1718         html {">Define Target</a>}
1719     }
1720 }
1721
1722 proc button-new-target {} {
1723     global useIcons
1724     global env
1725     global sessionId
1726     global scriptTarget
1727
1728     if {[string length $scriptTarget] == 0} return
1729
1730     if {!$useIcons} {
1731         html "\n | "
1732     }
1733     html {<a href="http:} $env(SCRIPT_NAME)
1734     html / $sessionId / $scriptTarget
1735     if {$useIcons} {
1736         html {"><img src="/egwgif/button-new-target.gif" }
1737         html {alt="New Target" border=0></a>}
1738     } else {
1739         html {">New Target</a>}
1740     }
1741 }
1742
1743 proc button-view-history {} {
1744     global useIcons
1745     global env
1746     global sessionId
1747     global nextSetNo
1748
1749     if {!$useIcons} {
1750         html "\n | "
1751     }
1752     html {<a href="http:} $env(SCRIPT_NAME)
1753     html / $sessionId {/history.egw;}
1754     catch { html "/" $nextSetNo}
1755     if {$useIcons} {
1756         html {"><img src="/egwgif/button-view-history.gif" alt="View History" }
1757         html {border=0></a>}
1758     } else {
1759         html {">View History</a>}
1760     }
1761 }
1762
1763 proc button-new-query {setNo} {
1764     global useIcons
1765     global env
1766     global sessionId
1767     global hist
1768     global scriptQuery
1769
1770     if {!$useIcons} {
1771         html "\n | "
1772     }
1773     html {<a href="http:} $env(SCRIPT_NAME)
1774     html / $sessionId / $scriptQuery {;/} $setNo + $hist($setNo,0,host) {">}
1775
1776     if {$useIcons} {
1777         html {<img src="/egwgif/button-new-query.gif" }
1778         html {alt="New Query" border=0></a>}
1779     } else {
1780         html {New Query</a>}
1781     }
1782 }
1783
1784 proc button-result-set {setNo tno} {
1785     global useIcons
1786     global env
1787     global sessionId
1788     global hist
1789
1790     if {!$useIcons} {
1791         html "\n | "
1792     }
1793     html {<a href="http:} $env(SCRIPT_NAME) / $sessionId 
1794     if {$tno > 0} {
1795         html {/msearch.egw/} $setNo
1796     } else {
1797         html {/search.egw/} $setNo + $tno
1798     }
1799     html + 1 + $hist($setNo,maxPresent)
1800     if {$useIcons} {
1801         html {"><img src="/egwgif/button-result-set.gif" }
1802         html {alt="Result Set" border=0></a>}
1803     } else {
1804         html {">Result Set</a>}
1805     }
1806 }
1807
1808 proc button-scan-window {setNo} {
1809     global useIcons
1810     global env
1811     global sessionId
1812     global hist
1813
1814     if {!$useIcons} {
1815         html "\n | "
1816     }
1817     set targetNo 0
1818     html {<a href="http:} $env(SCRIPT_NAME)
1819     html / $sessionId {/search.egw/} $setNo + $targetNo + {scan} {">}
1820     if {$useIcons} {
1821         html {<img src="/egwgif/button-scan-window.gif" }
1822         html {alt="Scan" border=0></a>}
1823     } else {
1824         html {Scan</a>}
1825     }
1826 }
1827
1828 proc maintenance {} {
1829     html {<hr>This page is maintained by }
1830     html {<a href="mailto:pwh@dtv.dk"> Peter Wad Hansen </a>.}
1831     html {Last modified March 15, 1996. <br>}
1832 }
1833
1834 proc splitHostSpec {host} {
1835     set i [string first / $host]
1836     if {$i > 1} {
1837         incr i -1
1838         return [string range $host 0 $i]
1839     }
1840     return $host
1841 }
1842
1843 proc splitDatabaseSpec {host} {
1844     set i [string first / $host]
1845     if {$i > 1} {
1846         incr i
1847         regsub -all -- - [string range $host $i end] { } res
1848         return $res
1849     }
1850     regsub -all -- - $host {} res
1851     return $res
1852 }
1853
1854 proc mergeHostSpec {host databases} {
1855     return ${host}/[join $databases -]
1856 }
1857
1858 proc mkAssoc {assoc host} {
1859     global targets
1860
1861     if {[catch {$assoc failback fail-response}]} {
1862         if {![string compare [lindex $targets($host) 1] WAIS]} {
1863             wais $assoc
1864         } else {
1865             ir $assoc
1866             $assoc maximumRecordSize 1000000
1867         }
1868     } else {
1869         if {![string compare [lindex $targets($host) 1] WAIS]} {
1870             if {[$assoc comstack] == "wais"} return
1871             wais $assoc
1872         } else {
1873             if {[$assoc comstack] == "tcpip"} return
1874             ir $assoc
1875             $assoc maximumRecordSize 1000000
1876         }
1877     }
1878 }
1879
1880 proc serverList {headlineProc targetProc} {
1881     global targets
1882     global groupsDescription
1883    
1884     proc targetsCmp {l r} {
1885         global targets
1886         return [string compare [string tolower [lindex $targets($l) 0]] \
1887                                [string tolower [lindex $targets($r) 0]]]
1888     }
1889     proc groupCmp {l r} {
1890         global groupsOrder
1891         if {[catch {set lo $groupsOrder($l)}]} {
1892             set lo 10
1893         }
1894         if {[catch {set ro $groupsOrder($r)}]} {
1895             set ro 10
1896         }
1897         return [expr $lo - $ro]
1898     }
1899     
1900     foreach tt [array names targets] {
1901         lappend groupsTmp([lindex $targets($tt) 6]) $tt
1902     }
1903     set gts [lsort -command groupCmp [array names groupsTmp]]
1904     foreach gt $gts {
1905         if {[info exists groupsDescription($gt)]} {
1906             eval $headlineProc [list $groupsDescription($gt)]
1907         } else {
1908             eval $headlineProc $gt
1909         }
1910         set tn [lsort -command targetsCmp $groupsTmp($gt)]
1911         foreach t $tn {
1912             eval $targetProc $t
1913         }
1914     }
1915
1916     rename targetsCmp {}
1917 }
1918
1919 proc session-lost {} {
1920     global useIcons
1921
1922     html {<head><title>WWW/Z39.50 Gateway: Session Expired</title></head>}
1923     html \n {<body>}
1924     set useIcons 1
1925     button-main
1926     html {<h1>Session Expired</h1>}
1927     html {Your session has expired. Please reload the gateways' }
1928     html {front page.<br><br>} \n
1929     set useIcons 0
1930     button-main
1931     html {</body></html>}
1932 }
1933
1934 if {[info exists utilExtension]} {
1935     source $utilExtension
1936 }
1937