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