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