#! /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 = "\n" . "\n" ."\n" . "\n" . "\n" . "\n" . "\n" ; my $XMLFOOTER = "\n" . "\n" ."\n"; my $XML_CHAPTER_HEADER = "\n" . "\n" . "\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 "\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 () { 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 () { print OFILE ; } print OFILE "$XML_CHAPTER_FOOTER"; close OFILET; close OFILE; } unlink($ofilefullt); } }