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