Debian Codegraph @

local/minimalist/feature-makedocs

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use IO::File;
5 use Getopt::Std;
6 our %opts = (W => 2);
7
8 getopts('ohwW:', \%opts) || usage();
9
10 usage() if ($opts{'h'});
11
12 usage() if ($#ARGV != 1);
13
14 # should be the feature-details.h file
15 my $inputfile = shift @ARGV;
16 my $outputfile = shift @ARGV;
17
18 my %features;
19 my %top;
20 my %files;
21 my $reqfile;
22
23 gather_data($inputfile);
24 if ($opts{'w'}) {
25 print_wiki_mode($outputfile);
26 } else {
27 print_org_mode($outputfile);
28 }
29
30 sub gather_data {
31 my ($inputfile) = @_;
32
33 open(I, $inputfile);
34 while(<I>) {
35 if (/(required|provided|wanted) by (.*) \*/) {
36 $reqfile = $2;
37 } elsif (/define NETSNMP_FEATURE_PROVIDE_(.*) 1/) {
38 my $lc = lc($1);
39 die "no reqfile currently set; bug!" if (!defined($reqfile));
40
41 add($lc);
42
43 push @{$files{$reqfile}{'provides'}}, $lc;
44 push @{$features{$lc}{'providedby'}}, $reqfile;
45
46 } elsif (/define NETSNMP_FEATURE_REQUIRE_(.*) 1/) {
47 my $lc = lc($1);
48 die "no reqfile currently set; bug!" if (!defined($reqfile));
49
50 add($lc);
51
52 push @{$files{$reqfile}{'requires'}}, $lc;
53 push @{$features{$lc}{'requiredby'}}, $reqfile;
54
55 } elsif (/define NETSNMP_FEATURE_WANT_(.*) 1/) {
56 my $lc = lc($1);
57 die "no reqfile currently set; bug!" if (!defined($reqfile));
58
59 add($lc);
60
61 push @{$files{$reqfile}{'wants'}}, $lc;
62 push @{$features{$lc}{'wantedby'}}, $reqfile;
63 } elsif (/define NETSNMP_FEATURE_(.*)_CHILD_OF_(.*) 1/) {
64 my $child = lc($1);
65 my $parent = lc($2);
66
67 add_child($child, $parent);
68 }
69 }
70 }
71
72 sub add {
73 my ($name) = @_;
74 if (!exists($features{$name})) {
75 # new feature entirely, mark it as a top node
76 $top{$name} = 1;
77 }
78 }
79
80 sub add_child {
81 my ($child, $parent) = @_;
82
83 add($parent);
84
85 if (exists($top{$child})) {
86 # it's no longer a top node if it's a child of something else
87 delete $top{$child};
88 }
89 $features{$parent}{'children'}{$child}++;
90
91 $features{$child}{'providedby'}, $reqfile;
92 }
93
94 ######################################################################
95 # org-mode output
96 #
97 sub print_org_mode {
98 my ($outputfile) = @_;
99
100 my $fh = new IO::File;
101
102 if (!$fh->open("> $outputfile")) {
103 die "error!\n";
104 }
105
106 foreach my $node (sort keys(%top)) {
107 print_org_node($fh, $node, "*");
108 }
109 }
110
111 sub print_org_node {
112 my ($fh, $node, $prefix) = @_;
113
114 my $spaces = $prefix;
115 $spaces =~ s/./ /g;
116
117 print $fh "$prefix $node\n";
118 if (exists($features{$node}{'providedby'})) {
119 print $fh "$spaces + provided in file: " .
120 join(", ", org_link_files(@{$features{$node}{'providedby'}})) . "\n";
121 }
122 if (exists($features{$node}{'requiredby'})) {
123 print $fh "$spaces + required in file: " .
124 join(", ", org_link_files(@{$features{$node}{'requiredby'}})) . "\n";
125 }
126 if (exists($features{$node}{'wantedby'})) {
127 print $fh "$spaces + wanted in file: " .
128 join(", ", org_link_files(@{$features{$node}{'wantedby'}})) . "\n";
129 }
130 if (exists($features{$node}{'children'})) {
131 foreach my $child (sort keys(%{$features{$node}{'children'}})) {
132 print_org_node($fh, $child, $prefix . "*");
133 }
134 }
135 }
136
137 sub org_link_files {
138 my @files = @_;
139 map { $_ = "[[file:$_][$_]]"; } @files;
140 return @files;
141 }
142
143 ######################################################################
144 # wiki output
145 #
146 sub print_wiki_mode {
147 my ($outputfile) = @_;
148
149 my $fh = new IO::File;
150 my $depth = 0;
151
152 if (!$fh->open("> $outputfile")) {
153 die "error!\n";
154 }
155
156 print $fh "'''The contents of this page is auto-generated from local/minimalist/feature-makedocs; do not edit by hand (changes will be lost)'''\n\n";
157 print $fh "Details of the feature marking system and how it is used can be found at [[Feature Marking and Selection]]\n\n";
158
159 foreach my $node (sort keys(%top)) {
160 print_wiki_node($fh, $node, $depth+1);
161 }
162 }
163
164 sub print_wiki_node {
165 my ($fh, $node, $depth) = @_;
166
167 my $dataheader;
168 my $depthincrease = 1;
169
170 if ($depth > $opts{'W'}) {
171 print $fh "*" x ($depth - $opts{'W'} + 1) . " $node\n";
172 $dataheader = "*" x ($depth - $opts{'W'} + 2);
173 $depthincrease++;
174 } else {
175 print $fh "=" x $depth . " $node " . "=" x $depth . "\n";
176 $dataheader = "*";
177 }
178
179 if (exists($features{$node}{'providedby'})) {
180 print $fh "$dataheader provided in file: " .
181 join(", ", wiki_link_files(@{$features{$node}{'providedby'}})) ."\n";
182 }
183 if (exists($features{$node}{'requiredby'})) {
184 print $fh "$dataheader required in file: " .
185 join(", ", wiki_link_files(@{$features{$node}{'requiredby'}})) ."\n";
186 }
187 if (exists($features{$node}{'wantedby'})) {
188 print $fh "$dataheader wanted in file: " .
189 join(", ", wiki_link_files(@{$features{$node}{'wantedby'}})) . "\n";
190 }
191 if (exists($features{$node}{'children'})) {
192 if ($depth >= $opts{'W'}) {
193 print $fh "$dataheader Children:\n";
194 }
195 foreach my $child (sort keys(%{$features{$node}{'children'}})) {
196 print_wiki_node($fh, $child, $depth + $depthincrease);
197 }
198 }
199 }
200
201 sub wiki_link_files {
202 my @files = @_;
203 map { $_ = "[http://net-snmp.svn.sourceforge.net/viewvc/net-snmp/trunk/net-snmp/$_?view=markup $_]"; } @files;
204 return @files;
205 }
206
207 ######################################################################
208 # help output
209 #
210 sub usage {
211 print "Usage: $0 [FLAGS] INPUTFILE OUTPUTFILE\n\n";
212 print "FLAGS:\n";
213 print "\t-h\thelp\n";
214 print "\t-o\tOutput style: Org-Mode [default]\n";
215 print "\t-w\tOutput style: wiki\n";
216 print "\t-W DEPTH\tWiki header to bullet list depth (default 2)\n";
217 print "\nINPUT/OUTPUT\n";
218 print "\tINPUTFILE:\tlocation of the include/net-snmp/feature-details.h file\n";
219 print "\tOUTPUTFILE:\tthe file to write the results to\n";
220 exit;
221 }