summaryrefslogtreecommitdiffstats
path: root/doc/SystemTap_Tapset_Reference/extractxml.pl
blob: 492a29cbf00b4b666d610f566929451f542d53f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/perl
# Generates xml files from tapset .stp files.
# Copyright (C) 2008 Red Hat Inc.
#
# This file is part of systemtap, and is free software.  You can
# redistribute it and/or modify it under the terms of the GNU General
# Public License (GPL); either version 2, or (at your option) any
# later version.

use strict;
use warnings;

use Cwd 'abs_path';
use File::Copy;
use File::Find;
use File::Path;
use Text::Wrap;
use IO::File;
use POSIX qw(tmpnam);

my $XMLHEADER = 
    "<?xml version='1.0'?>\n"
    . "<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML V4.5//EN\" \"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd\" [\n"
    . "]>\n"
    ."<!-- This file is machine generated based on tapset files \n"
    . "   Do not modify this file -->\n"
    . "<book>\n"
    . "<xi:include href=\"Book_Info.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\n"
    . "<xi:include href=\"Preface.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\n"
    . "<xi:include href=\"Introduction.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\n"
;
my $XMLFOOTER =
    "<xi:include href=\"Revision_History.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\n"
    . "<index />\n"
    ."</book>\n";

my $XML_CHAPTER_HEADER = 
    "<?xml version='1.0'?>\n"
    . "<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML V4.5//EN\" \"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd\" [\n"
    . "]>\n"
    . "<!-- This file is extracted from the tapset files \n"
    . "   Do not modify this file -->\n";

my $XML_CHAPTER_FOOTER = "";

my $inputdir;
if ($#ARGV >= 0) {
    $inputdir = $ARGV[0];
} else {
    $inputdir = ".";
}
$inputdir = abs_path($inputdir);

my $outputdir;
if ($#ARGV >= 1) {
    $outputdir = $ARGV[1];
} else {
    $outputdir = $inputdir;
}
$outputdir = abs_path($outputdir);

#attempt to create the output directory
if ($inputdir ne $outputdir) {
    if (! -d "$outputdir") {
	mkpath("$outputdir", 1, 0711);
    }
}

my %scripts = ();

print "Extracting xml from .stp files in $inputdir...\n";
find(\&extract_xml, $inputdir);


# Output list of extracted xml files
my $tapsetxml = "$outputdir/Tapset_Reference.xml";
open (TAPSETXML, ">$tapsetxml")
    || die "couldn't open $tapsetxml: $!";
print "Creating $tapsetxml...\n";
print TAPSETXML $XMLHEADER;

my $tapset;
foreach $tapset (sort keys %scripts) {
    print TAPSETXML 	"<xi:include href=\"$tapset\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\n"

}
print TAPSETXML $XMLFOOTER;
close (TAPSETXML);


sub extract_xml {
    my $file = $_;
    my $filename = $File::Find::name;
    my $ofile;
    my $ofilefullt;
    my $ofilefull;

    if (-f $file && $file =~ /\.stp$/) {
	open FILE, $file or die "couldn't open '$file': $!\n";

	$ofilefullt = tmpnam();
	open OFILET, ">$ofilefullt" or die "couldn't open '$ofilefullt': $!\n";

	print "Extracting xml from $filename...\n";
	
	while (<FILE>) {
	    print OFILET if s/\s*\/\/\///;
	}
	close OFILET;
	close FILE;

	#If xml was extracted make a .xml file
	if (-s $ofilefullt) {
	    #get rid of the inputdir part and .stp, add .xml
	    # chop off the search dir prefix.
	    $inputdir =~ s/\/$//;
	    $ofile = substr $filename, (length $inputdir) + 1;
	    $ofile =~ s/.stp/.xml/;
	    $ofile =~ s/\//_/g;
	    $scripts{$ofile} = $ofile;
	    print "$ofile\n";
	    $ofilefull = "$outputdir/$ofile";
	    open OFILE, ">$ofilefull"
		or die "couldn't open '$ofilefull': $!\n";
	    open OFILET, "$ofilefullt"
		or die "couldn't open '$ofilefullt': $!\n";
	    print OFILE "$XML_CHAPTER_HEADER";
	    while (<OFILET>) {
		print OFILE ;
	    }
	    print OFILE "$XML_CHAPTER_FOOTER";
	    close OFILET;
	    close OFILE;
	}
	unlink($ofilefullt);
    }
}