2 # $Id: z39util.tcl,v 1.22 1996/01/23 14:22:03 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 corp [$zset getMarc $no field 110 * a]
97 set meet [$zset getMarc $no field 111 * a]
98 set title [$zset getMarc $no field 245 * a]
99 if {[llength $author] == 0} {
100 set cover [$zset getMarc $no field 245 * {[bc]}]
102 set cover [$zset getMarc $no field 245 * b]
104 set location [$zset getMarc $no field 260 * a]
105 set publisher [$zset getMarc $no field 260 * b]
106 set year [$zset getMarc $no field 260 * c]
147 foreach v $location {
150 foreach v $publisher {
156 html { -- <a href="http:} $env(SCRIPT_NAME) /
157 html $sessionId {/showfull.egw/} $setNo + $tno + $no + full {">}
158 html "<em>view full</em></a>"
163 proc display-raw {zset no tno} {
164 set type [$zset type $no]
166 set err [lindex [$zset diag $no] 1]
167 set add [lindex [$zset diag $no] 2]
171 html "<h3>${no}</h3>\n"
172 html "Error ${err}${add} <br>\n"
178 set rtype [$zset recordType $no]
179 if {$rtype == "SUTRS"} {
180 html [join [$zset getSutrs $no]] "<br>\n"
183 if {[catch {set r [$zset getMarc $no line * * *]}]} {
184 html "Unknown record type: $rtype <br>\n"
188 set tag [lindex $line 0]
189 set indicator [lindex $line 1]
190 set fields [lindex $line 2]
191 set l [string length $indicator]
194 for {set i 0} {$i < $l} {incr i} {
195 if {[string index $indicator $i] == " "} {
198 html [string index $tag $i]
203 foreach field $fields {
204 set id [lindex $field 0]
205 set data [lindex $field 1]
207 html " <b>\$$id</b> "
215 proc put-marc-contents {cc} {
217 if {[string first :// $cc] > 0} {
218 foreach urltype {gopher http ftp mailto} {
219 if {[string first ${urltype}:// $cc] == 0} {
234 proc dl-marc-field {zset no tag id la lb sep} {
236 set c [$zset getMarc $no field $tag * $id]
241 if {$len > 1 && "x$lb" != "x"} {
242 html "<dt><b>$lb</b>\n<dd>"
244 html "<dt><b>$la</b>\n<dd>"
250 put-marc-contents $cc
256 proc dd-marc-field {zset no tag id start stop} {
258 set c [$zset getMarc $no field $tag * $id]
265 put-marc-contents $cc
272 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
274 set lines [$zset getMarc $no line $tag * *]
275 foreach line $lines {
276 foreach field [lindex $line 2] {
278 html "<dt><b>$lead</b>"
281 set id [lindex $field 0]
282 if {$id == $startid} {
288 html [lindex $field 1]
291 html [lindex $field 1]
300 proc display-full {zset no tno} {
301 set type [$zset type $no]
303 set err [lindex [$zset diag $no] 1]
304 set add [lindex [$zset diag $no] 2]
308 html "Error ${err}${add} <br>\n"
314 set rtype [$zset recordType $no]
315 if {$rtype == "SUTRS"} {
316 html [join [$zset getSutrs $no]] "<br>\n"
319 if {[catch {set r [$zset getMarc $no line * * *]}]} {
320 html "Unknown record type: $rtype <br>\n"
324 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
326 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
328 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
330 set n [dl-marc-field $zset $no 110 a "Corporate Name" {} ", "]
332 set n [dl-marc-field $zset $no 711 a "Meeting Name" {} ", "]
334 dd-marc-field $zset $no 711 {[bndc]} " " ""
336 set n [dl-marc-field $zset $no 111 a "Meeting Name" {} ", "]
338 dd-marc-field $zset $no 111 {[bndc]} " " " "
341 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
343 dd-marc-field $zset $no 245 b "<em>" "</em>"
344 dd-marc-field $zset $no 245 c " " ""
346 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
348 dl-marc-field $zset $no 520 a "Abstract" {} ", "
349 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
350 dl-marc-field $zset $no 650 * "Subject" {} ", "
351 dl-marc-field $zset $no 260 * "Publisher" {} " "
352 dl-marc-field $zset $no 300 * "Physical Description" {} " "
354 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
356 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
358 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
360 dl-marc-field $zset $no 513 a "Report type" {} ", "
361 dl-marc-field $zset $no 513 b "Period covered" {} ", "
362 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
363 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
364 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
366 dl-marc-field $zset $no 350 a "Price" {} ", "
367 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
368 dl-marc-field $zset $no 850 a "Holdings" {} ", "
370 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
372 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
377 dl-marc-field $zset $no 270 a "Street" {} ", "
378 dl-marc-field $zset $no 270 b "City" {} ", "
379 dl-marc-field $zset $no 270 c "State" {} ", "
380 dl-marc-field $zset $no 270 e "Zip code" {} ", "
381 dl-marc-field $zset $no 270 d "Country" {} ", "
382 dl-marc-field $zset $no 270 m "Network address" {} ", "
383 dl-marc-field $zset $no 301 a "Service hours" {} ", "
384 dl-marc-field $zset $no 270 k "Phone" {} ", "
385 dl-marc-field $zset $no 270 l "Fax" {} ", "
387 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
393 dl-marc-field $zset $no 010 a "LC control number" {} ", "
394 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
395 dl-marc-field $zset $no 020 a "ISBN" {} ", "
396 dl-marc-field $zset $no 022 a "ISSN" {} ", "
397 set url [$zset getMarc $no field 856 * u]
398 set sp [$zset getMarc $no field 856 * 3]
399 if {"x$url" != "x"} {
400 html "<dt><b>URL</b>\n"
404 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
406 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
407 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
408 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
409 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
410 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
411 dl-marc-field $zset $no 001 * "Local control number" {} ", "
416 proc display-rec {from to dfunc tno} {
420 while {$from <= $to} {
421 eval "$dfunc z39${tno}.${setNo} $from $tno"
425 while {$from <= $to} {
426 eval "$dfunc z39.${setNo} $from 0"
432 proc build-scan {t i} {
435 set term [wform entry$i]
437 set field [join [wform menu$i]]
439 foreach x [lindex $targets($t) 2] {
440 if {[lindex $x 0] == $field} {
441 set attr [lindex $x 1]
444 return [list $term $attr]
449 proc build-query {t ilines} {
454 for {set i 1} {$i <= $ilines} {incr i} {
455 set term [join [wform entry$i]]
456 if {[string length $term] > 0} {
457 set field [join [wform menu$i]]
458 foreach x [lindex $targets($t) 2] {
459 if {[lindex $x 0] == $field} {
460 set attr [lindex $x 1]
465 { set q "@and $q ${attr} ${term}" }
467 { set q "@or $q ${attr} ${term}" }
469 { set q "@not $q ${attr} ${term}" }
471 { set q "${attr} ${term}" }
473 set op [wform logic$i]
479 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
486 set host $hist($setNo,$tno,host)
487 set idAuth $hist($setNo,$tno,idAuthentication)
488 set database $hist($setNo,$tno,database)
489 set scanAttr $hist($setNo,$tno,scanAttr)
490 set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
493 set host $hist($setNo,host)
494 set idAuth $hist($setNo,idAuthentication)
495 set database $hist($setNo,database)
496 set scanAttr $hist($setNo,scanAttr)
497 set scanTerm $hist($setNo,$scanNo,scanTerm)
499 if {[catch [list $zz failback fail-response]]} {
502 if {[catch [list set oldHost [$zz connect]]]} {
505 set zs $zz.s$scanNo.$setNo
506 $zz callback ok-response
507 $zz failback fail-response
508 if {$oldHost != $host} {
509 catch [list $zz disconnect]
512 if {[catch [list $zz connect $host]]} {
513 displayError "Cannot connect to target" $host
515 } elseif {$sessionWait == 0} {
516 if {[catch {zwait sessionWait 300}]} {
518 displayError "Cannot connect to target" $host
521 if {$sessionWait != 1} {
522 displayError "Cannot connect to target" $host
526 $zz idAuthentication $idAuth
528 if {[catch {$zz init}]} {
529 displayError "Cannot initialize target" $host
533 if {[catch {zwait sessionWait 60}]} {
534 displayError "Cannot initialize target" $host
538 if {$sessionWait != "1"} {
539 displayError "Cannot initialize target" $host
543 if {![$zz initResult]} {
544 set u [$zz userInformationField]
546 displayError "Cannot initialize target $host" $u
550 if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
554 eval $zz databaseNames $database
558 $zs numberOfTermsRequested $scanLines
559 $zs preferredPositionInResponse $scanPos
561 $zz callback [list scan-response $zs]
564 $zs scan "${scanAttr} ${scanTerm}"
566 if {[catch {zwait sessionWait 60}]} {
567 wlog debug "timeout/cancel in scan"
568 displayError "Timeout in scan" {}
569 html "</body></html>\n"
573 if {$sessionWait == -1} {
574 displayError "Scan fail" "Connection closed"
575 html "</body></html>\n"
578 if {$sessionWait != 1} {
584 proc display-scan {setNo scanNo tno} {
595 set zs $zz.s$scanNo.$setNo
596 set m [$zs numberOfEntriesReturned]
599 set t [lindex [$zs scanLine 0] 1]
601 set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
603 set hist($setNo,[expr $scanNo - 1],scanTerm) $t
605 set t [lindex [$zs scanLine [expr $m - 1]] 1]
607 set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
609 set hist($setNo,[expr $scanNo + 1],scanTerm) $t
612 for {set i 0} {$i < $m} {incr i} {
613 regsub -all {\ } [lindex [$zs scanLine $i] 1] + tterm
614 html {<a href="http:} $env(SCRIPT_NAME)
615 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
616 html $hist($setNo,scan) + $tterm {">}
617 html [lindex [$zs scanLine $i] 1]
619 html [lindex [$zs scanLine $i] 2]
624 proc z39search {setNo piggy tno elements} {
631 set host $hist($setNo,$tno,host)
632 set idAuth $hist($setNo,$tno,idAuthentication)
633 set database $hist($setNo,$tno,database)
634 set query $hist($setNo,$tno,query)
637 set host $hist($setNo,host)
638 set idAuth $hist($setNo,idAuthentication)
639 set database $hist($setNo,database)
640 set query $hist($setNo,query)
642 if {[catch [list $zz failback fail-response]]} {
645 if {[catch [list set oldHost [$zz connect]]]} {
648 $zz callback ok-response
649 $zz failback fail-response
650 if {$oldHost != $host} {
651 catch [list $zz disconnect]
654 if {[catch [list $zz connect $host]]} {
655 displayError "Cannot connect to target" $host
657 } elseif {$sessionWait == 0} {
658 if {[catch {zwait sessionWait 300}]} {
660 displayError "Cannot connect to target" $host
663 if {$sessionWait != 1} {
664 displayError "Cannot connect to target" $host
668 $zz idAuthentication $idAuth
670 if {[catch {$zz init}]} {
671 displayError "Cannot initialize target" $host
675 if {[catch {zwait sessionWait 60}]} {
676 displayError "Cannot initialize target" $host
680 if {$sessionWait != "1"} {
681 displayError "Cannot initialize target" $host
685 if {![$zz initResult]} {
686 set u [$zz userInformationField]
688 displayError "Cannot initialize target $host" $u
692 if {[info exists hist($setNo,hits)] && \
693 ![catch [list $zz.$setNo smallSetUpperBound 0]]} {
698 ir-set $zz.$setNo $zz
700 if {![lindex $targets($host) 5]} {
703 $zz.$setNo smallSetElementSetNames $elements
704 $zz.$setNo mediumSetElementSetNames $elements
705 $zz.$setNo recordElements $elements
707 wlog debug "database=$database"
708 eval $zz.$setNo databaseNames $database
710 $zz.$setNo preferredRecordSyntax USMARC
712 $zz callback [list search-response $zz.$setNo]
714 $zz.$setNo largeSetLowerBound 999999
715 $zz.$setNo smallSetUpperBound 0
716 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
718 $zz.$setNo largeSetLowerBound 2
719 $zz.$setNo smallSetUpperBound 0
720 $zz.$setNo mediumSetPresentNumber 0
723 $zz.$setNo search $query
725 if {[catch {zwait sessionWait 600}]} {
726 wlog debug "timeout/cancel in search"
727 displayError "Timeout in search" {}
728 html "</body></html>\n"
733 if {$sessionWait == -1} {
734 displayError "Search fail" "Connection closed"
735 html "</body></html>\n"
738 if {$sessionWait != 1} {
741 set hist($setNo,hits) [$zz.$setNo resultCount]
745 proc init-m-response {i} {
749 wlog debug "init-m-response"
755 proc connect-m-response {i} {
759 wlog debug "connect-m-response"
760 z39$i callback [list init-m-response $i]
761 if {[catch {z39$i init}]} {
767 proc fail-m-response {i} {
771 wlog debug "fail-m-response"
776 proc search-m-response {setNo i} {
784 proc z39msearch {setNo piggy elements} {
790 set not $hist($setNo,0,host)
792 for {set i 1} {$i <= $not} {incr i} {
793 set host $hist($setNo,$i,host)
794 if {[catch {z39 failback fail-response}]} {
797 if {[catch {set oldHost [z39$i connect]}]} {
800 if {$oldHost != $host} {
801 catch {z39$i disconnect}
803 z39$i callback [list connect-m-response $i]
804 z39$i failback [list fail-m-response $i]
807 for {set i 1} {$i <= $not} {incr i} {
808 set oldHost [z39$i connect]
809 set host $hist($setNo,$i,host)
810 if {$oldHost == $host} {
814 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
815 html "Connecting to target " $host " <br>\n"
817 if {![catch {z39$i connect $host}]} {
822 wlog debug "Waiting for init response"
823 if {[catch {zwait zleft 10}]} {
828 for {set i 1} {$i <= $not} {incr i} {
829 html "host " $hist($setNo,$i,host) ": "
830 if {$zstatus($i) >= 1} {
832 ir-set z39$i.$setNo z39$i
833 set hist($setNo,$i,offset) 0
834 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
836 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
839 set thisElements $elements
841 z39$i.$setNo smallSetElementSetNames $thisElements
842 z39$i.$setNo mediumSetElementSetNames $thisElements
843 z39$i.$setNo recordElements $thisElements
845 z39$i.$setNo preferredRecordSyntax USMARC
846 z39$i callback [list search-m-response $setNo $i]
849 z39$i.$setNo largeSetLowerBound 999999
850 z39$i.$setNo smallSetUpperBound 0
851 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
853 z39$i.$setNo largeSetLowerBound 2
854 z39$i.$setNo smallSetUpperBound 0
855 z39$i.$setNo mediumSetPresentNumber 0
858 wlog debug "search " $hist($setNo,$i,query)
859 z39$i.$setNo search $hist($setNo,$i,query)
866 wlog debug "Waiting for search response"
867 if {[catch {zwait zleft 30}]} {
871 for {set i 1} {$i <= $not} {incr i} {
872 if {$zstatus($i) != 2} continue
873 set status [z39$i.$setNo responseStatus]
874 if {[lindex $status 0] != "NSD"} {
875 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
880 proc z39present {setNo tno setOffset setMax dfunc elements} {
887 set host $hist($setNo,$tno,host)
890 set host $hist($setNo,host)
893 if {![lindex $targets($host) 5]} {
897 $zz.$setNo elementSetNames $elements
898 $zz.$setNo recordElements $elements
899 set toGet [expr 1 + $setMax - $setOffset]
901 $zz callback [list search-response $zz.$setNo]
903 while {$setMax > 0 && $toGet > 0} {
904 for {set got 0} {$got < $toGet} {incr got} {
905 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
911 $zz.$setNo present $setOffset $toGet
912 if {[catch {zwait sessionWait 300}]} {
913 wlog debug "timeout/cancel in present"
917 if {$sessionWait == "0"} {
920 if {$sessionWait != "1"} {
923 set got [$zz.$setNo numberOfRecordsReturned]
928 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
929 set setOffset [expr $got + $setOffset]
930 set toGet [expr 1 + $setMax - $setOffset]
942 if {![info exists nextSetNo]} {
945 html "<h2>History</h2><dl><br>\n"
946 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
947 if {$hist($setNo,scan) > 0} continue
948 set host $hist($setNo,host)
949 html {<dt> } [lindex $targets($host) 0]
950 if {[llength [lindex $targets($host) 1]] > 1} {
952 foreach b $hist($setNo,database) {
958 if {[info exists hist($setNo,hits)]} {
959 html { <a href="http:} $env(SCRIPT_NAME)
960 html / $sessionId {/search.egw/} $setNo + 1
961 html + $hist($setNo,maxPresent)
962 html {">Result</a>: } $hist($setNo,hits) { hits.}
964 html {Search failed.}
967 html { <a href="http:} $env(SCRIPT_NAME)
968 html / $sessionId {/query.egw/} $host + $setNo
971 for {set i 1} {$i <= 3} {incr i} {
972 if {[string length $hist($setNo,form,entry$i)] > 0} {
973 html " <b>" [join $op " "] "</b> "
974 html $hist($setNo,form,menu$i) "=" $hist($setNo,form,entry$i)
975 set op $hist($setNo,form,logic$i)
982 proc displayError {msga msgb} {
984 html {<img src="/egwgif/noway.gif" alt="Error">}
985 html "<h2>" $msga "</h2>\n"
987 html "<h3>" $msgb "</h3>\n"
989 html "</center><p>\n"
992 proc button-europagate {} {
995 html {<img src="/egwgif/button-egw.gif" alt="Europagate" border=0></a>}
1001 proc button-new-target {more} {
1006 html {<a href="http:} $env(SCRIPT_NAME)
1007 html / $sessionId {/targets.egw}
1009 html {"><img src="/egwgif/button-new-target.gif" }
1010 html {alt="New Target" border=0></a>}
1012 html {">New Target</a>}
1021 proc button-view-history {more} {
1027 html {<a href="http:} $env(SCRIPT_NAME)
1028 html / $sessionId {/history.egw;}
1029 catch { html "/" $nextSetNo}
1031 html {"><img src="/egwgif/button-view-history.gif" alt="View History" }
1032 html {border=0></a>}
1034 html {">View History</a>}
1043 proc button-new-query {more setNo} {
1049 html {<a href="http:} $env(SCRIPT_NAME)
1050 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo {">}
1052 html {<img src="/egwgif/button-new-query.gif" }
1053 html {alt="New Query" border=0></a>}
1055 html {New Query</a>}
1064 proc button-scan-window {more setNo} {
1070 html {<a href="http:} $env(SCRIPT_NAME)
1071 html / $sessionId {/search.egw/} $setNo + {scan} {">}
1073 html {<img src="/egwgif/button-scan-window.gif" }
1074 html {alt="Scan" border=0></a>}
1085 proc maintenance {} {
1086 html {<hr>This page is maintained by }
1087 html {<a href="mailto:pwh@dtv.dk"> Peter Wad Hansen </a>.}
1088 html {Last modified 9. january 1996. <br>}
1089 html {<em> This and the following pages are under construction and }
1090 html {will continue to be so until the end of January 1996.</em>}