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