xref: /titanic_50/usr/src/cmd/dtrace/demo/mkdemo.pl (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#!/usr/perl5/bin/perl -w
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate#
5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate# with the License.
9*7c478bd9Sstevel@tonic-gate#
10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate# and limitations under the License.
14*7c478bd9Sstevel@tonic-gate#
15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate#
21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate#
23*7c478bd9Sstevel@tonic-gate#
24*7c478bd9Sstevel@tonic-gate# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*7c478bd9Sstevel@tonic-gate# Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate#
27*7c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gaterequire 5.005;
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gateuse strict;
32*7c478bd9Sstevel@tonic-gateuse warnings;
33*7c478bd9Sstevel@tonic-gateuse Time::localtime;
34*7c478bd9Sstevel@tonic-gateuse File::Basename;
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gateour ($cmd, $chapfile, $htmlfile, $dtrace_url, %chaps);
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate$cmd = "mkdemo";
39*7c478bd9Sstevel@tonic-gate$chapfile = "chapters";
40*7c478bd9Sstevel@tonic-gate$htmlfile = "index.html";
41*7c478bd9Sstevel@tonic-gate$dtrace_url = "http://www.sun.com/bigadmin/content/dtrace";
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gatesub chaps_read {
44*7c478bd9Sstevel@tonic-gate	my $fatal;
45*7c478bd9Sstevel@tonic-gate	my %hash;
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate	open(CHAPS, "$chapfile");
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate	while (<CHAPS>) {
50*7c478bd9Sstevel@tonic-gate		my $field;
51*7c478bd9Sstevel@tonic-gate		my $value;
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate		chop;
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate		if (/^#/) {
56*7c478bd9Sstevel@tonic-gate			next;
57*7c478bd9Sstevel@tonic-gate		}
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate		if (!/:/) {
60*7c478bd9Sstevel@tonic-gate			if (exists $hash{'name'}) {
61*7c478bd9Sstevel@tonic-gate				if (exists $chaps{$hash{'name'}}) {
62*7c478bd9Sstevel@tonic-gate					print "$cmd: chapter $hash{'name'} ";
63*7c478bd9Sstevel@tonic-gate					print "has two entries.\n";
64*7c478bd9Sstevel@tonic-gate					$fatal = 1;
65*7c478bd9Sstevel@tonic-gate				}
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate				$chaps{$hash{'name'}} = { %hash };
68*7c478bd9Sstevel@tonic-gate				%hash = ();
69*7c478bd9Sstevel@tonic-gate				next;
70*7c478bd9Sstevel@tonic-gate			}
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate			%hash = ();
73*7c478bd9Sstevel@tonic-gate			next;
74*7c478bd9Sstevel@tonic-gate		}
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate		($field, $value) = split /:\s*/, $_, 2;
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate		if ($field eq "descr") {
79*7c478bd9Sstevel@tonic-gate			$value .= " ";
80*7c478bd9Sstevel@tonic-gate		}
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate		$hash{$field} .= $value;
83*7c478bd9Sstevel@tonic-gate	}
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate	if ($fatal) {
86*7c478bd9Sstevel@tonic-gate		print "$cmd: fatal errors; cannot proceed.\n";
87*7c478bd9Sstevel@tonic-gate		exit;
88*7c478bd9Sstevel@tonic-gate	}
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate	close (CHAPS);
91*7c478bd9Sstevel@tonic-gate}
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gatesub chaps_ascending {
94*7c478bd9Sstevel@tonic-gate	$chaps{$a}{number} <=> $chaps{$b}{number};
95*7c478bd9Sstevel@tonic-gate}
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gatesub demo_process {
98*7c478bd9Sstevel@tonic-gate	my $chap = $_[0];
99*7c478bd9Sstevel@tonic-gate	my $demo = $_[1];
100*7c478bd9Sstevel@tonic-gate	my $year = localtime->year() + 1900;
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate	open DEMO, "<$chap/$demo" or die "Can't open demo $chap/$demo";
103*7c478bd9Sstevel@tonic-gate	open OUT, ">$demo" or die "Can't open $demo";
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate	while (<DEMO>) {
106*7c478bd9Sstevel@tonic-gate		print OUT $_;
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate		if (/Use is subject to license terms/) {
109*7c478bd9Sstevel@tonic-gate			print OUT <<EOF;
110*7c478bd9Sstevel@tonic-gate *
111*7c478bd9Sstevel@tonic-gate * This D script is used as an example in the Solaris Dynamic Tracing Guide
112*7c478bd9Sstevel@tonic-gate * in Chapter $chaps{$chap}{number}, \"$chaps{$chap}{title}\".
113*7c478bd9Sstevel@tonic-gate *
114*7c478bd9Sstevel@tonic-gate * The full text of Chapter $chaps{$chap}{number} may be found here:
115*7c478bd9Sstevel@tonic-gate *
116*7c478bd9Sstevel@tonic-gate *   $chaps{$chap}{url}
117*7c478bd9Sstevel@tonic-gate *
118*7c478bd9Sstevel@tonic-gate * On machines that have DTrace installed, this script is available as
119*7c478bd9Sstevel@tonic-gate * $demo in /usr/demo/dtrace, a directory that contains all D scripts
120*7c478bd9Sstevel@tonic-gate * used in the Solaris Dynamic Tracing Guide.  A table of the scripts and their
121*7c478bd9Sstevel@tonic-gate * corresponding chapters may be found here:
122*7c478bd9Sstevel@tonic-gate *
123*7c478bd9Sstevel@tonic-gate *   file:///usr/demo/dtrace/index.html
124*7c478bd9Sstevel@tonic-gateEOF
125*7c478bd9Sstevel@tonic-gate		}
126*7c478bd9Sstevel@tonic-gate	}
127*7c478bd9Sstevel@tonic-gate}
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gatesub demo_find {
130*7c478bd9Sstevel@tonic-gate	my $demo = $_[0];
131*7c478bd9Sstevel@tonic-gate	my $chap;
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate	foreach $chap (keys %chaps) {
134*7c478bd9Sstevel@tonic-gate		if (!stat("$chap/$demo")) {
135*7c478bd9Sstevel@tonic-gate			next;
136*7c478bd9Sstevel@tonic-gate		}
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate		demo_process($chap, $demo);
139*7c478bd9Sstevel@tonic-gate		return;
140*7c478bd9Sstevel@tonic-gate	}
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate	die "Couldn't find $demo in any chapter";
143*7c478bd9Sstevel@tonic-gate}
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gatesub chaps_process {
146*7c478bd9Sstevel@tonic-gate	my $outfile = $_[0];
147*7c478bd9Sstevel@tonic-gate	my $chap;
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate	open HTML, ">$outfile" or die "Can't open $outfile.";
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate	print HTML "<html>\n<head>\n";
152*7c478bd9Sstevel@tonic-gate	print HTML "<title>Example DTrace Scripts</title>\n";
153*7c478bd9Sstevel@tonic-gate	print HTML "</head>\n<body bgcolor=\"#ffffff\">\n";
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate	print HTML "<table width=\"85%\" border=0 align=\"center\"><tr><td>";
156*7c478bd9Sstevel@tonic-gate	print HTML "<h2>DTrace Examples</h2>\n";
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate	print HTML "<hr><p>\n";
159*7c478bd9Sstevel@tonic-gate	print HTML "Here are the <a href=\"$dtrace_url\">DTrace</a> scripts\n";
160*7c478bd9Sstevel@tonic-gate	print HTML "that are used as examples in the\n";
161*7c478bd9Sstevel@tonic-gate	print HTML "<a href=\"$chaps{book}{url}\">$chaps{book}{title}</a>. ";
162*7c478bd9Sstevel@tonic-gate	print HTML "For more information on any one script, follow the link\n";
163*7c478bd9Sstevel@tonic-gate	print HTML "to its corresponding chapter.\n";
164*7c478bd9Sstevel@tonic-gate	print HTML "<p>\n<hr><p>\n";
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate	print HTML "<left><table width=\"85%\" border=1 cellpadding=4 ";
167*7c478bd9Sstevel@tonic-gate	print HTML "cellspacing=0 align=\"center\" bgcolor=\"#ffffff\">\n";
168*7c478bd9Sstevel@tonic-gate	print HTML "<tr bgcolor=\"#5882a1\"><td width=\"50%\">";
169*7c478bd9Sstevel@tonic-gate	print HTML "<font color=\"#ffffff\"><b>Chapter</b></td></font>\n";
170*7c478bd9Sstevel@tonic-gate	print HTML "<td><font color=\"#ffffff\"><b>Script</b></td>\n";
171*7c478bd9Sstevel@tonic-gate	print HTML "</font></tr>\n";
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate	foreach $chap (sort chaps_ascending (keys %chaps)) {
174*7c478bd9Sstevel@tonic-gate		my @demos;
175*7c478bd9Sstevel@tonic-gate		my $demo;
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate		#
178*7c478bd9Sstevel@tonic-gate		# Open the directory associated with the chapter.
179*7c478bd9Sstevel@tonic-gate		#
180*7c478bd9Sstevel@tonic-gate		if ($chap =~ /^book$/) {
181*7c478bd9Sstevel@tonic-gate			next;
182*7c478bd9Sstevel@tonic-gate		}
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate		opendir(DEMOS, $chap) || die("Cannot open directory $chap");
185*7c478bd9Sstevel@tonic-gate		@demos = readdir(DEMOS);
186*7c478bd9Sstevel@tonic-gate		closedir(DEMOS);
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate		print HTML "<tr>\n";
189*7c478bd9Sstevel@tonic-gate		print HTML "<td align=left>";
190*7c478bd9Sstevel@tonic-gate		print HTML "<a href=\"$chaps{$chap}{url}\">";
191*7c478bd9Sstevel@tonic-gate		print HTML "Chapter $chaps{$chap}{number}: ";
192*7c478bd9Sstevel@tonic-gate		print HTML "$chaps{$chap}{title}</a></td>\n";
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate		print HTML "<td><table border=0>\n";
195*7c478bd9Sstevel@tonic-gate
196*7c478bd9Sstevel@tonic-gate		foreach $demo (@demos) {
197*7c478bd9Sstevel@tonic-gate			if ($demo !~ /^[a-z].*\.d$/) {
198*7c478bd9Sstevel@tonic-gate				next;
199*7c478bd9Sstevel@tonic-gate			}
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate			print HTML "<tr><td><a href=\"$demo\">$demo</a>";
202*7c478bd9Sstevel@tonic-gate			print HTML "</td></tr>\n";
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate			demo_process($chap, $demo);
205*7c478bd9Sstevel@tonic-gate		}
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate		print HTML "</table></td></tr>\n";
208*7c478bd9Sstevel@tonic-gate	}
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate	print HTML "</table>\n</td>\n<p>\n\n";
211*7c478bd9Sstevel@tonic-gate	print HTML "</td></tr>\n";
212*7c478bd9Sstevel@tonic-gate	print HTML "<tr><td><hr><small>Copyright ";
213*7c478bd9Sstevel@tonic-gate	print HTML localtime->year() + 1900;
214*7c478bd9Sstevel@tonic-gate	print HTML " Sun Microsystems</small>\n";
215*7c478bd9Sstevel@tonic-gate	print HTML "</table>\n";
216*7c478bd9Sstevel@tonic-gate	print HTML "</body>\n</html>\n";
217*7c478bd9Sstevel@tonic-gate	close HTML;
218*7c478bd9Sstevel@tonic-gate}
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gatechaps_read();
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gateif (basename($ARGV[0]) ne "$htmlfile") {
223*7c478bd9Sstevel@tonic-gate	demo_find(basename($ARGV[0]));
224*7c478bd9Sstevel@tonic-gate} else {
225*7c478bd9Sstevel@tonic-gate	chaps_process($htmlfile);
226*7c478bd9Sstevel@tonic-gate}
227