Added British English translation by Jen Ockwell
[gtk-doc.git] / gtkdoc-common.pl.in
blobea6dd34851b144c0a27ec593d69a8e9a229db267
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 2001 Damon Chaplin
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # These are functions used by several of the gtk-doc Perl scripts.
24 # We'll move more of the common routines here eventually, though they need to
25 # stop using global variables first.
31 #############################################################################
32 # Function : UpdateFileIfChanged
33 # Description : Compares the old version of the file with the new version and
34 # if the file has changed it moves the new version into the old
35 # versions place. This is used so we only change files if
36 # needed, so we can do proper dependency tracking and we don't
37 # needlessly check files into CVS that haven't changed.
38 # It returns 0 if the file hasn't changed, and 1 if it has.
39 # Arguments : $old_file - the pathname of the old file.
40 # $new_file - the pathname of the new version of the file.
41 # $make_backup - 1 if a backup of the old file should be kept.
42 # It will have the .bak suffix added to the file name.
43 #############################################################################
45 sub UpdateFileIfChanged {
46 my ($old_file, $new_file, $make_backup) = @_;
48 # print "Comparing $old_file with $new_file...\n";
50 # If the old file doesn't exist we want this to default to 1.
51 my $exit_code = 1;
53 if (-e $old_file) {
54 `cmp -s "$old_file" "$new_file"`;
55 $exit_code = $? >> 8;
56 # print " cmp exit code: $exit_code ($?)\n";
59 if ($exit_code > 1) {
60 die "Error running 'cmp $old_file $new_file'";
63 if ($exit_code == 1) {
64 # print " files changed - replacing old version with new version.\n";
66 if ($make_backup && -e $old_file) {
67 rename ($old_file, "$old_file.bak")
68 || die "Can't move $old_file to $old_file.bak: $!";
71 rename ($new_file, $old_file)
72 || die "Can't move $new_file to $old_file: $!";
74 return 1;
75 } else {
76 # print " files the same - deleting new version.\n";
78 unlink ("$new_file")
79 || die "Can't delete file: $new_file: $!";
81 return 0;
86 #############################################################################
87 # Function : ParseStructDeclaration
88 # Description : This function takes a structure declaration and
89 # breaks it into individual type declarations.
90 # Arguments : $declaration - the declaration to parse
91 # $is_object - true if this is an object structure
92 # $typefunc - function reference to apply to type
93 # $namefunc - function reference to apply to name
94 #############################################################################
96 sub ParseStructDeclaration {
97 my ($declaration, $is_object, $output_function_params, $typefunc, $namefunc) = @_;
99 # For forward struct declarations just return an empty array.
100 if ($declaration =~ m/struct\s+\S+\s*;/msg) {
101 return ();
104 # Remove all private parts of the declaration
106 # For objects, assume private
107 if ($is_object) {
108 $declaration =~ s!(struct\s+\w*\s*\{)
110 (?:/\*\s*<\s*public\s*>\s*\*/|(?=\}))!$1!msgx;
113 # Assume end of declaration if line begins with '}'
114 $declaration =~ s!\n?[ \t]*/\*\s*<\s*(private|protected)\s*>\s*\*/
116 (?:/\*\s*<\s*public\s*>\s*\*/|(?=^\}))!!msgx;
118 # Remove all other comments;
119 $declaration =~ s@/\*([^*]+|\*(?!/))*\*/@ @g;
121 my @result = ();
123 if ($declaration =~ /^\s*$/) {
124 return @result;
127 # Prime match after "struct {" declaration
128 if (!scalar($declaration =~ m/struct\s+\w*\s*\{/msg)) {
129 die "Structure declaration '$declaration' does not begin with struct [NAME] {\n";
132 # Treat lines in sequence, allowing singly nested anonymous structs
133 # and unions.
134 while ($declaration =~ m/\s*([^{;]+(\{[^\}]*\}[^{;]+)?);/msg) {
135 my $line = $1;
137 last if $line =~ /^\s*\}\s*\w*\s*$/;
139 # FIXME: Just ignore nested structs and unions for now
140 next if $line =~ /{/;
142 # ignore preprocessor directives
143 while ($line =~ /^#.*?\n\s*(.*)/msg) {
144 $line=$1;
147 last if $line =~ /^\s*\}\s*\w*\s*$/;
149 # Try to match structure members which are functions
150 if ($line =~ m/^
151 (const\s+|G_CONST_RETURN\s+|unsigned\s+|signed\s+|long\s+|short\s+)*(struct\s+|enum\s+)? # mod1
152 (\w+)\s* # type
153 (\**(?:\s*restrict)?)\s* # ptr1
154 (const\s+)? # mod2
155 (\**\s*) # ptr2
156 (const\s+)? # mod3
157 \(\s*\*\s*(\w+)\s*\)\s* # name
158 \(([^)]*)\)\s* # func_params
159 $/x) {
161 my $mod1 = defined($1) ? $1 : "";
162 if (defined($2)) { $mod1 .= $2; }
163 my $type = $3;
164 my $ptr1 = $4;
165 my $mod2 = defined($5) ? $5 : "";
166 my $ptr2 = $6;
167 my $mod3 = defined($7) ? $7 : "";
168 my $name = $8;
169 my $func_params = $9;
170 my $ptype = defined $typefunc ? $typefunc->($type) : $type;
171 my $pname = defined $namefunc ? $namefunc->($name) : $name;
173 push @result, $name;
175 if ($output_function_params) {
176 push @result, "$mod1$ptype$ptr1$mod2$ptr2$mod3 (*$pname) ($func_params)";
177 } else {
178 push @result, "$pname&#160;()";
182 # Try to match normal struct fields of comma-separated variables/
183 } elsif ($line =~ m/^
184 ((?:const\s+|volatile\s+|unsigned\s+|signed\s+|short\s+|long\s+)?)(struct\s+|enum\s+)? # mod1
185 (\w+)\s* # type
186 (\** \s* const\s+)? # mod2
187 (.*) # variables
188 $/x) {
190 my $mod1 = defined($1) ? $1 : "";
191 if (defined($2)) { $mod1 .= $2; }
192 my $type = $3;
193 my $ptype = defined $typefunc ? $typefunc->($type) : $type;
194 my $mod2 = defined($4) ? " " . $4 : "";
195 my $list = $5;
197 #print "'$mod1' '$type' '$mod2' '$list' \n";
199 $mod1 =~ s/ /&#160;/g;
200 $mod2 =~ s/ /&#160;/g;
202 my @names = split /,/, $list;
203 for my $n (@names) {
204 # Each variable can have any number of '*' before the
205 # identifier, and be followed by any number of pairs of
206 # brackets or a bit field specifier.
207 # e.g. *foo, ***bar, *baz[12][23], foo : 25.
208 if ($n =~ m/^\s* (\**(?:\s*restrict\b)?) \s* (\w+) \s* (?: ((?:\[[^\]]*\]\s*)+) | (:\s*\d+)?) \s* $/x) {
209 my $ptrs = $1;
210 my $name = $2;
211 my $array = defined($3) ? $3 : "";
212 my $bits = defined($4) ? " $4" : "";
214 if ($ptrs && $ptrs !~ m/\*$/) { $ptrs .= " "; }
215 $array =~ s/ /&#160;/g;
216 $bits =~ s/ /&#160;/g;
218 push @result, $name;
219 if (defined $namefunc) {
220 $name = $namefunc->($name);
222 push @result, "$mod1$ptype$mod2&#160;$ptrs$name$array$bits;";
224 #print "***** Matched line: $mod1$ptype$mod2 $ptrs$name$array$bits\n";
225 } else {
226 print "WARNING: Couldn't parse struct field: $n\n";
230 } else {
231 warn "Cannot parse structure field \"$line\"";
235 return @result;
239 #############################################################################
240 # Function : ParseEnumDeclaration
241 # Description : This function takes a enumeration declaration and
242 # breaks it into individual enum member declarations.
243 # Arguments : $declaration - the declaration to parse
244 #############################################################################
246 sub ParseEnumDeclaration {
247 my ($declaration, $is_object) = @_;
249 # For forward enum declarations just return an empty array.
250 if ($declaration =~ m/enum\s+\S+\s*;/msg) {
251 return ();
254 # Remove comments;
255 $declaration =~ s@/\*([^*]+|\*(?!/))*\*/@ @g;
257 my @result = ();
259 if ($declaration =~ /^\s*$/) {
260 return @result;
263 # Remove parenthesized expressions (in macros like GTK_BLAH = BLAH(1,3))
264 # to avoid getting confused by commas they might contain. This
265 # doesn't handle nested parentheses correctly.
267 $declaration =~ s/\([^)]+\)//g;
269 # Remove comma from comma - possible whitespace - closing brace sequence
270 # since it is legal in GNU C and C99 to have a trailing comma but doesn't
271 # result in an actual enum member
273 $declaration =~ s/,(\s*})/$1/g;
275 # Prime match after "typedef enum {" declaration
276 if (!scalar($declaration =~ m/(typedef\s+)?enum\s*(\S+\s*)?\{/msg)) {
277 die "Enum declaration '$declaration' does not begin with 'typedef enum {' or 'enum XXX {'\n";
280 # Treat lines in sequence.
281 while ($declaration =~ m/\s*([^,\}]+)([,\}])/msg) {
282 my $line = $1;
283 my $terminator = $2;
285 # ignore preprocessor directives
286 while ($line =~ /^#.*?\n\s*(.*)/msg) {
287 $line=$1;
290 if ($line =~ m/^(\w+)\s*(=.*)?$/msg) {
291 push @result, $1;
293 # Special case for GIOCondition, where the values are specified by
294 # macros which expand to include the equal sign like '=1'.
295 } elsif ($line =~ m/^(\w+)\s*GLIB_SYSDEF_POLL/msg) {
296 push @result, $1;
298 # Special case include of <gdk/gdkcursors.h>, just ignore it
299 } elsif ($line =~ m/^#include/) {
300 last;
302 # Special case for #ifdef/#else/#endif, just ignore it
303 } elsif ($line =~ m/^#(?:if|else|endif)/) {
304 last;
306 } else {
307 warn "Cannot parse enumeration member \"$line\"";
310 last if $terminator eq '}';
313 return @result;
316 #############################################################################
317 # Function : LogWarning
318 # Description : Log a warning in gcc style format
319 # Arguments : $file - the file the error comes from
320 # $line - line number for the wrong entry
321 # $message - description of the issue
322 #############################################################################
323 sub LogWarning {
324 my ($file, $line, $message) = @_;
326 $file="unknown" if !defined($file);
327 $line="0" if !defined($line);
329 print "$file:$line: warning: $message\n"