f320d0b021ba7fc70c6ae980bc38f43a02839f8b
[ir-tcl-moved-to-github.git] / doc / ir-tcl.sgml
1 <!doctype linuxdoc system>
2
3 <!--
4   $Id: ir-tcl.sgml,v 1.19 1996-06-24 16:24:39 adam Exp $
5 -->
6
7 <article>
8 <title>IrTcl User's Guide and Reference 
9 <author><htmlurl url="http://www.indexdata.dk/" name="Index Data">,
10 <tt><htmlurl url="mailto:info@index.ping.dk" name="info@index.ping.dk"></tt>
11 <date>$Revision: 1.19 $
12 <abstract>
13 This document describes IrTcl -- an information retrieval toolkit for
14 Tcl and Tk that provides access to the Z39.50/SR protocol.
15 </abstract>
16
17 <toc>
18
19 <sect>Introduction
20
21 <p>
22 This document describes the <sf/IrTcl/ information retrieval toolkit,
23 which offers a high-level, client interface to the Z39.50 and SR protocols.
24 The toolkit is based on the Tcl/Tk toolkit developed by Prof. John
25 K. Ousterhout at the University of California &lsqb;ref 1&rsqb;. 
26 Tcl is a simple, somewhat shell-like, interpreted language. What
27 makes Tcl attractive is that it also offers a C API, which makes
28 extensions to the language possible. The most important Tcl extension is
29 probably Tk -- A Motif look-and-feel interface to the X window
30 system.
31
32 To interface the Z39.50/SR protocol <sf/IrTcl/ uses <bf/YAZ/.
33 <bf/YAZ/ offers two transport types: RFC1729/BER on TCP/IP and the mOSI
34 protocol stack.
35 However, the mOSI transport is only an option, and hence it is not
36 needed unless you wish to communicate within an OSI environment.
37 See &lsqb;ref 2&rsqb; for more information about the XTI/mOSI implementation.
38
39 <sf/IrTcl/ provides two system environments:
40
41 <itemize>
42 <item> A simple command line shell -- useful for
43 testing purposes.
44 <item> A simple command line shell which operates within the Tk
45  environment -- makes it very easy to implement GUI clients.
46 </itemize>
47
48 <sect>Compilation and installation
49
50 <p>
51 In order to compile you need:
52 <itemize>
53 <item> An ANSI C compiler such as GNU C.
54 <item> <htmlurl url="http://www.sunlabs.com/research/tcl/" name="Tcl">.
55  Version 7.3, 7.4 and 7.5 has been tested.
56 <item> <htmlurl url="http://130.225.252.168/yaz.html" name="YAZ">
57  version 1.0 patch level 3 or higher.
58 </itemize>
59
60 As an option you may want:
61 <itemize>
62 <item> <htmlurl url="http://www.sunlabs.com/research/tcl/" name="Tk">.
63 Version 3.6, 4.0 and 4.1 has been tested.
64 <item> XTI/mOSI
65 </itemize>
66
67 Unpack the <sf/IrTcl/ package at the same directory level as <bf/YAZ/. 
68
69 Type:
70 <tscreen><verb>
71 $ ./configure
72 </verb></tscreen>
73
74 This command tries to configure <sf/IrTcl/ for your system and creates
75 a <tt>Makefile</tt>.
76
77 If the <tt>configure</tt> command cannot locate Tcl and Tk in your standard
78 locations for libraries searched by your C compiler it will guess 
79 that the libraries are located in <tt>/usr/local/lib</tt> and that 
80 the header files are located in <tt>/usr/local/include</tt>. 
81 If this is incorrect you will have to modify the <tt>Makefile</tt> yourself.
82
83 Compile <sf/IrTcl/ by typing:
84 <tscreen><verb>
85 $ make
86 </verb></tscreen>
87
88 If you don't have Tk you will only be able to create the <tt>ir-tcl</tt>
89 program and you must type <tt>make ir-tcl</tt> instead.
90
91 If successful, this will make <tt>ir-tcl</tt>, <tt>ir-tk</tt> (if
92 Tk is present) and a library called <tt>libirtcl.a</tt>.
93
94 To install the programs and support files type:
95 <tscreen><verb>
96 $ make install
97 </verb></tscreen>
98
99 If you wish to install man pages type:
100 <tscreen><verb>
101 $ make install.man
102 </verb></tscreen>
103
104 Summary of files installed (the names refer to the Makefile variables):
105
106 <descrip>
107 <tag><tt>ir-tk</tt></tag> The <sf/IrTcl/ shell for Tk.
108  Installed in <tt>BINDIR</tt> -- defaults to
109  <tt>/usr/local/bin</tt>. <tt>ir-tk</tt> works like
110  <tt>wish</tt> &mdash without arguments it reads commands from stdin.
111  A source file may be specified by option <tt>-f</tt>. <tt>ir-tk</tt>
112  accept the same set of options as <tt>wish</tt>.
113 <tag><tt>ir-tcl</tt></tag> The <sf/IrTcl/ shell for Tcl. Installed in
114  <tt>BINDIR</tt> -- defaults to <tt>/usr/local/bin</tt>.
115 <tag><tt>client.tcl</tt></tag> A graphical client for <tt>ir-tk</tt>.
116  The client is installed as an executable script called <tt>irclient</tt> in
117  <tt>BINDIR</tt>. This client needs a number of files, bitmaps, etc.
118  The client looks for the files in the current directory &mdash if
119  this fails it tries to look in the directory <tt>IRTCLDIR</tt>
120  -- defaults to <tt>/usr/local/lib/irtcl</tt>.
121 <tag><tt>libirtcl.a</tt></tag> The <sf/IrTcl/ library. 
122  Installed in <tt>LIBDIR</tt> -- defaults to <tt>/usr/local/lib</tt>.
123 <tag><tt>ir-tcl.h</tt></tag> The <sf/IrTcl/ header file.
124  Installed in <tt>INCDIR</tt> -- defaults to <tt>/usr/local/include</tt>.
125 <tag><tt>clientrc.tcl</tt></tag> A setup file with definitions
126  of target and queries. Read and updated by <tt>client.tcl</tt>. Installed
127  in <tt>IRTCLDIR</tt> -- defaults to <tt>/usr/local/lib/irtcl</tt>.
128 <tag><tt>formats/*</tt></tag> Display format files written
129  in Tk. Read by <tt>client.tcl</tt>. Installed 
130  in <tt>IRTCLDIR</tt> -- defaults to <tt>/usr/local/lib/irtcl</tt>.
131 <tag><tt>bitmaps/*</tt></tag> Various bitmap files. Read by
132  <tt>client.tcl</tt>. Installed 
133  in <tt>IRTCLDIR</tt> -- defaults to <tt>/usr/local/lib/irtcl</tt>.
134 <tag><tt>LICENSE</tt></tag> LICENSE file. Read by
135  <tt>client.tcl</tt>. Installed 
136  in <tt>IRTCLDIR</tt> -- defaults to <tt>/usr/local/lib/irtcl</tt>.
137 </descrip>
138
139 <sect1>ir-tcl
140
141 <p>
142 The <tt>ir-tcl</tt> program is a shell like <tt>tclsh</tt> except that
143 <tt>ir-tcl</tt> features the new set of information retrieval commands. 
144 Normally <tt>ir-tcl</tt> waits on <tt/stdin/ (for you to type commands) and
145 on sockets events (connected to Z39.50/SR targets).
146 You simply type the Tcl commands line by line. A filename may be specified as 
147 argument to <tt>ir-tcl</tt> in which case the file specified is evaluated 
148 as a script.
149
150 <sect1>ir-tk
151
152 <p>
153 <tt>ir-tk</tt> is a program that works like <tt>wish</tt> except that
154 <tt>ir-tk</tt> include the new set of commands. All options accepted
155 by <tt>wish</tt> are also accepted by <tt>ir-tk</tt>. 
156
157 The enclosed script <tt>client.tcl</tt> for <tt>ir-tk</tt> is a graphical 
158 client which demonstates an example of a user interface for the Z39.50/SR 
159 protocols. 
160 At first the script was relatively small but it has grown since the 
161 beginning. At present it is about 3000 lines. 
162
163 The client can be started directly from the top level directory 
164 of <sf/IrTcl/ by typing:
165 <tscreen><verb>
166 $ ir-tk -f client.tcl
167 </verb></tscreen>
168
169 Or, if you have installed <sf/IrTcl/ you may also type:
170 <tscreen><verb>
171 $ irclient
172 </verb></tscreen>
173
174 The client lets up define targets and query types within the interface.
175 Hence, you will not need to modify configation files. 
176
177 Stuff concerning targets can be found in the pull down menu 'Target'
178 with the following options:
179 <descrip>
180 <tag>Connect</tag> Establishes connection to a target.
181 <tag>Disconnect</tag> Closes a target connection.
182 <tag>About</tag> Shows implementation Id, implementation Version, etc 
183  for the current target.
184 <tag>Setup</tag> Pops up a target definition window. You may alter
185  a target definition.
186 <tag>Setup new</tag> Lets you define a new target.
187 </descrip> 
188
189 The term query type refers to a collection of search fields. The
190 pull down menu Options|Query deals with queries. You may
191 insert/modify/remove query types.
192
193 <sect>Overview of the API
194
195 <p>
196 Basically, <sf/IrTcl/ is a set of commands introduced to Tcl.
197 When extending Tcl there are two approaches: action-oriented commands
198 and object-oriented commands.
199
200 Action-oriented commands manipulate
201 Tcl variables and each command introduces only one action.
202 The string manipulation commands in Tcl are action oriented.
203
204 Object-oriented commands are added for every declared
205 variable (object). Object-oriented commands usually provide a set of
206 actions (methods) to manipulate the object.
207 The widgets in Tk (X objects) are examples of the object-oriented style.
208
209 <sf/IrTcl/ commands are object-oriented. The main reason
210 for this is that the data structures involved in the IR protocol
211 are not easily represented by Tcl data structures.
212 Also, the <sf/IrTcl/ objects tend to exist for a relativly long time.
213 Note that although we use the term object-oriented commands, this
214 does not mean that the programming style is strictly object-oriented. For
215 example, there is such no such thing as inheritance.
216
217 We are now ready to present the three commands introduced to Tcl by
218 <sf/IrTcl/:
219
220 <descrip>
221 <tag/ir/ The ir object represents a connection to a target. More
222 precisely it describes a Z-association. 
223 <tag/ir-set/ The ir-set describes a result set, which is
224 conceptually a collection of records returned by the target.
225 The ir-set object may retrieve records from a target by means of
226 the ir object; it may read/write records from/to a local file or it may be
227 updated with a user-edited record. 
228 <tag/ir-scan/ The scan object represents a list of scan lines
229 retrieved from a target.
230 </descrip>
231
232 <bf/Example/
233
234 To create a new IR object called <tt/z-assoc/ write:
235 <tscreen><verb>
236    ir z-assoc
237 </verb></tscreen>
238
239 <bf/End of example/
240
241 Each object provides a set of <em/settings/ which may either be
242 readable, writeable of both. All settings immediately follow
243 the name of the object. If a value is present the setting
244 is set to <em/value/.
245
246 <bf/Example/
247
248 We wish to set the preferred-message-size to 18000 on the
249 <tt/z-assoc/ object:
250
251 <tscreen><verb>
252    z-assoc preferredMessageSize 18000
253 </verb></tscreen>
254
255 To read the current value of preferred-message-size use:
256
257 <tscreen><verb>
258    z-assoc preferredMessageSize
259 </verb></tscreen>
260 <bf/End of example/
261
262 One important category consists of settings is those that relate to the
263 event-driven model. When <sf/IrTcl/ receives responses from the target, i.e.
264 init responses, search responses, etc., a <em/callback/ routine
265 is called. Callback routines are represented in Tcl as
266 a list, which is re-interpreted prior to invocation.
267 The method is similar to the one used in Tk to capture X events.
268
269 For each SR/Z39.50 request there is a corresponding object action. The most
270 important actions are:
271 <descrip>
272 <tag/connect/ Establishes connection with a target
273 <tag/init/ Sends an initialize request.
274 <tag/search/ Sends a search request.
275 <tag/present/ Sends a present request.
276 <tag/scan/ Sends a scan request.
277 </descrip>
278
279 <bf/Example/
280
281 This example shows a complete connect - init - search - present scenario. 
282
283 First an IR object, called <tt/z/, is created.
284 Also a result set <tt/z.1/ is introduced by the <tt/ir-set/ 
285 and it is specified that the result set uses <tt/z/ as its association.
286
287 The setting <tt/databaseNames/ is set to the 
288 database <tt/books/ to which the following searches are directed.
289 A callback is then defined and a connection is established to
290 <tt/fake.com/ by the <tt/connect/ action.
291 If the connect succeeds the <tt/connect-response/ is called.
292
293 In the Tcl procedure, <tt/connect-response/, a callback is defined
294 <em/before/ the init request is executed. 
295 The Tcl procedure <tt/init-response/ is called when a 
296 init response is returned from the target.
297
298 The <tt/init-response/ procedure sets up a <tt/search-response/ 
299 callback handler and sends a search-request by using a query which
300 consists of a single word <tt/science/.
301
302 When the <tt/search-response/ procedure is called it defines
303 a variable <tt/hits/ and sets it to the value of the setting
304 <tt/resultCount/. If <tt/hits/ is positive a present-request is
305 sent -- asking for 5 records from position 1. 
306
307 Finally, a present response is received and the number of records
308 returned is stored in the variable <tt/ret/.
309
310 <tscreen><verb>
311 ir z
312 z databaseNames books
313 ir-set z.1 z
314 z callback {connect-response}
315 z connect fake.com
316
317 proc connect-response {} {
318     z callback {init-response}
319     z init
320 }
321
322 proc init-response {} {
323     z callback {search-response}
324     z.1 search science
325 }
326
327 proc search-response {} {
328     set hits [z.1 resultCount]
329     puts "$hits hits"
330     if {$hits > 0} {
331         z callback {present-response}
332         z.1 present 1 5
333     }
334 }
335
336 proc present-response {} {
337     set ret [z.1 numberOfRecordsReturned]
338     puts "$ret records returned"
339 }
340 </verb></tscreen>
341 <bf/End of example/
342
343 The previous example program doesn't care about error conditions. 
344 If errors occur in the program they will be trapped by the Tcl error 
345 handler. This is not always appropriate. However, Tcl offers a 
346 <tt/catch/ command to support error handling by the program itself.
347
348 <sect>Associations
349
350 <p>
351 The ir object describes an association with a target.
352 This section covers the connect-init-disconnect actions provided
353 by the ir object.
354 An ir object is created by the <tt/ir/ command and the
355 created object enters a 'not connected' state, because it isn't
356 connected to a target yet. 
357
358 <sect1>Connect
359
360 <p>
361 A connection is established by the <tt/connect/ action which is
362 immediately followed by a hostname. A number of settings affect the
363 <tt/connect/ action. Obviously, these settings should be set
364 <bf/before/ connecting. The settings are:
365
366 <descrip>
367 <tag><tt>comstack </tt><tt>mosi|tcpip</tt></tag>
368  Comstack type.
369 <tag><tt>protocol </tt><tt>Z39|SR</tt></tag>
370  Protocol type - ANSI/NISO Z39.50 or ISO SR.
371 <tag><tt>callback </tt><em>list</em></tag>
372  Tcl script called when the connection is established.
373 <tag><tt>failback </tt><em>list</em></tag>
374  Fatal error Tcl script. Called on protocol errors or if target
375  closes connection.
376 </descrip>
377
378 If the connect is unsuccessful either the connect action itself
379 will return an error code or the failback handler is invoked.
380
381 In general, the <tt>failback</tt> handler is invoked when serious
382 unrecoverable errors occur when communicating with the target. 
383 In this case the <sf/IrTcl/ system shuts down the connection.
384 The <tt>failback</tt> handler might inspect the <tt>failInfo</tt>
385 setting to determine the cause of the failure; it returns
386 two elements. The first is an error integer; the second is an 
387 english representation of the error. The error codes and
388 the corresponding messages are:
389
390 <descrip>
391 <tag><tt>0</tt></tag>ok
392 <tag><tt>1</tt></tag>connect failed
393 <tag><tt>2</tt></tag>connection closed
394 <tag><tt>3</tt></tag>connection closed
395 <tag><tt>4</tt></tag>failed to decode incoming APDU
396 <tag><tt>5</tt></tag>unknown APDU
397 </descrip>
398
399 Note: in case 3 the connection was closed during read a read operation
400 whereas in case 4 it was closed during a write operation.
401
402 <sect1>Init
403
404 <p>
405 If the connect operation succeeds the <tt/init/ action should be used. 
406 The init related settings are:
407
408 <descrip>
409 <tag><tt>preferredMessageSize </tt><em>integer</em></tag>
410  Preferred-message-size. Default value is 30000.
411 <tag><tt>maximumRecordSize </tt><em>integer</em></tag>
412  Maximum-record-size. Default value is 30000.
413 <tag><tt>idAuthentication </tt><em>string</em> ...</tag>
414  Id-authentication. There are three forms. If any empty is
415  given, the Id-authentication is not used.  If one non-empty string
416  is given, the 'open' authentication is used. If three strings are 
417  specified, the version 'id-pass' authentication (version 3 only) 
418  is used in which case the first string is groupId; the second string 
419  is userId and the third string is password.
420 <tag><tt>implementationName </tt><em>string</em></tag>
421  Implementation-name of origin system. 
422 <tag><tt>implementationId</tt></tag>
423  Implementation-id of origin system. This setting is read-only.
424 <tag><tt>implementationVersion</tt></tag>
425  Implementation-version of origin system. This settings is read-only.
426 <tag><tt>options </tt><em>list</em></tag>
427  Options to be negotiated in the init service. The list contains 
428  the options that are set. Possible values are <tt>search</tt>, 
429  <tt>present</tt>, <tt>delSet</tt>, <tt>resourceReport</tt>, 
430  <tt>triggerResourceCtrl</tt>, <tt>resourceCtrl</tt>,
431  <tt>accessCtrl</tt>, <tt>scan</tt>, <tt>sort</tt>, 
432  <tt>extendedServices</tt>, <tt>level-1Segmentation</tt>, 
433  <tt>level-2Segmentation</tt>, <tt>concurrentOperations</tt> and
434  <tt>namedResultSets</tt>. Currently the default options are:
435  <tt>search</tt>, <tt>present</tt>, <tt>scan</tt> and 
436  <tt>namedResultSets</tt>. The <tt>options</tt> setting is set to its default
437  value when an ir object is created and when a <tt>disconnect</tt>
438  action is performed.
439 <tag><tt>protocolVersion </tt><em>integer</em></tag>
440  Protocol version: 2, 3, etc. Default is 2.
441 <tag><tt>referenceId </tt><em>string</em></tag>
442  Reference-id of init operation. If <em>string</em> is empty no 
443  reference-id is used.
444 <tag><tt>initResponse </tt><em>list</em></tag>
445  Init-response Tcl script.
446 <tag><tt>callback </tt><em>list</em></tag>
447  General response Tcl script. Only used if <tt>initResponse</tt>
448  is not specified.
449 </descrip>
450
451 The init-response handler should inspect some of the settings shown
452 below:
453
454 <descrip>
455 <tag><tt>initResult </tt>returns <em>boolean</em></tag>
456  Init response status. True if init operation was successful; 
457  false otherwise.
458 <tag><tt>preferredMessageSize </tt>returns <em>integer</em></tag>
459  Preferred-message-size after negotiation.
460 <tag><tt>maximumRecordSize </tt>returns <em>integer</em></tag>
461  Maximum-record-size after negotiation.
462 <tag><tt>targetImplementationName </tt>returns <em>string</em></tag>
463  Implementation-name of target system.
464 <tag><tt>targetImplementationId </tt>returns <em>string</em></tag>
465  Implementation-id of target system.
466 <tag><tt>targetImplementationVersion </tt>returns <em>string</em></tag>
467  Implementation-version of target system.
468 <tag><tt>options </tt>returns <em>list</em></tag>
469  Options after negotiation. The list contains the options that are set.
470 <tag><tt>protocolVersion </tt>returns <em>integer</em></tag>
471  Protocol version: 2, 3, etc after negotiation.
472 <tag><tt>userInformationField </tt>returns <em>string</em></tag>
473  User information field.
474 <tag><tt>referenceId </tt>returns <em>string</em></tag>
475  Reference-id of init response.
476 </descrip>
477
478 <bf/Example/
479
480 Consider a client with the ability to access multiple targets.
481
482 We define a list of targets that we wish to connect to.
483 Each item in the list describes the target parameters with
484 the following four components: association-name, comstack-type,
485 protocol-type and a hostname.
486
487 The list for the two targets: ISO/SR target DANBIB and TCP/Z39.50 
488 target Data Research, will be defined as:
489 <tscreen><verb>
490 set targetList { {danbib mosi SR 0103/find2.denet.dk:4500}
491                  {drs tcpip Z39 dranet.dra.com} }
492 </verb></tscreen>
493
494 The Tcl code below defines, connect and initialize the
495 targets in <tt/targetList/:
496
497 <tscreen><verb>
498 foreach target $targetList {
499     set assoc [lindex $target 0]
500     ir $assoc
501     $assoc comstack [lindex $target 1]
502     $assoc protocol [lindex $target 2]
503     $assoc failback [list fail-response $assoc]
504     $assoc callback [list connect-response $assoc]
505     $assoc connect [lindex $target 3]
506 }
507
508 proc connect-response {assoc} {
509     $assoc callback [list init-response $assoc]
510     $assoc init
511 }
512
513 proc fail-response {assoc} {
514     puts "$assoc closed connection or protocol error"
515 }
516
517 proc init-response {assoc} {
518     if {[$assoc initResult]} {
519         puts "$assoc initialized ok"
520     } else {
521         puts "$assoc didn't initialize"
522     }
523 }
524 </verb></tscreen>
525
526 <tt/target/ is bound to each item in the list of targets.
527 The <tt/assoc/ is set to the ir object name.
528 Then, the comstack, protocol and failback are set for the <tt/assoc/ object.
529 The ir object name is argument to the <tt/fail-response/ and 
530 <tt/connect-response/ routines. 
531 Note the use of the Tcl <tt/list/ command which 
532 is necessary here because the argument contains variables 
533 (<tt/assoc/) that should be substituted before the handler is defined.
534 After the connect operation, the <tt/init-response/ handler
535 is defined in much the same way as the failback handler.
536 And, finally, an init request is executed.
537
538 <bf/End of example/
539
540 <sect1>Disconnect
541
542 <p>
543 To terminate the connection the <tt/disconnect/ action should be used. 
544 This action has no parameters. 
545 Another connection may be established by a new <tt/connect/ action on
546 the same ir object.
547
548 <sect>Result sets
549
550 <p>
551 This section covers the queries used by <sf/IrTcl/, and how searches and
552 presents are handled.
553
554 A search operation and a result set is described by the ir set object.
555 The ir set object is defined by the <tt/ir-set/ command which
556 has two parameters. The first is the name of the new ir set object, and
557 the second, which is optional, is the name of an assocation -- an ir
558 object. The second argument is required if the ir set object should be able
559 to perform searches and presents. However, it is not required if
560 only ``local'' operations is done with the ir set object.
561
562 When the ir set object is created a number of settings are inherited
563 from the ir object, such as the selected databass, query type,
564 etc. Thus, the ir object contains what we could call default
565 settings.
566
567 <sect1>Queries
568
569 <p>
570 Search requests are sent by the <tt/search/ action which
571 takes a query as parameter. There are two types of queries,
572 RPN and CCL, controlled by the setting <tt/queryType/.
573 A string representation for the query is used in <sf/IrTcl/ since
574 Tcl has reasonably powerful string manipulaton capabilities.
575 The RPN query used in <sf/IrTcl/ is the prefix query notation also used in
576 the <bf/YAZ/ test client.
577
578 The CCL query is an uninterpreted octet-string which is parsed by the target.
579 We refer to the standard: ISO 8777. Note that only a few targets
580 actually support the CCL query and the interpretation of
581 the standard may vary.
582
583 The prefix query notation (which is converted to RPN) offer a few
584 operators. They are:
585
586 <descrip>
587 <tag><tt>@attr </tt><em>list op</em></tag>
588  The attributes in list are applied to op
589 <tag><tt>@and </tt><em>op1 op2</em></tag>
590  Boolean <em/and/ on op1 and op2
591 <tag><tt>@or </tt><em>op1 op2</em></tag>
592  Boolean <em/or/ on op1 and op2
593 <tag><tt>@not </tt><em>op1 op2</em></tag>
594  Boolean <em/not/ on op1 and op2
595 <tag><tt>@prox </tt><em>list op1 op2</em></tag>
596  Proximity operation on op1 and op2. Not implemented yet.
597 <tag><tt>@set </tt><em>name</em></tag>
598  Result set reference
599 </descrip>
600
601 It is simple to build RPN queries in <sf/IrTcl/. Search terms
602 are sequences of characters, as in:
603 <tscreen><verb>
604    science
605 </verb></tscreen>
606
607 Boolean operators use the prefix notation (instead of the suffix/RPN),
608 as in:
609 <tscreen><verb>
610    @and science technology
611 </verb></tscreen>
612
613 Search terms may be associated with attributes. These
614 attributes are indicated by the <tt/@attr/ operator.
615 Assuming the bib-1 attribute set, we can set the use-attribute 
616 (type is 1) to title (value is 4):
617
618 <tscreen><verb>
619    @attr 1=4 science
620 </verb></tscreen>
621
622 Also, it is possible to apply attributes to a range of search terms.
623 In the query below, both search terms have use=title but the <tt/tech/
624 term is right truncated:
625
626 <tscreen><verb>
627    @attr 1=4 @and @attr 5=1 tech beta
628 </verb></tscreen>
629
630 <sect1>Search
631
632 <p>
633 The settings that affect the search are listed below:
634
635 <descrip>
636 <tag><tt>databaseNames </tt><em>list</em></tag>
637  Database-names.
638 <tag><tt>smallSetUpperBound </tt><em>integer</em></tag>
639  Small set upper bound. Default 0.
640 <tag><tt>largeSetLowerBound </tt><em>integer</em></tag>
641  Large set lower bound. Default 2.
642 <tag><tt>mediumSetPresentNumber </tt><em>integer</em></tag>
643  Medium set present number. Default 0.
644 <tag><tt>replaceIndicator </tt><em>boolean</em></tag>
645  Replace-indicator. Default true (1).
646 <tag><tt>setName </tt><em>string</em></tag>
647  Name of result set.
648 <tag><tt>queryType rpn|ccl</tt></tag>
649  Query type-1 or query type-2. Default rpn (type-1).
650 <tag><tt>preferredRecordSyntax </tt><em>string</em></tag>
651  Preferred record syntax -- UNIMARC, USMARC, etc. 
652 <tag><tt>smallSetElementSetNames </tt><em>string</em></tag>
653  small-set-element-set names. If <em>string</em> is empty
654  the element set is not set. Default is empty (not set).
655 <tag><tt>mediumSetElementSetNames </tt><em>string</em></tag>
656  medium-set-element-set names. If <em>string</em> is empty
657  the element set is not set. Default is empty (not set).
658 <tag><tt>nextResultSetPosition </tt>returns <em>integer</em></tag>
659  Next result set position.
660 <tag><tt>referenceId </tt><em>string</em></tag>
661  Reference-id. If <em>string</em> is empty no reference-id is used.
662 <tag><tt>searchResponse </tt><em>list</em></tag>
663  Search-response Tcl script.
664 <tag><tt>callback </tt><em>list</em></tag>
665  General response Tcl script. Only used if searchResponse is not specified.
666  This setting is valid only for the <tt/ir/ object -- not the
667  <tt/ir-set/ object.
668 </descrip>
669
670 Setting the <tt/databaseNames/ is mandatory. All other settings
671 have reasonable defaults.
672 The search-response handler, specified by the <tt/callback/ - or
673 the <tt/searchResponse/ setting, 
674 should read some of the settings shown below:
675
676 <descrip>
677 <tag><tt>searchStatus</tt> returns <em>boolean</em></tag>
678  Search-status. True if search operation was successful; false
679  otherwise.
680 <tag><tt>responseStatus </tt>returns <em>list</em></tag>
681  Response status information.
682 <tag><tt>resultCount </tt>returns <em>integer</em></tag>
683  result-count
684 <tag><tt>numberOfRecordsReturned </tt>returns <em>integer</em></tag>
685  Number of records returned.
686 <tag><tt>referenceId </tt>returns <em>string</em></tag>
687  Reference-id of search response.
688 </descrip>
689
690 The <tt/responseStatus/ signals one of three conditions which
691 is indicated by the value of the first item in the list:
692
693 <descrip>
694 <tag><tt>NSD</tt></tag> indicates that the target has returned one or 
695 more non-surrogate diagnostic messages. The <tt/NSD/ item is followed by 
696 a list with all non-surrogate messages. Each non-surrogate message consists
697 of three items. The first item of the three items is the error
698 code (integer); the next item is a textual representation of the error
699 code in plain english; the third item is additional information, possibly
700 empty if no additional information was returned by the target.
701
702 <tag><tt>DBOSD</tt></tag> indicates a successful operation where the
703 target has returned one or more records. Each record may be
704 either a database record or a surrogate diagnostic.
705
706 <tag><tt>OK</tt></tag> indicates a successful operation -- no records are
707 returned from the target. 
708 </descrip>
709
710 <bf/Example/
711
712 We continue with the multiple-targets example. 
713 The <tt/init-response/ procedure will attempt to make searches:
714
715 <tscreen><verb>
716 proc init-response {assoc} {
717     puts "$assoc connected"
718     ir-set ${assoc}.1 $assoc
719     $assoc.1 queryType rpn
720     $assoc.1 databaseNames base-a base-b
721     $assoc callback [list search-response $assoc ${assoc}.1]
722     $assoc.1 search "@attr 1=4 @and @attr 5=1 tech beta"
723 }
724 </verb></tscreen>
725
726 An ir set object is defined and the
727 ir object is told about the name of ir object.
728 The ir set object use the name of the ir object as prefix.
729
730 Then, the query-type is defined to be RPN, i.e. we will
731 use the prefix query notation later on.
732
733 Two databases, <tt/base-a/ and <tt/base-b/, are selected.
734
735 A <tt/search-response/ handler is defined with the
736 ir object and the ir-set object as parameters and
737 the search is executed.
738
739 The first part of the <tt/search-response/ looks like:
740 <tscreen><verb>
741 proc search-response {assoc rset} {
742     set status [$rset responseStatus]
743     set type [lindex $status 0]
744     if {$type == "NSD"} {
745         set code [lindex $status 1]
746         set msg [lindex $status 2]
747         set addinfo [lindex $status 3]
748         puts "NSD $code: $msg: $addinfo"
749         return
750     } 
751     set hits [$rset resultCount]
752     if {$type == "DBOSD"} {
753         set ret [$rset numberOfRecordsReturned]
754         ...
755     }
756 }
757 </verb></tscreen>
758 The response status is stored in variable <tt/status/ and 
759 the first element indicates the condition.
760 If non-surrogate diagnostics are returned they are displayed.
761 Otherwise, the search was a success and the number of hits
762 is read. Finally, it is tested whether the search response
763 returned records (database or diagnostic).
764
765 Note that we actually didn't inspect the search status (setting
766 <tt/searchStatus/) to determine whether the search was successful or not, 
767 because the standard specifies that one or more non-surrogate
768 diagnostics should be returned by the target in case of errors.
769
770 <bf/End of example/
771
772 If one or more records are returned from the target they
773 will be stored in the result set object.
774 In the case in which the search response contains records, it is
775 very similar to the present response case. Therefore, some settings
776 are common to both situations.
777
778 <sect1>Present
779
780 <p>
781 The <tt/present/ action sends a present request. The <tt/present/ is
782 followed by two optional integers. The first integer is the 
783 result-set starting position -- defaults to 1. The second integer 
784 is the number of records requested -- defaults to 10. 
785 The settings which could be modified before a <tt/present/
786 action are:
787
788 <descrip>
789 <tag><tt>preferredRecordSyntax </tt><em>string</em></tag>
790  preferred record syntax -- UNIMARC, USMARC, etc.
791 <tag><tt>elementSetNames </tt><em>string</em></tag>
792  Element-set names. If <em>string</em> is empty
793  the element set is not set. Default is empty (not set).
794 <tag><tt>referenceId </tt><em>string</em></tag>
795  Reference-id. If <em>string</em> is empty no reference-id is used.
796 <tag><tt>presentResponse </tt><em>list</em></tag>
797  Present-response Tcl script. 
798 <tag><tt>callback </tt><em>list</em></tag>
799  General response Tcl script. Only used if presentResponse is not specified 
800  This setting is valid only for the <tt/ir/ object -- not the
801  <tt/ir-set/ object.
802 </descrip>
803
804 The present-response handler should inspect the settings
805 shown in table below.
806 Note that <tt/responseStatus/ and <tt/numberOfRecordsReturned/
807 settings were also used in the search-response case.
808
809 As in the search response case, records returned from the
810 target are stored in the result set object.
811
812 <descrip>
813 <tag><tt>presentStatus </tt>returns <em>boolean</em></tag>
814  Present-status.
815 <tag><tt>responseStatus </tt>returns <em>list</em></tag>
816  Response status information.
817 <tag><tt>numberOfRecordsReturned </tt>returns <em>integer</em></tag>
818  Number of records returned.
819 <tag><tt>nextResultSetPosition </tt>returns <em>integer</em></tag>
820  Next result set position.
821 <tag><tt>referenceId </tt>returns <em>string</em></tag>
822  Reference-id of present response.
823 </descrip>
824
825 <sect1>Records
826
827 <p>
828 Search responses and present responses may result in
829 one or more records stored in the ir set object if
830 the <tt/responseStatus/ setting indicates database or
831 surrogate diagnostics (<tt/DBOSD/). The individual
832 records, indexed by an integer position offset, should then be 
833 inspected.
834
835 If element set names have been specified either in the
836 search requests (<tt>smallSetElementSetNames</tt> /
837 <tt>mediumSetElementSetNames</tt>) or present requests 
838 (<tt>elementSetNames</tt>) the individual records in the
839 ir set object are assigned appropriate element set ids. 
840 In this mode records at a given position are treated different as
841 long as they have difference element set ids.
842 To inspect records with a particular element set id in subsequent
843 operations use the <tt>recordElements</tt> setting followed by the id.
844 If you have more than one record at a given position and you do not
845 use <tt>recordElements</tt> the record selected at the given position
846 is undefined.
847
848 The action <tt>type</tt> followed by an integer returns information 
849 about a given position in an ir set. There are three possiblities:
850
851 <descrip>
852 <tag><tt/SD/</tag> The item is a surrogate diagnostic record.
853 <tag><em/empty/</tag> There is no record at the specified position.
854 <tag><tt/DB/</tag> The item is a database record.
855 </descrip>
856
857 To handle the first case, surrogate diagnostic record, the
858 <tt/Diag/ action should be used. It returns three
859 items: error code (integer), text representation in plain english
860 (string), and additional information (string, possibly empty).
861
862 In the second case, no record, note that there still might
863 be a record at the position but with an id that differs from that
864 specified by <tt>recordElements</tt>.
865
866 In the third case, database record, the <tt/recordType/ action should
867 be used. It returns the record type at the given position.
868 Some record types are:
869
870 <tscreen>
871 UNIMARC
872 INTERMARC
873 CCF
874 USMARC
875 UKMARC
876 NORMARC
877 LIBRISMARC
878 DANMARC
879 FINMARC
880 SUTRS
881 </tscreen>
882
883 <bf/Example/
884
885 We continue our search-response example. In the case,
886 <tt/DBOSD/, we should inspect the result set items. 
887 Recall that the ir set name was passed to the
888 search-response handler as argument <tt/rset/.
889
890 <tscreen><verb>
891     if {$type == "DBOSD"} {
892         set ret [$rset numberOfRecordsReturned]
893         for {set i 1} {$i<=$ret} {incr i} {
894             set itype [$rset type $i]
895             if {$itype == "SD"} {
896                 set diag [$rset Diag $i]
897                 set code [lindex $diag 0]
898                 set msg [lindex $diag 1]
899                 set addinfo [lindex $diag 2]
900                 puts "$i: NSD $code: $msg: $addinfo"
901             } elseif {$itype == "DB"} {
902                 set rtype [$rset recordType $i]
903                 puts "$i: type is $rtype"
904             }
905         }
906     }
907 </verb></tscreen>
908 Each item in the result set is examined. 
909 If an item is a diagnostic message it is displayed; otherwise
910 if it's a database record its type is displayed.
911
912 <bf/End of example/
913
914 <sect1>MARC records
915
916 <p>
917 In the case, where there is a MARC record at a given position we
918 want to display it somehow. The action <tt/getMarc/ is what we need.
919 The <tt/getMarc/ is followed by a position integer and the type of
920 extraction we want to make: <tt/field/ or <tt/line/.
921
922 The <tt/field/ and <tt/line/ type are followed by three
923 parameters that serve as extraction masks.
924 They are called tag, indicator and field.
925 If the mask matches a tag/indicator/field of a record the information
926 is extracted. Two characters have special meaning in masks: the
927 dot (any character) and star (any number of any character).
928
929 The <tt/field/ type returns one or more lists of field information
930 that matches the mask specification. Only the content of fields
931 is returned.
932
933 The <tt/line/ type, on the other hand, returns a Tcl list that
934 completely describe the layout of the MARC record -- including
935 tags, fields, etc.
936
937 The <tt/field/ type is sufficient and efficient in the case, where only a
938 small number of fields are extracted, and in the case where no
939 further processing (in Tcl) is necessary.
940
941 However, if the MARC record is to be edited or altered in any way, the
942 <tt/line/ extraction is more powerful -- only limited by the Tcl
943 language itself.
944
945 <bf/Example/
946
947 Consider the record below:
948 <tscreen><verb>
949 001       11224466 
950 003    DLC
951 005    00000000000000.0
952 008    910710c19910701nju           00010 eng  
953 010    $a    11224466 
954 040    $a DLC $c DLC
955 050 00 $a 123-xyz
956 100 10 $a Jack Collins
957 245 10 $a How to program a computer
958 260 1  $a Penguin
959 263    $a 8710
960 300    $a p. cm.
961 </verb></tscreen>
962
963 Assuming this record is at position 1 in ir-set <tt/z.1/, we
964 might extract the title-field (245 * a), with the following command:
965 <tscreen><verb>
966 z.1 getMarc 1 field 245 * a
967 </verb></tscreen>
968
969 which gives:
970 <tscreen><verb>
971 {How to program a computer}
972 </verb></tscreen>
973
974 Using the <tt/line/ instead of <tt/field/ gives:
975 <tscreen><verb>
976 {245 {10} {{a {How to program a computer}} }}
977 </verb></tscreen>
978
979 If we wish to extract the whole record as a list, we use:
980 <tscreen><verb>
981 z.1 getMarc 1 line * * *
982 </verb></tscreen>
983
984 giving:
985 <tscreen><verb>
986 {001 {} {{{} {   11224466 }} }}
987 {003 {} {{{} DLC} }}
988 {005 {} {{{} 00000000000000.0} }}
989 {008 {} {{{} {910710c19910701nju           00010 eng  }} }}
990 {010 {  } {{a {   11224466 }} }}
991 {040 {  } {{a DLC} {c DLC} }}
992 {050 {00} {{a 123-xyz} }}
993 {100 {10} {{a {Jack Collins}} }}
994 {245 {10} {{a {How to program a computer}} }}
995 {260 {1 } {{a Penguin} }}
996 {263 {  } {{a 8710} }}
997 {300 {  } {{a {p. cm.}} }}
998 </verb></tscreen>
999
1000 <bf/End of example/
1001
1002 <bf/Example/
1003
1004 This example demonstrates how Tcl can be used to examine
1005 a MARC record in the list notation.
1006
1007 The procedure <tt/extract-format/ makes an extraction of
1008 fields in a MARC record based on a number of masks.
1009 There are 5 parameters, <tt/r/: a
1010 record in list notation, <tt/tag/: regular expression to
1011 match the record tags, <tt/ind/: regular expression to
1012 match indicators, <tt/field/: regular expression to 
1013 match fields, and finally <tt/text/: regular expression to 
1014 match the content of a field.
1015
1016 <tscreen><verb>
1017 proc extract-format {r tag ind field text} {
1018     foreach line $r {
1019         if {[regexp $tag [lindex $line 0]] && \
1020                 [regexp $ind [lindex $line 1]]} {
1021             foreach f [lindex $line 2] {
1022                 if {[regexp $field [lindex $f 0]]} {
1023                     if {[regexp $text [lindex $f 1]]} {
1024                         puts [lindex $f 1]
1025                     }
1026                 }
1027             }
1028         }
1029     }
1030 }
1031 </verb></tscreen>
1032
1033 To match <tt/comput/ followed by any number of character(s) in the
1034 245 fields in the record from the previous example, we could use:
1035 <tscreen><verb>
1036 set r [z.1 getMarc 1 line * * *]
1037
1038 extract-format $r 245 .. . comput
1039 </verb></tscreen>
1040 which gives:
1041 <tscreen><verb>
1042 How to program a computer
1043 </verb></tscreen>
1044
1045 <bf/End of example/
1046
1047 The <tt/putMarc/ action does the opposite of <tt/getMarc/. It
1048 copies a record in Tcl list notation to a ir set object and is
1049 needed if a result-set must be updated by a Tcl modified (user-edited)
1050 record.
1051
1052 <sect1>SUTRS
1053
1054 <p>
1055 In <sf/IrTcl/ a SUTRS record is treated as one single string. To retrieve
1056 a SUTRS record use the <tt>getSutrs</tt> followed by an index.
1057
1058 <sect>Scan
1059
1060 <p>
1061 To perform scan, a scan object must be created by the <tt>ir-scan</tt>
1062 command. This command has two arguments -- name of the scan object and
1063 name of the ir object. Basically, the scan object, provides one <tt>scan</tt>
1064 action which sends a scan request to the target. The <tt>action</tt>
1065 is followed by a string describing starting point of the term list. The
1066 format used is a simple subset of the query used in search requests. Only
1067 <tt>@attr</tt> specifications and simple terms are allowed.
1068 The settings that affect the scan are:
1069
1070 <descrip>
1071 <tag><tt>stepSize </tt><em>integer</em></tag>
1072  Step size. Default is 0.
1073 <tag><tt>numberOfTermsRequested </tt><em>integer</em></tag>
1074  Number of terms requested. Default is 20.
1075 <tag><tt>preferredPositionInResponse </tt><em>integer</em></tag>
1076  Preferred position in response. Default is 1.
1077 <tag><tt>databaseNames </tt><em>list</em></tag>
1078  Database names. Note that this setting is not (yet) supported for
1079  the scan object. You must set this for the ir object instead.
1080 <tag><tt>referenceId </tt><em>string</em></tag>
1081  Reference-id. If <em>string</em> is empty no reference-id is used.
1082 <tag><tt>scanResponse </tt><em>list</em></tag>
1083  Scan-response Tcl script. 
1084 <tag><tt>callback </tt><em>list</em></tag>
1085  General response Tcl script. Only used if <tt>scanResponse</tt>
1086  is not specified.
1087  This setting is valid only for the <tt/ir/ object -- not the
1088  <tt/ir-set/ object.
1089 </descrip>
1090
1091 The scan object normally holds one or more scan line entries upon
1092 successful completion. The table below summarizes the settings
1093 that should be used in a response handler.
1094
1095 <descrip>
1096 <tag><tt>scanStatus </tt>returns <em>integer</em></tag>
1097  Scan status. An integer between 0 and 6.
1098 <tag><tt>numberOfTermsReturned </tt>returns <em>integer</em></tag>
1099  Number of terms returned. 
1100 <tag><tt>positionOfTerm </tt>returns <em>integer</em></tag>
1101  An integer describing the position of term.
1102 <tag><tt>scanLine </tt>returns <em>list</em></tag>
1103  This function returns information about a given scan line (entry) at a given
1104  index specified by the integer. The first scan line is numbered zero;
1105  the second 1 and so on. A list is returned by the <tt>scanLine</tt>
1106  setting. The first element is <tt>T</tt> if the scan line
1107  is a normal term and <tt>SD</tt> if the scan line is a surrogate 
1108  diagnostic. In the first case (normal) the scan term is second element 
1109  in the list and the number of occurences is the third element.
1110  In the other case (surrogate diagnostic), the second element
1111  is the diagnostic code, the third a text representation of the error
1112  code and the fourth element is additional information.
1113 <tag><tt>referenceId </tt>returns <em>string</em></tag>
1114  Reference-id of scan response.
1115 </descrip>
1116
1117 <bf/Example/
1118
1119 We will scan for the terms after <tt>science</tt> in the Title index.
1120 We will assume that an ir object called <tt>z-assoc</tt> has already
1121 been created.
1122
1123 <tscreen><verb>
1124    z-assoc callback {scan-response}
1125    ir-scan z-scan z-assoc
1126    z-scan scan "@attr 1=4 science"
1127   
1128    proc scan-response {} {
1129        set status [z-scan status]
1130        if {$status == 0} {
1131            set no [z-scan numberOfTermsReturned]
1132            for {set i 0} {$i < $no} {incr i} {
1133                set line [z-scan scanLine $i]
1134                set type [lindex $line 0]
1135                if {$type == "T"} {
1136                    puts [lindex $line 1]
1137                } elseif {$type == "SD"} {
1138                    puts [lindex $line 1]
1139                }
1140            }
1141        }
1142    }
1143 </verb></tscreen>
1144 <bf/End of examle/
1145
1146 <sect>License
1147
1148 <p>
1149 Copyright &copy; 1995-1996, Index Data.
1150
1151 Permission to use, copy, modify, distribute, and sell this software and
1152 its documentation, in whole or in part, for any purpose, is hereby granted,
1153 provided that:
1154
1155 1. This copyright and permission notice appear in all copies of the
1156 software and its documentation. Notices of copyright or attribution
1157 which appear at the beginning of any file must remain unchanged.
1158
1159 2. The names of Index Data or the individual authors may not be used to
1160 endorse or promote products derived from this software without specific
1161 prior written permission.
1162
1163 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
1164 EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
1165 WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
1166 IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
1167 INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
1168 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
1169 NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
1170 LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
1171 OF THIS SOFTWARE.
1172
1173 <sect>About Index Data
1174
1175 <p>
1176 Index Data is a consulting and software-development enterprise that
1177 specialises in library and information management systems. Our
1178 interests and expertise span a broad range of related fields, and one
1179 of our primary, long-term objectives is the development of a powerful
1180 information management
1181 system with open network interfaces and hypermedia capabilities.
1182
1183 We make this software available free of charge, on a fairly unrestrictive
1184 license; as a service to the networking community, and to further the
1185 development of quality software for open network communication.
1186
1187 We'll be happy to answer questions about the software, and about ourselves
1188 in general.
1189
1190 <tscreen>
1191 Index Data&nl
1192 Ryesgade 3&nl
1193 2200 K&oslash;benhavn N&nl
1194 DENMARK
1195 </tscreen>
1196
1197 <p>
1198 <tscreen><verb>
1199 Phone: +45 3536 3672
1200 Fax  : +45 3536 0449
1201 Email: info@index.ping.dk
1202 </verb></tscreen>
1203
1204 <sect>References
1205
1206 <p>
1207
1208 <descrip>
1209 <tag>1 Ousterhout, John K.:</tag>
1210 Tcl and the Tk Toolkit. Addison-Wesley Company Inc (ISBN
1211 0-201-63337-X). The Tcl/Tk toolkit home page is
1212 <tt><htmlurl url="http://www.sunlabs.com/research/tcl/"
1213  name="http://www.sunlabs.com/research/tcl"></tt>.
1214 The primary ftp site is <tt><htmlurl url="ftp://ftp.smli.com/pub/tcl/"
1215 name="ftp://ftp.smli.com/pub/tcl/"></tt>.
1216 A mirror site can be found at <tt><htmlurl url="ftp://ftp.aud.alcatel.com/tcl/"
1217 name="ftp://ftp.aud.alcatel.com/tcl/"></tt>.
1218 <tag>2 Furniss, Peter:</tag>
1219 RFC 1698: Octet Sequences for Upper-Layer OSI to Support
1220 Basic Communications Applications.
1221 </descrip>
1222
1223 </article>