6 ## Copyright (C) 2015 Intel Corporation ##
8 ## This software falls under the GNU General Public License. ##
9 ## Please read the COPYING file for more information ##
12 # This software reads a XML file and a list of valid interal
13 # references to replace Docbook tags with links.
15 # The list of "valid internal references" must be one-per-line in the following format:
20 # The software walks over the XML file looking for xml tags representing possible references
21 # to the Document. Each reference will be cross checked against the "Valid Internal Reference" list. If
22 # the referece is found it replaces its content by a <link> tag.
25 # kernel-doc-xml-ref -db filename
26 # xml filename > outputfile
33 #Holds the database filename
42 "<function>(.*?)</function>",
43 "\"<function>\" . convert_function(\$1, \$line) . \"</function>\"",
44 "<structname>(.*?)</structname>",
45 "\"<structname>\" . convert_struct(\$1) . \"</structname>\"",
46 "<funcdef>(.*?)<function>(.*?)</function></funcdef>",
47 "\"<funcdef>\" . convert_param(\$1) . \"<function>\$2</function></funcdef>\"",
48 "<paramdef>(.*?)<parameter>(.*?)</parameter></paramdef>",
49 "\"<paramdef>\" . convert_param(\$1) . \"<parameter>\$2</parameter></paramdef>\"");
51 while($ARGV[0] =~ m/^-(.*)/) {
52 my $cmd = shift @ARGV;
54 $databasefile = shift @ARGV
59 $inputfile = shift @ARGV;
62 open (my $handle, '<', $databasefile) or die "Cannot open $databasefile";
63 chomp(my @lines = <$handle>);
73 foreach my $pattern (keys %highlights) {
74 $dohighlight .= "\$line =~ s:$pattern:$highlights{$pattern}:eg;\n";
77 open(FILE, $inputfile) or die("Could not open $inputfile") or die ("Cannot open $inputfile");
78 foreach my $line (<FILE>) {
87 $str =~ s/^\s+|\s+$//g;
91 sub has_key_defined($_)
93 if ( grep( /^$_[0]$/, @database)) {
99 # Gets a <function> content and add it a hyperlink if possible.
100 sub convert_function($_)
109 $key =~ s/[^A-Za-z0-9]/-/g;
110 $key = "API-" . $key;
112 # We shouldn't add links to <funcdef> prototype
113 if (!has_key_defined($key) || $line =~ m/\s+<funcdef/i) {
119 if ($arg =~ /(.*?)( ?)$/) {
123 return "<link linkend=\"$key\">$head</link>$tail";
126 # Converting a struct text to link
127 sub convert_struct($_)
131 $key =~ s/(struct )?(\w)/$2/g;
132 $key =~ s/[^A-Za-z0-9]/-/g;
133 $key = "API-struct-" . $key;
135 if (!has_key_defined($key)) {
139 my ($head, $tail) = split_pointer($arg);
140 return "<link linkend=\"$key\">$head</link>$tail";
143 # Identify "object *" elements
144 sub split_pointer($_)
147 if ($arg =~ /(.*?)( ?\* ?)/) {
153 sub convert_param($_)
156 my $keyname = convert_key_name($type);
158 if (!has_key_defined($keyname)) {
162 my ($head, $tail) = split_pointer($type);
163 return "<link linkend=\"$keyname\">$head</link>$tail";
167 # DocBook links are in the API-<TYPE>-<STRUCT-NAME> format
168 # This method gets an element and returns a valid DocBook reference for it.
169 sub convert_key_name($_)
171 #Pattern $2 is optional and might be uninitialized
172 no warnings 'uninitialized';
175 $str =~ s/(const|static)? ?(struct)? ?([a-zA-Z0-9_]+) ?(\*|&)?/$2 $3/g ;
178 $str =~ s/^\s+|\s+$//g;
181 $str =~ s/[^A-Za-z0-9]/-/g;
183 return "API-" . $str;
187 print "Usage: $0 -db database filename\n";
188 print " xml source file(s) > outputfile\n";
196 print STDERR "$errors errors\n";