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