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