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