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