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