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