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