Remove arbitrary division between xcb_types and xproto by merging
[free-sw/xcb/libxcb] / src / c-client.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 Copyright (C) 2004 Josh Triplett.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 -->
27 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
28                version="1.0"
29                xmlns:e="http://exslt.org/common">
30   
31   <xsl:output method="text" />
32
33   <xsl:strip-space elements="*" />
34
35   <!-- "header" or "source" -->
36   <xsl:param name="mode" />
37
38   <!-- Path to the core protocol descriptions. -->
39   <xsl:param name="base-path" />
40
41   <!-- Path to the extension protocol descriptions. -->
42   <xsl:param name="extension-path" select="$base-path" />
43
44   <xsl:variable name="h" select="$mode = 'header'" />
45   <xsl:variable name="c" select="$mode = 'source'" />
46   
47   <!-- String used to indent lines of code. -->
48   <xsl:variable name="indent-string" select="'    '" />
49
50   <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
51   <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
52
53   <xsl:variable name="header" select="/xcb/@header" />
54   <xsl:variable name="ucase-header"
55                 select="translate($header,$lcase,$ucase)" />
56
57   <xsl:variable name="ext" select="/xcb/@extension-name" />
58
59   <!-- Other protocol descriptions to search for types in, after checking the
60        current protocol description. -->
61   <xsl:variable name="search-path-rtf">
62     <xsl:for-each select="/xcb/import">
63       <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
64     </xsl:for-each>
65     <xsl:if test="not($header='xproto')">
66       <path><xsl:value-of select="concat($base-path, 'xproto.xml')" /></path>
67     </xsl:if>
68   </xsl:variable>
69   <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
70
71   <xsl:variable name="root" select="/" />
72   
73   <!-- First pass: Store everything in a variable. -->
74   <xsl:variable name="pass1-rtf">
75     <xsl:apply-templates select="/" mode="pass1" />
76   </xsl:variable>
77   <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
78   
79   <xsl:template match="xcb" mode="pass1">
80     <xcb>
81       <xsl:copy-of select="@*" />
82       <xsl:if test="$ext">
83         <constant type="XCBExtension" name="XCB{$ext}Id">
84           <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
85         </constant>
86         <function type="const XCBQueryExtensionRep *" name="XCB{$ext}Init">
87           <field type="XCBConnection *" name="c" />
88           <l>return XCBGetExtensionData(c, &amp;XCB<!--
89           --><xsl:value-of select="$ext" />Id);</l>
90         </function>
91       </xsl:if>
92       <xsl:apply-templates mode="pass1" />
93     </xcb>
94   </xsl:template>
95
96   <!-- Modify names that conflict with C++ keywords by prefixing them with an
97        underscore.  If the name parameter is not specified, it defaults to the
98        value of the name attribute on the context node. -->
99   <xsl:template name="canonical-var-name">
100     <xsl:param name="name" select="@name" />
101     <xsl:if test="$name='new' or $name='delete'
102                   or $name='class' or $name='operator'">
103       <xsl:text>_</xsl:text>
104     </xsl:if>
105     <xsl:value-of select="$name" />
106   </xsl:template>
107
108   <!-- List of core types, for use in canonical-type-name. -->
109   <xsl:variable name="core-types-rtf">
110     <type name="BOOL" />
111     <type name="BYTE" />
112     <type name="CARD8" />
113     <type name="CARD16" />
114     <type name="CARD32" />
115     <type name="INT8" />
116     <type name="INT16" />
117     <type name="INT32" />
118
119     <type name="char" />
120     <type name="void" />
121     <type name="float" />
122     <type name="double" />
123     <type name="XID" />
124   </xsl:variable>
125   <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
126
127   <!--
128     Output the canonical name for a type.  This will be
129     XCB{extension-containing-Type-if-any}Type, wherever the type is found in
130     the search path, or just Type if not found.  If the type parameter is not
131     specified, it defaults to the value of the type attribute on the context
132     node.
133   -->
134   <xsl:template name="canonical-type-name">
135     <xsl:param name="type" select="string(@type)" />
136
137     <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
138     <xsl:variable name="namespace" select="substring-before($type, ':')" />
139     <xsl:variable name="unqualified-type">
140       <xsl:choose>
141         <xsl:when test="$is-unqualified">
142           <xsl:value-of select="$type" />
143         </xsl:when>
144         <xsl:otherwise>
145           <xsl:value-of select="substring-after($type, ':')" />
146         </xsl:otherwise>
147       </xsl:choose>
148     </xsl:variable>
149
150     <xsl:choose>
151       <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
152         <xsl:value-of select="$type" />
153       </xsl:when>
154       <xsl:otherwise>
155         <xsl:variable name="type-definitions"
156                       select="(/xcb|document($search-path)/xcb
157                               )[$is-unqualified or @header=$namespace]
158                                /*[((self::struct or self::union
159                                     or self::xidtype or self::enum
160                                     or self::event or self::eventcopy
161                                     or self::error or self::errorcopy)
162                                    and @name=$unqualified-type)
163                                   or (self::typedef
164                                       and @newname=$unqualified-type)]" />
165         <xsl:choose>
166           <xsl:when test="count($type-definitions) = 1">
167             <xsl:for-each select="$type-definitions">
168               <xsl:text>XCB</xsl:text>
169               <xsl:value-of select="concat(/xcb/@extension-name,
170                                            $unqualified-type)" />
171             </xsl:for-each>
172           </xsl:when>
173           <xsl:when test="count($type-definitions) > 1">
174             <xsl:message terminate="yes">
175               <xsl:text>Multiple definitions of type "</xsl:text>
176               <xsl:value-of select="$type" />
177               <xsl:text>" found.</xsl:text>
178               <xsl:if test="$is-unqualified">
179                 <xsl:for-each select="$type-definitions">
180                   <xsl:text>
181     </xsl:text>
182                   <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
183                 </xsl:for-each>
184               </xsl:if>
185             </xsl:message>
186           </xsl:when>
187           <xsl:otherwise>
188             <xsl:message terminate="yes">
189               <xsl:text>No definitions of type "</xsl:text>
190               <xsl:value-of select="$type" />
191               <xsl:text>" found</xsl:text>
192               <xsl:if test="$is-unqualified">
193                 <xsl:text>, and it is not a known core type</xsl:text>
194               </xsl:if>
195               <xsl:text>.</xsl:text>
196             </xsl:message>
197           </xsl:otherwise>
198         </xsl:choose>   
199       </xsl:otherwise>
200     </xsl:choose>
201   </xsl:template>
202   
203   <!-- Helper template for requests, that outputs the cookie type.  The
204        context node must be the request. -->
205   <xsl:template name="cookie-type">
206     <xsl:text>XCB</xsl:text>
207     <xsl:choose>
208       <xsl:when test="reply">
209         <xsl:value-of select="concat($ext, @name)" />
210       </xsl:when>
211       <xsl:otherwise>
212         <xsl:text>Void</xsl:text>
213       </xsl:otherwise>
214     </xsl:choose>
215     <xsl:text>Cookie</xsl:text>
216   </xsl:template>
217
218   <xsl:template match="request" mode="pass1">
219     <xsl:if test="reply">
220       <struct name="XCB{$ext}{@name}Cookie">
221         <field type="unsigned int" name="sequence" />
222       </struct>
223     </xsl:if>
224     <struct name="XCB{$ext}{@name}Req">
225       <field type="CARD8" name="major_opcode" no-assign="true" />
226       <xsl:if test="$ext">
227         <field type="CARD8" name="minor_opcode" no-assign="true" />
228       </xsl:if>
229       <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
230       <middle>
231         <field type="CARD16" name="length" no-assign="true" />
232       </middle>
233     </struct>
234     <function name="XCB{$ext}{@name}">
235       <xsl:attribute name="type">
236         <xsl:call-template name="cookie-type" />
237       </xsl:attribute>
238       <field type="XCBConnection *" name="c" />
239       <xsl:apply-templates select="*[not(self::reply)]" mode="param" />
240       <do-request ref="XCB{$ext}{@name}Req" opcode="{@opcode}">
241         <xsl:if test="reply">
242           <xsl:attribute name="has-reply">true</xsl:attribute>
243         </xsl:if>
244       </do-request>
245     </function>
246     <xsl:if test="reply">
247       <struct name="XCB{$ext}{@name}Rep">
248         <field type="BYTE" name="response_type" />
249         <xsl:apply-templates select="reply/*" mode="field" />
250         <middle>
251           <field type="CARD16" name="sequence" />
252           <field type="CARD32" name="length" />
253         </middle>
254       </struct>
255       <iterator-functions ref="XCB{$ext}{@name}" kind="Rep" />
256       <function type="XCB{$ext}{@name}Rep *" name="XCB{$ext}{@name}Reply">
257         <field type="XCBConnection *" name="c" />
258         <field name="cookie">
259           <xsl:attribute name="type">
260             <xsl:call-template name="cookie-type" />
261           </xsl:attribute>
262         </field>
263         <field type="XCBGenericError **" name="e" />
264         <l>return (XCB<xsl:value-of select="concat($ext, @name)" />Rep *)<!--
265         --> XCBWaitForReply(c, cookie.sequence, e);</l>
266       </function>
267     </xsl:if>
268   </xsl:template>
269
270   <xsl:template match="xidtype" mode="pass1">
271     <struct name="XCB{$ext}{@name}">
272       <field type="CARD32" name="xid" />
273     </struct>
274     <iterator ref="XCB{$ext}{@name}" />
275     <iterator-functions ref="XCB{$ext}{@name}" />
276     <function type="XCB{$ext}{@name}" name="XCB{$ext}{@name}New">
277       <field type="XCBConnection *" name="c" />
278       <l>XCB<xsl:value-of select="concat($ext, @name)" /> ret;</l>
279       <l>ret.xid = XCBGenerateID(c);</l>
280       <l>return ret;</l>
281     </function>
282   </xsl:template>
283
284   <xsl:template match="struct|union" mode="pass1">
285     <struct name="XCB{$ext}{@name}">
286       <xsl:if test="self::union">
287         <xsl:attribute name="kind">union</xsl:attribute>
288       </xsl:if>
289       <xsl:apply-templates select="*" mode="field" />
290     </struct>
291     <iterator ref="XCB{$ext}{@name}" />
292     <iterator-functions ref="XCB{$ext}{@name}" />
293   </xsl:template>
294
295   <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
296     <xsl:variable name="suffix">
297       <xsl:choose>
298         <xsl:when test="self::event|self::eventcopy">
299           <xsl:text>Event</xsl:text>
300         </xsl:when>
301         <xsl:when test="self::error|self::errorcopy">
302           <xsl:text>Error</xsl:text>
303         </xsl:when>
304       </xsl:choose>
305     </xsl:variable>
306     <constant type="number" name="XCB{$ext}{@name}" value="{@number}" />
307     <xsl:choose>
308       <xsl:when test="self::event|self::error">
309         <struct name="XCB{$ext}{@name}{$suffix}">
310           <field type="BYTE" name="response_type" />
311           <xsl:if test="self::error">
312             <field type="BYTE" name="error_code" />
313           </xsl:if>
314           <xsl:apply-templates select="*" mode="field" />
315           <xsl:if test="not(self::event and boolean(@no-sequence-number))">
316             <middle>
317               <field type="CARD16" name="sequence" />
318             </middle>
319           </xsl:if>
320         </struct>
321       </xsl:when>
322       <xsl:when test="self::eventcopy|self::errorcopy">
323         <typedef newname="XCB{$ext}{@name}{$suffix}">
324           <xsl:attribute name="oldname">
325             <xsl:call-template name="canonical-type-name">
326               <xsl:with-param name="type" select="@ref" />
327             </xsl:call-template>
328             <xsl:value-of select="$suffix" />
329           </xsl:attribute>
330         </typedef>
331       </xsl:when>
332     </xsl:choose>
333   </xsl:template>
334
335   <xsl:template match="typedef" mode="pass1">
336     <typedef>
337       <xsl:attribute name="oldname">
338         <xsl:call-template name="canonical-type-name">
339           <xsl:with-param name="type" select="@oldname" />
340         </xsl:call-template>
341       </xsl:attribute>
342       <xsl:attribute name="newname">
343         <xsl:call-template name="canonical-type-name">
344           <xsl:with-param name="type" select="@newname" />
345         </xsl:call-template>
346       </xsl:attribute>
347     </typedef>
348     <iterator ref="XCB{$ext}{@newname}" />
349     <iterator-functions ref="XCB{$ext}{@newname}" />
350   </xsl:template>
351
352   <xsl:template match="enum" mode="pass1">
353     <enum name="XCB{$ext}{@name}">
354       <xsl:for-each select="item">
355         <item name="XCB{$ext}{../@name}{@name}">
356           <xsl:copy-of select="*" />
357         </item>
358       </xsl:for-each>
359     </enum>
360   </xsl:template>
361
362   <!--
363     Templates for processing fields.
364   -->
365
366   <xsl:template match="pad" mode="field">
367     <xsl:copy-of select="." />
368   </xsl:template>
369   
370   <xsl:template match="field|exprfield" mode="field">
371     <xsl:copy>
372       <xsl:attribute name="type">
373         <xsl:call-template name="canonical-type-name" />
374       </xsl:attribute>
375       <xsl:attribute name="name">
376         <xsl:call-template name="canonical-var-name" />
377       </xsl:attribute>
378       <xsl:copy-of select="*" />
379     </xsl:copy>
380   </xsl:template>
381
382   <xsl:template match="list" mode="field">
383     <xsl:variable name="type"><!--
384       --><xsl:call-template name="canonical-type-name" /><!--
385     --></xsl:variable>
386     <list type="{$type}">
387       <xsl:attribute name="name">
388         <xsl:call-template name="canonical-var-name" />
389       </xsl:attribute>
390       <xsl:if test="not(parent::request) and node()
391                     and not(.//*[not(self::value or self::op)])">
392         <xsl:attribute name="fixed">true</xsl:attribute>
393       </xsl:if>
394       <!-- Handle lists with no length expressions. -->
395       <xsl:if test="not(node())">
396         <xsl:choose>
397           <!-- In a request, refer to an implicit localparam for length. -->
398           <xsl:when test="parent::request">
399             <fieldref>
400               <xsl:value-of select="concat(@name, '_len')" />
401             </fieldref>
402           </xsl:when>
403           <!-- In a reply, use the length of the reply to determine the length
404                of the list. -->
405           <xsl:when test="parent::reply">
406             <op op="/">
407               <op op="&lt;&lt;">
408                 <fieldref>length</fieldref>
409                 <value>2</value>
410               </op>
411               <function-call name="sizeof">
412                 <param><xsl:value-of select="$type" /></param>
413               </function-call>
414             </op>
415           </xsl:when>
416           <!-- Other cases generate an error. -->
417           <xsl:otherwise>
418             <xsl:message terminate="yes"><!--
419               -->Encountered a list with no length expresssion outside a<!--
420               --> request or reply.<!--
421             --></xsl:message>
422           </xsl:otherwise>
423         </xsl:choose>
424       </xsl:if>
425       <xsl:copy-of select="*" />
426     </list>
427   </xsl:template>
428
429   <xsl:template match="valueparam" mode="field">
430     <field>
431       <xsl:attribute name="type">
432         <xsl:call-template name="canonical-type-name">
433           <xsl:with-param name="type" select="@value-mask-type" />
434         </xsl:call-template>
435       </xsl:attribute>
436       <xsl:attribute name="name">
437         <xsl:call-template name="canonical-var-name">
438           <xsl:with-param name="name" select="@value-mask-name" />
439         </xsl:call-template>
440       </xsl:attribute>
441     </field>
442     <list type="CARD32">
443       <xsl:attribute name="name">
444         <xsl:call-template name="canonical-var-name">
445           <xsl:with-param name="name" select="@value-list-name" />
446         </xsl:call-template>
447       </xsl:attribute>
448       <function-call name="XCBPopcount">
449         <param>
450           <fieldref>
451             <xsl:call-template name="canonical-var-name">
452               <xsl:with-param name="name" select="@value-mask-name" />
453             </xsl:call-template>
454           </fieldref>
455         </param>
456       </function-call>
457     </list>
458   </xsl:template>
459
460   <xsl:template match="field|localfield" mode="param">
461     <field>
462       <xsl:attribute name="type">
463         <xsl:call-template name="canonical-type-name" />
464       </xsl:attribute>
465       <xsl:attribute name="name">
466         <xsl:call-template name="canonical-var-name" />
467       </xsl:attribute>
468     </field>
469   </xsl:template>
470
471   <xsl:template match="list" mode="param">
472     <!-- If no length expression is provided, use a CARD32 localfield. -->
473     <xsl:if test="not(node())">
474       <field type="CARD32" name="{@name}_len" />
475     </xsl:if>
476     <field>
477       <xsl:attribute name="type">
478         <xsl:text>const </xsl:text>
479         <xsl:call-template name="canonical-type-name" />
480         <xsl:text> *</xsl:text>
481       </xsl:attribute>
482       <xsl:attribute name="name">
483         <xsl:call-template name="canonical-var-name" />
484       </xsl:attribute>
485     </field>
486   </xsl:template>
487
488   <xsl:template match="valueparam" mode="param">
489     <field>
490       <xsl:attribute name="type">
491         <xsl:call-template name="canonical-type-name">
492           <xsl:with-param name="type" select="@value-mask-type" />
493         </xsl:call-template>
494       </xsl:attribute>
495       <xsl:attribute name="name">
496         <xsl:call-template name="canonical-var-name">
497           <xsl:with-param name="name" select="@value-mask-name" />
498         </xsl:call-template>
499       </xsl:attribute>
500     </field>
501     <field type="const CARD32 *">
502       <xsl:attribute name="name">
503         <xsl:call-template name="canonical-var-name">
504           <xsl:with-param name="name" select="@value-list-name" />
505         </xsl:call-template>
506       </xsl:attribute>
507     </field>
508   </xsl:template>
509
510   <!-- Second pass: Process the variable. -->
511   <xsl:variable name="result-rtf">
512     <xsl:apply-templates select="$pass1/*" mode="pass2" />
513   </xsl:variable>
514   <xsl:variable name="result" select="e:node-set($result-rtf)" />
515
516   <xsl:template match="xcb" mode="pass2">
517     <xcb>
518       <xsl:copy-of select="@*" />
519       <xsl:apply-templates mode="pass2"
520                            select="constant|enum|struct|typedef|iterator" />
521       <xsl:apply-templates mode="pass2"
522                            select="function|iterator-functions" />
523     </xcb>
524   </xsl:template>
525
526   <!-- Generic rules for nodes that don't need further processing: copy node
527        with attributes, and recursively process the child nodes. -->
528   <xsl:template match="*" mode="pass2">
529     <xsl:copy>
530       <xsl:copy-of select="@*" />
531       <xsl:apply-templates mode="pass2" />
532     </xsl:copy>
533   </xsl:template>
534
535   <xsl:template match="struct" mode="pass2">
536     <xsl:if test="@kind='union' and list[not(@fixed)]">
537       <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
538     </xsl:if>
539     <struct name="{@name}">
540       <xsl:if test="@kind">
541         <xsl:attribute name="kind">
542           <xsl:value-of select="@kind" />
543         </xsl:attribute>
544       </xsl:if>
545       <!-- FIXME: This should go by size, not number of fields. -->
546       <xsl:copy-of select="node()[not(self::middle)
547                    and position() &lt; 3]" />
548       <xsl:if test="middle and (count(*[not(self::middle)]) &lt; 2)">
549         <pad bytes="{2 - count(*[not(self::middle)])}" />
550       </xsl:if>
551       <xsl:copy-of select="middle/*" />
552       <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
553     </struct>
554   </xsl:template>
555
556   <xsl:template match="do-request" mode="pass2">
557     <xsl:variable name="struct"
558                   select="$pass1/xcb/struct[@name=current()/@ref]" />
559
560     <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
561
562     <l>static const XCBProtocolRequest xcb_req = {</l>
563     <indent>
564       <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
565       <l>/* ext */ <xsl:choose>
566                      <xsl:when test="$ext">
567                        <xsl:text>&amp;XCB</xsl:text>
568                        <xsl:value-of select="$ext" />
569                        <xsl:text>Id</xsl:text>
570                      </xsl:when>
571                      <xsl:otherwise>0</xsl:otherwise>
572                    </xsl:choose>,</l>
573       <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
574       <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
575     </indent>
576     <l>};</l>
577
578     <l />
579     <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
580     <l><xsl:value-of select="../@type" /> xcb_ret;</l>
581     <l><xsl:value-of select="@ref" /> xcb_out;</l>
582
583     <l />
584     <xsl:apply-templates select="$struct//*[(self::field or self::exprfield)
585                                             and not(boolean(@no-assign))]"
586                          mode="assign" />
587
588     <l />
589     <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
590     <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
591     <l>xcb_parts[3].iov_base = 0;</l>
592     <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
593
594     <xsl:for-each select="$struct/list">
595       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
596       --><xsl:value-of select="@name" />;</l>
597       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
598       --><xsl:apply-templates mode="output-expression" /><!--
599       --><xsl:if test="not(@type = 'void')">
600         <xsl:text> * sizeof(</xsl:text>
601         <xsl:value-of select="@type" />
602         <xsl:text>)</xsl:text>
603       </xsl:if>;</l>
604       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
605       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_len = -xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len &amp; 3;</l>
606     </xsl:for-each>
607
608     <l>xcb_ret.sequence = XCBSendRequest(c, <!--
609     --><xsl:choose>
610          <xsl:when test="@has-reply">XCB_REQUEST_CHECKED</xsl:when>
611          <xsl:otherwise>0</xsl:otherwise>
612        </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
613     <l>return xcb_ret;</l>
614   </xsl:template>
615
616   <xsl:template match="field" mode="assign">
617     <l>
618       <xsl:text>xcb_out.</xsl:text>
619       <xsl:value-of select="@name" />
620       <xsl:text> = </xsl:text>
621       <xsl:value-of select="@name" />
622       <xsl:text>;</xsl:text>
623     </l>
624   </xsl:template>
625
626   <xsl:template match="exprfield" mode="assign">
627     <l>
628       <xsl:text>xcb_out.</xsl:text>
629       <xsl:value-of select="@name" />
630       <xsl:text> = </xsl:text>
631       <xsl:apply-templates mode="output-expression" />
632       <xsl:text>;</xsl:text>
633     </l>
634   </xsl:template>
635
636   <xsl:template match="iterator" mode="pass2">
637     <struct name="{@ref}Iter">
638       <field type="{@ref} *" name="data" />
639       <field type="int" name="rem" />
640       <field type="int" name="index" />
641     </struct>
642   </xsl:template>
643
644   <!-- Change a_name_like_this to ANameLikeThis.  If the parameter name is not
645        given, it defaults to the name attribute of the context node. -->
646   <xsl:template name="capitalize">
647     <xsl:param name="name" select="string(@name)" />
648     <xsl:if test="$name">
649       <xsl:value-of select="translate(substring($name,1,1), $lcase, $ucase)" />
650       <xsl:choose>
651         <xsl:when test="contains($name, '_')">
652           <xsl:value-of select="substring(substring-before($name, '_'), 2)" />
653           <xsl:call-template name="capitalize">
654             <xsl:with-param name="name" select="substring-after($name, '_')" />
655           </xsl:call-template>
656         </xsl:when>
657         <xsl:otherwise>
658           <xsl:value-of select="substring($name, 2)" />
659         </xsl:otherwise>
660       </xsl:choose>
661     </xsl:if>
662   </xsl:template>
663
664   <xsl:template match="iterator-functions" mode="pass2">
665     <xsl:variable name="ref" select="@ref" />
666     <xsl:variable name="kind" select="@kind" />
667     <xsl:variable name="struct"
668                   select="$pass1/xcb/struct[@name=concat($ref,$kind)]" />
669     <xsl:variable name="nextfields-rtf">
670       <nextfield>R + 1</nextfield>
671       <xsl:for-each select="$struct/list[not(@fixed)]">
672         <xsl:choose>
673           <xsl:when test="substring(@type, 1, 3) = 'XCB'">
674             <nextfield><xsl:value-of select="@type" />End(<!--
675             --><xsl:value-of select="$ref" /><!--
676             --><xsl:call-template name="capitalize" />Iter(R))</nextfield>
677           </xsl:when>
678           <xsl:otherwise>
679             <nextfield><xsl:value-of select="$ref" /><!--
680             --><xsl:call-template name="capitalize" />End(R)</nextfield>
681           </xsl:otherwise>
682         </xsl:choose>
683       </xsl:for-each>
684     </xsl:variable>
685     <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
686     <xsl:for-each select="$struct/list[not(@fixed)]">
687       <xsl:variable name="number"
688                     select="1+count(preceding-sibling::list[not(@fixed)])" />
689       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
690       <xsl:variable name="is-first"
691                     select="not(preceding-sibling::list[not(@fixed)])" />
692       <xsl:variable name="field-name"><!--
693         --><xsl:call-template name="capitalize" /><!--
694       --></xsl:variable>
695       <xsl:variable name="is-variable"
696                     select="$pass1/xcb/struct[@name=current()/@type]/list
697                             or document($search-path)/xcb
698                                /struct[concat('XCB',
699                                               ancestor::xcb/@extension-name,
700                                               @name) = current()/@type]
701                                /*[self::valueparam or self::list]" />
702       <xsl:if test="not($is-variable)">
703         <function type="{@type} *" name="{$ref}{$field-name}">
704           <field type="const {$ref}{$kind} *" name="R" />
705           <xsl:choose>
706             <xsl:when test="$is-first">
707               <l>return (<xsl:value-of select="@type" /> *) <!--
708               -->(<xsl:value-of select="$nextfield" />);</l>
709             </xsl:when>
710             <xsl:otherwise>
711               <l>XCBGenericIter prev = <!--
712               --><xsl:value-of select="$nextfield" />;</l>
713               <l>return (<xsl:value-of select="@type" /> *) <!--
714               -->((char *) prev.data + XCB_TYPE_PAD(<!--
715               --><xsl:value-of select="@type" />, prev.index));</l>
716             </xsl:otherwise>
717           </xsl:choose>
718         </function>
719       </xsl:if>
720       <function type="int" name="{$ref}{$field-name}Length">
721         <field type="const {$ref}{$kind} *" name="R" />
722         <l>return <xsl:apply-templates mode="output-expression">
723                     <xsl:with-param name="field-prefix" select="'R->'" />
724                   </xsl:apply-templates>;</l>
725       </function>
726       <xsl:choose>
727         <xsl:when test="substring(@type, 1, 3) = 'XCB'">
728           <function type="{@type}Iter" name="{$ref}{$field-name}Iter">
729             <field type="const {$ref}{$kind} *" name="R" />
730             <l><xsl:value-of select="@type" />Iter i;</l>
731             <xsl:choose>
732               <xsl:when test="$is-first">
733                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
734                 -->(<xsl:value-of select="$nextfield" />);</l>
735               </xsl:when>
736               <xsl:otherwise>
737                 <l>XCBGenericIter prev = <!--
738                 --><xsl:value-of select="$nextfield" />;</l>
739                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
740                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
741                 --><xsl:value-of select="@type" />, prev.index));</l>
742               </xsl:otherwise>
743             </xsl:choose>
744             <l>i.rem = <xsl:apply-templates mode="output-expression">
745                          <xsl:with-param name="field-prefix" select="'R->'" />
746                        </xsl:apply-templates>;</l>
747             <l>i.index = (char *) i.data - (char *) R;</l>
748             <l>return i;</l>
749           </function>
750         </xsl:when>
751         <xsl:otherwise>
752           <xsl:variable name="cast">
753             <xsl:choose>
754               <xsl:when test="@type='void'">char</xsl:when>
755               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
756             </xsl:choose>
757           </xsl:variable>
758           <function type="XCBGenericIter" name="{$ref}{$field-name}End">
759             <field type="const {$ref}{$kind} *" name="R" />
760             <l>XCBGenericIter i;</l>
761             <xsl:choose>
762               <xsl:when test="$is-first">
763                 <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
764                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
765                 --><xsl:apply-templates mode="output-expression">
766                      <xsl:with-param name="field-prefix" select="'R->'" />
767                    </xsl:apply-templates>);</l>
768               </xsl:when>
769               <xsl:otherwise>
770                 <l>XCBGenericIter child = <!--
771                 --><xsl:value-of select="$nextfield" />;</l>
772                 <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
773                 -->child.data) + (<!--
774                 --><xsl:apply-templates mode="output-expression">
775                      <xsl:with-param name="field-prefix" select="'R->'" />
776                    </xsl:apply-templates>);</l>
777               </xsl:otherwise>
778             </xsl:choose>
779             <l>i.rem = 0;</l>
780             <l>i.index = (char *) i.data - (char *) R;</l>
781             <l>return i;</l>
782           </function>
783         </xsl:otherwise>
784       </xsl:choose>
785     </xsl:for-each>
786     <xsl:if test="not($kind)">
787       <function type="void" name="{$ref}Next">
788         <field type="{$ref}Iter *" name="i" />
789         <xsl:choose>
790           <xsl:when test="$struct/list[not(@fixed)]">
791             <l><xsl:value-of select="$ref" /> *R = i->data;</l>
792             <l>XCBGenericIter child = <!--
793             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
794             <l>--i->rem;</l>
795             <l>i->data = (<xsl:value-of select="$ref" /> *) child.data;</l>
796             <l>i->index = child.index;</l>
797           </xsl:when>
798           <xsl:otherwise>
799             <l>--i->rem;</l>
800             <l>++i->data;</l>
801             <l>i->index += sizeof(<xsl:value-of select="$ref" />);</l>
802           </xsl:otherwise>
803         </xsl:choose>
804       </function>
805       <function type="XCBGenericIter" name="{$ref}End">
806         <field type="{$ref}Iter" name="i" />
807         <l>XCBGenericIter ret;</l>
808         <xsl:choose>
809           <xsl:when test="$struct/list[not(@fixed)]">
810             <l>while(i.rem > 0)</l>
811             <indent>
812               <l><xsl:value-of select="$ref" />Next(&amp;i);</l>
813             </indent>
814             <l>ret.data = i.data;</l>
815             <l>ret.rem = i.rem;</l>
816             <l>ret.index = i.index;</l>
817           </xsl:when>
818           <xsl:otherwise>
819             <l>ret.data = i.data + i.rem;</l>
820             <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
821             <l>ret.rem = 0;</l>
822           </xsl:otherwise>
823         </xsl:choose>
824         <l>return ret;</l>
825       </function>
826     </xsl:if>
827   </xsl:template>
828
829   <!-- Output the results. -->
830   <xsl:template match="/">
831     <xsl:if test="not(function-available('e:node-set'))">
832       <xsl:message terminate="yes"><!--
833         -->Error: This stylesheet requires the EXSL node-set extension.<!--
834       --></xsl:message>
835     </xsl:if>
836
837     <xsl:if test="not($h) and not($c)">
838       <xsl:message terminate="yes"><!--
839         -->Error: Parameter "mode" must be "header" or "source".<!--
840       --></xsl:message>
841     </xsl:if>
842
843     <xsl:apply-templates select="$result/*" mode="output" />
844   </xsl:template>
845
846   <xsl:template match="xcb" mode="output">
847     <xsl:variable name="guard"><!--
848       -->__<xsl:value-of select="$ucase-header" />_H<!--
849     --></xsl:variable>
850
851 <xsl:text>/*
852  * This file generated automatically from </xsl:text>
853 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
854  * Edit at your peril.
855  */
856 </xsl:text>
857
858 <xsl:if test="$h"><xsl:text>
859 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
860 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
861 </xsl:text>
862 #include "xcb.h"
863 <xsl:for-each select="$root/xcb/import">
864 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
865 </xsl:text>
866 </xsl:for-each>
867 <xsl:text>
868 </xsl:text>
869 </xsl:if>
870
871 <xsl:if test="$c"><xsl:text>
872 #include &lt;assert.h&gt;
873 #include "xcbext.h"
874 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
875
876 </xsl:text></xsl:if>
877
878     <xsl:apply-templates mode="output" />
879
880 <xsl:if test="$h">
881 <xsl:text>
882 #endif
883 </xsl:text>
884 </xsl:if>
885   </xsl:template>
886
887   <xsl:template match="constant" mode="output">
888     <xsl:choose>
889       <xsl:when test="@type = 'number'">
890         <xsl:if test="$h">
891           <xsl:text>#define </xsl:text>
892           <xsl:value-of select="@name" />
893           <xsl:text> </xsl:text>
894           <xsl:value-of select="@value" />
895           <xsl:text>
896
897 </xsl:text>
898         </xsl:if>
899       </xsl:when>
900       <xsl:when test="@type = 'string'">
901         <xsl:if test="$h">
902           <xsl:text>extern </xsl:text>
903         </xsl:if>
904         <xsl:text>const char </xsl:text>
905         <xsl:value-of select="@name" />
906         <xsl:text>[]</xsl:text>
907         <xsl:if test="$c">
908           <xsl:text> = "</xsl:text>
909           <xsl:value-of select="@value" />
910           <xsl:text>"</xsl:text>
911         </xsl:if>
912         <xsl:text>;
913
914 </xsl:text>
915       </xsl:when>
916       <xsl:otherwise>
917         <xsl:if test="$h">
918           <xsl:text>extern </xsl:text>
919         </xsl:if>
920         <xsl:call-template name="type-and-name" />
921         <xsl:if test="$c">
922           <xsl:text> = </xsl:text>
923           <xsl:value-of select="@value" />
924         </xsl:if>
925         <xsl:text>;
926
927 </xsl:text>
928       </xsl:otherwise>
929     </xsl:choose>
930   </xsl:template>
931
932   <xsl:template match="typedef" mode="output">
933     <xsl:if test="$h">
934       <xsl:text>typedef </xsl:text>
935       <xsl:value-of select="@oldname" />
936       <xsl:text> </xsl:text>
937       <xsl:value-of select="@newname" />
938       <xsl:text>;
939
940 </xsl:text>
941     </xsl:if>
942   </xsl:template>
943
944   <xsl:template match="struct" mode="output">
945     <xsl:if test="$h">
946       <xsl:variable name="type-lengths">
947         <xsl:call-template name="type-lengths">
948           <xsl:with-param name="items" select="field/@type" />
949         </xsl:call-template>
950       </xsl:variable>
951       <xsl:text>typedef </xsl:text>
952       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
953       <xsl:text> {
954 </xsl:text>
955       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
956         <xsl:text>    </xsl:text>
957         <xsl:apply-templates select=".">
958           <xsl:with-param name="type-lengths" select="$type-lengths" />
959         </xsl:apply-templates>
960         <xsl:text>;
961 </xsl:text>
962       </xsl:for-each>
963       <xsl:text>} </xsl:text>
964       <xsl:value-of select="@name" />
965       <xsl:text>;
966
967 </xsl:text>
968     </xsl:if>
969   </xsl:template>
970
971   <xsl:template match="enum" mode="output">
972     <xsl:if test="$h">
973       <xsl:text>typedef enum {
974     </xsl:text>
975       <xsl:call-template name="list">
976         <xsl:with-param name="separator"><xsl:text>,
977     </xsl:text></xsl:with-param>
978         <xsl:with-param name="items">
979           <xsl:for-each select="item">
980             <item>
981               <xsl:value-of select="@name" />
982               <xsl:if test="node()"> <!-- If there is an expression -->
983                 <xsl:text> = </xsl:text>
984                 <xsl:apply-templates mode="output-expression" />
985               </xsl:if>
986             </item>
987           </xsl:for-each>
988         </xsl:with-param>
989       </xsl:call-template>
990       <xsl:text>
991 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
992
993 </xsl:text>
994     </xsl:if>
995   </xsl:template>
996
997   <xsl:template match="function" mode="output">
998     <xsl:variable name="decl-open" select="concat(@name, ' (')" />
999     <xsl:variable name="type-lengths">
1000       <xsl:call-template name="type-lengths">
1001         <xsl:with-param name="items" select="field/@type" />
1002       </xsl:call-template>
1003     </xsl:variable>
1004     <xsl:value-of select="@type" />
1005     <xsl:text>
1006 </xsl:text>
1007     <xsl:value-of select="$decl-open" />
1008     <xsl:call-template name="list">
1009       <xsl:with-param name="separator">
1010         <xsl:text>,
1011 </xsl:text>
1012         <xsl:call-template name="repeat">
1013           <xsl:with-param name="count" select="string-length($decl-open)" />
1014         </xsl:call-template>
1015       </xsl:with-param>
1016       <xsl:with-param name="items">
1017         <xsl:for-each select="field">
1018           <item>
1019             <xsl:apply-templates select=".">
1020               <xsl:with-param name="type-lengths" select="$type-lengths" />
1021             </xsl:apply-templates>
1022           </item>
1023         </xsl:for-each>
1024       </xsl:with-param>
1025     </xsl:call-template>
1026     <xsl:text>)</xsl:text>
1027
1028     <xsl:if test="$h"><xsl:text>;
1029
1030 </xsl:text></xsl:if>
1031
1032     <xsl:if test="$c">
1033       <xsl:text>
1034 {
1035 </xsl:text>
1036       <xsl:apply-templates select="l|indent" mode="function-body">
1037         <xsl:with-param name="indent" select="$indent-string" />
1038       </xsl:apply-templates>
1039       <xsl:text>}
1040
1041 </xsl:text>
1042     </xsl:if>
1043   </xsl:template>
1044
1045   <xsl:template match="l" mode="function-body">
1046     <xsl:param name="indent" />
1047     <xsl:value-of select="concat($indent, .)" /><xsl:text>
1048 </xsl:text>
1049   </xsl:template>
1050
1051   <xsl:template match="indent" mode="function-body">
1052     <xsl:param name="indent" />
1053     <xsl:apply-templates select="l|indent" mode="function-body">
1054       <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1055     </xsl:apply-templates>
1056   </xsl:template>
1057
1058   <xsl:template match="value" mode="output-expression">
1059     <xsl:value-of select="." />
1060   </xsl:template>
1061
1062   <xsl:template match="fieldref" mode="output-expression">
1063     <xsl:param name="field-prefix" />
1064     <xsl:value-of select="concat($field-prefix, .)" />
1065   </xsl:template>
1066
1067   <xsl:template match="op" mode="output-expression">
1068     <xsl:param name="field-prefix" />
1069     <xsl:text>(</xsl:text>
1070     <xsl:apply-templates select="node()[1]" mode="output-expression">
1071       <xsl:with-param name="field-prefix" select="$field-prefix" />
1072     </xsl:apply-templates>
1073     <xsl:text> </xsl:text>
1074     <xsl:value-of select="@op" />
1075     <xsl:text> </xsl:text>
1076     <xsl:apply-templates select="node()[2]" mode="output-expression">
1077       <xsl:with-param name="field-prefix" select="$field-prefix" />
1078     </xsl:apply-templates>
1079     <xsl:text>)</xsl:text>
1080   </xsl:template>
1081
1082   <xsl:template match="bit" mode="output-expression">
1083     <xsl:text>(1 &lt;&lt; </xsl:text>
1084     <xsl:value-of select="." />
1085     <xsl:text>)</xsl:text>
1086   </xsl:template>
1087
1088   <xsl:template match="function-call" mode="output-expression">
1089     <xsl:param name="field-prefix" />
1090     <xsl:value-of select="@name" />
1091     <xsl:text>(</xsl:text>
1092     <xsl:call-template name="list">
1093       <xsl:with-param name="separator" select="', '" />
1094       <xsl:with-param name="items">
1095         <xsl:for-each select="param">
1096           <item><xsl:apply-templates mode="output-expression">
1097             <xsl:with-param name="field-prefix" select="$field-prefix" />
1098           </xsl:apply-templates></item>
1099         </xsl:for-each>
1100       </xsl:with-param>
1101     </xsl:call-template>
1102     <xsl:text>)</xsl:text>
1103   </xsl:template>
1104
1105   <!-- Catch invalid elements in expressions. -->
1106   <xsl:template match="*" mode="output-expression">
1107     <xsl:message terminate="yes">
1108       <xsl:text>Invalid element in expression: </xsl:text>
1109       <xsl:value-of select="name()" />
1110     </xsl:message>
1111   </xsl:template>
1112
1113   <xsl:template match="field|exprfield">
1114     <xsl:param name="type-lengths" select="0" />
1115     <xsl:call-template name="type-and-name">
1116       <xsl:with-param name="type-lengths" select="$type-lengths" />
1117     </xsl:call-template>
1118   </xsl:template>
1119
1120   <xsl:template match="list[@fixed]">
1121     <xsl:param name="type-lengths" select="0" />
1122     <xsl:call-template name="type-and-name">
1123       <xsl:with-param name="type-lengths" select="$type-lengths" />
1124     </xsl:call-template>
1125     <xsl:text>[</xsl:text>
1126     <xsl:apply-templates mode="output-expression" />
1127     <xsl:text>]</xsl:text>
1128   </xsl:template>
1129
1130   <xsl:template match="pad">
1131     <xsl:param name="type-lengths" select="0" />
1132
1133     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1134
1135     <xsl:call-template name="type-and-name">
1136       <xsl:with-param name="type" select="'CARD8'" />
1137       <xsl:with-param name="name">
1138         <xsl:text>pad</xsl:text>
1139         <xsl:value-of select="$padnum - 1" />
1140       </xsl:with-param>
1141       <xsl:with-param name="type-lengths" select="$type-lengths" />
1142     </xsl:call-template>
1143     <xsl:if test="@bytes > 1">
1144       <xsl:text>[</xsl:text>
1145       <xsl:value-of select="@bytes" />
1146       <xsl:text>]</xsl:text>
1147     </xsl:if>
1148   </xsl:template>
1149
1150   <!-- Output the given type and name (defaulting to the corresponding
1151        attributes of the context node), with the appropriate spacing.  The
1152        type must consist of a base type (which may contain spaces), then
1153        optionally a single space and a suffix of one or more '*' characters.
1154        If the type-lengths parameter is provided, use it to line up the base
1155        types and suffixs of the type declarations. -->
1156   <xsl:template name="type-and-name">
1157     <xsl:param name="type" select="@type" />
1158     <xsl:param name="name" select="@name" />
1159     <xsl:param name="type-lengths">
1160       <max-type-length>0</max-type-length>
1161       <max-suffix-length>0</max-suffix-length>
1162     </xsl:param>
1163     
1164     <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1165     <xsl:variable name="min-type-length"
1166                   select="$type-lengths-ns/max-type-length" />
1167     <xsl:variable name="min-suffix-length"
1168                   select="$type-lengths-ns/max-suffix-length" />
1169
1170     <xsl:variable name="base-type">
1171       <xsl:choose>
1172         <xsl:when test="contains($type, ' *')">
1173           <xsl:value-of select="substring-before($type, ' *')" />
1174         </xsl:when>
1175         <xsl:otherwise>
1176           <xsl:value-of select="$type" />
1177         </xsl:otherwise>
1178       </xsl:choose>
1179     </xsl:variable>
1180     <xsl:variable name="suffix">
1181       <xsl:if test="contains($type, ' *')">
1182         <xsl:text>*</xsl:text>
1183         <xsl:value-of select="substring-after($type, ' *')" />
1184       </xsl:if>
1185     </xsl:variable>
1186
1187     <xsl:value-of select="$base-type" />
1188     <xsl:if test="string-length($base-type) &lt; $min-type-length">
1189       <xsl:call-template name="repeat">
1190         <xsl:with-param name="count" select="$min-type-length
1191                                              - string-length($base-type)" />
1192       </xsl:call-template>
1193     </xsl:if>
1194     <xsl:text> </xsl:text>
1195     <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
1196       <xsl:call-template name="repeat">
1197         <xsl:with-param name="count" select="$min-suffix-length
1198                                              - string-length($suffix)" />
1199       </xsl:call-template>
1200     </xsl:if>
1201     <xsl:value-of select="$suffix" />
1202     <xsl:value-of select="$name" />
1203   </xsl:template>
1204
1205   <!-- Output a list with a given separator.  Empty items are skipped. -->
1206   <xsl:template name="list">
1207     <xsl:param name="separator" />
1208     <xsl:param name="items" />
1209
1210     <xsl:for-each select="e:node-set($items)/*">
1211       <xsl:value-of select="." />
1212       <xsl:if test="not(position() = last())">
1213         <xsl:value-of select="$separator" />
1214       </xsl:if>
1215     </xsl:for-each>
1216   </xsl:template>
1217
1218   <!-- Repeat a string (space by default) a given number of times. -->
1219   <xsl:template name="repeat">
1220     <xsl:param name="str" select="' '" />
1221     <xsl:param name="count" />
1222
1223     <xsl:if test="$count &gt; 0">
1224       <xsl:value-of select="$str" />
1225       <xsl:call-template name="repeat">
1226         <xsl:with-param name="str"   select="$str" />
1227         <xsl:with-param name="count" select="$count - 1" />
1228       </xsl:call-template>
1229     </xsl:if>
1230   </xsl:template>
1231
1232   <!-- Record the maximum type lengths of a set of types for use as the
1233        max-type-lengths parameter of type-and-name. -->
1234   <xsl:template name="type-lengths">
1235     <xsl:param name="items" />
1236     <xsl:variable name="type-lengths-rtf">
1237       <xsl:for-each select="$items">
1238         <item>
1239           <xsl:choose>
1240             <xsl:when test="contains(., ' *')">
1241               <xsl:value-of select="string-length(
1242                                     substring-before(., ' *'))" />
1243             </xsl:when>
1244             <xsl:otherwise>
1245               <xsl:value-of select="string-length(.)" />
1246             </xsl:otherwise>
1247           </xsl:choose>
1248         </item>
1249       </xsl:for-each>
1250     </xsl:variable>
1251     <xsl:variable name="suffix-lengths-rtf">
1252       <xsl:for-each select="$items">
1253         <item>
1254           <xsl:choose>
1255             <xsl:when test="contains(., ' *')">
1256               <xsl:value-of select="string-length(substring-after(., ' *'))
1257                                     + 1" />
1258             </xsl:when>
1259             <xsl:otherwise>
1260               <xsl:text>0</xsl:text>
1261             </xsl:otherwise>
1262           </xsl:choose>
1263         </item>
1264       </xsl:for-each>
1265     </xsl:variable>
1266     <max-type-length>
1267       <xsl:call-template name="max">
1268         <xsl:with-param name="items"
1269                         select="e:node-set($type-lengths-rtf)/*" />
1270       </xsl:call-template>
1271     </max-type-length>
1272     <max-suffix-length>
1273       <xsl:call-template name="max">
1274         <xsl:with-param name="items"
1275                         select="e:node-set($suffix-lengths-rtf)/*" />
1276       </xsl:call-template>
1277     </max-suffix-length>
1278   </xsl:template>
1279
1280   <!-- Return the maximum number in a set of numbers. -->
1281   <xsl:template name="max">
1282     <xsl:param name="items" />
1283     <xsl:choose>
1284       <xsl:when test="count($items) = 0">
1285         <xsl:text>0</xsl:text>
1286       </xsl:when>
1287       <xsl:otherwise>
1288         <xsl:variable name="head" select="number($items[1])" />
1289         <xsl:variable name="tail-max">
1290           <xsl:call-template name="max">
1291             <xsl:with-param name="items" select="$items[position() > 1]" />
1292           </xsl:call-template>
1293         </xsl:variable>
1294         <xsl:choose>
1295           <xsl:when test="$head > number($tail-max)">
1296             <xsl:value-of select="$head" />
1297           </xsl:when>
1298           <xsl:otherwise>
1299             <xsl:value-of select="$tail-max" />
1300           </xsl:otherwise>
1301         </xsl:choose>
1302       </xsl:otherwise>
1303     </xsl:choose>
1304   </xsl:template>
1305 </xsl:transform>