source: BLFS/xsl/scripts.xsl@ 67992a0

2.4 ablfs-more legacy new_features trunk
Last change on this file since 67992a0 was 67992a0, checked in by Pierre Labastie <pierre@…>, 9 years ago

Fortify unpacking in the scripts generated for BLFS, see ticket #1693

  • Property mode set to 100644
File size: 18.9 KB
Line 
1<?xml version="1.0"?>
2
3<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:exsl="http://exslt.org/common"
5 extension-element-prefixes="exsl"
6 version="1.0">
7
8<!-- $Id: scripts.xsl 34 2012-02-21 16:05:09Z labastie $ -->
9
10<!-- XSLT stylesheet to create shell scripts from "linear build" BLFS books. -->
11
12 <!-- Build as user (y) or as root (n)? -->
13 <xsl:param name="sudo" select="'y'"/>
14
15 <xsl:template match="/">
16 <xsl:apply-templates select="//sect1"/>
17 </xsl:template>
18
19<!--=================== Master chunks code ======================-->
20
21 <xsl:template match="sect1">
22
23 <xsl:if test="@id != 'bootscripts'">
24 <!-- The file names -->
25 <xsl:variable name="filename" select="@id"/>
26
27 <!-- The build order -->
28 <xsl:variable name="position" select="position()"/>
29 <xsl:variable name="order">
30 <xsl:choose>
31 <xsl:when test="string-length($position) = 1">
32 <xsl:text>00</xsl:text>
33 <xsl:value-of select="$position"/>
34 </xsl:when>
35 <xsl:when test="string-length($position) = 2">
36 <xsl:text>0</xsl:text>
37 <xsl:value-of select="$position"/>
38 </xsl:when>
39 <xsl:otherwise>
40 <xsl:value-of select="$position"/>
41 </xsl:otherwise>
42 </xsl:choose>
43 </xsl:variable>
44
45 <!-- Depuration code -->
46 <xsl:message>
47 <xsl:text>SCRIPT is </xsl:text>
48 <xsl:value-of select="concat($order,'-z-',$filename)"/>
49 <xsl:text>&#xA; FTPDIR is </xsl:text>
50 <xsl:value-of select="$filename"/>
51 <xsl:text>&#xA;&#xA;</xsl:text>
52 </xsl:message>
53
54 <!-- Creating the scripts -->
55 <exsl:document href="{$order}-z-{$filename}" method="text">
56 <xsl:text>#!/bin/bash&#xA;set -e&#xA;&#xA;</xsl:text>
57 <xsl:choose>
58 <!-- Package page -->
59 <xsl:when test="sect2[@role='package']">
60 <!-- We build in a subdirectory -->
61 <xsl:text>PKG_DIR=</xsl:text>
62 <xsl:value-of select="$filename"/>
63 <xsl:text>&#xA;</xsl:text>
64 <!-- Download code and build commands -->
65 <xsl:apply-templates select="sect2"/>
66 <!-- Clean-up -->
67 <xsl:text>cd $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
68 <!-- In some case, some files in the build tree are owned
69 by root -->
70 <xsl:if test="$sudo='y'">
71 <xsl:text>sudo </xsl:text>
72 </xsl:if>
73 <xsl:text>rm -rf $UNPACKDIR unpacked&#xA;&#xA;</xsl:text>
74 </xsl:when>
75 <!-- Non-package page -->
76 <xsl:otherwise>
77 <xsl:apply-templates select=".//screen"/>
78 </xsl:otherwise>
79 </xsl:choose>
80 <xsl:text>exit</xsl:text>
81 </exsl:document>
82 </xsl:if>
83 </xsl:template>
84
85<!--======================= Sub-sections code =======================-->
86
87 <xsl:template match="sect2">
88 <xsl:choose>
89 <xsl:when test="@role = 'package'">
90 <xsl:text>mkdir -p $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
91 <xsl:text>cd $SRC_DIR/$PKG_DIR&#xA;</xsl:text>
92 <!-- Download information is in bridgehead tags -->
93 <xsl:apply-templates select="bridgehead[@renderas='sect3']"/>
94 <xsl:text>&#xA;</xsl:text>
95 </xsl:when>
96 <xsl:when test="@role = 'qt4-prefix' or @role = 'qt5-prefix'">
97 <xsl:apply-templates select=".//screen"/>
98 </xsl:when>
99 <xsl:when test="@role = 'installation'">
100 <xsl:text>
101find . -maxdepth 1 -mindepth 1 -type d | xargs sudo rm -rf
102case $PACKAGE in
103 *.tar.gz|*.tar.bz2|*.tar.xz|*.tgz)
104 tar -xvf $PACKAGE &gt; unpacked
105 UNPACKDIR=`grep '[^./]\+' unpacked | head -n1 | sed 's@^./@@;s@/.*@@'`
106 ;;
107 *.zip)
108 zipinfo -1 $PACKAGE &gt; unpacked
109 UNPACKDIR="$(sed 's@/.*@@' unpacked | uniq )"
110 if test $(wc -w &lt;&lt;&lt; $UNPACKDIR) -eq 1; then
111 unzip $PACKAGE
112 else
113 UNPACKDIR=${PACKAGE%.zip}
114 unzip -d $UNPACKDIR $PACKAGE
115 fi
116 ;;
117 *)
118 UNPACKDIR=$PKG_DIR-build
119 mkdir $UNPACKDIR
120 cp $PACKAGE $UNPACKDIR
121 ;;
122esac
123cd $UNPACKDIR&#xA;
124</xsl:text>
125 <xsl:apply-templates select=".//screen | .//para/command"/>
126 <xsl:if test="$sudo = 'y'">
127 <xsl:text>sudo /sbin/</xsl:text>
128 </xsl:if>
129 <xsl:text>ldconfig&#xA;&#xA;</xsl:text>
130 </xsl:when>
131 <xsl:when test="@role = 'configuration'">
132 <xsl:apply-templates select=".//screen" mode="config"/>
133 </xsl:when>
134 </xsl:choose>
135 </xsl:template>
136
137<!--==================== Download code =======================-->
138
139 <!-- template for extracting the filename from an url in the form:
140 proto://internet.name/dir1/.../dirn/filename?condition.
141 Needed, because substring-after(...,'/') returns only the
142 substring after the first '/'. -->
143 <xsl:template name="package_name">
144 <xsl:param name="url" select="foo"/>
145 <xsl:param name="sub-url" select="substring-after($url,'/')"/>
146 <xsl:choose>
147 <xsl:when test="contains($sub-url,'/')">
148 <xsl:call-template name="package_name">
149 <xsl:with-param name="url" select="$sub-url"/>
150 </xsl:call-template>
151 </xsl:when>
152 <xsl:otherwise>
153 <xsl:choose>
154 <xsl:when test="contains($sub-url,'?')">
155 <xsl:value-of select="substring-before($sub-url,'?')"/>
156 </xsl:when>
157 <xsl:otherwise>
158 <xsl:value-of select="$sub-url"/>
159 </xsl:otherwise>
160 </xsl:choose>
161 </xsl:otherwise>
162 </xsl:choose>
163 </xsl:template>
164
165 <!-- Generates the code to download a package, an additional package or
166 a patch. -->
167 <xsl:template name="download-file">
168 <xsl:param name="httpurl" select="''"/>
169 <xsl:param name="ftpurl" select="''"/>
170 <xsl:param name="md5" select="''"/>
171 <xsl:param name="varname" select="''"/>
172 <xsl:variable name="package">
173 <xsl:call-template name="package_name">
174 <xsl:with-param name="url">
175 <xsl:choose>
176 <xsl:when test="string-length($httpurl) &gt; 10">
177 <xsl:value-of select="$httpurl"/>
178 </xsl:when>
179 <xsl:otherwise>
180 <xsl:value-of select="$ftpurl"/>
181 </xsl:otherwise>
182 </xsl:choose>
183 </xsl:with-param>
184 </xsl:call-template>
185 </xsl:variable>
186 <xsl:variable name="first_letter"
187 select="translate(substring($package,1,1),
188 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
189 'abcdefghijklmnopqrstuvwxyz')"/>
190 <xsl:text>&#xA;</xsl:text>
191 <xsl:value-of select="$varname"/>
192 <xsl:text>=</xsl:text>
193 <xsl:value-of select="$package"/>
194 <xsl:text>&#xA;if [[ ! -f $</xsl:text>
195 <xsl:value-of select="$varname"/>
196 <xsl:text> ]] ; then&#xA;</xsl:text>
197 <!-- SRC_ARCHIVE may have subdirectories or not -->
198 <xsl:text> if [[ -f $SRC_ARCHIVE/$PKG_DIR/$</xsl:text>
199 <xsl:value-of select="$varname"/>
200 <xsl:text> ]] ; then&#xA;</xsl:text>
201 <xsl:text> cp $SRC_ARCHIVE/$PKG_DIR/$</xsl:text>
202 <xsl:value-of select="$varname"/>
203 <xsl:text> $</xsl:text>
204 <xsl:value-of select="$varname"/>
205 <xsl:text>&#xA;</xsl:text>
206 <xsl:text> elif [[ -f $SRC_ARCHIVE/$</xsl:text>
207 <xsl:value-of select="$varname"/>
208 <xsl:text> ]] ; then&#xA;</xsl:text>
209 <xsl:text> cp $SRC_ARCHIVE/$</xsl:text>
210 <xsl:value-of select="$varname"/>
211 <xsl:text> $</xsl:text>
212 <xsl:value-of select="$varname"/>
213 <xsl:text>&#xA; else&#xA;</xsl:text>
214 <!-- The FTP_SERVER mirror -->
215 <xsl:text> wget -T 30 -t 5 ${FTP_SERVER}svn/</xsl:text>
216 <xsl:value-of select="$first_letter"/>
217 <xsl:text>/$</xsl:text>
218 <xsl:value-of select="$varname"/>
219 <xsl:if test="string-length($httpurl) &gt; 10">
220 <xsl:text> ||
221 wget -T 30 -t 5 </xsl:text>
222 <xsl:value-of select="$httpurl"/>
223 </xsl:if>
224 <xsl:if test="string-length($ftpurl) &gt; 10">
225 <xsl:text> ||
226 wget -T 30 -t 5 </xsl:text>
227 <xsl:value-of select="$ftpurl"/>
228 </xsl:if>
229 <xsl:text>
230 cp $</xsl:text>
231 <xsl:value-of select="$varname"/>
232 <xsl:text> $SRC_ARCHIVE
233 fi
234fi
235</xsl:text>
236 <xsl:if test="string-length($md5) &gt; 10">
237 <xsl:text>echo "</xsl:text>
238 <xsl:value-of select="$md5"/>
239 <xsl:text>&#x20;&#x20;$</xsl:text>
240 <xsl:value-of select="$varname"/>
241 <xsl:text>" | md5sum -c -
242</xsl:text>
243 </xsl:if>
244 </xsl:template>
245
246 <!-- Extract the MD5 sum information -->
247 <xsl:template match="para" mode="md5">
248 <xsl:choose>
249 <xsl:when test="contains(substring-after(string(),'sum: '),'&#xA;')">
250 <xsl:value-of select="substring-before(substring-after(string(),'sum: '),'&#xA;')"/>
251 </xsl:when>
252 <xsl:otherwise>
253 <xsl:value-of select="substring-after(string(),'sum: ')"/>
254 </xsl:otherwise>
255 </xsl:choose>
256 </xsl:template>
257
258 <!-- We have several templates itemizedlist, depending on whether we
259 expect the package information, or additional package(s) or patch(es)
260 information. Select the appropriate mode here. -->
261 <xsl:template match="bridgehead">
262 <xsl:choose>
263 <!-- Special case for Openjdk -->
264 <xsl:when test="contains(string(),'Source Package Information')">
265 <xsl:apply-templates
266 select="following-sibling::itemizedlist[1]//simplelist">
267 <xsl:with-param name="varname" select="'PACKAGE'"/>
268 </xsl:apply-templates>
269 <xsl:apply-templates select="following-sibling::itemizedlist
270 [preceding-sibling::bridgehead[1]=current()
271 and position() &gt;1]//simplelist">
272 <xsl:with-param name="varname" select="'PACKAGE1'"/>
273 </xsl:apply-templates>
274 </xsl:when>
275 <!-- Package information -->
276 <xsl:when test="contains(string(),'Package Information')">
277 <xsl:apply-templates select="following-sibling::itemizedlist
278 [preceding-sibling::bridgehead[1]=current()]"
279 mode="package"/>
280 </xsl:when>
281 <!-- Additional package information -->
282 <!-- special case for llvm -->
283 <xsl:when test="contains(string(),'Optional Download')">
284 <xsl:apply-templates select="following-sibling::itemizedlist"
285 mode="additional"/>
286 </xsl:when>
287 <!-- All other additional packages have "Additional" -->
288 <xsl:when test="contains(string(),'Additional')">
289 <xsl:apply-templates select="following-sibling::itemizedlist"
290 mode="additional"/>
291 </xsl:when>
292 <!-- Do not do anything if the dev has created another type of
293 bridgehead. -->
294 <xsl:otherwise/>
295 </xsl:choose>
296 </xsl:template>
297
298 <!-- Call the download code template with appropriate parameters -->
299 <xsl:template match="itemizedlist" mode="package">
300 <xsl:call-template name="download-file">
301 <xsl:with-param name="httpurl">
302 <xsl:value-of select="./listitem[1]/para/ulink/@url"/>
303 </xsl:with-param>
304 <xsl:with-param name="ftpurl">
305 <xsl:value-of select="./listitem/para[contains(string(),'FTP')]/ulink/@url"/>
306 </xsl:with-param>
307 <xsl:with-param name="md5">
308 <xsl:apply-templates select="./listitem/para[contains(string(),'MD5')]"
309 mode="md5"/>
310 </xsl:with-param>
311 <xsl:with-param name="varname" select="'PACKAGE'"/>
312 </xsl:call-template>
313 </xsl:template>
314
315 <xsl:template match="itemizedlist" mode="additional">
316 <!-- The normal layout is "one listitem"<->"one url", but some devs
317 find amusing to have FTP and/or MD5sum listitems, or to
318 enclose the download information inside a simplelist tag... -->
319 <xsl:for-each select="listitem[.//ulink]">
320 <xsl:choose>
321 <!-- hopefully, there was a HTTP line before -->
322 <xsl:when test="contains(string(./para),'FTP')"/>
323 <xsl:when test=".//simplelist">
324 <xsl:apply-templates select=".//simplelist">
325 <xsl:with-param name="varname" select="'PACKAGE1'"/>
326 </xsl:apply-templates>
327 </xsl:when>
328 <xsl:otherwise>
329 <xsl:call-template name="download-file">
330 <xsl:with-param name="httpurl">
331 <xsl:value-of select="./para/ulink/@url"/>
332 </xsl:with-param>
333 <xsl:with-param name="ftpurl">
334 <xsl:value-of
335 select="following-sibling::listitem[1]/
336 para[contains(string(),'FTP')]/ulink/@url"/>
337 </xsl:with-param>
338 <xsl:with-param name="md5">
339 <xsl:apply-templates
340 select="following-sibling::listitem[position()&lt;3]/
341 para[contains(string(),'MD5')]"
342 mode="md5"/>
343 </xsl:with-param>
344 <xsl:with-param name="varname">
345 <xsl:choose>
346 <xsl:when test="contains(./para/ulink/@url,'.patch')">
347 <xsl:text>PATCH</xsl:text>
348 </xsl:when>
349 <xsl:otherwise>
350 <xsl:text>PACKAGE1</xsl:text>
351 </xsl:otherwise>
352 </xsl:choose>
353 </xsl:with-param>
354 </xsl:call-template>
355 </xsl:otherwise>
356 </xsl:choose>
357 </xsl:for-each>
358 </xsl:template>
359
360 <!-- the simplelist case. Hopefully, the layout is one member for
361 url, one for md5 and others for various information, that we do not
362 use -->
363 <xsl:template match="simplelist">
364 <xsl:param name="varname" select="'PACKAGE1'"/>
365 <xsl:call-template name="download-file">
366 <xsl:with-param name="httpurl" select=".//ulink/@url"/>
367 <xsl:with-param name="md5">
368 <xsl:value-of select="substring-after(member[contains(string(),'MD5')],'sum: ')"/>
369 </xsl:with-param>
370 <xsl:with-param name="varname" select="$varname"/>
371 </xsl:call-template>
372 </xsl:template>
373<!--======================== Commands code ==========================-->
374
375 <xsl:template match="screen">
376 <xsl:if test="child::* = userinput and not(@role = 'nodump')">
377 <xsl:choose>
378 <xsl:when test="@role = 'root'">
379 <xsl:if test="$sudo = 'y'">
380 <xsl:text>sudo -E sh &lt;&lt; ROOT_EOF&#xA;</xsl:text>
381 </xsl:if>
382 <xsl:apply-templates mode="root"/>
383 <xsl:if test="$sudo = 'y'">
384 <xsl:text>&#xA;ROOT_EOF</xsl:text>
385 </xsl:if>
386 </xsl:when>
387 <xsl:otherwise>
388 <xsl:apply-templates select="userinput"/>
389 </xsl:otherwise>
390 </xsl:choose>
391 <xsl:text>&#xA;</xsl:text>
392 </xsl:if>
393 </xsl:template>
394
395 <xsl:template match="screen" mode="config">
396 <xsl:if test="preceding-sibling::para[1]/xref[@linkend='bootscripts']">
397 <xsl:text>[[ ! -d $SRC_DIR/blfs-bootscripts ]] &amp;&amp; mkdir $SRC_DIR/blfs-bootscripts
398pushd $SRC_DIR/blfs-bootscripts
399URL=</xsl:text>
400 <xsl:value-of select="id('bootscripts')//itemizedlist//ulink/@url"/><xsl:text>
401BOOTPACKG=$(basename $URL)
402if [[ ! -f $BOOTPACKG ]] ; then
403 if [[ -f $SRC_ARCHIVE/$PKG_DIR/$BOOTPACKG ]] ; then
404 cp $SRC_ARCHIVE/$PKG_DIR/$BOOTPACKG $BOOTPACKG
405 elif [[ -f $SRC_ARCHIVE/$BOOTPACKG ]] ; then
406 cp $SRC_ARCHIVE/$BOOTPACKG $BOOTPACKG
407 else
408 wget -T 30 -t 5 $URL
409 cp $BOOTPACKG $SRC_ARCHIVE
410 fi
411 rm -f unpacked
412fi
413
414if [[ -e unpacked ]] ; then
415 BOOTUNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
416 if ! [[ -d $BOOTUNPACKDIR ]]; then
417 rm unpacked
418 tar -xvf $BOOTPACKG > unpacked
419 BOOTUNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
420 fi
421else
422 tar -xvf $BOOTPACKG > unpacked
423 BOOTUNPACKDIR=`head -n1 unpacked | sed 's@^./@@;s@/.*@@'`
424fi
425cd $BOOTUNPACKDIR
426</xsl:text>
427 </xsl:if>
428 <xsl:apply-templates select='.'/>
429 <xsl:if test="preceding-sibling::para[1]/xref[@linkend='bootscripts']">
430 <xsl:text>
431popd</xsl:text>
432 </xsl:if>
433 <xsl:text>&#xA;</xsl:text>
434 </xsl:template>
435
436 <xsl:template match="para/command">
437 <xsl:variable name="ns" select="normalize-space(string())"/>
438 <xsl:if test="(contains($ns,'test') or
439 contains($ns,'check'))">
440 <xsl:text>#</xsl:text>
441 <xsl:value-of select="substring-before($ns,'make ')"/>
442 <xsl:text>make </xsl:text>
443 <xsl:if test="not(contains($ns,'-k'))">
444 <xsl:text>-k </xsl:text>
445 </xsl:if>
446 <xsl:value-of select="substring-after($ns,'make ')"/>
447 <xsl:text> || true&#xA;</xsl:text>
448 </xsl:if>
449 </xsl:template>
450
451 <xsl:template match="userinput">
452 <xsl:apply-templates/>
453 </xsl:template>
454
455 <xsl:template match="text()" mode="root">
456 <xsl:call-template name="output-root">
457 <xsl:with-param name="out-string" select="string()"/>
458 </xsl:call-template>
459 </xsl:template>
460
461 <xsl:template name="output-root">
462 <xsl:param name="out-string" select="''"/>
463 <xsl:choose>
464 <xsl:when test="contains($out-string,'make ')">
465 <xsl:call-template name="output-root">
466 <xsl:with-param name="out-string"
467 select="substring-before($out-string,'make ')"/>
468 </xsl:call-template>
469 <xsl:text>make -j1 </xsl:text>
470 <xsl:call-template name="output-root">
471 <xsl:with-param name="out-string"
472 select="substring-after($out-string,'make ')"/>
473 </xsl:call-template>
474 </xsl:when>
475 <xsl:when test="contains($out-string,'$') and $sudo = 'y'">
476 <xsl:call-template name="output-root">
477 <xsl:with-param name="out-string"
478 select="substring-before($out-string,'$')"/>
479 </xsl:call-template>
480 <xsl:text>\$</xsl:text>
481 <xsl:call-template name="output-root">
482 <xsl:with-param name="out-string"
483 select="substring-after($out-string,'$')"/>
484 </xsl:call-template>
485 </xsl:when>
486 <xsl:when test="contains($out-string,'`') and $sudo = 'y'">
487 <xsl:call-template name="output-root">
488 <xsl:with-param name="out-string"
489 select="substring-before($out-string,'`')"/>
490 </xsl:call-template>
491 <xsl:text>\`</xsl:text>
492 <xsl:call-template name="output-root">
493 <xsl:with-param name="out-string"
494 select="substring-after($out-string,'`')"/>
495 </xsl:call-template>
496 </xsl:when>
497 <xsl:when test="contains($out-string,'\') and $sudo = 'y'">
498 <xsl:call-template name="output-root">
499 <xsl:with-param name="out-string"
500 select="substring-before($out-string,'\')"/>
501 </xsl:call-template>
502 <xsl:text>\\</xsl:text>
503 <xsl:call-template name="output-root">
504 <xsl:with-param name="out-string"
505 select="substring-after($out-string,'\')"/>
506 </xsl:call-template>
507 </xsl:when>
508 <xsl:otherwise>
509 <xsl:value-of select="$out-string"/>
510 </xsl:otherwise>
511 </xsl:choose>
512 </xsl:template>
513
514 <xsl:template match="replaceable">
515 <xsl:text>**EDITME</xsl:text>
516 <xsl:apply-templates/>
517 <xsl:text>EDITME**</xsl:text>
518 </xsl:template>
519
520 <xsl:template match="replaceable" mode="root">
521 <xsl:text>**EDITME</xsl:text>
522 <xsl:apply-templates/>
523 <xsl:text>EDITME**</xsl:text>
524 </xsl:template>
525
526</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.