2 # $Id: z39util.tcl,v 1.19 1996/01/09 16:16:49 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 [wform menu$i]
388 foreach x [lindex $targets($t) 2] {
389 if {[lindex $x 0] == $field} {
390 set attr [lindex $x 1]
393 return [list $term $attr]
398 proc build-query {t ilines} {
403 for {set i 1} {$i <= $ilines} {incr i} {
404 set term [wform entry$i]
405 if {[string length $term] > 0} {
406 set field [wform menu$i]
407 foreach x [lindex $targets($t) 2] {
408 if {[lindex $x 0] == $field} {
409 set attr [lindex $x 1]
414 { set q "@and $q ${attr} ${term}" }
416 { set q "@or $q ${attr} ${term}" }
418 { set q "@not $q ${attr} ${term}" }
420 { set q "${attr} ${term}" }
422 set op [wform logic$i]
428 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
435 set host $hist($setNo,$tno,host)
436 set idAuth $hist($setNo,$tno,idAuthentication)
437 set database $hist($setNo,$tno,database)
438 set scanAttr $hist($setNo,$tno,scanAttr)
439 set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
442 set host $hist($setNo,host)
443 set idAuth $hist($setNo,idAuthentication)
444 set database $hist($setNo,database)
445 set scanAttr $hist($setNo,scanAttr)
446 set scanTerm $hist($setNo,$scanNo,scanTerm)
448 if {[catch [list $zz failback fail-response]]} {
451 if {[catch [list set oldHost [$zz connect]]]} {
454 set zs $zz.s$scanNo.$setNo
455 $zz callback ok-response
456 $zz failback fail-response
457 if {$oldHost != $host} {
458 catch [list $zz disconnect]
461 if {[catch [list $zz connect $host]]} {
462 displayError "Cannot connect to target" $host
464 } elseif {$sessionWait == 0} {
465 if {[catch {zwait sessionWait 300}]} {
467 displayError "Cannot connect to target" $host
470 if {$sessionWait != 1} {
471 displayError "Cannot connect to target" $host
475 $zz idAuthentication $idAuth
477 if {[catch {$zz init}]} {
478 displayError "Cannot initialize target" $host
482 if {[catch {zwait sessionWait 60}]} {
483 displayError "Cannot initialize target" $host
487 if {$sessionWait != "1"} {
488 displayError "Cannot initialize target" $host
492 if {![$zz initResult]} {
493 set u [$zz userInformationField]
495 displayError "Cannot initialize target $host" $u
499 if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
503 eval $zz databaseNames $database
507 $zs numberOfTermsRequested $scanLines
508 $zs preferredPositionInResponse $scanPos
510 $zz callback [list scan-response $zs]
513 $zs scan "${scanAttr} ${scanTerm}"
515 if {[catch {zwait sessionWait 600}]} {
516 wlog debug "timeout/cancel in scan"
517 displayError "Timeout in scan" {}
518 html "</body></html>\n"
522 if {$sessionWait == -1} {
523 displayError "Scan fail" "Connection closed"
524 html "</body></html>\n"
527 if {$sessionWait != 1} {
533 proc display-scan {setNo scanNo tno} {
544 set zs $zz.s$scanNo.$setNo
545 set m [$zs numberOfEntriesReturned]
548 set t [lindex [$zs scanLine 0] 1]
550 set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
552 set hist($setNo,[expr $scanNo - 1],scanTerm) $t
554 set t [lindex [$zs scanLine [expr $m - 1]] 1]
556 set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
558 set hist($setNo,[expr $scanNo + 1],scanTerm) $t
561 for {set i 0} {$i < $m} {incr i} {
562 regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
563 html {<a href="http:} $env(SCRIPT_NAME)
564 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
565 html $hist($setNo,scan) + $tterm {">}
566 html [lindex [$zs scanLine $i] 1]
568 html [lindex [$zs scanLine $i] 2]
573 proc z39search {setNo piggy tno elements} {
580 set host $hist($setNo,$tno,host)
581 set idAuth $hist($setNo,$tno,idAuthentication)
582 set database $hist($setNo,$tno,database)
583 set query $hist($setNo,$tno,query)
586 set host $hist($setNo,host)
587 set idAuth $hist($setNo,idAuthentication)
588 set database $hist($setNo,database)
589 set query $hist($setNo,query)
591 if {[catch [list $zz failback fail-response]]} {
594 if {[catch [list set oldHost [$zz connect]]]} {
597 $zz callback ok-response
598 $zz failback fail-response
599 if {$oldHost != $host} {
600 catch [list $zz disconnect]
603 if {[catch [list $zz connect $host]]} {
604 displayError "Cannot connect to target" $host
606 } elseif {$sessionWait == 0} {
607 if {[catch {zwait sessionWait 300}]} {
609 displayError "Cannot connect to target" $host
612 if {$sessionWait != 1} {
613 displayError "Cannot connect to target" $host
617 $zz idAuthentication $idAuth
619 if {[catch {$zz init}]} {
620 displayError "Cannot initialize target" $host
624 if {[catch {zwait sessionWait 60}]} {
625 displayError "Cannot initialize target" $host
629 if {$sessionWait != "1"} {
630 displayError "Cannot initialize target" $host
634 if {![$zz initResult]} {
635 set u [$zz userInformationField]
637 displayError "Cannot initialize target $host" $u
641 if {[info exists hist($setNo,hits)] && \
642 ![catch [list $zz.$setNo smallSetUpperBound 0]]} {
647 ir-set $zz.$setNo $zz
649 if {![lindex $targets($host) 5]} {
652 $zz.$setNo smallSetElementSetNames $elements
653 $zz.$setNo mediumSetElementSetNames $elements
654 $zz.$setNo recordElements $elements
656 wlog debug "database=$database"
657 eval $zz.$setNo databaseNames $database
659 $zz.$setNo preferredRecordSyntax USMARC
661 $zz callback [list search-response $zz.$setNo]
663 $zz.$setNo largeSetLowerBound 999999
664 $zz.$setNo smallSetUpperBound 0
665 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
667 $zz.$setNo largeSetLowerBound 2
668 $zz.$setNo smallSetUpperBound 0
669 $zz.$setNo mediumSetPresentNumber 0
672 $zz.$setNo search $query
674 if {[catch {zwait sessionWait 600}]} {
675 wlog debug "timeout/cancel in search"
676 displayError "Timeout in search" {}
677 html "</body></html>\n"
682 if {$sessionWait == -1} {
683 displayError "Search fail" "Connection closed"
684 html "</body></html>\n"
687 if {$sessionWait != 1} {
690 set hist($setNo,hits) [$zz.$setNo resultCount]
694 proc init-m-response {i} {
698 wlog debug "init-m-response"
704 proc connect-m-response {i} {
708 wlog debug "connect-m-response"
709 z39$i callback [list init-m-response $i]
710 if {[catch {z39$i init}]} {
716 proc fail-m-response {i} {
720 wlog debug "fail-m-response"
725 proc search-m-response {setNo i} {
733 proc z39msearch {setNo piggy elements} {
739 set not $hist($setNo,0,host)
741 for {set i 1} {$i <= $not} {incr i} {
742 set host $hist($setNo,$i,host)
743 if {[catch {z39 failback fail-response}]} {
746 if {[catch {set oldHost [z39$i connect]}]} {
749 if {$oldHost != $host} {
750 catch {z39$i disconnect}
752 z39$i callback [list connect-m-response $i]
753 z39$i failback [list fail-m-response $i]
756 for {set i 1} {$i <= $not} {incr i} {
757 set oldHost [z39$i connect]
758 set host $hist($setNo,$i,host)
759 if {$oldHost == $host} {
763 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
764 html "Connecting to target " $host " <br>\n"
766 if {![catch {z39$i connect $host}]} {
771 wlog debug "Waiting for init response"
772 if {[catch {zwait zleft 10}]} {
777 for {set i 1} {$i <= $not} {incr i} {
778 html "host " $hist($setNo,$i,host) ": "
779 if {$zstatus($i) >= 1} {
781 ir-set z39$i.$setNo z39$i
782 set hist($setNo,$i,offset) 0
783 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
785 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
788 set thisElements $elements
790 z39$i.$setNo smallSetElementSetNames $thisElements
791 z39$i.$setNo mediumSetElementSetNames $thisElements
792 z39$i.$setNo recordElements $thisElements
794 z39$i.$setNo preferredRecordSyntax USMARC
795 z39$i callback [list search-m-response $setNo $i]
798 z39$i.$setNo largeSetLowerBound 999999
799 z39$i.$setNo smallSetUpperBound 0
800 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
802 z39$i.$setNo largeSetLowerBound 2
803 z39$i.$setNo smallSetUpperBound 0
804 z39$i.$setNo mediumSetPresentNumber 0
807 wlog debug "search " $hist($setNo,$i,query)
808 z39$i.$setNo search $hist($setNo,$i,query)
815 wlog debug "Waiting for search response"
816 if {[catch {zwait zleft 30}]} {
820 for {set i 1} {$i <= $not} {incr i} {
821 if {$zstatus($i) != 2} continue
822 set status [z39$i.$setNo responseStatus]
823 if {[lindex $status 0] != "NSD"} {
824 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
829 proc z39present {setNo tno setOffset setMax dfunc elements} {
836 set host $hist($setNo,$tno,host)
839 set host $hist($setNo,host)
842 if {![lindex $targets($host) 5]} {
846 $zz.$setNo elementSetNames $elements
847 $zz.$setNo recordElements $elements
848 set toGet [expr 1 + $setMax - $setOffset]
850 $zz callback [list search-response $zz.$setNo]
852 while {$setMax > 0 && $toGet > 0} {
853 for {set got 0} {$got < $toGet} {incr got} {
854 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
860 $zz.$setNo present $setOffset $toGet
861 if {[catch {zwait sessionWait 300}]} {
862 wlog debug "timeout/cancel in present"
866 if {$sessionWait == "0"} {
869 if {$sessionWait != "1"} {
872 set got [$zz.$setNo numberOfRecordsReturned]
877 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
878 set setOffset [expr $got + $setOffset]
879 set toGet [expr 1 + $setMax - $setOffset]
891 if {![info exists nextSetNo]} {
894 html "<h2>History</h2><dl><br>\n"
895 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
896 if {$hist($setNo,scan) > 0} continue
897 set host $hist($setNo,host)
898 html {<dt> } [lindex $targets($host) 0]
899 if {[llength [lindex $targets($host) 1]] > 1} {
901 foreach b $hist($setNo,database) {
907 if {[info exists hist($setNo,hits)]} {
908 html { <a href="http:} $env(SCRIPT_NAME)
909 html / $sessionId {/search.egw/} $setNo + 1
910 html + $hist($setNo,maxPresent)
911 html {">Result</a>: } $hist($setNo,hits) { hits.}
913 html {Search failed.}
916 html { <a href="http:} $env(SCRIPT_NAME)
917 html / $sessionId {/query.egw/} $host + $setNo
920 for {set i 1} {$i <= 3} {incr i} {
921 if {[string length $hist($setNo,form,entry$i)] > 0} {
922 html " <b>" [join $op " "] "</b> "
923 html $hist($setNo,form,menu$i) "=" $hist($setNo,form,entry$i)
924 set op $hist($setNo,form,logic$i)
931 proc displayError {msga msgb} {
933 html {<img src="/egwgif/noway.gif" alt="Error">}
934 html "<h2>" $msga "</h2>\n"
936 html "<h3>" $msgb "</h3>\n"
938 html "</center><p>\n"
941 proc button-europagate {} {
944 html {<img src="/egwgif/button-egw.gif" alt="Europagate" border=0></a>}
950 proc button-new-target {more} {
955 html {<a href="http:} $env(SCRIPT_NAME)
956 html / $sessionId {/targets.egw}
958 html {"><img src="/egwgif/button-new-target.gif" }
959 html {alt="New Target" border=0></a>}
961 html {">New Target</a>}
970 proc button-view-history {more} {
975 html {<a href="http:} $env(SCRIPT_NAME)
976 html / $sessionId {/history.egw}
978 html {"><img src="/egwgif/button-view-history.gif" alt="View History" }
981 html {">View History</a>}
990 proc button-new-query {more setNo} {
996 html {<a href="http:} $env(SCRIPT_NAME)
997 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo {">}
999 html {<img src="/egwgif/button-new-query.gif" }
1000 html {alt="New Query" border=0></a>}
1002 html {New Query</a>}
1011 proc maintenance {} {
1012 html {<hr>This page is maintained by }
1013 html {<a href="mailto:pwh@dtv.dk"> Peter Wad Hansen </a>.}
1014 html {Last modified 9. january 1996. <br>}
1015 html {<em> This and the following pages are under construction and }
1016 html {will continue to be so until the end of January 1996.</em>}