403bc9c7327710d7918addbf07417048f8152b5b
[yaz-moved-to-github.git] / doc / book.xml
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
3     "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"
4 [
5      <!ENTITY % local SYSTEM "local.ent">
6      %local;
7      <!ENTITY % entities SYSTEM "entities.ent">
8      %entities;
9      <!ENTITY % idcommon SYSTEM "common/common.ent">
10      %idcommon;
11 ]>
12 <book>
13  <bookinfo>
14   <title>YAZ User&apos;s Guide and Reference</title>
15   <authorgroup>
16    <author><firstname>Sebastian</firstname><surname>Hammer</surname></author>
17    <author><firstname>Adam</firstname><surname>Dickmeiss</surname></author>
18    <author><firstname>Mike</firstname><surname>Taylor</surname></author>
19    <author><firstname>Heikki</firstname><surname>Levanto</surname></author>
20    <author><firstname>Dennis</firstname><surname>Schafroth</surname></author>
21   </authorgroup>
22   <releaseinfo>&version;</releaseinfo>
23   <copyright>
24    <year>&copyright-year;</year>
25    <holder>Index Data</holder>
26   </copyright>
27   <abstract>
28    <simpara>
29     This document is the programmer's guide and reference to the &yaz;
30     package version &version;. &yaz; is a compact toolkit that provides
31     access to the Z39.50 and SRU/Solr protocols, as well as a set of
32     higher-level tools for implementing the server and client
33     roles, respectively.
34     The documentation can be used on its own, or as a reference when
35     looking at the example applications provided with the package.
36    </simpara>
37    <simpara>
38     <inlinemediaobject>
39      <imageobject>
40       <imagedata fileref="common/id.png" format="PNG"/>
41      </imageobject>
42      <imageobject>
43       <imagedata fileref="common/id.eps" format="EPS"/>
44      </imageobject>
45     </inlinemediaobject>
46    </simpara></abstract>
47  </bookinfo>
48  <chapter id="introduction">
49   <title>Introduction</title>
50   <para>
51    &yaz; is a C/C++ library for information retrieval applications
52    using the Z39.50/SRU/Solr protocols for information retrieval.
53   </para>
54   <para>
55    Properties of &yaz;:
56    <itemizedlist>
57     <listitem>
58      <para>
59       Complete
60       <ulink url="&url.z39.50;">Z39.50</ulink> version 3 support.
61       Amendments and Z39.50-2002 revision is supported.
62     </para>
63     </listitem>
64     <listitem>
65      <para>
66       Supports
67       <ulink url="&url.sru;">SRU GET/POST/SOAP</ulink>
68       version 1.1, 1.2 and 2.0 (over HTTP and HTTPS).
69     </para>
70     </listitem>
71     <listitem>
72      <para>
73       Includes BER encoders/decoders for the
74       <ulink url="&url.ill;">ISO ILL</ulink>
75       protocol.
76     </para>
77     </listitem>
78     <listitem>
79      <para>
80       Supports
81       <ulink url="&url.solr;">Solr</ulink> Web Service version 1.4.x
82       (client side only)
83      </para>
84     </listitem>
85     <listitem>
86      <para>
87       Supports the following transports: BER over TCP/IP
88       (<ulink url="&url.ber.over.tcpip;">RFC1729</ulink>),
89       BER over unix local socket, and
90       <ulink url="&url.http.1.1;">HTTP 1.1</ulink>.
91     </para>
92     </listitem>
93     <listitem>
94      <para>
95       Secure Socket Layer support using
96       <ulink url="&url.gnutls;">GnuTLS</ulink>.
97       If enabled, &yaz; uses HTTPS transport (for SOAP) or
98       "Secure BER" (for Z39.50).
99      </para>
100     </listitem>
101     <listitem>
102      <para>
103       Offers
104       <ulink url="&url.zoom;">ZOOM</ulink> C API implementing
105       Z39.50, SRU and Solr Web Service.
106     </para>
107     </listitem>
108     <listitem>
109      <para>
110       The &yaz; library offers a set of useful utilities
111       related to the protocols, such as MARC (ISO2709) parser,
112       CCL (ISO8777) parser,
113       <ulink url="&url.cql;">CQL</ulink>
114       parser, memory management routines, character set conversion.
115      </para>
116     </listitem>
117     <listitem>
118      <para>
119       Portable code. &yaz; compiles out-of-the box on most Unixes and
120       on Windows using Microsoft Visual C++.
121      </para>
122     </listitem>
123     <listitem>
124      <para>
125       Fast operation. The C based BER encoders/decoders as well
126       as the server component of &yaz; is very fast.
127     </para>
128     </listitem>
129     <listitem>
130      <para>
131       Liberal license that allows for commercial use of &yaz;.
132      </para>
133     </listitem>
134    </itemizedlist>
135   </para>
136
137   <sect1 id="introduction.reading">
138    <title>Reading this Manual</title>
139    <para>
140     Most implementors only need to read a fraction of the
141     material in thie manual, so a quick walkthrough of the chapters
142     is in order.
143    </para>
144    <itemizedlist>
145     <listitem>
146      <para>
147       <xref linkend="installation"/> contains installation
148       instructions for &yaz;. You don't need reading this
149       if you expect to download &yaz; binaries.
150       However, the chapter contains information about how
151       to make <emphasis>your</emphasis> application link
152       with &yaz;.
153      </para>
154     </listitem>
155     <listitem>
156      <para>
157       <xref linkend="zoom"/> describes the ZOOM API of &yaz;.
158       This is definitely worth a read if you wish to develop a Z39.50/SRU
159       client.
160      </para>
161     </listitem>
162     <listitem>
163      <para>
164       <xref linkend="server"/> describes the generic frontend server
165       and explains how to develop server Z39.50/SRU applications for &yaz;.
166       Obviously worth reading if you're to develop a server.
167     </para>
168     </listitem>
169     <listitem>
170      <para>
171       <xref linkend="yaz-client"/> describes how to use the &yaz; Z39.50
172       client. If you're developer and wish to test your server
173       or a server from another party, you might find this chapter
174       useful.
175     </para>
176     </listitem>
177     <listitem>
178      <para>
179       <xref linkend="asn"/> documents the most commonly used Z39.50
180       C data structures offered by the &yaz; API. Client
181       developers using ZOOM and non-Z39.50 implementors may skip this.
182      </para>
183     </listitem>
184     <listitem>
185      <para>
186       <xref linkend="soap"/> describes how SRU and SOAP is used
187       in &yaz;. Only if you're developing SRU applications
188       this section is a must.
189      </para>
190     </listitem>
191     <listitem>
192      <para>
193       <xref linkend="tools"/> contains sections for the various
194       tools offered by &yaz;. Scan through the material quickly
195       and see what's relevant to you! SRU implementors
196       might find the <link linkend="cql">CQL</link> section
197       particularly useful.
198      </para>
199     </listitem>
200     <listitem>
201      <para>
202       <xref linkend="odr"/> goes through the details of the
203       ODR module which is the work horse that encodes and decodes
204       BER packages. Implementors using ZOOM only, do <emphasis>not</emphasis>
205       need reading this.
206       Most other Z39.50 implementors only need to read the first two
207       sections (<xref linkend="odr.introduction"/> and
208       <xref linkend="odr.use"/>).
209      </para>
210     </listitem>
211     <listitem>
212      <para>
213       <xref linkend="comstack"/> describes the network layer module
214       COMSTACK. Implementors using ZOOM or the generic frontend server
215       may skip this. Others, presumably, handling client/server
216      communication on their own should read this.
217      </para>
218     </listitem>
219    </itemizedlist>
220   </sect1>
221   <sect1 id="introduction.api">
222    <title>The API</title>
223    <para>
224     The <ulink url="&url.yaz;">&yaz;</ulink>
225     toolkit offers several different levels of access to the
226     <ulink url="&url.z39.50;">ISO23950/Z39.50</ulink>,
227     <ulink url="&url.ill;">ILL</ulink> and
228     <ulink url="&url.sru;">SRU</ulink>
229     protocols.
230     The level that you need to use depends on your requirements, and
231     the role (server or client) that you want to implement.
232     If you're developing a client application you should consider the
233     <link linkend="zoom">ZOOM</link> API.
234     It is, by far, the easiest way to develop clients in C.
235     Server implementers should consider the
236     <link linkend="server">generic frontend server</link>.
237     None of those high-level APIs support the whole protocol, but
238     they do include most facilities used in existing Z39.50 applications.
239    </para>
240    <para>
241     If you're using 'exotic' functionality (meaning anything not included in
242     the high-level APIs), developing non-standard extensions to Z39.50 or
243     you're going to develop an ILL application you'll have to learn the lower
244     level APIs of &yaz;.
245    </para>
246    <para>
247     The YAZ toolkit modules is shown in figure <xref linkend="yaz.layer"/>.
248    </para>
249    <figure id="yaz.layer">
250     <title>YAZ layers</title>
251     <mediaobject>
252      <imageobject>
253       <imagedata fileref="apilayer.png" format="PNG"/>
254      </imageobject>
255      <imageobject>
256       <imagedata fileref="apilayer.eps" format="EPS"/>
257      </imageobject>
258     </mediaobject>
259    </figure>
260    <para>
261     There are four layers.
262     <itemizedlist>
263      <listitem>
264       <para>A client or server application (or both).
265        This layer includes ZOOM and the generic frontend server.
266       </para>
267      </listitem>
268      <listitem>
269       <para>
270        The second layer provides a C represenation of the
271        protocol units (packages) for Z39.50 ASN.1, ILL ASN.1,
272        SRU.
273       </para>
274      </listitem>
275      <listitem>
276       <para>
277        The third layer encodes and decodes protocol data units to
278        simple packages (buffer with certain length). The &odr; module
279        encodes and decodes BER whereas the HTTP modules encodes and
280        decodes HTTP ruquests/responses.
281       </para>
282      </listitem>
283      <listitem>
284       <para>
285        The lowest layer is &comstack; which exchanges the encoded packages
286        with a peer process over a network.
287       </para>
288      </listitem>
289     </itemizedlist>
290    </para>
291    <para>
292     The &asn; module represents the ASN.1 definition of
293     the Z39.50 protocol. It establishes a set of type and
294     structure definitions, with one structure for each of the top-level
295     PDUs, and one structure or type for each of the contained ASN.1 types.
296     For primitive types, or other types that are defined by the ASN.1
297     standard itself (such as the EXTERNAL type), the C representation is
298     provided by the &odr; (Open Data Representation) subsystem.
299   </para>
300    <para>
301      &odr; is a basic mechanism for representing an
302     ASN.1 type in the C programming language, and for implementing BER
303     encoders and decoders for values of that type. The types defined in
304     the &asn; module generally have the prefix <literal>Z_</literal>, and
305     a suffix corresponding to the name of the type in the ASN.1
306     specification of the protocol (generally Z39.50-1995). In the case of
307     base types (those originating in the ASN.1 standard itself), the prefix
308     <literal>Odr_</literal> is sometimes seen. Either way, look for
309     the actual definition in either <filename>z-core.h</filename> (for the types
310     from the protocol), <filename>odr.h</filename> (for the primitive ASN.1
311     types).
312     The &asn; library also provides functions (which are, in turn,
313     defined using &odr; primitives) for encoding and decoding data values.
314     Their general form is
315     <funcsynopsis>
316      <funcprototype><funcdef>int <function>z_<replaceable>xxx</replaceable></function></funcdef>
317       <paramdef>ODR <parameter>o</parameter></paramdef>
318       <paramdef>Z_<replaceable>xxx</replaceable> **<parameter>p</parameter></paramdef>
319       <paramdef>int <parameter>optional</parameter></paramdef>
320       <paramdef>const char *<parameter>name</parameter></paramdef>
321      </funcprototype>
322     </funcsynopsis>
323     (note the lower-case &quot;z&quot; in the function name)
324    </para>
325    <note>
326     <para>
327      If you are using the premade definitions of the &asn; module, and you
328      are not adding new protocol of your own, the only parts of &odr; that you
329      need to worry about are documented in
330      <xref linkend="odr.use"/>.
331     </para>
332    </note>
333    <para>
334     When you have created a BER-encoded buffer, you can use the &comstack;
335     subsystem to transmit (or receive) data over the network. The &comstack;
336     module provides simple functions for establishing a connection
337     (passively or actively, depending on the role of your application),
338     and for exchanging BER-encoded PDUs over that connection. When you
339     create a connection endpoint, you need to specify what transport to
340     use (TCP/IP, SSL or UNIX sockets).
341     For the remainder of the connection's lifetime, you don't have
342     to worry about the underlying transport protocol at all - the &comstack;
343     will ensure that the correct mechanism is used.
344    </para>
345    <para>
346     We call the combined interfaces to &odr;, &asn;, and &comstack; the service
347     level API. It's the API that most closely models the Z39.50
348     service/protocol definition, and it provides unlimited access to all
349     fields and facilities of the protocol definitions.
350    </para>
351    <para>
352     The reason that the &yaz; service-level API is a conglomerate of the
353     APIs from three different submodules is twofold. First, we wanted to allow
354     the user a choice of different options for each major task. For instance,
355     if you don't like the protocol API provided by &odr;/&asn;, you
356     can use SNACC or BERUtils instead, and still have the benefits of the
357     transparent transport approach of the &comstack; module. Secondly,
358     we realize that you may have to fit the toolkit into an existing
359     event-processing structure, in a way that is incompatible with
360     the &comstack; interface or some other part of &yaz;.
361    </para>
362   </sect1>
363  </chapter>
364  <chapter id="installation">
365   <title>Compilation and Installation</title>
366   <sect1 id="installation-introduction">
367    <title>Introduction</title>
368    <para>
369     The latest version of the software will generally be found at:
370    </para>
371    <para>
372     <ulink url="&url.yaz.download;"/>
373    </para>
374    <para>
375     We have tried our best to keep the software portable, and on many
376     platforms, you should be able to compile everything with little or
377     no changes.
378    </para>
379    <para>
380     The software is regularly tested on
381     <ulink url="&url.debian;">Debian GNU/Linux</ulink>,
382     <ulink url="&url.centos;">CentOS</ulink>,
383     <ulink url="&url.ubuntu;">Ubuntu Linux</ulink>,
384     <ulink url="&url.freebsd;">FreeBSD (i386)</ulink>,
385     <ulink url="&url.macosx;">MAC OSX</ulink>,
386     <ulink url="&url.solaris;">Solaris</ulink>,
387     Windows 7, Windows XP.
388    </para>
389    <para>
390     Some versions have be known to work on HP/UX,
391     DEC Unix, <ulink url="&url.netbsd;">NetBSD</ulink>,
392     <ulink url="&url.openbsd;">OpenBSD</ulink>,
393     IBM AIX,
394     Data General DG/UX (with some CFLAGS tinkering),
395     SGI/IRIX, DDE Supermax, Apple Macintosh (using the Codewarrior programming
396     environment and the GUSI socket libraries),
397     IBM AS/400 .
398    </para>
399    <para>
400     If you move the software to other platforms, we'd be grateful if you'd
401     let us know about it. If you run into difficulties, we will try to help
402     if we can, and if you solve the problems, we would be happy to include
403     your fixes in the next release. So far, we have mostly avoided
404     <literal>#ifdefs</literal> for individual platforms, and we'd
405     like to keep it that way as far as it makes sense.
406    </para>
407    <para>
408     We maintain a mailing-list for the purpose of announcing new releases and
409     bug-fixes, as well as general discussion. Subscribe by
410     filling-in the form
411     <ulink url="&url.yaz.mailinglist;">here</ulink>.
412     General questions and problems can be directed at
413     <ulink url="&url.yaz.mail;"/>, or the address given at the top of
414      this document.
415    </para>
416   </sect1>
417   <sect1 id="installation.unix"><title>UNIX</title>
418    <para>
419     We provide
420     <ulink url="&url.debian;">Debian GNU/Linux</ulink> (i386 and amd64),
421     <ulink url="&url.ubuntu;">Ubuntu</ulink> (i386 and amd64)
422     and
423     <ulink url="&url.centos;">CentOS</ulink> (amd64 only) packages for &yaz;.
424     You should be able to create packages for other CPUs by building
425     them from the source package.
426    </para>
427    <para>
428     YAZ is also part of several packages repositories. Some of them are
429    </para>
430    <itemizedlist>
431     <listitem>
432      <para>
433       Solaris CSW: <ulink url="http://www.opencsw.org/packages/yaz/"/>
434      </para>
435     </listitem>
436     <listitem>
437      <para>
438       Solaris: <ulink url="http://unixpackages.com"/>
439      </para>
440     </listitem>
441     <listitem>
442      <para>
443       FreeBSD: <ulink url="http://www.freshports.org/net/yaz"/>
444      </para>
445     </listitem>
446     <listitem>
447      <para>
448       Debian: <ulink url="http://packages.debian.org/search?keywords=yaz"/>
449      </para>
450     </listitem>
451     <listitem>
452      <para>
453       Ubuntu: <ulink url="https://launchpad.net/ubuntu/+source/yaz"/>
454      </para>
455     </listitem>
456     <listitem>
457      <para>
458       NetBSD:
459       <ulink url="http://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/net/yaz/README.html"/>
460      </para>
461     </listitem>
462    </itemizedlist>
463    <sect2 id="installation.source.unix">
464     <title>Compiling from source on Unix</title>
465     <para>
466      Note that if your system doesn't have a native ANSI C compiler, you may
467      have to acquire one separately. We recommend
468      <ulink url="&url.gcc;">GCC</ulink>.
469     </para>
470     <para>
471      If you wish to use character set conversion facilities in &yaz; or if you
472      are compiling &yaz; for use with Zebra it is a good idea to ensure that
473      the iconv library is installed. Some Unixes today already have it
474      - if not, we suggest
475      <ulink url="&url.libiconv;">GNU libiconv</ulink>.
476     </para>
477     <para>
478      YAZ 3.0.16 and later includes a wrapper for the
479      <ulink url="&url.icu;">ICU</ulink>
480      (International Components for Unicode).
481      In order to use this, the developer version of the ICU library
482      must be available. ICU support is recommended for applications
483      such as Pazpar2 and Zebra.
484     </para>
485     <para>
486      The <ulink url="&url.libxslt;">libxslt</ulink>,
487      <ulink url="&url.libxml2;">libxml2</ulink> librararies are required
488      if &yaz; is to support SRU/Solr.
489      These libraries are very portable and should compile out-of-the
490      box on virtually all Unix platforms. It is available in binary
491      forms for Linux and others.
492     </para>
493     <para>
494      The GNU tools
495      <ulink url="&url.autoconf;">Autoconf</ulink>,
496      <ulink url="&url.automake;">Automake</ulink> and
497      <ulink url="&url.libtool;">Libtool</ulink>
498      are used to generate Makefiles and configure &yaz; for the system.
499      You do <emphasis>not</emphasis> these tools unless you're using the
500      Git version of &yaz;.
501     </para>
502     <para>
503      The CQL parser for &yaz; is built using
504      GNU <ulink url="&url.bison;">Bison</ulink>.
505      This tool is only needed if you're using the Git version of &yaz;.
506     </para>
507     <para>
508      &yaz; includes a tiny ASN.1 compiler. This compiler is
509      written in <ulink url="&url.tcl;">Tcl</ulink>.
510      But as for Bison you do not need it unless you're using Git
511      version of &yaz; or you're using the compiler to built own codecs
512      for private ASN.1.
513     </para>
514     <para>
515      Generally it should be sufficient to run configure without options,
516      like this:
517     </para>
518     <screen>
519      ./configure
520     </screen>
521     <para>
522      The configure script attempts to use use the C compiler specified by
523      the <literal>CC</literal> environment variable. If not set, GNU C will be
524      used if it is available. The <literal>CFLAGS</literal> environment
525      variable holds options to be passed to the C compiler. If you're using
526      Bourne-compatible shell you may pass something like this to use a
527      particular C compiler with optimization enabled:
528     </para>
529     <screen>
530      CC=/opt/ccs/bin/cc CFLAGS=-O ./configure
531     </screen>
532     <para>
533      To customize &yaz;, the configure script also accepts a set of options.
534      The most important are:
535      <variablelist>
536       <varlistentry>
537        <term>
538         <literal>--prefix</literal>=<replaceable>prefix</replaceable>
539        </term>
540        <listitem>
541         <para>Specifies installation prefix for &yaz;. This is
542         only needed if you run <literal>make install</literal> later to
543         perform a "system" installation. The prefix is
544         <literal>/usr/local</literal> if not specified.
545         </para>
546        </listitem>
547       </varlistentry>
548       <varlistentry>
549        <term>
550         <literal>--enable-tcpd</literal>
551        </term>
552        <listitem>
553         <para>The front end server will be built using Wietse's
554         <ulink url="&url.tcpwrapper;">TCP wrapper library</ulink>.
555         It allows you to allow/deny clients depending on IP number.
556         The TCP wrapper library is often used in GNU/Linux and
557         BSD distributions.
558         See
559         <citerefentry>
560          <refentrytitle>hosts_access</refentrytitle>
561          <manvolnum>5</manvolnum>
562         </citerefentry>
563         and
564         <citerefentry>
565          <refentrytitle>tcpd</refentrytitle>
566          <manvolnum>8</manvolnum>
567          </citerefentry>.
568         </para>
569        </listitem>
570       </varlistentry>
571       <varlistentry>
572        <term>
573         <literal>--enable-threads</literal>
574        </term>
575        <listitem>
576         <para>&yaz; will be built using POSIX threads.
577         Specifically, <constant>_REENTRANT</constant> will be defined during
578         compilation.
579         </para>
580        </listitem>
581       </varlistentry>
582       <varlistentry>
583        <term>
584         <literal>--disable-shared</literal>
585        </term>
586        <listitem>
587         <para>The make process will not create shared
588         libraries (also known as shared objects <filename>.so</filename>).
589         By default, shared libraries are created -
590         equivalent to <literal>--enable-shared</literal>.
591        </para>
592        </listitem>
593       </varlistentry>
594       <varlistentry>
595        <term>
596         <literal>--disable-shared</literal>
597        </term>
598        <listitem>
599         <para>The make process will not create
600         static libraries (<filename>.a</filename>).
601         By default, static libraries are created -
602         equivalent to <literal>--enable-static</literal>.
603         </para>
604        </listitem>
605       </varlistentry>
606       <varlistentry>
607        <term>
608         <literal>--with-iconv</literal>[=<replaceable>prefix</replaceable>]
609        </term>
610        <listitem>
611         <para>Compile &yaz; with iconv library in directory
612         <replaceable>prefix</replaceable>. By default configure will
613         search for iconv on the system. Use this option if it
614         doesn't find iconv. Alternatively,
615         <literal>--without-iconv</literal>, can be uset to force &yaz;
616         not to use iconv.
617         </para>
618        </listitem>
619       </varlistentry>
620       <varlistentry>
621        <term>
622         <literal>--with-xslt</literal>[=<replaceable>prefix</replaceable>]
623        </term>
624        <listitem>
625         <para>Compile &yaz; with
626         <ulink url="&url.libxslt;">libxslt</ulink> in directory
627         <replaceable>prefix</replaceable>.
628         Use this option if you want XSLT and XML support.
629         By default, configure will
630         search for libxslt on the system. Use this option if it
631         libxslt is not found automatically. Alternatively,
632         <literal>--without-xslt</literal>, can be used to force &yaz;
633         not to use libxslt.
634         </para>
635        </listitem>
636       </varlistentry>
637       <varlistentry>
638        <term>
639         <literal>--with-xml2</literal>[=<replaceable>prefix</replaceable>]
640        </term>
641        <listitem>
642         <para>Compile &yaz; with
643         <ulink url="&url.libxml2;">libxml2</ulink> in directory
644         <replaceable>prefix</replaceable>.
645         Use this option if you want &yaz; to use XML and support SRU/Solr.
646         By default, configure will
647         search for libxml2 on the system. Use this option if it
648         libxml2 is not found automatically. Alternatively,
649         <literal>--without-xml2</literal>, can be used to force &yaz;
650         not to use libxml2.
651         </para>
652         <para>
653          Note that option <literal>--with-xslt</literal>
654          also enables libxml2.
655         </para>
656        </listitem>
657       </varlistentry>
658       <varlistentry>
659        <term>
660         <literal>--with-gnutls</literal>[=<replaceable>prefix</replaceable>]
661        </term>
662        <listitem>
663         <para>&yaz; will be linked with the GNU TLS libraries and
664         an SSL COMSTACK will be provided. By default configure enables
665         SSL support for YAZ if the GNU TLS development libraries are found
666         on the system.
667         </para>
668        </listitem>
669       </varlistentry>
670       <varlistentry>
671        <term>
672         <literal>--with-icu</literal>[=<replaceable>prefix</replaceable>]
673        </term>
674        <listitem>
675         <para>&yaz; will be linked the
676         <ulink url="&url.icu;">ICU</ulink> library in the prefix if given.
677         If prefix is not given, the libraries exposed by the script
678         <application>icu-config</application> will be used if found.
679         </para>
680        </listitem>
681       </varlistentry>
682
683       <varlistentry>
684        <term>
685         <literal>--with-libgcrypt</literal>[=<replaceable>prefix</replaceable>]
686        </term>
687        <listitem>
688         <para>&yaz; will be linked with
689         <ulink url="&url.libgcrypt;">Libgcrypt</ulink> in the prefix if given.
690         If prefix is not given, the libraries exposed by the script
691         <application>libgcrypt-config</application> will be used if found.
692         </para>
693        </listitem>
694       </varlistentry>
695       <varlistentry>
696        <term>
697         <literal>--with-memcached</literal>
698        </term>
699        <listitem>
700         <para>&yaz; will be linked with
701         <ulink url="&url.libmemcached;">libMemcached</ulink> to allow
702         for result-set caching for ZOOM.
703         The prefix can not be given. Note that YAZ will only search
704         for libMemcached if Libgcrypt is also enabled.
705         Note that 0.40 of libmemcached is required.
706        </para>
707        </listitem>
708       </varlistentry>
709       <varlistentry>
710        <term>
711         <literal>--with-redis</literal>
712        </term>
713        <listitem>
714         <para>&yaz; will be linked with the hiredis C library
715         to allow for result-set caching for ZOOM on a
716         <ulink url="&url.redis;">redis</ulink> server.
717         The prefix can not be given. Note that YAZ will only search
718         for hiredis if Libgcrypt is also enabled.
719        </para>
720        </listitem>
721       </varlistentry>
722
723      </variablelist>
724     </para>
725     <para>
726      When configured, build the software by typing:
727      <screen>
728       make
729      </screen>
730     </para>
731     <para>
732      The following files are generated by the make process:
733      <variablelist>
734       <varlistentry>
735        <term><filename>src/libyaz.la</filename></term>
736        <listitem><para>
737         Main &yaz; library. This is no ordinary library. It's
738         a Libtool archive.
739         By default, &yaz; creates a static library in
740         <filename>lib/.libs/libyaz.a</filename>.
741        </para></listitem>
742       </varlistentry>
743       <varlistentry>
744        <term><filename>src/libyaz_server.la</filename></term>
745        <listitem><para>
746          Generic Frontend server. This is an add-on for libyaz.la.
747          Code in this library uses POSIX threads functions - if POSIX
748          threads are available on the platform.
749         </para></listitem>
750       </varlistentry>
751       <varlistentry>
752        <term><filename>src/libyaz_icu.la</filename></term>
753        <listitem><para>
754         Functions that wrap the ICU library.
755         </para></listitem>
756       </varlistentry>
757       <varlistentry>
758        <term><filename>ztest/yaz-ztest</filename></term>
759        <listitem><para>Test Z39.50 server.
760        </para></listitem>
761       </varlistentry>
762       <varlistentry>
763        <term><filename>client/yaz-client</filename></term>
764        <listitem><para>Z39.50 client for testing the protocol.
765        See chapter <link linkend="yaz-client">
766        YAZ client</link> for more information.
767        </para></listitem>
768       </varlistentry>
769       <varlistentry>
770        <term><filename>util/yaz-config</filename></term>
771        <listitem><para>A Bourne-shell script, generated by configure, that
772        specifies how external applications should compile - and link with
773        &yaz;.
774        </para></listitem>
775       </varlistentry>
776       <varlistentry>
777        <term><filename>util/yaz-asncomp</filename></term>
778        <listitem><para>The ASN.1 compiler for &yaz;. Requires the
779        Tcl Shell, <application>tclsh</application>, in
780        <literal>PATH</literal> to operate.
781        </para></listitem>
782       </varlistentry>
783       <varlistentry>
784        <term><filename>util/yaz-iconv</filename></term>
785        <listitem><para>This program converts data in one character set to
786        another. This command exercises the YAZ character set
787        conversion API.
788        </para></listitem>
789       </varlistentry>
790       <varlistentry>
791        <term><filename>util/yaz-marcdump</filename></term>
792        <listitem><para>This program parses ISO2709 encoded MARC records
793        and prints them in line-format or XML.
794        </para></listitem>
795       </varlistentry>
796       <varlistentry>
797        <term><filename>util/yaz-icu</filename></term>
798        <listitem><para>This program exposes the ICU wrapper library if that
799        is enabled for YAZ. Only if ICU is available this program is
800        useful.
801        </para></listitem>
802       </varlistentry>
803       <varlistentry>
804        <term><filename>util/yaz-url</filename></term>
805        <listitem><para>This program is a simple HTTP page fetcher ala
806        wget or curl.
807        </para></listitem>
808       </varlistentry>
809       <varlistentry>
810        <term><filename>zoom/zoomsh</filename></term>
811        <listitem><para>
812         A simple shell implemented on top of the
813         <link linkend="zoom">ZOOM</link> functions.
814         The shell is a command line application that allows you to enter
815         simple commands to perform ZOOM operations.
816        </para></listitem>
817       </varlistentry>
818       <varlistentry>
819        <term><filename>zoom/zoomtst1</filename>,
820        <filename>zoom/zoomtst2</filename>, ..</term>
821        <listitem><para>
822         Several small applications that demonstrates the ZOOM API.
823        </para></listitem>
824       </varlistentry>
825      </variablelist>
826     </para>
827     <para>
828      If you wish to install &yaz; in system directories
829      <filename>/usr/local/bin</filename>,
830      <filename>/usr/local/lib</filename> .. etc, you can type:
831     </para>
832     <screen>
833      make install
834     </screen>
835     <para>
836      You probably need to have root access in order to perform this.
837      You must specify the <literal>--prefix</literal> option for configure if
838      you wish to install &yaz; in other directories than the default
839      <filename>/usr/local/</filename>.
840     </para>
841     <para>
842      If you wish to perform an un-installation of &yaz;, use:
843     </para>
844     <screen>
845      make uninstall
846     </screen>
847     <para>
848      This will only work if you haven't reconfigured &yaz; (and therefore
849      changed installation prefix). Note that uninstall will not
850      remove directories created by make install, e.g.
851      <filename>/usr/local/include/yaz</filename>.
852     </para>
853    </sect2>
854    <sect2 id="installation-linking-yaz-unix">
855     <title>How to make apps using YAZ on UNIX</title>
856     <para>
857      This section describes how to compile - and link your own
858      applications using the &yaz; toolkit.
859      If you're used to Makefiles this shouldn't be hard. As for
860      other libraries you have used before, you have to set a proper include
861      path for your C/C++ compiler and specify the location of
862      &yaz; libraries. You can do it by hand, but generally we suggest
863      you use the <filename>yaz-config</filename> that is generated
864      by <filename>configure</filename>. This is especially
865      important if you're using the threaded version of &yaz; which
866      require you to pass more options to your linker/compiler.
867     </para>
868     <para>
869      The <filename>yaz-config</filename> script accepts command line
870      options that makes the <filename>yaz-config</filename> script print
871      options that you should use in your make process.
872      The most important ones are:
873      <literal>--cflags</literal>, <literal>--libs</literal>
874      which prints C compiler flags, and linker flags respectively.
875     </para>
876     <para>
877      A small and complete <literal>Makefile</literal> for a C
878      application consisting of one source file,
879      <filename>myprog.c</filename>, may look like this:
880      <screen>
881       YAZCONFIG=/usr/local/bin/yaz-config
882       CFLAGS=`$(YAZCONFIG) --cflags`
883       LIBS=`$(YAZCONFIG) --libs`
884       myprog: myprog.o
885          $(CC) $(CFLAGS) -o myprog myprog.o $(LIBS)
886       </screen>
887     </para>
888     <para>
889      The CFLAGS variable consists of a C compiler directive that will set
890      the include path to the <emphasis>parent</emphasis> directory
891      of <filename>yaz</filename>. That is, if &yaz; header files were
892      installed in <filename>/usr/local/include/yaz</filename>,
893      then include path is set to <filename>/usr/local/include</filename>.
894      Therefore, in your applications you should use
895      <screen>
896       #include &lt;yaz/proto.h>
897      </screen>
898      and <emphasis>not</emphasis>
899      <screen>
900       #include &lt;proto.h>
901      </screen>
902     </para>
903     <para>
904      For Libtool users, the <filename>yaz-config</filename> script provides
905      a different variant of option <literal>--libs</literal>, called
906      <literal>--lalibs</literal> that returns the name of the
907      Libtool archive(s) for &yaz; rather than the ordinary ones.
908     </para>
909     <para>
910      For applications using the threaded version of &yaz;,
911      specify <literal>threads</literal> after the
912      other options. When <literal>threads</literal> is given,
913      more flags and linker flags will be printed by
914      <filename>yaz-config</filename>. If our previous example was
915       using threads, you'd have to modify the lines that set
916      <literal>CFLAGS</literal> and <literal>LIBS</literal> as
917      follows:
918      <screen>
919       CFLAGS=`$(YAZCONFIG) --cflags threads`
920       LIBS=`$(YAZCONFIG) --libs threads`
921      </screen>
922      There is no need specify POSIX thread libraries in your Makefile.
923      The <literal>LIBS</literal> variable includes that as well.
924     </para>
925    </sect2>
926   </sect1>
927   <sect1 id="installation.win32">
928    <title>WIN32</title>
929    <para>The easiest way to install YAZ on Windows is by downloading
930    an installer from
931    <ulink url="&url.yaz.download.win32;">here</ulink>.
932    The installer comes with source too - in case you wish to
933    compile YAZ with different compiler options, etc.
934    </para>
935
936    <sect2 id="installation.win32.source">
937     <title>Compiling from Source on WIN32</title>
938     <para>
939      &yaz; is shipped with "makefiles" for the NMAKE tool that comes
940      with <ulink url="&url.vstudio;">
941      Microsoft Visual Studio</ulink>. It has been tested with
942      Microsoft Visual Studio 2003/2005/2008.
943     </para>
944     <para>
945      Start a command prompt and switch the sub directory
946      <filename>WIN</filename> where the file <filename>makefile</filename>
947      is located. Customize the installation by editing the
948      <filename>makefile</filename> file (for example by using notepad).
949      The following summarizes the most important settings in that file:
950      <variablelist>
951       <varlistentry>
952        <term><literal>DEBUG</literal></term>
953        <listitem><para>
954         If set to 1, the software is
955         compiled with debugging libraries (code generation is
956         multi-threaded debug DLL).
957         If set to 0, the software is compiled with release libraries
958         (code generation is multi-threaded DLL).
959        </para></listitem>
960       </varlistentry>
961       <varlistentry>
962        <term><literal>HAVE_TCL</literal>, <literal>TCL</literal></term>
963        <listitem><para>
964         If <literal>HAVE_TCL</literal> is set to 1, nmake will
965         use the ASN.1 compiler (<ulink url="&url.tcl;">Tcl</ulink> based).
966         You must set <literal>TCL</literal> to the full path of the Tcl
967         interpreter. A Windows version of Tcl is part of
968         <ulink url="&url.gitwindows;">Git for Windows</ulink>.
969        </para>
970        <para>
971         If you do not have Tcl installed, set
972         <literal>HAVE_TCL</literal> to 0.
973        </para></listitem>
974       </varlistentry>
975       <varlistentry>
976        <term><literal>HAVE_BISON</literal>,
977        <literal>BISON</literal></term>
978        <listitem><para>
979         If GNU Bison is present, you might set <literal>HAVE_BISON</literal>
980         to 1 and specify the Bison executable in <literal>BISON</literal>.
981         Bison is only required if you use the Git version of
982         YAZ or if you modify the grammar for CQL
983         (<filename>cql.y</filename>).
984        </para>
985        <para>
986         A Windows version of GNU Bison is part of
987         <ulink url="&url.gitwindows;">Git for Windows</ulink>.
988        </para></listitem>
989       </varlistentry>
990       <varlistentry>
991        <term><literal>HAVE_ICONV</literal>,
992        <literal>ICONV_DIR</literal></term>
993        <listitem><para>
994         If <literal>HAVE_ICONV</literal> is set to 1, YAZ is compiled
995         with iconv support. In this configuration, set
996         <literal>ICONV_DIR</literal> to the iconv source directory.
997        </para></listitem>
998       </varlistentry>
999       <varlistentry>
1000        <term><literal>HAVE_LIBXML2</literal>,
1001        <literal>LIBXML2_DIR</literal></term>
1002        <listitem>
1003         <para>
1004          If <literal>HAVE_LIBXML2</literal> is set to 1, YAZ is compiled
1005          with SRU support. In this configuration, set
1006          <literal>LIBXML2_DIR</literal> to the
1007          <ulink url="&url.libxml2;">libxml2</ulink> source directory
1008          and
1009          <literal>ZLIB_DIR</literal> to the zlib directory.
1010         </para>
1011         <para>
1012          Windows versions of libxslt, libxml2, zlib and iconv can be found
1013          <ulink url="&url.libxml2.download.win32;">
1014           Igor Zlatkovi&#x0107;' site</ulink>.
1015         </para>
1016         <note>
1017          <para>
1018           YAZ is not using zlib but libxml2 is depending on it.
1019          </para>
1020         </note>
1021        </listitem>
1022       </varlistentry>
1023       <varlistentry>
1024        <term><literal>HAVE_LIBXSLT</literal>,
1025        <literal>LIBXSLT_DIR</literal></term>
1026        <listitem>
1027         <para>
1028          If <literal>HAVE_LIBXSLT</literal> is set to 1, YAZ is compiled
1029          with XSLT support. In this configuration, set
1030          <literal>LIBXSLT_DIR</literal> to the
1031          <ulink url="&url.libxslt;">libxslt</ulink> source directory.
1032         </para>
1033         <note>
1034          <para>
1035           libxslt depends libxml2.
1036          </para>
1037         </note>
1038        </listitem>
1039       </varlistentry>
1040       <varlistentry>
1041        <term><literal>HAVE_ICU</literal>,
1042        <literal>ICU_DIR</literal></term>
1043        <listitem>
1044         <para>
1045          If <literal>HAVE_ICU</literal> is set to 1, YAZ is compiled
1046          with <ulink url="&url.icu;">ICU</ulink> support.
1047          In this configuration, set
1048          <literal>ICU_DIR</literal> to the
1049          <ulink url="&url.icu;">ICU</ulink> source directory.
1050         </para>
1051        </listitem>
1052       </varlistentry>
1053      </variablelist>
1054     </para>
1055     <para>
1056      When satisfied with the settings in the makefile, type
1057      <screen>
1058       nmake
1059      </screen>
1060     </para>
1061     <note>
1062      <para>
1063       If the <filename>nmake</filename> command is not found on your system
1064       you probably haven't defined the environment variables required to
1065       use that tool. To fix that, find and run the batch file
1066       <filename>vcvars32.bat</filename>. You need to run it from within
1067       the command prompt or set the environment variables "globally";
1068       otherwise it doesn't work.
1069      </para>
1070     </note>
1071     <para>
1072      If you wish to recompile &yaz; - for example if you modify
1073      settings in the <filename>makefile</filename> you can delete
1074      object files, etc by running.
1075      <screen>
1076       nmake clean
1077      </screen>
1078     </para>
1079     <para>
1080      The following files are generated upon successful compilation:
1081      <variablelist>
1082       <varlistentry>
1083        <term><filename>bin/yaz&soversion;.dll</filename> /
1084        <filename>bin/yaz&soversion;d.dll</filename></term>
1085        <listitem><para>
1086         &yaz; Release/Debug DLL.
1087        </para></listitem>
1088       </varlistentry>
1089       <varlistentry>
1090        <term><filename>lib/yaz&soversion;.lib</filename> /
1091        <filename>lib/yaz&soversion;d.lib</filename></term>
1092        <listitem><para>
1093         Import library for <filename>yaz&soversion;.dll</filename> /
1094         <filename>yaz&soversion;d.dll</filename>.
1095       </para></listitem>
1096       </varlistentry>
1097       <varlistentry>
1098        <term><filename>bin/yaz_cond&soversion;.dll</filename> /
1099        <filename>bin/yaz_cond&soversion;d.dll</filename></term>
1100        <listitem><para>
1101         Release/Debug DLL for condition variable utilities (condvar.c).
1102        </para></listitem>
1103       </varlistentry>
1104       <varlistentry>
1105        <term><filename>lib/yaz_cond&soversion;.lib</filename> /
1106        <filename>lib/yaz_cond&soversion;d.lib</filename></term>
1107        <listitem><para>
1108         Import library for <filename>yaz_cond&soversion;.dll</filename> /
1109         <filename>yaz_cond&soversion;d.dll</filename>.
1110        </para></listitem>
1111       </varlistentry>
1112       <varlistentry>
1113        <term><filename>bin/yaz_icu&soversion;.dll</filename> /
1114        <filename>bin/yaz_icu&soversion;d.dll</filename></term>
1115        <listitem><para>
1116         Release/Debug DLL for the ICU wrapper utility.
1117         Only build if HAVE_ICU is 1.
1118        </para></listitem>
1119       </varlistentry>
1120       <varlistentry>
1121        <term><filename>lib/yaz_icu&soversion;.lib</filename> /
1122        <filename>lib/yaz_icu&soversion;d.lib</filename></term>
1123        <listitem><para>
1124         Import library for <filename>yaz_icu&soversion;.dll</filename> /
1125         <filename>yaz_icu&soversion;d.dll</filename>.
1126        </para></listitem>
1127       </varlistentry>
1128       <varlistentry>
1129        <term><filename>bin/yaz-ztest.exe</filename></term>
1130        <listitem><para>
1131         Z39.50 multi-threaded test/example server. It's a WIN32
1132         console application.
1133       </para></listitem>
1134       </varlistentry>
1135       <varlistentry>
1136        <term><filename>bin/yaz-client.exe</filename></term>
1137        <listitem><para>
1138         &yaz; Z39.50 client application. It's a WIN32 console application.
1139         See chapter <link linkend="yaz-client">YAZ client</link> for more
1140         information.
1141       </para></listitem>
1142       </varlistentry>
1143       <varlistentry>
1144        <term><filename>bin/yaz-icu.exe</filename></term>
1145        <listitem><para>This program exposes the ICU wrapper library if that
1146        is enabled for YAZ. Only if ICU is available this program is
1147        build.
1148       </para></listitem>
1149       </varlistentry>
1150       <varlistentry>
1151        <term><filename>bin/zoomsh.exe</filename></term>
1152        <listitem><para>
1153         Simple console application implemented on top of the
1154         <link linkend="zoom">ZOOM</link> functions.
1155         The application is a command line shell that allows you to enter
1156         simple commands to perform ZOOM operations.
1157       </para></listitem>
1158       </varlistentry>
1159       <varlistentry>
1160        <term><filename>bin/zoomtst1.exe</filename>,
1161        <filename>bin/zoomtst2.exe</filename>, ..</term>
1162        <listitem><para>
1163         Several small applications that demonstrates the ZOOM API.
1164       </para></listitem>
1165       </varlistentry>
1166      </variablelist>
1167     </para>
1168    </sect2>
1169
1170    <sect2 id="installation-linking-yaz-win32">
1171     <title>How to make apps using YAZ on WIN32</title>
1172     <para>
1173      This section will go though the process of linking your WIN32
1174      applications with &yaz;.
1175     </para>
1176     <para>
1177      Some people are confused by the fact that we use the nmake
1178      tool to build &yaz;. They think they have to do that too - in order
1179      to make their WIN32 applications work with &yaz;. The good news is that
1180      you don't have to. You can use the integrated environment of
1181      Visual Studio if desired for your own application.
1182     </para>
1183     <para>
1184      When setting up a project or Makefile you have to set the following:
1185      <variablelist>
1186       <varlistentry>
1187        <term>include path</term>
1188        <listitem><para>
1189         Set it to the <filename>include</filename> directory of &yaz;.
1190         </para></listitem>
1191       </varlistentry>
1192       <varlistentry>
1193        <term>import library <filename>yaz&soversion;.lib</filename></term>
1194        <listitem><para>
1195         You must link with this library. It's located in the
1196         sub directory <filename>lib</filename> of &yaz;.
1197         If you want to link with the debug version of &yaz;, you must
1198         link against <filename>yaz&soversion;d.lib</filename> instead.
1199        </para></listitem>
1200       </varlistentry>
1201       <varlistentry>
1202        <term>dynamic link library
1203        <filename>yaz&soversion;.dll</filename>
1204        </term>
1205        <listitem><para>
1206         This DLL must be in your execution path when you invoke
1207         your application. Specifically, you should distribute this
1208         DLL with your application.
1209        </para></listitem>
1210       </varlistentry>
1211      </variablelist>
1212     </para>
1213    </sect2>
1214   </sect1>
1215  </chapter>
1216  <!--
1217      ### Still to document:
1218      ZOOM_connection_errcode(c)
1219      ZOOM_connection_errmsg(c)
1220      ZOOM_connection_addinfo(c)
1221      ZOOM_connection_addinfo(c)
1222      ZOOM_connection_diagset(c);
1223      ZOOM_connection_save_apdu_wrbuf
1224      ZOOM_diag_str(error)
1225      ZOOM_resultset_record_immediate(s, pos)
1226      ZOOM_resultset_cache_reset(r)
1227      ZOOM_options_set_callback(opt, function, handle)
1228      ZOOM_options_create_with_parent2(parent1, parent2)
1229      ZOOM_options_getl(opt, name, len)
1230      ZOOM_options_setl(opt, name, value, len)
1231      ZOOM_options_get_bool(opt, name, defa)
1232      ZOOM_options_get_int(opt, name, defa)
1233      ZOOM_options_set_int(opt, name, value)
1234  -->
1235  <chapter id="zoom">
1236   <title>ZOOM</title>
1237   <para>
1238    &zoom; is an acronym for 'Z39.50 Object-Orientation Model' and is
1239    an initiative started by Mike Taylor (Mike is from the UK, which
1240    explains the peculiar name of the model). The goal of &zoom; is to
1241    provide a common Z39.50 client API not bound to a particular
1242    programming language or toolkit.
1243   </para>
1244   <para>
1245    From YAZ version 2.1.12, <ulink url="&url.sru;">SRU</ulink> is supported.
1246    You can make SRU ZOOM connections by specifying scheme
1247    <literal>http://</literal> for the hostname for a connection.
1248    The dialect of SRU used is specified by the value of the
1249    connection's <literal>sru</literal> option, which may be SRU over
1250    HTTP GET (<literal>get</literal>),
1251    SRU over HTTP POST (<literal>post</literal>), (SRU over
1252    SOAP) (<literal>soap</literal>) or <literal>solr</literal>
1253    (<ulink url="&url.solr;">Solr</ulink> Web Service).
1254    Using the facility for embedding options in target strings, a
1255    connection can be forced to use SRU rather the SRW (the default) by
1256    prefixing the target string with <literal>sru=get,</literal>, like this:
1257    <literal>sru=get,http://sru.miketaylor.org.uk:80/sru.pl</literal>
1258   </para>
1259   <para>
1260    <ulink url="&url.solr;">Solr</ulink>  protocol support was added to
1261    YAZ in version 4.1.0, as a dialect of a SRU protocol, since both are
1262    HTTP based protocols.
1263   </para>
1264   <para>
1265    The lack of a simple Z39.50 client API for &yaz; has become more
1266    and more apparent over time. So when the first &zoom; specification
1267    became available,
1268    an implementation for &yaz; was quickly developed. For the first time, it is
1269    now as easy (or easier!) to develop clients than servers with &yaz;. This
1270    chapter describes the &zoom; C binding. Before going further, please
1271    reconsider whether C is the right programming language for the job.
1272    There are other language bindings available for &yaz;, and still
1273    more
1274    are in active development. See the
1275    <ulink url="&url.zoom;">ZOOM web-site</ulink> for
1276    more information.
1277   </para>
1278   <para>
1279    In order to fully understand this chapter you should read and
1280    try the example programs <literal>zoomtst1.c</literal>,
1281    <literal>zoomtst2.c</literal>, .. in the <literal>zoom</literal>
1282    directory.
1283   </para>
1284   <para>
1285    The C language misses features found in object oriented languages
1286    such as C++, Java, etc. For example, you'll have to manually,
1287    destroy all objects you create, even though you may think of them as
1288    temporary. Most objects has a <literal>_create</literal> - and a
1289    <literal>_destroy</literal> variant.
1290    All objects are in fact pointers to internal stuff, but you don't see
1291    that because of typedefs. All destroy methods should gracefully ignore a
1292    <literal>NULL</literal> pointer.
1293   </para>
1294   <para>
1295    In each of the sections below you'll find a sub section called
1296    protocol behavior, that describes how the API maps to the Z39.50
1297    protocol.
1298   </para>
1299   <sect1 id="zoom-connections">
1300    <title>Connections</title>
1301    <para>The Connection object is a session with a target.
1302    </para>
1303    <synopsis>
1304     #include &lt;yaz/zoom.h>
1305
1306     ZOOM_connection ZOOM_connection_new(const char *host, int portnum);
1307
1308     ZOOM_connection ZOOM_connection_create(ZOOM_options options);
1309
1310     void ZOOM_connection_connect(ZOOM_connection c, const char *host,
1311                                  int portnum);
1312     void ZOOM_connection_destroy(ZOOM_connection c);
1313    </synopsis>
1314    <para>
1315     Connection objects are created with either function
1316     <function>ZOOM_connection_new</function> or
1317     <function>ZOOM_connection_create</function>.
1318     The former creates and automatically attempts to establish a network
1319     connection with the target. The latter doesn't establish
1320     a connection immediately, thus allowing you to specify options
1321     before establishing network connection using the function
1322     <function>ZOOM_connection_connect</function>.
1323     If the port number, <literal>portnum</literal>, is zero, the
1324     <literal>host</literal> is consulted for a port specification.
1325     If no port is given, 210 is used. A colon denotes the beginning of
1326     a port number in the host string. If the host string includes a
1327     slash, the following part specifies a database for the connection.
1328    </para>
1329    <para>
1330     You can prefix the host with a scheme followed by colon. The
1331     default scheme is <literal>tcp</literal> (Z39.50 protocol).
1332     The scheme <literal>http</literal> selects SRU/get over HTTP by default,
1333     but can overridded to use SRU/post, SRW and the Solr protocol.
1334    </para>
1335    <para>
1336     You can prefix the scheme-qualified host-string with one or more
1337     comma-separated
1338     <literal><parameter>key</parameter>=<parameter>value</parameter></literal>
1339     sequences, each of which represents an option to be set into the
1340     connection structure <emphasis>before</emphasis> the
1341     protocol-level connection is forged and the initialization
1342     handshake takes place.  This facility can be used to provide
1343     authentication credentials, as in host-strings such as:
1344     <literal>user=admin,password=halfAm4n,tcp:localhost:8017/db</literal>
1345    </para>
1346    <para>
1347     Connection objects should be destroyed using the function
1348     <function>ZOOM_connection_destroy</function>.
1349    </para>
1350    <synopsis>
1351     void ZOOM_connection_option_set(ZOOM_connection c,
1352                                     const char *key, const char *val);
1353
1354     void ZOOM_connection_option_setl(ZOOM_connection c,
1355                                      const char *key,
1356                                      const char *val, int len);
1357
1358     const char *ZOOM_connection_option_get(ZOOM_connection c,
1359                                            const char *key);
1360     const char *ZOOM_connection_option_getl(ZOOM_connection c,
1361                                             const char *key,
1362                                             int *lenp);
1363    </synopsis>
1364    <para>
1365     The functions <function>ZOOM_connection_option_set</function> and
1366     <function>ZOOM_connection_option_setl</function> allows you to
1367     set an option given by <parameter>key</parameter> to the value
1368     <parameter>value</parameter> for the connection.
1369     For <function>ZOOM_connection_option_set</function>, the
1370     value is assumed to be a 0-terminated string. Function
1371     <function>ZOOM_connection_option_setl</function> specifies a
1372     value of a certain size (len).
1373    </para>
1374    <para>
1375     Functions <function>ZOOM_connection_option_get</function> and
1376     <function>ZOOM_connection_option_getl</function> returns
1377     the value for an option given by <parameter>key</parameter>.
1378    </para>
1379    <table id="zoom-connection-options" frame="top">
1380     <title>ZOOM Connection Options</title>
1381     <tgroup cols="3">
1382      <colspec colwidth="4*" colname="name"></colspec>
1383      <colspec colwidth="7*" colname="description"></colspec>
1384      <colspec colwidth="3*" colname="default"></colspec>
1385      <thead>
1386       <row>
1387        <entry>Option</entry>
1388        <entry>Description</entry>
1389        <entry>Default</entry>
1390       </row>
1391      </thead>
1392      <tbody>
1393       <row><entry>
1394        implementationName</entry><entry>Name of Your client
1395       </entry><entry>none</entry></row>
1396       <row><entry>
1397        user</entry><entry>Authentication user name
1398       </entry><entry>none</entry></row>
1399       <row><entry>
1400        group</entry><entry>Authentication group name
1401       </entry><entry>none</entry></row>
1402       <row><entry>
1403        password</entry><entry>Authentication password.
1404       </entry><entry>none</entry></row>
1405       <row><entry>
1406        authenticationMode</entry><entry>How authentication is encoded.
1407       </entry><entry>basic</entry></row>
1408       <row><entry>
1409        host</entry><entry>Target host. This setting is "read-only".
1410        It's automatically set internally when connecting to a target.
1411       </entry><entry>none</entry></row>
1412       <row><entry>
1413        proxy</entry><entry>Proxy host. If set, the logical host
1414        is encoded in the otherInfo area of the Z39.50 Init PDU
1415        with OID 1.2.840.10003.10.1000.81.1.
1416       </entry><entry>none</entry></row>
1417       <row><entry>
1418        clientIP</entry><entry>Client IP. If set, is
1419        encoded in the otherInfo area of a Z39.50 PDU with OID
1420        1.2.840.10003.10.1000.81.3. Holds the original IP addreses
1421        of a client. Is used of ZOOM is used in a gateway of some sort.
1422       </entry><entry>none</entry></row>
1423       <row><entry>
1424        async</entry><entry>If true (1) the connection operates in
1425        asynchronous operation which means that all calls are non-blocking
1426        except
1427        <link linkend="zoom.events"><function>ZOOM_event</function></link>.
1428       </entry><entry>0</entry></row>
1429       <row><entry>
1430        maximumRecordSize</entry><entry> Maximum size of single record.
1431       </entry><entry>1 MB</entry></row>
1432       <row><entry>
1433        preferredMessageSize</entry><entry> Maximum size of multiple records.
1434       </entry><entry>1 MB</entry></row>
1435       <row><entry>
1436        lang</entry><entry> Language for negotiation.
1437       </entry><entry>none</entry></row>
1438       <row><entry>
1439        charset</entry><entry> Character set for negotiation.
1440       </entry><entry>none</entry></row>
1441       <row><entry>
1442        serverImplementationId</entry><entry>
1443        Implementation ID of server.  (The old targetImplementationId
1444        option is also supported for the benefit of old applications.)
1445       </entry><entry>none</entry></row>
1446       <row><entry>
1447        targetImplementationName</entry><entry>
1448        Implementation Name of server.  (The old
1449        targetImplementationName option is also supported for the
1450        benefit of old applications.)
1451       </entry><entry>none</entry></row>
1452       <row><entry>
1453        serverImplementationVersion</entry><entry>
1454        Implementation Version of server.  (the old
1455        targetImplementationVersion option is also supported for the
1456        benefit of old applications.)
1457       </entry><entry>none</entry></row>
1458       <row><entry>
1459        databaseName</entry><entry>One or more database names
1460        separated by character plus (<literal>+</literal>), which to
1461        be used by subsequent search requests on this Connection.
1462       </entry><entry>Default</entry></row>
1463       <row><entry>
1464        piggyback</entry><entry>True (1) if piggyback should be
1465        used in searches; false (0) if not.
1466       </entry><entry>1</entry></row>
1467       <row><entry>
1468        smallSetUpperBound</entry><entry>If hits is less than or equal to this
1469        value, then target will return all records using small element set name
1470       </entry><entry>0</entry></row>
1471       <row><entry>
1472        largeSetLowerBound</entry><entry>If hits is greater than this
1473        value, the target will return no records.
1474       </entry><entry>1</entry></row>
1475       <row><entry>
1476        mediumSetPresentNumber</entry><entry>This value represents
1477        the number of records to be returned as part of a search when when
1478        hits is less than or equal to large set lower bound and if hits
1479        is greater than small set upper bound.
1480       </entry><entry>0</entry></row>
1481       <row><entry>
1482        smallSetElementSetName</entry><entry>
1483        The element set name to be used for small result sets.
1484       </entry><entry>none</entry></row>
1485       <row><entry>
1486        mediumSetElementSetName</entry><entry>
1487        The element set name to be for medium-sized result sets.
1488       </entry><entry>none</entry></row>
1489       <row><entry>
1490        init_opt_search, init_opt_present, init_opt_delSet, etc.</entry><entry>
1491        After a successful Init, these options may be interrogated to
1492        discover whether the server claims to support the specified
1493        operations.
1494       </entry><entry>none</entry></row>
1495       <row>
1496        <entry>sru</entry><entry>
1497        SRU/Solr transport type. Must be either <literal>soap</literal>,
1498        <literal>get</literal>, <literal>post</literal>, or
1499        <literal>solr</literal>.
1500       </entry><entry>soap</entry></row>
1501       <row><entry>
1502        sru_version</entry><entry>
1503        SRU/SRW version. Should be <literal>1.1</literal>, or
1504        <literal>1.2</literal>. This is , prior to connect, the version
1505        to offer (highest version). And following connect (in fact
1506        first operation), holds the negotiated version with the server
1507        (same or lower version).
1508       </entry><entry>1.2</entry></row>
1509       <row id="zoom.facets.option"><entry>
1510        facets</entry><entry>
1511        Requested or recommend facets may be given before a search is sent.
1512        The value of this setting is described in <xref linkend="facets"/>
1513        For inspection of the facets returned, refer to the functions
1514        described in <xref linkend="zoom.facets"/>.
1515       </entry><entry>none</entry></row>
1516       <row><entry>
1517        apdulog</entry><entry>
1518        If set to a true value such as "1", a log of low-level
1519        protocol packets is emitted on standard error stream.  This
1520        can be very useful for debugging.
1521       </entry><entry>0</entry></row>
1522       <row><entry>
1523        saveAPDU</entry><entry>
1524        If set to a true value such as "1", a log of low-level
1525        protocol packets is saved. The log can be retrieved by reading
1526        option APDU. Setting saveAPDU always has the side effect of
1527        resetting the currently saved log. This setting is
1528        <emphasis>write-only</emphasis>. If read, NULL will be returned.
1529        It is only recognized in
1530        <function>ZOOM_connection_option_set</function>.
1531       </entry><entry>0</entry></row>
1532       <row><entry>
1533        APDU</entry><entry>
1534        Returns the log of protocol packets. Will be empty if logging
1535        is not enabled (see saveAPDU above). This setting is
1536        <emphasis>read-only</emphasis>. It is only recognized if used
1537        in call to <function>ZOOM_connection_option_get</function> or
1538        <function>ZOOM_connection_option_getl</function>.
1539       </entry><entry></entry></row>
1540       <row><entry>
1541        memcached</entry><entry>
1542        If given and non-empty,
1543        <ulink url="&url.libmemcached;">libMemcached</ulink>
1544        will be configured for the connection.
1545        This option is inspected by ZOOM when a connection is  established.
1546        If the <literal>memcached</literal> option is given
1547        and YAZ is compiled without libMemcached support, an internal
1548        diagnostic (10018) will be thrown.
1549        libMemcached support is available for YAZ 5.0.13 or later. If this
1550        option is supplied for an earlier version of YAZ, it is
1551        <emphasis>ignored</emphasis>.
1552        The value of this option is a string passed verbatim to
1553        the <function>memcached</function> function part of libMemcached.
1554        Refer to man page
1555        <ulink url="http://manned.org/memcached.3">memcached(3)</ulink>.
1556        Earlier versions of libMemcached
1557        do not offer this function. In this case only the option
1558        <literal>--server=</literal><replaceable>host</replaceable> may
1559        be given (YAZ emulates that part of libMemcached).
1560       </entry><entry>none</entry></row>
1561       <row><entry>
1562        redis</entry><entry>
1563        If given and non-empty,
1564        a <ulink url="&url.redis;">redis</ulink> context will be created
1565        for the connection.
1566        This option is inspected by ZOOM when a connection is established.
1567        If the <literal>redis</literal> option is given
1568        and YAZ is compiled without redis support, an internal
1569        diagnostic (10018) will be thrown.
1570        redis support is available for YAZ 5.2.0 or later. If this
1571        option is supplied for an earlier version of YAZ, it is
1572        <emphasis>ignored</emphasis>.
1573        The value of this option is a set options, similar to that
1574        of the memcached function. At this stage only --server=host[:port]
1575        is supported. Later versions of YAZ might honor expiry for various
1576        items and other things that tune the redis usage.
1577       </entry><entry>none</entry></row>
1578      </tbody>
1579     </tgroup>
1580    </table>
1581    <para>
1582     If either option <literal>lang</literal> or <literal>charset</literal>
1583     is set, then
1584     <ulink url="&url.z39.50.charneg;">
1585      Character Set and Language Negotiation</ulink> is in effect.
1586    </para>
1587    <synopsis>
1588      int ZOOM_connection_error(ZOOM_connection c, const char **cp,
1589                                const char **addinfo);
1590      int ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1591                                  const char **addinfo, const char **dset);
1592    </synopsis>
1593    <para>
1594     Function <function>ZOOM_connection_error</function> checks for
1595     errors for the last operation(s) performed. The function returns
1596     zero if no errors occurred; non-zero otherwise indicating the error.
1597     Pointers <parameter>cp</parameter> and <parameter>addinfo</parameter>
1598     holds messages for the error and additional-info if passed as
1599     non-<literal>NULL</literal>. Function
1600     <function>ZOOM_connection_error_x</function> is an extended version
1601     of <function>ZOOM_connection_error</function> that is capable of
1602     returning name of diagnostic set in <parameter>dset</parameter>.
1603    </para>
1604    <sect2 id="zoom-connection-z39.50">
1605     <title>Z39.50 Protocol behavior</title>
1606     <para>
1607      The calls <function>ZOOM_connection_new</function> and
1608      <function>ZOOM_connection_connect</function> establishes a TCP/IP
1609      connection and sends an Initialize Request to the target if
1610      possible. In addition, the calls waits for an Initialize Response
1611      from the target and the result is inspected (OK or rejected).
1612     </para>
1613     <para>
1614      If <literal>proxy</literal> is set then the client will establish
1615      a TCP/IP connection with the peer as specified by the
1616      <literal>proxy</literal> host and the hostname as part of the
1617      connect calls will be set as part of the Initialize Request.
1618      The proxy server will then "forward" the PDU's transparently
1619      to the target behind the proxy.
1620     </para>
1621     <para>
1622      For the authentication parameters, if option <literal>user</literal>
1623      is set and both options <literal>group</literal> and
1624      <literal>pass</literal> are unset, then Open style
1625      authentication is used (Version 2/3) in which case the username
1626      is usually followed by a slash, then by a password.
1627      If either <literal>group</literal>
1628      or <literal>pass</literal> is set then idPass authentication
1629      (Version 3 only) is used. If none of the options are set, no
1630      authentication parameters are set as part of the Initialize Request
1631      (obviously).
1632     </para>
1633     <para>
1634      When option <literal>async</literal> is 1, it really means that
1635      all network operations are postponed (and queued) until the
1636      function <literal>ZOOM_event</literal> is invoked. When doing so
1637      it doesn't make sense to check for errors after
1638      <literal>ZOOM_connection_new</literal> is called since that
1639      operation "connecting - and init" is still incomplete and the
1640      API cannot tell the outcome (yet).
1641     </para>
1642     </sect2>
1643    <sect2 id="zoom.sru.init.behavior">
1644     <title>SRU/Solr Protocol behavior</title>
1645     <para>
1646      The HTTP based protocols (SRU, SRW, Solr) doesn't feature an
1647      Inititialize Request, so  the connection phase merely establishes a
1648      TCP/IP connection with the HTTP server.
1649     </para>
1650     <para>Most of the ZOOM connection options do not
1651      affect SRU/Solr and they are ignored. However, future versions
1652      of &yaz; might honor <literal>implementationName</literal> and
1653      put that as part of User-Agent header for HTTP requests.
1654      </para>
1655     <para>
1656      The <literal>charset</literal> is used in the Content-Type header
1657      of HTTP requests.
1658     </para>
1659     <para>
1660      Setting <literal>authentcationMode</literal> specifies how
1661      authentication parameters are encoded for HTTP. The default is
1662      "<literal>basic</literal>" where <literal>user</literal> and
1663      <literal>password</literal> are encoded by using HTTP basic
1664      authentication.
1665      </para>
1666     <para>
1667      If <literal>authentcationMode</literal> is "<literal>url</literal>", then
1668      user and password are encoded in the URL by parameters
1669      <literal>x-username</literal> and <literal>x-password</literal> as
1670      given by the SRU standard.
1671     </para>
1672    </sect2>
1673   </sect1>
1674   <sect1 id="zoom.query">
1675    <title>Queries</title>
1676    <para>
1677     Query objects represents queries.
1678    </para>
1679    <synopsis>
1680      ZOOM_query ZOOM_query_create(void);
1681
1682      void ZOOM_query_destroy(ZOOM_query q);
1683
1684      int ZOOM_query_prefix(ZOOM_query q, const char *str);
1685
1686      int ZOOM_query_cql(ZOOM_query s, const char *str);
1687
1688      int ZOOM_query_sortby(ZOOM_query q, const char *criteria);
1689
1690      int ZOOM_query_sortby2(ZOOM_query q, const char *strategy,
1691                             const char *criteria);
1692    </synopsis>
1693    <para>
1694     Create query objects using <function>ZOOM_query_create</function>
1695     and destroy them by calling <function>ZOOM_query_destroy</function>.
1696     RPN-queries can be specified in <link linkend="PQF">PQF</link>
1697     notation by using the
1698     function <function>ZOOM_query_prefix</function>.
1699     The <function>ZOOM_query_cql</function> specifies a CQL
1700     query to be sent to the server/target.
1701     More query types will be added in future versions of &yaz;, such as
1702     <link linkend="CCL">CCL</link> to RPN-mapping, native CCL query,
1703     etc. In addition to a search, a sort criteria may be set. Function
1704     <function>ZOOM_query_sortby</function> enables Z39.50 sorting and
1705     it takes sort criteria using the same string notation as
1706     yaz-client's <link linkend="sortspec">sort command</link>.
1707    </para>
1708    <para id="zoom.query.sortby2">
1709     <function>ZOOM_query_sortby2</function> is similar to
1710     <function>ZOOM_query_sortby</function> but allows a strategy for
1711     sorting. The reason for the strategy parameter is that some
1712     protocols offers multiple ways of performing sorting.
1713     For example, Z39.50 has the standard sort, which is performed after
1714     search on an existing result set.
1715     It's also possible to use CQL in Z39.50 as the query type and use
1716     CQL's SORTBY keyword. Finally, Index Data's
1717     Zebra server also allows sorting to be specified as part of RPN (Type 7).
1718    </para>
1719    <table id="zoom-sort-strategy" frame="top">
1720     <title>ZOOM sort strategy</title>
1721     <tgroup cols="2">
1722      <colspec colwidth="2*" colname="name"/>
1723      <colspec colwidth="5*" colname="description"/>
1724      <thead>
1725       <row>
1726        <entry>Name</entry>
1727        <entry>Description</entry>
1728       </row>
1729      </thead>
1730      <tbody>
1731       <row>
1732        <entry>z39.50</entry><entry>Z39.50 resultset sort</entry>
1733       </row>
1734       <row>
1735        <entry>type7</entry><entry>Sorting embedded in RPN(Type-7)</entry>
1736       </row>
1737       <row>
1738        <entry>cql</entry><entry>CQL SORTBY</entry>
1739       </row>
1740       <row>
1741        <entry>sru11</entry><entry>SRU sortKeys parameter</entry>
1742       </row>
1743       <row>
1744        <entry>solr</entry><entry>Solr sort</entry>
1745       </row>
1746       <row>
1747        <entry>embed</entry><entry>type7 for Z39.50, cql for SRU,
1748         solr for Solr protocol</entry>
1749       </row>
1750      </tbody>
1751     </tgroup>
1752    </table>
1753   </sect1>
1754   <sect1 id="zoom.resultsets"><title>Result sets</title>
1755    <para>
1756     The result set object is a container for records returned from
1757     a target.
1758    </para>
1759    <synopsis>
1760      ZOOM_resultset ZOOM_connection_search(ZOOM_connection, ZOOM_query q);
1761
1762      ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c,
1763                                                const char *q);
1764      void ZOOM_resultset_destroy(ZOOM_resultset r);
1765    </synopsis>
1766    <para>
1767     Function <function>ZOOM_connection_search</function> creates
1768     a result set given a connection and query.
1769     Destroy a result set by calling
1770     <function>ZOOM_resultset_destroy</function>.
1771     Simple clients may using PQF only may use function
1772     <function>ZOOM_connection_search_pqf</function> in which case
1773     creating query objects is not necessary.
1774    </para>
1775    <synopsis>
1776      void ZOOM_resultset_option_set(ZOOM_resultset r,
1777                                     const char *key, const char *val);
1778
1779      const char *ZOOM_resultset_option_get(ZOOM_resultset r, const char *key);
1780
1781      size_t ZOOM_resultset_size(ZOOM_resultset r);
1782    </synopsis>
1783    <para>
1784     Functions <function>ZOOM_resultset_options_set</function> and
1785     <function>ZOOM_resultset_get</function> sets and gets an option
1786     for a result set similar to <function>ZOOM_connection_option_get</function>
1787     and <function>ZOOM_connection_option_set</function>.
1788    </para>
1789    <para>
1790     The number of hits also called result-count is returned by
1791     function <function>ZOOM_resultset_size</function>.
1792    </para>
1793    <table id="zoom.resultset.options"
1794     frame="top"><title>ZOOM Result set Options</title>
1795     <tgroup cols="3">
1796      <colspec colwidth="4*" colname="name"></colspec>
1797      <colspec colwidth="7*" colname="description"></colspec>
1798      <colspec colwidth="2*" colname="default"></colspec>
1799      <thead>
1800       <row>
1801        <entry>Option</entry>
1802        <entry>Description</entry>
1803        <entry>Default</entry>
1804       </row>
1805      </thead>
1806      <tbody>
1807       <row><entry>
1808         start</entry><entry>Offset of first record to be
1809         retrieved from target. First record has offset 0 unlike the
1810         protocol specifications where first record has position 1.
1811         This option affects ZOOM_resultset_search and
1812         ZOOM_resultset_search_pqf and must be set before any of
1813         these functions are invoked. If a range of
1814         records must be fetched manually after search,
1815         function ZOOM_resultset_records should be used.
1816        </entry><entry>0</entry></row>
1817       <row><entry>
1818         count</entry><entry>Number of records to be retrieved.
1819         This option affects ZOOM_resultset_search and
1820         ZOOM_resultset_search_pqf and must be set before any of
1821         these functions are invoked.
1822        </entry><entry>0</entry></row>
1823       <row><entry>
1824         presentChunk</entry><entry>The number of records to be
1825         requested from the server in each chunk (present request). The
1826         value 0 means to request all the records in a single chunk.
1827         (The old <literal>step</literal>
1828         option is also supported for the benefit of old applications.)
1829        </entry><entry>0</entry></row>
1830       <row><entry>
1831         elementSetName</entry><entry>Element-Set name of records.
1832         Most targets should honor element set name <literal>B</literal>
1833         and <literal>F</literal> for brief and full respectively.
1834        </entry><entry>none</entry></row>
1835       <row><entry>
1836         preferredRecordSyntax</entry><entry>Preferred Syntax, such as
1837         <literal>USMARC</literal>, <literal>SUTRS</literal>, etc.
1838        </entry><entry>none</entry></row>
1839       <row><entry>
1840         schema</entry><entry>Schema for retrieval, such as
1841         <literal>Gils-schema</literal>, <literal>Geo-schema</literal>, etc.
1842        </entry><entry>none</entry></row>
1843       <row><entry>
1844         setname</entry><entry>Name of Result Set (Result Set ID).
1845         If this option isn't set, the ZOOM module will automatically
1846         allocate a result set name.
1847        </entry><entry>default</entry></row>
1848       <row><entry>
1849         rpnCharset</entry><entry>Character set for RPN terms.
1850         If this is set, ZOOM C will assume that the ZOOM application is
1851         running UTF-8. Terms in RPN queries are then converted to the
1852         rpnCharset. If this is unset, ZOOM C will not assume any encoding
1853         of RPN terms and no conversion is performed.
1854        </entry><entry>none</entry></row>
1855      </tbody>
1856     </tgroup>
1857    </table>
1858    <para>
1859     For servers that support Search Info report, the following
1860     options may be read using <function>ZOOM_resultset_get</function>.
1861     This detailed information is read after a successful search has
1862     completed.
1863    </para>
1864    <para>
1865     This information is a list of of items, where each item is
1866     information about a term or subquery. All items in the list
1867     are prefixed by
1868     <literal>SearchResult.</literal><replaceable>no</replaceable>
1869     where no presents the item number (0=first, 1=second).
1870     Read <literal>searchresult.size</literal> to determine the
1871     number of items.
1872    </para>
1873    <table id="zoom.search.info.report.options"
1874     frame="top"><title>Search Info Report Options</title>
1875     <tgroup cols="2">
1876      <colspec colwidth="4*" colname="name"></colspec>
1877      <colspec colwidth="7*" colname="description"></colspec>
1878      <thead>
1879       <row>
1880        <entry>Option</entry>
1881        <entry>Description</entry>
1882       </row>
1883      </thead>
1884      <tbody>
1885       <row>
1886        <entry>searchresult.size</entry>
1887        <entry>
1888         number of search result entries. This option is-nonexistant
1889         if no entries are returned by the server.
1890        </entry>
1891       </row>
1892       <row>
1893        <entry>searchresult.<replaceable>no</replaceable>.id</entry>
1894        <entry>sub query ID</entry>
1895       </row>
1896       <row>
1897        <entry>searchresult.<replaceable>no</replaceable>.count</entry>
1898        <entry>result count for item (number of hits)</entry>
1899       </row>
1900       <row>
1901        <entry>searchresult.<replaceable>no</replaceable>.subquery.term</entry>
1902        <entry>subquery term</entry>
1903       </row>
1904       <row>
1905        <entry>
1906         searchresult.<replaceable>no</replaceable>.interpretation.term
1907        </entry>
1908        <entry>interpretation term</entry>
1909       </row>
1910       <row>
1911        <entry>
1912         searchresult.<replaceable>no</replaceable>.recommendation.term
1913        </entry>
1914        <entry>recommendation term</entry>
1915       </row>
1916      </tbody>
1917     </tgroup>
1918    </table>
1919    <sect2 id="zoom.z3950.resultset.sort">
1920     <title>Z39.50 Result-set Sort</title>
1921     <synopsis>
1922      void ZOOM_resultset_sort(ZOOM_resultset r,
1923                               const char *sort_type, const char *sort_spec);
1924
1925      int ZOOM_resultset_sort1(ZOOM_resultset r,
1926                               const char *sort_type, const char *sort_spec);
1927     </synopsis>
1928     <para>
1929      <function>ZOOM_resultset_sort</function> and
1930      <function>ZOOM_resultset_sort1</function> both sort an existing
1931      result-set. The sort_type parameter is not use. Set it to "yaz".
1932      The sort_spec is same notation as ZOOM_query_sortby and identical
1933      to that offered by yaz-client's
1934      <link linkend="sortspec">sort command</link>.
1935     </para>
1936     <para>
1937      These functions only work for Z39.50. Use the more generic utility
1938      <link linkend="zoom.query.sortby2">
1939       <function>ZOOM_query_sortby2</function></link>
1940      for other protocols (and even Z39.50).
1941     </para>
1942    </sect2>
1943    <sect2 id="zoom.z3950.resultset.behavior">
1944     <title>Z39.50 Protocol behavior</title>
1945     <para>
1946      The creation of a result set involves at least a SearchRequest
1947      - SearchResponse protocol handshake. Following that, if a sort
1948      criteria was specified as part of the query, a SortRequest -
1949      SortResponse handshake takes place. Note that it is necessary to
1950      perform sorting before any retrieval takes place, so no records will
1951      be returned from the target as part of the SearchResponse because these
1952      would be unsorted. Hence, piggyback is disabled when sort criteria
1953      are set. Following Search - and a possible sort - Retrieval takes
1954      place - as one or more Present Requests/Response pairs being
1955      transferred.
1956      </para>
1957     <para>
1958      The API allows for two different modes for retrieval. A high level
1959      mode which is somewhat more powerful and a low level one.
1960      The low level is enabled when searching on a Connection object
1961      for which the settings
1962      <literal>smallSetUpperBound</literal>,
1963      <literal>mediumSetPresentNumber</literal> and
1964      <literal>largeSetLowerBound</literal> are set. The low level mode
1965      thus allows you to precisely set how records are returned as part
1966      of a search response as offered by the Z39.50 protocol.
1967      Since the client may be retrieving records as part of the
1968      search response, this mode doesn't work well if sorting is used.
1969      </para>
1970     <para>
1971      The high-level mode allows you to fetch a range of records from
1972      the result set with a given start offset. When you use this mode
1973      the client will automatically use piggyback if that is possible
1974      with the target and perform one or more present requests as needed.
1975      Even if the target returns fewer records as part of a present response
1976      because of a record size limit, etc. the client will repeat sending
1977      present requests. As an example, if option <literal>start</literal>
1978      is 0 (default) and <literal>count</literal> is 4, and
1979      <literal>piggyback</literal> is 1 (default) and no sorting criteria
1980      is specified, then the client will attempt to retrieve the 4
1981      records as part the search response (using piggyback). On the other
1982      hand, if either <literal>start</literal> is positive or if
1983      a sorting criteria is set, or if <literal>piggyback</literal>
1984      is 0, then the client will not perform piggyback but send Present
1985      Requests instead.
1986     </para>
1987     <para>
1988      If either of the options <literal>mediumSetElementSetName</literal> and
1989      <literal>smallSetElementSetName</literal> are unset, the value
1990      of option <literal>elementSetName</literal> is used for piggyback
1991      searches. This means that for the high-level mode you only have
1992      to specify one elementSetName option rather than three.
1993      </para>
1994    </sect2>
1995    <sect2 id="zoom.sru.resultset.behavior">
1996     <title>SRU Protocol behavior</title>
1997     <para>
1998      Current version of &yaz; does not take advantage of a result set id
1999      returned by the SRU server. Future versions might do, however.
2000      Since, the ZOOM driver does not save result set IDs any
2001      present (retrieval) is transformed to a SRU SearchRetrieveRequest
2002      with same query but, possibly, different offsets.
2003     </para>
2004     <para>
2005      Option <literal>schema</literal> specifies SRU schema
2006      for retrieval. However, options <literal>elementSetName</literal> and
2007      <literal>preferredRecordSyntax</literal> are ignored.
2008     </para>
2009     <para>
2010      Options <literal>start</literal> and <literal>count</literal>
2011      are supported by SRU.
2012      The remaining options
2013      <literal>piggyback</literal>,
2014      <literal>smallSetUpperBound</literal>,
2015      <literal>largeSetLowerBound</literal>,
2016      <literal>mediumSetPresentNumber</literal>,
2017      <literal>mediumSetElementSetName</literal>,
2018       <literal>smallSetElementSetName</literal> are
2019      unsupported.
2020     </para>
2021     <para>
2022      SRU supports CQL queries, <emphasis>not</emphasis> PQF.
2023      If PQF is used, however, the PQF query is transferred anyway
2024      using non-standard element <literal>pQuery</literal> in
2025      SRU SearchRetrieveRequest.
2026     </para>
2027     <para>
2028      Solr queries has to be done in Solr query format.
2029     </para>
2030     <para>
2031      Unfortunately, SRU or Solr does not define a database setting. Hence,
2032      <literal>databaseName</literal> is unsupported and ignored.
2033      However, the path part in host parameter for functions
2034      <function>ZOOM_connecton_new</function> and
2035      <function>ZOOM_connection_connect</function> acts as a
2036      database (at least for the &yaz; SRU server).
2037     </para>
2038    </sect2>
2039   </sect1>
2040   <sect1 id="zoom.records">
2041    <title>Records</title>
2042    <para>
2043     A record object is a retrieval record on the client side -
2044     created from result sets.
2045    </para>
2046    <synopsis>
2047      void ZOOM_resultset_records(ZOOM_resultset r,
2048                                  ZOOM_record *recs,
2049                                  size_t start, size_t count);
2050      ZOOM_record ZOOM_resultset_record(ZOOM_resultset s, size_t pos);
2051
2052      const char *ZOOM_record_get(ZOOM_record rec, const char *type,
2053                                  size_t *len);
2054
2055      int ZOOM_record_error(ZOOM_record rec, const char **msg,
2056                            const char **addinfo, const char **diagset);
2057
2058      ZOOM_record ZOOM_record_clone(ZOOM_record rec);
2059
2060      void ZOOM_record_destroy(ZOOM_record rec);
2061    </synopsis>
2062    <para>
2063     References to temporary records are returned by functions
2064     <function>ZOOM_resultset_records</function> or
2065     <function>ZOOM_resultset_record</function>.
2066     </para>
2067    <para>
2068     If a persistent reference to a record is desired
2069     <function>ZOOM_record_clone</function> should be used.
2070     It returns a record reference that should be destroyed
2071     by a call to <function>ZOOM_record_destroy</function>.
2072    </para>
2073    <para>
2074     A single record is returned by function
2075     <function>ZOOM_resultset_record</function> that takes a
2076     position as argument. First record has position zero.
2077     If no record could be obtained <literal>NULL</literal> is returned.
2078    </para>
2079    <para>
2080     Error information for a record can be checked with
2081     <function>ZOOM_record_error</function> which returns non-zero
2082     (error code) if record is in error, called <emphasis>Surrogate
2083      Diagnostics</emphasis> in Z39.50.
2084    </para>
2085    <para>
2086     Function <function>ZOOM_resultset_records</function> retrieves
2087     a number of records from a result set. Parameter <literal>start</literal>
2088     and <literal>count</literal> specifies the range of records to
2089     be returned. Upon completion array
2090     <literal>recs[0], ..recs[count-1]</literal>
2091     holds record objects for the records. The array of records
2092      <literal>recs</literal> should be allocated prior the call
2093     <function>ZOOM_resultset_records</function>. Note that for those
2094     records that couldn't be retrieved from the target
2095     <literal>recs[ ..]</literal> is set to <literal>NULL</literal>.
2096    </para>
2097    <para id="zoom.record.get">
2098     In order to extract information about a single record,
2099     <function>ZOOM_record_get</function> is provided. The
2100     function returns a pointer to certain record information. The
2101     nature (type) of the pointer depends on the parameter,
2102     <parameter>type</parameter>.
2103    </para>
2104    <para>
2105     The <parameter>type</parameter> is a string of the format:
2106    </para>
2107    <para>
2108     <replaceable>format</replaceable>[;charset=<replaceable>from</replaceable>[/<replaceable>opacfrom</replaceable>][,<replaceable>to</replaceable>]][;format=<replaceable>v</replaceable>]
2109    </para>
2110    <para>
2111     where <replaceable>format</replaceable> specifies the format of the
2112     returned record, <replaceable>from</replaceable>
2113     specifies the character set of the record in its original form
2114     (as returned by the server), <replaceable>to</replaceable> specifies
2115     the output (returned)
2116     character set encoding.
2117     If <replaceable>to</replaceable> is omitted UTF-8 is assumed.
2118     If charset is not given, then no character set conversion takes place.
2119    </para>
2120
2121    <para>OPAC records may be returned in a different
2122      set from the bibliographic MARC record. If this is this the case,
2123     <replaceable>opacfrom</replaceable> should be set to the character set
2124     of the OPAC record part.
2125    </para>
2126    <note>
2127      <para>
2128        Specifying the OPAC record character set requires YAZ 4.1.5 or later.
2129      </para>
2130    </note>
2131    <para>
2132     The format argument controls whether record data should be XML
2133     pretty-printed (post process operation).
2134     It is enabled only if format value <replaceable>v</replaceable> is
2135     <literal>1</literal> and the record content is XML well-formed.
2136    </para>
2137    <para>
2138     In addition, for certain types, the length
2139     <literal>len</literal> passed will be set to the size in bytes of
2140     the returned information.
2141     </para>
2142    <para>
2143     The following are the supported values for <replaceable>form</replaceable>.
2144     <variablelist>
2145      <varlistentry><term><literal>database</literal></term>
2146       <listitem><para>Database of record is returned
2147         as a C null-terminated string. Return type
2148         <literal>const char *</literal>.
2149        </para></listitem>
2150      </varlistentry>
2151      <varlistentry><term><literal>syntax</literal></term>
2152       <listitem><para>The transfer syntax of the record is returned
2153         as a C null-terminated string containing the symbolic name of
2154         the record syntax, e.g. <literal>Usmarc</literal>. Return type
2155         is
2156         <literal>const char *</literal>.
2157        </para></listitem>
2158      </varlistentry>
2159      <varlistentry><term><literal>schema</literal></term>
2160       <listitem><para>The schema of the record is returned
2161         as a C null-terminated string. Return type is
2162         <literal>const char *</literal>.
2163        </para></listitem>
2164      </varlistentry>
2165      <varlistentry><term><literal>render</literal></term>
2166       <listitem><para>The record is returned in a display friendly
2167         format. Upon completion buffer is returned
2168         (type <literal>const char *</literal>) and length is stored in
2169         <literal>*len</literal>.
2170        </para></listitem>
2171      </varlistentry>
2172      <varlistentry><term><literal>raw</literal></term>
2173       <listitem><para>The record is returned in the internal
2174         YAZ specific format. For GRS-1, Explain, and others, the
2175         raw data is returned as type
2176         <literal>Z_External *</literal> which is just the type for
2177         the member <literal>retrievalRecord</literal> in
2178         type <literal>NamePlusRecord</literal>.
2179         For SUTRS and octet aligned record (including all MARCs) the
2180         octet buffer is returned and the length of the buffer.
2181        </para></listitem>
2182      </varlistentry>
2183      <varlistentry><term><literal>xml</literal></term>
2184       <listitem><para>The record is returned in XML if possible.
2185         SRU, Solr and Z39.50 records with transfer syntax XML are
2186         returned verbatim. MARC records are returned in
2187         <ulink url="&url.marcxml;">
2188          MARCXML
2189          </ulink>
2190         (converted from ISO2709 to MARCXML by YAZ).
2191         OPAC records are also converted to XML and the
2192         bibliographic record is converted to MARCXML (when possible).
2193         GRS-1 records are not supported for this form.
2194         Upon completion, the XML buffer is returned
2195         (type <literal>const char *</literal>) and length is stored in
2196         <literal>*len</literal>.
2197        </para></listitem>
2198      </varlistentry>
2199      <varlistentry><term><literal>opac</literal></term>
2200       <listitem><para>OPAC information for record is returned in XML
2201         if an OPAC record is present at the position given. If no
2202         OPAC record is present, a NULL pointer is returned.
2203        </para></listitem>
2204      </varlistentry>
2205      <varlistentry><term><literal>txml</literal></term>
2206       <listitem><para>The record is returned in TurboMARC if possible.
2207         SRU and Z39.50 records with transfer syntax XML are
2208         returned verbatim. MARC records are returned in
2209         <link linkend="tools.turbomarc">
2210          TurboMARC
2211         </link>
2212         (converted from ISO2709 to TurboMARC by YAZ).
2213         Upon completion, the XML buffer is returned
2214         (type <literal>const char *</literal>) and length is stored in
2215         <literal>*len</literal>.
2216        </para></listitem>
2217      </varlistentry>
2218      <varlistentry><term><literal>json</literal></term>
2219       <listitem><para>Like xml, but MARC records are converted to
2220         <ulink url="&url.marc_in_json;">MARC-in-JSON</ulink>.
2221        </para></listitem>
2222      </varlistentry>
2223
2224     </variablelist>
2225    </para>
2226    <para>
2227     Most
2228     <ulink url="&url.marc21;">MARC21</ulink>
2229     records uses the
2230     <ulink url="&url.marc8;">MARC-8</ulink>
2231     character set encoding.
2232     An application that wishes to display in Latin-1 would use
2233     <screen>
2234      render; charset=marc8,iso-8859-1
2235     </screen>
2236    </para>
2237    <sect2 id="zoom.z3950.record.behavior">
2238     <title>Z39.50 Protocol behavior</title>
2239     <para>
2240      The functions <function>ZOOM_resultset_record</function> and
2241      <function>ZOOM_resultset_records</function> inspects the client-side
2242      record cache. Records not found in cache are fetched using
2243      Present.
2244      The functions may block (and perform network I/O)  - even though option
2245      <literal>async</literal> is 1, because they return records objects.
2246      (and there's no way to return records objects without retrieving them!).
2247      </para>
2248     <para>
2249      There is a trick, however, in the usage of function
2250      <function>ZOOM_resultset_records</function> that allows for
2251      delayed retrieval (and makes it non-blocking). By using
2252      a null pointer for <parameter>recs</parameter> you're indicating
2253      you're not interested in getting records objects
2254      <emphasis>now</emphasis>.
2255     </para>
2256    </sect2>
2257    <sect2 id="zoom.sru.record.behavior">
2258     <title>SRU/Solr Protocol behavior</title>
2259     <para>
2260      The ZOOM driver for SRU/Solr treats records returned by a SRU/Solr server
2261      as if they where Z39.50 records with transfer syntax XML and
2262      no element set name or database name.
2263     </para>
2264    </sect2>
2265   </sect1>
2266   <sect1 id="zoom.facets"><title>Facets</title>
2267    <para>
2268     Facet operations is not part of the official ZOOM specification, but
2269     is an Index Data extension for YAZ-based Z39.50 targets,
2270     <ulink url="&url.solr;">Solr</ulink> and SRU 2.0 targets.
2271
2272     Facets may be requestd by the
2273      <link linkend="zoom.facets.option">facets</link> option before a
2274     search is sent.
2275     For inspection of the returned facets, the following functions are
2276     available:
2277    </para>
2278    <synopsis>
2279     ZOOM_facet_field *ZOOM_resultset_facets(ZOOM_resultset r);
2280
2281     ZOOM_facet_field ZOOM_resultset_get_facet_field(ZOOM_resultset r,
2282                                                     const char *facet_name);
2283
2284     ZOOM_facet_field ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r,
2285                                                              int pos);
2286
2287     size_t ZOOM_resultset_facets_size(ZOOM_resultset r);
2288
2289     const char *ZOOM_facet_field_name(ZOOM_facet_field facet_field);
2290
2291     size_t ZOOM_facet_field_term_count(ZOOM_facet_field facet_field);
2292
2293     const char *ZOOM_facet_field_get_term(ZOOM_facet_field facet_field,
2294                                           size_t idx, int *freq);
2295    </synopsis>
2296    <para>
2297     References to temporary structures are returned by all functions.
2298     They are only valid as long the Result set is valid.
2299     <function>ZOOM_resultset_get_facet_field</function> or
2300     <function>ZOOM_resultset_get_facet_field_by_index</function>.
2301     <function>ZOOM_resultset_facets</function>.
2302     <function>ZOOM_facet_field_name</function>.
2303     <function>ZOOM_facet_field_get_term</function>.
2304     </para>
2305    <para id="zoom.resultset.get_facet_field">
2306     A single Facet field  is returned by function
2307     <function>ZOOM_resultset_get_facet_field</function> or
2308     <function>ZOOM_resultset_get_facet_field_by_index</function> that takes
2309     a  result set and facet name or positive index respectively. First
2310     facet has position zero. If no facet could be obtained (invalid name
2311     or index out of bounds) <literal>NULL</literal> is returned.
2312    </para>
2313    <para id="zoom.resultset.facets">
2314     An array of facets field can be returned by
2315     <function>ZOOM_resultset_facets</function>. The length of the array is
2316     given by <function>ZOOM_resultset_facets_size</function>. The array is
2317     zero-based and last entry will be at
2318     <function>ZOOM_resultset_facets_size(result_set)</function>-1.
2319    </para>
2320    <para id="zoom.resultset.facets_names">
2321     It is possible to interate over facets by name, by calling
2322     <function>ZOOM_resultset_facets_names</function>.
2323     This will return an const array of char * where each string can be used
2324     as parameter for <function>ZOOM_resultset_get_facet_field</function>.
2325    </para>
2326    <para>
2327    Function <function>ZOOM_facet_field_name</function> gets the request
2328     facet name from a returned facet field.
2329    </para>
2330    <para>
2331     Function <function>ZOOM_facet_field_get_term</function> returns the
2332     idx'th term and term count for a facet field.
2333     Idx must between 0 and
2334     <function>ZOOM_facet_field_term_count</function>-1, otherwise the
2335     returned reference will be <literal>NULL</literal>. On a valid idx, the
2336     value of the freq reference will be the term count.
2337     The <literal>freq</literal> parameter must be valid pointer to integer.
2338    </para>
2339    </sect1>
2340   <sect1 id="zoom.scan"><title>Scan</title>
2341    <para>
2342     This section describes an interface for Scan. Scan is not an
2343     official part of the ZOOM model yet. The result of a scan operation
2344     is the <literal>ZOOM_scanset</literal> which is a set of terms
2345     returned by a target.
2346    </para>
2347
2348    <para>
2349     The Scan interface is supported for both Z39.50, SRU and Solr.
2350    </para>
2351
2352    <synopsis>
2353     ZOOM_scanset ZOOM_connection_scan(ZOOM_connection c,
2354                                       const char *startpqf);
2355
2356     ZOOM_scanset ZOOM_connection_scan1(ZOOM_connection c,
2357                                        ZOOM_query q);
2358
2359     size_t ZOOM_scanset_size(ZOOM_scanset scan);
2360
2361     const char *ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
2362                                   size_t *occ, size_t *len);
2363
2364     const char *ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
2365                                           size_t *occ, size_t *len);
2366
2367     void ZOOM_scanset_destroy(ZOOM_scanset scan);
2368
2369     const char *ZOOM_scanset_option_get(ZOOM_scanset scan,
2370                                         const char *key);
2371
2372     void ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
2373                                  const char *val);
2374     </synopsis>
2375    <para>
2376     The scan set is created by function
2377     <function>ZOOM_connection_scan</function> which performs a scan
2378     operation on the connection using the specified
2379     <parameter>startpqf</parameter>.
2380     If the operation was successful, the size of the scan set can be
2381     retrieved by a call to <function>ZOOM_scanset_size</function>.
2382     Like result sets, the items are numbered 0,..size-1.
2383     To obtain information about a particular scan term, call function
2384     <function>ZOOM_scanset_term</function>. This function takes
2385     a scan set offset <literal>pos</literal> and returns a pointer
2386     to a <emphasis>raw term</emphasis> or <literal>NULL</literal> if
2387     non-present.
2388     If present, the <literal>occ</literal> and <literal>len</literal>
2389     are set to the number of occurrences and the length
2390     of the actual term respectively.
2391     <function>ZOOM_scanset_display_term</function> is similar to
2392     <function>ZOOM_scanset_term</function> except that it returns
2393     the <emphasis>display term</emphasis> rather than the raw term.
2394     In a few cases, the term is different from display term. Always
2395     use the display term for display and the raw term for subsequent
2396     scan operations (to get more terms, next scan result, etc).
2397    </para>
2398    <para>
2399     A scan set may be freed by a call to function
2400     <function>ZOOM_scanset_destroy</function>.
2401     Functions <function>ZOOM_scanset_option_get</function> and
2402     <function>ZOOM_scanset_option_set</function> retrieves and sets
2403     an option respectively.
2404    </para>
2405    <para>
2406     The <parameter>startpqf</parameter> is a subset of PQF, namely
2407     the Attributes+Term part. Multiple <literal>@attr</literal> can
2408     be used. For example to scan in title (complete) phrases:
2409     <literallayout>
2410      @attr 1=4 @attr 6=2 "science o"
2411     </literallayout>
2412    </para>
2413    <para>
2414     The <function>ZOOM_connecton_scan1</function> is a newer and
2415     more generic alternative to <function>ZOOM_connection_scan</function>
2416     which allows to use both CQL and PQF for Scan.
2417    </para>
2418    <table frame="top" id="zoom.scanset.options">
2419     <title>ZOOM Scan Set Options</title>
2420     <tgroup cols="3">
2421      <colspec colwidth="4*" colname="name"></colspec>
2422      <colspec colwidth="7*" colname="description"></colspec>
2423      <colspec colwidth="2*" colname="default"></colspec>
2424      <thead>
2425       <row>
2426        <entry>Option</entry>
2427        <entry>Description</entry>
2428        <entry>Default</entry>
2429       </row>
2430      </thead>
2431      <tbody>
2432       <row><entry>
2433         number</entry><entry>Number of Scan Terms requested in next scan.
2434         After scan it holds the actual number of terms returned.
2435        </entry><entry>20</entry></row>
2436       <row><entry>
2437         position</entry><entry>Preferred Position of term in response
2438         in next scan; actual position after completion of scan.
2439        </entry><entry>1</entry></row>
2440       <row><entry>
2441         stepSize</entry><entry>Step Size
2442        </entry><entry>0</entry></row>
2443       <row><entry>
2444         scanStatus</entry><entry>An integer indicating the Scan Status
2445         of last scan.
2446        </entry><entry>0</entry></row>
2447       <row><entry>
2448         rpnCharset</entry><entry>Character set for RPN terms.
2449         If this is set, ZOOM C will assume that the ZOOM application is
2450         running UTF-8. Terms in RPN queries are then converted to the
2451         rpnCharset. If this is unset, ZOOM C will not assume any encoding
2452         of RPN terms and no conversion is performed.
2453        </entry><entry>none</entry></row>
2454      </tbody>
2455     </tgroup>
2456    </table>
2457   </sect1>
2458   <sect1 id="zoom.extendedservices">
2459    <title>Extended Services</title>
2460    <para>
2461     ZOOM offers an interface to a subset of the Z39.50 extended services
2462     as well as a few privately defined ones:
2463    </para>
2464    <itemizedlist>
2465     <listitem>
2466      <para>
2467       Z39.50 Item Order (ILL).
2468       See <xref linkend="zoom.item.order"/>.
2469      </para>
2470     </listitem>
2471     <listitem>
2472      <para>
2473       Record Update. This allows a client to insert, modify or delete
2474       records.
2475       See <xref linkend="zoom.record.update"/>.
2476      </para>
2477     </listitem>
2478     <listitem>
2479      <para>
2480       Database Create. This a non-standard feature. Allows a client
2481       to create a database.
2482       See <xref linkend="zoom.database.create"/>.
2483      </para>
2484     </listitem>
2485     <listitem>
2486      <para>
2487       Database Drop. This a non-standard feature. Allows a client
2488       to delete/drop a database.
2489       See <xref linkend="zoom.database.drop"/>.
2490      </para>
2491      </listitem>
2492     <listitem>
2493      <para>
2494       Commit operation. This a non-standard feature. Allows a client
2495       to commit operations.
2496       See <xref linkend="zoom.commit"/>.
2497      </para>
2498     </listitem>
2499     <!-- all the ILL PDU options should go here too -->
2500    </itemizedlist>
2501    <para>
2502     To create an extended service operation a <literal>ZOOM_package</literal>
2503     must be created. The operation is a five step operation. The
2504     package is created, package is configured by means of options,
2505     the package is send, result is inspected (by means of options),
2506     the package is destroyed.
2507    </para>
2508    <synopsis>
2509     ZOOM_package ZOOM_connection_package(ZOOM_connection c,
2510                                          ZOOM_options options);
2511
2512     const char *ZOOM_package_option_get(ZOOM_package p,
2513                                         const char *key);
2514     void ZOOM_package_option_set(ZOOM_package p, const char *key,
2515                                  const char *val);
2516     void ZOOM_package_send(ZOOM_package p, const char *type);
2517
2518     void ZOOM_package_destroy(ZOOM_package p);
2519    </synopsis>
2520    <para>
2521     The <function>ZOOM_connection_package</function> creates a
2522     package for the connection given using the options specified.
2523    </para>
2524    <para>
2525     Functions <function>ZOOM_package_option_get</function> and
2526     <function>ZOOM_package_option_set</function> gets and sets
2527     options.
2528    </para>
2529    <para>
2530     <function>ZOOM_package_send</function> sends
2531     the package the via connection specified in
2532     <function>ZOOM_connection_package</function>.
2533     The <parameter>type</parameter> specifies the actual extended service
2534     package type to be sent.
2535    </para>
2536    <table frame="top" id="zoom.extendedservices.options">
2537     <title>Extended Service Common Options</title>
2538     <tgroup cols="3">
2539      <colspec colwidth="4*" colname="name"></colspec>
2540      <colspec colwidth="7*" colname="description"></colspec>
2541      <colspec colwidth="3*" colname="default"></colspec>
2542      <thead>
2543       <row>
2544        <entry>Option</entry>
2545        <entry>Description</entry>
2546        <entry>Default</entry>
2547       </row>
2548      </thead>
2549      <tbody>
2550       <row>
2551        <entry>package-name</entry>
2552        <entry>Extended Service Request package name. Must be specified
2553        as part of a request</entry>
2554        <entry>none</entry>
2555       </row>
2556       <row>
2557        <entry>user-id</entry>
2558        <entry>User ID of Extended Service Package. Is a request option</entry>
2559        <entry>none</entry>
2560       </row>
2561       <row>
2562        <entry>function</entry>
2563        <entry>
2564         Function of package - one of <literal>create</literal>,
2565         <literal>delete</literal>, <literal>modify</literal>. Is
2566         a request option.
2567        </entry>
2568        <entry><literal>create</literal></entry>
2569       </row>
2570       <row>
2571        <entry>waitAction</entry>
2572        <entry>
2573         Wait action for package. Possible values:
2574         <literal>wait</literal>, <literal>waitIfPossible</literal>,
2575         <literal>dontWait</literal> or <literal>dontReturnPackage</literal>.
2576        </entry>
2577        <entry><literal>waitIfPossible</literal></entry>
2578       </row>
2579       <row>
2580        <entry>targetReference</entry>
2581        <entry>
2582         Target Reference. This is part of the response as returned
2583         by the server. Read it after a successful operation.
2584        </entry>
2585        <entry><literal>none</literal></entry>
2586       </row>
2587      </tbody>
2588     </tgroup>
2589    </table>
2590    <sect2 id="zoom.item.order">
2591     <title>Item Order</title>
2592     <para>
2593      For Item Order, type must be set to <literal>itemorder</literal> in
2594      <function>ZOOM_package_send</function>.
2595     </para>
2596
2597     <table frame="top" id="zoom.item.order.options">
2598      <title>Item Order Options</title>
2599      <tgroup cols="3">
2600       <colspec colwidth="4*" colname="name"></colspec>
2601       <colspec colwidth="7*" colname="description"></colspec>
2602       <colspec colwidth="3*" colname="default"></colspec>
2603       <thead>
2604        <row>
2605         <entry>Option</entry>
2606         <entry>Description</entry>
2607         <entry>Default</entry>
2608        </row>
2609       </thead>
2610       <tbody>
2611        <row>
2612         <entry>contact-name</entry>
2613         <entry>ILL contact name</entry>
2614         <entry>none</entry>
2615        </row>
2616        <row>
2617         <entry>contact-phone</entry>
2618         <entry>ILL contact phone</entry>
2619         <entry>none</entry>
2620        </row>
2621        <row>
2622         <entry>contact-email</entry>
2623         <entry>ILL contact email</entry>
2624         <entry>none</entry>
2625        </row>
2626        <row>
2627         <entry>itemorder-item</entry>
2628         <entry>Position for item (record) requested. An integer</entry>
2629         <entry>1</entry>
2630        </row>
2631       </tbody>
2632      </tgroup>
2633     </table>
2634    </sect2>
2635    <sect2 id="zoom.record.update">
2636     <title>Record Update</title>
2637     <para>
2638      For Record Update, type must be set to <literal>update</literal> in
2639      <function>ZOOM_package_send</function>.
2640     </para>
2641     <table frame="top" id="zoom.record.update.options">
2642      <title>Record Update Options</title>
2643      <tgroup cols="3">
2644       <colspec colwidth="4*" colname="name"></colspec>
2645       <colspec colwidth="7*" colname="description"></colspec>
2646       <colspec colwidth="3*" colname="default"></colspec>
2647       <thead>
2648        <row>
2649         <entry>Option</entry>
2650         <entry>Description</entry>
2651         <entry>Default</entry>
2652        </row>
2653       </thead>
2654       <tbody>
2655        <row>
2656         <entry>action</entry>
2657         <entry>
2658          The update action. One of
2659          <literal>specialUpdate</literal>,
2660          <literal>recordInsert</literal>,
2661          <literal>recordReplace</literal>,
2662          <literal>recordDelete</literal>,
2663          <literal>elementUpdate</literal>.
2664         </entry>
2665         <entry><literal>specialUpdate (recordInsert for updateVersion=1 which does not support specialUpdate)</literal></entry>
2666        </row>
2667        <row>
2668         <entry>recordIdOpaque</entry>
2669         <entry>Opaque Record ID</entry>
2670         <entry>none</entry>
2671        </row>
2672        <row>
2673         <entry>recordIdNumber</entry>
2674         <entry>Record ID number</entry>
2675         <entry>none</entry>
2676        </row>
2677        <row>
2678         <entry>record</entry>
2679         <entry>The record itself</entry>
2680         <entry>none</entry>
2681        </row>
2682        <row>
2683         <entry>recordOpaque</entry>
2684         <entry>Specifies an opaque record which is
2685           encoded as an ASN.1 ANY type with the OID as tiven by option
2686           <literal>syntax</literal> (see below).
2687           Option <literal>recordOpaque</literal> is an alternative
2688           to record - and <literal>record</literal> option (above) is
2689           ignored if recordOpaque is set. This option is only available in
2690           YAZ 3.0.35 and later and is meant to facilitate Updates with
2691           servers from OCLC.
2692         </entry>
2693         <entry>none</entry>
2694        </row>
2695        <row>
2696         <entry>syntax</entry>
2697         <entry>The record syntax (transfer syntax). Is a string that
2698          is a known record syntax.
2699         </entry>
2700         <entry>no syntax</entry>
2701        </row>
2702        <row>
2703         <entry>databaseName</entry>
2704         <entry>Database from connection object</entry>
2705         <entry>Default</entry>
2706        </row>
2707        <row>
2708         <entry>correlationInfo.note</entry>
2709         <entry>Correlation Info Note (string)</entry>
2710         <entry>none</entry>
2711        </row>
2712        <row>
2713         <entry>correlationInfo.id</entry>
2714         <entry>Correlation Info ID (integer)</entry>
2715         <entry>none</entry>
2716        </row>
2717        <row>
2718         <entry>elementSetName</entry>
2719         <entry>Element Set for Record</entry>
2720         <entry>none</entry>
2721        </row>
2722        <row>
2723         <entry>updateVersion</entry>
2724         <entry>Record Update version which holds one of the values
2725          1, 2 or 3. Each version has a distinct OID:
2726          1.2.840.10003.9.5
2727          (<ulink url="&url.z39.50.extupdate1;">first version</ulink>) ,
2728          1.2.840.10003.9.5.1
2729          (second version) and
2730          1.2.840.10003.9.5.1.1
2731          (<ulink url="&url.z39.50.extupdate3;">third and
2732           newest version</ulink>).
2733         </entry>
2734         <entry>3</entry>
2735        </row>
2736       </tbody>
2737      </tgroup>
2738     </table>
2739
2740    </sect2>
2741
2742    <sect2 id="zoom.database.create"><title>Database Create</title>
2743     <para>
2744      For Database Create, type must be set to <literal>create</literal> in
2745      <function>ZOOM_package_send</function>.
2746     </para>
2747
2748     <table frame="top" id="zoom.database.create.options">
2749      <title>Database Create Options</title>
2750      <tgroup cols="3">
2751       <colspec colwidth="4*" colname="name"></colspec>
2752       <colspec colwidth="7*" colname="description"></colspec>
2753       <colspec colwidth="3*" colname="default"></colspec>
2754       <thead>
2755        <row>
2756         <entry>Option</entry>
2757         <entry>Description</entry>
2758         <entry>Default</entry>
2759        </row>
2760       </thead>
2761       <tbody>
2762        <row>
2763         <entry>databaseName</entry>
2764         <entry>Database from connection object</entry>
2765         <entry>Default</entry>
2766        </row>
2767       </tbody>
2768      </tgroup>
2769     </table>
2770    </sect2>
2771    <sect2 id="zoom.database.drop">
2772     <title>Database Drop</title>
2773     <para>
2774      For Database Drop, type must be set to <literal>drop</literal> in
2775      <function>ZOOM_package_send</function>.
2776     </para>
2777     <table frame="top" id="zoom.database.drop.options">
2778      <title>Database Drop Options</title>
2779      <tgroup cols="3">
2780       <colspec colwidth="4*" colname="name"></colspec>
2781       <colspec colwidth="7*" colname="description"></colspec>
2782       <colspec colwidth="3*" colname="default"></colspec>
2783       <thead>
2784        <row>
2785         <entry>Option</entry>
2786         <entry>Description</entry>
2787         <entry>Default</entry>
2788        </row>
2789       </thead>
2790       <tbody>
2791        <row>
2792         <entry>databaseName</entry>
2793         <entry>Database from connection object</entry>
2794         <entry>Default</entry>
2795        </row>
2796       </tbody>
2797      </tgroup>
2798     </table>
2799    </sect2>
2800    <sect2 id="zoom.commit">
2801     <title>Commit Operation</title>
2802     <para>
2803      For Commit, type must be set to <literal>commit</literal> in
2804      <function>ZOOM_package_send</function>.
2805     </para>
2806    </sect2>
2807    <sect2 id="zoom.extended.services.behavior">
2808     <title>Protocol behavior</title>
2809     <para>
2810      All the extended services are Z39.50-only.
2811     </para>
2812     <note>
2813      <para>
2814       The database create, drop and commit services are privately defined
2815       operations.
2816       Refer to <filename>esadmin.asn</filename> in YAZ for the ASN.1
2817       definitions.
2818      </para>
2819     </note>
2820    </sect2>
2821   </sect1>
2822   <sect1 id="zoom.options">
2823    <title>Options</title>
2824    <para>
2825     Most &zoom; objects provide a way to specify options to change behavior.
2826     From an implementation point of view a set of options is just like
2827     an associative array / hash.
2828    </para>
2829    <synopsis>
2830      ZOOM_options ZOOM_options_create(void);
2831
2832      ZOOM_options ZOOM_options_create_with_parent(ZOOM_options parent);
2833
2834      void ZOOM_options_destroy(ZOOM_options opt);
2835    </synopsis>
2836    <synopsis>
2837      const char *ZOOM_options_get(ZOOM_options opt, const char *name);
2838
2839      void ZOOM_options_set(ZOOM_options opt, const char *name,
2840                            const char *v);
2841    </synopsis>
2842    <synopsis>
2843      typedef const char *(*ZOOM_options_callback)
2844                             (void *handle, const char *name);
2845
2846      ZOOM_options_callback
2847              ZOOM_options_set_callback(ZOOM_options opt,
2848                                        ZOOM_options_callback c,
2849                                        void *handle);
2850    </synopsis>
2851   </sect1>
2852   <sect1 id="zoom.queryconversions">
2853    <title>Query conversions</title>
2854    <synopsis>
2855     int ZOOM_query_cql2rpn(ZOOM_query s, const char *cql_str,
2856                            ZOOM_connection conn);
2857
2858     int ZOOM_query_ccl2rpn(ZOOM_query s, const char *ccl_str,
2859                            const char *config,
2860                            int *ccl_error, const char **error_string,
2861                            int *error_pos);
2862    </synopsis>
2863    <para>
2864     <function>ZOOM_query_cql2rpn</function> translates the CQL string,
2865     client-side, into RPN which may be passed to the server.
2866     This is useful for server's that don't themselves
2867     support CQL, for which <function>ZOOM_query_cql</function> is useless.
2868     `conn' is used  only as a place to stash diagnostics if compilation
2869     fails; if this information is not needed, a null pointer may be used.
2870     The CQL conversion is driven by option <literal>cqlfile</literal> from
2871     connection conn. This specifies a conversion file (eg pqf.properties)
2872     which <emphasis>must</emphasis> be present.
2873    </para>
2874    <para>
2875     <function>ZOOM_query_ccl2rpn</function> translates the CCL string,
2876     client-side, into RPN which may be passed to the server.
2877     The conversion is driven by the specification given by
2878     <literal>config</literal>. Upon completion 0 is returned on success; -1
2879     is returned on on failure. Om failure <literal>error_string</literal> and
2880     <literal>error_pos</literal> holds error message and position of
2881     first error in original CCL string.
2882    </para>
2883   </sect1>
2884   <sect1 id="zoom.events"><title>Events</title>
2885    <para>
2886     If you're developing non-blocking applications, you have to deal
2887     with events.
2888    </para>
2889    <synopsis>
2890     int ZOOM_event(int no, ZOOM_connection *cs);
2891    </synopsis>
2892    <para>
2893     The <function>ZOOM_event</function> executes pending events for
2894     a number of connections. Supply the number of connections in
2895     <literal>no</literal> and an array of connections in
2896     <literal>cs</literal> (<literal>cs[0] ... cs[no-1]</literal>).
2897     A pending event could be a sending a search, receiving a response,
2898     etc.
2899     When an event has occurred for one of the connections, this function
2900     returns a positive integer <literal>n</literal> denoting that an event
2901     occurred for connection <literal>cs[n-1]</literal>.
2902     When no events are pending for the connections, a value of zero is
2903     returned.
2904     To ensure that all outstanding requests are performed call this function
2905     repeatedly until zero is returned.
2906    </para>
2907    <para>
2908     If <function>ZOOM_event</function> returns and returns non-zero, the
2909     last event that occurred can be expected.
2910    </para>
2911    <synopsis>
2912     int ZOOM_connection_last_event(ZOOM_connection cs);
2913    </synopsis>
2914    <para>
2915     <function>ZOOM_connection_last_event</function> returns an event type
2916     (integer) for the last event.
2917    </para>
2918
2919    <table frame="top" id="zoom.event.ids">
2920     <title>ZOOM Event IDs</title>
2921     <tgroup cols="2">
2922      <colspec colwidth="4*" colname="name"></colspec>
2923      <colspec colwidth="7*" colname="description"></colspec>
2924      <thead>
2925       <row>
2926        <entry>Event</entry>
2927        <entry>Description</entry>
2928       </row>
2929      </thead>
2930      <tbody>
2931       <row>
2932        <entry>ZOOM_EVENT_NONE</entry>
2933        <entry>No event has occurred</entry>
2934       </row>
2935       <row>
2936        <entry>ZOOM_EVENT_CONNECT</entry>
2937        <entry>TCP/IP connect has initiated</entry>
2938       </row>
2939       <row>
2940        <entry>ZOOM_EVENT_SEND_DATA</entry>
2941        <entry>Data has been transmitted (sending)</entry>
2942       </row>
2943       <row>
2944        <entry>ZOOM_EVENT_RECV_DATA</entry>
2945        <entry>Data has been received)</entry>
2946       </row>
2947       <row>
2948        <entry>ZOOM_EVENT_TIMEOUT</entry>
2949        <entry>Timeout</entry>
2950       </row>
2951       <row>
2952        <entry>ZOOM_EVENT_UNKNOWN</entry>
2953        <entry>Unknown event</entry>
2954       </row>
2955       <row>
2956        <entry>ZOOM_EVENT_SEND_APDU</entry>
2957        <entry>An APDU has been transmitted (sending)</entry>
2958       </row>
2959       <row>
2960        <entry>ZOOM_EVENT_RECV_APDU</entry>
2961        <entry>An APDU has been received</entry>
2962       </row>
2963       <row>
2964        <entry>ZOOM_EVENT_RECV_RECORD</entry>
2965        <entry>A result-set record has been received</entry>
2966       </row>
2967       <row>
2968        <entry>ZOOM_EVENT_RECV_SEARCH</entry>
2969        <entry>A search result been received</entry>
2970       </row>
2971      </tbody>
2972     </tgroup>
2973    </table>
2974   </sect1>
2975  </chapter>
2976  <chapter id="server">
2977   <title>Generic server</title>
2978   <sect1 id="server.introduction"><title>Introduction</title>
2979    <para>
2980     If you aren't into documentation, a good way to learn how the
2981     back end interface works is to look at the <filename>backend.h</filename>
2982     file. Then, look at the small dummy-server in
2983     <filename>ztest/ztest.c</filename>. The <filename>backend.h</filename>
2984     file also makes a good reference, once you've chewed your way through
2985     the prose of this file.
2986    </para>
2987    <para>
2988     If you have a database system that you would like to make available by
2989     means of Z39.50 or SRU, &yaz; basically offers your two options. You
2990     can use the APIs provided by the &asn;, &odr;, and &comstack;
2991     modules to
2992     create and decode PDUs, and exchange them with a client.
2993     Using this low-level interface gives you access to all fields and
2994     options of the protocol, and you can construct your server as close
2995     to your existing database as you like.
2996     It is also a fairly involved process, requiring
2997     you to set up an event-handling mechanism, protocol state machine,
2998     etc. To simplify server implementation, we have implemented a compact
2999     and simple, but reasonably full-functioned server-frontend that will
3000     handle most of the protocol mechanics, while leaving you to
3001     concentrate on your database interface.
3002    </para>
3003    <note>
3004     <para>
3005      The backend interface was designed in anticipation of a specific
3006      integration task, while still attempting to achieve some degree of
3007      generality. We realize fully that there are points where the
3008      interface can be improved significantly. If you have specific
3009      functions or parameters that you think could be useful, send us a
3010      mail (or better, sign on to the mailing list referred to in the
3011      top-level README file). We will try to fit good suggestions into future
3012      releases, to the extent that it can be done without requiring
3013      too many structural changes in existing applications.
3014     </para>
3015    </note>
3016    <note>
3017     <para>
3018      The &yaz; server does not support XCQL.
3019      </para>
3020    </note>
3021   </sect1>
3022   <sect1 id="server.frontend">
3023    <title>The Database Frontend</title>
3024    <para>
3025     We refer to this software as a generic database frontend. Your
3026     database system is the <emphasis>backend database</emphasis>, and the
3027     interface between the two is called the <emphasis>backend API</emphasis>.
3028     The backend API consists of a small number of function handlers and
3029     structure definitions. You are required to provide the
3030     <function>main()</function> routine for the server (which can be
3031     quite simple), as well as a set of handlers to match each of the
3032     prototypes.
3033     The interface functions that you write can use any mechanism you like
3034     to communicate with your database system: You might link the whole
3035     thing together with your database application and access it by
3036     function calls; you might use IPC to talk to a database server
3037     somewhere; or you might link with third-party software that handles
3038     the communication for you (like a commercial database client library).
3039     At any rate, the handlers will perform the tasks of:
3040    </para>
3041    <itemizedlist>
3042     <listitem><para>
3043      Initialization.
3044     </para></listitem>
3045     <listitem><para>
3046      Searching.
3047     </para></listitem>
3048     <listitem><para>
3049      Fetching records.
3050     </para></listitem>
3051     <listitem><para>
3052      Scanning the database index (optional - if you wish to implement SCAN).
3053     </para></listitem>
3054     <listitem><para>
3055      Extended Services (optional).
3056     </para></listitem>
3057     <listitem><para>
3058      Result-Set Delete (optional).
3059     </para></listitem>
3060     <listitem><para>
3061      Result-Set Sort (optional).
3062     </para></listitem>
3063     <listitem><para>
3064      Return Explain for SRU (optional).
3065     </para></listitem>
3066    </itemizedlist>
3067    <para>
3068     (more functions will be added in time to support as much of
3069     Z39.50-1995 as possible).
3070    </para>
3071   </sect1>
3072   <sect1 id="server.backend">
3073    <title>The Backend API</title>
3074    <para>
3075     The header file that you need to use the interface are in the
3076     <filename>include/yaz</filename> directory. It's called
3077     <filename>backend.h</filename>. It will include other files from
3078     the <filename>include/yaz</filename> directory, so you'll
3079     probably want to use the -I option of your compiler to tell it
3080     where to find the files. When you run
3081     <literal>make</literal> in the top-level &yaz; directory,
3082     everything you need to create your server is to link with the
3083     <filename>lib/libyaz.la</filename> library.
3084    </para>
3085   </sect1>
3086   <sect1 id="server.main">
3087    <title>Your main() Routine</title>
3088    <para>
3089     As mentioned, your <function>main()</function> routine can be quite brief.
3090     If you want to initialize global parameters, or read global configuration
3091     tables, this is the place to do it. At the end of the routine, you should
3092     call the function
3093    </para>
3094    <synopsis>
3095 int statserv_main(int argc, char **argv,
3096                   bend_initresult *(*bend_init)(bend_initrequest *r),
3097                   void (*bend_close)(void *handle));
3098    </synopsis>
3099    <para>
3100     The third and fourth arguments are pointers to handlers. Handler
3101     <function>bend_init</function> is called whenever the server receives
3102     an Initialize Request, so it serves as a Z39.50 session initializer. The
3103     <function>bend_close</function> handler is called when the session is
3104     closed.
3105    </para>
3106    <para>
3107     <function>statserv_main</function> will establish listening sockets
3108     according to the parameters given. When connection requests are received,
3109     the event handler will typically <function>fork()</function> and
3110     create a sub-process to handle a new connection.
3111     Alternatively the server may be setup to create threads for each
3112     connection.
3113     If you do use global variables and forking, you should be aware, then,
3114     that these cannot be shared between associations, unless you explicitly
3115     disable forking by command line parameters.
3116    </para>
3117    <para>
3118     The server provides a mechanism for controlling some of its behavior
3119     without using command-line options. The function
3120    </para>
3121    <synopsis>
3122     statserv_options_block *statserv_getcontrol(void);
3123    </synopsis>
3124    <para>
3125     will return a pointer to a <literal>struct statserv_options_block</literal>
3126     describing the current default settings of the server. The structure
3127     contains these elements:
3128     <variablelist>
3129      <varlistentry>
3130       <term><literal>int dynamic</literal></term>
3131       <listitem><para>
3132        A boolean value, which determines whether the server
3133        will fork on each incoming request (TRUE), or not (FALSE). Default is
3134        TRUE. This flag is only read by UNIX-based servers (WIN32 based servers
3135        doesn't fork).
3136      </para></listitem>
3137      </varlistentry>
3138      <varlistentry>
3139       <term><literal>int threads</literal></term>
3140       <listitem><para>
3141        A boolean value, which determines whether the server
3142        will create a thread on each incoming request (TRUE), or not (FALSE).
3143        Default is FALSE. This flag is only read by UNIX-based servers
3144        that offer POSIX Threads support.
3145        WIN32-based servers always operate in threaded mode.
3146      </para></listitem>
3147      </varlistentry>
3148      <varlistentry>
3149       <term><literal>int inetd</literal></term>
3150       <listitem><para>
3151        A boolean value, which determines whether the server
3152        will operates under a UNIX INET daemon (inetd). Default is FALSE.
3153      </para></listitem>
3154      </varlistentry>
3155      <varlistentry>
3156       <term><literal>char logfile[ODR_MAXNAME+1]</literal></term>
3157       <listitem><para>File for diagnostic output (&quot;&quot;: stderr).
3158      </para></listitem>
3159      </varlistentry>
3160      <varlistentry>
3161       <term><literal>char apdufile[ODR_MAXNAME+1]</literal></term>
3162       <listitem><para>
3163        Name of file for logging incoming and outgoing APDUs
3164        (&quot;&quot;: don't log APDUs, &quot;-&quot;:
3165        <literal>stderr</literal>).
3166      </para></listitem>
3167      </varlistentry>
3168      <varlistentry>
3169       <term><literal>char default_listen[1024]</literal></term>
3170       <listitem><para>Same form as the command-line specification of
3171       listener address. &quot;&quot;: no default listener address.
3172       Default is to listen at &quot;tcp:@:9999&quot;. You can only
3173       specify one default listener address in this fashion.
3174      </para></listitem>
3175      </varlistentry>
3176      <varlistentry>
3177       <term><literal>enum oid_proto default_proto;</literal></term>
3178       <listitem><para>Either <literal>PROTO_Z3950</literal> or
3179       <literal>PROTO_SR</literal>.
3180       Default is <literal>PROTO_Z39_50</literal>.
3181      </para></listitem>
3182      </varlistentry>
3183      <varlistentry>
3184       <term><literal>int idle_timeout;</literal></term>
3185       <listitem><para>Maximum session idle-time, in minutes. Zero indicates
3186       no (infinite) timeout. Default is 15 minutes.
3187      </para></listitem>
3188      </varlistentry>
3189      <varlistentry>
3190       <term><literal>int maxrecordsize;</literal></term>
3191       <listitem><para>Maximum permissible record (message) size. Default
3192       is 64 MB. This amount of memory will only be allocated if a
3193       client requests a very large amount of records in one operation
3194       (or a big record).
3195       Set it to a lower number if you are worried about resource
3196       consumption on your host system.
3197      </para></listitem>
3198      </varlistentry>
3199      <varlistentry>
3200       <term><literal>char configname[ODR_MAXNAME+1]</literal></term>
3201       <listitem><para>Passed to the backend when a new connection is received.
3202      </para></listitem>
3203      </varlistentry>
3204      <varlistentry>
3205       <term><literal>char setuid[ODR_MAXNAME+1]</literal></term>
3206       <listitem><para>Set user id to the user specified, after binding
3207       the listener addresses.
3208      </para></listitem>
3209      </varlistentry>
3210      <varlistentry>
3211       <term>
3212        <literal>void (*bend_start)(struct statserv_options_block *p)</literal>
3213       </term>
3214       <listitem><para>Pointer to function which is called after the
3215       command line options have been parsed - but before the server
3216       starts listening.
3217       For forked UNIX servers this handler is called in the mother
3218       process; for threaded servers this handler is called in the
3219       main thread.
3220       The default value of this pointer is NULL in which case it
3221       isn't invoked by the frontend server.
3222       When the server operates as an NT service this handler is called
3223       whenever the service is started.
3224       </para></listitem>
3225      </varlistentry>
3226      <varlistentry>
3227       <term>
3228        <literal>void (*bend_stop)(struct statserv_options_block *p)</literal>
3229       </term>
3230       <listitem><para>Pointer to function which is called whenever the server
3231       has stopped listening for incoming connections. This function pointer
3232       has a default value of NULL in which case it isn't called.
3233       When the server operates as an NT service this handler is called
3234       whenever the service is stopped.
3235       </para></listitem>
3236      </varlistentry>
3237      <varlistentry>
3238       <term><literal>void *handle</literal></term>
3239       <listitem><para>User defined pointer (default value NULL).
3240       This is a per-server handle that can be used to specify "user-data".
3241       Do not confuse this with the session-handle as returned by bend_init.
3242       </para></listitem>
3243      </varlistentry>
3244     </variablelist>
3245    </para>
3246    <para>
3247     The pointer returned by <literal>statserv_getcontrol</literal> points to
3248     a static area. You are allowed to change the contents of the structure,
3249     but the changes will not take effect before you call
3250    </para>
3251    <synopsis>
3252 void statserv_setcontrol(statserv_options_block *block);
3253    </synopsis>
3254    <note>
3255     <para>
3256      that you should generally update this structure before calling
3257      <function>statserv_main()</function>.
3258     </para>
3259    </note>
3260   </sect1>
3261   <sect1 id="server.backendfunctions">
3262    <title>The Backend Functions</title>
3263    <para>
3264     For each service of the protocol, the backend interface declares one or
3265     two functions. You are required to provide implementations of the
3266     functions representing the services that you wish to implement.
3267    </para>
3268    <sect2 id="server.init">
3269     <title>Init</title>
3270     <synopsis>
3271 bend_initresult (*bend_init)(bend_initrequest *r);
3272     </synopsis>
3273     <para>
3274      This handler is called once for each new connection request, after
3275      a new process/thread has been created, and an Initialize Request has
3276      been received from the client. The pointer to the
3277      <function>bend_init</function> handler is passed in the call to
3278      <function>statserv_start</function>.
3279     </para>
3280     <para>
3281      This handler is also called when operating in SRU mode - when
3282      a connection has been made (even though SRU does not offer
3283      this service).
3284     </para>
3285     <para>
3286      Unlike previous versions of YAZ, the <function>bend_init</function> also
3287      serves as a handler that defines the Z39.50 services that the backend
3288      wish to support. Pointers to <emphasis>all</emphasis> service handlers,
3289      including search - and fetch must be specified here in this handler.
3290     </para>
3291     <para>
3292      The request - and result structures are defined as
3293     </para>
3294     <synopsis>
3295 typedef struct bend_initrequest
3296 {
3297     /** \brief user/name/password to be read */
3298     Z_IdAuthentication *auth;
3299     /** \brief encoding stream (for results) */
3300     ODR stream;
3301     /** \brief printing stream */
3302     ODR print;
3303     /** \brief decoding stream (use stream for results) */
3304     ODR decode;
3305     /** \brief reference ID */
3306     Z_ReferenceId *referenceId;
3307     /** \brief peer address of client */
3308     char *peer_name;
3309
3310     /** \brief character set and language negotiation
3311
3312     see include/yaz/z-charneg.h
3313     */
3314     Z_CharSetandLanguageNegotiation *charneg_request;
3315
3316     /** \brief character negotiation response */
3317     Z_External *charneg_response;
3318
3319     /** \brief character set (encoding) for query terms
3320
3321     This is NULL by default. It should be set to the native character
3322     set that the backend assumes for query terms */
3323     char *query_charset;
3324
3325     /** \brief whehter query_charset also applies to recors
3326
3327     Is 0 (No) by default. Set to 1 (yes) if records is in the same
3328     character set as queries. If in doubt, use 0 (No).
3329     */
3330     int records_in_same_charset;
3331
3332     char *implementation_id;
3333     char *implementation_name;
3334     char *implementation_version;
3335
3336     /** \brief Z39.50 sort handler */
3337     int (*bend_sort)(void *handle, bend_sort_rr *rr);
3338     /** \brief SRU/Z39.50 search handler */
3339     int (*bend_search)(void *handle, bend_search_rr *rr);
3340     /** \brief SRU/Z39.50 fetch handler */
3341     int (*bend_fetch)(void *handle, bend_fetch_rr *rr);
3342     /** \brief SRU/Z39.50 present handler */
3343     int (*bend_present)(void *handle, bend_present_rr *rr);
3344     /** \brief Z39.50 extended services handler */
3345     int (*bend_esrequest) (void *handle, bend_esrequest_rr *rr);
3346     /** \brief Z39.50 delete result set handler */
3347     int (*bend_delete)(void *handle, bend_delete_rr *rr);
3348     /** \brief Z39.50 scan handler */
3349     int (*bend_scan)(void *handle, bend_scan_rr *rr);
3350     /** \brief Z39.50 segment facility handler */
3351     int (*bend_segment)(void *handle, bend_segment_rr *rr);
3352     /** \brief SRU explain handler */
3353     int (*bend_explain)(void *handle, bend_explain_rr *rr);
3354     /** \brief SRU scan handler */
3355     int (*bend_srw_scan)(void *handle, bend_scan_rr *rr);
3356     /** \brief SRU record update handler */
3357     int (*bend_srw_update)(void *handle, bend_update_rr *rr);
3358
3359     /** \brief whether named result sets are supported (0=disable, 1=enable) */
3360     int named_result_sets;
3361 } bend_initrequest;
3362
3363 typedef struct bend_initresult
3364 {
3365     int errcode;               /* 0==OK */
3366     char *errstring;           /* system error string or NULL */
3367     void *handle;              /* private handle to the backend module */
3368 } bend_initresult;
3369     </synopsis>
3370     <para>
3371      In general, the server frontend expects that the
3372      <literal>bend_*result</literal> pointer that you return is valid at
3373      least until the next call to a <literal>bend_* function</literal>.
3374      This applies to all of the functions described herein. The parameter
3375      structure passed to you in the call belongs to the server frontend, and
3376      you should not make assumptions about its contents after the current
3377      function call has completed. In other words, if you want to retain any
3378      of the contents of a request structure, you should copy them.
3379     </para>
3380     <para>
3381      The <literal>errcode</literal> should be zero if the initialization of
3382      the backend went well. Any other value will be interpreted as an error.
3383      The <literal>errstring</literal> isn't used in the current version, but
3384      one option would be to stick it in the initResponse as a VisibleString.
3385      The <literal>handle</literal> is the most important parameter. It should
3386      be set to some value that uniquely identifies the current session to
3387      the backend implementation. It is used by the frontend server in any
3388      future calls to a backend function.
3389      The typical use is to set it to point to a dynamically allocated state
3390      structure that is private to your backend module.
3391     </para>
3392     <para>
3393      The <literal>auth</literal> member holds the authentication information
3394      part of the Z39.50 Initialize Request. Interpret this if your serves
3395      requires authentication.
3396     </para>
3397     <para>
3398      The members <literal>peer_name</literal>,
3399      <literal>implementation_id</literal>,
3400      <literal>implementation_name</literal> and
3401      <literal>implementation_version</literal> holds
3402      DNS of client, ID of implementor, name
3403      of client (Z39.50) implementation - and version.
3404     </para>
3405     <para>
3406      The <literal>bend_</literal> - members are set to NULL when
3407      <function>bend_init</function> is called. Modify the pointers by
3408      setting them to point to backend functions.
3409     </para>
3410    </sect2>
3411    <sect2 id="server.search.retrieve">
3412     <title>Search and Retrieve</title>
3413     <para>
3414      We now describe the handlers that are required to support search -
3415      and retrieve. You must support two functions - one for search - and one
3416      for fetch (retrieval of one record). If desirable you can provide a
3417      third handler which is called when a present request is received which
3418      allows you to optimize retrieval of multiple-records.
3419     </para>
3420     <synopsis>
3421 int (*bend_search) (void *handle, bend_search_rr *rr);
3422
3423 typedef struct {
3424     char *setname;             /* name to give to this set */
3425     int replace_set;           /* replace set, if it already exists */
3426     int num_bases;             /* number of databases in list */
3427     char **basenames;          /* databases to search */
3428     Z_ReferenceId *referenceId;/* reference ID */
3429     Z_Query *query;            /* query structure */
3430     ODR stream;                /* encode stream */
3431     ODR decode;                /* decode stream */
3432     ODR print;                 /* print stream */
3433
3434     bend_request request;
3435     bend_association association;
3436     int *fd;
3437     int hits;                  /* number of hits */
3438     int errcode;               /* 0==OK */
3439     char *errstring;           /* system error string or NULL */
3440     Z_OtherInformation *search_info; /* additional search info */
3441     char *srw_sortKeys;        /* holds SRU/SRW sortKeys info */
3442     char *srw_setname;         /* holds SRU/SRW generated resultsetID */
3443     int *srw_setnameIdleTime;  /* holds SRU/SRW life-time */
3444     int estimated_hit_count;   /* if hit count is estimated */
3445     int partial_resultset;     /* if result set is partial */
3446 } bend_search_rr;
3447     </synopsis>
3448     <para>
3449      The <function>bend_search</function> handler is a fairly close
3450      approximation of a protocol Z39.50 Search Request - and Response PDUs
3451      The <literal>setname</literal> is the resultSetName from the protocol.
3452      You are required to establish a mapping between the set name and whatever
3453      your backend database likes to use.
3454      Similarly, the <literal>replace_set</literal> is a boolean value
3455      corresponding to the resultSetIndicator field in the protocol.
3456      <literal>num_bases/basenames</literal> is a length of/array of character
3457      pointers to the database names provided by the client.
3458      The <literal>query</literal> is the full query structure as defined in
3459      the protocol ASN.1 specification.
3460      It can be either of the possible query types, and it's up to you to
3461      determine if you can handle the provided query type.
3462      Rather than reproduce the C interface here, we'll refer you to the
3463      structure definitions in the file
3464      <filename>include/yaz/z-core.h</filename>. If you want to look at the
3465      attributeSetId OID of the RPN query, you can either match it against
3466      your own internal tables, or you can use the <link linkend="tools.oid">
3467      OID tools</link>.
3468     </para>
3469     <para>
3470      The structure contains a number of hits, and an
3471      <literal>errcode/errstring</literal> pair. If an error occurs
3472      during the search, or if you're unhappy with the request, you should
3473      set the errcode to a value from the BIB-1 diagnostic set. The value
3474      will then be returned to the user in a nonsurrogate diagnostic record
3475      in the response. The <literal>errstring</literal>, if provided, will
3476      go in the addinfo field. Look at the protocol definition for the
3477      defined error codes, and the suggested uses of the addinfo field.
3478     </para>
3479     <para>
3480      The <function>bend_search</function> handler is also called when
3481      the frontend server receives a SRU SearchRetrieveRequest.
3482      For SRU, a CQL query is usually provided by the client.
3483      The CQL query is available as part of <literal>Z_Query</literal>
3484      structure (note that CQL is now part of Z39.50 via an external).
3485      To support CQL in existing implementations that only do Type-1,
3486      we refer to the CQL-to-PQF tool described
3487      <link linkend="cql.to.pqf">here</link>.
3488     </para>
3489     <para>
3490      To maintain backwards compatibility, the frontend server
3491      of yaz always assume that error codes are BIB-1 diagnostics.
3492      For SRU operation, a Bib-1 diagnostic code is mapped to
3493      SRU diagnostic.
3494     </para>
3495     <synopsis>
3496 int (*bend_fetch) (void *handle, bend_fetch_rr *rr);
3497
3498 typedef struct bend_fetch_rr {
3499     char *setname;             /* set name */
3500     int number;                /* record number */
3501     Z_ReferenceId *referenceId;/* reference ID */
3502     Odr_oid *request_format;        /* format, transfer syntax (OID) */
3503     Z_RecordComposition *comp; /* Formatting instructions */
3504     ODR stream;                /* encoding stream - memory source if req */
3505     ODR print;                 /* printing stream */
3506
3507     char *basename;            /* name of database that provided record */
3508     int len;                   /* length of record or -1 if structured */
3509     char *record;              /* record */
3510     int last_in_set;           /* is it?  */
3511     Odr_oid *output_format;        /* response format/syntax (OID) */
3512     int errcode;               /* 0==success */
3513     char *errstring;           /* system error string or NULL */
3514     int surrogate_flag;        /* surrogate diagnostic */
3515     char *schema;              /* string record schema input/output */
3516 } bend_fetch_rr;
3517     </synopsis>
3518     <para>
3519      The frontend server calls the <function>bend_fetch</function> handler
3520      when it needs database records to fulfill a Z39.50 Search Request, a
3521      Z39.50 Present Request or a SRU SearchRetrieveRequest.
3522      The <literal>setname</literal> is simply the name of the result set
3523      that holds the reference to the desired record.
3524      The <literal>number</literal> is the offset into the set (with 1
3525      being the first record in the set). The <literal>format</literal> field
3526      is the record format requested by the client (See
3527      <xref linkend="tools.oid"/>).
3528      A value of NULL for <literal>format</literal> indicates that the
3529      client did not request a specific format.
3530      The <literal>stream</literal> argument is an &odr; stream which
3531      should be used for allocating space for structured data records.
3532      The stream will be reset when all records have been assembled, and
3533      the response package has been transmitted.
3534      For unstructured data, the backend is responsible for maintaining a
3535      static or dynamic buffer for the record between calls.
3536     </para>
3537     <para>
3538      If a SRU SearchRetrieveRequest is received by the frontend server,
3539      the <literal>referenceId</literal> is NULL and the
3540      <literal>format</literal> (transfer syntax) is the OID for XML.
3541      The schema for SRU is stored in both the
3542      <literal>Z_RecordComposition</literal>
3543      structure and <literal>schema</literal> (simple string).
3544     </para>
3545     <para>
3546      In the structure, the <literal>basename</literal> is the name of the
3547      database that holds the
3548      record. <literal>len</literal> is the length of the record returned, in
3549      bytes, and <literal>record</literal> is a pointer to the record.
3550      <literal>last_in_set</literal> should be nonzero only if the record
3551      returned is the last one in the given result set.
3552      <literal>errcode</literal> and <literal>errstring</literal>, if
3553      given, will be interpreted as a global error pertaining to the
3554      set, and will be returned in a non-surrogate-diagnostic.
3555      If you wish to return the error as a surrogate-diagnostic
3556      (local error) you can do this by setting
3557      <literal>surrogate_flag</literal> to 1 also.
3558     </para>
3559     <para>
3560      If the <literal>len</literal> field has the value -1, then
3561      <literal>record</literal> is assumed to point to a constructed data
3562      type. The <literal>format</literal> field will be used to determine
3563      which encoder should be used to serialize the data.
3564     </para>
3565     <note>
3566      <para>
3567       If your backend generates structured records, it should use
3568       <function>odr_malloc()</function> on the provided stream for allocating
3569       data: This allows the frontend server to keep track of the record sizes.
3570      </para>
3571     </note>
3572     <para>
3573      The <literal>format</literal> field is mapped to an object identifier
3574      in the direct reference of the resulting EXTERNAL representation
3575      of the record.
3576     </para>
3577     <note>
3578      <para>
3579       The current version of &yaz; only supports the direct reference mode.
3580      </para>
3581     </note>
3582     <synopsis>
3583 int (*bend_present) (void *handle, bend_present_rr *rr);
3584
3585 typedef struct {
3586     char *setname;             /* set name */
3587     int start;
3588     int number;                /* record number */
3589     Odr_oid *format;           /* format, transfer syntax (OID) */
3590     Z_ReferenceId *referenceId;/* reference ID */
3591     Z_RecordComposition *comp; /* Formatting instructions */
3592     ODR stream;                /* encoding stream - memory source if required */
3593     ODR print;                 /* printing stream */
3594     bend_request request;
3595     bend_association association;
3596
3597     int hits;                  /* number of hits */
3598     int errcode;               /* 0==OK */
3599     char *errstring;           /* system error string or NULL */
3600 } bend_present_rr;
3601     </synopsis>
3602     <para>
3603      The <function>bend_present</function> handler is called when
3604      the server receives a Z39.50 Present Request.
3605      The <literal>setname</literal>,
3606      <literal>start</literal> and <literal>number</literal> is the
3607      name of the result set - start position - and number of records to
3608      be retrieved respectively. <literal>format</literal> and
3609      <literal>comp</literal> is the preferred transfer syntax and element
3610      specifications of the present request.
3611     </para>
3612     <para>
3613      Note that this is handler serves as a supplement for
3614      <function>bend_fetch</function> and need not to be defined in order to
3615      support search - and retrieve.
3616     </para>
3617    </sect2>
3618    <sect2 id="server.delete">
3619     <title>Delete</title>
3620     <para>
3621      For back-ends that supports delete of a result set only one handler
3622      must be defined.
3623     </para>
3624     <synopsis>
3625 int (*bend_delete)(void *handle, bend_delete_rr *rr);
3626
3627 typedef struct bend_delete_rr {
3628     int function;
3629     int num_setnames;
3630     char **setnames;
3631     Z_ReferenceId *referenceId;
3632     int delete_status;      /* status for the whole operation */
3633     int *statuses;          /* status each set - indexed as setnames */
3634     ODR stream;
3635     ODR print;
3636 } bend_delete_rr;
3637     </synopsis>
3638     <note>
3639      <para>
3640       The delete set function definition is rather primitive, mostly because
3641       we have had no practical need for it as of yet. If someone wants
3642       to provide a full delete service, we'd be happy to add the
3643       extra parameters that are required. Are there clients out there
3644       that will actually delete sets they no longer need?
3645      </para>
3646     </note>
3647    </sect2>
3648    <sect2 id="server.scan">
3649     <title>Scan</title>
3650     <para>
3651      For servers that wish to offer the scan service one handler
3652      must be defined.
3653     </para>
3654     <synopsis>
3655 int (*bend_scan)(void *handle, bend_scan_rr *rr);
3656
3657 typedef enum {
3658     BEND_SCAN_SUCCESS,  /* ok */
3659     BEND_SCAN_PARTIAL   /* not all entries could be found */
3660 } bend_scan_status;
3661
3662 typedef struct bend_scan_rr {
3663     int num_bases;      /* number of elements in databaselist */
3664     char **basenames;   /* databases to search */
3665     Odr_oid *attributeset;
3666     Z_ReferenceId *referenceId; /* reference ID */
3667     Z_AttributesPlusTerm *term;
3668     ODR stream;         /* encoding stream - memory source if required */
3669     ODR print;          /* printing stream */
3670
3671     int *step_size;     /* step size */
3672     int term_position;  /* desired index of term in result list/returned */
3673     int num_entries;    /* number of entries requested/returned */
3674
3675     /* scan term entries. The called handler does not have
3676        to allocate this. Size of entries is num_entries (see above) */
3677     struct scan_entry *entries;
3678     bend_scan_status status;
3679     int errcode;
3680     char *errstring;
3681     char *scanClause;   /* CQL scan clause */
3682     char *setname;      /* Scan in result set (NULL if omitted) */
3683 } bend_scan_rr;
3684     </synopsis>
3685    <para>
3686     This backend server handles both Z39.50 scan
3687     and SRU scan. In order for a handler to distinguish between SRU (CQL) scan
3688     Z39.50 Scan , it must check for a non-NULL value of
3689     <literal>scanClause</literal>.
3690    </para>
3691    <note>
3692     <para>
3693      if designed today, it would be a choice using a union or similar,
3694      but that would break binary compatibility with existing servers.
3695     </para>
3696     </note>
3697    </sect2>
3698   </sect1>
3699   <sect1 id="server.invocation">
3700    <title>Application Invocation</title>
3701    <para>
3702     The finished application has the following
3703     invocation syntax (by way of <function>statserv_main()</function>):
3704    </para>
3705    &gfs-synopsis;
3706    <para>
3707     The options are:
3708     &gfs-options;
3709    </para>
3710    <para>
3711     A listener specification consists of a transport mode followed by a
3712     colon (:) followed by a listener address. The transport mode is
3713     either <literal>tcp</literal>, <literal>unix:</literal> or
3714     <literal>ssl</literal>.
3715    </para>
3716    <para>
3717     For TCP and SSL, an address has the form
3718    </para>
3719    <synopsis>
3720     hostname | IP-number [: portnumber]
3721    </synopsis>
3722    <para>
3723     The port number defaults to 210 (standard Z39.50 port).
3724    </para>
3725    <para>
3726     For UNIX, the address is the filename of socket.
3727    </para>
3728    <para>
3729     For TCP/IP and SSL, the special hostnames <literal>@</literal>,
3730     maps to <literal>IN6ADDR_ANY_INIT</literal> with
3731     IPV4 binding as well (bindv6only=0),
3732     The special hostname <literal>@4</literal> binds to
3733     <literal>INADDR_ANY</literal> (IPV4 only listener).
3734     The special hostname <literal>@6</literal> binds to
3735     <literal>IN6ADDR_ANY_INIT</literal> with bindv6only=1 (IPV6 only listener).
3736    </para>
3737    <example id="server.example.running.unix">
3738     <title>Running the GFS on Unix</title>
3739     <para>
3740      Assuming the server application <replaceable>appname</replaceable> is
3741      started as root, the following will make it listen on port 210.
3742      The server will change identity to <literal>nobody</literal>
3743      and write its log to <filename>/var/log/app.log</filename>.
3744      <screen>
3745       application -l /var/log/app.log -u nobody tcp:@:210
3746      </screen>
3747     </para>
3748     <para>
3749      The server will accept Z39.50 requests and offer SRU service on port 210.
3750     </para>
3751    </example>
3752    <example id="server.example.apache.sru">
3753     <title>Setting up Apache as SRU Frontend</title>
3754     <para>
3755      If you use <ulink url="&url.apache;">Apache</ulink>
3756      as your public web server and want to offer HTTP port 80
3757      access to the YAZ server on 210, you can use the
3758      <ulink url="&url.apache.directive.proxypass;">
3759       <literal>ProxyPass</literal></ulink>
3760      directive.
3761      If you have virtual host
3762      <literal>srw.mydomain</literal> you can use the following directives
3763      in Apache's httpd.conf:
3764      <screen>
3765       &lt;VirtualHost *>
3766        ErrorLog /home/srw/logs/error_log
3767        TransferLog /home/srw/logs/access_log
3768        ProxyPass / http://srw.mydomain:210/
3769       &lt;/VirtualHost>
3770      </screen>
3771     </para>
3772     <para>
3773      The above for the Apache 1.3 series.
3774     </para>
3775    </example>
3776    <example id="server.example.local.access">
3777     <title>Running a server with local access only</title>
3778     <para>
3779      Servers that is only being accessed from the local host should listen
3780      on UNIX file socket rather than a Internet socket. To listen on
3781      <filename>/tmp/mysocket</filename> start the server as follows:
3782      <screen>
3783       application unix:/tmp/mysocket
3784      </screen>
3785     </para>
3786    </example>
3787   </sect1>
3788   <sect1 id="server.vhosts">
3789    <title>GFS Configuration and Virtual Hosts</title>
3790    &gfs-virtual;
3791   </sect1>
3792  </chapter>
3793  <chapter id="asn">
3794   <title>The Z39.50 ASN.1 Module</title>
3795   <sect1 id="asn.introduction">
3796    <title>Introduction</title>
3797    <para>
3798     The &asn; module provides you with a set of C struct definitions for the
3799     various PDUs of the Z39.50 protocol, as well as for the complex types
3800     appearing within the PDUs. For the primitive data types, the C
3801     representation often takes the form of an ordinary C language type,
3802     such as <literal>Odr_int</literal> which is equivalent to an integral
3803     C integer. For ASN.1 constructs that have no direct
3804     representation in C, such as general octet strings and bit strings,
3805     the &odr; module (see section <link linkend="odr">The ODR Module</link>)
3806     provides auxiliary definitions.
3807    </para>
3808    <para>
3809     The &asn; module is located in sub directory <filename>z39.50</filename>.
3810     There you'll find C files that implements encoders and decoders for the
3811     Z39.50 types. You'll also find the protocol definitions:
3812     <filename>z3950v3.asn</filename>, <filename>esupdate.asn</filename>,
3813     and others.
3814    </para>
3815   </sect1>
3816   <sect1 id="asn.preparing">
3817    <title>Preparing PDUs</title>
3818    <para>
3819     A structure representing a complex ASN.1 type doesn't in itself contain the
3820     members of that type. Instead, the structure contains
3821     <emphasis>pointers</emphasis> to the members of the type.
3822     This is necessary, in part, to allow a mechanism for specifying which
3823     of the optional structure (SEQUENCE) members are present, and which
3824     are not. It follows that you will need to somehow provide space for
3825     the individual members of the structure, and set the pointers to
3826     refer to the members.
3827    </para>
3828    <para>
3829     The conversion routines don't care how you allocate and maintain your
3830     C structures - they just follow the pointers that you provide.
3831     Depending on the complexity of your application, and your personal
3832     taste, there are at least three different approaches that you may take
3833     when you allocate the structures.
3834    </para>
3835    <para>
3836     You can use static or automatic local variables in the function that
3837     prepares the PDU. This is a simple approach, and it provides the most
3838     efficient form of memory management. While it works well for flat
3839     PDUs like the InitReqest, it will generally not be sufficient for say,
3840     the generation of an arbitrarily complex RPN query structure.
3841    </para>
3842    <para>
3843     You can individually create the structure and its members using the
3844     <function>malloc(2)</function> function. If you want to ensure that
3845     the data is freed when it is no longer needed, you will have to
3846     define a function that individually releases each member of a
3847     structure before freeing the structure itself.
3848    </para>
3849    <para>
3850     You can use the <function>odr_malloc()</function> function (see
3851     <xref linkend="odr.use"/> for details). When you use
3852     <function>odr_malloc()</function>, you can release all of the
3853     allocated data in a single operation, independent of any pointers and
3854     relations between the data. <function>odr_malloc()</function> is based on a
3855     &quot;nibble-memory&quot;
3856     scheme, in which large portions of memory are allocated, and then
3857     gradually handed out with each call to <function>odr_malloc()</function>.
3858     The next time you call <function>odr_reset()</function>, all of the
3859     memory allocated since the last call is recycled for future use (actually,
3860     it is placed on a free-list).
3861    </para>
3862    <para>
3863     You can combine all of the methods described here. This will often be
3864     the most practical approach. For instance, you might use
3865     <function>odr_malloc()</function> to allocate an entire structure and
3866     some of its elements, while you leave other elements pointing to global
3867     or per-session default variables.
3868    </para>
3869    <para>
3870     The &asn; module provides an important aid in creating new PDUs. For
3871     each of the PDU types (say, <function>Z_InitRequest</function>), a
3872     function is provided that allocates and initializes an instance of
3873     that PDU type for you. In the case of the InitRequest, the function is
3874     simply named <function>zget_InitRequest()</function>, and it sets up
3875     reasonable default value for all of the mandatory members. The optional
3876     members are generally initialized to null pointers. This last aspect
3877     is very important: it ensures that if the PDU definitions are
3878     extended after you finish your implementation (to accommodate
3879     new versions of the protocol, say), you won't get into trouble with
3880     uninitialized pointers in your structures. The functions use
3881     <function>odr_malloc()</function> to
3882     allocate the PDUs and its members, so you can free everything again with a
3883     single call to <function>odr_reset()</function>. We strongly recommend
3884     that you use the <literal>zget_*</literal>
3885     functions whenever you are preparing a PDU (in a C++ API, the
3886     <literal>zget_</literal>
3887     functions would probably be promoted to constructors for the
3888     individual types).
3889    </para>
3890    <para>
3891    The prototype for the individual PDU types generally look like this:
3892    </para>
3893    <synopsis>
3894     Z_&lt;type> *zget_&lt;type>(ODR o);
3895    </synopsis>
3896    <para>
3897     eg.:
3898    </para>
3899    <synopsis>
3900     Z_InitRequest *zget_InitRequest(ODR o);
3901    </synopsis>
3902    <para>
3903    The &odr; handle should generally be your encoding stream, but it
3904     needn't be.
3905    </para>
3906    <para>
3907     As well as the individual PDU functions, a function
3908     <function>zget_APDU()</function> is provided, which allocates
3909     a top-level Z-APDU of the type requested:
3910    </para>
3911    <synopsis>
3912     Z_APDU *zget_APDU(ODR o, int which);
3913    </synopsis>
3914    <para>
3915     The <varname>which</varname> parameter is (of course) the discriminator
3916     belonging to the <varname>Z_APDU</varname> <literal>CHOICE</literal> type.
3917     All of the interface described here is provided by the &asn; module, and
3918     you access it through the <filename>proto.h</filename> header file.
3919    </para>
3920   </sect1>
3921   <sect1 id="asn.external">
3922    <title>EXTERNAL Data</title>
3923    <para>
3924     In order to achieve extensibility and adaptability to different
3925     application domains, the new version of the protocol defines many
3926     structures outside of the main ASN.1 specification, referencing them
3927     through ASN.1 EXTERNAL constructs. To simplify the construction and
3928     access to the externally referenced data, the &asn; module defines a
3929     specialized version of the EXTERNAL construct, called
3930     <literal>Z_External</literal>.It is defined thus:
3931    </para>
3932    <screen>
3933 typedef struct Z_External
3934 {
3935     Odr_oid *direct_reference;
3936     int *indirect_reference;
3937     char *descriptor;
3938     enum
3939     {
3940         /* Generic types */
3941         Z_External_single = 0,
3942         Z_External_octet,
3943         Z_External_arbitrary,
3944
3945         /* Specific types */
3946         Z_External_SUTRS,
3947         Z_External_explainRecord,
3948         Z_External_resourceReport1,
3949         Z_External_resourceReport2
3950
3951     ...
3952
3953     } which;
3954     union
3955     {
3956         /* Generic types */
3957         Odr_any *single_ASN1_type;
3958         Odr_oct *octet_aligned;
3959         Odr_bitmask *arbitrary;
3960
3961         /* Specific types */
3962         Z_SUTRS *sutrs;
3963         Z_ExplainRecord *explainRecord;
3964         Z_ResourceReport1 *resourceReport1;
3965         Z_ResourceReport2 *resourceReport2;
3966
3967         ...
3968
3969     } u;
3970 } Z_External;
3971    </screen>
3972    <para>
3973     When decoding, the &asn; module will attempt to determine which
3974     syntax describes the data by looking at the reference fields
3975     (currently only the direct-reference). For ASN.1 structured data, you
3976     need only consult the <literal>which</literal> field to determine the
3977     type of data. You can the access  the data directly through the union.
3978     When constructing data for encoding, you set the union pointer to point
3979     to the data, and set the <literal>which</literal> field accordingly.
3980     Remember also to set the direct (or indirect) reference to the correct
3981     OID for the data type.
3982     For non-ASN.1 data such as MARC records, use the
3983     <literal>octet_aligned</literal> arm of the union.
3984    </para>
3985    <para>
3986     Some servers return ASN.1 structured data values (eg. database
3987     records) as BER-encoded records placed in the
3988     <literal>octet-aligned</literal> branch of the EXTERNAL CHOICE.
3989     The ASN-module will <emphasis>not</emphasis> automatically decode
3990     these records. To help you decode the records in the application, the
3991     function
3992    </para>
3993    <screen>
3994    Z_ext_typeent *z_ext_gettypebyref(const oid *oid);
3995    </screen>
3996    <para>
3997     Can be used to retrieve information about the known, external data
3998     types. The function return a pointer to a static area, or NULL, if no
3999     match for the given direct reference is found. The
4000     <literal>Z_ext_typeent</literal>
4001     is defined as:
4002    </para>
4003    <screen>
4004 typedef struct Z_ext_typeent
4005 {
4006     int oid[OID_SIZE]; /* the direct-reference OID. */
4007     int what;          /* discriminator value for the external CHOICE */
4008     Odr_fun fun;       /* decoder function */
4009 } Z_ext_typeent;
4010    </screen>
4011    <para>
4012     The <literal>what</literal> member contains the
4013     <literal>Z_External</literal> union discriminator value for the
4014     given type: For the SUTRS record syntax, the value would be
4015     <literal>Z_External_sutrs</literal>.
4016     The <literal>fun</literal> member contains a pointer to the
4017     function which encodes/decodes the given type. Again, for the SUTRS
4018     record syntax, the value of <literal>fun</literal> would be
4019     <literal>z_SUTRS</literal> (a function pointer).
4020    </para>
4021    <para>
4022     If you receive an EXTERNAL which contains an octet-string value that
4023     you suspect of being an ASN.1-structured data value, you can use
4024     <literal>z_ext_gettypebyref</literal> to look for the provided
4025     direct-reference.
4026     If the return value is different from NULL, you can use the provided
4027     function to decode the BER string (see <xref linkend="odr.use"/>
4028     ).
4029    </para>
4030    <para>
4031     If you want to <emphasis>send</emphasis> EXTERNALs containing
4032     ASN.1-structured values in the occtet-aligned branch of the CHOICE, this
4033     is possible too. However, on the encoding phase, it requires a somewhat
4034     involved juggling around of the various buffers involved.
4035    </para>
4036    <para>
4037     If you need to add new, externally defined data types, you must update
4038     the struct above, in the source file <filename>prt-ext.h</filename>, as
4039     well as the encoder/decoder in the file <filename>prt-ext.c</filename>.
4040     When changing the latter, remember to update both the
4041     <literal>arm</literal> arrary and the list
4042     <literal>type_table</literal>, which drives the CHOICE biasing that
4043     is necessary to tell the different, structured types apart
4044     on decoding.
4045    </para>
4046    <note>
4047     <para>
4048      Eventually, the EXTERNAL processing will most likely
4049      automatically insert the correct OIDs or indirect-refs. First,
4050      however, we need to determine how application-context management
4051      (specifically the presentation-context-list) should fit into the
4052      various modules.
4053     </para>
4054    </note>
4055   </sect1>
4056   <sect1 id="asn.pdu">
4057    <title>PDU Contents Table</title>
4058    <para>
4059     We include, for reference, a listing of the fields of each top-level
4060     PDU, as well as their default settings.
4061    </para>
4062    <table frame="top" id="asn.default.initialize.request">
4063     <title>Default settings for PDU Initialize Request</title>
4064     <tgroup cols="3">
4065      <colspec colwidth="7*" colname="field"></colspec>
4066      <colspec colwidth="5*" colname="type"></colspec>
4067      <colspec colwidth="7*" colname="value"></colspec>
4068     <thead>
4069      <row>
4070       <entry>Field</entry>
4071       <entry>Type</entry>
4072       <entry>Default Value</entry>
4073      </row>
4074     </thead>
4075     <tbody>
4076      <row><entry>
4077       referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4078      </entry></row>
4079      <row><entry>
4080       protocolVersion</entry><entry>Odr_bitmask</entry><entry>Empty bitmask
4081      </entry></row>
4082      <row><entry>
4083       options</entry><entry>Odr_bitmask</entry><entry>Empty bitmask
4084      </entry></row>
4085      <row><entry>
4086       preferredMessageSize</entry><entry>Odr_int</entry><entry>30*1024
4087      </entry></row>
4088      <row><entry>
4089       maximumRecordSize</entry><entry>Odr_int</entry><entry>30*1024
4090      </entry></row>
4091      <row><entry>
4092       idAuthentication</entry><entry>Z_IdAuthentication</entry><entry>NULL
4093      </entry></row>
4094      <row><entry>
4095       implementationId</entry><entry>char*</entry><entry>"81"
4096      </entry></row>
4097      <row><entry>
4098       implementationName</entry><entry>char*</entry><entry>"YAZ"
4099      </entry></row>
4100      <row><entry>
4101       implementationVersion</entry><entry>char*</entry><entry>YAZ_VERSION
4102      </entry></row>
4103      <row><entry>
4104       userInformationField</entry><entry>Z_UserInformation</entry><entry>NULL
4105      </entry></row>
4106      <row><entry>
4107       otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4108      </entry></row>
4109     </tbody>
4110     </tgroup>
4111    </table>
4112    <table frame="top" id="asn.default.initialize.response">
4113     <title>Default settings for PDU Initialize Response</title>
4114     <tgroup cols="3">
4115      <colspec colwidth="7*" colname="field"></colspec>
4116      <colspec colwidth="5*" colname="type"></colspec>
4117      <colspec colwidth="7*" colname="value"></colspec>
4118      <thead>
4119       <row>
4120        <entry>Field</entry>
4121        <entry>Type</entry>
4122        <entry>Default Value</entry>
4123       </row>
4124      </thead>
4125      <tbody>
4126       <row><entry>
4127        referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4128       </entry></row>
4129       <row><entry>
4130        protocolVersion</entry><entry>Odr_bitmask</entry><entry>Empty bitmask
4131       </entry></row>
4132       <row><entry>
4133        options</entry><entry>Odr_bitmask</entry><entry>Empty bitmask
4134       </entry></row>
4135       <row><entry>
4136        preferredMessageSize</entry><entry>Odr_int</entry><entry>30*1024
4137       </entry></row>
4138       <row><entry>
4139        maximumRecordSize</entry><entry>Odr_int</entry><entry>30*1024
4140       </entry></row>
4141       <row><entry>
4142        result</entry><entry>Odr_bool</entry><entry>TRUE
4143       </entry></row>
4144       <row><entry>
4145        implementationId</entry><entry>char*</entry><entry>"id)"
4146       </entry></row>
4147       <row><entry>
4148        implementationName</entry><entry>char*</entry><entry>"YAZ"
4149       </entry></row>
4150       <row><entry>
4151        implementationVersion</entry><entry>char*</entry><entry>YAZ_VERSION
4152       </entry></row>
4153       <row><entry>
4154        userInformationField</entry><entry>Z_UserInformation</entry><entry>NULL
4155       </entry></row>
4156       <row><entry>
4157        otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4158       </entry></row>
4159      </tbody>
4160     </tgroup>
4161    </table>
4162    <table frame="top" id="asn.default.search.request">
4163     <title>Default settings for PDU Search Request</title>
4164     <tgroup cols="3">
4165      <colspec colwidth="7*" colname="field"></colspec>
4166      <colspec colwidth="5*" colname="type"></colspec>
4167      <colspec colwidth="7*" colname="value"></colspec>
4168      <thead>
4169       <row>
4170        <entry>Field</entry>
4171        <entry>Type</entry>
4172        <entry>Default Value</entry>
4173       </row>
4174      </thead>
4175      <tbody>
4176       <row><entry>
4177         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4178        </entry></row>
4179       <row><entry>
4180         smallSetUpperBound</entry><entry>Odr_int</entry><entry>0
4181        </entry></row>
4182       <row><entry>
4183         largeSetLowerBound</entry><entry>Odr_int</entry><entry>1
4184        </entry></row>
4185       <row><entry>
4186         mediumSetPresentNumber</entry><entry>Odr_int</entry><entry>0
4187        </entry></row>
4188       <row><entry>
4189         replaceIndicator</entry><entry>Odr_bool</entry><entry>TRUE
4190        </entry></row>
4191       <row><entry>
4192         resultSetName</entry><entry>char *</entry><entry>"default"
4193        </entry></row>
4194       <row><entry>
4195         num_databaseNames</entry><entry>Odr_int</entry><entry>0
4196        </entry></row>
4197       <row><entry>
4198         databaseNames</entry><entry>char **</entry><entry>NULL
4199        </entry></row>
4200       <row><entry>
4201         smallSetElementSetNames</entry><entry>Z_ElementSetNames
4202        </entry><entry>NULL
4203        </entry></row>
4204       <row><entry>
4205         mediumSetElementSetNames</entry><entry>Z_ElementSetNames
4206        </entry><entry>NULL
4207        </entry></row>
4208       <row><entry>
4209         preferredRecordSyntax</entry><entry>Odr_oid</entry><entry>NULL
4210        </entry></row>
4211       <row><entry>
4212         query</entry><entry>Z_Query</entry><entry>NULL
4213        </entry></row>
4214       <row><entry>
4215         additionalSearchInfo</entry><entry>Z_OtherInformation
4216        </entry><entry>NULL
4217        </entry></row>
4218       <row><entry>
4219         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4220        </entry></row>
4221      </tbody>
4222     </tgroup>
4223    </table>
4224    <table frame="top" id="asn.default.search.response">
4225     <title>Default settings for PDU Search Response</title>
4226     <tgroup cols="3">
4227      <colspec colwidth="7*" colname="field"></colspec>
4228      <colspec colwidth="5*" colname="type"></colspec>
4229      <colspec colwidth="7*" colname="value"></colspec>
4230      <thead>
4231       <row>
4232        <entry>Field</entry>
4233        <entry>Type</entry>
4234        <entry>Default Value</entry>
4235       </row>
4236      </thead>
4237      <tbody>
4238       <row><entry>
4239        referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4240       </entry></row>
4241       <row><entry>
4242        resultCount</entry><entry>Odr_int</entry><entry>0
4243       </entry></row>
4244       <row><entry>
4245        numberOfRecordsReturned</entry><entry>Odr_int</entry><entry>0
4246       </entry></row>
4247       <row><entry>
4248        nextResultSetPosition</entry><entry>Odr_int</entry><entry>0
4249       </entry></row>
4250       <row><entry>
4251        searchStatus</entry><entry>Odr_bool</entry><entry>TRUE
4252       </entry></row>
4253       <row><entry>
4254        resultSetStatus</entry><entry>Odr_int</entry><entry>NULL
4255       </entry></row>
4256       <row><entry>
4257        presentStatus</entry><entry>Odr_int</entry><entry>NULL
4258       </entry></row>
4259       <row><entry>
4260        records</entry><entry>Z_Records</entry><entry>NULL
4261       </entry></row>
4262       <row><entry>
4263        additionalSearchInfo</entry>
4264        <entry>Z_OtherInformation</entry><entry>NULL
4265       </entry></row>
4266       <row><entry>
4267        otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4268       </entry></row>
4269      </tbody>
4270     </tgroup>
4271    </table>
4272    <table frame="top" id="asn.default.present.request">
4273     <title>Default settings for PDU Present Request</title>
4274     <tgroup cols="3">
4275      <colspec colwidth="7*" colname="field"></colspec>
4276      <colspec colwidth="5*" colname="type"></colspec>
4277      <colspec colwidth="7*" colname="value"></colspec>
4278      <thead>
4279       <row>
4280        <entry>Field</entry>
4281        <entry>Type</entry>
4282        <entry>Default Value</entry>
4283       </row>
4284      </thead>
4285      <tbody>
4286       <row><entry>
4287         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4288        </entry></row>
4289       <row><entry>
4290         resultSetId</entry><entry>char*</entry><entry>"default"
4291        </entry></row>
4292       <row><entry>
4293         resultSetStartPoint</entry><entry>Odr_int</entry><entry>1
4294        </entry></row>
4295       <row><entry>
4296         numberOfRecordsRequested</entry><entry>Odr_int</entry><entry>10
4297        </entry></row>
4298       <row><entry>
4299         num_ranges</entry><entry>Odr_int</entry><entry>0
4300        </entry></row>
4301       <row><entry>
4302         additionalRanges</entry><entry>Z_Range</entry><entry>NULL
4303        </entry></row>
4304       <row><entry>
4305         recordComposition</entry><entry>Z_RecordComposition</entry><entry>NULL
4306        </entry></row>
4307       <row><entry>
4308         preferredRecordSyntax</entry><entry>Odr_oid</entry><entry>NULL
4309        </entry></row>
4310       <row><entry>
4311         maxSegmentCount</entry><entry>Odr_int</entry><entry>NULL
4312        </entry></row>
4313       <row><entry>
4314         maxRecordSize</entry><entry>Odr_int</entry><entry>NULL
4315        </entry></row>
4316       <row><entry>
4317         maxSegmentSize</entry><entry>Odr_int</entry><entry>NULL
4318        </entry></row>
4319       <row><entry>
4320         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4321        </entry></row>
4322      </tbody>
4323     </tgroup>
4324    </table>
4325    <table frame="top" id="asn.default.present.response">
4326     <title>Default settings for PDU Present Response</title>
4327     <tgroup cols="3">
4328      <colspec colwidth="7*" colname="field"></colspec>
4329      <colspec colwidth="5*" colname="type"></colspec>
4330      <colspec colwidth="7*" colname="value"></colspec>
4331      <thead>
4332       <row>
4333        <entry>Field</entry>
4334        <entry>Type</entry>
4335        <entry>Default Value</entry>
4336       </row>
4337      </thead>
4338      <tbody>
4339       <row><entry>
4340         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4341        </entry></row>
4342       <row><entry>
4343         numberOfRecordsReturned</entry><entry>Odr_int</entry><entry>0
4344        </entry></row>
4345       <row><entry>
4346         nextResultSetPosition</entry><entry>Odr_int</entry><entry>0
4347        </entry></row>
4348       <row><entry>
4349         presentStatus</entry><entry>Odr_int</entry><entry>Z_PresentStatus_success
4350        </entry></row>
4351       <row><entry>
4352         records</entry><entry>Z_Records</entry><entry>NULL
4353        </entry></row>
4354       <row><entry>
4355         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4356        </entry></row>
4357      </tbody>
4358     </tgroup>
4359    </table>
4360    <table frame="top" id="asn.default.delete.result.set.request">
4361     <title>Default settings for Delete Result Set Request</title>
4362     <tgroup cols="3">
4363      <colspec colwidth="7*" colname="field"></colspec>
4364      <colspec colwidth="5*" colname="type"></colspec>
4365      <colspec colwidth="7*" colname="value"></colspec>
4366      <thead>
4367       <row>
4368        <entry>Field</entry>
4369        <entry>Type</entry>
4370        <entry>Default Value</entry>
4371       </row>
4372      </thead>
4373      <tbody>
4374       <row><entry>referenceId
4375        </entry><entry>Z_ReferenceId</entry><entry>NULL
4376        </entry></row>
4377       <row><entry>
4378         deleteFunction</entry><entry>Odr_int</entry><entry>Z_DeleteResultSetRequest_list
4379        </entry></row>
4380       <row><entry>
4381         num_ids</entry><entry>Odr_int</entry><entry>0
4382        </entry></row>
4383       <row><entry>
4384         resultSetList</entry><entry>char**</entry><entry>NULL
4385        </entry></row>
4386       <row><entry>
4387         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4388        </entry></row>
4389      </tbody>
4390     </tgroup>
4391    </table>
4392    <table frame="top" id="asn.default.delete.result.set.response">
4393     <title>Default settings for Delete Result Set Response</title>
4394     <tgroup cols="3">
4395      <colspec colwidth="7*" colname="field"></colspec>
4396      <colspec colwidth="5*" colname="type"></colspec>
4397      <colspec colwidth="7*" colname="value"></colspec>
4398      <thead>
4399       <row>
4400        <entry>Field</entry>
4401        <entry>Type</entry>
4402        <entry>Default Value</entry>
4403       </row>
4404      </thead>
4405      <tbody>
4406       <row><entry>
4407         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4408        </entry></row>
4409       <row><entry>
4410         deleteOperationStatus</entry><entry>Odr_int</entry>
4411        <entry>Z_DeleteStatus_success</entry></row>
4412       <row><entry>
4413         num_statuses</entry><entry>Odr_int</entry><entry>0
4414        </entry></row>
4415       <row><entry>
4416         deleteListStatuses</entry><entry>Z_ListStatus**</entry><entry>NULL
4417        </entry></row>
4418       <row><entry>
4419         numberNotDeleted</entry><entry>Odr_int</entry><entry>NULL
4420        </entry></row>
4421       <row><entry>
4422         num_bulkStatuses</entry><entry>Odr_int</entry><entry>0
4423        </entry></row>
4424       <row><entry>
4425         bulkStatuses</entry><entry>Z_ListStatus</entry><entry>NUL
4426         L</entry></row>
4427       <row><entry>
4428         deleteMessage</entry><entry>char*</entry><entry>NULL
4429        </entry></row>
4430       <row><entry>
4431         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4432        </entry></row>
4433      </tbody>
4434     </tgroup>
4435    </table>
4436    <table frame="top" id="asn.default.scan.request">
4437     <title>Default settings for Scan Request</title>
4438     <tgroup cols="3">
4439      <colspec colwidth="7*" colname="field"></colspec>
4440      <colspec colwidth="5*" colname="type"></colspec>
4441      <colspec colwidth="7*" colname="value"></colspec>
4442      <thead>
4443       <row>
4444        <entry>Field</entry>
4445        <entry>Type</entry>
4446        <entry>Default Value</entry>
4447       </row>
4448      </thead>
4449      <tbody>
4450       <row><entry>
4451         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4452        </entry></row>
4453       <row><entry>
4454         num_databaseNames</entry><entry>Odr_int</entry><entry>0
4455        </entry></row>
4456       <row><entry>
4457         databaseNames</entry><entry>char**</entry><entry>NULL
4458        </entry></row>
4459       <row><entry>
4460         attributeSet</entry><entry>Odr_oid</entry><entry>NULL
4461        </entry></row>
4462       <row><entry>
4463         termListAndStartPoint</entry><entry>Z_AttributesPlus...
4464        </entry><entry>NULL</entry></row>
4465       <row><entry>
4466         stepSize</entry><entry>Odr_int</entry><entry>NULL
4467        </entry></row>
4468       <row><entry>
4469         numberOfTermsRequested</entry><entry>Odr_int</entry><entry>20
4470        </entry></row>
4471       <row><entry>
4472         preferredPositionInResponse</entry><entry>Odr_int</entry><entry>NULL
4473        </entry></row>
4474       <row><entry>
4475         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4476        </entry></row>
4477      </tbody>
4478     </tgroup>
4479    </table>
4480    <table frame="top" id="asn.default.scan.response">
4481     <title>Default settings for Scan Response</title>
4482     <tgroup cols="3">
4483      <colspec colwidth="7*" colname="field"></colspec>
4484      <colspec colwidth="5*" colname="type"></colspec>
4485      <colspec colwidth="7*" colname="value"></colspec>
4486      <thead>
4487       <row>
4488        <entry>Field</entry>
4489        <entry>Type</entry>
4490        <entry>Default Value</entry>
4491       </row>
4492      </thead>
4493      <tbody>
4494       <row><entry>
4495        referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4496       </entry></row>
4497       <row><entry>
4498        stepSize</entry><entry>Odr_int</entry><entry>NULL
4499       </entry></row>
4500       <row><entry>
4501        scanStatus</entry><entry>Odr_int</entry><entry>Z_Scan_success
4502       </entry></row>
4503       <row><entry>
4504        numberOfEntriesReturned</entry><entry>Odr_int</entry><entry>0
4505       </entry></row>
4506       <row><entry>
4507        positionOfTerm</entry><entry>Odr_int</entry><entry>NULL
4508       </entry></row>
4509       <row><entry>
4510        entries</entry><entry>Z_ListEntris</entry><entry>NULL
4511       </entry></row>
4512       <row><entry>
4513        attributeSet</entry><entry>Odr_oid</entry><entry>NULL
4514       </entry></row>
4515       <row><entry>
4516        otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4517       </entry></row>
4518      </tbody>
4519     </tgroup>
4520    </table>
4521    <table frame="top" id="asn.default.trigger.resource.control.request">
4522     <title>Default settings for Trigger Resource Control Request</title>
4523     <tgroup cols="3">
4524      <colspec colwidth="7*" colname="field"></colspec>
4525      <colspec colwidth="5*" colname="type"></colspec>
4526      <colspec colwidth="7*" colname="value"></colspec>
4527      <thead>
4528       <row>
4529        <entry>Field</entry>
4530        <entry>Type</entry>
4531        <entry>Default Value</entry>
4532       </row>
4533      </thead>
4534      <tbody>
4535       <row><entry>
4536         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4537        </entry></row>
4538       <row><entry>
4539         requestedAction</entry><entry>Odr_int</entry><entry>
4540         Z_TriggerResourceCtrl_resou..
4541        </entry></row>
4542       <row><entry>
4543         prefResourceReportFormat</entry><entry>Odr_oid</entry><entry>NULL
4544        </entry></row>
4545       <row><entry>
4546         resultSetWanted</entry><entry>Odr_bool</entry><entry>NULL
4547        </entry></row>
4548       <row><entry>
4549         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4550        </entry></row>
4551      </tbody>
4552     </tgroup>
4553    </table>
4554    <table frame="top" id="asn.default.resource.control.request">
4555     <title>Default settings for Resource Control Request</title>
4556     <tgroup cols="3">
4557      <colspec colwidth="7*" colname="field"></colspec>
4558      <colspec colwidth="5*" colname="type"></colspec>
4559      <colspec colwidth="7*" colname="value"></colspec>
4560      <thead>
4561       <row>
4562        <entry>Field</entry>
4563        <entry>Type</entry>
4564        <entry>Default Value</entry>
4565       </row>
4566      </thead>
4567      <tbody>
4568       <row><entry>
4569         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4570        </entry></row>
4571       <row><entry>
4572         suspendedFlag</entry><entry>Odr_bool</entry><entry>NULL
4573        </entry></row>
4574       <row><entry>
4575         resourceReport</entry><entry>Z_External</entry><entry>NULL
4576        </entry></row>
4577       <row><entry>
4578         partialResultsAvailable</entry><entry>Odr_int</entry><entry>NULL
4579        </entry></row>
4580       <row><entry>
4581         responseRequired</entry><entry>Odr_bool</entry><entry>FALSE
4582        </entry></row>
4583       <row><entry>
4584         triggeredRequestFlag</entry><entry>Odr_bool</entry><entry>NULL
4585        </entry></row>
4586       <row><entry>
4587         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4588        </entry></row>
4589      </tbody>
4590     </tgroup>
4591    </table>
4592    <table frame="top" id="asn.default.resource.control.response">
4593     <title>Default settings for Resource Control Response</title>
4594     <tgroup cols="3">
4595      <colspec colwidth="7*" colname="field"></colspec>
4596      <colspec colwidth="5*" colname="type"></colspec>
4597      <colspec colwidth="7*" colname="value"></colspec>
4598      <thead>
4599       <row>
4600        <entry>Field</entry>
4601        <entry>Type</entry>
4602        <entry>Default Value</entry>
4603       </row>
4604      </thead>
4605      <tbody>
4606       <row><entry>
4607         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4608        </entry></row>
4609       <row><entry>
4610         continueFlag</entry><entry>bool_t</entry><entry>TRUE
4611        </entry></row>
4612       <row><entry>
4613         resultSetWanted</entry><entry>bool_t</entry><entry>NULL
4614        </entry></row>
4615       <row><entry>
4616         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4617        </entry></row>
4618      </tbody>
4619     </tgroup>
4620    </table>
4621    <table frame="top" id="asn.default.access.control.request">
4622     <title>Default settings for Access Control Request</title>
4623     <tgroup cols="3">
4624      <colspec colwidth="7*" colname="field"></colspec>
4625      <colspec colwidth="5*" colname="type"></colspec>
4626      <colspec colwidth="7*" colname="value"></colspec>
4627      <thead>
4628       <row>
4629        <entry>Field</entry>
4630        <entry>Type</entry>
4631        <entry>Default Value</entry>
4632       </row>
4633      </thead>
4634      <tbody>
4635       <row><entry>
4636         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4637        </entry></row>
4638       <row><entry>
4639         which</entry><entry>enum</entry><entry>Z_AccessRequest_simpleForm;
4640        </entry></row>
4641       <row><entry>
4642         u</entry><entry>union</entry><entry>NULL
4643        </entry></row>
4644       <row><entry>
4645         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4646        </entry></row>
4647      </tbody>
4648     </tgroup>
4649    </table>
4650    <table frame="top" id="asn.default.access.control.response">
4651     <title>Default settings for Access Control Response</title>
4652     <tgroup cols="3">
4653      <colspec colwidth="7*" colname="field"></colspec>
4654      <colspec colwidth="5*" colname="type"></colspec>
4655      <colspec colwidth="7*" colname="value"></colspec>
4656      <thead>
4657       <row>
4658        <entry>Field</entry>
4659        <entry>Type</entry>
4660        <entry>Default Value</entry>
4661       </row>
4662      </thead>
4663      <tbody>
4664       <row><entry>
4665         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4666        </entry></row>
4667       <row><entry>
4668         which</entry><entry>enum</entry><entry>Z_AccessResponse_simpleForm
4669        </entry></row>
4670       <row><entry>
4671         u</entry><entry>union</entry><entry>NULL
4672        </entry></row>
4673       <row><entry>
4674         diagnostic</entry><entry>Z_DiagRec</entry><entry>NULL
4675        </entry></row>
4676       <row><entry>
4677         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4678        </entry></row>
4679      </tbody>
4680     </tgroup>
4681    </table>
4682    <table frame="top" id="asn.default.segment">
4683     <title>Default settings for Segment</title>
4684     <tgroup cols="3">
4685      <colspec colwidth="7*" colname="field"></colspec>
4686      <colspec colwidth="5*" colname="type"></colspec>
4687      <colspec colwidth="7*" colname="value"></colspec>
4688      <thead>
4689       <row>
4690        <entry>Field</entry>
4691        <entry>Type</entry>
4692        <entry>Default Value</entry>
4693       </row>
4694      </thead>
4695      <tbody>
4696       <row><entry>
4697         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4698        </entry></row>
4699       <row><entry>
4700         numberOfRecordsReturned</entry><entry>Odr_int</entry><entry>value=0
4701        </entry></row>
4702       <row><entry>
4703         num_segmentRecords</entry><entry>Odr_int</entry><entry>0
4704        </entry></row>
4705       <row><entry>
4706         segmentRecords</entry><entry>Z_NamePlusRecord</entry><entry>NULL
4707        </entry></row>
4708       <row><entry>otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4709        </entry></row>
4710      </tbody>
4711     </tgroup>
4712    </table>
4713    <table frame="top" id="asn.default.close">
4714     <title>Default settings for Close</title>
4715     <tgroup cols="3">
4716      <colspec colwidth="7*" colname="field"></colspec>
4717      <colspec colwidth="5*" colname="type"></colspec>
4718      <colspec colwidth="7*" colname="value"></colspec>
4719      <thead>
4720       <row>
4721        <entry>Field</entry>
4722        <entry>Type</entry>
4723        <entry>Default Value</entry>
4724       </row>
4725      </thead>
4726      <tbody>
4727       <row><entry>
4728         referenceId</entry><entry>Z_ReferenceId</entry><entry>NULL
4729        </entry></row>
4730       <row><entry>
4731         closeReason</entry><entry>Odr_int</entry><entry>Z_Close_finished
4732        </entry></row>
4733       <row><entry>
4734         diagnosticInformation</entry><entry>char*</entry><entry>NULL
4735        </entry></row>
4736       <row><entry>
4737         resourceReportFormat</entry><entry>Odr_oid</entry><entry>NULL
4738        </entry></row>
4739       <row><entry>
4740         resourceFormat</entry><entry>Z_External</entry><entry>NULL
4741        </entry></row>
4742       <row><entry>
4743         otherInfo</entry><entry>Z_OtherInformation</entry><entry>NULL
4744        </entry></row>
4745      </tbody>
4746     </tgroup>
4747    </table>
4748   </sect1>
4749  </chapter>
4750  <chapter id="soap">
4751   <title>SOAP and SRU</title>
4752   <sect1 id="soap.introduction">
4753    <title>Introduction</title>
4754    <para>
4755     &yaz; uses a very simple implementation of
4756     <ulink url="&url.soap;">SOAP</ulink> that only,
4757     currenly, supports what is sufficient to offer SRU SOAP functionality.
4758     The implementation uses the
4759     <ulink url="&url.libxml2.api.tree;">tree API</ulink> of
4760     libxml2 to encode and decode SOAP packages.
4761    </para>
4762    <para>
4763     Like the Z39.50 ASN.1 module, the &yaz; SRU implementation uses
4764     simple C structs to represent SOAP packages as well as
4765     HTTP packages.
4766    </para>
4767   </sect1>
4768   <sect1 id="soap.http">
4769    <title>HTTP</title>
4770    <para>
4771     &yaz; only offers HTTP as transport carrier for SOAP, but it is
4772     relatively easy to change that.
4773    </para>
4774    <para>
4775     The following definition of <literal>Z_GDU</literal> (Generic Data
4776     Unit) allows for both HTTP and Z39.50 in one packet.
4777    </para>
4778    <synopsis>
4779 #include &lt;yaz/zgdu.h&gt;
4780
4781 #define Z_GDU_Z3950         1
4782 #define Z_GDU_HTTP_Request  2
4783 #define Z_GDU_HTTP_Response 3
4784 typedef struct {
4785   int which;
4786   union {
4787     Z_APDU *z3950;
4788     Z_HTTP_Request *HTTP_Request;
4789     Z_HTTP_Response *HTTP_Response;
4790   } u;
4791 } Z_GDU ;
4792    </synopsis>
4793    <para>
4794     The corresponding Z_GDU encoder/decoder is <function>z_GDU</function>.
4795     The <literal>z3950</literal> is any of the known BER encoded Z39.50
4796     APDUs.
4797     <literal>HTTP_Request</literal> and <literal>HTTP_Response</literal>
4798     is the HTTP Request and Response respectively.
4799    </para>
4800   </sect1>
4801   <sect1 id="soap.xml">
4802    <title>SOAP Packages</title>
4803    <para>
4804     Every SOAP package in &yaz; is represented as follows:
4805     <synopsis>
4806 #include &lt;yaz/soap.h&gt;
4807
4808 typedef struct {
4809     char *fault_code;
4810     char *fault_string;
4811     char *details;
4812 } Z_SOAP_Fault;
4813
4814 typedef struct {
4815     int no;
4816     char *ns;
4817     void *p;
4818 } Z_SOAP_Generic;
4819
4820 #define Z_SOAP_fault 1
4821 #define Z_SOAP_generic 2
4822 #define Z_SOAP_error 3
4823 typedef struct {
4824     int which;
4825     union {
4826         Z_SOAP_Fault   *fault;
4827         Z_SOAP_Generic *generic;
4828         Z_SOAP_Fault   *soap_error;
4829     } u;
4830     const char *ns;
4831 } Z_SOAP;
4832     </synopsis>
4833    </para>
4834    <para>
4835     The <literal>fault</literal> and <literal>soap_error</literal>
4836     arms represent both a SOAP fault - struct
4837     <literal>Z_SOAP_Fault</literal>. Any other generic
4838     (valid) package is represented by <literal>Z_SOAP_Generic</literal>.
4839    </para>
4840    <para>
4841     The <literal>ns</literal> as part of <literal>Z_SOAP</literal>
4842     is the namespace for SOAP itself and reflects the SOAP
4843     version. For version 1.1 it is
4844     <literal>http://schemas.xmlsoap.org/soap/envelope/</literal>,
4845     for version 1.2 it is
4846     <literal>http://www.w3.org/2001/06/soap-envelope</literal>.
4847    </para>
4848    <synopsis>
4849 int z_soap_codec(ODR o, Z_SOAP **pp,
4850                  char **content_buf, int *content_len,
4851                  Z_SOAP_Handler *handlers);
4852    </synopsis>
4853    <para>
4854     The <literal>content_buf</literal> and <literal>content_len</literal>
4855     is XML buffer and length of buffer respectively.
4856    </para>
4857    <para>
4858     The <literal>handlers</literal> is a list of SOAP codec
4859     handlers - one handler for each service namespace. For SRU SOAP, the
4860     namespace would be <literal>http://www.loc.gov/zing/srw/v1.0/</literal>.
4861    </para>
4862    <para>
4863     When decoding, the <function>z_soap_codec</function>
4864     inspects the XML content
4865     and tries to match one of the services namespaces of the
4866     supplied handlers. If there is a match a handler function
4867     is invoked which decodes that particular SOAP package.
4868     If successful, the returned <literal>Z_SOAP</literal> package will be
4869     of type <literal>Z_SOAP_Generic</literal>.
4870     Member <literal>no</literal> is
4871     set the offset of handler that matched; <literal>ns</literal>
4872     is set to namespace of matching handler; the void pointer
4873     <literal>p</literal> is set to the C data structure assocatiated
4874     with the handler.
4875    </para>
4876    <para>
4877     When a NULL namespace is met (member <literal>ns</literal> bwlow),
4878     that specifies end-of-list.
4879    </para>
4880    <para>
4881     Each handler is defined as follows:
4882     <synopsis>
4883 typedef struct {
4884     char *ns;
4885     void *client_data;
4886     Z_SOAP_fun f;
4887 } Z_SOAP_Handler;
4888     </synopsis>
4889     The <literal>ns</literal> is namespace of service associated with
4890     handler <literal>f</literal>. <literal>client_data</literal>
4891     is user-defined data which is passed to handler.
4892    </para>
4893    <para>
4894     The prototype for a SOAP service handler is:
4895     <synopsis>
4896 int handler(ODR o, void * ptr, void **handler_data,
4897             void *client_data, const char *ns);
4898     </synopsis>
4899     The <parameter>o</parameter> specifies the mode (decode/encode)
4900     as usual. The second argument, <parameter>ptr</parameter>,
4901     is a libxml2 tree node pointer (<literal>xmlNodePtr</literal>)
4902     and is a pointer to the <literal>Body</literal> element
4903     of the SOAP package. The <parameter>handler_data</parameter>
4904     is an opaque pointer to a C definitions associated with the
4905     SOAP service. <parameter>client_data</parameter> is the pointer
4906     which was set as part of the <literal>Z_SOAP_handler</literal>.
4907     Finally, <parameter>ns</parameter> the service namespace.
4908    </para>
4909   </sect1>
4910   <sect1 id="soap.srw">
4911    <title>SRU</title>
4912    <para>
4913     SRU SOAP is just one implementation of a SOAP handler as described
4914     in the previous section.
4915     The encoder/decoder handler for SRU is defined as
4916     follows:
4917     <synopsis>
4918 #include &lt;yaz/srw.h&gt;
4919
4920 int yaz_srw_codec(ODR o, void * pptr,
4921                   Z_SRW_GDU **handler_data,
4922                   void *client_data, const char *ns);
4923     </synopsis>
4924     Here, <literal>Z_SRW_GDU</literal> is either
4925     searchRetrieveRequest or a searchRetrieveResponse.
4926    </para>
4927    <note>
4928     <para>
4929      The xQuery and xSortKeys are not handled yet by
4930      the SRW implementation of &yaz;. Explain is also missing.
4931      Future versions of &yaz; will include these features.
4932     </para>
4933    </note>
4934    <para>
4935     The definition of searchRetrieveRequest is:
4936     <synopsis>
4937 typedef struct {
4938
4939 #define Z_SRW_query_type_cql  1
4940 #define Z_SRW_query_type_xcql 2
4941 #define Z_SRW_query_type_pqf  3
4942     int query_type;
4943     union {
4944         char *cql;
4945         char *xcql;
4946         char *pqf;
4947     } query;
4948
4949 #define Z_SRW_sort_type_none 1
4950 #define Z_SRW_sort_type_sort 2
4951 #define Z_SRW_sort_type_xSort 3
4952     int sort_type;
4953     union {
4954         char *none;
4955         char *sortKeys;
4956         char *xSortKeys;
4957     } sort;
4958     int  *startRecord;
4959     int  *maximumRecords;
4960     char *recordSchema;
4961     char *recordPacking;
4962     char *database;
4963 } Z_SRW_searchRetrieveRequest;
4964     </synopsis>
4965     Please observe that data of type xsd:string is represented
4966     as a char pointer (<literal>char *</literal>). A null pointer
4967     means that the element is absent.
4968     Data of type xsd:integer is representd as a pointer to
4969     an int (<literal>int *</literal>). Again, a null pointer
4970     us used for absent elements.
4971    </para>
4972    <para>
4973     The SearchRetrieveResponse has the following definition.
4974     <synopsis>
4975 typedef struct {
4976     int * numberOfRecords;
4977     char * resultSetId;
4978     int * resultSetIdleTime;
4979
4980     Z_SRW_record *records;
4981     int num_records;
4982
4983     Z_SRW_diagnostic *diagnostics;
4984     int num_diagnostics;
4985     int *nextRecordPosition;
4986 } Z_SRW_searchRetrieveResponse;
4987     </synopsis>
4988     The <literal>num_records</literal> and <literal>num_diagnostics</literal>
4989     is number of returned records and diagnostics respectively and also
4990     correspond to the "size of" arrays <literal>records</literal>
4991     and <literal>diagnostics</literal>.
4992    </para>
4993    <para>
4994     A retrieval record is defined as follows:
4995     <synopsis>
4996 typedef struct {
4997     char *recordSchema;
4998     char *recordData_buf;
4999     int recordData_len;
5000     int *recordPosition;
5001 } Z_SRW_record;
5002     </synopsis>
5003     The record data is defined as a buffer of some length so that
5004     data can be of any type. SRW 1.0 currenly doesn't allow for this
5005     (only XML), but future versions might do.
5006    </para>
5007    <para>
5008     And, a diagnostic as:
5009     <synopsis>
5010 typedef struct {
5011     int  *code;
5012     char *details;
5013 } Z_SRW_diagnostic;
5014     </synopsis>
5015    </para>
5016   </sect1>
5017  </chapter>
5018  <chapter id="tools">
5019   <title>Supporting Tools</title>
5020   <para>
5021    In support of the service API - primarily the ASN module, which
5022    provides the pro-grammatic interface to the Z39.50 APDUs, &yaz; contains
5023    a collection of tools that support the development of applications.
5024   </para>
5025   <sect1 id="tools.query">
5026    <title>Query Syntax Parsers</title>
5027    <para>
5028     Since the type-1 (RPN) query structure has no direct, useful string
5029     representation, every origin application needs to provide some form of
5030     mapping from a local query notation or representation to a
5031     <token>Z_RPNQuery</token> structure. Some programmers will prefer to
5032     construct the query manually, perhaps using
5033     <function>odr_malloc()</function> to simplify memory management.
5034     The &yaz; distribution includes three separate, query-generating tools
5035     that may be of use to you.
5036    </para>
5037    <sect2 id="PQF">
5038     <title>Prefix Query Format</title>
5039     <para>
5040      Since RPN or reverse polish notation is really just a fancy way of
5041      describing a suffix notation format (operator follows operands), it
5042      would seem that the confusion is total when we now introduce a prefix
5043      notation for RPN. The reason is one of simple laziness - it's somewhat
5044      simpler to interpret a prefix format, and this utility was designed
5045      for maximum simplicity, to provide a baseline representation for use
5046      in simple test applications and scripting environments (like Tcl). The
5047      demonstration client included with YAZ uses the PQF.
5048     </para>
5049     <note>
5050      <para>
5051       The PQF have been adopted by other parties developing Z39.50
5052       software. It is often referred to as Prefix Query Notation
5053       - PQN.
5054      </para>
5055     </note>
5056     <para>
5057      The PQF is defined by the pquery module in the YAZ library.
5058      There are two sets of function that have similar behavior. First
5059      set operates on a PQF parser handle, second set doesn't. First set
5060      set of functions are more flexible than the second set. Second set
5061      is obsolete and is only provided to ensure backwards compatibility.
5062     </para>
5063     <para>
5064      First set of functions all operate on a PQF parser handle:
5065     </para>
5066     <synopsis>
5067      #include &lt;yaz/pquery.h&gt;
5068
5069      YAZ_PQF_Parser yaz_pqf_create(void);
5070
5071      void yaz_pqf_destroy(YAZ_PQF_Parser p);
5072
5073      Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf);
5074
5075      Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
5076                           Odr_oid **attributeSetId, const char *qbuf);
5077
5078      int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off);
5079     </synopsis>
5080     <para>
5081      A PQF parser is created and destructed by functions
5082      <function>yaz_pqf_create</function> and
5083      <function>yaz_pqf_destroy</function> respectively.
5084      Function <function>yaz_pqf_parse</function> parses query given
5085      by string <literal>qbuf</literal>. If parsing was successful,
5086      a Z39.50 RPN Query is returned which is created using ODR stream
5087      <literal>o</literal>. If parsing failed, a NULL pointer is
5088      returned.
5089      Function <function>yaz_pqf_scan</function> takes a scan query in
5090      <literal>qbuf</literal>. If parsing was successful, the function
5091      returns attributes plus term pointer and modifies
5092      <literal>attributeSetId</literal> to hold attribute set for the
5093      scan request - both allocated using ODR stream <literal>o</literal>.
5094      If parsing failed, yaz_pqf_scan returns a NULL pointer.
5095      Error information for bad queries can be obtained by a call to
5096      <function>yaz_pqf_error</function> which returns an error code and
5097      modifies <literal>*msg</literal> to point to an error description,
5098      and modifies <literal>*off</literal> to the offset within last
5099      query were parsing failed.
5100     </para>
5101     <para>
5102      The second set of functions are declared as follows:
5103     </para>
5104     <synopsis>
5105      #include &lt;yaz/pquery.h&gt;
5106
5107      Z_RPNQuery *p_query_rpn(ODR o, oid_proto proto, const char *qbuf);
5108
5109      Z_AttributesPlusTerm *p_query_scan(ODR o, oid_proto proto,
5110                              Odr_oid **attributeSetP, const char *qbuf);
5111
5112      int p_query_attset(const char *arg);
5113     </synopsis>
5114     <para>
5115      The function <function>p_query_rpn()</function> takes as arguments an
5116      &odr; stream (see section <link linkend="odr">The ODR Module</link>)
5117      to provide a memory source (the structure created is released on
5118      the next call to <function>odr_reset()</function> on the stream), a
5119      protocol identifier (one of the constants <token>PROTO_Z3950</token> and
5120      <token>PROTO_SR</token>), an attribute set reference, and
5121      finally a null-terminated string holding the query string.
5122     </para>
5123     <para>
5124      If the parse went well, <function>p_query_rpn()</function> returns a
5125      pointer to a <literal>Z_RPNQuery</literal> structure which can be
5126      placed directly into a <literal>Z_SearchRequest</literal>.
5127      If parsing failed, due to syntax error, a NULL pointer is returned.
5128     </para>
5129     <para>
5130      The <literal>p_query_attset</literal> specifies which attribute set
5131      to use if the query doesn't specify one by the
5132      <literal>@attrset</literal> operator.
5133      The <literal>p_query_attset</literal> returns 0 if the argument is a
5134      valid attribute set specifier; otherwise the function returns -1.
5135     </para>
5136     <para>
5137      The grammar of the PQF is as follows:
5138     </para>
5139     <literallayout>
5140      query ::= top-set query-struct.
5141
5142      top-set ::= [ '@attrset' string ]
5143
5144      query-struct ::= attr-spec | simple | complex | '@term' term-type query
5145
5146      attr-spec ::= '@attr' [ string ] string query-struct
5147
5148      complex ::= operator query-struct query-struct.
5149
5150      operator ::= '@and' | '@or' | '@not' | '@prox' proximity.
5151
5152      simple ::= result-set | term.
5153
5154      result-set ::= '@set' string.
5155
5156      term ::= string.
5157
5158      proximity ::= exclusion distance ordered relation which-code unit-code.
5159
5160      exclusion ::= '1' | '0' | 'void'.
5161
5162      distance ::= integer.
5163
5164      ordered ::= '1' | '0'.
5165
5166      relation ::= integer.
5167
5168      which-code ::= 'known' | 'private' | integer.
5169
5170      unit-code ::= integer.
5171
5172      term-type ::= 'general' | 'numeric' | 'string' | 'oid' | 'datetime' | 'null'.
5173     </literallayout>
5174     <para>
5175      You will note that the syntax above is a fairly faithful
5176      representation of RPN, except for the Attribute, which has been
5177      moved a step away from the term, allowing you to associate one or more
5178      attributes with an entire query structure. The parser will
5179      automatically apply the given attributes to each term as required.
5180     </para>
5181     <para>
5182      The @attr operator is followed by an attribute specification
5183      (<literal>attr-spec</literal> above). The specification consists
5184      of an optional attribute set, an attribute type-value pair and
5185      a sub-query. The attribute type-value pair is packed in one string:
5186      an attribute type, an equals sign, and an attribute value, like this:
5187      <literal>@attr 1=1003</literal>.
5188      The type is always an integer but the value may be either an
5189      integer or a string (if it doesn't start with a digit character).
5190      A string attribute-value is encoded as a Type-1 ``complex''
5191      attribute with the list of values containing the single string
5192      specified, and including no semantic indicators.
5193     </para>
5194     <para>
5195      Version 3 of the Z39.50 specification defines various encoding of terms.
5196      Use <literal>@term </literal> <replaceable>type</replaceable>
5197      <replaceable>string</replaceable>,
5198      where type is one of: <literal>general</literal>,
5199      <literal>numeric</literal> or <literal>string</literal>
5200      (for InternationalString).
5201      If no term type has been given, the <literal>general</literal> form
5202      is used.  This is the only encoding allowed in both versions 2 and 3
5203      of the Z39.50 standard.
5204     </para>
5205     <sect3 id="PQF-prox">
5206      <title>Using Proximity Operators with PQF</title>
5207      <note>
5208       <para>
5209        This is an advanced topic, describing how to construct
5210        queries that make very specific requirements on the
5211        relative location of their operands.
5212        You may wish to skip this section and go straight to
5213        <link linkend="pqf-examples">the example PQF queries</link>.
5214       </para>
5215       <para>
5216        <warning>
5217         <para>
5218          Most Z39.50 servers do not support proximity searching, or
5219          support only a small subset of the full functionality that
5220          can be expressed using the PQF proximity operator.  Be
5221          aware that the ability to <emphasis>express</emphasis> a
5222          query in PQF is no guarantee that any given server will
5223          be able to <emphasis>execute</emphasis> it.
5224         </para>
5225        </warning>
5226       </para>
5227      </note>
5228      <para>
5229       The proximity operator <literal>@prox</literal> is a special
5230       and more restrictive version of the conjunction operator
5231       <literal>@and</literal>.  Its semantics are described in
5232       section 3.7.2 (Proximity) of Z39.50 the standard itself, which
5233       can be read on-line at
5234       <ulink url="&url.z39.50.proximity;"/>
5235      </para>
5236      <para>
5237       In PQF, the proximity operation is represented by a sequence
5238       of the form
5239       <screen>
5240        @prox <replaceable>exclusion</replaceable> <replaceable>distance</replaceable> <replaceable>ordered</replaceable> <replaceable>relation</replaceable> <replaceable>which-code</replaceable> <replaceable>unit-code</replaceable>
5241       </screen>
5242       in which the meanings of the parameters are as described in in
5243       the standard, and they can take the following values:
5244       <itemizedlist>
5245        <listitem>
5246         <formalpara><title>exclusion</title>
5247         <para>
5248          0 = false (i.e. the proximity condition specified by the
5249          remaining parameters must be satisfied) or
5250          1 = true (the proximity condition specified by the
5251          remaining parameters must <emphasis>not</emphasis> be
5252          satisifed).
5253         </para>
5254        </formalpara>
5255        </listitem>
5256        <listitem>
5257         <formalpara><title>distance</title><para>
5258         An integer specifying the difference between the locations
5259         of the operands: e.g. two adjacent words would have
5260         distance=1 since their locations differ by one unit.
5261        </para>
5262        </formalpara></listitem>
5263        <listitem>
5264         <formalpara><title>ordered</title><para>
5265         1 = ordered (the operands must occur in the order the
5266         query specifies them) or
5267         0 = unordered (they may appear in either order).
5268        </para>
5269        </formalpara>
5270        </listitem>
5271        <listitem>
5272         <formalpara><title>relation</title><para>
5273         Recognised values are
5274         1 (lessThan),
5275         2 (lessThanOrEqual),
5276         3 (equal),
5277         4 (greaterThanOrEqual),
5278         5 (greaterThan) and
5279         6 (notEqual).
5280        </para>
5281        </formalpara>
5282        </listitem>
5283        <listitem>
5284         <formalpara><title>which-code</title><para>
5285         <literal>known</literal>
5286         or
5287         <literal>k</literal>
5288         (the unit-code parameter is taken from the well-known list
5289         of alternatives described in below) or
5290         <literal>private</literal>
5291         or
5292         <literal>p</literal>
5293         (the unit-code paramater has semantics specific to an
5294         out-of-band agreement such as a profile).
5295        </para>
5296        </formalpara>
5297        </listitem>
5298        <listitem>
5299         <formalpara><title>unit-code</title><para>
5300         If the which-code parameter is <literal>known</literal>
5301         then the recognised values are
5302         1 (character),
5303         2 (word),
5304         3 (sentence),
5305         4 (paragraph),
5306         5 (section),
5307         6 (chapter),
5308         7 (document),
5309         8 (element),
5310         9 (subelement),
5311         10 (elementType) and
5312         11 (byte).
5313         If which-code is <literal>private</literal> then the
5314         acceptable values are determined by the profile.
5315        </para>
5316         </formalpara>
5317        </listitem>
5318       </itemizedlist>
5319       (The numeric values of the relation and well-known unit-code
5320       parameters are taken straight from
5321       <ulink url="&url.z39.50.proximity.asn1;"
5322              >the ASN.1</ulink> of the proximity structure in the standard.)
5323      </para>
5324     </sect3>
5325     <sect3 id="pqf-examples">
5326      <title>PQF queries</title>
5327      <example id="example.pqf.simple.terms">
5328       <title>PQF queries using simple terms</title>
5329       <para>
5330        <screen>
5331         dylan
5332
5333         "bob dylan"
5334        </screen>
5335       </para>
5336      </example>
5337      <example id="pqf.example.pqf.boolean.operators">
5338       <title>PQF boolean operators</title>
5339       <para>
5340        <screen>
5341         @or "dylan" "zimmerman"
5342
5343         @and @or dylan zimmerman when
5344
5345         @and when @or dylan zimmerman
5346        </screen>
5347       </para>
5348      </example>
5349      <example id="example.pqf.result.sets">
5350       <title>PQF references to result sets</title>
5351       <para>
5352        <screen>
5353         @set Result-1
5354
5355         @and @set seta @set setb
5356        </screen>
5357       </para>
5358      </example>
5359      <example id="example.pqf.attributes">
5360       <title>Attributes for terms</title>
5361       <para>
5362        <screen>
5363         @attr 1=4 computer
5364
5365         @attr 1=4 @attr 4=1 "self portrait"
5366
5367         @attrset exp1 @attr 1=1 CategoryList
5368
5369         @attr gils 1=2008 Copenhagen
5370
5371         @attr 1=/book/title computer
5372        </screen>
5373       </para>
5374      </example>
5375      <example id="example.pqf.proximity">
5376       <title>PQF Proximity queries</title>
5377       <para>
5378        <screen>
5379         @prox 0 3 1 2 k 2 dylan zimmerman
5380        </screen>
5381        Here the parameters 0, 3, 1, 2, k and 2 represent exclusion,
5382        distance, ordered, relation, which-code and unit-code, in that
5383        order.  So:
5384        <itemizedlist>
5385         <listitem>
5386          <para>exclusion = 0: the proximity condition must hold</para>
5387         </listitem>
5388         <listitem>
5389          <para>distance = 3: the terms must be three units apart</para>
5390         </listitem>
5391         <listitem>
5392          <para>
5393           ordered = 1: they must occur in the order they are specified
5394         </para>
5395         </listitem>
5396         <listitem>
5397          <para>
5398         relation = 2: lessThanOrEqual (to the distance of 3 units)
5399         </para>
5400         </listitem>
5401         <listitem>
5402          <para>
5403           which-code is ``known'', so the standard unit-codes are used
5404          </para>
5405         </listitem>
5406         <listitem>
5407          <para>unit-code = 2: word.</para>
5408         </listitem>
5409        </itemizedlist>
5410        So the whole proximity query means that the words
5411        <literal>dylan</literal> and <literal>zimmerman</literal> must
5412        both occur in the record, in that order, differing in position
5413        by three or fewer words (i.e. with two or fewer words between
5414        them.)  The query would find ``Bob Dylan, aka. Robert
5415        Zimmerman'', but not ``Bob Dylan, born as Robert Zimmerman''
5416        since the distance in this case is four.
5417       </para>
5418      </example>
5419      <example id="example.pqf.search.term.type">
5420       <title>PQF specification of search term type</title>
5421       <para>
5422        <screen>
5423         @term string "a UTF-8 string, maybe?"
5424        </screen>
5425       </para>
5426      </example>
5427      <example id="example.pqf.mixed.queries">
5428       <title>PQF mixed queries</title>
5429       <para>
5430        <screen>
5431         @or @and bob dylan @set Result-1
5432
5433         @attr 4=1 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
5434
5435         @and @attr 2=4 @attr gils 1=2038 -114 @attr 2=2 @attr gils 1=2039 -109
5436        </screen>
5437        The last of these examples is a spatial search: in
5438        <ulink url="http://www.gils.net/prof_v2.html#sec_7_4"
5439               >the GILS attribute set</ulink>,
5440        access point
5441        2038 indicates West Bounding Coordinate and
5442        2030 indicates East Bounding Coordinate,
5443        so the query is for areas extending from -114 degrees
5444        to no more than -109 degrees.
5445       </para>
5446      </example>
5447     </sect3>
5448    </sect2>
5449    <sect2 id="CCL"><title>CCL</title>
5450     <para>
5451      Not all users enjoy typing in prefix query structures and numerical
5452      attribute values, even in a minimalistic test client. In the library
5453      world, the more intuitive Common Command Language - CCL (ISO 8777)
5454      has enjoyed some popularity - especially before the widespread
5455      availability of graphical interfaces. It is still useful in
5456      applications where you for some reason or other need to provide a
5457      symbolic language for expressing boolean query structures.
5458     </para>
5459     <sect3 id="ccl.syntax">
5460      <title>CCL Syntax</title>
5461      <para>
5462       The CCL parser obeys the following grammar for the FIND argument.
5463       The syntax is annotated by in the lines prefixed by
5464       <literal>--</literal>.
5465      </para>
5466      <screen>
5467       CCL-Find ::= CCL-Find Op Elements
5468                 | Elements.
5469
5470       Op ::= "and" | "or" | "not"
5471       -- The above means that Elements are separated by boolean operators.
5472
5473       Elements ::= '(' CCL-Find ')'
5474                 | Set
5475                 | Terms
5476                 | Qualifiers Relation Terms
5477                 | Qualifiers Relation '(' CCL-Find ')'
5478                 | Qualifiers '=' string '-' string
5479       -- Elements is either a recursive definition, a result set reference, a
5480       -- list of terms, qualifiers followed by terms, qualifiers followed
5481       -- by a recursive definition or qualifiers in a range (lower - upper).
5482
5483       Set ::= 'set' = string
5484       -- Reference to a result set
5485
5486       Terms ::= Terms Prox Term
5487              | Term
5488       -- Proximity of terms.
5489
5490       Term ::= Term string
5491             | string
5492       -- This basically means that a term may include a blank
5493
5494       Qualifiers ::= Qualifiers ',' string
5495                   | string
5496       -- Qualifiers is a list of strings separated by comma
5497
5498       Relation ::= '=' | '>=' | '&lt;=' | '&lt;>' | '>' | '&lt;'
5499       -- Relational operators. This really doesn't follow the ISO8777
5500       -- standard.
5501
5502       Prox ::= '%' | '!'
5503       -- Proximity operator
5504
5505      </screen>
5506      <example id="example.ccl.queries">
5507       <title>CCL queries</title>
5508       <para>
5509        The following queries are all valid:
5510       </para>
5511       <screen>
5512        dylan
5513
5514        "bob dylan"
5515
5516        dylan or zimmerman
5517
5518        set=1
5519
5520        (dylan and bob) or set=1
5521
5522        righttrunc?
5523
5524        "notrunc?"
5525
5526        singlechar#mask
5527       </screen>
5528       <para>
5529        Assuming that the qualifiers <literal>ti</literal>,
5530        <literal>au</literal>
5531        and <literal>date</literal> are defined we may use:
5532       </para>
5533       <screen>
5534        ti=self portrait
5535
5536        au=(bob dylan and slow train coming)
5537
5538        date>1980 and (ti=((self portrait)))
5539       </screen>
5540      </example>
5541     </sect3>
5542     <sect3 id="ccl.qualifiers">
5543      <title>CCL Qualifiers</title>
5544      <para>
5545       Qualifiers are used to direct the search to a particular searchable
5546       index, such as title (ti) and author indexes (au). The CCL standard
5547       itself doesn't specify a particular set of qualifiers, but it does
5548       suggest a few short-hand notations. You can customize the CCL parser
5549       to support a particular set of qualifiers to reflect the current target
5550       profile. Traditionally, a qualifier would map to a particular
5551       use-attribute within the BIB-1 attribute set. It is also
5552       possible to set other attributes, such as the structure
5553       attribute.
5554      </para>
5555      <para>
5556       A  CCL profile is a set of predefined CCL qualifiers that may be
5557       read from a file or set in the CCL API.
5558       The YAZ client reads its CCL qualifiers from a file named
5559       <filename>default.bib</filename>. There are four types of
5560       lines in a CCL profile: qualifier specification,
5561       qualifier alias, comments and directives.
5562      </para>
5563      <sect4 id="ccl.qualifier.specification">
5564       <title>Qualifier specification</title>
5565       <para>
5566        A qualifier specification is of the form:
5567       </para>
5568       <para>
5569        <replaceable>qualifier-name</replaceable>
5570        [<replaceable>attributeset</replaceable><literal>,</literal>]<replaceable>type</replaceable><literal>=</literal><replaceable>val</replaceable>
5571        [<replaceable>attributeset</replaceable><literal>,</literal>]<replaceable>type</replaceable><literal>=</literal><replaceable>val</replaceable> ...
5572       </para>
5573       <para>
5574        where <replaceable>qualifier-name</replaceable> is the name of the
5575        qualifier to be used (eg. <literal>ti</literal>),
5576        <replaceable>type</replaceable> is attribute type in the attribute
5577        set (Bib-1 is used if no attribute set is given) and
5578        <replaceable>val</replaceable> is attribute value.
5579        The <replaceable>type</replaceable> can be specified as an
5580        integer or as it be specified either as a single-letter:
5581        <literal>u</literal> for use,
5582        <literal>r</literal> for relation,<literal>p</literal> for position,
5583        <literal>s</literal> for structure,<literal>t</literal> for truncation
5584        or <literal>c</literal> for completeness.
5585        The attributes for the special qualifier name <literal>term</literal>
5586        are used when no CCL qualifier is given in a query.
5587        <table id="ccl.common.bib1.attributes">
5588         <title>Common Bib-1 attributes</title>
5589         <tgroup cols="2">
5590          <colspec colwidth="2*" colname="type"></colspec>
5591          <colspec colwidth="9*" colname="description"></colspec>
5592          <thead>
5593           <row>
5594            <entry>Type</entry>
5595            <entry>Description</entry>
5596           </row>
5597          </thead>
5598          <tbody>
5599           <row>
5600            <entry><literal>u=</literal><replaceable>value</replaceable></entry>
5601            <entry>
5602             Use attribute (1). Common use attributes are
5603             1 Personal-name, 4 Title, 7 ISBN, 8 ISSN, 30 Date,
5604             62 Subject, 1003 Author), 1016 Any. Specify value
5605             as an integer.
5606            </entry>
5607           </row>
5608           <row>
5609            <entry><literal>r=</literal><replaceable>value</replaceable></entry>
5610            <entry>
5611             Relation attribute (2). Common values are
5612             1 &lt;, 2 &lt;=, 3 =, 4 &gt;=, 5 &gt;, 6 &lt;&gt;,
5613             100 phonetic, 101 stem, 102 relevance, 103 always matches.
5614            </entry>
5615           </row>
5616           <row>
5617            <entry><literal>p=</literal><replaceable>value</replaceable></entry>
5618            <entry>
5619             Position attribute (3). Values: 1 first in field, 2
5620             first in any subfield, 3 any position in field.
5621            </entry>
5622           </row>
5623           <row>
5624            <entry><literal>s=</literal><replaceable>value</replaceable></entry>
5625            <entry>
5626             Structure attribute (4). Values: 1 phrase, 2 word,
5627             3 key, 4 year, 5 date, 6 word list, 100 date (un),
5628             101 name (norm), 102 name (un), 103 structure, 104 urx,
5629             105 free-form-text, 106 document-text, 107 local-number,
5630             108 string, 109 numeric string.
5631            </entry>
5632           </row>
5633           <row>
5634            <entry><literal>t=</literal><replaceable>value</replaceable></entry>
5635            <entry>
5636             Truncation attribute (5). Values: 1 right, 2 left,
5637             3 left&amp; right, 100 none, 101 process #, 102 regular-1,
5638             103 regular-2, 104 CCL.
5639            </entry>
5640           </row>
5641           <row>
5642            <entry><literal>c=</literal><replaceable>value</replaceable></entry>
5643            <entry>
5644             Completeness attribute (6). Values: 1 incomplete subfield,
5645             2 complete subfield, 3 complete field.
5646            </entry>
5647           </row>
5648          </tbody>
5649         </tgroup>
5650        </table>
5651       </para>
5652       <para>
5653        Refer to <xref linkend="bib1"/> or the complete
5654        <ulink url="&url.z39.50.attset.bib1;">list of Bib-1 attributes</ulink>
5655       </para>
5656       <para>
5657        It is also possible to specify non-numeric attribute values,
5658        which are used in combination with certain types.
5659        The special combinations are:
5660        <table id="ccl.special.attribute.combos">
5661         <title>Special attribute combos</title>
5662         <tgroup cols="2">
5663          <colspec colwidth="2*" colname="name"></colspec>
5664          <colspec colwidth="9*" colname="description"></colspec>
5665          <thead>
5666           <row>
5667            <entry>Name</entry>
5668            <entry>Description</entry>
5669           </row>
5670          </thead>
5671          <tbody>
5672           <row>
5673            <entry><literal>s=pw</literal></entry>
5674            <entry>
5675             The structure is set to either word or phrase depending
5676             on the number of tokens in a term (phrase-word).
5677            </entry>
5678           </row>
5679           <row>
5680            <entry><literal>s=al</literal></entry>
5681            <entry>
5682             Each token in the term is ANDed. (and-list).
5683             This does not set the structure at all.
5684            </entry>
5685           </row>
5686           <row><entry><literal>s=ol</literal></entry>
5687           <entry>
5688            Each token in the term is ORed. (or-list).
5689            This does not set the structure at all.
5690           </entry>
5691           </row>
5692           <row><entry><literal>s=ag</literal></entry>
5693           <entry>
5694            Tokens that appears as phrases (with blank in them) gets
5695            structure phrase attached (4=1). Tokens that appear to be words
5696            gets structure word attached (4=2). Phrases and words are
5697            ANDed. This is a variant of s=al and s=pw, with the main
5698            difference that words are not split (with operator AND)
5699            but instead kept in one RPN token. This facility appeared
5700            in YAZ 4.2.38.
5701           </entry>
5702           </row>
5703           <row><entry><literal>r=o</literal></entry>
5704           <entry>
5705            Allows ranges and the operators greather-than, less-than, ...
5706            equals.
5707            This sets Bib-1 relation attribute accordingly (relation
5708            ordered). A query construct is only treated as a range if
5709            dash is used and that is surrounded by white-space. So
5710            <literal>-1980</literal> is treated as term
5711            <literal>"-1980"</literal> not <literal>&lt;= 1980</literal>.
5712            If <literal>- 1980</literal> is used, however, that is
5713            treated as a range.
5714           </entry>
5715           </row>
5716           <row><entry><literal>r=r</literal></entry>
5717           <entry>
5718            Similar to <literal>r=o</literal> but assumes that terms
5719            are non-negative (not prefixed with <literal>-</literal>).
5720            Thus, a dash will always be treated as a range.
5721            The construct <literal>1980-1990</literal> is
5722            treated as a range with <literal>r=r</literal> but as a
5723            single term <literal>"1980-1990"</literal> with
5724            <literal>r=o</literal>. The special attribute
5725            <literal>r=r</literal> is available in YAZ 2.0.24 or later.
5726           </entry>
5727           </row>
5728           <row><entry><literal>r=omiteq</literal></entry>
5729           <entry>
5730            This will omit relation=equals (@attr 2=3) when r=o / r=r
5731            is used. This is useful for servers that somehow breaks
5732            when an explicit relation=equals is used. Omitting the
5733            relation is usually safe because "equals" is the default
5734            behavior. This tweak was added in YAZ version 5.1.2.
5735           </entry>
5736           </row>
5737           <row><entry><literal>t=l</literal></entry>
5738           <entry>
5739            Allows term to be left-truncated.
5740            If term is of the form <literal>?x</literal>, the resulting
5741            Type-1 term is <literal>x</literal> and truncation is left.
5742           </entry>
5743           </row>
5744           <row><entry><literal>t=r</literal></entry>
5745           <entry>
5746            Allows term to be right-truncated.
5747            If term is of the form <literal>x?</literal>, the resulting
5748            Type-1 term is <literal>x</literal> and truncation is right.
5749           </entry>
5750           </row>
5751           <row><entry><literal>t=n</literal></entry>
5752           <entry>
5753            If term is does not include <literal>?</literal>, the
5754            truncation attribute is set to none (100).
5755           </entry>
5756           </row>
5757           <row><entry><literal>t=b</literal></entry>
5758           <entry>
5759            Allows term to be both left&amp;right truncated.
5760            If term is of the form <literal>?x?</literal>, the
5761            resulting term is <literal>x</literal> and trunctation is
5762            set to both left&amp;right.
5763           </entry>
5764           </row>
5765           <row><entry><literal>t=x</literal></entry>
5766           <entry>
5767            Allows masking anywhere in a term, thus fully supporting
5768            # (mask one character) and ? (zero or more of any).
5769            If masking is used, trunction is set to 102 (regexp-1 in term)
5770            and the term is converted accordingly to a regular expression.
5771           </entry>
5772           </row>
5773           <row><entry><literal>t=z</literal></entry>
5774           <entry>
5775            Allows masking anywhere in a term, thus fully supporting
5776            # (mask one character) and ? (zero or more of any).
5777            If masking is used, trunction is set to 104 (Z39.58 in term)
5778            and the term is converted accordingly to Z39.58 masking term -
5779            actually the same truncation as CCL itself.
5780           </entry>
5781           </row>
5782          </tbody>
5783         </tgroup>
5784        </table>
5785       </para>
5786       <example id="example.ccl.profile">
5787        <title>CCL profile</title>
5788        <para>
5789         Consider the following definition:
5790        </para>
5791        <screen>
5792         ti       u=4 s=1
5793         au       u=1 s=1
5794         term     s=105
5795         ranked   r=102
5796         date     u=30 r=o
5797        </screen>
5798        <para>
5799         <literal>ti</literal> and <literal>au</literal> both set
5800         structure attribute to phrase (s=1).
5801         <literal>ti</literal>
5802         sets the use-attribute to 4. <literal>au</literal> sets the
5803         use-attribute to 1.
5804         When no qualifiers are used in the query the structure-attribute is
5805         set to free-form-text (105) (rule for <literal>term</literal>).
5806         The <literal>date</literal> sets the relation attribute to
5807         the relation used in the CCL query and sets the use attribute
5808         to 30 (Bib-1 Date).
5809        </para>
5810        <para>
5811         You can combine attributes. To Search for "ranked title" you
5812         can do
5813         <screen>
5814          ti,ranked=knuth computer
5815         </screen>
5816         which will set relation=ranked, use=title, structure=phrase.
5817        </para>
5818        <para>
5819         Query
5820         <screen>
5821          date > 1980
5822         </screen>
5823         is a valid query. But
5824         <screen>
5825          ti > 1980
5826         </screen>
5827         is invalid.
5828        </para>
5829       </example>
5830      </sect4>
5831      <sect4 id="ccl.qualifier.alias">
5832       <title>Qualifier alias</title>
5833       <para>
5834        A qualifier alias is of the form:
5835       </para>
5836       <para>
5837        <replaceable>q</replaceable>
5838        <replaceable>q1</replaceable> <replaceable>q2</replaceable> ..
5839       </para>
5840       <para>
5841        which declares <replaceable>q</replaceable> to
5842        be an alias for <replaceable>q1</replaceable>,
5843        <replaceable>q2</replaceable>... such that the CCL
5844        query <replaceable>q=x</replaceable> is equivalent to
5845        <replaceable>q1=x or q2=x or ...</replaceable>.
5846       </para>
5847      </sect4>
5848      <sect4 id="ccl.comments">
5849       <title>Comments</title>
5850       <para>
5851        Lines with white space or lines that begin with
5852        character <literal>#</literal> are treated as comments.
5853       </para>
5854      </sect4>
5855      <sect4 id="ccl.directives">
5856       <title>Directives</title>
5857       <para>
5858        Directive specifications takes the form
5859       </para>
5860       <para><literal>@</literal><replaceable>directive</replaceable> <replaceable>value</replaceable>
5861       </para>
5862       <table id="ccl.directives.table">
5863        <title>CCL directives</title>
5864        <tgroup cols="3">
5865         <colspec colwidth="2*" colname="name"></colspec>
5866         <colspec colwidth="8*" colname="description"></colspec>
5867         <colspec colwidth="1*" colname="default"></colspec>
5868         <thead>
5869          <row>
5870           <entry>Name</entry>
5871           <entry>Description</entry>
5872           <entry>Default</entry>
5873          </row>
5874         </thead>
5875         <tbody>
5876          <row>
5877           <entry>truncation</entry>
5878           <entry>Truncation character</entry>
5879           <entry><literal>?</literal></entry>
5880          </row>
5881          <row>
5882           <entry>mask</entry>
5883           <entry>Masking character. Requires YAZ 4.2.58 or later</entry>
5884           <entry><literal>#</literal></entry>
5885          </row>
5886          <row>
5887           <entry>field</entry>
5888           <entry>Specifies how multiple fields are to be
5889            combined. There are two modes: <literal>or</literal>:
5890            multiple qualifier fields are ORed,
5891            <literal>merge</literal>: attributes for the qualifier
5892            fields are merged and assigned to one term.
5893            </entry>
5894           <entry><literal>merge</literal></entry>
5895          </row>
5896          <row>
5897           <entry>case</entry>
5898           <entry>Specifies if CCL operators and qualifiers should be
5899            compared with case sensitivity or not. Specify 1 for
5900            case sensitive; 0 for case insensitive.</entry>
5901           <entry><literal>1</literal></entry>
5902          </row>
5903          <row>
5904           <entry>and</entry>
5905           <entry>Specifies token for CCL operator AND.</entry>
5906           <entry><literal>and</literal></entry>
5907          </row>
5908          <row>
5909           <entry>or</entry>
5910           <entry>Specifies token for CCL operator OR.</entry>
5911           <entry><literal>or</literal></entry>
5912          </row>
5913          <row>
5914           <entry>not</entry>
5915           <entry>Specifies token for CCL operator NOT.</entry>
5916           <entry><literal>not</literal></entry>
5917          </row>
5918          <row>
5919           <entry>set</entry>
5920           <entry>Specifies token for CCL operator SET.</entry>
5921           <entry><literal>set</literal></entry>
5922          </row>
5923         </tbody>
5924        </tgroup>
5925       </table>
5926      </sect4>
5927     </sect3>
5928     <sect3 id="ccl.api">
5929      <title>CCL API</title>
5930      <para>
5931       All public definitions can be found in the header file
5932       <filename>ccl.h</filename>. A profile identifier is of type
5933       <literal>CCL_bibset</literal>. A profile must be created with the call
5934       to the function <function>ccl_qual_mk</function> which returns a profile
5935       handle of type <literal>CCL_bibset</literal>.
5936      </para>
5937      <para>
5938       To read a file containing qualifier definitions the function
5939       <function>ccl_qual_file</function> may be convenient. This function
5940       takes an already opened <literal>FILE</literal> handle pointer as
5941       argument along with a <literal>CCL_bibset</literal> handle.
5942      </para>
5943      <para>
5944       To parse a simple string with a FIND query use the function
5945      </para>
5946      <screen>
5947 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
5948                                   int *error, int *pos);
5949      </screen>
5950      <para>
5951       which takes the CCL profile (<literal>bibset</literal>) and query
5952       (<literal>str</literal>) as input. Upon successful completion the RPN
5953       tree is returned. If an error occur, such as a syntax error, the integer
5954       pointed to by <literal>error</literal> holds the error code and
5955       <literal>pos</literal> holds the offset inside query string in which
5956       the parsing failed.
5957      </para>
5958      <para>
5959       An English representation of the error may be obtained by calling
5960       the <literal>ccl_err_msg</literal> function. The error codes are
5961       listed in <filename>ccl.h</filename>.
5962      </para>
5963      <para>
5964       To convert the CCL RPN tree (type
5965       <literal>struct ccl_rpn_node *</literal>)
5966       to the Z_RPNQuery of YAZ the function <function>ccl_rpn_query</function>
5967       must be used. This function which is part of YAZ is implemented in
5968       <filename>yaz-ccl.c</filename>.
5969       After calling this function the CCL RPN tree is probably no longer
5970       needed. The <literal>ccl_rpn_delete</literal> destroys the CCL RPN tree.
5971      </para>
5972      <para>
5973       A CCL profile may be destroyed by calling the
5974       <function>ccl_qual_rm</function> function.
5975      </para>
5976      <para>
5977       The token names for the CCL operators may be changed by setting the
5978       globals (all type <literal>char *</literal>)
5979       <literal>ccl_token_and</literal>, <literal>ccl_token_or</literal>,
5980       <literal>ccl_token_not</literal> and <literal>ccl_token_set</literal>.
5981       An operator may have aliases, i.e. there may be more than one name for
5982       the operator. To do this, separate each alias with a space character.
5983      </para>
5984     </sect3>
5985    </sect2>
5986    <sect2 id="cql">
5987     <title>CQL</title>
5988     <para>
5989      <ulink url="&url.cql;">CQL</ulink>
5990      - Common Query Language - was defined for the
5991      <ulink url="&url.sru;">SRU</ulink> protocol.
5992      In many ways CQL has a similar syntax to CCL.
5993      The objective of CQL is different. Where CCL aims to be
5994      an end-user language, CQL is <emphasis>the</emphasis> protocol
5995      query language for SRU.
5996     </para>
5997     <tip>
5998      <para>
5999       If you are new to CQL, read the
6000       <ulink url="&url.cql.intro;">Gentle Introduction</ulink>.
6001      </para>
6002     </tip>
6003     <para>
6004      The CQL parser in &yaz; provides the following:
6005      <itemizedlist>
6006       <listitem>
6007        <para>
6008         It parses and validates a CQL query.
6009        </para>
6010       </listitem>
6011       <listitem>
6012        <para>
6013         It generates a C structure that allows you to convert
6014         a CQL query to some other query language, such as SQL.
6015        </para>
6016       </listitem>
6017       <listitem>
6018        <para>
6019         The parser converts a valid CQL query to PQF, thus providing a
6020         way to use CQL for both SRU servers and Z39.50 targets at the
6021         same time.
6022        </para>
6023       </listitem>
6024       <listitem>
6025        <para>
6026         The parser converts CQL to XCQL.
6027         XCQL is an XML representation of CQL.
6028         XCQL is part of the SRU specification. However, since SRU
6029         supports CQL only, we don't expect XCQL to be widely used.
6030         Furthermore, CQL has the advantage over XCQL that it is
6031         easy to read.
6032        </para>
6033       </listitem>
6034      </itemizedlist>
6035     </para>
6036     <sect3 id="cql.parsing">
6037      <title>CQL parsing</title>
6038      <para>
6039       A CQL parser is represented by the <literal>CQL_parser</literal>
6040       handle. Its contents should be considered &yaz; internal (private).
6041       <synopsis>
6042 #include &lt;yaz/cql.h&gt;
6043
6044 typedef struct cql_parser *CQL_parser;
6045
6046 CQL_parser cql_parser_create(void);
6047 void cql_parser_destroy(CQL_parser cp);
6048       </synopsis>
6049      A parser is created by <function>cql_parser_create</function> and
6050      is destroyed by <function>cql_parser_destroy</function>.
6051      </para>
6052      <para>
6053       To parse a CQL query string, the following function
6054       is provided:
6055       <synopsis>
6056 int cql_parser_string(CQL_parser cp, const char *str);
6057       </synopsis>
6058       A CQL query is parsed by the <function>cql_parser_string</function>
6059       which takes a query <parameter>str</parameter>.
6060       If the query was valid (no syntax errors), then zero is returned;
6061       otherwise -1 is returned to indicate a syntax error.
6062      </para>
6063      <para>
6064       <synopsis>
6065 int cql_parser_stream(CQL_parser cp,
6066                       int (*getbyte)(void *client_data),
6067                       void (*ungetbyte)(int b, void *client_data),
6068                       void *client_data);
6069
6070 int cql_parser_stdio(CQL_parser cp, FILE *f);
6071       </synopsis>
6072       The functions <function>cql_parser_stream</function> and
6073       <function>cql_parser_stdio</function> parses a CQL query
6074       - just like <function>cql_parser_string</function>.
6075       The only difference is that the CQL query can be
6076       fed to the parser in different ways.
6077       The <function>cql_parser_stream</function> uses a generic
6078       byte stream as input. The <function>cql_parser_stdio</function>
6079       uses a <literal>FILE</literal> handle which is opened for reading.
6080      </para>
6081     </sect3>
6082     <sect3 id="cql.tree">
6083      <title>CQL tree</title>
6084      <para>
6085       The the query string is valid, the CQL parser
6086       generates a tree representing the structure of the
6087       CQL query.
6088      </para>
6089      <para>
6090       <synopsis>
6091 struct cql_node *cql_parser_result(CQL_parser cp);
6092       </synopsis>
6093       <function>cql_parser_result</function> returns the
6094       a pointer to the root node of the resulting tree.
6095      </para>
6096      <para>
6097       Each node in a CQL tree is represented by a
6098       <literal>struct cql_node</literal>.
6099       It is defined as follows:
6100       <synopsis>
6101 #define CQL_NODE_ST 1
6102 #define CQL_NODE_BOOL 2
6103 #define CQL_NODE_SORT 3
6104 struct cql_node {
6105     int which;
6106     union {
6107         struct {
6108             char *index;
6109             char *index_uri;
6110             char *term;
6111             char *relation;
6112             char *relation_uri;
6113             struct cql_node *modifiers;
6114         } st;
6115         struct {
6116             char *value;
6117             struct cql_node *left;
6118             struct cql_node *right;
6119             struct cql_node *modifiers;
6120         } boolean;
6121         struct {
6122             char *index;
6123             struct cql_node *next;
6124             struct cql_node *modifiers;
6125             struct cql_node *search;
6126         } sort;
6127     } u;
6128 };
6129       </synopsis>
6130       There are three node types: search term (ST), boolean (BOOL)
6131       and sortby (SORT).
6132       A modifier is treated as a search term too.
6133      </para>
6134      <para>
6135       The search term node has five members:
6136       <itemizedlist>
6137        <listitem>
6138         <para>
6139          <literal>index</literal>: index for search term.
6140          If an index is unspecified for a search term,
6141          <literal>index</literal> will be NULL.
6142         </para>
6143        </listitem>
6144        <listitem>
6145         <para>
6146          <literal>index_uri</literal>: index URi for search term
6147          or NULL if none could be resolved for the index.
6148         </para>
6149        </listitem>
6150        <listitem>
6151         <para>
6152          <literal>term</literal>: the search term itself.
6153         </para>
6154        </listitem>
6155        <listitem>
6156         <para>
6157          <literal>relation</literal>: relation for search term.
6158         </para>
6159        </listitem>
6160        <listitem>
6161         <para>
6162          <literal>relation_uri</literal>: relation URI for search term.
6163         </para>
6164        </listitem>
6165        <listitem>
6166         <para>
6167          <literal>modifiers</literal>: relation modifiers for search
6168          term. The <literal>modifiers</literal> list itself of cql_nodes
6169          each of type <literal>ST</literal>.
6170         </para>
6171        </listitem>
6172       </itemizedlist>
6173      </para>
6174      <para>
6175       The boolean node represents <literal>and</literal>,
6176       <literal>or</literal>, <literal>not</literal> +
6177       proximity.
6178       <itemizedlist>
6179        <listitem>
6180         <para>
6181          <literal>left</literal> and <literal>right</literal>: left
6182          - and right operand respectively.
6183         </para>
6184        </listitem>
6185        <listitem>
6186         <para>
6187          <literal>modifiers</literal>: proximity arguments.
6188         </para>
6189        </listitem>
6190       </itemizedlist>
6191      </para>
6192      <para>
6193       The sort node represents both the SORTBY clause.
6194      </para>
6195     </sect3>
6196     <sect3 id="cql.to.pqf">
6197      <title>CQL to PQF conversion</title>
6198      <para>
6199       Conversion to PQF (and Z39.50 RPN) is tricky by the fact
6200       that the resulting RPN depends on the Z39.50 target
6201       capabilities (combinations of supported attributes).
6202       In addition, the CQL and SRU operates on index prefixes
6203       (URI or strings), whereas the RPN uses Object Identifiers
6204       for attribute sets.
6205      </para>
6206      <para>
6207       The CQL library of &yaz; defines a <literal>cql_transform_t</literal>
6208       type. It represents a particular mapping between CQL and RPN.
6209       This handle is created and destroyed by the functions:
6210      <synopsis>
6211 cql_transform_t cql_transform_open_FILE (FILE *f);
6212 cql_transform_t cql_transform_open_fname(const char *fname);
6213 void cql_transform_close(cql_transform_t ct);
6214      </synopsis>
6215      The first two functions create a tranformation handle from
6216      either an already open FILE or from a filename respectively.
6217      </para>
6218      <para>
6219       The handle is destroyed by <function>cql_transform_close</function>
6220       in which case no further reference of the handle is allowed.
6221      </para>
6222      <para>
6223       When a <literal>cql_transform_t</literal> handle has been created
6224       you can convert to RPN.
6225       <synopsis>
6226 int cql_transform_buf(cql_transform_t ct,
6227                       struct cql_node *cn, char *out, int max);
6228       </synopsis>
6229       This function converts the CQL tree <literal>cn</literal>
6230       using handle <literal>ct</literal>.
6231       For the resulting PQF, you supply a buffer <literal>out</literal>
6232       which must be able to hold at at least <literal>max</literal>
6233       characters.
6234      </para>
6235      <para>
6236       If conversion failed, <function>cql_transform_buf</function>
6237       returns a non-zero SRU error code; otherwise zero is returned
6238       (conversion successful).  The meanings of the numeric error
6239       codes are listed in the SRU specification somewhere (no
6240       direct link anymore).
6241      </para>
6242      <para>
6243       If conversion fails, more information can be obtained by calling
6244       <synopsis>
6245 int cql_transform_error(cql_transform_t ct, char **addinfop);
6246       </synopsis>
6247       This function returns the most recently returned numeric
6248       error-code and sets the string-pointer at
6249       <literal>*addinfop</literal> to point to a string containing
6250       additional information about the error that occurred: for
6251       example, if the error code is 15 (``Illegal or unsupported context
6252       set''), the additional information is the name of the requested
6253       context set that was not recognised.
6254      </para>
6255      <para>
6256       The SRU error-codes may be translated into brief human-readable
6257       error messages using
6258       <synopsis>
6259 const char *cql_strerror(int code);
6260       </synopsis>
6261      </para>
6262      <para>
6263       If you wish to be able to produce a PQF result in a different
6264       way, there are two alternatives.
6265       <synopsis>
6266 void cql_transform_pr(cql_transform_t ct,
6267                       struct cql_node *cn,
6268                       void (*pr)(const char *buf, void *client_data),
6269                       void *client_data);
6270
6271 int cql_transform_FILE(cql_transform_t ct,
6272                        struct cql_node *cn, FILE *f);
6273       </synopsis>
6274       The former function produces output to a user-defined
6275       output stream. The latter writes the result to an already
6276       open <literal>FILE</literal>.
6277      </para>
6278     </sect3>
6279     <sect3 id="cql.to.rpn">
6280      <title>Specification of CQL to RPN mappings</title>
6281      <para>
6282       The file supplied to functions
6283       <function>cql_transform_open_FILE</function>,
6284       <function>cql_transform_open_fname</function> follows
6285       a structure found in many Unix utilities.
6286       It consists of mapping specifications - one per line.
6287       Lines starting with <literal>#</literal> are ignored (comments).
6288      </para>
6289      <para>
6290       Each line is of the form
6291       <literallayout>
6292        <replaceable>CQL pattern</replaceable><literal> = </literal> <replaceable> RPN equivalent</replaceable>
6293       </literallayout>
6294      </para>
6295      <para>
6296       An RPN pattern is a simple attribute list. Each attribute pair
6297       takes the form:
6298       <literallayout>
6299        [<replaceable>set</replaceable>] <replaceable>type</replaceable><literal>=</literal><replaceable>value</replaceable>
6300       </literallayout>
6301       The attribute <replaceable>set</replaceable> is optional.
6302       The <replaceable>type</replaceable> is the attribute type,
6303       <replaceable>value</replaceable> the attribute value.
6304      </para>
6305      <para>
6306       The character <literal>*</literal> (asterisk) has special meaning
6307       when used in the RPN pattern.
6308       Each occurrence of <literal>*</literal> is substituted with the
6309       CQL matching name (index, relation, qualifier etc).
6310       This facility can be used to copy a CQL name verbatim to the RPN result.
6311      </para>
6312      <para>
6313       The following CQL patterns are recognized:
6314       <variablelist>
6315        <varlistentry>
6316         <term>
6317          <literal>index.</literal><replaceable>set</replaceable><literal>.</literal><replaceable>name</replaceable>
6318         </term>
6319         <listitem>
6320          <para>
6321           This pattern is invoked when a CQL index, such as
6322           dc.title is converted. <replaceable>set</replaceable>
6323           and <replaceable>name</replaceable> are the context set and index
6324           name respectively.
6325           Typically, the RPN specifies an equivalent use attribute.
6326          </para>
6327          <para>
6328           For terms not bound by an index the pattern
6329           <literal>index.cql.serverChoice</literal> is used.
6330           Here, the prefix <literal>cql</literal> is defined as
6331           <literal>http://www.loc.gov/zing/cql/cql-indexes/v1.0/</literal>.
6332           If this pattern is not defined, the mapping will fail.
6333          </para>
6334          <para>
6335           The pattern,
6336           <literal>index.</literal><replaceable>set</replaceable><literal>.*</literal>
6337           is used when no other index pattern is matched.
6338          </para>
6339         </listitem>
6340        </varlistentry>
6341        <varlistentry>
6342         <term>
6343          <literal>qualifier.</literal><replaceable>set</replaceable><literal>.</literal><replaceable>name</replaceable>
6344          (DEPRECATED)
6345         </term>
6346         <listitem>
6347          <para>
6348           For backwards compatibility, this is recognised as a synonym of
6349           <literal>index.</literal><replaceable>set</replaceable><literal>.</literal><replaceable>name</replaceable>
6350          </para>
6351         </listitem>
6352        </varlistentry>
6353        <varlistentry>
6354         <term>
6355          <literal>relation.</literal><replaceable>relation</replaceable>
6356         </term>
6357         <listitem>
6358          <para>
6359           This pattern specifies how a CQL relation is mapped to RPN.
6360           <replaceable>pattern</replaceable> is name of relation
6361           operator. Since <literal>=</literal> is used as
6362           separator between CQL pattern and RPN, CQL relations
6363           including <literal>=</literal> cannot be
6364           used directly. To avoid a conflict, the names
6365           <literal>ge</literal>,
6366           <literal>eq</literal>,
6367           <literal>le</literal>,
6368           must be used for CQL operators, greater-than-or-equal,
6369           equal, less-than-or-equal respectively.
6370           The RPN pattern is supposed to include a relation attribute.
6371          </para>
6372          <para>
6373           For terms not bound by a relation, the pattern
6374           <literal>relation.scr</literal> is used. If the pattern
6375           is not defined, the mapping will fail.
6376          </para>
6377          <para>
6378           The special pattern, <literal>relation.*</literal> is used
6379           when no other relation pattern is matched.
6380          </para>
6381         </listitem>
6382        </varlistentry>
6383        <varlistentry>
6384         <term>
6385          <literal>relationModifier.</literal><replaceable>mod</replaceable>
6386         </term>
6387         <listitem>
6388          <para>
6389           This pattern specifies how a CQL relation modifier is mapped to RPN.
6390           The RPN pattern is usually a relation attribute.
6391          </para>
6392         </listitem>
6393        </varlistentry>
6394        <varlistentry>
6395         <term>
6396          <literal>structure.</literal><replaceable>type</replaceable>
6397         </term>
6398         <listitem>
6399          <para>
6400           This pattern specifies how a CQL structure is mapped to RPN.
6401           Note that this CQL pattern is somewhat to similar to
6402           CQL pattern <literal>relation</literal>.
6403           The <replaceable>type</replaceable> is a CQL relation.
6404          </para>
6405          <para>
6406           The pattern, <literal>structure.*</literal> is used
6407           when no other structure pattern is matched.
6408           Usually, the RPN equivalent specifies a structure attribute.
6409          </para>
6410         </listitem>
6411        </varlistentry>
6412        <varlistentry>
6413         <term>
6414          <literal>position.</literal><replaceable>type</replaceable>
6415         </term>
6416         <listitem>
6417          <para>
6418           This pattern specifies how the anchor (position) of
6419           CQL is mapped to RPN.
6420           The <replaceable>type</replaceable> is one
6421           of <literal>first</literal>, <literal>any</literal>,
6422           <literal>last</literal>, <literal>firstAndLast</literal>.
6423          </para>
6424          <para>
6425           The pattern, <literal>position.*</literal> is used
6426           when no other position pattern is matched.
6427          </para>
6428         </listitem>
6429        </varlistentry>
6430        <varlistentry>
6431         <term>
6432          <literal>set.</literal><replaceable>prefix</replaceable>
6433         </term>
6434         <listitem>
6435          <para>
6436           This specification defines a CQL context set for a given prefix.
6437           The value on the right hand side is the URI for the set -
6438           <emphasis>not</emphasis> RPN. All prefixes used in
6439           index patterns must be defined this way.
6440          </para>
6441         </listitem>
6442        </varlistentry>
6443        <varlistentry>
6444         <term>
6445          <literal>set</literal>
6446         </term>
6447         <listitem>
6448          <para>
6449           This specification defines a default CQL context set for index names.
6450           The value on the right hand side is the URI for the set.
6451          </para>
6452         </listitem>
6453        </varlistentry>
6454       </variablelist>
6455      </para>
6456      <example id="example.cql.to.rpn.mapping">
6457       <title>CQL to RPN mapping file</title>
6458       <para>
6459        This simple file defines two context sets, three indexes and three
6460        relations, a position pattern and a default structure.
6461       </para>
6462       <programlisting><![CDATA[
6463        set.cql  = http://www.loc.gov/zing/cql/context-sets/cql/v1.1/
6464        set.dc   = http://www.loc.gov/zing/cql/dc-indexes/v1.0/
6465
6466        index.cql.serverChoice = 1=1016
6467        index.dc.title         = 1=4
6468        index.dc.subject       = 1=21
6469
6470        relation.<             = 2=1
6471        relation.eq            = 2=3
6472        relation.scr           = 2=3
6473
6474        position.any           = 3=3 6=1
6475
6476        structure.*            = 4=1
6477 ]]>
6478       </programlisting>
6479       <para>
6480        With the mappings above, the CQL query
6481        <screen>
6482         computer
6483        </screen>
6484        is converted to the PQF:
6485        <screen>
6486         @attr 1=1016 @attr 2=3 @attr 4=1 @attr 3=3 @attr 6=1 "computer"
6487        </screen>
6488        by rules <literal>index.cql.serverChoice</literal>,
6489        <literal>relation.scr</literal>, <literal>structure.*</literal>,
6490        <literal>position.any</literal>.
6491       </para>
6492       <para>
6493        CQL query
6494        <screen>
6495         computer^
6496        </screen>
6497        is rejected, since <literal>position.right</literal> is
6498        undefined.
6499       </para>
6500       <para>
6501        CQL query
6502        <screen>
6503         >my = "http://www.loc.gov/zing/cql/dc-indexes/v1.0/" my.title = x
6504        </screen>
6505        is converted to
6506        <screen>
6507         @attr 1=4 @attr 2=3 @attr 4=1 @attr 3=3 @attr 6=1 "x"
6508        </screen>
6509       </para>
6510      </example>
6511      <example id="example.cql.to.rpn.string">
6512       <title>CQL to RPN string attributes</title>
6513       <para>
6514        In this example we allow any index to be passed to RPN as
6515        a use attribute.
6516       </para>
6517       <programlisting><![CDATA[
6518        # Identifiers for prefixes used in this file. (index.*)
6519        set.cql  = info:srw/cql-context-set/1/cql-v1.1
6520        set.rpn  = http://bogus/rpn
6521        set      = http://bogus/rpn
6522
6523        # The default index when none is specified by the query
6524        index.cql.serverChoice     = 1=any
6525
6526        index.rpn.*                = 1=*
6527        relation.eq                = 2=3
6528        structure.*                = 4=1
6529        position.any               = 3=3
6530 ]]>
6531       </programlisting>
6532       <para>
6533        The <literal>http://bogus/rpn</literal> context set is also the default
6534        so we can make queries such as
6535        <screen>
6536         title = a
6537        </screen>
6538        which is converted to
6539        <screen>
6540         @attr 2=3 @attr 4=1 @attr 3=3 @attr 1=title "a"
6541        </screen>
6542       </para>
6543      </example>
6544      <example id="example.cql.to.rpn.bathprofile">
6545       <title>CQL to RPN using Bath Profile</title>
6546       <para>
6547        The file <filename>etc/pqf.properties</filename> has mappings from
6548        the Bath Profile and Dublin Core to RPN.
6549        If YAZ is installed as a package it's usually located
6550        in <filename>/usr/share/yaz/etc</filename> and part of the
6551        development package, such as <literal>libyaz-dev</literal>.
6552       </para>
6553      </example>
6554     </sect3>
6555     <sect3 id="cql.xcql">
6556      <title>CQL to XCQL conversion</title>
6557      <para>
6558       Conversion from CQL to XCQL is trivial and does not
6559       require a mapping to be defined.
6560       There three functions to choose from depending on the
6561       way you wish to store the resulting output (XML buffer
6562       containing XCQL).
6563       <synopsis>
6564 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
6565 void cql_to_xml(struct cql_node *cn,
6566                 void (*pr)(const char *buf, void *client_data),
6567                 void *client_data);
6568 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
6569       </synopsis>
6570       Function <function>cql_to_xml_buf</function> converts
6571       to XCQL and stores result in a user supplied buffer of a given
6572       max size.
6573      </para>
6574      <para>
6575       <function>cql_to_xml</function> writes the result in
6576       a user defined output stream.
6577       <function>cql_to_xml_stdio</function> writes to a
6578       a file.
6579      </para>
6580     </sect3>
6581     <sect3 id="rpn.to.cql">
6582      <title>PQF to CQL conversion</title>
6583      <para>
6584       Conversion from PQF to CQL is offered by the two functions shown
6585       below. The former uses a generic stream for result. The latter
6586       puts result in a WRBUF (string container).
6587       <synopsis>
6588 #include &lt;yaz/rpn2cql.h>
6589
6590 int cql_transform_rpn2cql_stream(cql_transform_t ct,
6591                                  void (*pr)(const char *buf, void *client_data),
6592                                  void *client_data,
6593                                  Z_RPNQuery *q);
6594
6595 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
6596                                 WRBUF w,
6597                                 Z_RPNQuery *q);
6598       </synopsis>
6599       The configuration is the same as used in CQL to PQF conversions.
6600      </para>
6601     </sect3>
6602    </sect2>
6603   </sect1>
6604   <sect1 id="tools.oid">
6605    <title>Object Identifiers</title>
6606    <para>
6607     The basic YAZ representation of an OID is an array of integers,
6608     terminated with the value -1. This integer is of type
6609     <literal>Odr_oid</literal>.
6610    </para>
6611    <para>
6612     Fundamental OID operations and the type <literal>Odr_oid</literal>
6613     are defined in <filename>yaz/oid_util.h</filename>.
6614    </para>
6615    <para>
6616     An OID can either be declared as a automatic variable or it can
6617     allocated using the memory utilities or ODR/NMEM. It's
6618     guaranteed that an OID can fit in <literal>OID_SIZE</literal> integers.
6619    </para>
6620    <example id="tools.oid.bib1.1"><title>Create OID on stack</title>
6621     <para>
6622      We can create an OID for the Bib-1 attribute set with:
6623      <screen>
6624       Odr_oid bib1[OID_SIZE];
6625       bib1[0] = 1;
6626       bib1[1] = 2;
6627       bib1[2] = 840;
6628       bib1[3] = 10003;
6629       bib1[4] = 3;
6630       bib1[5] = 1;
6631       bib1[6] = -1;
6632      </screen>
6633     </para>
6634    </example>
6635    <para>
6636     And OID may also be filled from a string-based representation using
6637     dots (.). This is achieved by function
6638     <screen>
6639      int oid_dotstring_to_oid(const char *name, Odr_oid *oid);
6640     </screen>
6641     This functions returns 0 if name could be converted; -1 otherwise.
6642    </para>
6643    <example id="tools.oid.bib1.2"><title>Using oid_oiddotstring_to_oid</title>
6644     <para>
6645      We can fill the Bib-1 attribute set OID easier with:
6646      <screen>
6647       Odr_oid bib1[OID_SIZE];
6648       oid_oiddotstring_to_oid("1.2.840.10003.3.1", bib1);
6649      </screen>
6650    </para>
6651    </example>
6652    <para>
6653     We can also allocate an OID dynamically on a ODR stream with:
6654     <screen>
6655     Odr_oid *odr_getoidbystr(ODR o, const char *str);
6656     </screen>
6657     This creates an OID from string-based representation using dots.
6658     This function take an &odr; stream as parameter. This stream is used to
6659     allocate memory for the data elements, which is released on a
6660     subsequent call to <function>odr_reset()</function> on that stream.
6661    </para>
6662    <example id="tools.oid.bib1.3">
6663     <title>Using odr_getoidbystr</title>
6664     <para>
6665      We can create a OID for the Bib-1 attribute set with:
6666      <screen>
6667       Odr_oid *bib1 = odr_getoidbystr(odr, "1.2.840.10003.3.1");
6668      </screen>
6669     </para>
6670    </example>
6671    <para>
6672     The function
6673     <screen>
6674      char *oid_oid_to_dotstring(const Odr_oid *oid, char *oidbuf)
6675     </screen>
6676     does the reverse of <function>oid_oiddotstring_to_oid</function>. It
6677     converts an OID to the string-based representation using dots.
6678     The supplied char buffer <literal>oidbuf</literal> holds the resulting
6679     string and must be at least <literal>OID_STR_MAX</literal> in size.
6680    </para>
6681    <para>
6682     OIDs can be copied with <function>oid_oidcpy</function> which takes
6683     two OID lists as arguments. Alternativly, an OID copy can be allocated
6684     on a ODR stream with:
6685     <screen>
6686      Odr_oid *odr_oiddup(ODR odr, const Odr_oid *o);
6687     </screen>
6688    </para>
6689    <para>
6690     OIDs can be compared with <function>oid_oidcmp</function> which returns
6691     zero if the two OIDs provided are identical; non-zero otherwise.
6692    </para>
6693    <sect2 id="tools.oid.database">
6694     <title>OID database</title>
6695     <para>
6696      From YAZ version 3 and later, the oident system has been replaced
6697      by an OID database. OID database is a misnomer .. the old odient
6698      system was also a database.
6699     </para>
6700     <para>
6701      The OID database is really just a map between named Object Identifiers
6702      (string) and their OID raw equivalents. Most operations either
6703      convert from string to OID or other way around.
6704     </para>
6705     <para>
6706      Unfortunately, whenever we supply a string we must also specify the
6707      <emphasis>OID class</emphasis>. The class is necessary because some
6708      strings correspond to multiple OIDs. An example of such a string is
6709      <literal>Bib-1</literal> which may either be an attribute-set
6710      or a diagnostic-set.
6711     </para>
6712     <para>
6713      Applications using the YAZ database should include
6714      <filename>yaz/oid_db.h</filename>.
6715     </para>
6716     <para>
6717      A YAZ database handle is of type <literal>yaz_oid_db_t</literal>.
6718      Actually that's a pointer. You need not think deal with that.
6719      YAZ has a built-in database which can be considered "constant" for
6720      most purposes.
6721      We can get hold that by using function <function>yaz_oid_std</function>.
6722     </para>
6723     <para>
6724      All functions with prefix <function>yaz_string_to_oid</function>
6725      converts from class + string to OID. We have variants of this
6726      operation due to different memory allocation strategies.
6727     </para>
6728     <para>
6729      All functions with prefix
6730      <function>yaz_oid_to_string</function> converts from OID to string
6731      + class.
6732     </para>
6733     <example id="tools.oid.bib1.4">
6734      <title>Create OID with YAZ DB</title>
6735      <para>
6736       We can create an OID for the Bib-1 attribute set on the ODR stream
6737       odr with:
6738       <screen>
6739         Odr_oid *bib1 =
6740         yaz_string_to_oid_odr(yaz_oid_std(), CLASS_ATTSET, "Bib-1", odr);
6741       </screen>
6742       This is more complex than using <function>odr_getoidbystr</function>.
6743       You would only use <function>yaz_string_to_oid_odr</function> when the
6744       string (here Bib-1) is supplied by a user or configuration.
6745      </para>
6746     </example>
6747    </sect2>
6748    <sect2 id="tools.oid.std">
6749     <title>Standard OIDs</title>
6750     <para>
6751      All the object identifers in the standard OID database as returned
6752      by <function>yaz_oid_std</function> can referenced directly in a
6753      program as a constant OID.
6754      Each constant OID is prefixed with <literal>yaz_oid_</literal> -
6755      followed by OID class (lowercase) - then by OID name (normalized and
6756      lowercase).
6757     </para>
6758     <para>
6759      See <xref linkend="list-oids"/> for list of all object identifiers
6760      built into YAZ.
6761      These are declared in <filename>yaz/oid_std.h</filename> but are
6762      included by <filename>yaz/oid_db.h</filename> as well.
6763     </para>
6764     <example id="tools.oid.bib1.5">
6765      <title>Use a built-in OID</title>
6766      <para>
6767       We can allocate our own OID filled with the constant OID for
6768       Bib-1 with:
6769       <screen>
6770        Odr_oid *bib1 = odr_oiddup(o, yaz_oid_attset_bib1);
6771       </screen>
6772      </para>
6773     </example>
6774    </sect2>
6775   </sect1>
6776   <sect1 id="tools.nmem">
6777    <title>Nibble Memory</title>
6778    <para>
6779     Sometimes when you need to allocate and construct a large,
6780     interconnected complex of structures, it can be a bit of a pain to
6781     release the associated memory again. For the structures describing the
6782     Z39.50 PDUs and related structures, it is convenient to use the
6783     memory-management system of the &odr; subsystem (see
6784     <xref linkend="odr.use"/>). However, in some circumstances
6785     where you might otherwise benefit from using a simple nibble memory
6786     management system, it may be impractical to use
6787     <function>odr_malloc()</function> and <function>odr_reset()</function>.
6788     For this purpose, the memory manager which also supports the &odr;
6789     streams is made available in the NMEM module. The external interface
6790     to this module is given in the <filename>nmem.h</filename> file.
6791    </para>
6792    <para>
6793     The following prototypes are given:
6794    </para>
6795    <screen>
6796     NMEM nmem_create(void);
6797     void nmem_destroy(NMEM n);
6798     void *nmem_malloc(NMEM n, size_t size);
6799     void nmem_reset(NMEM n);
6800     size_t nmem_total(NMEM n);
6801     void nmem_init(void);
6802     void nmem_exit(void);
6803    </screen>
6804    <para>
6805     The <function>nmem_create()</function> function returns a pointer to a
6806     memory control handle, which can be released again by
6807     <function>nmem_destroy()</function> when no longer needed.
6808     The function <function>nmem_malloc()</function> allocates a block of
6809     memory of the requested size. A call to <function>nmem_reset()</function>
6810     or <function>nmem_destroy()</function> will release all memory allocated
6811     on the handle since it was created (or since the last call to
6812     <function>nmem_reset()</function>. The function
6813     <function>nmem_total()</function> returns the number of bytes currently
6814     allocated on the handle.
6815    </para>
6816    <para>
6817     The nibble memory pool is shared amongst threads. POSIX
6818     mutex'es and WIN32 Critical sections are introduced to keep the
6819     module thread safe. Function <function>nmem_init()</function>
6820     initializes the nibble memory library and it is called automatically
6821     the first time the <literal>YAZ.DLL</literal> is loaded. &yaz; uses
6822     function <function>DllMain</function> to achieve this. You should
6823     <emphasis>not</emphasis> call <function>nmem_init</function> or
6824     <function>nmem_exit</function> unless you're absolute sure what
6825     you're doing. Note that in previous &yaz; versions you'd have to call
6826     <function>nmem_init</function> yourself.
6827    </para>
6828   </sect1>
6829   <sect1 id="tools.log">
6830    <title>Log</title>
6831    <para>
6832     &yaz; has evolved a fairly complex log system which should be useful both
6833     for debugging &yaz; itself, debugging applications that use &yaz;, and for
6834     production use of those applications.
6835    </para>
6836    <para>
6837     The log functions are declared in header <filename>yaz/log.h</filename>
6838     and implemented in <filename>src/log.c</filename>.
6839     Due to name clash with syslog and some math utilities the logging
6840     interface has been modified as of YAZ 2.0.29. The obsolete interface
6841     is still available if in header file <filename>yaz/log.h</filename>.
6842     The key points of the interface are:
6843    </para>
6844    <screen>
6845     void yaz_log(int level, const char *fmt, ...)
6846     void yaz_log_init(int level, const char *prefix, const char *name);
6847     void yaz_log_init_file(const char *fname);
6848     void yaz_log_init_level(int level);
6849     void yaz_log_init_prefix(const char *prefix);
6850     void yaz_log_time_format(const char *fmt);
6851     void yaz_log_init_max_size(int mx);
6852
6853     int yaz_log_mask_str(const char *str);
6854     int yaz_log_module_level(const char *name);
6855    </screen>
6856    <para>
6857     The reason for the whole log module is the <function>yaz_log</function>
6858     function. It takes a bitmask indicating the log levels, a
6859     <literal>printf</literal>-like format string, and a variable number of
6860     arguments to log.
6861    </para>
6862    <para>
6863     The <literal>log level</literal> is a bit mask, that says on which level(s)
6864     the log entry should be made, and optionally set some behaviour of the
6865     logging. In the most simple cases, it can be one of <literal>YLOG_FATAL,
6866     YLOG_DEBUG, YLOG_WARN, YLOG_LOG</literal>. Those can be combined with bits
6867     that modify the way the log entry is written:<literal>YLOG_ERRNO,
6868     YLOG_NOTIME, YLOG_FLUSH</literal>.
6869     Most of the rest of the bits are deprecated, and should not be used. Use
6870     the dynamic log levels instead.
6871    </para>
6872    <para>
6873     Applications that use &yaz;, should not use the LOG_LOG for ordinary
6874     messages, but should make use of the dynamic loglevel system. This consists
6875     of two parts, defining the loglevel and checking it.
6876    </para>
6877    <para>
6878     To define the log levels, the (main) program should pass a string to
6879     <function>yaz_log_mask_str</function> to define which log levels are to be
6880     logged. This string should be a comma-separated list of log level names,
6881     and can contain both hard-coded names and dynamic ones. The log level
6882     calculation starts with <literal>YLOG_DEFAULT_LEVEL</literal> and adds a bit
6883     for each word it meets, unless the word starts with a '-', in which case it
6884     clears the bit. If the string <literal>'none'</literal> is found,
6885     all bits are cleared. Typically this string comes from the command-line,
6886     often identified by <literal>-v</literal>. The
6887     <function>yaz_log_mask_str</function> returns a log level that should be
6888     passed to <function>yaz_log_init_level</function> for it to take effect.
6889    </para>
6890    <para>
6891     Each module should check what log bits it should be used, by calling
6892     <function>yaz_log_module_level</function> with a suitable name for the
6893     module. The name is cleared from a preceding path and an extension, if any,
6894     so it is quite possible to use <literal>__FILE__</literal> for it. If the
6895     name has been passed to <function>yaz_log_mask_str</function>, the routine
6896     returns a non-zero bitmask, which should then be used in consequent calls
6897     to yaz_log. (It can also be tested, so as to avoid unnecessary calls to
6898     yaz_log, in time-critical places, or when the log entry would take time
6899     to construct.)
6900    </para>
6901    <para>
6902     Yaz uses the following dynamic log levels:
6903     <literal>server, session, request, requestdetail</literal> for the server
6904     functionality.
6905     <literal>zoom</literal> for the zoom client api.
6906     <literal>ztest</literal> for the simple test server.
6907     <literal>malloc, nmem, odr, eventl</literal> for internal
6908     debugging of yaz itself.
6909     Of course, any program using yaz is welcome to define as many new
6910     ones, as it needs.
6911    </para>
6912    <para>
6913     By default the log is written to stderr, but this can be changed by a call
6914     to <function>yaz_log_init_file</function> or
6915     <function>yaz_log_init</function>. If the log is directed to a file, the
6916     file size is checked at every write, and if it exceeds the limit given in
6917     <function>yaz_log_init_max_size</function>, the log is rotated. The
6918     rotation keeps one old version (with a <literal>.1</literal> appended to
6919     the name). The size defaults to 1GB. Setting it to zero will disable the
6920     rotation feature.
6921    </para>
6922    <screen>
6923     A typical yaz-log looks like this
6924   13:23:14-23/11 yaz-ztest(1) [session] Starting session from tcp:127.0.0.1 (pid=30968)
6925   13:23:14-23/11 yaz-ztest(1) [request] Init from 'YAZ' (81) (ver 2.0.28) OK
6926   13:23:17-23/11 yaz-ztest(1) [request] Search Z: @attrset Bib-1 foo  OK:7 hits
6927   13:23:22-23/11 yaz-ztest(1) [request] Present: [1] 2+2  OK 2 records returned
6928   13:24:13-23/11 yaz-ztest(1) [request] Close OK
6929    </screen>
6930    <para>
6931     The log entries start with a time stamp. This can be omitted by setting the
6932     <literal>YLOG_NOTIME</literal> bit in the loglevel. This way automatic tests
6933     can be hoped to produce identical log files, that are easy to diff. The
6934     format of the time stamp can be set with
6935     <function>yaz_log_time_format</function>, which takes a format string just
6936     like <function>strftime</function>.
6937    </para>
6938    <para>
6939     Next in a log line comes the prefix, often the name of the program. For
6940     yaz-based servers, it can also contain the session number. Then
6941     comes one or more logbits in square brackets, depending on the logging
6942     level set by <function>yaz_log_init_level</function> and the loglevel
6943     passed to <function>yaz_log_init_level</function>. Finally comes the format
6944     string and additional values passed to <function>yaz_log</function>
6945    </para>
6946    <para>
6947     The log level <literal>YLOG_LOGLVL</literal>, enabled by the string
6948     <literal>loglevel</literal>, will log all the log-level affecting
6949     operations. This can come in handy if you need to know what other log
6950     levels would be useful. Grep the logfile for <literal>[loglevel]</literal>.
6951    </para>
6952    <para>
6953     The log system is almost independent of the rest of &yaz;, the only
6954     important dependence is of <filename>nmem</filename>, and that only for
6955     using the semaphore definition there.
6956    </para>
6957    <para>
6958     The dynamic log levels and log rotation were introduced in &yaz; 2.0.28. At
6959     the same time, the log bit names were changed from
6960     <literal>LOG_something</literal> to <literal>YLOG_something</literal>,
6961     to avoid collision with <filename>syslog.h</filename>.
6962    </para>
6963   </sect1>
6964   <sect1 id="marc">
6965    <title>MARC</title>
6966    <para>
6967     YAZ provides a fast utility for working with MARC records.
6968     Early versions of the MARC utility only allowed decoding of ISO2709.
6969     Today the utility may both encode - and decode to a varity of formats.
6970    </para>
6971    <synopsis><![CDATA[
6972     #include <yaz/marcdisp.h>
6973
6974     /* create handler */
6975     yaz_marc_t yaz_marc_create(void);
6976     /* destroy */
6977     void yaz_marc_destroy(yaz_marc_t mt);
6978
6979     /* set XML mode YAZ_MARC_LINE, YAZ_MARC_SIMPLEXML, ... */
6980     void yaz_marc_xml(yaz_marc_t mt, int xmlmode);
6981     #define YAZ_MARC_LINE      0
6982     #define YAZ_MARC_SIMPLEXML 1
6983     #define YAZ_MARC_OAIMARC   2
6984     #define YAZ_MARC_MARCXML   3
6985     #define YAZ_MARC_ISO2709   4
6986     #define YAZ_MARC_XCHANGE   5
6987     #define YAZ_MARC_CHECK     6
6988     #define YAZ_MARC_TURBOMARC 7
6989     #define YAZ_MARC_JSON      8
6990
6991     /* supply iconv handle for character set conversion .. */
6992     void yaz_marc_iconv(yaz_marc_t mt, yaz_iconv_t cd);
6993
6994     /* set debug level, 0=none, 1=more, 2=even more, .. */
6995     void yaz_marc_debug(yaz_marc_t mt, int level);
6996
6997     /* decode MARC in buf of size bsize. Returns >0 on success; <=0 on failure.
6998     On success, result in *result with size *rsize. */
6999     int yaz_marc_decode_buf(yaz_marc_t mt, const char *buf, int bsize,
7000                             const char **result, size_t *rsize);
7001
7002     /* decode MARC in buf of size bsize. Returns >0 on success; <=0 on failure.
7003        On success, result in WRBUF */
7004     int yaz_marc_decode_wrbuf(yaz_marc_t mt, const char *buf,
7005                               int bsize, WRBUF wrbuf);
7006 ]]>
7007    </synopsis>
7008    <note>
7009     <para>
7010      The synopsis is just a basic subset of all functionality. Refer
7011      to the actual header file <filename>marcdisp.h</filename> for
7012      details.
7013     </para>
7014    </note>
7015    <para>
7016     A MARC conversion handle must be created by using
7017     <function>yaz_marc_create</function> and destroyed
7018     by calling <function>yaz_marc_destroy</function>.
7019    </para>
7020    <para>
7021     All other function operate on a <literal>yaz_marc_t</literal> handle.
7022     The output is specified by a call to <function>yaz_marc_xml</function>.
7023     The <literal>xmlmode</literal> must be one of
7024     <variablelist>
7025      <varlistentry>
7026       <term>YAZ_MARC_LINE</term>
7027       <listitem>
7028        <para>
7029         A simple line-by-line format suitable for display but not
7030         recommend for further (machine) processing.
7031        </para>
7032       </listitem>
7033      </varlistentry>
7034      <varlistentry>
7035       <term>YAZ_MARC_MARCXML</term>
7036       <listitem>
7037        <para>
7038         <ulink url="&url.marcxml;">MARCXML</ulink>.
7039        </para>
7040       </listitem>
7041      </varlistentry>
7042      <varlistentry>
7043       <term>YAZ_MARC_ISO2709</term>
7044       <listitem>
7045        <para>
7046         ISO2709 (sometimes just referred to as "MARC").
7047        </para>
7048       </listitem>
7049      </varlistentry>
7050      <varlistentry>
7051       <term>YAZ_MARC_XCHANGE</term>
7052       <listitem>
7053        <para>
7054         <ulink url="&url.marcxchange;">MarcXchange</ulink>.
7055        </para>
7056       </listitem>
7057      </varlistentry>
7058      <varlistentry>
7059       <term>YAZ_MARC_CHECK</term>
7060       <listitem>
7061        <para>
7062         Pseudo format for validation only. Does not generate
7063         any real output except diagnostics.
7064        </para>
7065       </listitem>
7066      </varlistentry>
7067      <varlistentry>
7068       <term>YAZ_MARC_TURBOMARC</term>
7069       <listitem>
7070        <para>
7071         XML format with same semantics as MARCXML but more compact
7072         and geared towards fast processing with XSLT. Refer to
7073         <xref linkend="tools.turbomarc"/> for more information.
7074        </para>
7075       </listitem>
7076      </varlistentry>
7077      <varlistentry>
7078       <term>YAZ_MARC_JSON</term>
7079       <listitem>
7080        <para>
7081         <ulink url="&url.marc_in_json;">MARC-in_JSON</ulink> format.
7082        </para>
7083       </listitem>
7084      </varlistentry>
7085     </variablelist>
7086    </para>
7087    <para>
7088     The actual conversion functions are
7089     <function>yaz_marc_decode_buf</function> and
7090     <function>yaz_marc_decode_wrbuf</function> which decodes and encodes
7091     a MARC record. The former function operates on simple buffers, the
7092     stores the resulting record in a WRBUF handle (WRBUF is a simple string
7093     type).
7094    </para>
7095    <example id="example.marc.display">
7096     <title>Display of MARC record</title>
7097     <para>
7098      The following program snippet illustrates how the MARC API may
7099      be used to convert a MARC record to the line-by-line format:
7100      <programlisting><![CDATA[
7101       void print_marc(const char *marc_buf, int marc_buf_size)
7102       {
7103          char *result;      /* for result buf */
7104          size_t result_len;    /* for size of result */
7105          yaz_marc_t mt = yaz_marc_create();
7106          yaz_marc_xml(mt, YAZ_MARC_LINE);
7107          yaz_marc_decode_buf(mt, marc_buf, marc_buf_size,
7108                              &result, &result_len);
7109          fwrite(result, result_len, 1, stdout);
7110          yaz_marc_destroy(mt);  /* note that result is now freed... */
7111       }
7112 ]]>
7113      </programlisting>
7114     </para>
7115    </example>
7116    <sect2 id="tools.turbomarc">
7117     <title>TurboMARC</title>
7118     <para>
7119      TurboMARC is yet another XML encoding of a MARC record. The format
7120      was designed for fast processing with XSLT.
7121     </para>
7122     <para>
7123      Applications like
7124      Pazpar2 uses XSLT to convert an XML encoded MARC record to an internal
7125      representation. This conversion mostly check the tag of a MARC field
7126      to determine the basic rules in the conversion. This check is
7127      costly when that is tag is encoded as an attribute in MARCXML.
7128      By having the tag value as the element instead, makes processing
7129      many times faster (at least for Libxslt).
7130     </para>
7131     <para>
7132      TurboMARC is encoded as follows:
7133      <itemizedlist>
7134       <listitem>
7135        <para>
7136         Record elements is part of namespace
7137         "<literal>http://www.indexdata.com/turbomarc</literal>".
7138       </para>
7139       </listitem>
7140       <listitem>
7141        <para>
7142         A record is enclosed in element <literal>r</literal>.
7143       </para>
7144       </listitem>
7145       <listitem>
7146        <para>
7147         A collection of records is enclosed in element
7148         <literal>collection</literal>.
7149        </para>
7150       </listitem>
7151       <listitem>
7152        <para>
7153         The leader is encoded as element <literal>l</literal> with the
7154         leader content as its (text) value.
7155       </para>
7156       </listitem>
7157       <listitem>
7158        <para>
7159         A control field is encoded as element <literal>c</literal> concatenated
7160         with the tag value of the control field if the tag value
7161         matches the regular expression <literal>[a-zA-Z0-9]*</literal>.
7162         If the tag value do not match the regular expression
7163         <literal>[a-zA-Z0-9]*</literal> the control field is encoded
7164         as element <literal>c</literal> and attribute <literal>code</literal>
7165         will hold the tag value.
7166         This rule ensure that in the rare cases where a tag value might
7167         result in a non-wellformed XML YAZ encode it as a coded attribute
7168         (as in MARCXML).
7169        </para>
7170        <para>
7171         The control field content is the the text value of this element.
7172         Indicators are encoded as attribute names
7173         <literal>i1</literal>, <literal>i2</literal>, etc.. and
7174         corresponding values for each indicator.
7175        </para>
7176       </listitem>
7177       <listitem>
7178        <para>
7179         A data field is encoded as element <literal>d</literal> concatenated
7180         with the tag value of the data field or using the attribute
7181         <literal>code</literal> as described in the rules for control fields.
7182         The children of the data field element is subfield elements.
7183         Each subfield element is encoded as <literal>s</literal>
7184         concatenated with the sub field code.
7185         The text of the subfield element is the contents of the subfield.
7186         Indicators are encoded as attributes for the data field element similar
7187         to the encoding for control fields.
7188       </para>
7189       </listitem>
7190      </itemizedlist>
7191     </para>
7192    </sect2>
7193   </sect1>
7194   <sect1 id="tools.retrieval">
7195    <title>Retrieval Facility</title>
7196    <para>
7197     YAZ version 2.1.20 or later includes a Retrieval facility tool
7198     which allows a SRU/Z39.50 to describe itself and perform record
7199     conversions. The idea is the following:
7200     <itemizedlist>
7201      <listitem>
7202       <para>
7203        An SRU/Z39.50 client sends a retrieval request which includes
7204        a combination of the following parameters: syntax (format),
7205        schema (or element set name).
7206       </para>
7207      </listitem>
7208      <listitem>
7209       <para>
7210        The retrieval facility is invoked with parameters in a
7211        server/proxy. The retrieval facility matches the parameters a set of
7212        "supported" retrieval types.
7213        If there is no match, the retrieval signals an error
7214        (syntax and / or schema not supported).
7215       </para>
7216      </listitem>
7217      <listitem>
7218       <para>
7219        For a successful match, the backend is invoked with the same
7220        or altered retrieval parameters (syntax, schema). If
7221        a record is received from the backend, it is converted to the
7222        frontend name / syntax.
7223       </para>
7224      </listitem>
7225      <listitem>
7226       <para>
7227        The resulting record is sent back the client and tagged with
7228        the frontend syntax / schema.
7229       </para>
7230      </listitem>
7231     </itemizedlist>
7232    </para>
7233    <para>
7234     The Retrieval facility is driven by an XML configuration. The
7235     configuration is neither Z39.50 ZeeRex or SRU ZeeRex. But it
7236     should be easy to generate both of them from the XML configuration.
7237     (unfortunately the two versions
7238     of ZeeRex differ substantially in this regard).
7239    </para>
7240    <sect2 id="tools.retrieval.format">
7241     <title>Retrieval XML format</title>
7242     <para>
7243      All elements should be covered by namespace
7244      <literal>http://indexdata.com/yaz</literal> .
7245      The root element node must be <literal>retrievalinfo</literal>.
7246     </para>
7247     <para>
7248      The <literal>retrievalinfo</literal> must include one or
7249      more <literal>retrieval</literal> elements. Each
7250     <literal>retrieval</literal> defines specific combination of
7251      syntax, name and identifier supported by this retrieval service.
7252     </para>
7253     <para>
7254      The <literal>retrieval</literal> element may include any of the
7255      following attributes:
7256      <variablelist>
7257       <varlistentry><term><literal>syntax</literal> (REQUIRED)</term>
7258        <listitem>
7259         <para>
7260          Defines the record syntax. Possible values is any
7261          of the names defined in YAZ' OID database or a raw
7262          OID in (n.n ... n).
7263         </para>
7264        </listitem>
7265       </varlistentry>
7266       <varlistentry><term><literal>name</literal> (OPTIONAL)</term>
7267        <listitem>
7268         <para>
7269          Defines the name of the retrieval format. This can be
7270          any string. For SRU, the value, is equivalent to schema (short-hand);
7271          for Z39.50 it's equivalent to simple element set name.
7272          For YAZ 3.0.24 and later this name may be specified as a glob
7273          expression with operators
7274          <literal>*</literal> and <literal>?</literal>.
7275         </para>
7276        </listitem>
7277       </varlistentry>
7278       <varlistentry><term><literal>identifier</literal> (OPTIONAL)</term>
7279        <listitem>
7280         <para>
7281          Defines the URI schema name of the retrieval format. This can be
7282          any string. For SRU, the value, is equivalent to URI schema.
7283          For Z39.50, there is no equivalent.
7284         </para>
7285        </listitem>
7286       </varlistentry>
7287      </variablelist>
7288     </para>
7289     <para>
7290      The <literal>retrieval</literal> may include one
7291      <literal>backend</literal> element. If a <literal>backend</literal>
7292      element is given, it specifies how the records are retrieved by
7293      some backend and how the records are converted from the backend to
7294      the "frontend".
7295     </para>
7296     <para>
7297      The attributes, <literal>name</literal> and <literal>syntax</literal>
7298      may be specified for the <literal>backend</literal> element. These
7299      semantics of these attributes is equivalent to those for the
7300      <literal>retrieval</literal>. However, these values are passed to
7301      the "backend".
7302     </para>
7303     <para>
7304      The <literal>backend</literal> element may includes one or more
7305      conversion instructions (as children elements). The supported
7306      conversions are:
7307      <variablelist>
7308       <varlistentry><term><literal>marc</literal></term>
7309        <listitem>
7310         <para>
7311          The <literal>marc</literal> element specifies a conversion
7312          to - and from ISO2709 encoded MARC and
7313          <ulink url="&url.marcxml;">&acro.marcxml;</ulink>/MarcXchange.
7314          The following attributes may be specified:
7315          <variablelist>
7316           <varlistentry>
7317            <term><literal>inputformat</literal> (REQUIRED)</term>
7318            <listitem>
7319             <para>
7320              Format of input. Supported values are
7321              <literal>marc</literal> (for ISO2709), <literal>xml</literal>
7322              (MARCXML/MarcXchange) and <literal>json</literal>
7323              (<ulink url="&url.marc_in_json;">MARC-in_JSON</ulink>).
7324             </para>
7325            </listitem>
7326           </varlistentry>
7327           <varlistentry>
7328            <term><literal>outputformat</literal> (REQUIRED)</term>
7329            <listitem>
7330             <para>
7331              Format of output. Supported values are
7332              <literal>line</literal> (MARC line format);
7333              <literal>marcxml</literal> (for MARCXML),
7334              <literal>marc</literal> (ISO2709),
7335              <literal>marcxhcange</literal> (for MarcXchange),
7336              or <literal>json</literal>
7337              (<ulink url="&url.marc_in_json;">MARC-in_JSON </ulink>).
7338             </para>
7339            </listitem>
7340           </varlistentry>
7341           <varlistentry>
7342            <term><literal>inputcharset</literal> (OPTIONAL)</term>
7343            <listitem>
7344             <para>
7345              Encoding of input. For XML input formats, this need not
7346              be given, but for ISO2709 based inputformats, this should
7347              be set to the encoding used. For MARC21 records, a common
7348              inputcharset value  would be <literal>marc-8</literal>.
7349             </para>
7350            </listitem>
7351           </varlistentry>
7352           <varlistentry>
7353            <term><literal>outputcharset</literal> (OPTIONAL)</term>
7354            <listitem>
7355             <para>
7356              Encoding of output. If outputformat is XML based, it is
7357              strongly recommened to use <literal>utf-8</literal>.
7358             </para>
7359            </listitem>
7360           </varlistentry>
7361          </variablelist>
7362         </para>
7363        </listitem>
7364       </varlistentry>
7365       <varlistentry>
7366        <term><literal>xslt</literal></term>
7367        <listitem>
7368         <para>
7369          The <literal>xslt</literal> element specifies a conversion
7370          via &acro.xslt;. The following attributes may be specified:
7371          <variablelist>
7372           <varlistentry><term><literal>stylesheet</literal> (REQUIRED)</term>
7373            <listitem>
7374             <para>
7375              Stylesheet file.
7376             </para>
7377            </listitem>
7378           </varlistentry>
7379          </variablelist>
7380         </para>
7381        </listitem>
7382       </varlistentry>
7383       <varlistentry>
7384        <term><literal>solrmarc</literal></term>
7385        <listitem>
7386         <para>
7387          The <literal>solrmarc</literal> decodes solrmarc records.
7388          It assumes that the input is pure solrmarc text (no escaping)
7389          and will convert all sequences of the form #XX; to a single
7390          character of the hexadecimal value as given by XX. The output,
7391          presumably, is a valid ISO2709 buffer.
7392         </para>
7393         <para>
7394          This conversion is available in YAZ 5.0.21 and later.
7395         </para>
7396        </listitem>
7397       </varlistentry>
7398      </variablelist>
7399     </para>
7400    </sect2>
7401    <sect2 id="tools.retrieval.examples">
7402     <title>Retrieval Facility Examples</title>
7403     <example id="tools.retrieval.marc21">
7404      <title>MARC21 backend</title>
7405      <para>
7406       A typical way to use the retrieval facility is to enable XML
7407       for servers that only supports ISO2709 encoded MARC21 records.
7408      </para>
7409      <programlisting><![CDATA[
7410      <retrievalinfo>
7411        <retrieval syntax="usmarc" name="F"/>
7412        <retrieval syntax="usmarc" name="B"/>
7413        <retrieval syntax="xml" name="marcxml"
7414                   identifier="info:srw/schema/1/marcxml-v1.1">
7415          <backend syntax="usmarc" name="F">
7416            <marc inputformat="marc" outputformat="marcxml"
7417                  inputcharset="marc-8"/>
7418          </backend>
7419        </retrieval>
7420        <retrieval syntax="xml" name="dc">
7421          <backend syntax="usmarc" name="F">
7422            <marc inputformat="marc" outputformat="marcxml"
7423                  inputcharset="marc-8"/>
7424            <xslt stylesheet="MARC21slim2DC.xsl"/>
7425          </backend>
7426        </retrieval>
7427      </retrievalinfo>
7428 ]]>
7429      </programlisting>
7430      <para>
7431       This means that our frontend supports:
7432       <itemizedlist>
7433        <listitem>
7434         <para>
7435          MARC21 F(ull) records.
7436         </para>
7437        </listitem>
7438        <listitem>
7439         <para>
7440          MARC21 B(rief) records.
7441         </para>
7442        </listitem>
7443        <listitem>
7444         <para>
7445          MARCXML records.
7446         </para>
7447        </listitem>
7448        <listitem>
7449         <para>
7450          Dublin core records.
7451         </para>
7452        </listitem>
7453       </itemizedlist>
7454      </para>
7455     </example>
7456     <example id="tools.retrieval.marcxml">
7457      <title>MARCXML backend</title>
7458      <para>
7459       SRW/SRU and Solr backends returns records in XML.
7460       If they return MARCXML or MarcXchange, the retrieval module
7461       can convert those into ISO2709 formats, most commonly USMARC
7462       (AKA MARC21).
7463       In this example, the backend returns MARCXML for schema="marcxml".
7464      </para>
7465      <programlisting><![CDATA[
7466      <retrievalinfo>
7467        <retrieval syntax="usmarc">
7468          <backend syntax="xml" name="marcxml">
7469            <marc inputformat="xml" outputformat="marc"
7470                  outputcharset="marc-8"/>
7471          </backend>
7472        </retrieval>
7473        <retrieval syntax="xml" name="marcxml"
7474                   identifier="info:srw/schema/1/marcxml-v1.1"/>
7475        <retrieval syntax="xml" name="dc">
7476          <backend syntax="xml" name="marcxml">
7477            <xslt stylesheet="MARC21slim2DC.xsl"/>
7478          </backend>
7479        </retrieval>
7480      </retrievalinfo>
7481 ]]>
7482      </programlisting>
7483      <para>
7484       This means that our frontend supports:
7485       <itemizedlist>
7486        <listitem>
7487         <para>
7488          MARC21 records (any element set name) in MARC-8 encoding.
7489         </para>
7490        </listitem>
7491        <listitem>
7492         <para>
7493          MARCXML records for element-set=marcxml
7494         </para>
7495        </listitem>
7496        <listitem>
7497         <para>
7498          Dublin core records for element-set=dc.
7499         </para>
7500        </listitem>
7501       </itemizedlist>
7502      </para>
7503     </example>
7504    </sect2>
7505    <sect2 id="tools.retrieval.api">
7506     <title>API</title>
7507     <para>
7508      It should be easy to use the retrieval systems from applications. Refer
7509      to the headers
7510      <filename>yaz/retrieval.h</filename> and
7511      <filename>yaz/record_conv.h</filename>.
7512     </para>
7513    </sect2>
7514   </sect1>
7515   <sect1 id="sorting">
7516    <title>Sorting</title>
7517    <para>
7518     This chapter describes sorting and how it is supported in YAZ.
7519     Sorting applies to a result-set.
7520     The
7521     <ulink url="http://www.loc.gov/z3950/agency/markup/05.html#3.2.7">
7522      Z39.50 sorting facility
7523     </ulink>
7524     takes one or more input result-sets
7525     and one result-set as output. The most simple case is that
7526     the input-set is the same as the output-set.
7527    </para>
7528    <para>
7529     Z39.50 sorting has a separate APDU (service) that is, thus, performed
7530     following a search (two phases).
7531    </para>
7532    <para>
7533     In SRU/Solr, however, the model is different. Here, sorting is specified
7534     during the the search operation. Note, however, that SRU might
7535     perform sort as separate search, by referring to an existing result-set
7536     in the query (result-set reference).
7537    </para>
7538    <sect2>
7539     <title>Using the Z39.50 sort service</title>
7540     <para>
7541      yaz-client and the ZOOM API supports the Z39.50 sort facility. In any
7542      case the sort sequence or sort critiera is using a string notation.
7543      This notation is a one-line notation suitable for being manually
7544      entered or generated and allows for easy logging (one liner).
7545      For the ZOOM API, the sort is specified in the call to ZOOM_query_sortby
7546      function. For yaz-client the sort is performed and specified using
7547      the sort and sort+ commands. For description of the sort criteria notation
7548      refer to the <link linkend="sortspec">sort command</link> in the
7549      yaz-client manual.
7550     </para>
7551     <para>
7552      The ZOOM API might choose one of several sort strategies for
7553      sorting. Refer to <xref linkend="zoom-sort-strategy"/>.
7554     </para>
7555    </sect2>
7556    <sect2>
7557     <title>Type-7 sort</title>
7558     <para>
7559      Type-7 sort is an extension to the Bib-1 based RPN query where the
7560      sort specification is embedded as an Attribute-Plus-Term.
7561     </para>
7562     <para>
7563      The objectives for introducing Type-7 sorting is that it allows
7564      a client to perform sorting even if it does not implement/support
7565      Z39.50 sort. Virtually all Z39.50 client software supports
7566      RPN queries. It also may improve performance because the sort
7567      critieria is specified along with the search query.
7568     </para>
7569     <para>
7570      The sort is triggered by the presence of type 7 and the value of type 7
7571      specifies the
7572      <ulink url="http://www.loc.gov/z3950/agency/asn1.html#SortKeySpec">
7573       sortRelation
7574      </ulink>
7575      The value for type 7 is 1 for ascending and 2 for descending.
7576      For the
7577      <ulink url="http://www.loc.gov/z3950/agency/asn1.html#SortElement">
7578       sortElement
7579      </ulink>
7580      only the generic part is handled. If generic sortKey is of type
7581      sortField, then attribute type 1 is present and the value is
7582      sortField (InternationalString). If generic sortKey is of type
7583      sortAttributes, then the attributes in list is used . generic sortKey
7584      of type elementSpec is not supported.
7585     </para>
7586     <para>
7587      The term in the sorting Attribute-Plus-Term combo should hold
7588      an integer. The value is 0 for primary sorting criteria, 1 for second
7589      criteria, etc.
7590     </para>
7591    </sect2>
7592   </sect1>
7593   <sect1 id="facets">
7594    <title>Facets</title>
7595    <para>
7596     YAZ supports facets for in Solr, SRU 2.0 and Z39.50 protocols.
7597    </para>
7598    <para>
7599     Like Type-1/RPN, YAZ supports a string notation for specifying
7600     facets. For the API this is performed by
7601     <function>yaz_pqf_parse_facet_list</function>.
7602    </para>
7603    <para>
7604     For ZOOM C the facets are given by option "facets"
7605     For yaz-client it is used for the facets command.
7606    </para>
7607    <para>
7608     The grammar of this specification is as follows:
7609     <literallayout>
7610    facet-spec ::= facet-list
7611
7612    facet-list ::= facet-list ',' attr-spec | attr-spec
7613
7614    attr-spec ::= attr-spec '@attr' string | '@attr' string
7615
7616     </literallayout>
7617     The notation is inspired by PQF. The string following '@attr'
7618     may not include blanks and is of the form
7619     <replaceable>type</replaceable><literal>=</literal><replaceable>value</replaceable>,
7620     where <replaceable>type</replaceable> is an integer and
7621     <replaceable>value</replaceable> is a string or an integer.
7622    </para>
7623    <para>
7624     The Facets specification is not Bib-1. The following types apply:
7625    </para>
7626    <table id="facet.attributes">
7627     <title>Facet attributes</title>
7628     <tgroup cols="2">
7629      <colspec colwidth="2*" colname="type"></colspec>
7630      <colspec colwidth="9*" colname="description"></colspec>
7631      <thead>
7632       <row>
7633        <entry>Type</entry>
7634        <entry>Description</entry>
7635       </row>
7636      </thead>
7637      <tbody>
7638       <row>
7639        <entry>1</entry>
7640        <entry>
7641         Field-name. This is often a string, eg "Author", "Year", etc.
7642        </entry>
7643       </row>
7644       <row>
7645        <entry>2</entry>
7646        <entry>
7647         Sort order. Value should be an integer.
7648         Value 0: count descending (frequency). Value 1: alpha ascending.
7649        </entry>
7650       </row>
7651       <row>
7652        <entry>3</entry>
7653        <entry>
7654         Number of terms requested.
7655        </entry>
7656       </row>
7657       <row>
7658        <entry>4</entry>
7659        <entry>
7660         Start offset.
7661        </entry>
7662       </row>
7663      </tbody>
7664     </tgroup>
7665    </table>
7666   </sect1>
7667  </chapter>
7668  <chapter id="odr">
7669   <title>The ODR Module</title>
7670   <sect1 id="odr.introduction">
7671    <title>Introduction</title>
7672    <para>
7673     &odr; is the BER-encoding/decoding subsystem of &yaz;. Care as been taken
7674     to isolate &odr; from the rest of the package - specifically from the
7675     transport interface. &odr; may be used in any context where basic
7676     ASN.1/BER representations are used.
7677    </para>
7678    <para>
7679     If you are only interested in writing a Z39.50 implementation based on
7680     the PDUs that are already provided with &yaz;, you only need to concern
7681     yourself with the section on managing ODR streams
7682     (<xref linkend="odr.use"/>). Only if you need to
7683     implement ASN.1 beyond that which has been provided, should you
7684     worry about the second half of the documentation
7685     (<xref linkend="odr.programming"/>).
7686     If you use one of the higher-level interfaces, you can skip this
7687     section entirely.
7688    </para>
7689    <para>
7690     This is important, so we'll repeat it for emphasis: <emphasis>You do
7691     not need to read <xref linkend="odr.programming"/>
7692     to implement Z39.50 with &yaz;.</emphasis>
7693    </para>
7694    <para>
7695     If you need a part of the protocol that isn't already in &yaz;, you
7696     should contact the authors before going to work on it yourself: We
7697     might already be working on it. Conversely, if you implement a useful
7698     part of the protocol before us, we'd be happy to include it in a
7699     future release.
7700    </para>
7701   </sect1>
7702   <sect1 id="odr.use">
7703    <title>Using ODR</title>
7704    <sect2 id="odr.streams">
7705     <title>ODR Streams</title>
7706     <para>
7707      Conceptually, the ODR stream is the source of encoded data in the
7708      decoding mode; when encoding, it is the receptacle for the encoded
7709      data. Before you can use an ODR stream it must be allocated. This is
7710      done with the function
7711     </para>
7712     <synopsis>
7713      ODR odr_createmem(int direction);
7714     </synopsis>
7715     <para>
7716      The <function>odr_createmem()</function> function takes as argument one
7717      of three manifest constants: <literal>ODR_ENCODE</literal>,
7718      <literal>ODR_DECODE</literal>, or <literal>ODR_PRINT</literal>.
7719      An &odr; stream can be in only one mode - it is not possible to change
7720      its mode once it's selected. Typically, your program will allocate
7721      at least two ODR streams - one for decoding, and one for encoding.
7722     </para>
7723     <para>
7724      When you're done with the stream, you can use
7725     </para>
7726     <synopsis>
7727      void odr_destroy(ODR o);
7728     </synopsis>
7729     <para>
7730      to release the resources allocated for the stream.
7731     </para>
7732    </sect2>
7733    <sect2 id="odr.memory.management">
7734     <title id="memory">Memory Management</title>
7735     <para>
7736      Two forms of memory management take place in the &odr; system. The first
7737      one, which has to do with allocating little bits of memory (sometimes
7738      quite large bits of memory, actually) when a protocol package is
7739      decoded, and turned into a complex of interlinked structures. This
7740      section deals with this system, and how you can use it for your own
7741      purposes. The next section deals with the memory management which is
7742      required when encoding data - to make sure that a large enough buffer is
7743      available to hold the fully encoded PDU.
7744     </para>
7745     <para>
7746      The &odr; module has its own memory management system, which is
7747      used whenever memory is required. Specifically, it is used to allocate
7748      space for data when decoding incoming PDUs. You can use the memory
7749      system for your own purposes, by using the function
7750     </para>
7751     <synopsis>
7752      void *odr_malloc(ODR o, size_t size);
7753     </synopsis>
7754     <para>
7755      You can't use the normal <function>free(2)</function> routine to free
7756      memory allocated by this function, and &odr; doesn't provide a parallel
7757      function. Instead, you can call
7758     </para>
7759     <synopsis>
7760      void odr_reset(ODR o);
7761     </synopsis>
7762     <para>
7763      when you are done with the
7764      memory: Everything allocated since the last call to
7765      <function>odr_reset()</function> is released.
7766      The <function>odr_reset()</function> call is also required to clear
7767      up an error condition on a stream.
7768     </para>
7769     <para>
7770      The function
7771     </para>
7772     <synopsis>
7773      size_t odr_total(ODR o);
7774     </synopsis>
7775     <para>
7776      returns the number of bytes allocated on the stream since the last call to
7777      <function>odr_reset()</function>.
7778     </para>
7779     <para>
7780      The memory subsystem of &odr; is fairly efficient at allocating and
7781      releasing little bits of memory. Rather than managing the individual,
7782      small bits of space, the system maintains a free-list of larger chunks
7783      of memory, which are handed out in small bits. This scheme is
7784      generally known as a <emphasis>nibble memory</emphasis> system.
7785      It is very useful for maintaining short-lived constructions such
7786      as protocol PDUs.
7787     </para>
7788     <para>
7789      If you want to retain a bit of memory beyond the next call to
7790      <function>odr_reset()</function>, you can use the function
7791     </para>
7792     <synopsis>
7793      ODR_MEM odr_extract_mem(ODR o);
7794     </synopsis>
7795     <para>
7796      This function will give you control of the memory recently allocated
7797      on the ODR stream. The memory will live (past calls to
7798      <function>odr_reset()</function>), until you call the function
7799     </para>
7800     <synopsis>
7801      void odr_release_mem(ODR_MEM p);
7802     </synopsis>
7803     <para>
7804      The opaque <literal>ODR_MEM</literal> handle has no other purpose than
7805      referencing the memory block for you until you want to release it.
7806     </para>
7807     <para>
7808      You can use <function>odr_extract_mem()</function> repeatedly between
7809      allocating data, to retain individual control of separate chunks of data.
7810     </para>
7811    </sect2>
7812    <sect2 id="odr.encoding.and.decoding">
7813     <title>Encoding and Decoding Data</title>
7814     <para>
7815      When encoding data, the ODR stream will write the encoded octet string
7816      in an internal buffer. To retrieve the data, use the function
7817     </para>
7818     <synopsis>
7819      char *odr_getbuf(ODR o, int *len, int *size);
7820     </synopsis>
7821     <para>
7822      The integer pointed to by len is set to the length of the encoded
7823      data, and a pointer to that data is returned. <literal>*size</literal>
7824      is set to the size of the buffer (unless <literal>size</literal> is null,
7825      signaling that you are not interested in the size). The next call to
7826      a primitive function using the same &odr; stream will overwrite the
7827      data, unless a different buffer has been supplied using the call
7828     </para>
7829     <synopsis>
7830      void odr_setbuf(ODR o, char *buf, int len, int can_grow);
7831     </synopsis>
7832     <para>
7833      which sets the encoding (or decoding) buffer used by
7834      <literal>o</literal> to <literal>buf</literal>, using the length
7835      <literal>len</literal>.
7836      Before a call to an encoding function, you can use
7837      <function>odr_setbuf()</function> to provide the stream with an encoding
7838      buffer of sufficient size (length). The <literal>can_grow</literal>
7839      parameter tells the encoding &odr; stream whether it is allowed to use
7840      <function>realloc(2)</function> to increase the size of the buffer when
7841      necessary. The default condition of a new encoding stream is equivalent
7842      to the results of calling
7843     </para>
7844     <synopsis>
7845      odr_setbuf(stream, 0, 0, 1);
7846     </synopsis>
7847     <para>
7848      In this case, the stream will allocate and reallocate memory as
7849      necessary. The stream reallocates memory by repeatedly doubling the
7850      size of the buffer - the result is that the buffer will typically
7851      reach its maximum, working size with only a small number of reallocation
7852      operations. The memory is freed by the stream when the latter is destroyed,
7853      unless it was assigned by the user with the <literal>can_grow</literal>
7854      parameter set to zero (in this case, you are expected to retain
7855      control of the memory yourself).
7856     </para>
7857     <para>
7858      To assume full control of an encoded buffer, you must first call
7859      <function>odr_getbuf()</function> to fetch the buffer and its length.
7860      Next, you should call <function>odr_setbuf()</function> to provide a
7861      different buffer (or a null pointer) to the stream. In the simplest
7862      case, you will reuse the same buffer over and over again, and you
7863      will just need to call <function>odr_getbuf()</function> after each
7864      encoding operation to get the length and address of the buffer.
7865      Note that the stream may reallocate the buffer during an encoding
7866      operation, so it is necessary to retrieve the correct address after
7867      each encoding operation.
7868     </para>
7869     <para>
7870      It is important to realize that the ODR stream will not release this
7871      memory when you call <function>odr_reset()</function>: It will
7872      merely update its internal pointers to prepare for the encoding of a
7873      new data value.
7874      When the stream is released by the <function>odr_destroy()</function>
7875      function, the memory given to it by <function>odr_setbuf</function> will
7876      be released <emphasis>only</emphasis> if the <literal>can_grow</literal>
7877      parameter to <function>odr_setbuf()</function> was nonzero. The
7878      <literal>can_grow</literal> parameter, in other words, is a way of
7879      signaling who is to own the buffer, you or the ODR stream. If you never call
7880      <function>odr_setbuf()</function> on your encoding stream, which is
7881      typically the case, the buffer allocated by the stream will belong to
7882      the stream by default.
7883     </para>
7884     <para>
7885      When you wish to decode data, you should first call
7886      <function>odr_setbuf()</function>, to tell the decoding stream
7887      where to find the encoded data, and how long the buffer is
7888      (the <literal>can_grow</literal> parameter is ignored by a decoding
7889      stream). After this, you can call the function corresponding to the
7890      data you wish to decode (eg, <function>odr_integer()</function> odr
7891      <function>z_APDU()</function>).
7892     </para>
7893     <example id="example.odr.encoding.and.decoding.functions">
7894      <title>Encoding and decoding functions</title>
7895      <synopsis>
7896       int odr_integer(ODR o, Odr_int **p, int optional, const char *name);
7897
7898       int z_APDU(ODR o, Z_APDU **p, int optional, const char *name);
7899      </synopsis>
7900     </example>
7901     <para>
7902      If the data is absent (or doesn't match the tag corresponding to
7903      the type), the return value will be either 0 or 1 depending on the
7904      <literal>optional</literal> flag. If <literal>optional</literal>
7905      is 0 and the data is absent, an error flag will be raised in the
7906      stream, and you'll need to call <function>odr_reset()</function> before
7907      you can use the stream again. If <literal>optional</literal> is
7908      nonzero, the pointer <emphasis>pointed</emphasis> to/ by
7909      <literal>p</literal> will be set to the null value, and the function
7910      will return 1.
7911      The <literal>name</literal> argument is used to pretty-print the
7912      tag in question. It may be set to <literal>NULL</literal> if
7913      pretty-printing is not desired.
7914     </para>
7915     <para>
7916      If the data value is found where it's expected, the pointer
7917      <emphasis>pointed to</emphasis> by the <literal>p</literal> argument
7918      will be set to point to the decoded type.
7919      The space for the type will be allocated and owned by the &odr;
7920      stream, and it will live until you call
7921      <function>odr_reset()</function> on the stream. You cannot use
7922      <function>free(2)</function> to release the memory.
7923      You can decode several data elements (by repeated calls to
7924      <function>odr_setbuf()</function> and your decoding function), and
7925      new memory will be allocated each time. When you do call
7926      <function>odr_reset()</function>, everything decoded since the
7927      last call to <function>odr_reset()</function> will be released.
7928     </para>
7929     <example id="example.odr.encoding.of.integer">
7930      <title>Encoding and decoding of an integer</title>
7931      <para>
7932       The use of the double indirection can be a little confusing at first
7933       (its purpose will become clear later on, hopefully),
7934       so an example is in order. We'll encode an integer value, and
7935       immediately decode it again using a different stream. A useless, but
7936       informative operation.
7937      </para>
7938      <programlisting><![CDATA[
7939 void do_nothing_useful(Odr_int value)
7940 {
7941     ODR encode, decode;
7942     Odr_int *valp, *resvalp;
7943     char *bufferp;
7944     int len;
7945
7946     /* allocate streams */
7947     if (!(encode = odr_createmem(ODR_ENCODE)))
7948         return;
7949     if (!(decode = odr_createmem(ODR_DECODE)))
7950         return;
7951
7952     valp = &value;
7953     if (odr_integer(encode, &valp, 0, 0) == 0)
7954     {
7955         printf("encoding went bad\n");
7956         return;
7957     }
7958     bufferp = odr_getbuf(encode, &len, 0);
7959     printf("length of encoded data is %d\n", len);
7960
7961     /* now let's decode the thing again */
7962     odr_setbuf(decode, bufferp, len, 0);
7963     if (odr_integer(decode, &resvalp, 0, 0) == 0)
7964     {
7965         printf("decoding went bad\n");
7966         return;
7967     }
7968     /* ODR_INT_PRINTF format for printf (such as %d) */
7969     printf("the value is " ODR_INT_PRINTF "\n", *resvalp);
7970
7971     /* clean up */
7972     odr_destroy(encode);
7973     odr_destroy(decode);
7974 }
7975 ]]>
7976      </programlisting>
7977      <para>
7978       This looks like a lot of work, offhand. In practice, the &odr; streams
7979       will typically be allocated once, in the beginning of your program
7980       (or at the beginning of a new network session), and the encoding
7981       and decoding will only take place in a few, isolated places in your
7982       program, so the overhead is quite manageable.
7983      </para>
7984     </example>
7985    </sect2>
7986    <sect2 id="odr.printing">
7987     <title>Printing</title>
7988     <para>
7989      When an ODR stream is created of type <literal>ODR_PRINT</literal>
7990      the ODR module will print the contents of a PDU in a readable format.
7991      By default output is written to the <literal>stderr</literal> stream.
7992      This behavior can be changed, however, by calling the function
7993      <synopsis>
7994       odr_setprint(ODR o, FILE *file);
7995      </synopsis>
7996      before encoders or decoders are being invoked.
7997      It is also possible to direct the output to a buffer (of indeed
7998      another file), by using the more generic mechanism:
7999      <synopsis>
8000       void odr_set_stream(ODR o, void *handle,
8001                          void (*stream_write)(ODR o, void *handle, int type,
8002                                               const char *buf, int len),
8003                          void (*stream_close)(void *handle));
8004      </synopsis>
8005      Here the user provides an opaque handle and two handlers,
8006      <replaceable>stream_write</replaceable> for writing,
8007      and <replaceable>stream_close</replaceable> which is supposed
8008      to close/free resources associated with handle.
8009      The <replaceable>stream_close</replaceable> handler is optional and
8010      if NULL for the function is provided, it will not be invoked.
8011      The <replaceable>stream_write</replaceable> takes the ODR handle
8012      as parameter, the user defined handle, a type
8013      <literal>ODR_OCTETSTRING</literal>, <literal>ODR_VISIBLESTRING</literal>
8014      which indicates the type of contents is being written.
8015     </para>
8016     <para>
8017      Another utility useful for diagnostics (error handling) or as
8018      part of the printing facilities is:
8019      <synopsis>
8020       const char **odr_get_element_path(ODR o);
8021      </synopsis>
8022      which returns a list of current elements that ODR deals with at the
8023      moment. For the returned array, say <literal>ar</literal>,
8024      <literal>ar[0]</literal> is the top level element,
8025      <literal>ar[n]</literal> is the last. The last element has the
8026      property that <literal>ar[n+1] == NULL</literal>.
8027     </para>
8028     <example id="example.odr.element.path.record">
8029      <title>Element Path for record</title>
8030      <para>
8031       For a database record part of a PresentResponse the
8032       array returned by <function>odr_get_element</function>
8033       is <literal>presentResponse</literal>, <literal>databaseOrSurDiagnostics</literal>, <literal>?</literal>, <literal>record</literal>, <literal>?</literal>, <literal>databaseRecord</literal> . The question mark appears due to
8034       unnamed constructions.
8035      </para>
8036     </example>
8037    </sect2>
8038    <sect2 id="odr.diagnostics">
8039     <title>Diagnostics</title>
8040     <para>
8041      The encoding/decoding functions all return 0 when an error occurs.
8042      Until you call <function>odr_reset()</function>, you cannot use the
8043      stream again, and any function called will immediately return 0.
8044     </para>
8045     <para>
8046      To provide information to the programmer or administrator, the function
8047     </para>
8048     <synopsis>
8049      void odr_perror(ODR o, char *message);
8050     </synopsis>
8051     <para>
8052      is provided, which prints the <literal>message</literal> argument to
8053      <literal>stderr</literal> along with an error message from the stream.
8054     </para>
8055     <para>
8056      You can also use the function
8057     </para>
8058     <synopsis>
8059      int odr_geterror(ODR o);
8060     </synopsis>
8061     <para>
8062      to get the current error number from the screen. The number will be
8063      one of these constants:
8064     </para>
8065     <table frame="top" id="odr.error.codes">
8066      <title>ODR Error codes</title>
8067      <tgroup cols="2">
8068       <thead>
8069        <row>
8070         <entry>code</entry>
8071         <entry>Description</entry>
8072        </row>
8073       </thead>
8074       <tbody>
8075        <row>
8076         <entry>OMEMORY</entry><entry>Memory allocation failed.</entry>
8077        </row>
8078        <row>
8079         <entry>OSYSERR</entry><entry>A system- or library call has failed.
8080          The standard diagnostic variable <literal>errno</literal> should be
8081          examined to determine the actual error.</entry>
8082        </row>
8083        <row>
8084         <entry>OSPACE</entry><entry>No more space for encoding.
8085          This will only occur when the user has explicitly provided a
8086          buffer for an encoding stream without allowing the system to
8087          allocate more space.</entry>
8088        </row>
8089        <row>
8090         <entry>OREQUIRED</entry><entry>This is a common protocol error; A
8091          required data element was missing during encoding or decoding.</entry>
8092        </row>
8093        <row>
8094         <entry>OUNEXPECTED</entry><entry>An unexpected data element was
8095         found during decoding.</entry>
8096        </row>
8097        <row>
8098         <entry>OOTHER</entry><entry>Other error. This is typically an
8099         indication of misuse of the &odr; system by the programmer, and also
8100         that the diagnostic system isn't as good as it should be, yet.</entry>
8101        </row>
8102       </tbody>
8103      </tgroup>
8104     </table>
8105     <para>
8106      The character string array
8107     </para>
8108     <synopsis>
8109      char *odr_errlist[]
8110     </synopsis>
8111     <para>
8112      can be indexed by the error code to obtain a human-readable
8113      representation of the problem.
8114     </para>
8115    </sect2>
8116    <sect2 id="odr.summary.and.synopsis">
8117     <title>Summary and Synopsis</title>
8118     <synopsis>
8119      #include &lt;yaz/odr.h>
8120
8121      ODR odr_createmem(int direction);
8122
8123      void odr_destroy(ODR o);
8124
8125      void odr_reset(ODR o);
8126
8127      char *odr_getbuf(ODR o, int *len, int *size);
8128
8129      void odr_setbuf(ODR o, char *buf, int len, int can_grow);
8130
8131      void *odr_malloc(ODR o, int size);
8132
8133      NMEM odr_extract_mem(ODR o);
8134
8135      int odr_geterror(ODR o);
8136
8137      void odr_perror(ODR o, const char *message);
8138
8139      extern char *odr_errlist[];
8140     </synopsis>
8141    </sect2>
8142   </sect1>
8143   <sect1 id="odr.programming">
8144    <title>Programming with ODR</title>
8145    <para>
8146     The API of &odr; is designed to reflect the structure of ASN.1, rather
8147     than BER itself. Future releases may be able to represent data in
8148     other external forms.
8149    </para>
8150    <tip>
8151     <para>
8152      There is an ASN.1 tutorial available at
8153      <ulink url="&url.asn.1.tutorial;">this site</ulink>.
8154      This site also has standards for ASN.1 (X.680) and BER (X.690)
8155      <ulink url="&url.asn.1.standards;">online</ulink>.
8156     </para>
8157    </tip>
8158    <para>
8159     The ODR interface is based loosely on that of the Sun Microsystems
8160     XDR routines.
8161     Specifically, each function which corresponds to an ASN.1 primitive
8162     type has a dual function. Depending on the settings of the ODR
8163     stream which is supplied as a parameter, the function may be used
8164     either to encode or decode data. The functions that can be built
8165     using these primitive functions, to represent more complex data types,
8166     share this quality. The result is that you only have to enter the
8167     definition for a type once - and you have the functionality of encoding,
8168     decoding (and pretty-printing) all in one unit.
8169     The resulting C source code is quite compact, and is a pretty
8170     straightforward representation of the source ASN.1 specification.
8171    </para>
8172    <para>
8173     In many cases, the model of the XDR functions works quite well in this
8174     role.
8175     In others, it is less elegant. Most of the hassle comes from the optional
8176     SEQUENCE members which don't exist in XDR.
8177    </para>
8178    <sect2 id="odr.primitive.asn1.types">
8179     <title>The Primitive ASN.1 Types</title>
8180     <para>
8181      ASN.1 defines a number of primitive types (many of which correspond
8182      roughly to primitive types in structured programming languages, such as C).
8183     </para>
8184     <sect3 id="odr.integer">
8185      <title>INTEGER</title>
8186      <para>
8187       The &odr; function for encoding or decoding (or printing) the ASN.1
8188       INTEGER type looks like this:
8189      </para>
8190      <synopsis>
8191       int odr_integer(ODR o, Odr_int **p, int optional, const char *name);
8192      </synopsis>
8193      <para>
8194       The <literal>Odr_int</literal> is just a simple integer.
8195      </para>
8196      <para>
8197       This form is typical of the primitive &odr; functions. They are named
8198       after the type of data that they encode or decode. They take an &odr;
8199       stream, an indirect reference to the type in question, and an
8200       <literal>optional</literal> flag (corresponding to the OPTIONAL keyword
8201       of ASN.1) as parameters. They all return an integer value of either one
8202       or zero.
8203       When you use the primitive functions to construct encoders for complex
8204       types of your own, you should follow this model as well. This
8205       ensures that your new types can be reused as elements in yet more
8206       complex types.
8207      </para>
8208      <para>
8209       The <literal>o</literal> parameter should obviously refer to a properly
8210       initialized &odr; stream of the right type (encoding/decoding/printing)
8211       for the operation that you wish to perform.
8212      </para>
8213      <para>
8214       When encoding or printing, the function first looks at
8215       <literal>* p</literal>. If <literal>* p</literal> (the pointer pointed
8216       to by <literal>p</literal>) is a null pointer, this is taken to mean that
8217       the data element is absent. If the <literal>optional</literal> parameter
8218       is nonzero, the function will return one (signifying success) without
8219       any further processing. If the <literal>optional</literal> is zero, an
8220       internal error flag is set in the &odr; stream, and the function will
8221       return 0. No further operations can be carried out on the stream without
8222       a call to the function <function>odr_reset()</function>.
8223      </para>
8224      <para>
8225       If <literal>*p</literal> is not a null pointer, it is expected to
8226       point to an instance of the data type. The data will be subjected to
8227       the encoding rules, and the result will be placed in the buffer held
8228       by the &odr; stream.
8229      </para>
8230      <para>
8231       The other ASN.1 primitives have similar functions that operate in
8232       similar manners:
8233      </para>
8234     </sect3>
8235     <sect3 id="odr.boolean">
8236      <title>BOOLEAN</title>
8237      <synopsis>
8238 int odr_bool(ODR o, Odr_bool **p, int optional, const char *name);
8239      </synopsis>
8240     </sect3>
8241     <sect3 id="odr.real">
8242      <title>REAL</title>
8243      <para>
8244       Not defined.
8245      </para>
8246     </sect3>
8247     <sect3 id="odr.null">
8248      <title>NULL</title>
8249      <synopsis>
8250 int odr_null(ODR o, Odr_null **p, int optional, const char *name);
8251      </synopsis>
8252      <para>
8253       In this case, the value of **p is not important. If <literal>*p</literal>
8254       is different from the null pointer, the null value is present, otherwise
8255       it's absent.
8256      </para>
8257     </sect3>
8258     <sect3 id="odr.octet.string">
8259      <title>OCTET STRING</title>
8260      <synopsis>
8261 typedef struct odr_oct
8262 {
8263     unsigned char *buf;
8264     int len;
8265 } Odr_oct;
8266
8267 int odr_octetstring(ODR o, Odr_oct **p, int optional,
8268                     const char *name);
8269      </synopsis>
8270      <para>
8271       The <literal>buf</literal> field should point to the character array
8272       that holds the octetstring. The <literal>len</literal> field holds the
8273       actual length.
8274       The character array need not be null terminated.
8275      </para>
8276      <para>
8277       To make things a little easier, an alternative is given for string
8278       types that are not expected to contain embedded NULL characters (eg.
8279       VisibleString):
8280      </para>
8281      <synopsis>
8282       int odr_cstring(ODR o, char **p, int optional, const char *name);
8283      </synopsis>
8284      <para>
8285       Which encoded or decodes between OCTETSTRING representations and
8286       null-terminates C strings.
8287      </para>
8288      <para>
8289       Functions are provided for the derived string types, eg:
8290      </para>
8291      <synopsis>
8292 int odr_visiblestring(ODR o, char **p, int optional,
8293                       const char *name);
8294      </synopsis>
8295     </sect3>
8296     <sect3 id="odr.bit.string">
8297      <title>BIT STRING</title>
8298      <synopsis>
8299 int odr_bitstring(ODR o, Odr_bitmask **p, int optional,
8300                   const char *name);
8301      </synopsis>
8302      <para>
8303       The opaque type <literal>Odr_bitmask</literal> is only suitable for
8304       holding relatively brief bit strings, eg. for options fields, etc.
8305       The constant <literal>ODR_BITMASK_SIZE</literal> multiplied by 8
8306       gives the maximum possible number of bits.
8307      </para>
8308      <para>
8309       A set of macros are provided for manipulating the
8310       <literal>Odr_bitmask</literal> type:
8311      </para>
8312      <synopsis>
8313 void ODR_MASK_ZERO(Odr_bitmask *b);
8314
8315 void ODR_MASK_SET(Odr_bitmask *b, int bitno);
8316
8317 void ODR_MASK_CLEAR(Odr_bitmask *b, int bitno);
8318
8319 int ODR_MASK_GET(Odr_bitmask *b, int bitno);
8320      </synopsis>
8321      <para>
8322       The functions are modeled after the manipulation functions that
8323       accompany the <literal>fd_set</literal> type used by the
8324       <function>select(2)</function> call.
8325       <literal>ODR_MASK_ZERO</literal> should always be called first on a
8326       new bitmask, to initialize the bits to zero.
8327      </para>
8328     </sect3>
8329     <sect3 id="odr.object.identifier">
8330      <title>OBJECT IDENTIFIER</title>
8331      <synopsis>
8332 int odr_oid(ODR o, Odr_oid **p, int optional, const char *name);
8333      </synopsis>
8334      <para>
8335       The C OID representation is simply an array of integers, terminated by
8336       the value -1 (the <literal>Odr_oid</literal> type is synonymous with
8337       the <literal>short</literal> type).
8338       We suggest that you use the OID database module (see
8339       <xref linkend="tools.oid.database"/>) to handle object identifiers
8340       in your application.
8341      </para>
8342     </sect3>
8343    </sect2>
8344    <sect2 id="odr.tagging.primitive.types">
8345     <title>Tagging Primitive Types</title>
8346     <para>
8347      The simplest way of tagging a type is to use the
8348      <function>odr_implicit_tag()</function> or
8349      <function>odr_explicit_tag()</function> macros:
8350     </para>
8351     <synopsis>
8352 int odr_implicit_tag(ODR o, Odr_fun fun, int class, int tag,
8353                      int optional, const char *name);
8354
8355 int odr_explicit_tag(ODR o, Odr_fun fun, int class, int tag,
8356                      int optional, const char *name);
8357     </synopsis>
8358     <para>
8359      To create a type derived from the integer type by implicit tagging, you
8360      might write:
8361     </para>
8362     <screen>
8363      MyInt ::= [210] IMPLICIT INTEGER
8364     </screen>
8365     <para>
8366      In the &odr; system, this would be written like:
8367     </para>
8368     <screen>
8369 int myInt(ODR o, Odr_int **p, int optional, const char *name)
8370 {
8371     return odr_implicit_tag(o, odr_integer, p,
8372                             ODR_CONTEXT, 210, optional, name);
8373 }
8374     </screen>
8375     <para>
8376      The function <function>myInt()</function> can then be used like any of
8377      the primitive functions provided by &odr;. Note that the behavior of
8378      <function>odr_explicit_tag()</function>
8379      and <function>odr_implicit_tag()</function> macros
8380      act exactly the same as the functions they are applied to - they
8381      respond to error conditions, etc, in the same manner - they
8382      simply have three extra parameters. The class parameter may
8383      take one of the values: <literal>ODR_CONTEXT</literal>,
8384      <literal>ODR_PRIVATE</literal>, <literal>ODR_UNIVERSAL</literal>, or
8385      <literal>/ODR_APPLICATION</literal>.
8386     </para>
8387    </sect2>
8388    <sect2 id="odr.constructed.types">
8389     <title>Constructed Types</title>
8390     <para>
8391      Constructed types are created by combining primitive types. The
8392       &odr; system only implements the SEQUENCE and SEQUENCE OF constructions
8393      (although adding the rest of the container types should be simple
8394      enough, if the need arises).
8395     </para>
8396     <para>
8397      For implementing SEQUENCEs, the functions
8398     </para>
8399     <synopsis>
8400 int odr_sequence_begin(ODR o, void *p, int size, const char *name);
8401 int odr_sequence_end(ODR o);
8402     </synopsis>
8403     <para>
8404      are provided.
8405     </para>
8406     <para>
8407      The <function>odr_sequence_begin()</function> function should be
8408      called in the beginning of a function that implements a SEQUENCE type.
8409      Its parameters are the &odr; stream, a pointer (to a pointer to the type
8410      you're implementing), and the <literal>size</literal> of the type
8411      (typically a C structure). On encoding, it returns 1 if
8412      <literal>* p</literal> is a null pointer. The <literal>size</literal>
8413      parameter is ignored. On decoding, it returns 1 if the type is found in
8414      the data stream. <literal>size</literal> bytes of memory are allocated,
8415      and <literal>*p</literal> is set to point to this space.
8416      <function>odr_sequence_end()</function> is called at the end of the
8417      complex function. Assume that a type is defined like this:
8418     </para>
8419     <screen>
8420 MySequence ::= SEQUENCE {
8421      intval INTEGER,
8422      boolval BOOLEAN OPTIONAL
8423 }
8424     </screen>
8425     <para>
8426      The corresponding &odr; encoder/decoder function and the associated data
8427      structures could be written like this:
8428     </para>
8429     <screen>
8430 typedef struct MySequence
8431 {
8432     Odr_int *intval;
8433     Odr_bool *boolval;
8434 } MySequence;
8435
8436 int mySequence(ODR o, MySequence **p, int optional, const char *name)
8437 {
8438     if (odr_sequence_begin(o, p, sizeof(**p), name) == 0)
8439         return optional &amp;&amp; odr_ok(o);
8440     return
8441         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
8442         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
8443         odr_sequence_end(o);
8444 }
8445     </screen>
8446     <para>
8447      Note the 1 in the call to <function>odr_bool()</function>, to mark
8448      that the sequence member is optional.
8449      If either of the member types had been tagged, the macros
8450      <function>odr_implicit_tag()</function> or
8451      <function>odr_explicit_tag()</function>
8452      could have been used.
8453      The new function can be used exactly like the standard functions provided
8454      with &odr;. It will encode, decode or pretty-print a data value of the
8455      <literal>MySequence</literal> type. We like to name types with an
8456      initial capital, as done in ASN.1 definitions, and to name the
8457      corresponding function with the first character of the name in lower case.
8458      You could, of course, name your structures, types, and functions any way
8459      you please - as long as you're consistent, and your code is easily readable.
8460      <literal>odr_ok</literal> is just that - a predicate that returns the
8461      state of the stream. It is used to ensure that the behavior of the new
8462      type is compatible with the interface of the primitive types.
8463     </para>
8464    </sect2>
8465    <sect2 id="odr.tagging.constructed.types">
8466     <title>Tagging Constructed Types</title>
8467     <note>
8468      <para>
8469       See <xref linkend="odr.tagging.primitive.types"/> for information
8470       on how to tag the primitive types, as well as types that are
8471       already defined.
8472      </para>
8473     </note>
8474     <sect3 id="odr.implicit.tagging">
8475      <title>Implicit Tagging</title>
8476      <para>
8477       Assume the type above had been defined as
8478      </para>
8479      <screen>
8480 MySequence ::= [10] IMPLICIT SEQUENCE {
8481       intval INTEGER,
8482       boolval BOOLEAN OPTIONAL
8483 }
8484      </screen>
8485      <para>
8486       You would implement this in &odr; by calling the function
8487      </para>
8488      <synopsis>
8489 int odr_implicit_settag(ODR o, int class, int tag);
8490      </synopsis>
8491      <para>
8492       which overrides the tag of the type immediately following it. The
8493       macro <function>odr_implicit_tag()</function> works by calling
8494       <function>odr_implicit_settag()</function> immediately
8495       before calling the function pointer argument.
8496       Your type function could look like this:
8497      </para>
8498      <screen>
8499 int mySequence(ODR o, MySequence **p, int optional, const char *name)
8500 {
8501     if (odr_implicit_settag(o, ODR_CONTEXT, 10) == 0 ||
8502         odr_sequence_begin(o, p, sizeof(**p), name) == 0)
8503         return optional &amp;&amp; odr_ok(o);
8504     return
8505         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
8506         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
8507         odr_sequence_end(o);
8508 }
8509      </screen>
8510      <para>
8511       The definition of the structure <literal>MySequence</literal> would be
8512       the same.
8513      </para>
8514     </sect3>
8515     <sect3 id="odr.explicit.tagging">
8516      <title>Explicit Tagging</title>
8517      <para>
8518       Explicit tagging of constructed types is a little more complicated,
8519       since you are in effect adding a level of construction to the data.
8520      </para>
8521      <para>
8522       Assume the definition:
8523      </para>
8524      <screen>
8525 MySequence ::= [10] IMPLICIT SEQUENCE {
8526    intval INTEGER,
8527    boolval BOOLEAN OPTIONAL
8528 }
8529      </screen>
8530      <para>
8531       Since the new type has an extra level of construction, two new functions
8532       are needed to encapsulate the base type:
8533      </para>
8534      <synopsis>
8535 int odr_constructed_begin(ODR o, void *p, int class, int tag,
8536                           const char *name);
8537
8538 int odr_constructed_end(ODR o);
8539      </synopsis>
8540      <para>
8541       Assume that the IMPLICIT in the type definition above were replaced
8542       with EXPLICIT (or that the IMPLICIT keyword were simply deleted, which
8543       would be equivalent). The structure definition would look the same,
8544       but the function would look like this:
8545      </para>
8546      <screen>
8547 int mySequence(ODR o, MySequence **p, int optional, const char *name)
8548 {
8549     if (odr_constructed_begin(o, p, ODR_CONTEXT, 10, name) == 0)
8550         return optional &amp;&amp; odr_ok(o);
8551     if (o->direction == ODR_DECODE)
8552         *p = odr_malloc(o, sizeof(**p));
8553     if (odr_sequence_begin(o, p, sizeof(**p), 0) == 0)
8554     {
8555         *p = 0; /* this is almost certainly a protocol error */
8556         return 0;
8557     }
8558     return
8559         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
8560         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
8561         odr_sequence_end(o) &amp;&amp;
8562         odr_constructed_end(o);
8563 }
8564      </screen>
8565      <para>
8566       Notice that the interface here gets kind of nasty. The reason is
8567       simple: Explicitly tagged, constructed types are fairly rare in
8568       the protocols that we care about, so the
8569       esthetic annoyance (not to mention the dangers of a cluttered
8570       interface) is less than the time that would be required to develop a
8571       better interface. Nevertheless, it is far from satisfying, and it's a
8572       point that will be worked on in the future. One option for you would
8573       be to simply apply the <function>odr_explicit_tag()</function> macro to
8574       the first function, and not
8575       have to worry about <function>odr_constructed_*</function> yourself.
8576       Incidentally, as you might have guessed, the
8577       <function>odr_sequence_</function> functions are themselves
8578       implemented using the <function>/odr_constructed_</function> functions.
8579      </para>
8580     </sect3>
8581    </sect2>
8582    <sect2 id="odr.sequence.of">
8583     <title>SEQUENCE OF</title>
8584     <para>
8585      To handle sequences (arrays) of a specific type, the function
8586     </para>
8587     <synopsis>
8588 int odr_sequence_of(ODR o, int (*fun)(ODR o, void *p, int optional),
8589                     void *p, int *num, const char *name);
8590     </synopsis>
8591     <para>
8592      The <literal>fun</literal> parameter is a pointer to the decoder/encoder
8593      function of the type. <literal>p</literal> is a pointer to an array of
8594      pointers to your type. <literal>num</literal> is the number of elements
8595      in the array.
8596     </para>
8597     <para>
8598      Assume a type
8599     </para>
8600     <screen>
8601 MyArray ::= SEQUENCE OF INTEGER
8602     </screen>
8603     <para>
8604      The C representation might be
8605     </para>
8606     <screen>
8607 typedef struct MyArray
8608 {
8609     int num_elements;
8610     Odr_int **elements;
8611 } MyArray;
8612     </screen>
8613     <para>
8614      And the function might look like
8615     </para>
8616     <screen>
8617 int myArray(ODR o, MyArray **p, int optional, const char *name)
8618 {
8619     if (o->direction == ODR_DECODE)
8620         *p = odr_malloc(o, sizeof(**p));
8621     if (odr_sequence_of(o, odr_integer, &amp;(*p)->elements,
8622         &amp;(*p)->num_elements, name))
8623         return 1;
8624     *p = 0;
8625         return optional &amp;&amp; odr_ok(o);
8626 }
8627     </screen>
8628    </sect2>
8629    <sect2 id="odr.choice.types">
8630     <title>CHOICE Types</title>
8631     <para>
8632      The choice type is used fairly often in some ASN.1 definitions, so
8633      some work has gone into streamlining its interface.
8634     </para>
8635     <para>
8636      CHOICE types are handled by the function:
8637     </para>
8638     <synopsis>
8639 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
8640                const char *name);
8641     </synopsis>
8642     <para>
8643      The <literal>arm</literal> array is used to describe each of the possible
8644      types that the CHOICE type may assume. Internally in your application,
8645      the CHOICE type is represented as a discriminated union. That is, a
8646      C union accompanied by an integer (or enum) identifying the active
8647      'arm' of the union.
8648      <literal>whichp</literal> is a pointer to the union discriminator.
8649      When encoding, it is examined to determine the current type.
8650      When decoding, it is set to reference the type that was found in
8651      the input stream.
8652     </para>
8653     <para>
8654      The Odr_arm type is defined thus:
8655     </para>
8656     <screen>
8657 typedef struct odr_arm
8658 {
8659     int tagmode;
8660     int class;
8661     int tag;
8662     int which;
8663     Odr_fun fun;
8664     char *name;
8665 } Odr_arm;
8666     </screen>
8667     <para>
8668      The interpretation of the fields are:
8669     </para>
8670     <variablelist>
8671      <varlistentry>
8672       <term>tagmode</term>
8673       <listitem><para>Either <literal>ODR_IMPLICIT</literal>,
8674       <literal>ODR_EXPLICIT</literal>, or <literal>ODR_NONE</literal> (-1)
8675       to mark   no tagging.</para></listitem>
8676      </varlistentry>
8677      <varlistentry>
8678       <term>which</term>
8679       <listitem><para>The value of the discriminator that corresponds to
8680       this CHOICE element. Typically, it will be a #defined constant, or
8681       an enum member.</para></listitem>
8682      </varlistentry>
8683      <varlistentry>
8684       <term>fun</term>
8685       <listitem><para>A pointer to a function that implements the type of
8686       the CHOICE member. It may be either a standard &odr; type or a type
8687       defined by yourself.</para></listitem>
8688      </varlistentry>
8689      <varlistentry>
8690       <term>name</term>
8691       <listitem><para>Name of tag.</para></listitem>
8692      </varlistentry>
8693     </variablelist>
8694     <para>
8695      A handy way to prepare the array for use by the
8696      <function>odr_choice()</function> function is to
8697      define it as a static, initialized array in the beginning of your
8698      decoding/encoding function. Assume the type definition:
8699     </para>
8700     <screen>
8701 MyChoice ::= CHOICE {
8702     untagged INTEGER,
8703     tagged   [99] IMPLICIT INTEGER,
8704     other    BOOLEAN
8705 }
8706     </screen>
8707     <para>
8708      Your C type might look like
8709     </para>
8710     <screen>
8711 typedef struct MyChoice
8712 {
8713     enum
8714     {
8715         MyChoice_untagged,
8716         MyChoice_tagged,
8717         MyChoice_other
8718     } which;
8719     union
8720     {
8721         Odr_int *untagged;
8722         Odr_int *tagged;
8723         Odr_bool *other;
8724     } u;
8725 };
8726     </screen>
8727     <para>
8728      And your function could look like this:
8729     </para>
8730     <screen>
8731 int myChoice(ODR o, MyChoice **p, int optional, const char *name)
8732 {
8733     static Odr_arm arm[] =
8734     {
8735       {-1, -1, -1, MyChoice_untagged, odr_integer, "untagged"},
8736       {ODR_IMPLICIT, ODR_CONTEXT, 99, MyChoice_tagged, odr_integer,
8737       "tagged"},
8738       {-1, -1, -1, MyChoice_other, odr_boolean, "other"},
8739       {-1, -1, -1, -1, 0}
8740     };
8741
8742     if (o->direction == ODR_DECODE)
8743         *p = odr_malloc(o, sizeof(**p);
8744     else if (!*p)
8745         return optional &amp;&amp; odr_ok(o);
8746
8747     if (odr_choice(o, arm, &amp;(*p)->u, &amp;(*p)->which), name)
8748         return 1;
8749     *p = 0;
8750         return optional &amp;&amp; odr_ok(o);
8751 }
8752     </screen>
8753     <para>
8754      In some cases (say, a non-optional choice which is a member of a
8755      sequence), you can "embed" the union and its discriminator in the
8756      structure belonging to the enclosing type, and you won't need to
8757      fiddle with memory allocation to create a separate structure to
8758      wrap the discriminator and union.
8759     </para>
8760     <para>
8761      The corresponding function is somewhat nicer in the Sun XDR interface.
8762      Most of the complexity of this interface comes from the possibility of
8763      declaring sequence elements (including CHOICEs) optional.
8764     </para>
8765     <para>
8766      The ASN.1 specifications naturally requires that each member of a
8767      CHOICE have a distinct tag, so they can be told apart on decoding.
8768      Sometimes it can be useful to define a CHOICE that has multiple types
8769      that share the same tag. You'll need some other mechanism, perhaps
8770      keyed to the context of the CHOICE type. In effect, we would like to
8771      introduce a level of context-sensitiveness to our ASN.1 specification.
8772      When encoding an internal representation, we have no problem, as long
8773      as each CHOICE member has a distinct discriminator value. For
8774      decoding, we need a way to tell the choice function to look for a
8775      specific arm of the table. The function
8776     </para>
8777     <synopsis>
8778 void odr_choice_bias(ODR o, int what);
8779     </synopsis>
8780     <para>
8781      provides this functionality. When called, it leaves a notice for the next
8782      call to <function>odr_choice()</function> to be called on the decoding
8783      stream <literal>o</literal> that only the <literal>arm</literal> entry with
8784      a <literal>which</literal> field equal to <literal>what</literal>
8785      should be tried.
8786     </para>
8787     <para>
8788      The most important application (perhaps the only one, really) is in
8789      the definition of application-specific EXTERNAL encoders/decoders
8790      which will automatically decode an ANY member given the direct or
8791      indirect reference.
8792     </para>
8793    </sect2>
8794   </sect1>
8795   <sect1 id="odr.debugging">
8796    <title>Debugging</title>
8797    <para>
8798     The protocol modules are suffering somewhat from a lack of diagnostic
8799     tools at the moment. Specifically ways to pretty-print PDUs that
8800     aren't recognized by the system. We'll include something to this end
8801     in a not-too-distant release. In the meantime, what we do when we get
8802     packages we don't understand is to compile the ODR module with
8803     <literal>ODR_DEBUG</literal> defined. This causes the module to dump tracing
8804     information as it processes data units. With this output and the
8805     protocol specification (Z39.50), it is generally fairly easy to see
8806     what goes wrong.
8807    </para>
8808   </sect1>
8809  </chapter>
8810  <chapter id="comstack">
8811   <title>The COMSTACK Module</title>
8812   <sect1 id="comstack.synopsis">
8813    <title>Synopsis (blocking mode)</title>
8814    <programlisting><![CDATA[
8815     COMSTACK stack;
8816     char *buf = 0;
8817     int size = 0, length_incoming;
8818     char server_address_str[] = "localhost:9999";
8819     void *server_address_ip;
8820     int status;
8821
8822     char *protocol_package = "GET / HTTP/1.0\r\n\r\n";
8823     int protocol_package_length = strlen(protocol_package);
8824
8825     stack = cs_create(tcpip_type, 1, PROTO_HTTP);
8826     if (!stack) {
8827         perror("cs_create");  /* use perror() here since we have no stack yet */
8828         return -1;
8829     }
8830
8831     server_address_ip = cs_straddr(stack, server_address_str);
8832     if (!server_address_ip) {
8833         fprintf(stderr, "cs_straddr: address could not be resolved\n");
8834         return -1;
8835     }
8836
8837     status = cs_connect(stack, server_address_ip);
8838     if (status) {
8839         fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack));
8840         return -1;
8841     }
8842
8843     status = cs_rcvconnect(stack);
8844     if (status) {
8845         fprintf(stderr, "cs_rcvconnect: %s\n", cs_strerror(stack));
8846         return -1;
8847     }
8848
8849     status = cs_put(stack, protocol_package, protocol_package_length);
8850     if (status) {
8851         fprintf(stderr, "cs_put: %s\n", cs_strerror(stack));
8852         return -1;
8853     }
8854
8855     /* Now get a response */
8856     length_incoming = cs_get(stack, &buf, &size);
8857     if (!length_incoming) {
8858         fprintf(stderr, "Connection closed\n");
8859         return -1;
8860     } else if (length_incoming < 0) {
8861         fprintf(stderr, "cs_get: %s\n", cs_strerror(stack));
8862         return -1;
8863     }
8864
8865     /* Print result */
8866     fwrite(buf, length_incoming, 1, stdout);
8867
8868     /* clean up */
8869     cs_close(stack);
8870     if (buf)
8871         xfree(buf);
8872     return 0;
8873 ]]>
8874    </programlisting>
8875
8876   </sect1>
8877   <sect1 id="comstack.introduction">
8878    <title>Introduction</title>
8879    <para>
8880     The &comstack;
8881     subsystem provides a transparent interface to different types of transport
8882     stacks for the exchange of BER-encoded data and HTTP packets.
8883     At present, the RFC1729 method (BER over TCP/IP), local UNIX socket and an
8884     experimental SSL stack are supported, but others may be added in time.
8885     The philosophy of the
8886     module is to provide a simple interface by hiding unused options and
8887     facilities of the underlying libraries. This is always done at the risk
8888     of losing generality, and it may prove that the interface will need
8889     extension later on.
8890    </para>
8891    <note>
8892     <para>
8893      There hasn't been interest in the XTImOSI stack for some years.
8894      Therefore, it is no longer supported.
8895      </para>
8896    </note>
8897    <para>
8898     The interface is implemented in such a fashion that only the
8899     sub-layers constructed to the transport methods that you wish to
8900     use in your application are linked in.
8901    </para>
8902    <para>
8903     You will note that even though simplicity was a goal in the design,
8904     the interface is still orders of magnitudes more complex than the
8905     transport systems found in many other packages. One reason is that
8906     the interface needs to support the somewhat different requirements of
8907     the different lower-layer communications stacks; another important
8908     reason is that the interface seeks to provide a more or less
8909     industrial-strength approach to asynchronous event-handling.
8910     When no function is allowed to block, things get more complex -
8911     particularly on the server side.
8912     We urge you to have a look at the demonstration client and server
8913     provided with the package. They are meant to be easily readable and
8914     instructive, while still being at least moderately useful.
8915    </para>
8916   </sect1>
8917   <sect1 id="comstack.common">
8918    <title>Common Functions</title>
8919    <sect2 id="comstack.managing.endpoints">
8920     <title>Managing Endpoints</title>
8921     <synopsis>
8922      COMSTACK cs_create(CS_TYPE type, int blocking, int protocol);
8923     </synopsis>
8924     <para>
8925      Creates an instance of the protocol stack - a communications endpoint.
8926      The <literal>type</literal> parameter determines the mode
8927      of communication. At present the following values are supported:
8928     </para>
8929     <variablelist>
8930      <varlistentry>
8931       <term><literal>tcpip_type</literal></term>
8932       <listitem><para>TCP/IP (BER over TCP/IP or HTTP over TCP/IP)
8933       </para></listitem>
8934      </varlistentry>
8935      <varlistentry>
8936       <term><literal>ssl_type</literal></term>
8937       <listitem><para>Secure Socket Layer (SSL). This COMSTACK
8938       is experimental and is not fully implemented. If
8939       HTTP is used, this effectively is HTTPS.
8940       </para></listitem>
8941      </varlistentry>
8942      <varlistentry>
8943       <term><literal>unix_type</literal></term>
8944       <listitem><para>Unix socket (unix only). Local Transfer via
8945       file socket. See <citerefentry><refentrytitle>unix</refentrytitle>
8946       <manvolnum>7</manvolnum></citerefentry>.
8947       </para></listitem>
8948      </varlistentry>
8949     </variablelist>
8950     <para>
8951      The <function>cs_create</function> function returns a null-pointer
8952      if a system error occurs.
8953      The <literal>blocking</literal> parameter should be one if
8954      you wish the association to operate in blocking mode, zero otherwise.
8955      The <literal>protocol</literal> field should be
8956      <literal>PROTO_Z3950</literal> or <literal>PROTO_HTTP</literal>.
8957      Protocol <literal>PROTO_SR</literal> is no longer supported.
8958     </para>
8959     <synopsis>
8960      void cs_close(COMSTACK handle);
8961     </synopsis>
8962     <para>
8963      Closes the connection (as elegantly as the lower layers will permit),
8964      and releases the resources pointed to by the
8965      <literal>handle</literal>
8966      parameter. The
8967      <literal>handle</literal>
8968      should not be referenced again after this call.
8969     </para>
8970     <note>
8971      <para>
8972       We really need a soft disconnect, don't we?
8973      </para>
8974     </note>
8975    </sect2>
8976    <sect2 id="comstack.data.exchange">
8977     <title>Data Exchange</title>
8978     <synopsis>
8979      int cs_put(COMSTACK handle, char *buf, int len);
8980     </synopsis>
8981     <para>
8982      Sends <literal>buf</literal> down the wire.
8983      In blocking mode, this function will return only when a full buffer has
8984      been written, or an error has occurred. In nonblocking mode, it's
8985      possible that the function will be unable to send the full buffer
8986      at once, which will be indicated by a return value of 1.
8987      The function will keep track of the number of octets already written; you
8988      should call it repeatedly with the same values of <literal>buf</literal>
8989      and <literal>len</literal>, until the buffer has been transmitted.
8990      When a full buffer has been sent, the function will return 0 for
8991      success. -1 indicates an error condition (see below).
8992     </para>
8993     <synopsis>
8994      int cs_get(COMSTACK handle, char **buf, int *size);
8995     </synopsis>
8996     <para>
8997      Receives a PDU or HTTP Response from the peer. Returns the number of
8998      bytes read.
8999      In nonblocking mode, it is possible that not all of the packet can be
9000      read at once. In this case, the function returns 1. To simplify the
9001      interface, the function is
9002      responsible for managing the size of the buffer. It will be reallocated
9003      if necessary to contain large packages, and will sometimes be moved
9004      around internally by the subsystem when partial packages are read. Before
9005      calling
9006      <function>cs_get</function>
9007      for the fist time, the buffer can be initialized to the null pointer,
9008      and the length should also be set to 0 - cs_get will perform a
9009      <function>malloc(2)</function>
9010      on the buffer for you. When a full buffer has been read, the size of
9011      the package is returned (which will always be greater than 1). -1
9012      indicates an error condition.
9013     </para>
9014     <para>
9015      See also the <function>cs_more()</function> function below.
9016     </para>
9017     <synopsis>
9018      int cs_more(COMSTACK handle);
9019     </synopsis>
9020     <para>
9021      The <function>cs_more()</function> function should be used in conjunction
9022      with <function>cs_get</function> and
9023      <function>select(2)</function>.
9024      The <function>cs_get()</function> function will sometimes
9025      (notably in the TCP/IP mode) read more than a single protocol package
9026      off the network. When this happens, the extra package is stored
9027      by the subsystem. After calling <function>cs_get()</function>, and before
9028      waiting for more input, You should always call
9029      <function>cs_more()</function>
9030      to check if there's a full protocol package already read. If
9031      <function>cs_more()</function>
9032      returns 1,
9033      <function>cs_get()</function>
9034      can be used to immediately fetch the new package. For the
9035      mOSI
9036      subsystem, the function should always return 0, but if you want your
9037      stuff to be protocol independent, you should use it.
9038     </para>
9039     <note>
9040      <para>
9041       The <function>cs_more()</function>
9042       function is required because the RFC1729-method
9043       does not provide a way of separating individual PDUs, short of
9044       partially decoding the BER. Some other implementations will carefully
9045       nibble at the packet by calling
9046       <function>read(2)</function>
9047       several times. This was felt to be too inefficient (or at least
9048       clumsy) - hence the call for this extra function.
9049      </para>
9050     </note>
9051     <synopsis>
9052      int cs_look(COMSTACK handle);
9053     </synopsis>
9054     <para>
9055      This function is useful when you're operating in nonblocking
9056      mode. Call it when
9057      <function>select(2)</function>
9058      tells you there's something happening on the line. It returns one of
9059      the following values:
9060     </para>
9061     <variablelist>
9062      <varlistentry>
9063       <term>CS_NONE</term>
9064       <listitem><para>
9065        No event is pending. The data found on the line was not a
9066        complete package.
9067       </para></listitem>
9068      </varlistentry>
9069      <varlistentry>
9070       <term>CS_CONNECT</term>
9071       <listitem><para>
9072        A response to your connect request has been received. Call
9073        <function>cs_rcvconnect</function>
9074        to process the event and to finalize the connection establishment.
9075       </para></listitem>
9076      </varlistentry>
9077      <varlistentry>
9078       <term>CS_DISCON</term>
9079       <listitem><para>
9080        The other side has closed the connection (or maybe sent a disconnect
9081        request - but do we care? Maybe later). Call
9082        <function>cs_close</function> to close your end of the association
9083        as well.
9084       </para></listitem>
9085      </varlistentry>
9086      <varlistentry>
9087       <term>CS_LISTEN</term>
9088       <listitem><para>
9089        A connect request has been received.
9090        Call <function>cs_listen</function> to process the event.
9091       </para></listitem>
9092      </varlistentry>
9093      <varlistentry>
9094       <term>CS_DATA</term>
9095       <listitem><para>
9096        There's data to be found on the line.
9097        Call <function>cs_get</function> to get it.
9098       </para></listitem>
9099      </varlistentry>
9100     </variablelist>
9101     <note>
9102      <para>
9103       You should be aware that even if
9104       <function>cs_look()</function>
9105       tells you that there's an event event pending, the corresponding
9106       function may still return and tell you there was nothing to be found.
9107       This means that only part of a package was available for reading. The
9108       same event will show up again, when more data has arrived.
9109      </para>
9110     </note>
9111     <synopsis>
9112      int cs_fileno(COMSTACK h);
9113     </synopsis>
9114     <para>
9115      Returns the file descriptor of the association. Use this when
9116      file-level operations on the endpoint are required
9117      (<function>select(2)</function> operations, specifically).
9118     </para>
9119    </sect2>
9120   </sect1>
9121   <sect1 id="comstack.client">
9122    <title>Client Side</title>
9123    <synopsis>
9124     int cs_connect(COMSTACK handle, void *address);
9125    </synopsis>
9126    <para>
9127     Initiate a connection with the target at <literal>address</literal>
9128     (more on addresses below). The function will return 0 on success, and 1 if
9129     the operation does not complete immediately (this will only
9130     happen on a nonblocking endpoint). In this case, use
9131     <function>cs_rcvconnect</function> to complete the operation,
9132     when <function>select(2)</function> or <function>poll(2)</function>
9133     reports input pending on the association.
9134    </para>
9135    <synopsis>
9136     int cs_rcvconnect(COMSTACK handle);
9137    </synopsis>
9138    <para>
9139     Complete a connect operation initiated by <function>cs_connect()</function>.
9140     It will return 0 on success; 1 if the operation has not yet completed (in
9141     this case, call the function again later); -1 if an error has occurred.
9142    </para>
9143   </sect1>
9144   <sect1 id="comstack.server">
9145    <title>Server Side</title>
9146    <para>
9147     To establish a server under the <application>inetd</application>
9148     server, you can use
9149    </para>
9150    <synopsis>
9151     COMSTACK cs_createbysocket(int socket, CS_TYPE type, int blocking,
9152                                int protocol);
9153    </synopsis>
9154    <para>
9155     The <literal>socket</literal> parameter is an established socket (when
9156     your application is invoked from <application>inetd</application>, the
9157     socket will typically be 0.
9158     The following parameters are identical to the ones for
9159     <function>cs_create</function>.
9160    </para>
9161    <synopsis>
9162     int cs_bind(COMSTACK handle, void *address, int mode)
9163    </synopsis>
9164    <para>
9165     Binds a local address to the endpoint. Read about addresses below. The
9166     <literal>mode</literal> parameter should be either
9167     <literal>CS_CLIENT</literal> or <literal>CS_SERVER</literal>.
9168    </para>
9169    <synopsis>
9170     int cs_listen(COMSTACK handle, char *addr, int *addrlen);
9171    </synopsis>
9172    <para>
9173     Call this to process incoming events on an endpoint that has been
9174     bound in listening mode. It will return 0 to indicate that the connect
9175     request has been received, 1 to signal a partial reception, and -1 to
9176     indicate an error condition.
9177    </para>
9178    <synopsis>
9179     COMSTACK cs_accept(COMSTACK handle);
9180    </synopsis>
9181    <para>
9182     This finalizes the server-side association establishment, after
9183     cs_listen has completed successfully. It returns a new connection
9184     endpoint, which represents the new association. The application will
9185     typically wish to fork off a process to handle the association at this
9186     point, and continue listen for new connections on the old
9187     <literal>handle</literal>.
9188    </para>
9189    <para>
9190     You can use the call
9191    </para>
9192    <synopsis>
9193     const char *cs_addrstr(COMSTACK);
9194    </synopsis>
9195    <para>
9196     on an established connection to retrieve the host-name of the remote host.
9197    </para>
9198    <note>
9199     <para>
9200      You may need to use this function with some care if your
9201      name server service is slow or unreliable
9202     </para>
9203    </note>
9204   </sect1>
9205   <sect1 id="comstack.addresses">
9206    <title>Addresses</title>
9207    <para>
9208     The low-level format of the addresses are different depending on the
9209     mode of communication you have chosen. A function is provided by each
9210     of the lower layers to map a user-friendly string-form address to the
9211     binary form required by the lower layers.
9212    </para>
9213    <synopsis>
9214     void *cs_straddr(COMSTACK handle, const char *str);
9215    </synopsis>
9216    <para>
9217     The format for TCP/IP and SSL addresses is:
9218    </para>
9219    <synopsis>
9220     &lt;host> [ ':' &lt;portnum> ]
9221    </synopsis>
9222    <para>
9223     The <literal>hostname</literal> can be either a domain name or an
9224     IP address. The port number, if omitted, defaults to 210.
9225    </para>
9226    <para>
9227     For TCP/IP and SSL, the special hostnames <literal>@</literal>,
9228     maps to <literal>IN6ADDR_ANY_INIT</literal> with
9229     IPV4 binding as well (bindv6only=0),
9230     The special hostname <literal>@4</literal> binds to
9231     <literal>INADDR_ANY</literal> (IPV4 only listener).
9232     The special hostname <literal>@6</literal> binds to
9233     <literal>IN6ADDR_ANY_INIT</literal> with bindv6only=1 (IPV6 only listener).
9234    </para>
9235    <para>
9236     For UNIX sockets, the format of an address is the socket filename.
9237    </para>
9238    <para>
9239     When a connection has been established, you can use
9240    </para>
9241    <synopsis>
9242     const char *cs_addrstr(COMSTACK h);
9243    </synopsis>
9244    <para>
9245     to retrieve the host name of the peer system. The function returns
9246     a pointer to a static area, which is overwritten on the next call
9247     to the function.
9248    </para>
9249    <para>
9250     A fairly recent addition to the &comstack; module is the utility
9251     function
9252    </para>
9253    <synopsis>
9254     COMSTACK cs_create_host (const char *str, int blocking, void **vp);
9255    </synopsis>
9256    <para>
9257     which is just a wrapper for <function>cs_create</function> and
9258     <function>cs_straddr</function>. The <parameter>str</parameter>
9259     is similar to that described for <function>cs_straddr</function>
9260     but with a prefix denoting the &comstack; type. Prefixes supported
9261     are <literal>tcp:</literal>, <literal>unix:</literal> and
9262     <literal>ssl:</literal> for TCP/IP, UNIX and SSL respectively.
9263     If no prefix is given, then TCP/IP is used.
9264     The <parameter>blocking</parameter> is passed to
9265     function <function>cs_create</function>. The third parameter
9266     <parameter>vp</parameter> is a pointer to &comstack; stack type
9267     specific values.
9268     Parameter <parameter>vp</parameter> is reserved for future use.
9269     Set it to <literal>NULL</literal>.
9270    </para>
9271   </sect1>
9272   <sect1 id="comstack.ssl">
9273    <title>SSL</title>
9274    <para>
9275     <synopsis>
9276      void *cs_get_ssl(COMSTACK cs);
9277     </synopsis>
9278     Returns the SSL handle, <literal>SSL *</literal> for comstack. If comstack
9279     is not of type SSL, NULL is returned.
9280    </para>
9281    <para>
9282     <synopsis>
9283      int cs_set_ssl_ctx(COMSTACK cs, void *ctx);
9284     </synopsis>
9285     Sets SSL context for comstack. The parameter is expected to be of type
9286     <literal>SSL_CTX *</literal>. This function should be called just
9287     after comstack has been created (before connect, bind, etc).
9288     This function returns 1 for success; 0 for failure.
9289    </para>
9290    <para>
9291     <synopsis>
9292      int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname);
9293     </synopsis>
9294     Sets SSL certificate for comstack as a PEM file. This function
9295     returns 1 for success; 0 for failure.
9296    </para>
9297    <para>
9298     <synopsis>
9299      int cs_get_ssl_peer_certificate_x509(COMSTACK cs, char **buf, int *len);
9300     </synopsis>
9301     This function returns the peer certificate. If successful,
9302     <literal>*buf</literal> and <literal>*len</literal> holds
9303     X509 buffer and length respectively. Buffer should be freed
9304     with <literal>xfree</literal>. This function returns 1 for success;
9305     0 for failure.
9306    </para>
9307   </sect1>
9308   <sect1 id="comstack.diagnostics">
9309    <title>Diagnostics</title>
9310    <para>
9311     All functions return -1 if an error occurs. Typically, the functions
9312     will return 0 on success, but the data exchange functions
9313     (<function>cs_get</function>, <function>cs_put</function>,
9314     <function>cs_more</function>) follow special rules. Consult their
9315     descriptions.
9316    </para>
9317    <para>
9318     The error code for the COMSTACK can be retrieved using C macro
9319     <function>cs_errno</function> which will return one
9320     of the error codes <literal>CSYSERR</literal>,
9321     <literal>CSOUTSTATE</literal>,
9322     <literal>CSNODATA</literal>, ...
9323    </para>
9324    <synopsis>
9325     int cs_errno(COMSTACK handle);
9326    </synopsis>
9327    <para>
9328     You can the textual representation of the error code
9329     by using <function>cs_errmsg</function> - which
9330     works like <function>strerror(3)</function>
9331    </para>
9332    <synopsis>
9333     const char *cs_errmsg(int n);
9334    </synopsis>
9335    <para>
9336     It is also possible to get straight to the textual represenataion
9337     without the error code by using
9338     <function>cs_strerror</function>.
9339    </para>
9340    <synopsis>
9341     const char *cs_strerror(COMSTACK h);
9342    </synopsis>
9343   </sect1>
9344   <sect1 id="comstack.summary">
9345    <title>Summary and Synopsis</title>
9346    <synopsis><![CDATA[
9347     #include <yaz/comstack.h>
9348
9349     #include <yaz/tcpip.h>  /* this is for TCP/IP and SSL support */
9350     #include <yaz/unix.h>   /* this is for UNIX socket support */
9351
9352     COMSTACK cs_create(CS_TYPE type, int blocking, int protocol);
9353
9354     COMSTACK cs_createbysocket(int s, CS_TYPE type, int blocking,
9355                                int protocol);
9356     COMSTACK cs_create_host(const char *str, int blocking,
9357                             void **vp);
9358
9359     int cs_bind(COMSTACK handle, int mode);
9360
9361     int cs_connect(COMSTACK handle, void *address);
9362
9363     int cs_rcvconnect(COMSTACK handle);
9364
9365     int cs_listen(COMSTACK handle);
9366
9367     COMSTACK cs_accept(COMSTACK handle);
9368
9369     int cs_put(COMSTACK handle, char *buf, int len);
9370
9371     int cs_get(COMSTACK handle, char **buf, int *size);
9372
9373     int cs_more(COMSTACK handle);
9374
9375     void cs_close(COMSTACK handle);
9376
9377     int cs_look(COMSTACK handle);
9378
9379     void *cs_straddr(COMSTACK handle, const char *str);
9380
9381     const char *cs_addrstr(COMSTACK h);
9382 ]]>
9383    </synopsis>
9384   </sect1>
9385  </chapter>
9386  <chapter id="future">
9387   <title>Future Directions</title>
9388   <para>
9389    We have a new and better version of the front-end server on the drawing
9390    board. Resources and external commitments will govern when we'll be
9391    able to do something real with it. Features should include greater
9392    flexibility, greater support for access/resource control, and easy
9393    support for Explain (possibly with Zebra as an extra database engine).
9394   </para>
9395   <para>
9396    &yaz; is a BER toolkit and as such should support all protocols
9397    out there based on that. We'd like to see running ILL applications.
9398    It shouldn't be that hard. Another thing that would be interesting is
9399    LDAP. Maybe a generic framework for doing IR using both LDAP and
9400    Z39.50 transparently.
9401   </para>
9402   <para>
9403    The SOAP implementation is incomplete. In the future we hope
9404    to add more features to it. Perhaps make a WSDL/XML Schema compiler.
9405    The authors of libxml2 are already working on XML Schema / RelaxNG
9406    compilers so this may not be too hard.
9407   </para>
9408   <para>
9409    It would be neat to have a proper module mechanism for the Generic
9410    Frontend Server so that backend would be dynamically
9411    loaded (as shared objects / DLLs).
9412   </para>
9413   <para>
9414    Other than that, &yaz; generally moves in the directions which appear to
9415    make the most people happy (including ourselves, as prime users of the
9416    software). If there's something you'd like to see in here, then drop
9417    us a note and let's see what we can come up with.
9418   </para>
9419  </chapter>
9420  <reference id="reference">
9421   <title>Reference</title>
9422    <partintro id="reference-introduction">
9423     <para>
9424      The material in this chapter is drawn directly from the individual
9425      manual entries.
9426     </para>
9427    </partintro>
9428    &manref;
9429  </reference>
9430  <appendix id="list-oids">
9431   <title>List of Object Identifiers</title>
9432   <para>
9433    These is a list of object identifiers that are built into YAZ.
9434   </para>
9435   &std-oid-table;
9436  </appendix>
9437  <appendix id="bib1-diagnostics">
9438   <title>Bib-1 diagnostics</title>
9439   <para>
9440    List of Bib-1 diagnostics that are known to YAZ.
9441   </para>
9442   &bib1-diag-table;
9443  </appendix>
9444  <appendix id="sru-diagnostics">
9445   <title>SRU diagnostics</title>
9446   <para>
9447    List of SRU diagnostics that are known to YAZ.
9448   </para>
9449   &srw-diag-table;
9450  </appendix>
9451  <appendix id="license">
9452   <title>License</title>
9453   <sect1 id="license.indexdata">
9454    <title>Index Data Copyright</title>
9455    <para>
9456     Copyright &#xa9; &copyright-year; Index Data.
9457    </para>
9458    <para>
9459     All rights reserved.
9460    </para>
9461    <para>
9462     Redistribution and use in source and binary forms, with or without
9463     modification, are permitted provided that the following conditions are met:
9464    </para>
9465    <itemizedlist>
9466     <listitem>
9467      <para>
9468       Redistributions of source code must retain the above copyright
9469       notice, this list of conditions and the following disclaimer.
9470      </para>
9471     </listitem>
9472     <listitem>
9473      <para>
9474       Redistributions in binary form must reproduce the above copyright
9475       notice, this list of conditions and the following disclaimer in the
9476       documentation and/or other materials provided with the distribution.
9477      </para>
9478     </listitem>
9479     <listitem>
9480      <para>
9481       Neither the name of Index Data nor the names of its contributors
9482       may be used to endorse or promote products derived from this
9483       software without specific prior written permission.
9484       </para>
9485     </listitem>
9486    </itemizedlist>
9487    <para>
9488     THIS SOFTWARE IS PROVIDED BY INDEX DATA ``AS IS'' AND ANY
9489     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9490     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
9491     DISCLAIMED. IN NO EVENT SHALL INDEX DATA BE LIABLE FOR
9492     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9493     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
9494     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
9495     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9496     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9497     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9498     SUCH DAMAGE.
9499    </para>
9500   </sect1>
9501  </appendix>
9502  <appendix id="indexdata">
9503   <title>About Index Data</title>
9504   <para>
9505    Index Data is a consulting and software-development enterprise that
9506    specializes in library and information management systems. Our
9507    interests and expertise span a broad range of related fields, and one
9508    of our primary, long-term objectives is the development of a powerful
9509    information management
9510    system with open network interfaces and hyper-media capabilities.
9511   </para><para>
9512    We make this software available free of charge, on a fairly unrestrictive
9513    license; as a service to the networking community, and to further the
9514    development of quality software for open network communication.
9515   </para><para>
9516    We'll be happy to answer questions about the software, and about ourselves
9517    in general.
9518   </para>
9519   <para>
9520    <address>
9521     Index Data ApS
9522     <street>Amagerf&#xe6;lledvej 56</street>
9523     <postcode>2300 Copenhagen S</postcode>
9524     <country>Denmark</country>
9525     Email <email>info@indexdata.dk</email>
9526    </address>
9527   </para>
9528   <para>
9529    The Hacker's Jargon File has the following to say about the
9530    use of the
9531    prefix &quot;YA&quot; in the name of a software product.
9532   </para>
9533   <para>
9534    <citation>
9535     Yet Another. adj. 1. Of your own work: A
9536     humorous allusion often used in titles to acknowledge that the
9537     topic is not original, though the content is.  As in &quot;Yet Another
9538     AI Group&quot; or &quot;Yet Another Simulated Annealing Algorithm&quot;.
9539     2. Of
9540     others' work: Describes something of which there are already far
9541     too many.
9542    </citation>
9543   </para>
9544  </appendix>
9545  <appendix id="credits">
9546   <title>Credits</title>
9547   <para>
9548    This appendix lists individuals that have contributed in the development
9549    of &yaz;. Some have contributed with code, while others have provided bug
9550    fixes or suggestions. If we're missing somebody, of if you, for
9551    whatever reason, don't like to be listed here, let us know.
9552   </para>
9553   <itemizedlist>
9554    <listitem><para>
9555     Gary Anderson
9556    </para></listitem>
9557    <listitem><para>
9558     Dimitrios Andreadis
9559     </para></listitem>
9560    <listitem><para>
9561     Morten B&#xf8;geskov
9562    </para></listitem>
9563    <listitem><para>
9564     Rocco Carbone
9565    </para></listitem>
9566    <listitem><para>
9567     Matthew Carey
9568    </para></listitem>
9569    <listitem><para>
9570     Hans van Dalen
9571    </para></listitem>
9572    <listitem><para>
9573     Irina Dijour
9574    </para></listitem>
9575    <listitem><para>
9576     Larry E. Dixson
9577    </para></listitem>
9578    <listitem><para>
9579     Hans van den Dool
9580    </para></listitem>
9581    <listitem><para>
9582     Mads Bondo Dydensborg
9583    </para></listitem>
9584    <listitem><para>
9585     Franck Falcoz
9586    </para></listitem>
9587    <listitem><para>
9588     Kevin Gamiel
9589    </para></listitem>
9590    <listitem><para>
9591     Morten Garkier Hendriksen
9592    </para></listitem>
9593    <listitem><para>
9594     Morten Holmqvist
9595    </para></listitem>
9596    <listitem><para>
9597     Ian Ibbotson
9598    </para></listitem>
9599    <listitem><para>
9600     Shigeru Ishida
9601    </para></listitem>
9602    <listitem><para>
9603     Heiko Jansen
9604    </para></listitem>
9605    <listitem><para>
9606     David Johnson
9607    </para></listitem>
9608    <listitem><para>
9609     Oleg Kolobov
9610    </para></listitem>
9611    <listitem><para>
9612     Giannis Kosmas
9613    </para></listitem>
9614    <listitem><para>
9615     Kang-Jin Lee
9616    </para></listitem>
9617    <listitem><para>
9618     Pieter Van Lierop
9619    </para></listitem>
9620    <listitem><para>
9621     Stefan Lohrum
9622    </para></listitem>
9623    <listitem><para>
9624     Ronald van der Meer
9625    </para></listitem>
9626    <listitem><para>
9627     Thomas W. Place
9628    </para></listitem>
9629    <listitem><para>
9630     Peter Popovics
9631    </para></listitem>
9632    <listitem><para>
9633     Jacob Chr. Poulsen
9634    </para></listitem>
9635    <listitem><para>
9636     Ko van der Sloot
9637    </para></listitem>
9638    <listitem><para>
9639     Mike Taylor
9640    </para></listitem>
9641    <listitem><para>
9642     Rustam T. Usmanov
9643    </para></listitem>
9644    <listitem><para>
9645     Charles Woodfield
9646    </para></listitem>
9647    <listitem><para>
9648     Tom Andr&#xe9; &#xd8;verland
9649    </para></listitem>
9650   </itemizedlist>
9651  </appendix>
9652 </book>
9653
9654 <!-- Keep this comment at the end of the file
9655 Local variables:
9656 mode: nxml
9657 nxml-child-indent: 1
9658 End:
9659 -->