Add documentation for CQL->RPN transformation's error-reporting
[yaz-moved-to-github.git] / doc / tools.xml
1 <!-- $Id: tools.xml,v 1.23 2003-05-22 16:57:28 mike Exp $ -->
2  <chapter id="tools"><title>Supporting Tools</title>
3   
4   <para>
5    In support of the service API - primarily the ASN module, which
6    provides the pro-grammatic interface to the Z39.50 APDUs, &yaz; contains
7    a collection of tools that support the development of applications.
8   </para>
9
10   <sect1 id="tools.query"><title>Query Syntax Parsers</title>
11
12    <para>
13     Since the type-1 (RPN) query structure has no direct, useful string
14     representation, every origin application needs to provide some form of
15     mapping from a local query notation or representation to a
16     <token>Z_RPNQuery</token> structure. Some programmers will prefer to
17     construct the query manually, perhaps using
18     <function>odr_malloc()</function> to simplify memory management.
19     The &yaz; distribution includes two separate, query-generating tools
20     that may be of use to you.
21    </para>
22
23    <sect2 id="PQF"><title>Prefix Query Format</title>
24
25     <para>
26      Since RPN or reverse polish notation is really just a fancy way of
27      describing a suffix notation format (operator follows operands), it
28      would seem that the confusion is total when we now introduce a prefix
29      notation for RPN. The reason is one of simple laziness - it's somewhat
30      simpler to interpret a prefix format, and this utility was designed
31      for maximum simplicity, to provide a baseline representation for use
32      in simple test applications and scripting environments (like Tcl). The
33      demonstration client included with YAZ uses the PQF.
34     </para>
35
36     <note>
37      <para>
38       The PQF have been adopted by other parties developing Z39.50
39       software. It is often referred to as Prefix Query Notation
40       - PQN.
41      </para>
42     </note>
43     <para>
44      The PQF is defined by the pquery module in the YAZ library. 
45      There are two sets of function that have similar behavior. First
46      set operates on a PQF parser handle, second set doesn't. First set
47      set of functions are more flexible than the second set. Second set
48      is obsolete and is only provided to ensure backwards compatibility.
49     </para>
50     <para>
51      First set of functions all operate on a PQF parser handle:
52     </para>
53     <synopsis>
54      #include &lt;yaz/pquery.h&gt;
55
56      YAZ_PQF_Parser yaz_pqf_create (void);
57
58      void yaz_pqf_destroy (YAZ_PQF_Parser p);
59
60      Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf);
61
62      Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
63                           Odr_oid **attributeSetId, const char *qbuf);
64
65
66      int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off);
67     </synopsis>
68     <para>
69      A PQF parser is created and destructed by functions
70      <function>yaz_pqf_create</function> and
71      <function>yaz_pqf_destroy</function> respectively.
72      Function <function>yaz_pqf_parse</function> parses query given
73      by string <literal>qbuf</literal>. If parsing was successful,
74      a Z39.50 RPN Query is returned which is created using ODR stream
75      <literal>o</literal>. If parsing failed, a NULL pointer is
76      returned.
77      Function <function>yaz_pqf_scan</function> takes a scan query in 
78      <literal>qbuf</literal>. If parsing was successful, the function
79      returns attributes plus term pointer and modifies
80      <literal>attributeSetId</literal> to hold attribute set for the
81      scan request - both allocated using ODR stream <literal>o</literal>.
82      If parsing failed, yaz_pqf_scan returns a NULL pointer.
83      Error information for bad queries can be obtained by a call to
84      <function>yaz_pqf_error</function> which returns an error code and
85      modifies <literal>*msg</literal> to point to an error description,
86      and modifies <literal>*off</literal> to the offset within last
87      query were parsing failed.
88     </para>
89     <para>
90      The second set of functions are declared as follows:
91     </para>
92     <synopsis>
93      #include &lt;yaz/pquery.h&gt;
94
95      Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf);
96
97      Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
98                              Odr_oid **attributeSetP, const char *qbuf);
99
100      int p_query_attset (const char *arg);
101     </synopsis>
102     <para>
103      The function <function>p_query_rpn()</function> takes as arguments an
104       &odr; stream (see section <link linkend="odr">The ODR Module</link>)
105      to provide a memory source (the structure created is released on
106      the next call to <function>odr_reset()</function> on the stream), a
107      protocol identifier (one of the constants <token>PROTO_Z3950</token> and
108      <token>PROTO_SR</token>), an attribute set reference, and
109      finally a null-terminated string holding the query string.
110     </para>
111     <para>
112      If the parse went well, <function>p_query_rpn()</function> returns a
113      pointer to a <literal>Z_RPNQuery</literal> structure which can be
114      placed directly into a <literal>Z_SearchRequest</literal>. 
115      If parsing failed, due to syntax error, a NULL pointer is returned.
116     </para>
117     <para>
118      The <literal>p_query_attset</literal> specifies which attribute set
119      to use if the query doesn't specify one by the
120      <literal>@attrset</literal> operator.
121      The <literal>p_query_attset</literal> returns 0 if the argument is a
122      valid attribute set specifier; otherwise the function returns -1.
123     </para>
124
125     <para>
126      The grammar of the PQF is as follows:
127     </para>
128
129     <literallayout>
130      query ::= top-set query-struct.
131
132      top-set ::= &lsqb; '@attrset' string &rsqb;
133
134      query-struct ::= attr-spec | simple | complex | '@term' term-type
135
136      attr-spec ::= '@attr' &lsqb; string &rsqb; string query-struct
137
138      complex ::= operator query-struct query-struct.
139
140      operator ::= '@and' | '@or' | '@not' | '@prox' proximity.
141
142      simple ::= result-set | term.
143
144      result-set ::= '@set' string.
145
146      term ::= string.
147
148      proximity ::= exclusion distance ordered relation which-code unit-code.
149
150      exclusion ::= '1' | '0' | 'void'.
151
152      distance ::= integer.
153
154      ordered ::= '1' | '0'.
155
156      relation ::= integer.
157
158      which-code ::= 'known' | 'private' | integer.
159
160      unit-code ::= integer.
161
162      term-type ::= 'general' | 'numeric' | 'string' | 'oid' | 'datetime' | 'null'.
163     </literallayout>
164
165     <para>
166      You will note that the syntax above is a fairly faithful
167      representation of RPN, except for the Attribute, which has been
168      moved a step away from the term, allowing you to associate one or more
169      attributes with an entire query structure. The parser will
170      automatically apply the given attributes to each term as required.
171     </para>
172
173     <para>
174      The @attr operator is followed by an attribute specification 
175      (<literal>attr-spec</literal> above). The specification consists
176      of optional an attribute set, an attribute type-value pair and
177      a sub query. The attribute type-value pair is packed in one string:
178      an attribute type, a dash, followed by an attribute value. 
179      The type is always an integer but the value may be either an
180      integer or a string (if it doesn't start with a digit character).
181     </para>
182
183     <para>
184      Version 3 of the Z39.50 specification defines various encoding of terms.
185      Use <literal>@term </literal> <replaceable>type</replaceable>
186      <replaceable>string</replaceable>,
187      where type is one of: <literal>general</literal>,
188      <literal>numeric</literal> or <literal>string</literal>
189      (for InternationalString).
190      If no term type has been given, the <literal>general</literal> form
191      is used.  This is the only encoding allowed in both versions 2 and 3
192      of the Z39.50 standard.
193     </para>
194     
195     <sect3 id="PQF-prox">
196       <title>Using Proximity Operators with PQF</title>
197       <note>
198         <para>
199           This is an advanced topic, describing how to construct
200           queries that make very specific requirements on the
201           relative location of their operands.
202           You may wish to skip this section and go straight to
203           <link linkend="pqf-examples">the example PQF queries</link>.
204         </para>
205         <para>
206           <warning>
207             <para>
208               Most Z39.50 servers do not support proximity searching, or
209               support only a small subset of the full functionality that
210               can be expressed using the PQF proximity operator.  Be
211               aware that the ability to <emphasis>express</emphasis> a
212               query in PQF is no guarantee that any given server will
213               be able to <emphasis>execute</emphasis> it.
214             </para>
215           </warning>
216         </para>
217       </note>
218       <para>
219         The proximity operator <literal>@prox</literal> is a special
220         and more restrictive version of the conjunction operator
221         <literal>@and</literal>.  Its semantics are described in 
222         section 3.7.2 (Proximity) of Z39.50 the standard itself, which
223         can be read on-line at
224         <ulink url="http://lcweb.loc.gov/z3950/agency/markup/09.html"/>
225       </para>
226       <para>
227         In PQF, the proximity operation is represented by a sequence
228         of the form
229         <screen>
230 @prox <replaceable>exclusion</replaceable> <replaceable>distance</replaceable> <replaceable>ordered</replaceable> <replaceable>relation</replaceable> <replaceable>which-code</replaceable> <replaceable>unit-code</replaceable>
231         </screen>
232         in which the meanings of the parameters are as described in in
233         the standard, and they can take the following values:
234         <itemizedlist>
235           <listitem><formalpara><title>exclusion</title><para>
236             0 = false (i.e. the proximity condition specified by the
237             remaining parameters must be satisfied) or
238             1 = true (the proximity condition specified by the
239             remaining parameters must <emphasis>not</emphasis> be
240             satisifed).
241           </para></formalpara></listitem>
242           <listitem><formalpara><title>distance</title><para>
243             An integer specifying the difference between the locations
244             of the operands: e.g. two adjacent words would have
245             distance=1 since their locations differ by one unit.
246           </para></formalpara></listitem>
247           <listitem><formalpara><title>ordered</title><para>
248             1 = ordered (the operands must occur in the order the
249             query specifies them) or
250             0 = unordered (they may appear in either order).
251           </para></formalpara></listitem>
252           <listitem><formalpara><title>relation</title><para>
253             Recognised values are
254             1 (lessThan),
255             2 (lessThanOrEqual),
256             3 (equal),
257             4 (greaterThanOrEqual),
258             5 (greaterThan) and
259             6 (notEqual).
260           </para></formalpara></listitem>
261           <listitem><formalpara><title>which-code</title><para>
262             <literal>known</literal>
263             or
264             <literal>k</literal>
265             (the unit-code parameter is taken from the well-known list
266             of alternatives described in below) or
267             <literal>private</literal>
268             or
269             <literal>p</literal>
270             (the unit-code paramater has semantics specific to an
271             out-of-band agreement such as a profile).
272           </para></formalpara></listitem>
273           <listitem><formalpara><title>unit-code</title><para>
274             If the which-code parameter is <literal>known</literal>
275             then the recognised values are
276             1 (character),
277             2 (word),
278             3 (sentence),
279             4 (paragraph),
280             5 (section),
281             6 (chapter),
282             7 (document),
283             8 (element),
284             9 (subelement),
285             10 (elementType) and
286             11 (byte).
287             If which-code is <literal>private</literal> then the
288             acceptable values are determined by the profile.
289           </para></formalpara></listitem>
290         </itemizedlist>
291         (The numeric values of the relation and well-known unit-code
292         parameters are taken straight from
293         <ulink url="http://lcweb.loc.gov/z3950/agency/asn1.html#ProximityOperator"
294         >the ASN.1</ulink> of the proximity structure in the standard.)
295       </para>
296     </sect3>
297
298     <sect3 id="pqf-examples"><title>PQF queries</title>
299
300      <para>Queries using simple terms.
301       <screen>
302       dylan
303       "bob dylan"
304       </screen>
305      </para>
306      <para>Boolean operators.
307       <screen>
308        @or "dylan" "zimmerman"
309        @and @or dylan zimmerman when
310        @and when @or dylan zimmerman
311       </screen>
312      </para>
313      <para>
314       Reference to result sets.
315       <screen>
316        @set Result-1
317        @and @set seta setb
318       </screen>
319      </para>
320      <para>
321       Attributes for terms.
322       <screen>
323        @attr 1=4 computer
324        @attr 1=4 @attr 4=1 "self portrait"
325        @attr exp1 @attr 1=1 CategoryList
326        @attr gils 1=2008 Copenhagen
327        @attr 1=/book/title computer
328       </screen>
329      </para>
330      <para>
331       Proximity.
332       <screen>
333        @prox 0 3 1 2 k 2 dylan zimmerman
334       </screen>
335       <note><para>
336       Here the parameters 0, 3, 1, 2, k and 2 represent exclusion,
337       distance, ordered, relation, which-code and unit-code, in that
338       order.  So:
339       <itemizedlist>
340         <listitem><para>
341           exclusion = 0: the proximity condition must hold
342         </para></listitem>
343         <listitem><para>
344           distance = 3: the terms must be three units apart
345         </para></listitem>
346         <listitem><para>
347           ordered = 1: they must occur in the order they are specified
348         </para></listitem>
349         <listitem><para>
350           relation = 2: lessThanOrEqual (to the distance of 3 units)
351         </para></listitem>
352         <listitem><para>
353           which-code is ``known'', so the standard unit-codes are used
354         </para></listitem>
355         <listitem><para>
356           unit-code = 2: word.
357         </para></listitem>
358       </itemizedlist>
359       So the whole proximity query means that the words
360       <literal>dylan</literal> and <literal>zimmerman</literal> must
361       both occur in the record, in that order, differing in position
362       by three or fewer words (i.e. with two or fewer words between
363       them.)  The query would find ``Bob Dylan, aka. Robert
364       Zimmerman'', but not ``Bob Dylan, born as Robert Zimmerman''
365       since the distance in this case is four.
366       </para></note>
367      </para>
368      <para>
369       Specifying term type.
370       <screen>
371        @term string "a UTF-8 string, maybe?"
372       </screen>
373      </para>
374      <para>Mixed queries
375       <screen>
376        @or @and bob dylan @set Result-1
377        
378        @attr 4=1 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
379        
380        @and @attr 2=4 @attr gils 1=2038 -114 @attr 2=2 @attr gils 1=2039 -109
381       </screen>
382       <note>
383         <para>
384           The last of these examples is a spatial search: in
385           <ulink url="http://www.gils.net/prof_v2.html#sec_7_4"
386           >the GILS attribute set</ulink>,
387           access point
388           2038 indicates West Bounding Coordinate and
389           2030 indicates East Bounding Coordinate,
390           so the query is for areas extending from -114 degrees
391           to no more than -109 degrees.
392         </para>
393       </note>
394      </para>
395     </sect3>
396    </sect2>
397    <sect2 id="CCL"><title>Common Command Language</title>
398
399     <para>
400      Not all users enjoy typing in prefix query structures and numerical
401      attribute values, even in a minimalistic test client. In the library
402      world, the more intuitive Common Command Language (or ISO 8777) has
403      enjoyed some popularity - especially before the widespread
404      availability of graphical interfaces. It is still useful in
405      applications where you for some reason or other need to provide a
406      symbolic language for expressing boolean query structures.
407     </para>
408
409     <para>
410      The <ulink url="http://europagate.dtv.dk/">EUROPAGATE</ulink>
411      research project working under the Libraries programme
412      of the European Commission's DG XIII has, amongst other useful tools,
413      implemented a general-purpose CCL parser which produces an output
414      structure that can be trivially converted to the internal RPN
415      representation of &yaz; (The <literal>Z_RPNQuery</literal> structure).
416      Since the CCL utility - along with the rest of the software
417      produced by EUROPAGATE - is made freely available on a liberal
418      license, it is included as a supplement to &yaz;.
419     </para>
420
421     <sect3><title>CCL Syntax</title>
422
423      <para>
424       The CCL parser obeys the following grammar for the FIND argument.
425       The syntax is annotated by in the lines prefixed by
426       <literal>&dash;&dash;</literal>.
427      </para>
428
429      <screen>
430       CCL-Find ::= CCL-Find Op Elements
431                 | Elements.
432
433       Op ::= "and" | "or" | "not"
434       -- The above means that Elements are separated by boolean operators.
435
436       Elements ::= '(' CCL-Find ')'
437                 | Set
438                 | Terms
439                 | Qualifiers Relation Terms
440                 | Qualifiers Relation '(' CCL-Find ')'
441                 | Qualifiers '=' string '-' string
442       -- Elements is either a recursive definition, a result set reference, a
443       -- list of terms, qualifiers followed by terms, qualifiers followed
444       -- by a recursive definition or qualifiers in a range (lower - upper).
445
446       Set ::= 'set' = string
447       -- Reference to a result set
448
449       Terms ::= Terms Prox Term
450              | Term
451       -- Proximity of terms.
452
453       Term ::= Term string
454             | string
455       -- This basically means that a term may include a blank
456
457       Qualifiers ::= Qualifiers ',' string
458                   | string
459       -- Qualifiers is a list of strings separated by comma
460
461       Relation ::= '=' | '>=' | '&lt;=' | '&lt;>' | '>' | '&lt;'
462       -- Relational operators. This really doesn't follow the ISO8777
463       -- standard.
464
465       Prox ::= '%' | '!'
466       -- Proximity operator
467
468      </screen>
469      
470      <example><title>CCL queries</title>
471       <para>
472        The following queries are all valid:
473       </para>
474       
475       <screen>
476        dylan
477        
478        "bob dylan"
479        
480        dylan or zimmerman
481        
482        set=1
483        
484        (dylan and bob) or set=1
485        
486       </screen>
487       <para>
488        Assuming that the qualifiers <literal>ti</literal>,
489        <literal>au</literal>
490        and <literal>date</literal> are defined we may use:
491       </para>
492       
493       <screen>
494        ti=self portrait
495        
496        au=(bob dylan and slow train coming)
497
498        date>1980 and (ti=((self portrait)))
499        
500       </screen>
501      </example>
502      
503     </sect3>
504     <sect3><title>CCL Qualifiers</title>
505      
506      <para>
507       Qualifiers are used to direct the search to a particular searchable
508       index, such as title (ti) and author indexes (au). The CCL standard
509       itself doesn't specify a particular set of qualifiers, but it does
510       suggest a few short-hand notations. You can customize the CCL parser
511       to support a particular set of qualifiers to reflect the current target
512       profile. Traditionally, a qualifier would map to a particular
513       use-attribute within the BIB-1 attribute set. However, you could also
514       define qualifiers that would set, for example, the
515       structure-attribute.
516      </para>
517
518      <para>
519       A  CCL profile is a set of predefined CCL qualifiers that may be
520       read from a file.
521       The YAZ client reads its CCL qualifiers from a file named
522       <filename>default.bib</filename>. Each line in the file has the form:
523      </para>
524
525      <para>
526       <replaceable>qualifier-name</replaceable>  
527       [<replaceable>attributeset</replaceable><literal>,</literal>]<replaceable>type</replaceable><literal>=</literal><replaceable>val</replaceable>
528       [<replaceable>attributeset</replaceable><literal>,</literal>]<replaceable>type</replaceable><literal>=</literal><replaceable>val</replaceable> ...      
529      </para>
530
531      <para>
532       where <replaceable>qualifier-name</replaceable> is the name of the
533       qualifier to be used (eg. <literal>ti</literal>),
534       <replaceable>type</replaceable> is attribute type in the attribute
535       set (Bib-1 is used if no attribute set is given) and
536       <replaceable>val</replaceable> is attribute value.
537       The <replaceable>type</replaceable> can be specified as an
538       integer or as it be specified either as a single-letter:
539       <literal>u</literal> for use, 
540       <literal>r</literal> for relation,<literal>p</literal> for position,
541       <literal>s</literal> for structure,<literal>t</literal> for truncation
542       or <literal>c</literal> for completeness.
543       The attributes for the special qualifier name <literal>term</literal>
544       are used when no CCL qualifier is given in a query.
545      </para>
546
547      <example><title>CCL profile</title>
548       <para>
549        Consider the following definition:
550       </para>
551       
552       <screen>
553        ti       u=4 s=1
554        au       u=1 s=1
555        term     s=105
556        ranked   r=102
557       </screen>
558       <para>
559        Three qualifiers are defined, <literal>ti</literal>, 
560        <literal>au</literal> and <literal>ranked</literal>.
561        <literal>ti</literal> and <literal>au</literal> both set 
562        structure attribute to phrase (s=1).
563        <literal>ti</literal>
564        sets the use-attribute to 4. <literal>au</literal> sets the
565        use-attribute to 1.
566        When no qualifiers are used in the query the structure-attribute is
567        set to free-form-text (105).
568       </para>
569       <para>
570        You can combine attributes. To Search for "ranked title" you
571        can do 
572        <screen>
573         ti,ranked=knuth computer
574        </screen>
575        which will use "relation is ranked", "use is title", "structure is
576        phrase".
577       </para>
578      </example>
579      
580     </sect3>
581     <sect3><title>CCL API</title>
582      <para>
583       All public definitions can be found in the header file
584       <filename>ccl.h</filename>. A profile identifier is of type
585       <literal>CCL_bibset</literal>. A profile must be created with the call
586       to the function <function>ccl_qual_mk</function> which returns a profile
587       handle of type <literal>CCL_bibset</literal>.
588      </para>
589
590      <para>
591       To read a file containing qualifier definitions the function
592       <function>ccl_qual_file</function> may be convenient. This function
593       takes an already opened <literal>FILE</literal> handle pointer as
594       argument along with a <literal>CCL_bibset</literal> handle.
595      </para>
596
597      <para>
598       To parse a simple string with a FIND query use the function
599      </para>
600      <screen>
601 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
602                                    int *error, int *pos);
603      </screen>
604      <para>
605       which takes the CCL profile (<literal>bibset</literal>) and query
606       (<literal>str</literal>) as input. Upon successful completion the RPN
607       tree is returned. If an error occur, such as a syntax error, the integer
608       pointed to by <literal>error</literal> holds the error code and
609       <literal>pos</literal> holds the offset inside query string in which
610       the parsing failed.
611      </para>
612
613      <para>
614       An English representation of the error may be obtained by calling
615       the <literal>ccl_err_msg</literal> function. The error codes are
616       listed in <filename>ccl.h</filename>.
617      </para>
618
619      <para>
620       To convert the CCL RPN tree (type
621       <literal>struct ccl_rpn_node *</literal>)
622       to the Z_RPNQuery of YAZ the function <function>ccl_rpn_query</function>
623       must be used. This function which is part of YAZ is implemented in
624       <filename>yaz-ccl.c</filename>.
625       After calling this function the CCL RPN tree is probably no longer
626       needed. The <literal>ccl_rpn_delete</literal> destroys the CCL RPN tree.
627      </para>
628
629      <para>
630       A CCL profile may be destroyed by calling the
631       <function>ccl_qual_rm</function> function.
632      </para>
633
634      <para>
635       The token names for the CCL operators may be changed by setting the
636       globals (all type <literal>char *</literal>)
637       <literal>ccl_token_and</literal>, <literal>ccl_token_or</literal>,
638       <literal>ccl_token_not</literal> and <literal>ccl_token_set</literal>.
639       An operator may have aliases, i.e. there may be more than one name for
640       the operator. To do this, separate each alias with a space character.
641      </para>
642     </sect3>
643    </sect2>
644    <sect2 id="tools.cql"><title>CQL</title>
645     <para>
646      <ulink url="http://www.loc.gov/z3950/agency/zing/cql/">CQL</ulink>
647       - Common Query Language - was defined for the
648      <ulink url="http://www.loc.gov/z3950/agency/zing/srw/">SRW</ulink>
649      protocol.
650      In many ways CQL has a similar syntax to CCL.
651      The objective of CQL is different. Where CCL aims to be
652      an end-user language, CQL is <emphasis>the</emphasis> protocol
653      query language for SRW.
654     </para>
655     <tip>
656      <para>
657       If you are new to CQL, read the 
658       <ulink url="http://zing.z3950.org/cql/intro.html">Gentle
659        Introduction</ulink>.
660      </para>
661     </tip>
662     <para>
663      The CQL parser in &yaz; provides the following:
664      <itemizedlist>
665       <listitem>
666        <para>
667         It parses and validates a CQL query.
668        </para>
669       </listitem>
670       <listitem>
671        <para>
672         It generates a C structure that allows you to convert
673         a CQL query to some other query language, such as SQL.
674        </para>
675       </listitem>
676       <listitem>
677        <para>
678         The parser converts a valid CQL query to PQF, thus providing a
679         way to use CQL for both SRW/SRU servers and Z39.50 targets at the
680         same time.
681        </para>
682       </listitem>
683       <listitem>
684        <para>
685         The parser converts CQL to
686         <ulink url="http://www.loc.gov/z3950/agency/zing/cql/xcql.html">
687          XCQL</ulink>.
688         XCQL is an XML representation of CQL.
689         XCQL is part of the SRW specification. However, since SRU
690         supports CQL only, we don't expect XCQL to be widely used.
691         Furthermore, CQL has the advantage over XCQL that it is
692         easy to read.
693        </para>
694       </listitem>
695      </itemizedlist>
696     </para>
697     <sect3 id="tools.cql.parsing"><title>CQL parsing</title>
698      <para>
699       A CQL parser is represented by the <literal>CQL_parser</literal>
700       handle. Its contents should be considered &yaz; internal (private).
701       <synopsis>
702 #include &lt;yaz/cql.h&gt;
703
704 typedef struct cql_parser *CQL_parser;
705
706 CQL_parser cql_parser_create(void);
707 void cql_parser_destroy(CQL_parser cp);
708       </synopsis>
709      A parser is created by <function>cql_parser_create</function> and
710      is destroyed by <function>cql_parser_destroy</function>.
711      </para>
712      <para>
713       To parse a CQL query string, the following function
714       is provided:
715       <synopsis>
716 int cql_parser_string(CQL_parser cp, const char *str);
717       </synopsis>
718       A CQL query is parsed by the <function>cql_parser_string</function>
719       which takes a query <parameter>str</parameter>.
720       If the query was valid (no syntax errors), then zero is returned;
721       otherwise a non-zero error code is returned.
722      </para>
723      <para>
724       <synopsis>
725 int cql_parser_stream(CQL_parser cp,
726                       int (*getbyte)(void *client_data),
727                       void (*ungetbyte)(int b, void *client_data),
728                       void *client_data);
729
730 int cql_parser_stdio(CQL_parser cp, FILE *f);
731       </synopsis>
732       The functions <function>cql_parser_stream</function> and
733       <function>cql_parser_stdio</function> parses a CQL query
734       - just like <function>cql_parser_string</function>.
735       The only difference is that the CQL query can be
736       fed to the parser in different ways.
737       The <function>cql_parser_stream</function> uses a generic
738       byte stream as input. The <function>cql_parser_stdio</function>
739       uses a <literal>FILE</literal> handle which is opened for reading.
740      </para>
741     </sect3>
742     
743     <sect3 id="tools.cql.tree"><title>CQL tree</title>
744      <para>
745       The the query string is validl, the CQL parser
746       generates a tree representing the structure of the
747       CQL query.
748      </para>
749      <para>
750       <synopsis>
751 struct cql_node *cql_parser_result(CQL_parser cp);
752       </synopsis>
753       <function>cql_parser_result</function> returns the
754       a pointer to the root node of the resulting tree.
755      </para>
756      <para>
757       Each node in a CQL tree is represented by a 
758       <literal>struct cql_node</literal>.
759       It is defined as follows:
760       <synopsis>
761 #define CQL_NODE_ST 1
762 #define CQL_NODE_BOOL 2
763 #define CQL_NODE_MOD 3
764 struct cql_node {
765     int which;
766     union {
767         struct {
768             char *index;
769             char *term;
770             char *relation;
771             struct cql_node *modifiers;
772             struct cql_node *prefixes;
773         } st;
774         struct {
775             char *value;
776             struct cql_node *left;
777             struct cql_node *right;
778             struct cql_node *modifiers;
779             struct cql_node *prefixes;
780         } boolean;
781         struct {
782             char *name;
783             char *value;
784             struct cql_node *next;
785         } mod;
786     } u;
787 };
788       </synopsis>
789       There are three kinds of nodes, search term (ST), boolean (BOOL),
790       and modifier (MOD).
791      </para>
792      <para>
793       The search term node has five members:
794       <itemizedlist>
795        <listitem>
796         <para>
797          <literal>index</literal>: index for search term.
798          If an index is unspecified for a search term,
799          <literal>index</literal> will be NULL.
800         </para>
801        </listitem>
802        <listitem>
803         <para>
804          <literal>term</literal>: the search term itself.
805         </para>
806        </listitem>
807        <listitem>
808         <para>
809          <literal>relation</literal>: relation for search term.
810         </para>
811        </listitem>
812        <listitem>
813         <para>
814          <literal>modifiers</literal>: relation modifiers for search
815          term. The <literal>modifiers</literal> is a simple linked
816          list (NULL for last entry). Each relation modifier node
817          is of type <literal>MOD</literal>.
818         </para>
819        </listitem>
820        <listitem>
821         <para>
822          <literal>prefixes</literal>: index prefixes for search
823          term. The <literal>prefixes</literal> is a simple linked
824          list (NULL for last entry). Each prefix node
825          is of type <literal>MOD</literal>.
826         </para>
827        </listitem>
828       </itemizedlist>
829      </para>
830
831      <para>
832       The boolean node represents both <literal>and</literal>,
833       <literal>or</literal>, not as well as
834       proximity.
835       <itemizedlist>
836        <listitem>
837         <para>
838          <literal>left</literal> and <literal>right</literal>: left
839          - and right operand respectively.
840         </para>
841        </listitem>
842        <listitem>
843         <para>
844          <literal>modifiers</literal>: proximity arguments.
845         </para>
846        </listitem>
847        <listitem>
848         <para>
849          <literal>prefixes</literal>: index prefixes.
850          The <literal>prefixes</literal> is a simple linked
851          list (NULL for last entry). Each prefix node
852          is of type <literal>MOD</literal>.
853         </para>
854        </listitem>
855       </itemizedlist>
856      </para>
857
858      <para>
859       The modifier node is a "utility" node used for name-value pairs,
860       such as prefixes, proximity arguements, etc.
861       <itemizedlist>
862        <listitem>
863         <para>
864          <literal>name</literal> name of mod node.
865         </para>
866        </listitem>
867        <listitem>
868         <para>
869          <literal>value</literal> value of mod node.
870         </para>
871        </listitem>
872        <listitem>
873         <para>
874          <literal>next</literal>: pointer to next node which is
875          always a mod node (NULL for last entry).
876         </para>
877        </listitem>
878       </itemizedlist>
879      </para>
880
881     </sect3>
882     <sect3 id="tools.cql.pqf"><title>CQL to PQF conversion</title>
883      <para>
884       Conversion to PQF (and Z39.50 RPN) is tricky by the fact
885       that the resulting RPN depends on the Z39.50 target
886       capabilities (combinations of supported attributes). 
887       In addition, the CQL and SRW operates on index prefixes
888       (URI or strings), whereas the RPN uses Object Identifiers
889       for attribute sets.
890      </para>
891      <para>
892       The CQL library of &yaz; defines a <literal>cql_transform_t</literal>
893       type. It represents a particular mapping between CQL and RPN.
894       This handle is created and destroyed by the functions:
895      <synopsis>
896 cql_transform_t cql_transform_open_FILE (FILE *f);
897 cql_transform_t cql_transform_open_fname(const char *fname);
898 void cql_transform_close(cql_transform_t ct);
899       </synopsis>
900       The first two functions create a tranformation handle from
901       either an already open FILE or from a filename respectively.
902      </para>
903      <para>
904       The handle is destroyed by <function>cql_transform_close</function> 
905       in which case no further reference of the handle is allowed.
906      </para>
907      <para>
908       When a <literal>cql_transform_t</literal> handle has been created
909       you can convert to RPN.
910       <synopsis>
911 int cql_transform_buf(cql_transform_t ct,
912                       struct cql_node *cn, char *out, int max);
913       </synopsis>
914       This function converts the CQL tree <literal>cn</literal> 
915       using handle <literal>ct</literal>.
916       For the resulting PQF, you supply a buffer <literal>out</literal>
917       which must be able to hold at at least <literal>max</literal>
918       characters.
919      </para>
920      <para>
921       If conversion failed, <function>cql_transform_buf</function>
922       returns a non-zero SRW error code; otherwise zero is returned
923       (conversion successful).  The meanings of the numeric error
924       codes are listed in the SRW specifications at
925       <ulink url="http://www.loc.gov/srw/diagnostic-list.html"/>
926      </para>
927      <para>
928       If conversion fails, more information can be obtained by calling
929       <synopsis>
930 int cql_transform_error(cql_transform_t ct, char **addinfop);
931       </synopsis>
932       This function returns the most recently returned numeric
933       error-code and sets the string-pointer at
934       <literal>*addinfop</literal> to point to a string containing
935       additional information about the error that occurred: for
936       example, if the error code is 15 (``Illegal or unsupported index
937       set''), the additional information is the name of the requested
938       index set that was not recognised.
939      </para>
940      <para>
941       If you wish to be able to produce a PQF result in a different
942       way, there are two alternatives.
943       <synopsis>
944 void cql_transform_pr(cql_transform_t ct,
945                       struct cql_node *cn,
946                       void (*pr)(const char *buf, void *client_data),
947                       void *client_data);
948
949 int cql_transform_FILE(cql_transform_t ct,
950                        struct cql_node *cn, FILE *f);
951       </synopsis>
952       The former function produces output to a user-defined
953       output stream. The latter writes the result to an already
954       open <literal>FILE</literal>.
955      </para>
956     </sect3>
957     <sect3 id="tools.cql.map">
958      <title>Specification of CQL to RPN mapping</title>
959      <para>
960       The file supplied to functions 
961       <function>cql_transform_open_FILE</function>,
962       <function>cql_transform_open_fname</function> follows
963       a structure found in many Unix utilities.
964       It consists of mapping specifications - one per line.
965       Lines starting with <literal>#</literal> are ignored (comments).
966      </para>
967      <para>
968       Each line is of the form
969       <literallayout>
970        <replaceable>CQL pattern</replaceable><literal> = </literal> <replaceable> RPN equivalent</replaceable>
971       </literallayout>
972      </para>
973      <para>
974       An RPN pattern is a simple attribute list. Each attribute pair
975       takes the form:
976       <literallayout>
977        [<replaceable>set</replaceable>] <replaceable>type</replaceable><literal>=</literal><replaceable>value</replaceable>
978       </literallayout>
979       The attribute <replaceable>set</replaceable> is optional.
980       The <replaceable>type</replaceable> is the attribute type,
981       <replaceable>value</replaceable> the attribute value.
982      </para>
983      <para>
984       The following CQL patterns are recognized:
985       <variablelist>
986        <varlistentry><term>
987          <literal>qualifier.</literal><replaceable>set</replaceable><literal>.</literal><replaceable>name</replaceable>
988         </term>
989         <listitem>
990          <para>
991           This pattern is invoked when a CQL qualifier, such as 
992           dc.title is converted. <replaceable>set</replaceable>
993           and <replaceable>name</replaceable> is the index set and qualifier
994           name respectively.
995           Typically, the RPN specifies an equivalent use attribute.
996          </para>
997          <para>
998           For terms not bound by a qualifier the pattern
999           <literal>qualifier.srw.serverChoice</literal> is used.
1000           Here, the prefix <literal>srw</literal> is defined as
1001           <literal>http://www.loc.gov/zing/cql/srw-indexes/v1.0/</literal>.
1002           If this pattern is not defined, the mapping will fail.
1003          </para>
1004         </listitem>
1005        </varlistentry>
1006        <varlistentry><term>
1007          <literal>relation.</literal><replaceable>relation</replaceable>
1008         </term>
1009         <listitem>
1010          <para>
1011           This pattern specifies how a CQL relation is mapped to RPN.
1012           <replaceable>pattern</replaceable> is name of relation
1013           operator. Since <literal>=</literal> is used as
1014           separator between CQL pattern and RPN, CQL relations
1015           including <literal>=</literal> cannot be
1016           used directly. To avoid a conflict, the names
1017           <literal>ge</literal>,
1018           <literal>eq</literal>,
1019           <literal>le</literal>,
1020           must be used for CQL operators, greater-than-or-equal,
1021           equal, less-than-or-equal respectively.
1022           The RPN pattern is supposed to include a relation attribute.
1023          </para>
1024          <para>
1025           For terms not bound by a relation, the pattern
1026           <literal>relation.scr</literal> is used. If the pattern
1027           is not defined, the mapping will fail.
1028          </para>
1029          <para>
1030           The special pattern, <literal>relation.*</literal> is used
1031           when no other relation pattern is matched.
1032          </para>
1033         </listitem>
1034        </varlistentry>
1035
1036        <varlistentry><term>
1037          <literal>relationModifier.</literal><replaceable>mod</replaceable>
1038         </term>
1039         <listitem>
1040          <para>
1041           This pattern specifies how a CQL relation modifier is mapped to RPN.
1042           The RPN pattern is usually a relation attribute.
1043          </para>
1044         </listitem>
1045        </varlistentry>
1046
1047        <varlistentry><term>
1048          <literal>structure.</literal><replaceable>type</replaceable>
1049         </term>
1050         <listitem>
1051          <para>
1052           This pattern specifies how a CQL structure is mapped to RPN.
1053           Note that this CQL pattern is somewhat to similar to
1054           CQL pattern <literal>relation</literal>. 
1055           The <replaceable>type</replaceable> is a CQL relation.
1056          </para>
1057          <para>
1058           The pattern, <literal>structure.*</literal> is used
1059           when no other structure pattern is matched.
1060           Usually, the RPN equivalent specifies a structure attribute.
1061          </para>
1062         </listitem>
1063        </varlistentry>
1064
1065        <varlistentry><term>
1066          <literal>position.</literal><replaceable>type</replaceable>
1067         </term>
1068         <listitem>
1069          <para>
1070           This pattern specifies how the anchor (position) of
1071           CQL is mapped to RPN.
1072           The <replaceable>type</replaceable> is one
1073           of <literal>first</literal>, <literal>any</literal>,
1074           <literal>last</literal>, <literal>firstAndLast</literal>.
1075          </para>
1076          <para>
1077           The pattern, <literal>position.*</literal> is used
1078           when no other position pattern is matched.
1079          </para>
1080         </listitem>
1081        </varlistentry>
1082
1083        <varlistentry><term>
1084          <literal>set.</literal><replaceable>prefix</replaceable>
1085         </term>
1086         <listitem>
1087          <para>
1088           This specification defines a CQL index set for a given prefix.
1089           The value on the right hand side is the URI for the set - 
1090           <emphasis>not</emphasis> RPN. All prefixes used in
1091           qualifier patterns must be defined this way.
1092          </para>
1093         </listitem>
1094        </varlistentry>
1095       </variablelist>
1096      </para>
1097      <example><title>CQL to RPN mapping file</title>
1098       <para>
1099        This simple file defines two index sets, three qualifiers and three
1100        relations, a position pattern and a default structure.
1101       </para>
1102       <programlisting><![CDATA[
1103        set.srw    = http://www.loc.gov/zing/cql/srw-indexes/v1.0/
1104        set.dc     = http://www.loc.gov/zing/cql/dc-indexes/v1.0/
1105
1106        qualifier.srw.serverChoice = 1=1016
1107        qualifier.dc.title         = 1=4
1108        qualifier.dc.subject       = 1=21
1109   
1110        relation.<                 = 2=1
1111        relation.eq                = 2=3
1112        relation.scr               = 2=3
1113
1114        position.any               = 3=3 6=1
1115
1116        structure.*                = 4=1
1117 ]]>
1118       </programlisting>
1119       <para>
1120        With the mappings above, the CQL query
1121        <screen>
1122         computer
1123        </screen>
1124        is converted to the PQF:
1125        <screen>
1126         @attr 1=1016 @attr 2=3 @attr 4=1 @attr 3=3 @attr 6=1 "computer"
1127        </screen>
1128        by rules <literal>qualifier.srw.serverChoice</literal>,
1129        <literal>relation.scr</literal>, <literal>structure.*</literal>,
1130        <literal>position.any</literal>.
1131       </para>
1132       <para>
1133        CQL query
1134        <screen>
1135         computer^
1136        </screen>
1137        is rejected, since <literal>position.right</literal> is
1138        undefined.
1139       </para>
1140       <para>
1141        CQL query
1142        <screen>
1143         >my = "http://www.loc.gov/zing/cql/dc-indexes/v1.0/" my.title = x
1144        </screen>
1145        is converted to
1146        <screen>
1147         @attr 1=4 @attr 2=3 @attr 4=1 @attr 3=3 @attr 6=1 "x"
1148        </screen>
1149       </para>
1150      </example>
1151     </sect3>
1152     <sect3 id="tools.cql.xcql"><title>CQL to XCQL conversion</title>
1153      <para>
1154       Conversion from CQL to XCQL is trivial and does not
1155       require a mapping to be defined.
1156       There three functions to choose from depending on the
1157       way you wish to store the resulting output (XML buffer
1158       containing XCQL).
1159       <synopsis>
1160 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
1161 void cql_to_xml(struct cql_node *cn, 
1162                 void (*pr)(const char *buf, void *client_data),
1163                 void *client_data);
1164 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
1165       </synopsis>
1166       Function <function>cql_to_xml_buf</function> converts
1167       to XCQL and stores result in a user supplied buffer of a given
1168       max size.
1169      </para>
1170      <para>
1171       <function>cql_to_xml</function> writes the result in
1172       a user defined output stream.
1173       <function>cql_to_xml_stdio</function> writes to a
1174       a file.
1175      </para>
1176     </sect3>
1177    </sect2>
1178   </sect1>
1179   <sect1 id="tools.oid"><title>Object Identifiers</title>
1180
1181    <para>
1182     The basic YAZ representation of an OID is an array of integers,
1183     terminated with the value -1. The &odr; module provides two
1184     utility-functions to create and copy this type of data elements:
1185    </para>
1186
1187    <screen>
1188     Odr_oid *odr_getoidbystr(ODR o, char *str);
1189    </screen>
1190
1191    <para>
1192     Creates an OID based on a string-based representation using dots (.)
1193     to separate elements in the OID.
1194    </para>
1195
1196    <screen>
1197     Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
1198    </screen>
1199
1200    <para>
1201     Creates a copy of the OID referenced by the <emphasis>o</emphasis>
1202     parameter.
1203     Both functions take an &odr; stream as parameter. This stream is used to
1204     allocate memory for the data elements, which is released on a
1205     subsequent call to <function>odr_reset()</function> on that stream.
1206    </para>
1207
1208    <para>
1209     The OID module provides a higher-level representation of the
1210     family of object identifiers which describe the Z39.50 protocol and its
1211     related objects. The definition of the module interface is given in
1212     the <filename>oid.h</filename> file.
1213    </para>
1214
1215    <para>
1216     The interface is mainly based on the <literal>oident</literal> structure.
1217     The definition of this structure looks like this:
1218    </para>
1219
1220    <screen>
1221 typedef struct oident
1222 {
1223     oid_proto proto;
1224     oid_class oclass;
1225     oid_value value;
1226     int oidsuffix[OID_SIZE];
1227     char *desc;
1228 } oident;
1229    </screen>
1230
1231    <para>
1232     The proto field takes one of the values
1233    </para>
1234
1235    <screen>
1236     PROTO_Z3950
1237     PROTO_SR
1238    </screen>
1239
1240    <para>
1241     If you don't care about talking to SR-based implementations (few
1242     exist, and they may become fewer still if and when the ISO SR and ANSI
1243     Z39.50 documents are merged into a single standard), you can ignore
1244     this field on incoming packages, and always set it to PROTO_Z3950
1245     for outgoing packages.
1246    </para>
1247    <para>
1248
1249     The oclass field takes one of the values
1250    </para>
1251
1252    <screen>
1253     CLASS_APPCTX
1254     CLASS_ABSYN
1255     CLASS_ATTSET
1256     CLASS_TRANSYN
1257     CLASS_DIAGSET
1258     CLASS_RECSYN
1259     CLASS_RESFORM
1260     CLASS_ACCFORM
1261     CLASS_EXTSERV
1262     CLASS_USERINFO
1263     CLASS_ELEMSPEC
1264     CLASS_VARSET
1265     CLASS_SCHEMA
1266     CLASS_TAGSET
1267     CLASS_GENERAL
1268    </screen>
1269
1270    <para>
1271     corresponding to the OID classes defined by the Z39.50 standard.
1272
1273     Finally, the value field takes one of the values
1274    </para>
1275
1276    <screen>
1277     VAL_APDU
1278     VAL_BER
1279     VAL_BASIC_CTX
1280     VAL_BIB1
1281     VAL_EXP1
1282     VAL_EXT1
1283     VAL_CCL1
1284     VAL_GILS
1285     VAL_WAIS
1286     VAL_STAS
1287     VAL_DIAG1
1288     VAL_ISO2709
1289     VAL_UNIMARC
1290     VAL_INTERMARC
1291     VAL_CCF
1292     VAL_USMARC
1293     VAL_UKMARC
1294     VAL_NORMARC
1295     VAL_LIBRISMARC
1296     VAL_DANMARC
1297     VAL_FINMARC
1298     VAL_MAB
1299     VAL_CANMARC
1300     VAL_SBN
1301     VAL_PICAMARC
1302     VAL_AUSMARC
1303     VAL_IBERMARC
1304     VAL_EXPLAIN
1305     VAL_SUTRS
1306     VAL_OPAC
1307     VAL_SUMMARY
1308     VAL_GRS0
1309     VAL_GRS1
1310     VAL_EXTENDED
1311     VAL_RESOURCE1
1312     VAL_RESOURCE2
1313     VAL_PROMPT1
1314     VAL_DES1
1315     VAL_KRB1
1316     VAL_PRESSET
1317     VAL_PQUERY
1318     VAL_PCQUERY
1319     VAL_ITEMORDER
1320     VAL_DBUPDATE
1321     VAL_EXPORTSPEC
1322     VAL_EXPORTINV
1323     VAL_NONE
1324     VAL_SETM
1325     VAL_SETG
1326     VAL_VAR1
1327     VAL_ESPEC1
1328    </screen>
1329
1330    <para>
1331     again, corresponding to the specific OIDs defined by the standard.
1332    </para>
1333
1334    <para>
1335     The desc field contains a brief, mnemonic name for the OID in question.
1336    </para>
1337
1338    <para>
1339     The function
1340    </para>
1341
1342    <screen>
1343     struct oident *oid_getentbyoid(int *o);
1344    </screen>
1345
1346    <para>
1347     takes as argument an OID, and returns a pointer to a static area
1348     containing an <literal>oident</literal> structure. You typically use
1349     this function when you receive a PDU containing an OID, and you wish
1350     to branch out depending on the specific OID value.
1351    </para>
1352
1353    <para>
1354     The function
1355    </para>
1356
1357    <screen>
1358     int *oid_ent_to_oid(struct oident *ent, int *dst);
1359    </screen>
1360
1361    <para>
1362     Takes as argument an <literal>oident</literal> structure - in which
1363     the <literal>proto</literal>, <literal>oclass</literal>/, and
1364     <literal>value</literal> fields are assumed to be set correctly -
1365     and returns a pointer to a the buffer as given by <literal>dst</literal>
1366     containing the base
1367     representation of the corresponding OID. The function returns
1368     NULL and the array dst is unchanged if a mapping couldn't place.
1369     The array <literal>dst</literal> should be at least of size
1370     <literal>OID_SIZE</literal>.
1371    </para>
1372    <para>
1373
1374     The <function>oid_ent_to_oid()</function> function can be used whenever
1375     you need to prepare a PDU containing one or more OIDs. The separation of
1376     the <literal>protocol</literal> element from the remainder of the
1377     OID-description makes it simple to write applications that can
1378     communicate with either Z39.50 or OSI SR-based applications.
1379    </para>
1380
1381    <para>
1382     The function
1383    </para>
1384
1385    <screen>
1386     oid_value oid_getvalbyname(const char *name);
1387    </screen>
1388
1389    <para>
1390     takes as argument a mnemonic OID name, and returns the
1391     <literal>/value</literal> field of the first entry in the database that 
1392     contains the given name in its <literal>desc</literal> field.
1393    </para>
1394
1395    <para>
1396     Finally, the module provides the following utility functions, whose
1397     meaning should be obvious:
1398    </para>
1399
1400    <screen>
1401     void oid_oidcpy(int *t, int *s);
1402     void oid_oidcat(int *t, int *s);
1403     int oid_oidcmp(int *o1, int *o2);
1404     int oid_oidlen(int *o);
1405    </screen>
1406
1407    <note>
1408     <para>
1409      The OID module has been criticized - and perhaps rightly so
1410      - for needlessly abstracting the
1411      representation of OIDs. Other toolkits use a simple
1412      string-representation of OIDs with good results. In practice, we have
1413      found the interface comfortable and quick to work with, and it is a
1414      simple matter (for what it's worth) to create applications compatible
1415      with both ISO SR and Z39.50. Finally, the use of the
1416      <literal>/oident</literal> database is by no means mandatory.
1417      You can easily create your own system for representing OIDs, as long
1418      as it is compatible with the low-level integer-array representation
1419      of the ODR module.
1420     </para>
1421    </note>
1422
1423   </sect1>
1424
1425   <sect1 id="tools.nmem"><title>Nibble Memory</title>
1426
1427    <para>
1428     Sometimes when you need to allocate and construct a large,
1429     interconnected complex of structures, it can be a bit of a pain to
1430     release the associated memory again. For the structures describing the
1431     Z39.50 PDUs and related structures, it is convenient to use the
1432     memory-management system of the &odr; subsystem (see
1433     <link linkend="odr-use">Using ODR</link>). However, in some circumstances
1434     where you might otherwise benefit from using a simple nibble memory
1435     management system, it may be impractical to use
1436     <function>odr_malloc()</function> and <function>odr_reset()</function>.
1437     For this purpose, the memory manager which also supports the &odr;
1438     streams is made available in the NMEM module. The external interface
1439     to this module is given in the <filename>nmem.h</filename> file.
1440    </para>
1441
1442    <para>
1443     The following prototypes are given:
1444    </para>
1445
1446    <screen>
1447     NMEM nmem_create(void);
1448     void nmem_destroy(NMEM n);
1449     void *nmem_malloc(NMEM n, int size);
1450     void nmem_reset(NMEM n);
1451     int nmem_total(NMEM n);
1452     void nmem_init(void);
1453     void nmem_exit(void);
1454    </screen>
1455
1456    <para>
1457     The <function>nmem_create()</function> function returns a pointer to a
1458     memory control handle, which can be released again by
1459     <function>nmem_destroy()</function> when no longer needed.
1460     The function <function>nmem_malloc()</function> allocates a block of
1461     memory of the requested size. A call to <function>nmem_reset()</function>
1462     or <function>nmem_destroy()</function> will release all memory allocated
1463     on the handle since it was created (or since the last call to
1464     <function>nmem_reset()</function>. The function
1465     <function>nmem_total()</function> returns the number of bytes currently
1466     allocated on the handle.
1467    </para>
1468
1469    <para>
1470     The nibble memory pool is shared amongst threads. POSIX
1471     mutex'es and WIN32 Critical sections are introduced to keep the
1472     module thread safe. Function <function>nmem_init()</function>
1473     initializes the nibble memory library and it is called automatically
1474     the first time the <literal>YAZ.DLL</literal> is loaded. &yaz; uses
1475     function <function>DllMain</function> to achieve this. You should
1476     <emphasis>not</emphasis> call <function>nmem_init</function> or
1477     <function>nmem_exit</function> unless you're absolute sure what
1478     you're doing. Note that in previous &yaz; versions you'd have to call
1479     <function>nmem_init</function> yourself. 
1480    </para>
1481
1482   </sect1>
1483  </chapter>
1484  
1485  <!-- Keep this comment at the end of the file
1486  Local variables:
1487  mode: sgml
1488  sgml-omittag:t
1489  sgml-shorttag:t
1490  sgml-minimize-attributes:nil
1491  sgml-always-quote-attributes:t
1492  sgml-indent-step:1
1493  sgml-indent-data:t
1494  sgml-parent-document: "yaz.xml"
1495  sgml-local-catalogs: nil
1496  sgml-namecase-general:t
1497  End:
1498  -->