xref: /illumos-gate/usr/src/cmd/dtrace/demo/mkdemo.pl (revision 92a0208178405fef708b0283ffcaa02fbc3468ff)
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 2008 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}{index} <=> $chaps{$b}{index};
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 * wiki in the \"$chaps{$chap}{title}\" Chapter.
112 *
113 * The full text of the this chapter 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	close (DEMO);
128	close (OUT);
129}
130
131sub demo_find {
132	my $demo = $_[0];
133	my $chap;
134
135	foreach $chap (keys %chaps) {
136		if (!stat("$chap/$demo")) {
137			next;
138		}
139
140		demo_process($chap, $demo);
141		return;
142	}
143
144	die "Couldn't find $demo in any chapter";
145}
146
147sub chaps_process {
148	my $outfile = $_[0];
149	my $chap;
150
151	open HTML, ">$outfile" or die "Can't open $outfile.";
152
153	print HTML "<html>\n<head>\n";
154	print HTML "<title>Example DTrace Scripts</title>\n";
155	print HTML "</head>\n<body bgcolor=\"#ffffff\">\n";
156
157	print HTML "<table width=\"85%\" border=0 align=\"center\"><tr><td>";
158	print HTML "<h2>DTrace Examples</h2>\n";
159
160	print HTML "<hr><p>\n";
161	print HTML "Here are the <a href=\"$dtrace_url\">DTrace</a> scripts\n";
162	print HTML "that are used as examples in the\n";
163	print HTML "<a href=\"$chaps{book}{url}\">$chaps{book}{title}</a>. ";
164	print HTML "For more information on any one script, follow the link\n";
165	print HTML "to its corresponding chapter.\n";
166	print HTML "<p>\n<hr><p>\n";
167
168	print HTML "<left><table width=\"85%\" border=1 cellpadding=4 ";
169	print HTML "cellspacing=0 align=\"center\" bgcolor=\"#ffffff\">\n";
170	print HTML "<tr bgcolor=\"#5882a1\"><td width=\"50%\">";
171	print HTML "<font color=\"#ffffff\"><b>Chapter</b></td></font>\n";
172	print HTML "<td><font color=\"#ffffff\"><b>Script</b></td>\n";
173	print HTML "</font></tr>\n";
174
175	foreach $chap (sort chaps_ascending (keys %chaps)) {
176		my @demos;
177		my $demo;
178
179		#
180		# Open the directory associated with the chapter.
181		#
182		if ($chap =~ /^book$/) {
183			next;
184		}
185
186		opendir(DEMOS, $chap) || die("Cannot open directory $chap");
187		@demos = readdir(DEMOS);
188		closedir(DEMOS);
189
190		print HTML "<tr>\n";
191		print HTML "<td align=left>";
192		print HTML "<a href=\"$chaps{$chap}{url}\">";
193		print HTML "$chaps{$chap}{title}</a></td>\n";
194
195		print HTML "<td><table border=0>\n";
196
197		foreach $demo (sort(@demos)) {
198			if ($demo !~ /^[a-z].*\.d$/) {
199				next;
200			}
201
202			print HTML "<tr><td><a href=\"$demo\">$demo</a>";
203			print HTML "</td></tr>\n";
204
205			demo_process($chap, $demo);
206		}
207
208		print HTML "</table></td></tr>\n";
209	}
210
211	print HTML "</table>\n</td>\n<p>\n\n";
212	print HTML "</td></tr>\n";
213	print HTML "<tr><td><hr><small>Copyright ";
214	print HTML localtime->year() + 1900;
215	print HTML " Sun Microsystems</small>\n";
216	print HTML "</table>\n";
217	print HTML "</body>\n</html>\n";
218	close HTML;
219}
220
221chaps_read();
222
223if (basename($ARGV[0]) ne "$htmlfile") {
224	demo_find(basename($ARGV[0]));
225} else {
226	chaps_process($htmlfile);
227}
228