2 # $Id: z39util.tcl,v 1.7 1995/11/10 14:47:32 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 {sno} {
32 set status [z39.$sno responseStatus]
33 if {[lindex $status 0] == "NSD"} {
34 z39.$sno nextResultSetPosition 0
35 set code [lindex $status 1]
36 set msg [lindex $status 2]
37 set addinfo [lindex $status 3]
38 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
50 proc fail-response {} {
55 proc display-brief {zset no tno} {
60 set type [$zset type $no]
62 set err [lindex [$zset diag $no] 1]
63 set add [lindex [$zset diag $no] 2]
67 html "${no} Error ${err}${add} <br>\n"
74 set rtype [$zset recordType $no]
75 if {$rtype == "SUTRS"} {
76 html [join [$zset getSutrs $no]]
81 set title [lindex [$zset getMarc $no field 245 * a] 0]
82 set year [lindex [$zset getMarc $no field 260 * c] 0]
84 html {<a href="http:} $env(SCRIPT_NAME) /
85 html $sessionId {/showfull.egw/} $setNo + $tno + $no + full
86 html {"> } $title {</a>} " <i> ${year} </i>"
91 proc display-raw {zset no tno} {
92 set type [$zset type $no]
94 set err [lindex [$zset diag $no] 1]
95 set add [lindex [$zset diag $no] 2]
99 html "<h3>${no}</h3>\n"
100 html "Error ${err}${add} <br>\n"
106 set rtype [$zset recordType $no]
107 if {$rtype == "SUTRS"} {
108 html [join [$zset getSutrs $no]] "<br>\n"
111 if {[catch {set r [$zset getMarc $no line * * *]}]} {
112 html "Unknown record type: $rtype <br>\n"
116 set tag [lindex $line 0]
117 set indicator [lindex $line 1]
118 set fields [lindex $line 2]
119 set l [string length $indicator]
122 for {set i 0} {$i < $l} {incr i} {
123 if {[string index $indicator $i] == " "} {
126 html [string index $tag $i]
130 foreach field $fields {
131 set id [lindex $field 0]
132 set data [lindex $field 1]
134 html " <b>\$$id</b> "
142 proc put-marc-contents {cc} {
144 if {[string first :// $cc] > 0} {
145 foreach urltype {gopher http ftp mailto} {
146 if {[string first ${urltype}:// $cc] == 0} {
157 html {">} $urltype { reference</a>}
161 proc dl-marc-field {zset no tag id la lb sep} {
163 set c [$zset getMarc $no field $tag * $id]
168 if {$len > 1 && "x$lb" != "x"} {
169 html "<dt><b>$lb</b>\n<dd>"
171 html "<dt><b>$la</b>\n<dd>"
177 put-marc-contents $cc
183 proc dd-marc-field {zset no tag id start stop} {
185 set c [$zset getMarc $no field $tag * $id]
192 put-marc-contents $cc
199 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
201 set lines [$zset getMarc $no line $tag * *]
202 foreach line $lines {
203 foreach field [lindex $line 2] {
205 html "<dt><b>$lead</b>"
208 set id [lindex $field 0]
209 if {$id == $startid} {
215 html [lindex $field 1]
218 html [lindex $field 1]
227 proc display-full {zset no tno} {
228 set type [$zset type $no]
230 set err [lindex [$zset diag $no] 1]
231 set add [lindex [$zset diag $no] 2]
235 html "Error ${err}${add} <br>\n"
241 set rtype [$zset recordType $no]
242 if {$rtype == "SUTRS"} {
243 html [join [$zset getSutrs $no]] "<br>\n"
246 if {[catch {set r [$zset getMarc $no line * * *]}]} {
247 html "Unknown record type: $rtype <br>\n"
251 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
253 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
255 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
257 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
259 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
261 dd-marc-field $zset $no 245 b "<em>" "</em>"
262 dd-marc-field $zset $no 245 c " " ""
264 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
266 dl-marc-field $zset $no 520 a "Abstract" {} ", "
267 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
268 dl-marc-field $zset $no 650 * "Subject" {} ", "
269 dl-marc-field $zset $no 260 * "Publisher" {} " "
270 dl-marc-field $zset $no 300 * "Physical Description" {} " "
272 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
274 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
276 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
278 dl-marc-field $zset $no 513 a "Report type" {} ", "
279 dl-marc-field $zset $no 513 b "Period covered" {} ", "
280 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
281 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
282 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
284 dl-marc-field $zset $no 350 a "Price" {} ", "
285 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
286 dl-marc-field $zset $no 850 a "Holdings" {} ", "
288 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
290 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
295 dl-marc-field $zset $no 270 a "Street" {} ", "
296 dl-marc-field $zset $no 270 b "City" {} ", "
297 dl-marc-field $zset $no 270 c "State" {} ", "
298 dl-marc-field $zset $no 270 e "Zip code" {} ", "
299 dl-marc-field $zset $no 270 d "Country" {} ", "
300 dl-marc-field $zset $no 270 m "Network address" {} ", "
301 dl-marc-field $zset $no 301 a "Service hours" {} ", "
302 dl-marc-field $zset $no 270 k "Phone" {} ", "
303 dl-marc-field $zset $no 270 l "Fax" {} ", "
305 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
311 dl-marc-field $zset $no 010 a "LC control number" {} ", "
312 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
313 dl-marc-field $zset $no 020 a "ISBN" {} ", "
314 dl-marc-field $zset $no 022 a "ISSN" {} ", "
315 set url [$zset getMarc $no field 856 * u]
316 set sp [$zset getMarc $no field 856 * 3]
317 if {"x$url" != "x"} {
318 html "<dt><b>URL</b>\n"
322 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
324 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
325 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
326 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
327 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
328 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
329 dl-marc-field $zset $no 001 * "Local control number" {} ", "
334 proc display-rec {from to dfunc tno} {
338 while {$from <= $to} {
339 eval "$dfunc z39${tno}.${setNo} $from $tno"
343 while {$from <= $to} {
344 eval "$dfunc z39.${setNo} $from 0"
350 proc build-query {t} {
355 for {set i 1} {$i < 4} {incr i} {
356 set term [wform entry$i]
358 set field [wform menu$i]
359 foreach x [lindex $targets($t) 2] {
360 if {[lindex $x 0] == $field} {
361 set attr [lindex $x 1]
366 { set q "@and $q ${attr} \{${term}\}" }
368 { set q "@or $q ${attr} \{${term}\}" }
370 { set q "@not $q ${attr} \{${term}\}" }
372 { set q "${attr} \{${term}\}" }
374 set op [wform logic$i]
380 proc z39search {setNo piggy tno} {
386 set host $hist($setNo,$tno,host)
387 set idAuth $hist($setNo,$tno,idAuthentication)
388 set database $hist($setNo,$tno,database)
389 set query $hist($setNo,$tno,query)
392 set host $hist($setNo,host)
393 set idAuth $hist($setNo,idAuthentication)
394 set database $hist($setNo,database)
395 set query $hist($setNo,query)
397 if {[catch [list $zz failback fail-response]]} {
400 if {[catch [list set oldHost [$zz connect]]]} {
403 $zz callback ok-response
404 $zz failback fail-response
405 if {$oldHost != $host} {
406 catch [list $zz disconnect]
408 html "Connecting to target " $host " <br>\n"
410 if {[catch [list $zz connect $host]]} {
411 html "Cannot connect to target ${host} <br>\n"
413 } elseif {$sessionWait == 0} {
415 if {$sessionWait != 1} {
416 html "Cannot connect to target ${host} <br>\n"
420 $zz idAuthentication $idAuth
422 if {[catch [list $zz init]]} {
423 html "Cannot initialize with target ${host} <br>\n"
426 if {[catch {zwait sessionWait 60}]} {
427 html "Cannot initialize with target ${host} <br>\n"
431 if {$sessionWait != "1"} {
432 html "Cannot initialize with target ${host} <br>\n"
436 if {![$zz initResult]} {
437 set u [$zz userInformationField]
439 html "Connection rejected by target: $u <br>\n"
443 if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
446 ir-set $zz.$setNo $zz
447 eval $zz.$setNo databaseNames $database
449 $zz.$setNo preferredRecordSyntax USMARC
451 $zz callback search-response $setNo
453 $zz.$setNo largeSetLowerBound 999999
454 $zz.$setNo smallSetUpperBound 0
455 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
457 $zz.$setNo largeSetLowerBound 2
458 $zz.$setNo smallSetUpperBound 0
459 $zz.$setNo mediumSetPresentNumber 0
462 $zz.$setNo search $query
464 if {[catch {zwait sessionWait 600}]} {
465 html "</body></html>\n"
470 if {$sessionWait != 1} {
471 html "</body></html>\n"
475 set status [$zz.$setNo responseStatus]
476 if {[lindex $status 0] == "NSD"} {
477 set code [lindex $status 1]
478 set msg [lindex $status 2]
479 set addinfo [lindex $status 3]
480 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
483 set hist($setNo,hits) [$zz.$setNo resultCount]
487 proc init-m-response {i} {
491 wlog debug "init-m-response"
497 proc connect-m-response {i} {
501 wlog debug "connect-m-response"
502 z39$i callback [list init-m-response $i]
503 if {[catch {z39$i init}]} {
509 proc fail-m-response {i} {
513 wlog debug "fail-m-response"
518 proc search-m-response {setNo i} {
526 proc z39msearch {setNo piggy} {
531 set not $hist($setNo,0,host)
533 for {set i 1} {$i <= $not} {incr i} {
534 set host $hist($setNo,$i,host)
535 if {[catch {z39 failback fail-response}]} {
538 if {[catch {set oldHost [z39$i connect]}]} {
541 if {$oldHost != $host} {
542 catch {z39$i disconnect}
544 z39$i callback [list connect-m-response $i]
545 z39$i failback [list fail-m-response $i]
548 for {set i 1} {$i <= $not} {incr i} {
549 set oldHost [z39$i connect]
550 set host $hist($setNo,$i,host)
551 if {$oldHost == $host} {
555 html "Connecting to target " $host " <br>\n"
557 if {![catch {z39$i connect $host}]} {
562 wlog debug "Waiting for init response"
563 if {[catch {zwait zleft 10}]} {
568 for {set i 1} {$i <= $not} {incr i} {
569 html "host " $hist($setNo,$i,host) ": "
570 if {$zstatus($i) >= 1} {
572 ir-set z39$i.$setNo z39$i
573 set hist($setNo,$i,offset) 0
574 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
575 z39$i.$setNo preferredRecordSyntax USMARC
576 z39$i callback [list search-m-response $setNo $i]
579 z39$i.$setNo largeSetLowerBound 999999
580 z39$i.$setNo smallSetUpperBound 0
581 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
583 z39$i.$setNo largeSetLowerBound 2
584 z39$i.$setNo smallSetUpperBound 0
585 z39$i.$setNo mediumSetPresentNumber 0
588 wlog debug "search " $hist($setNo,$i,query)
589 z39$i.$setNo search $hist($setNo,$i,query)
596 wlog debug "Waiting for search response"
597 if {[catch {zwait zleft 30}]} {
601 for {set i 1} {$i <= $not} {incr i} {
602 if {$zstatus($i) != 2} continue
603 set status [z39$i.$setNo responseStatus]
604 if {[lindex $status 0] != "NSD"} {
605 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
610 proc z39present {setNo tno setOffset setMax dfunc} {
620 set toGet [expr 1 + $setMax - $setOffset]
621 while {$setMax > 0 && $toGet > 0} {
622 for {set got 0} {$got < $toGet} {incr got} {
623 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
629 $zz.$setNo present $setOffset $toGet
630 if {[catch {zwait sessionWait 300}]} {
634 if {$sessionWait != "1"} {
637 set got [$zz.$setNo numberOfRecordsReturned]
642 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
643 set setOffset [expr $got + $setOffset]
644 set toGet [expr 1 + $setMax - $setOffset]
656 if {![info exists nextSetNo]} {
659 html "<hr><h3>History</h3><dl>\n"
660 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
661 html {<dt> <a href="http:} $env(SCRIPT_NAME)
662 html / $sessionId {/search.egw/} $setNo + 1
663 html + [expr $hist($setNo,maxPresent) - 1]
664 html {"> } [lindex $targets($hist($setNo,host)) 0]
665 if {[llength $hist($setNo,database)] > 1} {
667 foreach b $hist($setNo,database) {
673 if {[info exists hist($setNo,hits)]} {
674 html $hist($setNo,hits) " hits"