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