2 # $Id: z39util.tcl,v 1.20 1996/01/12 10:05:42 adam Exp $
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]}]} {
13 puts $f "set ${var} \{$v\}"
16 eval "set v \$${var}(\$n)"
17 puts $f "set ${var}($n) \{$v\}"
21 puts $f "set ${var} \{$v\}"
29 proc search-response {zz} {
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)"
46 proc scan-response {zz} {
49 set status [$zz scanStatus]
51 displayError "Scan fail" ""
63 proc fail-response {} {
68 proc display-brief {zset no tno} {
75 set type [$zset type $no]
77 set err [lindex [$zset diag $no] 1]
78 set add [lindex [$zset diag $no] 2]
82 html "${no} Error ${err}${add} <br>\n"
88 set rtype [$zset recordType $no]
89 if {$rtype == "SUTRS"} {
90 html [join [$zset getSutrs $no]]
95 set author [$zset getMarc $no field 100 * a]
96 set title [lindex [$zset getMarc $no field 245 * a] 0]
97 set year [lindex [$zset getMarc $no field 260 * c] 0]
110 html {<a href="http:} $env(SCRIPT_NAME) /
111 html $sessionId {/showfull.egw/} $setNo + $tno + $no + full {">}
112 if {[string length $title] == 0} {
117 html {</a>} " <i> ${year} </i>"
122 proc display-raw {zset no tno} {
123 set type [$zset type $no]
125 set err [lindex [$zset diag $no] 1]
126 set add [lindex [$zset diag $no] 2]
130 html "<h3>${no}</h3>\n"
131 html "Error ${err}${add} <br>\n"
137 set rtype [$zset recordType $no]
138 if {$rtype == "SUTRS"} {
139 html [join [$zset getSutrs $no]] "<br>\n"
142 if {[catch {set r [$zset getMarc $no line * * *]}]} {
143 html "Unknown record type: $rtype <br>\n"
147 set tag [lindex $line 0]
148 set indicator [lindex $line 1]
149 set fields [lindex $line 2]
150 set l [string length $indicator]
153 for {set i 0} {$i < $l} {incr i} {
154 if {[string index $indicator $i] == " "} {
157 html [string index $tag $i]
162 foreach field $fields {
163 set id [lindex $field 0]
164 set data [lindex $field 1]
166 html " <b>\$$id</b> "
174 proc put-marc-contents {cc} {
176 if {[string first :// $cc] > 0} {
177 foreach urltype {gopher http ftp mailto} {
178 if {[string first ${urltype}:// $cc] == 0} {
193 proc dl-marc-field {zset no tag id la lb sep} {
195 set c [$zset getMarc $no field $tag * $id]
200 if {$len > 1 && "x$lb" != "x"} {
201 html "<dt><b>$lb</b>\n<dd>"
203 html "<dt><b>$la</b>\n<dd>"
209 put-marc-contents $cc
215 proc dd-marc-field {zset no tag id start stop} {
217 set c [$zset getMarc $no field $tag * $id]
224 put-marc-contents $cc
231 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
233 set lines [$zset getMarc $no line $tag * *]
234 foreach line $lines {
235 foreach field [lindex $line 2] {
237 html "<dt><b>$lead</b>"
240 set id [lindex $field 0]
241 if {$id == $startid} {
247 html [lindex $field 1]
250 html [lindex $field 1]
259 proc display-full {zset no tno} {
260 set type [$zset type $no]
262 set err [lindex [$zset diag $no] 1]
263 set add [lindex [$zset diag $no] 2]
267 html "Error ${err}${add} <br>\n"
273 set rtype [$zset recordType $no]
274 if {$rtype == "SUTRS"} {
275 html [join [$zset getSutrs $no]] "<br>\n"
278 if {[catch {set r [$zset getMarc $no line * * *]}]} {
279 html "Unknown record type: $rtype <br>\n"
283 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
285 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
287 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
289 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
291 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
293 dd-marc-field $zset $no 245 b "<em>" "</em>"
294 dd-marc-field $zset $no 245 c " " ""
296 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
298 dl-marc-field $zset $no 520 a "Abstract" {} ", "
299 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
300 dl-marc-field $zset $no 650 * "Subject" {} ", "
301 dl-marc-field $zset $no 260 * "Publisher" {} " "
302 dl-marc-field $zset $no 300 * "Physical Description" {} " "
304 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
306 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
308 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
310 dl-marc-field $zset $no 513 a "Report type" {} ", "
311 dl-marc-field $zset $no 513 b "Period covered" {} ", "
312 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
313 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
314 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
316 dl-marc-field $zset $no 350 a "Price" {} ", "
317 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
318 dl-marc-field $zset $no 850 a "Holdings" {} ", "
320 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
322 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
327 dl-marc-field $zset $no 270 a "Street" {} ", "
328 dl-marc-field $zset $no 270 b "City" {} ", "
329 dl-marc-field $zset $no 270 c "State" {} ", "
330 dl-marc-field $zset $no 270 e "Zip code" {} ", "
331 dl-marc-field $zset $no 270 d "Country" {} ", "
332 dl-marc-field $zset $no 270 m "Network address" {} ", "
333 dl-marc-field $zset $no 301 a "Service hours" {} ", "
334 dl-marc-field $zset $no 270 k "Phone" {} ", "
335 dl-marc-field $zset $no 270 l "Fax" {} ", "
337 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
343 dl-marc-field $zset $no 010 a "LC control number" {} ", "
344 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
345 dl-marc-field $zset $no 020 a "ISBN" {} ", "
346 dl-marc-field $zset $no 022 a "ISSN" {} ", "
347 set url [$zset getMarc $no field 856 * u]
348 set sp [$zset getMarc $no field 856 * 3]
349 if {"x$url" != "x"} {
350 html "<dt><b>URL</b>\n"
354 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
356 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
357 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
358 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
359 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
360 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
361 dl-marc-field $zset $no 001 * "Local control number" {} ", "
366 proc display-rec {from to dfunc tno} {
370 while {$from <= $to} {
371 eval "$dfunc z39${tno}.${setNo} $from $tno"
375 while {$from <= $to} {
376 eval "$dfunc z39.${setNo} $from 0"
382 proc build-scan {t i} {
385 set term [wform entry$i]
387 set field [join [wform menu$i]]
389 foreach x [lindex $targets($t) 2] {
390 if {[lindex $x 0] == $field} {
391 set attr [lindex $x 1]
394 return [list $term $attr]
399 proc build-query {t ilines} {
404 for {set i 1} {$i <= $ilines} {incr i} {
405 set term [join [wform entry$i]]
406 if {[string length $term] > 0} {
407 set field [join [wform menu$i]]
408 foreach x [lindex $targets($t) 2] {
409 if {[lindex $x 0] == $field} {
410 set attr [lindex $x 1]
415 { set q "@and $q ${attr} ${term}" }
417 { set q "@or $q ${attr} ${term}" }
419 { set q "@not $q ${attr} ${term}" }
421 { set q "${attr} ${term}" }
423 set op [wform logic$i]
429 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
436 set host $hist($setNo,$tno,host)
437 set idAuth $hist($setNo,$tno,idAuthentication)
438 set database $hist($setNo,$tno,database)
439 set scanAttr $hist($setNo,$tno,scanAttr)
440 set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
443 set host $hist($setNo,host)
444 set idAuth $hist($setNo,idAuthentication)
445 set database $hist($setNo,database)
446 set scanAttr $hist($setNo,scanAttr)
447 set scanTerm $hist($setNo,$scanNo,scanTerm)
449 if {[catch [list $zz failback fail-response]]} {
452 if {[catch [list set oldHost [$zz connect]]]} {
455 set zs $zz.s$scanNo.$setNo
456 $zz callback ok-response
457 $zz failback fail-response
458 if {$oldHost != $host} {
459 catch [list $zz disconnect]
462 if {[catch [list $zz connect $host]]} {
463 displayError "Cannot connect to target" $host
465 } elseif {$sessionWait == 0} {
466 if {[catch {zwait sessionWait 300}]} {
468 displayError "Cannot connect to target" $host
471 if {$sessionWait != 1} {
472 displayError "Cannot connect to target" $host
476 $zz idAuthentication $idAuth
478 if {[catch {$zz init}]} {
479 displayError "Cannot initialize target" $host
483 if {[catch {zwait sessionWait 60}]} {
484 displayError "Cannot initialize target" $host
488 if {$sessionWait != "1"} {
489 displayError "Cannot initialize target" $host
493 if {![$zz initResult]} {
494 set u [$zz userInformationField]
496 displayError "Cannot initialize target $host" $u
500 if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
504 eval $zz databaseNames $database
508 $zs numberOfTermsRequested $scanLines
509 $zs preferredPositionInResponse $scanPos
511 $zz callback [list scan-response $zs]
514 $zs scan "${scanAttr} ${scanTerm}"
516 if {[catch {zwait sessionWait 600}]} {
517 wlog debug "timeout/cancel in scan"
518 displayError "Timeout in scan" {}
519 html "</body></html>\n"
523 if {$sessionWait == -1} {
524 displayError "Scan fail" "Connection closed"
525 html "</body></html>\n"
528 if {$sessionWait != 1} {
534 proc display-scan {setNo scanNo tno} {
545 set zs $zz.s$scanNo.$setNo
546 set m [$zs numberOfEntriesReturned]
549 set t [lindex [$zs scanLine 0] 1]
551 set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
553 set hist($setNo,[expr $scanNo - 1],scanTerm) $t
555 set t [lindex [$zs scanLine [expr $m - 1]] 1]
557 set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
559 set hist($setNo,[expr $scanNo + 1],scanTerm) $t
562 for {set i 0} {$i < $m} {incr i} {
563 regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
564 html {<a href="http:} $env(SCRIPT_NAME)
565 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
566 html $hist($setNo,scan) + $tterm {">}
567 html [lindex [$zs scanLine $i] 1]
569 html [lindex [$zs scanLine $i] 2]
574 proc z39search {setNo piggy tno elements} {
581 set host $hist($setNo,$tno,host)
582 set idAuth $hist($setNo,$tno,idAuthentication)
583 set database $hist($setNo,$tno,database)
584 set query $hist($setNo,$tno,query)
587 set host $hist($setNo,host)
588 set idAuth $hist($setNo,idAuthentication)
589 set database $hist($setNo,database)
590 set query $hist($setNo,query)
592 if {[catch [list $zz failback fail-response]]} {
595 if {[catch [list set oldHost [$zz connect]]]} {
598 $zz callback ok-response
599 $zz failback fail-response
600 if {$oldHost != $host} {
601 catch [list $zz disconnect]
604 if {[catch [list $zz connect $host]]} {
605 displayError "Cannot connect to target" $host
607 } elseif {$sessionWait == 0} {
608 if {[catch {zwait sessionWait 300}]} {
610 displayError "Cannot connect to target" $host
613 if {$sessionWait != 1} {
614 displayError "Cannot connect to target" $host
618 $zz idAuthentication $idAuth
620 if {[catch {$zz init}]} {
621 displayError "Cannot initialize target" $host
625 if {[catch {zwait sessionWait 60}]} {
626 displayError "Cannot initialize target" $host
630 if {$sessionWait != "1"} {
631 displayError "Cannot initialize target" $host
635 if {![$zz initResult]} {
636 set u [$zz userInformationField]
638 displayError "Cannot initialize target $host" $u
642 if {[info exists hist($setNo,hits)] && \
643 ![catch [list $zz.$setNo smallSetUpperBound 0]]} {
648 ir-set $zz.$setNo $zz
650 if {![lindex $targets($host) 5]} {
653 $zz.$setNo smallSetElementSetNames $elements
654 $zz.$setNo mediumSetElementSetNames $elements
655 $zz.$setNo recordElements $elements
657 wlog debug "database=$database"
658 eval $zz.$setNo databaseNames $database
660 $zz.$setNo preferredRecordSyntax USMARC
662 $zz callback [list search-response $zz.$setNo]
664 $zz.$setNo largeSetLowerBound 999999
665 $zz.$setNo smallSetUpperBound 0
666 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
668 $zz.$setNo largeSetLowerBound 2
669 $zz.$setNo smallSetUpperBound 0
670 $zz.$setNo mediumSetPresentNumber 0
673 $zz.$setNo search $query
675 if {[catch {zwait sessionWait 600}]} {
676 wlog debug "timeout/cancel in search"
677 displayError "Timeout in search" {}
678 html "</body></html>\n"
683 if {$sessionWait == -1} {
684 displayError "Search fail" "Connection closed"
685 html "</body></html>\n"
688 if {$sessionWait != 1} {
691 set hist($setNo,hits) [$zz.$setNo resultCount]
695 proc init-m-response {i} {
699 wlog debug "init-m-response"
705 proc connect-m-response {i} {
709 wlog debug "connect-m-response"
710 z39$i callback [list init-m-response $i]
711 if {[catch {z39$i init}]} {
717 proc fail-m-response {i} {
721 wlog debug "fail-m-response"
726 proc search-m-response {setNo i} {
734 proc z39msearch {setNo piggy elements} {
740 set not $hist($setNo,0,host)
742 for {set i 1} {$i <= $not} {incr i} {
743 set host $hist($setNo,$i,host)
744 if {[catch {z39 failback fail-response}]} {
747 if {[catch {set oldHost [z39$i connect]}]} {
750 if {$oldHost != $host} {
751 catch {z39$i disconnect}
753 z39$i callback [list connect-m-response $i]
754 z39$i failback [list fail-m-response $i]
757 for {set i 1} {$i <= $not} {incr i} {
758 set oldHost [z39$i connect]
759 set host $hist($setNo,$i,host)
760 if {$oldHost == $host} {
764 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
765 html "Connecting to target " $host " <br>\n"
767 if {![catch {z39$i connect $host}]} {
772 wlog debug "Waiting for init response"
773 if {[catch {zwait zleft 10}]} {
778 for {set i 1} {$i <= $not} {incr i} {
779 html "host " $hist($setNo,$i,host) ": "
780 if {$zstatus($i) >= 1} {
782 ir-set z39$i.$setNo z39$i
783 set hist($setNo,$i,offset) 0
784 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
786 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
789 set thisElements $elements
791 z39$i.$setNo smallSetElementSetNames $thisElements
792 z39$i.$setNo mediumSetElementSetNames $thisElements
793 z39$i.$setNo recordElements $thisElements
795 z39$i.$setNo preferredRecordSyntax USMARC
796 z39$i callback [list search-m-response $setNo $i]
799 z39$i.$setNo largeSetLowerBound 999999
800 z39$i.$setNo smallSetUpperBound 0
801 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
803 z39$i.$setNo largeSetLowerBound 2
804 z39$i.$setNo smallSetUpperBound 0
805 z39$i.$setNo mediumSetPresentNumber 0
808 wlog debug "search " $hist($setNo,$i,query)
809 z39$i.$setNo search $hist($setNo,$i,query)
816 wlog debug "Waiting for search response"
817 if {[catch {zwait zleft 30}]} {
821 for {set i 1} {$i <= $not} {incr i} {
822 if {$zstatus($i) != 2} continue
823 set status [z39$i.$setNo responseStatus]
824 if {[lindex $status 0] != "NSD"} {
825 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
830 proc z39present {setNo tno setOffset setMax dfunc elements} {
837 set host $hist($setNo,$tno,host)
840 set host $hist($setNo,host)
843 if {![lindex $targets($host) 5]} {
847 $zz.$setNo elementSetNames $elements
848 $zz.$setNo recordElements $elements
849 set toGet [expr 1 + $setMax - $setOffset]
851 $zz callback [list search-response $zz.$setNo]
853 while {$setMax > 0 && $toGet > 0} {
854 for {set got 0} {$got < $toGet} {incr got} {
855 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
861 $zz.$setNo present $setOffset $toGet
862 if {[catch {zwait sessionWait 300}]} {
863 wlog debug "timeout/cancel in present"
867 if {$sessionWait == "0"} {
870 if {$sessionWait != "1"} {
873 set got [$zz.$setNo numberOfRecordsReturned]
878 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
879 set setOffset [expr $got + $setOffset]
880 set toGet [expr 1 + $setMax - $setOffset]
892 if {![info exists nextSetNo]} {
895 html "<h2>History</h2><dl><br>\n"
896 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
897 if {$hist($setNo,scan) > 0} continue
898 set host $hist($setNo,host)
899 html {<dt> } [lindex $targets($host) 0]
900 if {[llength [lindex $targets($host) 1]] > 1} {
902 foreach b $hist($setNo,database) {
908 if {[info exists hist($setNo,hits)]} {
909 html { <a href="http:} $env(SCRIPT_NAME)
910 html / $sessionId {/search.egw/} $setNo + 1
911 html + $hist($setNo,maxPresent)
912 html {">Result</a>: } $hist($setNo,hits) { hits.}
914 html {Search failed.}
917 html { <a href="http:} $env(SCRIPT_NAME)
918 html / $sessionId {/query.egw/} $host + $setNo
921 for {set i 1} {$i <= 3} {incr i} {
922 if {[string length $hist($setNo,form,entry$i)] > 0} {
923 html " <b>" [join $op " "] "</b> "
924 html $hist($setNo,form,menu$i) "=" $hist($setNo,form,entry$i)
925 set op $hist($setNo,form,logic$i)
932 proc displayError {msga msgb} {
934 html {<img src="/egwgif/noway.gif" alt="Error">}
935 html "<h2>" $msga "</h2>\n"
937 html "<h3>" $msgb "</h3>\n"
939 html "</center><p>\n"
942 proc button-europagate {} {
945 html {<img src="/egwgif/button-egw.gif" alt="Europagate" border=0></a>}
951 proc button-new-target {more} {
956 html {<a href="http:} $env(SCRIPT_NAME)
957 html / $sessionId {/targets.egw}
959 html {"><img src="/egwgif/button-new-target.gif" }
960 html {alt="New Target" border=0></a>}
962 html {">New Target</a>}
971 proc button-view-history {more} {
977 html {<a href="http:} $env(SCRIPT_NAME)
978 html / $sessionId {/history.egw;}
979 catch { html "/" $nextSetNo}
981 html {"><img src="/egwgif/button-view-history.gif" alt="View History" }
984 html {">View History</a>}
993 proc button-new-query {more setNo} {
999 html {<a href="http:} $env(SCRIPT_NAME)
1000 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo {">}
1002 html {<img src="/egwgif/button-new-query.gif" }
1003 html {alt="New Query" border=0></a>}
1005 html {New Query</a>}
1014 proc maintenance {} {
1015 html {<hr>This page is maintained by }
1016 html {<a href="mailto:pwh@dtv.dk"> Peter Wad Hansen </a>.}
1017 html {Last modified 9. january 1996. <br>}
1018 html {<em> This and the following pages are under construction and }
1019 html {will continue to be so until the end of January 1996.</em>}