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