BFU: Fix searching in menus past unselectable items.
[elinks.git] / doc / tools / code2doc
blobf91ef8fd6892191c1e5ddb8822204fd12f71ea74
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use diagnostics;
5 use Getopt::Std;
7 my $HELP = "Usage: $0 [FILE]...
8 Parses [FILE], outputing the result to stdout.";
10 sub usage {
11 print "@_\n";
12 exit;
15 our($opt_h, $opt_v, $title, $body, $indent, $idpath, $inblock);
17 getopts("hv") or usage($HELP);
18 $opt_v and usage("Copyleft (c) 2006, Russ Rowan (See `COPYING')");
19 usage($HELP) if $opt_h or @ARGV < 1;
21 sub put_section {
22 if ($title) {
23 print "\n$title\n";
24 $title =~ s/[^-]/-/g;
25 print "$title\n" if not $indent;
27 if ($body) {
28 $body =~ s/#newline#/$indent/g;
29 print "$body\n";
31 $title = $body = undef;
34 $idpath = $title = $body = "";
35 while (<>)
37 my $end = s/\s*\*+\//\n/ ? 'yes' : undef;
39 if ($end and /[^=]*[\s*](\w+)[\s:,;].*\/\*:\s*(.*)([.]\s*)?$/) {
40 # Implicit id for enum values and struct members.
41 print "\nid:[$idpath$1]::\n\t$2.\n";
43 } elsif ($inblock) {
44 # Redo the indentation, preserve empty lines.
45 s/^(\s|\*)*//;
46 s/^$/\n/;
47 $body .= "#newline#" . $_;
49 } elsif (s/\s*\/\*\*+\s*(.*)/$1/) {
50 # Found magic header; flush, record title and set indentation.
51 put_section;
52 $title = "$1";
53 $indent = /::/ ? "\t" : "";
55 } else {
56 next if not ($title or $body) or /^\s$/;
58 my $orig_title = $title;
61 # Combine multi-line declarations to one. Break at empty
62 # for #define ...
63 while (not /(struct|enum|typedef|[^=])*[\s*](\w+).*[\[:,;{]/) {
64 my $line = $_;
65 $_ .= <>;
66 last if $_ eq $line;
69 if (/struct\s+(\w+)\s*{/) {
70 $title = "struct:$1" . "[$title]";
71 $idpath = "$1.";
73 } elsif (/enum\s+(\w+)\s*{/) {
74 $title = "enum:$1" . "[$title]";
75 $idpath = "";
77 } elsif (/#define/) {
78 if (/#define\s+(\w+)[(]/) {
79 $title = "func:$1" . "[$title]";
80 } elsif (/#define\s+(\w+)/) {
81 $title = "macro:$1" . "[$title]";
83 $idpath = "";
85 } elsif (/typedef/) {
86 if (/.*\(\*(\w+)\)\(/) {
87 $title = "typedef:$1" . "[$title]";
88 } elsif (/typedef.*\s(\w+);/) {
89 $title = "typedef:$1" . "[$title]";
91 $idpath = "";
93 } elsif (/.*[\s*](\w+)\(/) {
94 $title = "func:$1" . "[$title]";
95 $idpath = "";
97 } elsif (/.*\(\*(\w+)\)\(/) {
98 $body = "#newline#" . $title if not $body;
99 $title = "id:[$idpath$1]::";
100 $indent = "\t";
102 } elsif (/[^=]*[\s*](\w+)[\[\s,:;]/) {
103 $body = "#newline#" . $title if not $body;
104 $title = "id:[$idpath$1]::";
105 $indent = "\t";
107 put_section if $orig_title ne $title;
108 next;
111 $inblock = $end ? undef : 'yes';