Accept "char const *" as well. Fixes #544172.
[gtk-doc.git] / gtkdoc-mkdb.in
blob3f0f499bda9d04460b1287d7702655ae9a27bb7a
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #############################################################################
23 # Script      : gtkdoc-mkdb
24 # Description : This creates the DocBook files from the edited templates.
26 #               NOTE: When creating SGML IDS, we append ":CAPS" to all
27 #               all-caps identifiers to prevent name clashes. (It basically
28 #               never is the case that mixed-case identifiers would collide.)
29 #               See the CreateValidSGMLID function.
30 #############################################################################
32 use strict;
33 use Getopt::Long;
35 unshift @INC, '@PACKAGE_DATA_DIR@';
36 require "gtkdoc-common.pl";
38 # Options
40 # name of documentation module
41 my $MODULE;
42 my $TMPL_DIR;
43 my $SGML_OUTPUT_DIR;
44 my @SOURCE_DIRS;
45 my $SOURCE_SUFFIXES = "";
46 my $IGNORE_FILES = "";
47 my $PRINT_VERSION;
48 my $PRINT_HELP;
49 my $OUTPUT_ALL_SYMBOLS;
50 my $MAIN_SGML_FILE;
51 my $EXPAND_CONTENT_FILES = "";
52 my $SGML_MODE;
53 my $DEFAULT_STABILITY;
54 my $DEFAULT_INCLUDES;
55 my $OUTPUT_FORMAT;
57 my %optctl = ('module' => \$MODULE,
58               'source-dir' => \@SOURCE_DIRS,
59               'source-suffixes' => \$SOURCE_SUFFIXES,
60               'ignore-files' => \$IGNORE_FILES,
61               'output-dir' => \$SGML_OUTPUT_DIR,
62               'tmpl-dir' => \$TMPL_DIR,
63               'version' => \$PRINT_VERSION,
64               'help' => \$PRINT_HELP,
65               'main-sgml-file' => \$MAIN_SGML_FILE,
66               'expand-content-files' => \$EXPAND_CONTENT_FILES,
67               'outputallsymbols' => \$OUTPUT_ALL_SYMBOLS,
68               'sgml-mode' => \$SGML_MODE,
69               'default-stability' => \$DEFAULT_STABILITY,
70               'default-includes' => \$DEFAULT_INCLUDES,
71               'output-format' => \$OUTPUT_FORMAT);
72 GetOptions(\%optctl, "module=s", "source-dir:s", "source-suffixes:s", "ignore-files:s", "output-dir:s", "tmpl-dir:s", "version", "outputallsymbols", "expand-content-files:s", "main-sgml-file:s", "extra-db-files:s", "help", "sgml-mode", "default-stability:s", "default-includes:s", "output-format:s");
74 if ($PRINT_VERSION) {
75     print "@VERSION@\n";
76     exit 0;
79 if (!$MODULE) {
80     $PRINT_HELP = 1;
83 if ($DEFAULT_STABILITY && $DEFAULT_STABILITY ne "Stable"
84     && $DEFAULT_STABILITY ne "Private" && $DEFAULT_STABILITY ne "Unstable") {
85     $PRINT_HELP = 1;
88 if ($PRINT_HELP) {
89     print <<EOF;
90 gtkdoc-mkdb version @VERSION@
92 --module=MODULE_NAME       Name of the doc module being parsed
93 --source-dir=DIRNAME       Directories which contain inline reference material
94 --source-suffixes=SUFFIXES Suffixes of source files to scan, comma-separated
95 --ignore-files=FILES       Files or directories which should not be scanned
96                            May be used more than once for multiple directories
97 --output-dir=DIRNAME       Directory to put the generated DocBook files in
98 --tmpl-dir=DIRNAME         Directory in which template files may be found
99 --main-sgml-file=FILE      File containing the toplevel DocBook file.
100 --expand-content-files=FILES Extra DocBook files to expand abbreviations in.
101 --output-format=FORMAT     Format to use for the generated docbook, XML or SGML.
102 --sgml-mode                Allow DocBook markup in inline documentation.
103 --default-stability=LEVEL  Specify default stability Level. Valid values are
104                            Stable, Unstable, or Private.
105 --default-includes=FILENAMES Specify default includes for section Synopsis
106 --version                  Print the version of this program
107 --help                     Print this help
109     exit 0;
112 my ($empty_element_end, $doctype_header);
114 if (lc($OUTPUT_FORMAT) eq "xml") {
115     if (!$MAIN_SGML_FILE) {
116         # backwards compatibility
117         if (-e "${MODULE}-docs.sgml") {
118             $MAIN_SGML_FILE="${MODULE}-docs.sgml";
119         } else {
120             $MAIN_SGML_FILE="${MODULE}-docs.xml";
121         }
122     }
123     $OUTPUT_FORMAT = "xml";
124     $empty_element_end = "/>";
126     if (-e $MAIN_SGML_FILE) {
127         open(INPUT, "<$MAIN_SGML_FILE") || die "Can't open $MAIN_SGML_FILE";
128         $doctype_header = "";
129         while (<INPUT>) {
130             if (/^\s*<(book|chapter|article)/) {
131                 if ($_ !~ m/http:\/\/www.w3.org\/200[13]\/XInclude/) {
132                     $doctype_header = "";
133                 }
134                 last;
135             }
136             $doctype_header .= $_;
137         }
138         close(INPUT);
139     } else {
140         $doctype_header =
141 "<?xml version=\"1.0\"?>\n" .
142 "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\"\n" .
143 "               \"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\">\n";
144     }
145     $doctype_header =~ s/<!DOCTYPE \w+/<!DOCTYPE refentry/;
146 } else {
147     if (!$MAIN_SGML_FILE) {
148         $MAIN_SGML_FILE="${MODULE}-docs.sgml";
149     }
150     $OUTPUT_FORMAT = "sgml";
151     $doctype_header = "";
152     $empty_element_end = ">";
155 my $ROOT_DIR = ".";
157 # All the files are written in subdirectories beneath here.
158 $TMPL_DIR = $TMPL_DIR ? $TMPL_DIR : "$ROOT_DIR/tmpl";
160 # This is where we put all the DocBook output.
161 $SGML_OUTPUT_DIR = $SGML_OUTPUT_DIR ? $SGML_OUTPUT_DIR : "$ROOT_DIR/$OUTPUT_FORMAT";
163 # This file contains the object hierarchy.
164 my $OBJECT_TREE_FILE = "$ROOT_DIR/$MODULE.hierarchy";
166 # This file contains the interfaces.
167 my $INTERFACES_FILE = "$ROOT_DIR/$MODULE.interfaces";
169 # This file contains the prerequisites.
170 my $PREREQUISITES_FILE = "$ROOT_DIR/$MODULE.prerequisites";
172 # This file contains signal arguments and names.
173 my $SIGNALS_FILE = "$ROOT_DIR/$MODULE.signals";
175 # The file containing Arg information.
176 my $ARGS_FILE = "$ROOT_DIR/$MODULE.args";
178 # These global arrays store information on signals. Each signal has an entry
179 # in each of these arrays at the same index, like a multi-dimensional array.
180 my @SignalObjects;      # The GtkObject which emits the signal.
181 my @SignalNames;        # The signal name.
182 my @SignalReturns;      # The return type.
183 my @SignalFlags;        # Flags for the signal
184 my @SignalPrototypes;   # The rest of the prototype of the signal handler.
186 # These global arrays store information on Args. Each Arg has an entry
187 # in each of these arrays at the same index, like a multi-dimensional array.
188 my @ArgObjects;         # The GtkObject which has the Arg.
189 my @ArgNames;           # The Arg name.
190 my @ArgTypes;           # The Arg type - gint, GtkArrowType etc.
191 my @ArgFlags;           # How the Arg can be used - readable/writable etc.
192 my @ArgNicks;           # The nickname of the Arg.
193 my @ArgBlurbs;          # Docstring of the Arg.
194 my @ArgDefaults;        # Default value of the Arg.
195 my @ArgRanges;          # The range of the Arg type
196 # These global hashes store declaration info keyed on a symbol name.
197 my %Declarations;
198 my %DeclarationTypes;
199 my %DeclarationConditional;
200 my %DeclarationOutput;
201 my %Deprecated;
202 my %Since;
203 my %StabilityLevel;
204 my %StructHasTypedef;
206 # These global hashes store the existing documentation.
207 my %SymbolDocs;
208 my %SymbolTypes;
209 my %SymbolParams;
210 my %SymbolSourceFile;
211 my %SymbolSourceLine;
213 # These global hashes store documentation scanned from the source files.
214 my %SourceSymbolDocs;
215 my %SourceSymbolParams;
216 my %SourceSymbolSourceFile;
217 my %SourceSymbolSourceLine;
219 # all documentation goes in here, so we can do coverage analysis
220 my %AllSymbols;
221 my %AllIncompleteSymbols;
222 my %AllDocumentedSymbols;
224 # Undeclared yet documented symbols
225 my %UndeclaredSymbols;
227 # These global arrays store GtkObject and subclasses and the hierarchy.
228 my @Objects;
229 my @ObjectLevels;
231 my %Interfaces;
232 my %Prerequisites;
234 # holds the symbols which are mentioned in $MODULE-sections.txt
235 my %KnownSymbols;
237 # Standard C preprocessor directives, which we ignore for '#' abbreviations.
238 my %PreProcessorDirectives;
239 $PreProcessorDirectives{"assert"} = 1;
240 $PreProcessorDirectives{"define"} = 1;
241 $PreProcessorDirectives{"elif"} = 1;
242 $PreProcessorDirectives{"else"} = 1;
243 $PreProcessorDirectives{"endif"} = 1;
244 $PreProcessorDirectives{"error"} = 1;
245 $PreProcessorDirectives{"if"} = 1;
246 $PreProcessorDirectives{"ifdef"} = 1;
247 $PreProcessorDirectives{"ifndef"} = 1;
248 $PreProcessorDirectives{"include"} = 1;
249 $PreProcessorDirectives{"line"} = 1;
250 $PreProcessorDirectives{"pragma"} = 1;
251 $PreProcessorDirectives{"unassert"} = 1;
252 $PreProcessorDirectives{"undef"} = 1;
253 $PreProcessorDirectives{"warning"} = 1;
255 # Create the root DocBook output directory if it doens't exist.
256 if (! -e $SGML_OUTPUT_DIR) {
257     mkdir ("$SGML_OUTPUT_DIR", 0777)
258         || die "Can't create directory: $SGML_OUTPUT_DIR";
261 # Function and other declaration output settings.
262 my $RETURN_TYPE_FIELD_WIDTH = 20;
263 my $SYMBOL_FIELD_WIDTH = 36;
264 my $SIGNAL_FIELD_WIDTH = 16;
266 &ReadKnownSymbols ("$ROOT_DIR/$MODULE-sections.txt");
267 &ReadSignalsFile ($SIGNALS_FILE);
268 &ReadArgsFile ($ARGS_FILE);
269 &ReadObjectHierarchy;
270 &ReadInterfaces;
271 &ReadPrerequisites;
273 &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-decl.txt", 0);
274 if (-f "$ROOT_DIR/$MODULE-overrides.txt") {
275     &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-overrides.txt", 1);
278 for my $dir (@SOURCE_DIRS) {
279     &ReadSourceDocumentation ($dir);
282 my $changed = &OutputSGML ("$ROOT_DIR/$MODULE-sections.txt");
284 # If any of the DocBook SGML files have changed, update the timestamp file (so
285 # it can be used for Makefile dependencies).
286 if ($changed || ! -e "$ROOT_DIR/sgml.stamp") {
287     open (TIMESTAMP, ">$ROOT_DIR/sgml.stamp")
288         || die "Can't create $ROOT_DIR/sgml.stamp: $!";
289     print (TIMESTAMP "timestamp");
290     close (TIMESTAMP);
293 #############################################################################
294 # Function    : OutputObjectList
295 # Description : This outputs the alphabetical list of objects, in a columned
296 #               table. FIXME: Currently this also outputs ancestor objects
297 #               which may not actually be in this module.
298 # Arguments   : none
299 #############################################################################
301 sub OutputObjectList {
302     my $cols = 3;
303     my $old_object_index = "$SGML_OUTPUT_DIR/object_index.sgml";
304     my $new_object_index = "$SGML_OUTPUT_DIR/object_index.new";
306     open (OUTPUT, ">$new_object_index")
307         || die "Can't create $new_object_index: $!";
308     print (OUTPUT <<EOF);
309 <informaltable pgwide="1" frame="none">
310 <tgroup cols="$cols">
311 <colspec colwidth="1*"${empty_element_end}
312 <colspec colwidth="1*"${empty_element_end}
313 <colspec colwidth="1*"${empty_element_end}
314 <tbody>
317     my $count = 0;
318     my $object;
319     foreach $object (sort(@Objects)) {
320         my $xref = &MakeXRef ($object);
321         if ($count % $cols == 0) { print (OUTPUT "<row>\n"); }
322         print (OUTPUT "<entry>$xref</entry>\n");
323         if ($count % $cols == ($cols - 1)) { print (OUTPUT "</row>\n"); }
324         $count++;
325     }
326     if ($count == 0) {
327         # emit an empty row, since empty tables are invalid
328         print (OUTPUT "<row><entry> </entry></row>\n");
329     }
331     print (OUTPUT <<EOF);
332 </tbody></tgroup></informaltable>
334     close (OUTPUT);
336     &UpdateFileIfChanged ($old_object_index, $new_object_index, 0);
340 #############################################################################
341 # Function    : OutputSGML
342 # Description : This collects the output for each section of the docs, and
343 #               outputs each file when the end of the section is found.
344 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
345 #               the functions/macros/structs etc. being documented, organised
346 #               into sections and subsections.
347 #############################################################################
349 sub OutputSGML {
350     my ($file) = @_;
352     #print "Reading: $file\n";
353     open (INPUT, $file)
354         || die "Can't open $file: $!";
355     my $filename = "";
356     my $book_top = "";
357     my $book_bottom = "";
358     my $includes = (defined $DEFAULT_INCLUDES) ? $DEFAULT_INCLUDES : "";
359     my $section_includes = "";
360     my $in_section = 0;
361     my $title = "";
362     my $subsection = "";
363     my $synopsis;
364     my $details;
365     my $num_symbols;
366     my $changed = 0;
367     my $signals_synop = "";
368     my $signals_desc = "";
369     my $args_synop = "";
370     my $child_args_synop = "";
371     my $style_args_synop = "";
372     my $args_desc = "";
373     my $child_args_desc = "";
374     my $style_args_desc = "";
375     my $hierarchy = "";
376     my $interfaces = "";
377     my $implementations = "";
378     my $prerequisites = "";
379     my $derived = "";
380     my @file_objects = ();
381     my %templates = ();
382     my %symbols = ();
384     # merge the source docs, in case there are no templates
385     &MergeSourceDocumentation;
387     while (<INPUT>) {
388         if (m/^#/) {
389             next;
391         } elsif (m/^<SECTION>/) {
392             $synopsis = "";
393             $details = "";
394             $num_symbols = 0;
395             $in_section = 1;
396             @file_objects = ();
397             %symbols = ();
399         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
400             $synopsis .= "\n";
401             $subsection = $1;
403         } elsif (m/^<SUBSECTION>/) {
405         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
406             $title = $1;
407             #print "Section: $title\n";
409             # We don't want warnings if object & class structs aren't used.
410             $DeclarationOutput{$title} = 1;
411             $DeclarationOutput{"${title}Class"} = 1;
413         } elsif (m/^<FILE>(.*)<\/FILE>/) {
414             $filename = $1;
415             if (! defined $templates{$filename}) {
416                if (&ReadTemplateFile ("$TMPL_DIR/$filename", 1)) {
417                    &MergeSourceDocumentation;
418                    $templates{$filename}=$.;
419                }
420             } else {
421                 &LogWarning ($file, $., "Double <FILE>$filename</FILE> entry. ".
422                     "Previous occurrence on line ".$templates{$filename}.".");
423             }
425         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
426             if ($in_section) {
427                 $section_includes = $1;
428             } else {
429                 if (defined $DEFAULT_INCLUDES) {
430                     &LogWarning ($file, $., "Default <INCLUDE> being overridden by command line option.");
431                 }
432                 else {
433                     $includes = $1;
434                 }
435             }
437         } elsif (m/^<\/SECTION>/) {
438             if($title eq "" && $filename eq "") {
439                 &LogWarning ($file, $., "Section has no title and no file.");
440             }
441             # FIXME: one of those would be enough
442             if ($title eq "") {
443                 $title = $filename;
444             }
445             if ($filename eq "") {
446                 $filename = $title;
447             }
448             #print "End of section: $title\n";
450             $filename =~ s/\s/_/g;
452             my $section_id = $SourceSymbolDocs{"$TMPL_DIR/$filename:Section_Id"};
453             if (defined ($section_id) && $section_id !~ m/^\s*$/) {
454                 # Do nothing. Use section_id as it is.
455             } elsif (&CheckIsObject ($title)) {
456                 # GtkObjects use their class name as the ID.
457                 $section_id = &CreateValidSGMLID ($title);
458             } else {
459                 $section_id = &CreateValidSGMLID ("$MODULE-$title");
460             }
462             if ($num_symbols > 0) {
463                 if (lc($OUTPUT_FORMAT) eq "xml") {
464                     $book_bottom .= "    <xi:include href=\"xml/$filename.xml\"/>\n";
465                 } else {
466                     $book_top.="<!ENTITY $section_id SYSTEM \"sgml/$filename.sgml\">\n";
467                     $book_bottom .= "    &$section_id;\n";
468                 }
470                 if (defined ($SourceSymbolDocs{"$TMPL_DIR/$filename:Include"})) {
471                     if ($section_includes) {
472                         &LogWarning ($file, $., "Section <INCLUDE> being overridden by inline comments.");
473                     }
474                     $section_includes = $SourceSymbolDocs{"$TMPL_DIR/$filename:Include"};
475                 }
476                 if ($section_includes eq "") {
477                     $section_includes = $includes;
478                 }
480                  $signals_synop =~ s/^\n*//g;
481                  $signals_synop =~ s/\n+$/\n/g;
482                 if ($signals_synop ne '') {
483                     $signals_synop = <<EOF;
484 <refsect1 id="$section_id.signals" role="signal_proto">
485 <title role="signal_proto.title">Signals</title>
486 <synopsis>
487 ${signals_synop}</synopsis>
488 </refsect1>
490                      $signals_desc =~ s/^\n*//g;
491                      $signals_desc =~ s/\n+$/\n/g;
492                     $signals_desc  = <<EOF;
493 <refsect1 id="$section_id.signal-details" role="signals">
494 <title role="signals.title">Signal Details</title>
495 $signals_desc
496 </refsect1>
498                 }
500                  $args_synop =~ s/^\n*//g;
501                  $args_synop =~ s/\n+$/\n/g;
502                 if ($args_synop ne '') {
503                     $args_synop = <<EOF;
504 <refsect1 id="$section_id.properties" role="properties">
505 <title role="properties.title">Properties</title>
506 <synopsis>
507 ${args_synop}</synopsis>
508 </refsect1>
510                      $args_desc =~ s/^\n*//g;
511                      $args_desc =~ s/\n+$/\n/g;
512                     $args_desc  = <<EOF;
513 <refsect1 id="$section_id.property-details" role="property_details">
514 <title role="property_details.title">Property Details</title>
515 $args_desc
516 </refsect1>
518                 }
520                  $child_args_synop =~ s/^\n*//g;
521                  $child_args_synop =~ s/\n+$/\n/g;
522                 if ($child_args_synop ne '') {
523                     $args_synop .= <<EOF;
524 <refsect1 id="$section_id.child-properties" role="child_properties">
525 <title role="child_properties.title">Child Properties</title>
526 <synopsis>
527 ${child_args_synop}</synopsis>
528 </refsect1>
530                      $args_desc =~ s/^\n*//g;
531                      $args_desc =~ s/\n+$/\n/g;
532                     $args_desc .= <<EOF;
533 <refsect1 id="$section_id.child-property-details" role="child_property_details">
534 <title role="child_property_details.title">Child Property Details</title>
535 $child_args_desc
536 </refsect1>
538                 }
540                  $style_args_synop =~ s/^\n*//g;
541                  $style_args_synop =~ s/\n+$/\n/g;
542                 if ($style_args_synop ne '') {
543                     $args_synop .= <<EOF;
544 <refsect1 id="$section_id.style-properties" role="style_properties">
545 <title role="style_properties.title">Style Properties</title>
546 <synopsis>
547 ${style_args_synop}</synopsis>
548 </refsect1>
550                      $args_desc =~ s/^\n*//g;
551                      $args_desc =~ s/\n+$/\n/g;
552                     $args_desc .= <<EOF;
553 <refsect1 id="$section_id.style-property-details" role="style_properties_details">
554 <title role="style_properties_details.title">Style Property Details</title>
555 $style_args_desc
556 </refsect1>
558                 }
560                  $hierarchy =~ s/^\n*//g;
561                  $hierarchy =~ s/\n+$/\n/g;
562                 if ($hierarchy ne "") {
563                     $hierarchy = <<EOF;
564 <refsect1 id="$section_id.object-hierarchy" role="object_hierarchy">
565 <title role="object_hierarchy.title">Object Hierarchy</title>
566 $hierarchy
567 </refsect1>
569                 }
571                  $interfaces =~ s/^\n*//g;
572                  $interfaces =~ s/\n+$/\n/g;
573                 if ($interfaces ne "") {
574                     $interfaces = <<EOF;
575 <refsect1 id="$section_id.implemented-interfaces" role="impl_interfaces">
576 <title role="impl_interfaces.title">Implemented Interfaces</title>
577 $interfaces
578 </refsect1>
580                 }
582                  $implementations =~ s/^\n*//g;
583                  $implementations =~ s/\n+$/\n/g;
584                 if ($implementations ne "") {
585                     $implementations = <<EOF;
586 <refsect1 id="$section_id.implementations" role="implementations">
587 <title role="implementations.title">Known Implementations</title>
588 $implementations
589 </refsect1>
591                 }
593                  $prerequisites =~ s/^\n*//g;
594                  $prerequisites =~ s/\n+$/\n/g;
595                 if ($prerequisites ne "") {
596                     $prerequisites = <<EOF;
597 <refsect1 id="$section_id.prerequisites" role="prerequisites">
598 <title role="prerequisites.title">Prerequisites</title>
599 $prerequisites
600 </refsect1>
602                 }
604                  $derived =~ s/^\n*//g;
605                  $derived =~ s/\n+$/\n/g;
606                 if ($derived ne "") {
607                     $derived = <<EOF;
608 <refsect1 id="$section_id.derived-interfaces" role="derived_interfaces">
609 <title role="derived_interfaces.title">Known Derived Interfaces</title>
610 $derived
611 </refsect1>
613                 }
615                 $synopsis =~ s/^\n*//g;
616                 $synopsis =~ s/\n+$/\n/g;
617                 my $file_changed = &OutputSGMLFile ($filename, $title, $section_id,
618                                                     $section_includes,
619                                                     \$synopsis, \$details,
620                                                     \$signals_synop, \$signals_desc,
621                                                     \$args_synop, \$args_desc,
622                                                     \$hierarchy, \$interfaces,
623                                                     \$implementations,
624                                                     \$prerequisites, \$derived,
625                                                     \@file_objects);
626                 if ($file_changed) {
627                     $changed = 1;
628                 }
629             }
630             $title = "";
631             $subsection = "";
632             $in_section = 0;
633             $section_includes = "";
634             $signals_synop = "";
635             $signals_desc = "";
636             $args_synop = "";
637             $child_args_synop = "";
638             $style_args_synop = "";
639             $args_desc = "";
640             $child_args_desc = "";
641             $style_args_desc = "";
642             $hierarchy = "";
643             $interfaces = "";
644             $implementations = "";
645             $prerequisites = "";
646             $derived = "";
648         } elsif (m/^(\S+)/) {
649             my $symbol = $1;
650             #print "  Symbol: $symbol\n";
652             # FIXME: check for duplicate entries
653             if (! defined $symbols{$symbol}) {
654                 my $declaration = $Declarations{$symbol};
655                 if (defined ($declaration)) {
656                     # We don't want standard macros/functions of GtkObjects,
657                     # or private declarations.
658                     if ($subsection ne "Standard" && $subsection ne "Private") {
659                         if (&CheckIsObject ($symbol)) {
660                             push @file_objects, $symbol;
661                         }
662                         my ($synop, $desc) = &OutputDeclaration ($symbol,
663                                                                  $declaration);
664                         my ($sig_synop, $sig_desc) = &GetSignals ($symbol);
665                         my ($arg_synop, $child_arg_synop, $style_arg_synop,
666                             $arg_desc, $child_arg_desc, $style_arg_desc) = &GetArgs ($symbol);
667                         my $hier = &GetHierarchy ($symbol);
668                         my $ifaces = &GetInterfaces ($symbol);
669                         my $impls = &GetImplementations ($symbol);
670                         my $prereqs = &GetPrerequisites ($symbol);
671                         my $der = &GetDerived ($symbol);
672                         $synopsis .= $synop;
673                         $details .= $desc;
674                         $signals_synop .= $sig_synop;
675                         $signals_desc .= $sig_desc;
676                         $args_synop .= $arg_synop;
677                         $child_args_synop .= $child_arg_synop;
678                         $style_args_synop .= $style_arg_synop;
679                         $args_desc .= $arg_desc;
680                         $child_args_desc .= $child_arg_desc;
681                         $style_args_desc .= $style_arg_desc;
682                         $hierarchy .= $hier;
683                         $interfaces .= $ifaces;
684                         $implementations .= $impls;
685                         $prerequisites .= $prereqs;
686                         $derived .= $der;
687                     }
688     
689                     # Note that the declaration has been output.
690                     $DeclarationOutput{$symbol} = 1;
691                 } elsif ($subsection ne "Standard" && $subsection ne "Private") {
692                     $UndeclaredSymbols{$symbol} = 1;
693                     &LogWarning ($file, $., "No declaration found for $symbol.");
694                 }
695                 $num_symbols++;
696                 $symbols{$symbol}=$.;
697             }
698             else {
699                 &LogWarning ($file, $., "Double symbol entry for $symbol. ".
700                     "Previous occurrence on line ".$symbols{$symbol}.".");
701             }
702         }
703     }
704     close (INPUT);
706     &OutputMissingDocumentation;
707     &OutputUndeclaredSymbols;
709     if ($OUTPUT_ALL_SYMBOLS) {
710         &OutputAllSymbols;
711     }
713     for $filename (split (' ', $EXPAND_CONTENT_FILES)) {
714         my $file_changed = &OutputExtraFile ($filename);
715         if ($file_changed) {
716             $changed = 1;
717         }
718     }
720     &OutputBook ($book_top, $book_bottom);
722     return $changed;
726 #############################################################################
727 # Function    : ReadKnownSymbols
728 # Description : This collects the names of non-private symbols from the
729 #               $MODULE-sections.txt file.
730 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
731 #               the functions/macros/structs etc. being documented, organised
732 #               into sections and subsections.
733 #############################################################################
735 sub ReadKnownSymbols {
736     my ($file) = @_;
738     my $subsection = "";
740     #print "Reading: $file\n";
741     open (INPUT, $file)
742         || die "Can't open $file: $!";
744     while (<INPUT>) {
745         if (m/^#/) {
746             next;
748         } elsif (m/^<SECTION>/) {
749             $subsection = "";
751         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
752             $subsection = $1;
754         } elsif (m/^<SUBSECTION>/) {
755             next;
757         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
758             next;
760         } elsif (m/^<FILE>(.*)<\/FILE>/) {
761             next;
763         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
764             next;
766         } elsif (m/^<\/SECTION>/) {
767             next;
769         } elsif (m/^(\S+)/) {
770             my $symbol = $1;
772             if ($subsection ne "Standard" && $subsection ne "Private") {
773                 $KnownSymbols{$symbol} = 1;
774             }
775             else {
776                 $KnownSymbols{$symbol} = 0;
777             }
778         }
779     }
780     close (INPUT);
784 #############################################################################
785 # Function    : OutputDeclaration
786 # Description : Returns the synopsis and detailed description DocBook
787 #               describing one function/macro etc.
788 # Arguments   : $symbol - the name of the function/macro begin described.
789 #               $declaration - the declaration of the function/macro.
790 #############################################################################
792 sub OutputDeclaration {
793     my ($symbol, $declaration) = @_;
795     my $type = $DeclarationTypes {$symbol};
796     if ($type eq 'MACRO') {
797         return &OutputMacro ($symbol, $declaration);
798     } elsif ($type eq 'TYPEDEF') {
799         return &OutputTypedef ($symbol, $declaration);
800     } elsif ($type eq 'STRUCT') {
801         return &OutputStruct ($symbol, $declaration);
802     } elsif ($type eq 'ENUM') {
803         return &OutputEnum ($symbol, $declaration);
804     } elsif ($type eq 'UNION') {
805         return &OutputUnion ($symbol, $declaration);
806     } elsif ($type eq 'VARIABLE') {
807         return &OutputVariable ($symbol, $declaration);
809     } elsif ($type eq 'FUNCTION') {
810         return &OutputFunction ($symbol, $declaration, $type);
811     } elsif ($type eq 'USER_FUNCTION') {
812         return &OutputFunction ($symbol, $declaration, $type);
813     } else {
814         die "Unknown symbol type";
815     }
819 #############################################################################
820 # Function    : OutputSymbolTraits
821 # Description : Returns the Since and StabilityLevel paragraphs for a symbol.
822 # Arguments   : $symbol - the name of the function/macro begin described.
823 #############################################################################
825 sub OutputSymbolTraits {
826     my ($symbol) = @_;
827     my $desc = "";
829     if (exists $Since{$symbol}) {
830         $desc .= "<para role=\"since\">Since $Since{$symbol}</para>";
831     }
832     if (exists $StabilityLevel{$symbol}) {
833         $desc .= "<para role=\"stability\">Stability Level: $StabilityLevel{$symbol}</para>";
834     }
835     return $desc;
839 #############################################################################
840 # Function    : OutputMacro
841 # Description : Returns the synopsis and detailed description of a macro.
842 # Arguments   : $symbol - the macro.
843 #               $declaration - the declaration of the macro.
844 #############################################################################
846 sub OutputMacro {
847     my ($symbol, $declaration) = @_;
848     my $id = &CreateValidSGMLID ($symbol);
849     my $condition = &MakeConditionDescription ($symbol);
850     my $synop = &MakeReturnField("#define") . "<link linkend=\"$id\">$symbol</link>";
851     my $desc;
852     my $padding = "";
853     my $args = "";
854     my $pad;
856     if ($declaration =~ m/^\s*#\s*define(\s+)\w+(\([^\)]*\))/) {
857         $padding = $1;
858         $args = $2;
860         if (length ($symbol) < $SYMBOL_FIELD_WIDTH) {
861             $synop .= (' ' x ($SYMBOL_FIELD_WIDTH - length ($symbol)));
862         }
864         # Try to align all the lines of args correctly.
865         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH + $SYMBOL_FIELD_WIDTH + 1);
866         my $args_padded = $args;
867         $args_padded =~ s/ *\\\n */\n$pad/gm;
868         $synop .= &CreateValidSGML ($args_padded);
869     }
870     $synop .= "\n";
872     my $title = $symbol . ($args ? "()" : "");
873     $desc = "<refsect2 id=\"$id\" role=\"macro\"$condition>\n<title>$title</title>\n";
874     $desc .= MakeIndexterms($symbol, $id);
876     # Don't output the macro definition if is is a conditional macro or it
877     # looks like a function, i.e. starts with "g_" or "_?gnome_", or it is
878     # longer than 2 lines, otherwise we get lots of complicated macros like
879     # g_assert.
880     if (!defined ($DeclarationConditional{$symbol}) && ($symbol !~ m/^g_/)
881         && ($symbol !~ m/^_?gnome_/) && (($declaration =~ tr/\n//) < 2)) {
882         $declaration = &CreateValidSGML ($declaration);
883         $desc .= "<programlisting>$declaration</programlisting>\n";
884     } else {
885         $desc .= "<programlisting>" . &MakeReturnField("#define") . "$symbol";
886         # Align each line so that if should all line up OK.
887         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH - length ("#define "));
888         $args =~ s/\n/\n$pad/gm;
889         $desc .= &CreateValidSGML ($args);
890         $desc .= "</programlisting>\n";
891     }
893     $desc .= &MakeDeprecationNote($symbol);
895     my $parameters = &OutputParamDescriptions ("MACRO", $symbol);
896     my $parameters_output = 0;
898     if (defined ($SymbolDocs{$symbol})) {
899         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
901         # Try to insert the parameter table at the author's desired position.
902         # Otherwise we need to tag it onto the end.
903         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
904           $parameters_output = 1;
905         }
906         $desc .= $symbol_docs;
907     }
909     if ($parameters_output == 0) {
910         $desc .= $parameters;
911     }
913     $desc .= OutputSymbolTraits ($symbol);
914     $desc .= "</refsect2>\n";
915     return ($synop, $desc);
919 #############################################################################
920 # Function    : OutputTypedef
921 # Description : Returns the synopsis and detailed description of a typedef.
922 # Arguments   : $symbol - the typedef.
923 #               $declaration - the declaration of the typedef,
924 #                 e.g. 'typedef unsigned int guint;'
925 #############################################################################
927 sub OutputTypedef {
928     my ($symbol, $declaration) = @_;
929     my $id = &CreateValidSGMLID ($symbol);
930     my $condition = &MakeConditionDescription ($symbol);
931     my $synop = &MakeReturnField("typedef") . "<link linkend=\"$id\">$symbol</link>;\n";
932     my $desc = "<refsect2 id=\"$id\" role=\"typedef\"$condition>\n<title>$symbol</title>\n";
934     $desc .= MakeIndexterms($symbol, $id);
936     if (!defined ($DeclarationConditional{$symbol})) {
937         $declaration = &CreateValidSGML ($declaration);
938         $desc .= "<programlisting>$declaration</programlisting>\n";
939     }
941     $desc .= &MakeDeprecationNote($symbol);
943     if (defined ($SymbolDocs{$symbol})) {
944         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
945     }
946     $desc .= OutputSymbolTraits ($symbol);
947     $desc .= "</refsect2>\n";
948     return ($synop, $desc);
952 #############################################################################
953 # Function    : OutputStruct
954 # Description : Returns the synopsis and detailed description of a struct.
955 #               We check if it is a widget struct, and if so we only output
956 #               parts of it that are noted as public fields.
957 #               We also use a different SGML ID for widget structs, since the
958 #               original ID is used for the entire RefEntry.
959 # Arguments   : $symbol - the struct.
960 #               $declaration - the declaration of the struct.
961 #############################################################################
963 sub OutputStruct {
964     my ($symbol, $declaration) = @_;
966     my $is_widget_struct = 0;
967     my $default_to_public = 1;
968     if (&CheckIsObject ($symbol)) {
969     #print "Found widget struct: $symbol\n";
970     $is_widget_struct = 1;
971         $default_to_public = 0;
972     }
974     my $id;
975     my $condition;
976     if ($is_widget_struct) {
977         $id = &CreateValidSGMLID ($symbol . "_struct");
978         $condition = &MakeConditionDescription ($symbol . "_struct");
979     } else {
980         $id = &CreateValidSGMLID ($symbol);
981         $condition = &MakeConditionDescription ($symbol);
982     }
984     # Determine if it is a simple struct or it also has a typedef.
985     my $has_typedef = 0;
986     if ($StructHasTypedef{$symbol} || $declaration =~ m/^\s*typedef\s+/) {
987       $has_typedef = 1;
988     }
990     my $synop;
991     my $desc;
992     if ($has_typedef) {
993         # For structs with typedefs we just output the struct name.
994         $synop = &MakeReturnField("") . "<link linkend=\"$id\">$symbol</link>;\n";
995         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>$symbol</title>\n";
996     } else {
997         $synop = &MakeReturnField("struct") . "<link linkend=\"$id\">$symbol</link>;\n";
998         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>struct $symbol</title>\n";
999     }
1001     $desc .= MakeIndexterms($symbol, $id);
1003     # Form a pretty-printed, private-data-removed form of the declaration
1005     my $decl_out = "";
1006     if ($declaration =~ m/^\s*$/) {
1007         #print "Found opaque struct: $symbol\n";
1008         $decl_out = "typedef struct _$symbol $symbol;";
1009     } elsif ($declaration =~ m/^\s*struct\s+\w+\s*;\s*$/) {
1010         #print "Found opaque struct: $symbol\n";
1011         $decl_out = "struct $symbol;";
1012     } else {
1013         my $public = $default_to_public;
1014         my $new_declaration = "";
1015         my $decl_line;
1016         my $decl = $declaration;
1018         if ($decl =~ m/^\s*(typedef\s+)?struct\s*\w*\s*(?:\/\*.*\*\/)?\s*{(.*)}\s*\w*\s*;\s*$/s) {
1019             my $struct_contents = $2;
1021             foreach $decl_line (split (/\n/, $struct_contents)) {
1022                 #print "Struct line: $decl_line\n";
1023                 if ($decl_line =~ m%/\*\s*<\s*public\s*>\s*\*/%) {
1024                     $public = 1;
1025                 } elsif ($decl_line =~ m%/\*\s*<\s*(private|protected)\s*>\s*\*/%) {
1026                     $public = 0;
1027                 } elsif ($public) {
1028                     $new_declaration .= $decl_line . "\n";
1029                 }
1030             }
1032             if ($new_declaration) {
1033                 # Strip any blank lines off the ends.
1034                 $new_declaration =~ s/^\s*\n//;
1035                 $new_declaration =~ s/\n\s*$/\n/;
1037                 if ($has_typedef) {
1038                     $decl_out = "typedef struct {\n" . $new_declaration
1039                       . "} $symbol;\n";
1040                 } else {
1041                     $decl_out = "struct $symbol {\n" . $new_declaration
1042                       . "};\n";
1043                 }
1044             }
1045         } else {
1046             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1047                 "Couldn't parse struct:\n$declaration");
1048         }
1050         # If we couldn't parse the struct or it was all private, output an
1051         # empty struct declaration.
1052         if ($decl_out eq "") {
1053             if ($has_typedef) {
1054                 $decl_out = "typedef struct _$symbol $symbol;";
1055             } else {
1056                 $decl_out = "struct $symbol;";
1057             }
1058         }
1059     }
1061     $decl_out = &CreateValidSGML ($decl_out);
1062     $desc .= "<programlisting>$decl_out</programlisting>\n";
1064     $desc .= &MakeDeprecationNote($symbol);
1066     if (defined ($SymbolDocs{$symbol})) {
1067         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1068     }
1070     # Create a table of fields and descriptions
1072     # FIXME: Inserting &nbsp's into the produced type declarations here would
1073     #        improve the output in most situations ... except for function
1074     #        members of structs!
1075     my @fields = ParseStructDeclaration($declaration, !$default_to_public,
1076                                         0, \&MakeXRef,
1077                                         sub {
1078                                             "<structfield>$_[0]</structfield>";
1079                                         });
1080     my $params = $SymbolParams{$symbol};
1082     # If no parameters are filled in, we don't generate the description
1083     # table, for backwards compatibility
1085     my $found = 0;
1086     if (defined $params) {
1087         for (my $i = 1; $i <= $#$params; $i += 2) {
1088             if ($params->[$i] =~ /\S/) {
1089                 $found = 1;
1090                 last;
1091             }
1092         }
1093     }
1095     if ($found) {
1096         my %field_descrs = @$params;
1098             $desc .= <<EOF;
1099 <variablelist role="struct">
1101         while (@fields) {
1102             my $field_name = shift @fields;
1103             my $text = shift @fields;
1104             my $field_descr = $field_descrs{$field_name};
1106             $desc .= "<varlistentry>\n<term>$text</term>\n";
1107             if (defined $field_descr) {
1108                 $desc .= "<listitem><simpara>".&ExpandAbbreviations($symbol, $field_descr)."</simpara></listitem>\n";
1109             } else {
1110                 $desc .= "<listitem></listitem>\n";
1111             }
1112             $desc .= "</varlistentry>\n";
1113         }
1115         $desc .= "</variablelist>";
1116     }
1117     $desc .= OutputSymbolTraits ($symbol);
1118     $desc .= "</refsect2>\n";
1119     return ($synop, $desc);
1123 #############################################################################
1124 # Function    : OutputEnum
1125 # Description : Returns the synopsis and detailed description of a enum.
1126 # Arguments   : $symbol - the enum.
1127 #               $declaration - the declaration of the enum.
1128 #############################################################################
1130 sub OutputEnum {
1131     my ($symbol, $declaration) = @_;
1132     my $id = &CreateValidSGMLID ($symbol);
1133     my $condition = &MakeConditionDescription ($symbol);
1134     my $synop = &MakeReturnField("enum") . "<link linkend=\"$id\">$symbol</link>;\n";
1135     my $desc = "<refsect2 id=\"$id\" role=\"enum\"$condition>\n<title>enum $symbol</title>\n";
1137     $desc .= MakeIndexterms($symbol, $id);
1139     $declaration = &CreateValidSGML ($declaration);
1140     $desc .= "<programlisting>$declaration</programlisting>\n";
1142     $desc .= &MakeDeprecationNote($symbol);
1144     if (defined ($SymbolDocs{$symbol})) {
1145         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1146     }
1148     # Create a table of fields and descriptions
1150     my @members = ParseEnumDeclaration($declaration);
1151     my $params = $SymbolParams{$symbol};
1153     # If no parameters are filled in, we don't generate the description
1154     # table, for backwards compatibility
1156     my $found = 0;
1157     if (defined $params) {
1158         for (my $i = 1; $i <= $#$params; $i += 2) {
1159             if ($params->[$i] =~ /\S/) {
1160                 $found = 1;
1161                 last;
1162             }
1163         }
1164     }
1166     if ($found) {
1167         my %member_descrs = @$params;
1169             $desc .= <<EOF;
1170 <variablelist role="enum">
1172         for my $member_name (@members) {
1173             my $member_descr = $member_descrs{$member_name};
1175             $id = &CreateValidSGMLID ($member_name);
1176             $condition = &MakeConditionDescription ($member_name);
1177             $desc .= "<varlistentry id=\"$id\" role=\"constant\"$condition>\n<term><literal>$member_name</literal></term>\n";
1178             if (defined $member_descr) {
1179                 $desc .= "<listitem><simpara>".&ExpandAbbreviations($symbol, $member_descr)."</simpara></listitem>\n";
1180             } else {
1181                 $desc .= "<listitem></listitem>\n";
1182             }
1183             $desc .= "</varlistentry>\n";
1184         }
1186         $desc .= "</variablelist>";
1187     }
1189     $desc .= OutputSymbolTraits ($symbol);
1190     $desc .= "</refsect2>\n";
1191     return ($synop, $desc);
1195 #############################################################################
1196 # Function    : OutputUnion
1197 # Description : Returns the synopsis and detailed description of a union.
1198 # Arguments   : $symbol - the union.
1199 #               $declaration - the declaration of the union.
1200 #############################################################################
1202 sub OutputUnion {
1203     my ($symbol, $declaration) = @_;
1204     my $id = &CreateValidSGMLID ($symbol);
1205     my $condition = &MakeConditionDescription ($symbol);
1206     my $synop = &MakeReturnField("union") . "<link linkend=\"$id\">$symbol</link>;\n";
1207     my $desc = "<refsect2 id=\"$id\" role=\"union\"$condition>\n<title>union $symbol</title>\n";
1209     $desc .= MakeIndexterms($symbol, $id);
1211     $declaration = &CreateValidSGML ($declaration);
1212     $desc .= "<programlisting>$declaration</programlisting>\n";
1214     $desc .= &MakeDeprecationNote($symbol);
1216     if (defined ($SymbolDocs{$symbol})) {
1217         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1218     }
1219     $desc .= OutputSymbolTraits ($symbol);
1220     $desc .= "</refsect2>\n";
1221     return ($synop, $desc);
1225 #############################################################################
1226 # Function    : OutputVariable
1227 # Description : Returns the synopsis and detailed description of a variable.
1228 # Arguments   : $symbol - the extern'ed variable.
1229 #               $declaration - the declaration of the variable.
1230 #############################################################################
1232 sub OutputVariable {
1233     my ($symbol, $declaration) = @_;
1234     my $id = &CreateValidSGMLID ($symbol);
1235     my $condition = &MakeConditionDescription ($symbol);
1237     my $synop;
1238     if ($declaration =~ m/^\s*extern\s+((const\s+|unsigned\s+)*\w+)(\s+\*+|\*+|\s)(\s*)([A-Za-z]\w*)\s*;/) {
1239         my $mod = defined ($1) ? $1 : "";
1240         my $ptr = defined ($3) ? $3 : "";
1241         my $space = defined ($4) ? $4 : "";
1242         $synop = &MakeReturnField("extern") . "$mod$ptr$space<link linkend=\"$id\">$symbol</link>;\n";
1244     } else {
1245         $synop = &MakeReturnField("extern") . "<link linkend=\"$id\">$symbol</link>;\n";
1246     }
1248     my $desc = "<refsect2 id=\"$id\" role=\"variable\"$condition>\n<title>$symbol</title>\n";
1250     $desc .= MakeIndexterms($symbol, $id);
1252     $declaration = &CreateValidSGML ($declaration);
1253     $desc .= "<programlisting>$declaration</programlisting>\n";
1255     $desc .= &MakeDeprecationNote($symbol);
1257     if (defined ($SymbolDocs{$symbol})) {
1258         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1259     }
1260     $desc .= OutputSymbolTraits ($symbol);
1261     $desc .= "</refsect2>\n";
1262     return ($synop, $desc);
1266 #############################################################################
1267 # Function    : OutputFunction
1268 # Description : Returns the synopsis and detailed description of a function.
1269 # Arguments   : $symbol - the function.
1270 #               $declaration - the declaration of the function.
1271 #############################################################################
1273 sub OutputFunction {
1274     my ($symbol, $declaration, $symbol_type) = @_;
1275     my $id = &CreateValidSGMLID ($symbol);
1276     my $condition = &MakeConditionDescription ($symbol);
1278     # Take out the return type     $1                                                           $3   $4
1279     $declaration =~ s/<RETURNS>\s*((const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|enum\s+)*)(\w+)(\s*\**\s*(const|G_CONST_RETURN)?\s*\**\s*(restrict)?\s*)<\/RETURNS>\n//;
1280     my $type_modifier = defined($1) ? $1 : "";
1281     my $type = $3;
1282     my $pointer = $4;
1283     #print "$symbol pointer is $pointer\n";
1284     my $xref = &MakeXRef ($type);
1285     my $start = "";
1286     #if ($symbol_type eq 'USER_FUNCTION') {
1287     #    $start = "typedef ";
1288     #}
1290     # We output const rather than G_CONST_RETURN.
1291     $type_modifier =~ s/G_CONST_RETURN/const/g;
1292     $pointer =~ s/G_CONST_RETURN/const/g;
1293     $pointer =~ s/^\s+/ /g;
1295     my $ret_type_len = length ($start) + length ($type_modifier)
1296         + length ($pointer) + length ($type);
1297     my $ret_type_output;
1298     my $symbol_len;
1299     if ($ret_type_len < $RETURN_TYPE_FIELD_WIDTH) {
1300         $ret_type_output = "$start$type_modifier$xref$pointer"
1301             . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
1302         $symbol_len = 0;
1303     } else {
1304 #       $ret_type_output = "$start$type_modifier$xref$pointer\n"
1305 #           . (' ' x $RETURN_TYPE_FIELD_WIDTH);
1307         $ret_type_output = "$start$type_modifier$xref$pointer ";
1308         $symbol_len = $ret_type_len + 1 - $RETURN_TYPE_FIELD_WIDTH;
1309     }
1311     $symbol_len += length ($symbol);
1312     my $char1 = my $char2 = my $char3 = "";
1313     if ($symbol_type eq 'USER_FUNCTION') {
1314         $symbol_len += 3;
1315         $char1 = "(";
1316         $char2 = "*";
1317         $char3 = ")";
1318     }
1320     my ($symbol_output, $symbol_desc_output);
1321     if ($symbol_len < $SYMBOL_FIELD_WIDTH) {
1322         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3"
1323             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1324         $symbol_desc_output = "$char1$char2$symbol$char3"
1325             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1326     } else {
1327         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3\n"
1328             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1329         $symbol_desc_output = "$char1$char2$symbol$char3\n"
1330             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1331     }
1333     my $synop = $ret_type_output . $symbol_output . '(';
1334     my $desc = "<refsect2 id=\"$id\" role=\"function\"$condition>\n<title>${symbol} ()</title>\n";
1336     $desc .= MakeIndexterms($symbol, $id);
1338     $desc  .= "<programlisting>${ret_type_output}$symbol_desc_output(";
1340     my $param_num = 0;
1341     while ($declaration ne "") {
1342         #print "$declaration";
1344         if ($declaration =~ s/^[\s,]+//) {
1345             # skip whitespace and commas
1346             next;
1348         } elsif ($declaration =~ s/^void\s*[,\n]//) {
1349             $synop .= "void";
1350             $desc  .= "void";
1352         } elsif ($declaration =~ s/^...\s*[,\n]//) {
1353             if ($param_num == 0) {
1354                 $synop .= "...";
1355                 $desc  .= "...";
1356             } else {
1357                 $synop .= ",\n"
1358                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1359                     . " ...";
1360                 $desc  .= ",\n"
1361                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1362                     . " ...";
1363             }
1365         # allow alphanumerics, '_', '[' & ']' in param names
1366         # Try to match a standard parameter (keep in sync with gtkdoc-mktmpl)
1367         #                                $1                                                                                                                                    $2                             $3                                                           $4       $5
1368         } elsif ($declaration =~ s/^\s*((?:G_CONST_RETURN|G_GNUC_UNUSED|unsigned long|unsigned short|signed long|signed short|unsigned|signed|long|short|volatile|const)\s+)*((?:struct\b|enum\b)?\s*\w+)\s*((?:(?:const\b|restrict\b)?\s*\*?\s*(?:const\b|restrict\b)?\s*)*)(\w+)?\s*((?:\[\S*\])*)\s*[,\n]//) {
1369             my $pre     = defined($1) ? $1 : "";
1370             my $type    = $2;
1371             my $ptr     = defined($3) ? $3 : "";
1372             my $name    = defined($4) ? $4 : "";
1373             my $array   = defined($5) ? $5 : "";
1375             $pre  =~ s/\s+/ /g;
1376             $type =~ s/\s+/ /g;
1377             $ptr  =~ s/\s+/ /g;
1378             $ptr  =~ s/\s+$//;
1379             if ($ptr && $ptr !~ m/\*$/) { $ptr .= " "; }
1381             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1383             if (($name eq "") && $pre =~ m/^((un)?signed .*)\s?/ ) {
1384                 $name = $type;
1385                 $type = "$1";
1386                 $pre = "";
1387             }
1389             my $xref = &MakeXRef ($type);
1390             my $label   = "$pre$xref $ptr$name$array";
1392             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1394             if ($param_num == 0) {
1395                 $synop .= "$label";
1396                 $desc  .= "$label";
1397             } else {
1398                 $synop .= ",\n"
1399                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1400                     . " $label";
1401                 $desc  .= ",\n"
1402                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1403                     . " $label";
1404             }
1406         # Try to match parameters which are functions (keep in sync with gtkdoc-mktmpl)
1407         #                              $1                                       $2          $3      $4                        $5                    $7             $8
1408         } elsif ($declaration =~ s/^(const\s+|G_CONST_RETURN\s+|unsigned\s+)*(struct\s+)?(\w+)\s*(\**)\s*(?:restrict\b)?\s*(const\s+)?\(\s*\*+\s*(\w+)\s*\)\s*\(([^)]*)\)\s*[,\n]//) {
1409             my $mod1 = defined($1) ? $1 : "";
1410             if (defined($2)) { $mod1 .= $2; }
1411             my $type = $3;
1412             my $ptr1 = $4;
1413             my $mod2 = defined($5) ? $5 : "";
1414             my $func_ptr = $6;
1415             my $name = $7;
1416             my $func_params = defined($8) ? $8 : "";
1417             
1418             #if (!defined($type)) { print "## no type\n"; };
1419             #if (!defined($ptr1)) { print "## no ptr1\n"; };
1420             #if (!defined($func_ptr)) { print "## no func_ptr\n"; };
1421             #if (!defined($name)) { print "## no name\n"; };
1423             if ($ptr1 && $ptr1 !~ m/\*$/) { $ptr1 .= " "; }
1424             $func_ptr  =~ s/\s+//g;
1425             my $xref = &MakeXRef ($type);
1426             my $label = "$mod1$xref$ptr1$mod2 ($func_ptr$name) ($func_params)";
1428             #print "Type: [$mod1][$xref][$ptr1][$mod2] ([$func_ptr][$name]) ($func_params)\n";
1429             if ($param_num == 0) {
1430                 $synop .= "$label";
1431                 $desc  .= "$label";
1432             } else {
1433                 $synop .= ",\n"
1434                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1435                     . " $label";
1436                 $desc  .= ",\n"
1437                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1438                     . " $label";
1439             }
1441         } else {
1442             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1443                 "Can't parse args for function $symbol: $declaration");
1444             last;
1445         }
1446         $param_num++;
1447     }
1448     $synop .= ");\n";
1449     $desc  .= ");</programlisting>\n";
1451     $desc .= &MakeDeprecationNote($symbol);
1453     my $parameters = &OutputParamDescriptions ("FUNCTION", $symbol);
1454     my $parameters_output = 0;
1456     if (defined ($SymbolDocs{$symbol})) {
1457         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1459         # Try to insert the parameter table at the author's desired position.
1460         # Otherwise we need to tag it onto the end.
1461         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1462           $parameters_output = 1;
1463         }
1464         $desc .= $symbol_docs;
1465     }
1467     if ($parameters_output == 0) {
1468         $desc .= $parameters;
1469     }
1471     $desc .= OutputSymbolTraits ($symbol);
1472     $desc .= "</refsect2>\n";
1473     return ($synop, $desc);
1477 #############################################################################
1478 # Function    : OutputParamDescriptions
1479 # Description : Returns the DocBook output describing the parameters of a
1480 #               function, macro or signal handler.
1481 # Arguments   : $symbol_type - 'FUNCTION', 'MACRO' or 'SIGNAL'. Signal
1482 #                 handlers have an implicit user_data parameter last.
1483 #               $symbol - the name of the function/macro being described.
1484 #############################################################################
1486 sub OutputParamDescriptions {
1487     my ($symbol_type, $symbol) = @_;
1488     my $output = "";
1489     if (defined ($SymbolParams{$symbol})) {
1490         my $returns = "";
1491         my $params = $SymbolParams{$symbol};
1492         my $params_desc = "";
1493         my $j;
1494         for ($j = 0; $j <= $#$params; $j += 2) {
1495             my $param_name = $$params[$j];
1496             my $param = $$params[$j + 1];
1497             if ($param_name eq "Returns") {
1498                 $returns = &ExpandAbbreviations($symbol, $param);
1499             } else {
1500                 if ($param_name eq "Varargs") {
1501                     $param_name = "...";
1502                 }
1503                 $param = &ExpandAbbreviations($symbol, $param);
1504                 $params_desc .= "<varlistentry><term><parameter>$param_name</parameter>&nbsp;:</term>\n<listitem><simpara>$param</simpara></listitem></varlistentry>\n";
1505             }
1506         }
1508         # Signals have an implicit user_data parameter which we describe.
1509         if ($symbol_type eq "SIGNAL") {
1510             $params_desc .= "<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>\n<listitem><simpara>user data set when the signal handler was connected.</simpara></listitem></varlistentry>\n";
1511         }
1513         # Start a table if we need one.
1514         if ($params_desc || $returns) {
1515             $output .= <<EOF;
1516 <variablelist role="params">
1519             if ($params_desc ne "") {
1520 #               $output .= "<varlistentry><term>Parameters:</term><listitem></listitem></varlistentry>\n";
1521                 $output .= $params_desc;
1522             }
1524             # Output the returns info last.
1525             if ($returns) {
1526                 $output .= "<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara>$returns</simpara></listitem></varlistentry>\n";
1527             }
1529             # Finish the table.
1530             $output .= "</variablelist>";
1531         }
1532     }
1533     return $output;
1537 #############################################################################
1538 # Function    : ParseStabilityLevel
1539 # Description : Parses a stability level and outputs a warning if it isn't
1540 #               valid.
1541 # Arguments   : $stability - the stability text.
1542 #               $file, $line - context for error message
1543 #               $message - description of where the level is from, to use in
1544 #               any error message.
1545 # Returns     : The parsed stability level string.
1546 #############################################################################
1548 sub ParseStabilityLevel {
1549     my ($stability, $file, $line, $message) = @_;
1551     $stability =~ s/^\s*//;
1552     $stability =~ s/\s*$//;
1553     if ($stability =~ m/^stable$/i) {
1554         $stability = "Stable";
1555     } elsif ($stability =~ m/^unstable$/i) {
1556         $stability = "Unstable";
1557     } elsif ($stability =~ m/^private$/i) {
1558         $stability = "Private";
1559     } else {
1560         &LogWarning ($file, $line, "$message is $stability.".
1561             "It should be one of these: Stable, Unstable, or Private.");
1562     }
1563     return $stability;
1567 #############################################################################
1568 # Function    : OutputSGMLFile
1569 # Description : Outputs the final DocBook file for one section.
1570 # Arguments   : $file - the name of the file.
1571 #               $title - the title from the $MODULE-sections.txt file, which
1572 #                 will be overridden by the title in the template file.
1573 #               $section_id - the SGML id to use for the toplevel tag.
1574 #               $includes - comma-separates list of include files added at top
1575 #                 of synopsis, with '<' '>' around them (if not already enclosed in "").
1576 #               $synopsis - reference to the DocBook for the Synopsis part.
1577 #               $details - reference to the DocBook for the Details part.
1578 #               $signal_synop - reference to the DocBook for the Signal Synopsis part
1579 #               $signal_desc - reference to the DocBook for the Signal Description part
1580 #               $args_synop - reference to the DocBook for the Arg Synopsis part
1581 #               $args_desc - reference to the DocBook for the Arg Description part
1582 #               $hierarchy - reference to the DocBook for the Object Hierarchy part
1583 #               $interfaces - reference to the DocBook for the Interfaces part
1584 #               $implementations - reference to the DocBook for the Known Implementations part
1585 #               $prerequisites - reference to the DocBook for the Prerequisites part
1586 #               $derived - reference to the DocBook for the Derived Interfaces part
1587 #               $file_objects - reference to an array of objects in this file
1588 #############################################################################
1590 sub OutputSGMLFile {
1591     my ($file, $title, $section_id, $includes, $synopsis, $details, $signals_synop, $signals_desc, $args_synop, $args_desc, $hierarchy, $interfaces, $implementations, $prerequisites, $derived, $file_objects) = @_;
1593     #print "Output sgml for file $file with title '$title'\n";
1594     
1595     # The edited title overrides the one from the sections file.
1596     my $new_title = $SymbolDocs{"$TMPL_DIR/$file:Title"};
1597     if (defined ($new_title) && $new_title !~ m/^\s*$/) {
1598         $title = $new_title;
1599         #print "Found title: $title\n";
1600     }
1601     my $short_desc = $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
1602     if (!defined ($short_desc) || $short_desc =~ m/^\s*$/) {
1603         $short_desc = "";
1604     } else {
1605         $short_desc = &ExpandAbbreviations("$title:Short_description",
1606                                            $short_desc);
1607         #print "Found short_desc: $short_desc";
1608     }
1609     my $long_desc = $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
1610     if (!defined ($long_desc) || $long_desc =~ m/^\s*$/) {
1611         $long_desc = "";
1612     } else {
1613         $long_desc = &ExpandAbbreviations("$title:Long_description",
1614                                           $long_desc);
1615         #print "Found long_desc: $long_desc";
1616     }
1617     my $see_also = $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
1618     if (!defined ($see_also) || $see_also =~ m%^\s*(<para>)?\s*(</para>)?\s*$%) {
1619         $see_also = "";
1620     } else {
1621         $see_also = &ExpandAbbreviations("$title:See_Also", $see_also);
1622         #print "Found see_also: $see_also";
1623     }
1624     if ($see_also) {
1625         $see_also = "<refsect1 id=\"$section_id.see-also\">\n<title>See Also</title>\n$see_also\n</refsect1>\n";
1626     }
1627     my $stability = $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
1628     if (!defined ($stability) || $stability =~ m/^\s*$/) {
1629         $stability = "";
1630     } else {
1631         $stability = &ParseStabilityLevel($stability, $file, $., "Section stability level");
1632         #print "Found stability: $stability";
1633     }
1634     if ($stability) {
1635         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$stability, unless otherwise indicated\n</refsect1>\n";
1636     } elsif ($DEFAULT_STABILITY) {
1637         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$DEFAULT_STABILITY, unless otherwise indicated\n</refsect1>\n";
1638     }
1640     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
1641         gmtime (time);
1642     my $month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
1643     $year += 1900;
1645     my $include_output = "";
1646     my $include;
1647     foreach $include (split (/,/, $includes)) {
1648         if ($include =~ m/^\".+\"$/) {
1649             $include_output .= "#include ${include}\n";
1650         }
1651         else {
1652             $include =~ s/^\s+|\s+$//gs;
1653             $include_output .= "#include &lt;${include}&gt;\n";
1654         }
1655     }
1656     if ($include_output ne '') {
1657         $include_output = "\n$include_output\n";
1658     }
1660     my $old_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT";
1661     my $new_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT.new";
1663     open (OUTPUT, ">$new_sgml_file")
1664         || die "Can't create $new_sgml_file: $!";
1666     my $object_anchors = "";
1667     foreach my $object (@$file_objects) {
1668         next if ($object eq $section_id);
1669         my $id = CreateValidSGMLID($object);
1670         #print "Debug: Adding anchor for $object\n";
1671         $object_anchors .= "<anchor id=\"$id\"$empty_element_end";
1672     }
1674     # We used to output this, but is messes up our UpdateFileIfChanged code
1675     # since it changes every day (and it is only used in the man pages):
1676     # "<refentry id="$section_id" revision="$mday $month $year">"
1678     if (lc($OUTPUT_FORMAT) eq "xml") {
1679         print OUTPUT $doctype_header;
1680     }
1682     print OUTPUT <<EOF;
1683 <refentry id="$section_id">
1684 <refmeta>
1685 <refentrytitle role="top_of_page" id="$section_id.top_of_page">$title</refentrytitle>
1686 <manvolnum>3</manvolnum>
1687 <refmiscinfo>\U$MODULE\E Library</refmiscinfo>
1688 </refmeta>
1690 <refnamediv>
1691 <refname>$title</refname>
1692 <refpurpose>$short_desc</refpurpose>
1693 <!--[<xref linkend="desc" endterm="desc.title"/>]-->
1694 </refnamediv>
1695 $stability
1696 <refsynopsisdiv id="$section_id.synopsis" role="synopsis">
1697 <title role="synopsis.title">Synopsis</title>
1698 $object_anchors
1699 <synopsis>
1700 $include_output$${synopsis}</synopsis>
1701 </refsynopsisdiv>
1703 $$hierarchy
1704 $$prerequisites
1705 $$derived
1706 $$interfaces
1707 $$implementations
1708 $$args_synop
1709 $$signals_synop
1711 <refsect1 id="$section_id.description" role="desc">
1712 <title role="desc.title">Description</title>
1713 $long_desc
1714 </refsect1>
1716 <refsect1 id="$section_id.details" role="details">
1717 <title role="details.title">Details</title>
1718 $$details
1719 </refsect1>
1720 $$args_desc
1721 $$signals_desc
1723 $see_also
1724 </refentry>
1726     close (OUTPUT);
1728     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
1732 #############################################################################
1733 # Function    : OutputExtraFile
1734 # Description : Copies an "extra" DocBook file into the output directory,
1735 #               expanding abbreviations
1736 # Arguments   : $file - the source file.
1737 #############################################################################
1738 sub OutputExtraFile {
1739     my ($file) = @_;
1741     my $basename;
1743     ($basename = $file) =~ s!^.*/!!;
1745     my $old_sgml_file = "$SGML_OUTPUT_DIR/$basename";
1746     my $new_sgml_file = "$SGML_OUTPUT_DIR/$basename.new";
1748     my $contents;
1750     open(EXTRA_FILE, "<$file") || die "Can't open $file";
1752     {
1753         local $/;
1754         $contents = <EXTRA_FILE>;
1755     }
1757     open (OUTPUT, ">$new_sgml_file")
1758         || die "Can't create $new_sgml_file: $!";
1760     print OUTPUT &ExpandAbbreviations ("$basename file", $contents);
1761     close (OUTPUT);
1763     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
1765 #############################################################################
1766 # Function    : OutputBook
1767 # Description : Outputs the SGML entities that need to be included into the
1768 #               main SGML file for the module.
1769 # Arguments   : $book_top - the declarations of the entities, which are added
1770 #                 at the top of the main SGML file.
1771 #               $book_bottom - the references to the entities, which are
1772 #                 added in the main SGML file at the desired position.
1773 #############################################################################
1775 sub OutputBook {
1776     my ($book_top, $book_bottom) = @_;
1778     my $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top";
1779     my $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top.new";
1781     open (OUTPUT, ">$new_file")
1782         || die "Can't create $new_file: $!";
1783     print OUTPUT $book_top;
1784     close (OUTPUT);
1786     &UpdateFileIfChanged ($old_file, $new_file, 0);
1789     $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom";
1790     $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom.new";
1792     open (OUTPUT, ">$new_file")
1793         || die "Can't create $new_file: $!";
1794     print OUTPUT $book_bottom;
1795     close (OUTPUT);
1797     &UpdateFileIfChanged ($old_file, $new_file, 0);
1800     # If the main SGML file hasn't been created yet, we create it here.
1801     # The user can tweak it later.
1802     if ($MAIN_SGML_FILE && ! -e $MAIN_SGML_FILE) {
1803       open (OUTPUT, ">$MAIN_SGML_FILE")
1804         || die "Can't create $MAIN_SGML_FILE: $!";
1806       if (lc($OUTPUT_FORMAT) eq "xml") {
1807           print OUTPUT <<EOF;
1808 <?xml version="1.0"?>
1809 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
1810                "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
1811 <book id="index" xmlns:xi="http://www.w3.org/2003/XInclude">
1813       } else {
1814         print OUTPUT <<EOF;
1815 <!doctype book PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
1817         print OUTPUT $book_top;
1818         print OUTPUT <<EOF;
1820 <book id="index">
1822       }
1824 print OUTPUT <<EOF;
1825   <bookinfo>
1826     <title>$MODULE Reference Manual</title>
1827     <releaseinfo>
1828       for $MODULE [VERSION]
1829       The latest version of this documentation can be found on-line at
1830       <ulink role="online-location" url="http://[SERVER]/$MODULE/index.html">http://[SERVER]/$MODULE/</ulink>.
1831     </releaseinfo>
1832   </bookinfo>
1834   <chapter>
1835     <title>[Insert title here]</title>
1837       print OUTPUT $book_bottom;
1839       print OUTPUT <<EOF;
1840   </chapter>
1841 </book>
1844       close (OUTPUT);
1845     }
1849 #############################################################################
1850 # Function    : CreateValidSGMLID
1851 # Description : Creates a valid SGML 'id' from the given string.
1852 #               NOTE: SGML ids are case-insensitive, so we have a few special
1853 #                     cases to avoid clashes of ids.
1854 # Arguments   : $id - the string to be converted into a valid SGML id.
1855 #############################################################################
1857 sub CreateValidSGMLID {
1858     my ($id) = $_[0];
1860     # Append ":CAPS" to all all-caps identifiers
1862     # Special case, '_' would end up as '' so we use 'gettext-macro' instead.
1863     if ($id eq "_") { return "gettext-macro"; }
1865     if ($id !~ /[a-z]/) { $id .= ":CAPS" };
1867     $id =~ s/[_ ]/-/g;
1868     $id =~ s/[,\.]//g;
1869     $id =~ s/^-*//;
1870     $id =~ s/::/-/g;
1872     return $id;
1876 #############################################################################
1877 # Function    : CreateValidSGML
1878 # Description : This turns any chars which are used in SGML into entities,
1879 #               e.g. '<' into '&lt;'
1880 # Arguments   : $text - the text to turn into proper SGML.
1881 #############################################################################
1883 sub CreateValidSGML {
1884     my ($text) = @_;
1885     $text =~ s/&/&amp;/g;       # Do this first, or the others get messed up.
1886     $text =~ s/</&lt;/g;
1887     $text =~ s/>/&gt;/g;
1888     return $text;
1891 #############################################################################
1892 # Function    : ConvertSGMLChars
1893 # Description : This is used for text in source code comment blocks, to turn
1894 #               chars which are used in SGML into entities, e.g. '<' into
1895 #               '&lt;'. Depending on $SGML_MODE, this is done
1896 #               unconditionally or only if the character doesn't seem to be
1897 #               part of an SGML construct (tag or entity reference).
1898 # Arguments   : $text - the text to turn into proper SGML.
1899 #############################################################################
1901 sub ConvertSGMLChars {
1902     my ($symbol, $text) = @_;
1904     if ($SGML_MODE) {
1905         # For the SGML mode only convert to entities outside CDATA sections.
1906         return &ModifyXMLElements ($text, $symbol,
1907                                    "<!\\[CDATA\\[|<programlisting[^>]*>",
1908                                    \&ConvertSGMLCharsEndTag,
1909                                    \&ConvertSGMLCharsCallback);
1910     } else {
1911         # For the simple non-sgml mode, convert to entities everywhere.
1912         $text =~ s/&/&amp;/g;   # Do this first, or the others get messed up.
1913         $text =~ s/</&lt;/g;
1914         $text =~ s/>/&gt;/g;
1915         return $text;
1916     }
1920 sub ConvertSGMLCharsEndTag {
1921   if ($_[0] eq "<!\[CDATA\[") {
1922     return "]]>";
1923   } else {
1924     return "</programlisting>";
1925   }
1928 sub ConvertSGMLCharsCallback {
1929   my ($text, $symbol, $tag) = @_;
1931   if ($tag =~ m/^<programlisting/) {
1932     # We can handle <programlisting> specially here.
1933     return &ModifyXMLElements ($text, $symbol,
1934                                "<!\\[CDATA\\[",
1935                                \&ConvertSGMLCharsEndTag,
1936                                \&ConvertSGMLCharsCallback2);
1937   } elsif ($tag eq "") {
1938     # If we're not in CDATA convert to entities.
1939     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
1940     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
1941     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
1943     # Handle "#include <xxxxx>"
1944     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
1945   }
1947   return $text;
1950 sub ConvertSGMLCharsCallback2 {
1951   my ($text, $symbol, $tag) = @_;
1953   # If we're not in CDATA convert to entities.
1954   # We could handle <programlisting> differently, though I'm not sure it helps.
1955   if ($tag eq "") {
1956     # replace only if its not a tag
1957     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
1958     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
1959     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
1961     # Handle "#include <xxxxx>"
1962     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
1963   }
1965   return $text;
1969 #############################################################################
1970 # Function    : ExpandAbbreviations
1971 # Description : This turns the abbreviations function(), macro(), @param,
1972 #               %constant, and #symbol into appropriate DocBook markup.
1973 #               CDATA sections and <programlisting> parts are skipped.
1974 # Arguments   : $symbol - the symbol being documented, for error messages.
1975 #               $text - the text to expand.
1976 #############################################################################
1978 sub ExpandAbbreviations {
1979   my ($symbol, $text) = @_;
1981   # Convert "|[" and "]|" into the start and end of program listing examples.
1982   $text =~ s%\|\[%<informalexample><programlisting>%g;
1983   $text =~ s%\]\|%</programlisting></informalexample>%g;
1985   # keep CDATA unmodified, preserve ulink tags (ideally we preseve all tags
1986   # as such)
1987   return &ModifyXMLElements ($text, $symbol,
1988                              "<!\\[CDATA\\[|<ulink[^>]*>|<programlisting[^>]*>",
1989                              \&ExpandAbbreviationsEndTag,
1990                              \&ExpandAbbreviationsCallback);
1994 # Returns the end tag corresponding to the given start tag.
1995 sub ExpandAbbreviationsEndTag {
1996   my ($start_tag) = @_;
1998   if ($start_tag eq "<!\[CDATA\[") {
1999     return "]]>";
2000   } elsif ($start_tag =~ m/<(\w+)/) {
2001     return "</$1>";
2002   }
2005 # Called inside or outside each CDATA or <programlisting> section.
2006 sub ExpandAbbreviationsCallback {
2007   my ($text, $symbol, $tag) = @_;
2009   if ($tag =~ m/^<programlisting/) {
2010     # Handle any embedded CDATA sections.
2011     return &ModifyXMLElements ($text, $symbol,
2012                                "<!\\[CDATA\\[",
2013                                \&ExpandAbbreviationsEndTag,
2014                                \&ExpandAbbreviationsCallback2);
2015   } elsif ($tag eq "") {
2016     # We are outside any CDATA or <programlisting> sections, so we expand
2017     # any gtk-doc abbreviations.
2019     # Convert 'function()' or 'macro()'.
2020     $text =~ s/(\w+)\s*\(\)/&MakeXRef($1, &tagify($1 . "()", "function"));/eg;
2022     # Convert '@param', but not '\@param'.
2023     #$text =~ s/\@(\w+((\.|->)\w+)*)/<parameter>$1<\/parameter>/g;
2024     $text =~ s/([^\\])\@(\w+((\.|->)\w+)*)/$1<parameter>$2<\/parameter>/g;
2025     $text =~ s/\\\@/\@/g;
2027     # Convert '%constant', but not '\%constant'.
2028     # Also allow negative numbers, e.g. %-1.
2029     #$text =~ s/\%(-?\w+)/&MakeXRef($1, &tagify($1, "literal"));/eg;
2030     $text =~ s/([^\\])\%(-?\w+)/$1.&MakeXRef($2, &tagify($2, "literal"));/eg;
2031     $text =~ s/\\\%/\%/g;
2033     # Convert '#symbol', but not '\#symbol'.
2034     #$text =~ s/#([\w\-:]+)/&MakeHashXRef($1, "type");/eg;
2035     $text =~ s/([^\\])#([\w\-:]+)/$1.&MakeHashXRef($2, "type");/eg;
2036     $text =~ s/\\#/#/g;
2037   }
2039   return $text;
2042 # This is called inside a <programlisting>
2043 sub ExpandAbbreviationsCallback2 {
2044   my ($text, $symbol, $tag) = @_;
2046   if ($tag eq "") {
2047     # We are inside a <programlisting> but outside any CDATA sections,
2048     # so we expand any gtk-doc abbreviations.
2049     # FIXME: why is this different from &ExpandAbbreviationsCallback(),
2050     #        why not just call it
2051     $text =~ s/#(\w+)/&MakeHashXRef($1, "");/eg;
2052   }
2054   return $text;
2057 sub MakeHashXRef {
2058     my ($symbol, $tag) = @_;;
2059     my $text = $symbol;
2061     # Check for things like '#include', '#define', and skip them.
2062     if ($PreProcessorDirectives{$symbol}) {
2063       return "#$symbol";
2064     }
2066     # Get rid of any special '-struct' suffix.
2067     $text =~ s/-struct$//;
2069     # If the symbol is in the form "Object::signal", then change the symbol to
2070     # "Object-signal" and use "signal" as the text.
2071     if ($symbol =~ s/::/-/) {
2072       $text = "\"$'\"";
2073     }
2075     # If the symbol is in the form "Object:property", then change the symbol to
2076     # "Object--property" and use "property" as the text.
2077     if ($symbol =~ s/:/--/) {
2078       $text = "\"$'\"";
2079     }
2081     if ($tag ne "") {
2082       $text = tagify ($text, $tag);
2083     }
2085     return &MakeXRef($symbol, $text);
2089 #############################################################################
2090 # Function    : ModifyXMLElements
2091 # Description : Looks for given XML element tags within the text, and calls
2092 #               the callback on pieces of text inside & outside those elements.
2093 #               Used for special handling of text inside things like CDATA
2094 #               and <programlisting>.
2095 # Arguments   : $text - the text.
2096 #               $symbol - the symbol currently being documented (only used for
2097 #                      error messages).
2098 #               $start_tag_regexp - the regular expression to match start tags.
2099 #                      e.g. "<!\\[CDATA\\[|<programlisting[^>]*>" to match
2100 #                      CDATA sections or programlisting elements.
2101 #               $end_tag_func - function which is passed the matched start tag
2102 #                      and should return the appropriate end tag string.
2103 #               $callback - callback called with each part of the text. It is
2104 #                      called with a piece of text, the symbol being
2105 #                      documented, and the matched start tag or "" if the text
2106 #                      is outside the XML elements being matched.
2107 #############################################################################
2108 sub ModifyXMLElements {
2109     my ($text, $symbol, $start_tag_regexp, $end_tag_func, $callback) = @_;
2110     my ($before_tag, $start_tag, $end_tag_regexp, $end_tag);
2111     my $result = "";
2113     while ($text =~ m/$start_tag_regexp/s) {
2114       $before_tag = $`; # Prematch for last successful match string
2115       $start_tag = $&;  # Last successful match
2116       $text = $';       # Postmatch for last successful match string
2118       $result .= &$callback ($before_tag, $symbol, "");
2119       $result .= $start_tag;
2121       # get the mathing end-tag for current tag
2122       $end_tag_regexp = &$end_tag_func ($start_tag);
2124       if ($text =~ m/$end_tag_regexp/s) {
2125         $before_tag = $`;
2126         $end_tag = $&;
2127         $text = $';
2129         $result .= &$callback ($before_tag, $symbol, $start_tag);
2130         $result .= $end_tag;
2131       } else {
2132         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2133             "Can't find tag end: $end_tag_regexp in docs for: $symbol.");
2134         # Just assume it is all inside the tag.
2135         $result .= &$callback ($text, $symbol, $start_tag);
2136         $text = "";
2137       }
2138     }
2140     # Handle any remaining text outside the tags.
2141     $result .= &$callback ($text, $symbol, "");
2143     return $result;
2146 sub noop {
2147   return $_[0];
2150 # Adds a tag around some text.
2151 # e.g tagify("Text", "literal") => "<literal>Text</literal>".
2152 sub tagify {
2153    my ($text, $elem) = @_;
2154    return "<" . $elem . ">" . $text . "</" . $elem . ">";
2158 #############################################################################
2159 # Function    : MakeXRef
2160 # Description : This returns a cross-reference link to the given symbol.
2161 #               Though it doesn't try to do this for a few standard C types
2162 #               that it knows won't be in the documentation.
2163 # Arguments   : $symbol - the symbol to try to create a XRef to.
2164 #               $text - text text to put inside the XRef, defaults to $symbol
2165 #############################################################################
2167 sub MakeXRef {
2168     my ($symbol, $text) = ($_[0], $_[1]);
2169     if (!defined($text)) {
2170         $text = $symbol;
2172         # Get rid of special '-struct' suffix.
2173         $text =~ s/-struct$//;
2174     }
2176     #print "Getting type link for $symbol -> $text\n";
2178     my $symbol_id = &CreateValidSGMLID ($symbol);
2179     return "<link linkend=\"$symbol_id\">$text</link>";
2183 #############################################################################
2184 # Function    : MakeIndexterms
2185 # Description : This returns a indexterm elements for the given symbol
2186 # Arguments   : $symbol - the symbol to create indexterms for
2187 #############################################################################
2189 sub MakeIndexterms {
2190   my ($symbol, $id) = @_;
2191   my $terms =  "";
2193   if (exists $Deprecated{$symbol}) {
2194       $terms .= "<indexterm zone=\"$id\" role=\"deprecated\"><primary>$symbol</primary></indexterm>"
2195   }
2196   if (exists $Since{$symbol}) {
2197      my $since = $Since{$symbol};
2198      $since =~ s/^\s+//;
2199      $since =~ s/\s+$//;
2200      if ($since ne "") {
2201          $terms .= "<indexterm zone=\"$id\" role=\"$since\"><primary>$symbol</primary></indexterm>";
2202      }
2203   }
2204   if ($terms eq "") {
2205      $terms .= "<indexterm zone=\"$id\"><primary>$symbol</primary></indexterm>";
2206   }
2208   return $terms;
2211 #############################################################################
2212 # Function    : MakeDeprecationNote
2213 # Description : This returns a deprecation warning for the given symbol.
2214 # Arguments   : $symbol - the symbol to try to create a warning for.
2215 #############################################################################
2217 sub MakeDeprecationNote {
2218     my ($symbol) = $_[0];
2219     my $desc = "";
2220     my $note = "";
2221     if (exists $Deprecated{$symbol}) {
2222         $desc .= "<warning>";
2224         if ($Deprecated{$symbol} =~ /^\s*([0-9\.]+)\s*:/) {
2225                 $desc .= "<para><literal>$symbol</literal> has been deprecated since version $1 and should not be used in newly-written code.";
2226         } else {
2227                 $desc .= "<para><literal>$symbol</literal> is deprecated and should not be used in newly-written code.";
2228         }
2229         if ($Deprecated{$symbol} ne "") {
2230             $note = &ExpandAbbreviations($symbol, $Deprecated{$symbol});
2231             $note =~ s/^\s*([0-9\.]+)\s*:\s*//;
2232             $note =~ s/^\s+//;
2233             $note =~ s/\s+$//;
2234             $note =~ s%\n{2,}%\n</para>\n<para>\n%g;
2235             $desc .= " " . $note;
2236         }
2237         $desc .= "</para></warning>\n";
2238     }
2239     return $desc;
2242 #############################################################################
2243 # Function    : MakeConditionDescription
2244 # Description : This returns a sumary of conditions for the given symbol.
2245 # Arguments   : $symbol - the symbol to try to create the sumary.
2246 #############################################################################
2248 sub MakeConditionDescription {
2249     my ($symbol) = $_[0];
2250     my $desc = "";
2252     if (exists $Deprecated{$symbol}) {
2253         if ($desc ne "") {
2254             $desc .= "|";
2255         }
2257         if ($Deprecated{$symbol} =~ /^\s*(.*?)\s*$/) {
2258                 $desc .= "deprecated:$1";
2259         } else {
2260                 $desc .= "deprecated";
2261         }
2262     }
2264     if (exists $Since{$symbol}) {
2265         if ($desc ne "") {
2266             $desc .= "|";
2267         }
2269         if ($Since{$symbol} =~ /^\s*(.*?)\s*$/) {
2270                 $desc .= "since:$1";
2271         } else {
2272                 $desc .= "since";
2273         }
2274     }
2276     if (exists $StabilityLevel{$symbol}) {
2277         if ($desc ne "") {
2278             $desc .= "|";
2279         }
2280         $desc .= "stability:".$StabilityLevel{$symbol};
2281     }
2283     if ($desc ne "") {
2284         $desc=" condition=\"".$desc."\"";
2285         #print "condition for '$symbol' = '$desc'\n";
2286     }
2287     return $desc;
2290 #############################################################################
2291 # Function    : GetHierarchy
2292 # Description : Returns the DocBook output describing the ancestors and
2293 #               immediate children of a GObject subclass. It uses the
2294 #               global @Objects and @ObjectLevels arrays to walk the tree.
2295 # Arguments   : $object - the GtkObject subclass.
2296 #############################################################################
2298 sub GetHierarchy {
2299     my ($object) = @_;
2301     # Find object in the objects array.
2302     my $found = 0;
2303     my @children = ();
2304     my $i;
2305     my $level;
2306     my $j;
2307     for ($i = 0; $i < @Objects; $i++) {
2308         if ($found) {
2309             if ($ObjectLevels[$i] <= $level) {
2310             last;
2311         }
2312             elsif ($ObjectLevels[$i] == $level + 1) {
2313                 push (@children, $Objects[$i]);
2314             }
2315         }
2316         elsif ($Objects[$i] eq $object) {
2317             $found = 1;
2318             $j = $i;
2319             $level = $ObjectLevels[$i];
2320         }
2321     }
2322     if (!$found) {
2323         return "";
2324     }
2326     # Walk up the hierarchy, pushing ancestors onto the ancestors array.
2327     my @ancestors = ();
2328     push (@ancestors, $object);
2329     #print "Level: $level\n";
2330     while ($level > 1) {
2331         $j--;
2332         if ($ObjectLevels[$j] < $level) {
2333             push (@ancestors, $Objects[$j]);
2334             $level = $ObjectLevels[$j];
2335             #print "Level: $level\n";
2336         }
2337     }
2339     # Output the ancestors list, indented and with links.
2340     my $hierarchy = "<synopsis>\n";
2341     $level = 0;
2342     for ($i = $#ancestors; $i >= 0; $i--) {
2343         my $link_text;
2344         # Don't add a link to the current widget, i.e. when i == 0.
2345         if ($i > 0) {
2346             my $ancestor_id = &CreateValidSGMLID ($ancestors[$i]);
2347             $link_text = "<link linkend=\"$ancestor_id\">$ancestors[$i]</link>";
2348         } else {
2349             $link_text = "$ancestors[$i]";
2350         }
2351         if ($level == 0) {
2352             $hierarchy .= "  $link_text\n";
2353         } else {
2354 #           $hierarchy .= ' ' x ($level * 6 - 3) . "|\n";
2355             $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2356         }
2357         $level++;
2358     }
2359     for ($i = 0; $i <= $#children; $i++) {
2360       my $id = &CreateValidSGMLID ($children[$i]);
2361       my $link_text = "<link linkend=\"$id\">$children[$i]</link>";
2362       $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2363     }
2364     $hierarchy .= "</synopsis>\n";
2366     return $hierarchy;
2370 #############################################################################
2371 # Function    : GetInterfaces
2372 # Description : Returns the DocBook output describing the interfaces
2373 #               implemented by a class. It uses the global %Interfaces hash.
2374 # Arguments   : $object - the GtkObject subclass.
2375 #############################################################################
2377 sub GetInterfaces {
2378     my ($object) = @_;
2379     my $text = "";
2380     my $i;
2382     # Find object in the objects array.
2383     if (exists($Interfaces{$object})) {
2384         my @ifaces = split(' ', $Interfaces{$object});
2385         $text = <<EOF;
2386 <para>
2387 $object implements
2389         for ($i = 0; $i <= $#ifaces; $i++) {
2390             my $id = &CreateValidSGMLID ($ifaces[$i]);
2391             $text .= " <link linkend=\"$id\">$ifaces[$i]</link>";
2392             if ($i < $#ifaces - 1) {
2393                 $text .= ', ';
2394             }
2395             elsif ($i < $#ifaces) {
2396                 $text .= ' and ';
2397             }
2398             else {
2399                 $text .= '.';
2400             }
2401         }
2402         $text .= <<EOF;
2403 </para>
2405     }
2407     return $text;
2410 #############################################################################
2411 # Function    : GetImplementations
2412 # Description : Returns the DocBook output describing the implementations
2413 #               of an interface. It uses the global %Interfaces hash.
2414 # Arguments   : $object - the GtkObject subclass.
2415 #############################################################################
2417 sub GetImplementations {
2418     my ($object) = @_;
2419     my @impls = ();
2420     my $text = "";
2421     my $i;
2422     foreach my $key (keys %Interfaces) {
2423         if ($Interfaces{$key} =~ /\b$object\b/) {
2424             push (@impls, $key);
2425         }
2426     }
2427     if ($#impls >= 0) {
2428         $text = <<EOF;
2429 <para>
2430 $object is implemented by
2432         for ($i = 0; $i <= $#impls; $i++) {
2433             my $id = &CreateValidSGMLID ($impls[$i]);
2434             $text .= " <link linkend=\"$id\">$impls[$i]</link>";
2435             if ($i < $#impls - 1) {
2436                 $text .= ', ';
2437             }
2438             elsif ($i < $#impls) {
2439                 $text .= ' and ';
2440             }
2441             else {
2442                 $text .= '.';
2443             }
2444         }
2445         $text .= <<EOF;
2446 </para>
2448     }
2449     return $text;
2453 #############################################################################
2454 # Function    : GetPrerequisites
2455 # Description : Returns the DocBook output describing the prerequisites
2456 #               of an interface. It uses the global %Prerequisites hash.
2457 # Arguments   : $iface - the interface.
2458 #############################################################################
2460 sub GetPrerequisites {
2461     my ($iface) = @_;
2462     my $text = "";
2463     my $i;
2465     if (exists($Prerequisites{$iface})) {
2466         $text = <<EOF;
2467 <para>
2468 $iface requires
2470         my @prereqs = split(' ', $Prerequisites{$iface});
2471         for ($i = 0; $i <= $#prereqs; $i++) {
2472             my $id = &CreateValidSGMLID ($prereqs[$i]);
2473             $text .= " <link linkend=\"$id\">$prereqs[$i]</link>";
2474             if ($i < $#prereqs - 1) {
2475                 $text .= ', ';
2476             }
2477             elsif ($i < $#prereqs) {
2478                 $text .= ' and ';
2479             }
2480             else {
2481                 $text .= '.';
2482             }
2483         }
2484         $text .= <<EOF;
2485 </para>
2487     }
2488     return $text;
2491 #############################################################################
2492 # Function    : GetDerived
2493 # Description : Returns the DocBook output describing the derived interfaces
2494 #               of an interface. It uses the global %Prerequisites hash.
2495 # Arguments   : $iface - the interface.
2496 #############################################################################
2498 sub GetDerived {
2499     my ($iface) = @_;
2500     my $text = "";
2501     my $i;
2503     my @derived = ();
2504     foreach my $key (keys %Prerequisites) {
2505         if ($Prerequisites{$key} =~ /\b$iface\b/) {
2506             push (@derived, $key);
2507         }
2508     }
2509     if ($#derived >= 0) {
2510         $text = <<EOF;
2511 <para>
2512 $iface is required by
2514         for ($i = 0; $i <= $#derived; $i++) {
2515             my $id = &CreateValidSGMLID ($derived[$i]);
2516             $text .= " <link linkend=\"$id\">$derived[$i]</link>";
2517             if ($i < $#derived - 1) {
2518                 $text .= ', ';
2519             }
2520             elsif ($i < $#derived) {
2521                 $text .= ' and ';
2522             }
2523             else {
2524                 $text .= '.';
2525             }
2526         }
2527         $text .= <<EOF;
2528 </para>
2530     }
2531     return $text;
2535 #############################################################################
2536 # Function    : GetSignals
2537 # Description : Returns the synopsis and detailed description DocBook output
2538 #               for the signal handlers of a given GtkObject subclass.
2539 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
2540 #############################################################################
2542 sub GetSignals {
2543     my ($object) = @_;
2544     my $synop = "";
2545     my $desc = "";
2547     my $i;
2548     for ($i = 0; $i <= $#SignalObjects; $i++) {
2549         if ($SignalObjects[$i] eq $object) {
2550             #print "Found signal: $SignalNames[$i]\n";
2551             my $name = $SignalNames[$i];
2552             my $symbol = "${object}::${name}";
2553             my $id = &CreateValidSGMLID ("$object-$name");
2555             my $pad = ' ' x (46 - length($name));
2556             $synop .= "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad ";
2558             $desc .= "<refsect2 id=\"$id\"><title>The <literal>&quot;$name&quot;</literal> signal</title>\n";
2559             $desc .= MakeIndexterms($symbol, $id);
2560             $desc .= "<programlisting>";
2562             $SignalReturns[$i] =~ m/\s*(const\s+)?(\w+)\s*(\**)/;
2563             my $type_modifier = defined($1) ? $1 : "";
2564             my $type = $2;
2565             my $pointer = $3;
2566             my $xref = &MakeXRef ($type);
2568             my $ret_type_len = length ($type_modifier) + length ($pointer)
2569                 + length ($type);
2570             my $ret_type_output = "$type_modifier$xref$pointer"
2571                 . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
2573             $desc  .= "${ret_type_output}user_function " . &MakeReturnField("") . " (";
2575             my $sourceparams = $SourceSymbolParams{$symbol};
2576             my @params = split ("\n", $SignalPrototypes[$i]);
2577             my $j;
2578             my $l;
2579             my $type_len = length("gpointer");
2580             my $name_len = length("user_data");
2581             # do two passes, the first one is to calculate padding
2582             for ($l = 0; $l < 2; $l++) {
2583                 for ($j = 0; $j <= $#params; $j++) {
2584                     # allow alphanumerics, '_', '[' & ']' in param names
2585                     if ($params[$j] =~ m/^\s*(\w+)\s*(\**)\s*([\w\[\]]+)\s*$/) {
2586                         $type = $1;
2587                         $pointer = $2;
2588                         if (defined($sourceparams)) {
2589                             $name = $$sourceparams[2 * $j];
2590                         }
2591                         else {
2592                             $name = $3;
2593                         }
2594                         if (!defined($name)) {
2595                             $name = "arg$j";
2596                         }
2597                         if ($l == 0) {
2598                             if (length($type) + length($pointer) > $type_len) {
2599                                 $type_len = length($type) + length($pointer);
2600                             }
2601                             if (length($name) > $name_len) {
2602                                 $name_len = length($name);
2603                             }
2604                         }
2605                         else {
2606                             $xref = &MakeXRef ($type);
2607                             $pad = ' ' x ($type_len - length($type) - length($pointer));
2608                             $desc .= "$xref$pad $pointer$name,\n";
2609                             $desc .= (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
2610                         }
2611                     } else {
2612                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2613                              "Can't parse arg: $params[$j]\nArgs:$SignalPrototypes[$i]");
2614                     }
2615                 }
2616             }
2617             $xref = &MakeXRef ("gpointer");
2618             $pad = ' ' x ($type_len - length("gpointer"));
2619             $desc  .= "$xref$pad user_data)";
2621             my $flags = $SignalFlags[$i];
2622             my $flags_string = "";
2624             if (defined ($flags)) {
2625               if ($flags =~ m/f/) {
2626                 $flags_string = "Run First";
2627               }
2628               elsif ($flags =~ m/l/) {
2629                 $flags_string = "Run Last";
2630               }
2631               elsif ($flags =~ m/c/) {
2632                 $flags_string = "Cleanup";
2633               }
2634               if ($flags =~ m/r/) {
2635                 if ($flags_string) { $flags_string .= " / "; }
2636                 $flags_string .= "No Recursion";
2637               }
2638               if ($flags =~ m/d/) {
2639                 if ($flags_string) { $flags_string .= " / "; }
2640                 $flags_string .= "Has Details";
2641               }
2642               if ($flags =~ m/a/) {
2643                 if ($flags_string) { $flags_string .= " / "; }
2644                 $flags_string .= "Action";
2645               }
2646               if ($flags =~ m/h/) {
2647                 if ($flags_string) { $flags_string .= " / "; }
2648                 $flags_string .= "No Hooks";
2649               }
2650             }
2652             if ($flags_string)
2653               {
2654                 $synop .= ": $flags_string\n";
2656                 $pad = ' ' x (5 + $name_len - length("user_data"));
2657                 $desc  .= "$pad : $flags_string</programlisting>\n";
2658               }
2659             else
2660               {
2661                 $synop .= "\n";
2662                 $desc  .= "</programlisting>\n";
2663               }
2665             my $parameters = &OutputParamDescriptions ("SIGNAL", $symbol);
2666             my $parameters_output = 0;
2668             $AllSymbols{$symbol} = 1;
2669             if (defined ($SymbolDocs{$symbol})) {
2670                 my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
2672                 # Try to insert the parameter table at the author's desired
2673                 # position. Otherwise we need to tag it onto the end.
2674                 if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
2675                   $parameters_output = 1;
2676                 }
2677                 $desc .= $symbol_docs;
2679                 if (!IsEmptyDoc($SymbolDocs{$symbol})) {
2680                     $AllDocumentedSymbols{$symbol} = 1;
2681                 }
2682             }
2684             if ($parameters_output == 0) {
2685                 $desc .= $parameters;
2686               }
2688             if (exists $Since{$symbol}) {
2689               $desc .= "<para>Since $Since{$symbol}</para>";
2690             }
2691             if (exists $StabilityLevel{$symbol}) {
2692               $desc .= "<para>Stability Level: $StabilityLevel{$symbol}</para>";
2693             }
2694             $desc .= "</refsect2>";
2695         }
2696     }
2697     return ($synop, $desc);
2701 #############################################################################
2702 # Function    : GetArgs
2703 # Description : Returns the synopsis and detailed description DocBook output
2704 #               for the Args of a given GtkObject subclass.
2705 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
2706 #############################################################################
2708 sub GetArgs {
2709     my ($object) = @_;
2710     my $synop = "";
2711     my $desc = "";
2712     my $child_synop = "";
2713     my $child_desc = "";
2714     my $style_synop = "";
2715     my $style_desc = "";
2717     my $i;
2718     for ($i = 0; $i <= $#ArgObjects; $i++) {
2719         if ($ArgObjects[$i] eq $object) {
2720             #print "Found arg: $ArgNames[$i]\n";
2721             my $name = $ArgNames[$i];
2722             # Remember only one colon so we don't clash with signals.
2723             my $symbol = "${object}:${name}";
2724             # I've used two dashes here for the same reason.
2725             my $id = &CreateValidSGMLID ("$object--$name");
2727             my $type = $ArgTypes[$i];
2728             my $type_output;
2729             my $range = $ArgRanges[$i];
2730             my $range_output = CreateValidSGML($range);
2731             my $default = $ArgDefaults[$i];
2732             my $default_output = CreateValidSGML($default);
2734             if ($type eq "GtkString") {
2735                 $type = "char*";
2736             }
2737             if ($type eq "GtkSignal") {
2738                 $type = "GtkSignalFunc, gpointer";
2739                 $type_output = &MakeXRef ("GtkSignalFunc") . ", "
2740                     . &MakeXRef ("gpointer");
2741             } elsif ($type =~ m/^(\w+)\*$/) {
2742                 $type_output = &MakeXRef ($1) . "*";
2743             } else {
2744                 $type_output = &MakeXRef ($type);
2745             }
2747             my $flags = $ArgFlags[$i];
2748             my $flags_string = "";
2750             if ($flags =~ m/r/) {
2751                 $flags_string = "Read";
2752             }
2753             if ($flags =~ m/w/) {
2754                 if ($flags_string) { $flags_string .= " / "; }
2755                 $flags_string .= "Write";
2756             }
2757             if ($flags =~ m/x/) {
2758                 if ($flags_string) { $flags_string .= " / "; }
2759                 $flags_string .= "Construct";
2760             }
2761             if ($flags =~ m/X/) {
2762                 if ($flags_string) { $flags_string .= " / "; }
2763                 $flags_string .= "Construct Only";
2764             }
2766             $AllSymbols{$symbol} = 1;
2767             my $blurb;
2768             if (defined($SymbolDocs{$symbol}) &&
2769                 !IsEmptyDoc($SymbolDocs{$symbol})) {
2770                 $blurb = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
2771                 $AllDocumentedSymbols{$symbol} = 1;
2772             }
2773             else {
2774                 if (!($ArgBlurbs[$i] eq "")) {
2775                     $AllDocumentedSymbols{$symbol} = 1;
2776                 }
2777                 $blurb = "<para>" . &CreateValidSGML ($ArgBlurbs[$i]) . "</para>";
2778             }
2780             my $pad1 = " " x (24 - length ($name));
2781             my $pad2 = " " x (20 - length ($type));
2782             my $kind = "";
2784             if ($flags =~ m/c/) {
2785                 $kind = "child property";
2786             }
2787             elsif ($flags =~ m/s/) {
2788                 $kind = "style property";
2789             }
2790             else {
2791                 $kind = "property";
2792             }
2794             my $arg_synop = "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad1 $type_output $pad2 : $flags_string\n";
2795             my $arg_desc = "<refsect2 id=\"$id\"><title>The <literal>&quot;$name&quot;</literal> $kind</title>\n";
2796             $arg_desc .= MakeIndexterms($symbol, $id);
2797             $arg_desc .= "<programlisting>  &quot;$name&quot;$pad1 $type_output $pad2 : $flags_string</programlisting>\n";
2798             $arg_desc .= $blurb;
2799             if ($range ne "") {
2800                 $arg_desc .= "<para>Allowed values: $range_output</para>\n";
2801             }
2802             if ($default ne "") {
2803                 $arg_desc .= "<para>Default value: $default_output</para>\n";
2804             }
2805             if (exists $Since{$symbol}) {
2806               $arg_desc .= "<para>Since $Since{$symbol}</para>\n";
2807             }
2808             if (exists $StabilityLevel{$symbol}) {
2809               $arg_desc .= "<para>Stability Level $StabilityLevel{$symbol}</para>\n";
2810             }
2811             $arg_desc .= "</refsect2>\n";
2813             if ($flags =~ m/c/) {
2814                 $child_synop .= $arg_synop;
2815                 $child_desc .= $arg_desc;
2816             }
2817             elsif ($flags =~ m/s/) {
2818                 $style_synop .= $arg_synop;
2819                 $style_desc .= $arg_desc;
2820             }
2821             else {
2822                 $synop .= $arg_synop;
2823                 $desc .= $arg_desc;
2824             }
2825         }
2826     }
2827     return ($synop, $child_synop, $style_synop, $desc, $child_desc, $style_desc);
2831 #############################################################################
2832 # Function    : ReadSourceDocumentation
2833 # Description : This reads in the documentation embedded in comment blocks
2834 #               in the source code (for Gnome).
2836 #               Parameter descriptions override any in the template files.
2837 #               Function descriptions are placed before any description from
2838 #               the template files.
2840 #               It recursively descends the source directory looking for .c
2841 #               files and scans them looking for specially-formatted comment
2842 #               blocks.
2844 # Arguments   : $source_dir - the directory to scan.
2845 #############m###############################################################
2847 sub ReadSourceDocumentation {
2848     my ($source_dir) = @_;
2849     my ($file, $dir, @suffix_list, $suffix);
2850     #print "Scanning source directory: $source_dir\n";
2852     # This array holds any subdirectories found.
2853     my (@subdirs) = ();
2855     @suffix_list = split (/,/, $SOURCE_SUFFIXES);
2857     opendir (SRCDIR, $source_dir)
2858         || die "Can't open source directory $source_dir: $!";
2860     foreach $file (readdir (SRCDIR)) {
2861       if ($file =~ /^\./) {
2862         next;
2863       } elsif (-d "$source_dir/$file") {
2864         push (@subdirs, $file);
2865       } elsif (@suffix_list) {
2866         foreach $suffix (@suffix_list) {
2867           if ($file =~ m/\.\Q${suffix}\E$/) {
2868             &ScanSourceFile ("$source_dir/$file");
2869           }
2870         }
2871       } elsif ($file =~ m/\.[ch]$/) {
2872         &ScanSourceFile ("$source_dir/$file");
2873       }
2874     }
2875     closedir (SRCDIR);
2877     # Now recursively scan the subdirectories.
2878     foreach $dir (@subdirs) {
2879         next if ($IGNORE_FILES =~ m/(\s|^)\Q${dir}\E(\s|$)/);
2880         &ReadSourceDocumentation ("$source_dir/$dir");
2881     }
2885 #############################################################################
2886 # Function    : ScanSourceFile
2887 # Description : Scans one source file looking for specially-formatted comment
2888 #               blocks. Later &MergeSourceDocumentation is used to merge any
2889 #               documentation found with the documentation already read in
2890 #               from the template files.
2892 # Arguments   : $file - the file to scan.
2893 #############################################################################
2895 sub ScanSourceFile {
2896     my ($file) = @_;
2897     my $basename;
2899     if ($file =~ m/^.*[\/\\]([^\/\\]*)$/) {
2900         $basename = $1;
2901     } else {
2902         &LogWarning ($file, 1, "Can't find basename for this filename.");
2903         $basename = $file;
2904     }
2906     # Check if the basename is in the list of files to ignore.
2907     if ($IGNORE_FILES =~ m/(\s|^)\Q${basename}\E(\s|$)/) {
2908         return;
2909     }
2911     #print "DEBUG: Scanning $file\n";
2913     open (SRCFILE, $file)
2914         || die "Can't open $file: $!";
2915     my $in_comment_block = 0;
2916     my $symbol;
2917     my ($in_description, $in_return, $in_since, $in_stability, $in_deprecated);
2918     my ($description, $return_desc, $return_start, $return_style);
2919     my ($since_desc, $stability_desc, $deprecated_desc);
2920     my $current_param;
2921     my $ignore_broken_returns;
2922     my @params;
2923     while (<SRCFILE>) {
2924         # Look for the start of a comment block.
2925         if (!$in_comment_block) {
2926             if (m%^\s*/\*.*\*/%) {
2927                 #one-line comment - not gtkdoc
2928             } elsif (m%^\s*/\*\*\s%) {
2929                 #print "Found comment block start\n";
2931                 $in_comment_block = 1;
2933                 # Reset all the symbol data.
2934                 $symbol = "";
2935                 $in_description = 0;
2936                 $in_return = 0;
2937                 $in_since = 0;
2938                 $in_deprecated = 0;
2939                 $in_stability = 0;
2940                 $description = "";
2941                 $return_desc = "";
2942                 $return_style = "";
2943                 $since_desc = "";
2944                 $deprecated_desc = "";
2945                 $stability_desc = "";
2946                 $current_param = -1;
2947                 $ignore_broken_returns = 0;
2948                 @params = ();
2949             }
2950             next;
2951         }
2953         # We're in a comment block. Check if we've found the end of it.
2954         if (m%^\s*\*+/%) {
2955             if (!$symbol) {
2956                 &LogWarning ($file, $., "Symbol name not found at the start of the comment block.");
2957             } else {
2958                 # Add the return value description onto the end of the params.
2959                 if ($return_desc) {
2960                     push (@params, "Returns");
2961                     push (@params, $return_desc);
2962                     if ($return_style eq 'broken') {
2963                         &LogWarning ($file, $., "Free-form return value description in $symbol. Use `Returns:' to avoid ambiguities.");
2964                     }
2965                 }
2966                 # Convert special SGML characters
2967                 $description = &ConvertSGMLChars ($symbol, $description);
2968                 my $k;
2969                 for ($k = 1; $k <= $#params; $k += 2) {
2970                     $params[$k] = &ConvertSGMLChars ($symbol, $params[$k]);
2971                 }
2973                 # Handle Section docs
2974                 if ($symbol =~ m/SECTION:\s*(.*)/) {
2975                     my $real_symbol=$1;
2976                     my $key;
2978                     #print "SECTION DOCS found in source for : '$real_symbol'\n";
2979                     $ignore_broken_returns = 1;
2980                     for ($k = 0; $k <= $#params; $k += 2) {
2981                         #print "   '".$params[$k]."'\n";
2982                         $params[$k] = "\L$params[$k]";
2983                         undef $key;
2984                         if ($params[$k] eq "short_description") {
2985                             $key = "$TMPL_DIR/$real_symbol:Short_Description";
2986                         } elsif ($params[$k] eq "see_also") {
2987                             $key = "$TMPL_DIR/$real_symbol:See_Also";
2988                         } elsif ($params[$k] eq "title") {
2989                             $key = "$TMPL_DIR/$real_symbol:Title";
2990                         } elsif ($params[$k] eq "stability") {
2991                             $key = "$TMPL_DIR/$real_symbol:Stability_Level";
2992                         } elsif ($params[$k] eq "section_id") {
2993                             $key = "$TMPL_DIR/$real_symbol:Section_Id";
2994                         } elsif ($params[$k] eq "include") {
2995                             $key = "$TMPL_DIR/$real_symbol:Include";
2996                         }
2997                         if (defined($key)) {
2998                             $SourceSymbolDocs{$key}=$params[$k+1];
2999                             $SourceSymbolSourceFile{$key} = $file;
3000                             $SourceSymbolSourceLine{$key} = $.;
3001                         }
3002                     }
3003                     $SourceSymbolDocs{"$TMPL_DIR/$real_symbol:Long_Description"}=$description;
3004                     $SourceSymbolSourceFile{"$TMPL_DIR/$real_symbol:Long_Description"} = $file;
3005                     $SourceSymbolSourceLine{"$TMPL_DIR/$real_symbol:Long_Description"} = $.;
3006                     #$SourceSymbolTypes{$symbol} = "SECTION";
3007                 } else {
3008                     #print "SYMBOL DOCS found in source for : '$symbol' ",length($description), "\n";
3009                     $SourceSymbolDocs{$symbol} = $description;
3010                     $SourceSymbolParams{$symbol} = [ @params ];
3011                     # FIXME $SourceSymbolTypes{$symbol} = "STRUCT,SIGNAL,ARG,FUNCTION,MACRO";
3012                     #if (defined $DeclarationTypes{$symbol}) {
3013                     #    $SourceSymbolTypes{$symbol} = $DeclarationTypes{$symbol}
3014                     #}
3015                     $SourceSymbolSourceFile{$symbol} = $file;
3016                     $SourceSymbolSourceLine{$symbol} = $.;
3017                 }
3019                 if ($since_desc) {
3020                     $Since{$symbol} = &ConvertSGMLChars ($symbol, $since_desc);
3021                 }
3023                 if ($stability_desc) {
3024                     $stability_desc = &ParseStabilityLevel($stability_desc, $file, $., "Stability level for $symbol");
3025                     $StabilityLevel{$symbol} = &ConvertSGMLChars ($symbol, $stability_desc);
3026                 }
3028                 if ($deprecated_desc) {
3029                     if (exists $Deprecated{$symbol}) {
3030                     }
3031                     else {
3032                          # don't warn for signals and properties
3033                          #if ($symbol !~ m/::?(.*)/) {
3034                          if (defined $DeclarationTypes{$symbol}) {
3035                              &LogWarning ($file, $., 
3036                                  "$symbol is deprecated in the inline comments, but no deprecation guards were found around the declaration.".
3037                                  " (See the --deprecated-guards option for gtkdoc-scan.)");
3038                          }
3039                     }
3040                     $Deprecated{$symbol} = &ConvertSGMLChars ($symbol, $deprecated_desc);
3041                 }
3042             }
3044             $in_comment_block = 0;
3045             next;
3046         }
3048         # Get rid of ' * ' at start of every line in the comment block.
3049         s%^\s*\*\s?%%;
3050         # But make sure we don't get rid of the newline at the end.
3051         if (!$_) {
3052             $_ = "\n";
3053         }
3054         #print "DEBUG: scanning :$_";
3056         # If we haven't found the symbol name yet, look for it.
3057         if (!$symbol) {
3058             if (m%^\s*(SECTION:\s*\S+)%) {
3059                 $symbol = $1;
3060                 #print "SECTION DOCS found in source for : '$symbol'\n";
3061                 $ignore_broken_returns = 1;
3062             } elsif (m%^\s*([\w:-]*\w)\s*:?%) {
3063                 $symbol = $1;
3064                 #print "SYMBOL DOCS found in source for : '$symbol'\n";
3065             }
3066             next;
3067         }
3069         # If we're in the return value description, add it to the end.
3070         if ($in_return) {
3071             # If we find another valid returns line, we assume that the first
3072             # one was really part of the description.
3073             if (m/^\s*(returns:|return\s+value:)/i) {
3074                 if ($return_style eq 'broken') {
3075                     $description .= $return_start . $return_desc;
3076                 }
3077                 $return_start = $1;
3078                 if ($return_style eq 'sane') {
3079                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3080                 }
3081                 $return_style = 'sane';
3082                 $ignore_broken_returns = 1;
3083                 $return_desc = $';
3084             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3085                 $description .= $return_start . $return_desc;
3086                 $return_start = $1;
3087                 $return_style = 'broken';
3088                 $return_desc = $';
3089             } elsif (m%^\s*since:%i) {
3090                 $since_desc = $';
3091                 $in_since = 1;
3092                 $in_return = 0;
3093             } elsif (m%^\s*stability:%i) {
3094                 $stability_desc = $';
3095                 $in_stability = 1;
3096                 $in_return = 0;
3097             } elsif (m%^\s*deprecated:%i) {
3098                 $deprecated_desc = $';
3099                 $in_deprecated = 1;
3100                 $in_return = 0;
3101             } else {
3102                 $return_desc .= $_;
3103             }
3104             next;
3105         }
3107         if ($in_since) {
3108             if (m/^\s*(returns:|return\s+value:)/i) {
3109                 if ($return_style eq 'broken') {
3110                     $description .= $return_start . $return_desc;
3111                 }
3112                 $return_start = $1;
3113                 if ($return_style eq 'sane') {
3114                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3115                 }
3116                 $return_style = 'sane';
3117                 $ignore_broken_returns = 1;
3118                 $return_desc = $';
3119                 $in_return = 1;
3120                 $in_since = 0;
3121             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3122                 $return_start = $1;
3123                 $return_style = 'broken';
3124                 $return_desc = $';
3125                 $in_return = 1;
3126                 $in_since = 0;
3127             } elsif (m%^\s*deprecated:%i) {
3128                 $deprecated_desc = $';
3129                 $in_deprecated = 1;
3130                 $in_since = 0;
3131             } elsif (m%^\s*stability:%i) {
3132                 $stability_desc = $';
3133                 $in_stability = 1;
3134                 $in_since = 0;
3135             } else {
3136                 $since_desc .= $_;
3137             }
3138             next;
3139         }
3141         if ($in_stability) {
3142             if (m/^\s*(returns:|return\s+value:)/i) {
3143                 if ($return_style eq 'broken') {
3144                     $description .= $return_start . $return_desc;
3145                 }
3146                 $return_start = $1;
3147                 if ($return_style eq 'sane') {
3148                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3149                 }
3150                 $return_style = 'sane';
3151                 $ignore_broken_returns = 1;
3152                 $return_desc = $';
3153                 $in_return = 1;
3154                 $in_stability = 0;
3155             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3156                 $return_start = $1;
3157                 $return_style = 'broken';
3158                 $return_desc = $';
3159                 $in_return = 1;
3160                 $in_stability = 0;
3161             } elsif (m%^\s*deprecated:%i) {
3162                 $deprecated_desc = $';
3163                 $in_deprecated = 1;
3164                 $in_stability = 0;
3165             } elsif (m%^\s*since:%i) {
3166                 $since_desc = $';
3167                 $in_since = 1;
3168                 $in_stability = 0;
3169             } else {
3170                 $stability_desc .= $_;
3171             }
3172             next;
3173         }
3175         if ($in_deprecated) {
3176             if (m/^\s*(returns:|return\s+value:)/i) {
3177                 if ($return_style eq 'broken') {
3178                     $description .= $return_start . $return_desc;
3179                 }
3180                 $return_start = $1;
3181                 if ($return_style eq 'sane') {
3182                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3183                 }
3184                 $return_style = 'sane';
3185                 $ignore_broken_returns = 1;
3186                 $return_desc = $';
3187                 $in_return = 1;
3188                 $in_deprecated = 0;
3189             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3190                 $return_start = $1;
3191                 $return_style = 'broken';
3192                 $return_desc = $';
3193                 $in_return = 1;
3194                 $in_deprecated = 0;
3195             } elsif (m%^\s*since:%i) {
3196                 $since_desc = $';
3197                 $in_since = 1;
3198                 $in_deprecated = 0;
3199             } elsif (m%^\s*stability:%i) {
3200                 $stability_desc = $';
3201                 $in_stability = 1;
3202                 $in_deprecated = 0;
3203             } else {
3204                 $deprecated_desc .= $_;
3205             }
3206             next;
3207         }
3209         # If we're in the description part, check for the 'Returns:' line.
3210         # If that isn't found, add the text to the end.
3211         if ($in_description) {
3212             # Get rid of 'Description:'
3213             s%^\s*Description:%%;
3215             if (m/^\s*(returns:|return\s+value:)/i) {
3216                 if ($return_style eq 'broken') {
3217                     $description .= $return_start . $return_desc;
3218                 }
3219                 $return_start = $1;
3220                 if ($return_style eq 'sane') {
3221                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3222                 }
3223                 $return_style = 'sane';
3224                 $ignore_broken_returns = 1;
3225                 $return_desc = $';
3226                 $in_return = 1;
3227                 next;
3228             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3229                 $return_start = $1;
3230                 $return_style = 'broken';
3231                 $return_desc = $';
3232                 $in_return = 1;
3233                 next;
3234             } elsif (m%^\s*since:%i) {
3235                 $since_desc = $';
3236                 $in_since = 1;
3237                 next;
3238             } elsif (m%^\s*deprecated:%i) {
3239                 $deprecated_desc = $';
3240                 $in_deprecated = 1;
3241                 next;
3242             } elsif (m%^\s*stability:%i) {
3243                 $stability_desc = $';
3244                 $in_stability = 1;
3245                 next;
3246             }
3248             $description .= $_;
3249             next;
3250         }
3252         # We must be in the parameters. Check for the empty line below them.
3253         if (m%^\s*$%) {
3254             $in_description = 1;
3255             next;
3256         }
3258         # Look for a parameter name.
3259         if (m%^\s*@(\S+)\s*:%) {
3260             my $param_name = $1;
3261             #print "Found parameter: $param_name\n";
3262             # Allow '...' as the Varargs parameter.
3263             if ($param_name eq "...") {
3264                 $param_name = "Varargs";
3265             }
3266             if ("\L$param_name" eq "returns") {
3267                 $return_style = 'sane';
3268                 $ignore_broken_returns = 1;
3269             }
3270             push (@params, $param_name);
3271             push (@params, $');
3272             $current_param += 2;
3273             next;
3274         }
3276         # We must be in the middle of a parameter description, so add it on
3277         # to the last element in @params.
3278         if ($current_param == -1) {
3279             &LogWarning ($file, $., "Parsing comment block file : parameter expected.");
3280         } else {
3281             $params[$#params] .= $_;
3282         }
3283     }
3284     close (SRCFILE);
3287 #############################################################################
3288 # Function    : OutputMissingDocumentation
3289 # Description : Outputs report of documentation coverage to a file
3291 # Arguments   : none
3292 #############################################################################
3294 sub OutputMissingDocumentation {
3295      my $n_documented = 0;
3296      my $n_incomplete = 0;
3297      my $total = 0;
3298      my $symbol;
3299      my $percent;
3300      my $msg;
3301      my $buffer = "";
3302      my $buffer_deprecated = "";
3303      my $buffer_descriptions = "";
3305      open (UNDOCUMENTED, ">$ROOT_DIR/$MODULE-undocumented.txt")
3306           || die "Can't create $ROOT_DIR/$MODULE-undocumented.txt: $!";
3308      foreach $symbol (sort (keys (%AllSymbols))) {
3309           if ($symbol !~ /:(Title|Long_Description|Short_Description|See_Also|Stability_Level|Include)/) {
3310               $total++;
3311               if (exists ($AllDocumentedSymbols{$symbol})) {
3312                   $n_documented++;
3313                   if (exists ($AllIncompleteSymbols{$symbol})) {
3314                       $n_incomplete++;
3315                       $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3316                   }
3317               } elsif (exists $Deprecated{$symbol}) {
3318                   if (exists ($AllIncompleteSymbols{$symbol})) {
3319                       $n_incomplete++;
3320                       $buffer_deprecated .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3321                   } else {
3322                       $buffer_deprecated .= $symbol . "\n";
3323                   }
3324               } else {
3325                   if (exists ($AllIncompleteSymbols{$symbol})) {
3326                       $n_incomplete++;
3327                       $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3328                   } else {
3329                       $buffer .= $symbol . "\n";
3330                   }
3331               }
3332           } elsif ($symbol =~ /:(Long_Description|Short_Description)/) {
3333               $total++;
3334               #my $len1=(exists($SymbolDocs{$symbol}))?length($SymbolDocs{$symbol}):-1;
3335               #my $len2=(exists($AllDocumentedSymbols{$symbol}))?length($AllDocumentedSymbols{$symbol}):-1;
3336               #print "%%%% $symbol : $len1,$len2\n";
3337               if (((exists ($SymbolDocs{$symbol})) && (length ($SymbolDocs{$symbol}) > 0))
3338               || ((exists ($AllDocumentedSymbols{$symbol})) && (length ($AllDocumentedSymbols{$symbol}) > 0))) {
3339                   $n_documented++;
3340               } else {
3341                   $symbol =~ m/^.*\/(.*)$/;
3342                   $buffer_descriptions .= $1 . "\n";
3343               }
3344           }
3345      }
3347      $buffer .= "\n" . $buffer_deprecated . "\n" . $buffer_descriptions;
3349      if ($total == 0) {
3350           $percent = 100;
3351      } else {
3352           $percent = ($n_documented / $total) * 100.0;
3353      }
3355      printf UNDOCUMENTED "%.0f%% symbol docs coverage.\n", $percent;
3356      print UNDOCUMENTED "$n_documented symbols documented.\n";
3357      print UNDOCUMENTED "$n_incomplete symbols incomplete.\n";
3358      print UNDOCUMENTED ($total - $n_documented) . " not documented.\n\n\n";
3360      print UNDOCUMENTED $buffer;
3362      close (UNDOCUMENTED);
3364      printf (("%.0f%% symbol docs coverage ($n_documented symbols documented, $n_incomplete symbols incomplete, " . ($total - $n_documented) . " not documented)\nSee $MODULE-undocumented.txt for a list of missing docs.\nThe doc coverage percentage doesn't include intro sections.\n"), $percent);
3368 #############################################################################
3369 # Function    : OutputUndeclaredSymbols
3370 # Description : Outputs symbols that are undeclared yet documented to a file
3372 # Arguments   : none
3373 #############################################################################
3375 sub OutputUndeclaredSymbols {
3376     open(UNDECLARED, ">$ROOT_DIR/$MODULE-undeclared.txt")
3377         || die "Can't create $ROOT_DIR/$MODULE-undeclared.txt";
3379     if (%UndeclaredSymbols) {
3380         print UNDECLARED (join("\n", sort keys %UndeclaredSymbols));
3381         print UNDECLARED "\n";
3382         print "See $MODULE-undeclared.txt for the list of undeclared symbols.\n"
3383     }
3384     close(UNDECLARED);
3388 #############################################################################
3389 # Function    : OutputAllSymbols
3390 # Description : Outputs list of all symbols to a file
3392 # Arguments   : none
3393 #############################################################################
3395 sub OutputAllSymbols {
3396      my $n_documented = 0;
3397      my $total = 0;
3398      my $symbol;
3399      my $percent;
3400      my $msg;
3402      open (SYMBOLS, ">$ROOT_DIR/$MODULE-symbols.txt")
3403           || die "Can't create $ROOT_DIR/$MODULE-symbols.txt: $!";
3405      foreach $symbol (sort (keys (%AllSymbols))) {
3406           print SYMBOLS $symbol . "\n"
3407      }
3409      close (SYMBOLS);
3413 #############################################################################
3414 # Function    : MergeSourceDocumentation
3415 # Description : This merges documentation read from a source file into the
3416 #               documentation read in from a template file.
3418 #               Parameter descriptions override any in the template files.
3419 #               Function descriptions are placed before any description from
3420 #               the template files.
3422 # Arguments   : none
3423 #############################################################################
3425 sub MergeSourceDocumentation {
3426     my $symbol;
3427     my @Symbols;
3429     if (scalar %SymbolDocs) {
3430         @Symbols=keys (%SymbolDocs);
3431         #print "num existing entries: ".(scalar @Symbols)."\n";
3432         #print "  ",$Symbols[0], " ",$Symbols[1],"\n";
3433     }
3434     else {
3435         # filter scanned declarations, with what we suppress from -sections.txt
3436         my %tmp = ();
3437         foreach $symbol (keys (%Declarations)) {
3438             if ($KnownSymbols{$symbol}) {
3439                 $tmp{$symbol}=1;
3440             }
3441         }
3442         # and add whats found in the source
3443         foreach $symbol (keys (%SourceSymbolDocs)) {
3444             $tmp{$symbol}=1;
3445         }
3446         @Symbols = keys (%tmp);
3447         #print "num source entries: ".(scalar @Symbols)."\n";
3448     }
3449     foreach $symbol (@Symbols) {
3450         $AllSymbols{$symbol} = 1;
3452         my $have_tmpl_docs = 0;
3454         ## See if the symbol is documented in template
3455         my $tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
3456         my $check_tmpl_doc =$tmpl_doc;
3457         # remove all xml-tags and whitespaces
3458         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
3459         $check_tmpl_doc =~ s/<.*?>//g;
3460         $check_tmpl_doc =~ s/\s//g;
3461         # anything left ?
3462         if ($check_tmpl_doc ne "") {
3463             $have_tmpl_docs = 1;
3464             #print "## [$check_tmpl_doc]\n";
3465         }
3467         if (exists ($SourceSymbolDocs{$symbol})) {
3468             my $type = $DeclarationTypes {$symbol};
3470             #print "merging [$symbol] from source\n";
3472             my $item = "Parameter";
3473             if (defined ($type)) {
3474                 if ($type eq 'STRUCT') {
3475                     $item = "Field";
3476                 } elsif ($type eq 'ENUM') {
3477                     $item = "Value";
3478                 } elsif ($type eq 'UNION') {
3479                     $item = "Field";
3480                 }
3481             } else {
3482                 $type="SIGNAL";
3483             }
3485             my $src_doc = $SourceSymbolDocs{$symbol};
3486             # remove leading and training whitespaces
3487             $src_doc =~ s/^\s+//;
3488             $src_doc =~ s/\s+$//;
3490             # Don't output warnings for overridden titles as titles are
3491             # automatically generated in the -sections.txt file, and thus they
3492             # are often overridden.
3493             if ($have_tmpl_docs && $symbol !~ m/:Title$/) {
3494                 # check if content is different
3495                 if ($tmpl_doc ne $src_doc) {
3496                     #print "[$tmpl_doc] [$src_doc]\n";
3497                     &LogWarning ($SourceSymbolSourceFile{$symbol}, $SourceSymbolSourceLine{$symbol},
3498                         "Documentation in template ".$SymbolSourceFile{$symbol}.":".$SymbolSourceLine{$symbol}." for $symbol being overridden by inline comments.");
3499                 }
3500             }
3502             if ($src_doc ne "") {
3503                  $AllDocumentedSymbols{$symbol} = 1;
3504             }
3506             # Convert <!--PARAMETERS--> with any blank lines around it to
3507             # a </para> followed by <!--PARAMETERS--> followed by <para>.
3508             $src_doc =~ s%\n+\s*<!--PARAMETERS-->\s*\n+%\n</para>\n<!--PARAMETERS-->\n<para>\n%g;
3510             # If there is a blank line, finish the paragraph and start another.
3511             $src_doc = &ConvertBlankLines ($src_doc, $symbol);
3513             if ($symbol =~ m/$TMPL_DIR\/.+:Long_Description/) {
3514                 $SymbolDocs{$symbol} = "<para>\n$src_doc</para>\n$tmpl_doc";
3515             } elsif ($symbol =~ m/$TMPL_DIR\/.+:.+/) {
3516                 # For the title/summary/see also section docs we don't want to
3517                 # add any <para> tags.
3518                 $SymbolDocs{$symbol} = "$src_doc"
3519             } else {
3520                 $SymbolDocs{$symbol} = "<para>\n$src_doc</para>\n$tmpl_doc";
3521             }
3523             # merge parameters
3524             if ($symbol =~ m/.*::.*/) {
3525                 # For signals we prefer the param names from the source docs,
3526                 # since the ones from the templates are likely to contain the
3527                 # artificial argn names which are generated by gtkdoc-scangobj.
3528                 $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
3529                 # FIXME: we need to check for empty docs here as well!
3530             } else {
3531                 # The templates contain the definitive parameter names and order,
3532                 # so we will not change that. We only override the actual text.
3533                 my $tmpl_params = $SymbolParams{$symbol};
3534                 if (!defined ($tmpl_params)) {
3535                     #print "No merge needed for $symbol\n";
3536                     $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
3537                 } else {
3538                     my $params = $SourceSymbolParams{$symbol};
3539                     my $j;
3540                     #print "Merge needed for $symbol, tmpl_params: ",$#$tmpl_params,", source_params: ",$#$params," \n";
3541                     for ($j = 0; $j <= $#$tmpl_params; $j += 2) {
3542                         my $tmpl_param_name = $$tmpl_params[$j];
3544                         # Allow '...' as the Varargs parameter.
3545                         if ($tmpl_param_name eq "...") {
3546                             $tmpl_param_name = "Varargs";
3547                         }
3549                         # Try to find the param in the source comment documentation.
3550                         my $found = 0;
3551                         my $k;
3552                         for ($k = 0; $k <= $#$params; $k += 2) {
3553                             my $param_name = $$params[$k];
3554                             my $param_desc = $$params[$k + 1];
3556                             # We accept changes in case, since the Gnome source docs
3557                             # contain a lot of these.
3558                             if ("\L$param_name" eq "\L$tmpl_param_name") {
3559                                 $found = 1;
3561                                 # Override the description.
3562                                 $$tmpl_params[$j + 1] = $param_desc;
3564                                 # Set the name to "" to mark it as used.
3565                                 $$params[$k] = "";
3566                                 last;
3567                             }
3568                         }
3570                         # If it looks like the parameters are there, but not
3571                         # in the right place, try to explain a bit better.
3572                         if ((!$found) && ($src_doc =~ m/\@$tmpl_param_name:/)) {
3573                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3574                                 "Parameters for $symbol must start on the line immediately after the function or macro name.");
3575                         }
3576                     }
3578                     # Now we output a warning if parameters have been described which
3579                     # do not exist.
3580                     for ($j = 0; $j <= $#$params; $j += 2) {
3581                         my $param_name = $$params[$j];
3582                         if ($param_name) {
3583                             # the template builder cannot detect if a macro returns
3584                             # a result or not
3585                             if(($type eq "MACRO") && ($param_name eq "Returns")) {
3586                                 next;
3587                             }
3588                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3589                                 "$item described in source code comment block but does not exist. $type: $symbol $item: $param_name.");
3590                         }
3591                     }
3592                 }
3593             }
3594         } else {
3595             if ($have_tmpl_docs) {
3596                 $AllDocumentedSymbols{$symbol} = 1;
3597                 #print "merging [$symbol] from template\n";
3598             }
3599             else {
3600                 #print "[$symbol] undocumented\n";
3601             }
3602         }
3604         # if this symbol is documented, check if docs are complete
3605         $check_tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
3606         # remove all xml-tags and whitespaces
3607         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
3608         $check_tmpl_doc =~ s/<.*?>//g;
3609         $check_tmpl_doc =~ s/\s//g;
3610         if ($check_tmpl_doc ne "") {
3611             my $tmpl_params = $SymbolParams{$symbol};
3612             if (defined ($tmpl_params)) {
3613                 my $type = $DeclarationTypes {$symbol};
3615                 my $item = "Parameter";
3616                 if (defined ($type)) {
3617                     if ($type eq 'STRUCT') {
3618                         $item = "Field";
3619                     } elsif ($type eq 'ENUM') {
3620                         $item = "Value";
3621                     } elsif ($type eq 'UNION') {
3622                         $item = "Field";
3623                     }
3624                 } else {
3625                     $type="SIGNAL";
3626                 }
3628                 #print "Check param docs for $symbol, tmpl_params: ",$#$tmpl_params,"\n";
3630                 my $j;
3631                 for ($j = 0; $j <= $#$tmpl_params; $j += 2) {
3632                     # Output a warning if the parameter is empty and
3633                     # remember for stats.
3634                     my $tmpl_param_name = $$tmpl_params[$j];
3635                     my $tmpl_param_desc = $$tmpl_params[$j + 1];
3636                     if ($tmpl_param_desc !~ m/\S/) {
3637                         if (exists ($AllIncompleteSymbols{$symbol})) {
3638                             $AllIncompleteSymbols{$symbol}.=", ".$tmpl_param_name;
3639                         } else {
3640                             $AllIncompleteSymbols{$symbol}=$tmpl_param_name;
3641                         }
3642                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3643                             "$item description for $tmpl_param_name is missing in source code comment block.");
3644                     }
3645                 }
3646             }
3647         }
3648    }
3649    #print "num doc entries: ".(scalar %SymbolDocs)."\n";
3652 sub IsEmptyDoc {
3653     my ($doc) = @_;
3655     if ($doc =~ /^\s*<para>\s*(FIXME)?\s*<\/para>\s*$/) {
3656         return 1;
3657     } else {
3658         return 0;
3659     }
3663 # This converts blank lines to "</para><para>", but only outside CDATA and
3664 # <programlisting> tags.
3665 sub ConvertBlankLines {
3666     return &ModifyXMLElements ($_[0], $_[1],
3667                                "<!\\[CDATA\\[|<programlisting[^>]*>|\\|\\[",
3668                                \&ConvertBlankLinesEndTag,
3669                                \&ConvertBlankLinesCallback);
3673 sub ConvertBlankLinesEndTag {
3674   if ($_[0] eq "<!\[CDATA\[") {
3675     return "]]>";
3676   } elsif ($_[0] eq "|[") {
3677     return "]\\|";
3678   } else {
3679     return "</programlisting>";
3680   }
3683 sub ConvertBlankLinesCallback {
3684   my ($text, $symbol, $tag) = @_;
3686   # If we're not in CDATA or a <programlisting> we convert blank lines so
3687   # they start a new <para>.
3688   if ($tag eq "") {
3689     $text =~ s%\n{2,}%\n</para>\n<para>\n%g;
3690   }
3692   return $text;
3696 #############################################################################
3697 # LIBRARY FUNCTIONS -   These functions are used in both gtkdoc-mkdb and
3698 #                       gtkdoc-mktmpl and should eventually be moved to a
3699 #                       separate library.
3700 #############################################################################
3702 #############################################################################
3703 # Function    : ReadDeclarationsFile
3704 # Description : This reads in a file containing the function/macro/enum etc.
3705 #               declarations.
3707 #               Note that in some cases there are several declarations with
3708 #               the same name, e.g. for conditional macros. In this case we
3709 #               set a flag in the %DeclarationConditional hash so the
3710 #               declaration is not shown in the docs.
3712 #               If a macro and a function have the same name, e.g. for
3713 #               gtk_object_ref, the function declaration takes precedence.
3715 #               Some opaque structs are just declared with 'typedef struct
3716 #               _name name;' in which case the declaration may be empty.
3717 #               The structure may have been found later in the header, so
3718 #               that overrides the empty declaration.
3720 # Arguments   : $file - the declarations file to read
3721 #               $override - if declarations in this file should override
3722 #                       any current declaration.
3723 #############################################################################
3725 sub ReadDeclarationsFile {
3726     my ($file, $override) = @_;
3728     if ($override == 0) {
3729         %Declarations = ();
3730         %DeclarationTypes = ();
3731         %DeclarationConditional = ();
3732         %DeclarationOutput = ();
3733     }
3735     open (INPUT, $file)
3736         || die "Can't open $file: $!";
3737     my $declaration_type = "";
3738     my $declaration_name;
3739     my $declaration;
3740     my $is_deprecated = 0;
3741     while (<INPUT>) {
3742         if (!$declaration_type) {
3743             if (m/^<([^>]+)>/) {
3744                 $declaration_type = $1;
3745                 $declaration_name = "";
3746                 #print "Found declaration: $declaration_type\n";
3747                 $declaration = "";
3748             }
3749         } else {
3750             if (m%^<NAME>(.*)</NAME>%) {
3751                 $declaration_name = $1;
3752             } elsif (m%^<DEPRECATED/>%) {
3753                 $is_deprecated = 1;
3754             } elsif (m%^</$declaration_type>%) {
3755                 #print "Found end of declaration: $declaration_name\n";
3756                 # Check that the declaration has a name
3757                 if ($declaration_name eq "") {
3758                     print "ERROR: $declaration_type has no name $file:$.\n";
3759                 }
3761                 # If the declaration is an empty typedef struct _XXX XXX
3762                 # set the flag to indicate the struct has a typedef.
3763                 if ($declaration_type eq 'STRUCT'
3764                     && $declaration =~ m/^\s*$/) {
3765                     #print "Struct has typedef: $declaration_name\n";
3766                     $StructHasTypedef{$declaration_name} = 1;
3767                 }
3769                 # Check if the symbol is already defined.
3770                 if (defined ($Declarations{$declaration_name})
3771                     && $override == 0) {
3772                     # Function declarations take precedence.
3773                     if ($DeclarationTypes{$declaration_name} eq 'FUNCTION') {
3774                         # Ignore it.
3775                     } elsif ($declaration_type eq 'FUNCTION') {
3776                         if ($is_deprecated) {
3777                             $Deprecated{$declaration_name} = "";
3778                         }
3779                         $Declarations{$declaration_name} = $declaration;
3780                         $DeclarationTypes{$declaration_name} = $declaration_type;
3781                     } elsif ($DeclarationTypes{$declaration_name}
3782                               eq $declaration_type) {
3783                         # If the existing declaration is empty, or is just a
3784                         # forward declaration of a struct, override it.
3785                         if ($declaration_type eq 'STRUCT') {
3786                             if ($Declarations{$declaration_name} =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
3787                                 if ($is_deprecated) {
3788                                     $Deprecated{$declaration_name} = "";
3789                                 }
3790                                 $Declarations{$declaration_name} = $declaration;
3791                             } elsif ($declaration =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
3792                                 # Ignore an empty or forward declaration.
3793                             } else {
3794                                 &LogWarning ($file, $., "Structure $declaration_name has multiple definitions.");
3795                             }
3796                         } else {
3797                             # set flag in %DeclarationConditional hash for
3798                             # multiply defined macros/typedefs.
3799                             $DeclarationConditional{$declaration_name} = 1;
3800                         }
3801                     } else {
3802                         &LogWarning ($file, $., "$declaration_name has multiple definitions.");
3803                     }
3804                 } else {
3805                     if ($is_deprecated) {
3806                         $Deprecated{$declaration_name} = "";
3807                     }
3808                     $Declarations{$declaration_name} = $declaration;
3809                     $DeclarationTypes{$declaration_name} = $declaration_type;
3810                 }
3812                 $declaration_type = "";
3813                 $is_deprecated = 0;
3814             } else {
3815                 $declaration .= $_;
3816             }
3817         }
3818     }
3819     close (INPUT);
3823 #############################################################################
3824 # Function    : ReadSignalsFile
3825 # Description : This reads in an existing file which contains information on
3826 #               all GTK signals. It creates the arrays @SignalNames and
3827 #               @SignalPrototypes containing info on the signals. The first
3828 #               line of the SignalPrototype is the return type of the signal
3829 #               handler. The remaining lines are the parameters passed to it.
3830 #               The last parameter, "gpointer user_data" is always the same
3831 #               so is not included.
3832 # Arguments   : $file - the file containing the signal handler prototype
3833 #                       information.
3834 #############################################################################
3836 sub ReadSignalsFile {
3837     my ($file) = @_;
3839     my $in_signal = 0;
3840     my $signal_object;
3841     my $signal_name;
3842     my $signal_returns;
3843     my $signal_flags;
3844     my $signal_prototype;
3846     # Reset the signal info.
3847     @SignalObjects = ();
3848     @SignalNames = ();
3849     @SignalReturns = ();
3850     @SignalFlags = ();
3851     @SignalPrototypes = ();
3853     if (! -f $file) {
3854         return;
3855     }
3856     if (!open (INPUT, $file)) {
3857         warn "Can't open $file - skipping signals\n";
3858         return;
3859     }
3860     while (<INPUT>) {
3861         if (!$in_signal) {
3862             if (m/^<SIGNAL>/) {
3863                 $in_signal = 1;
3864                 $signal_object = "";
3865                 $signal_name = "";
3866                 $signal_returns = "";
3867                 $signal_prototype = "";
3868             }
3869         } else {
3870             if (m/^<NAME>(.*)<\/NAME>/) {
3871                 $signal_name = $1;
3872                 if ($signal_name =~ m/^(.*)::(.*)$/) {
3873                     $signal_object = $1;
3874                     ($signal_name = $2) =~ s/_/-/g;
3875                     #print "Found signal: $signal_name\n";
3876                 } else {
3877                     &LogWarning ($file, $., "Invalid signal name: $signal_name.");
3878                 }
3879             } elsif (m/^<RETURNS>(.*)<\/RETURNS>/) {
3880                 $signal_returns = $1;
3881             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
3882                 $signal_flags = $1;
3883             } elsif (m%^</SIGNAL>%) {
3884                 #print "Found end of signal: ${signal_object}::${signal_name}\nReturns: ${signal_returns}\n${signal_prototype}";
3885                 push (@SignalObjects, $signal_object);
3886                 push (@SignalNames, $signal_name);
3887                 push (@SignalReturns, $signal_returns);
3888                 push (@SignalFlags, $signal_flags);
3889                 push (@SignalPrototypes, $signal_prototype);
3890                 $in_signal = 0;
3891             } else {
3892                 $signal_prototype .= $_;
3893             }
3894         }
3895     }
3896     close (INPUT);
3900 #############################################################################
3901 # Function    : ReadTemplateFile
3902 # Description : This reads in the manually-edited documentation file
3903 #               corresponding to the file currently being created, so we can
3904 #               insert the documentation at the appropriate places.
3905 #               It outputs %SymbolTypes, %SymbolDocs and %SymbolParams, which
3906 #               is a hash of arrays.
3907 #               NOTE: This function is duplicated in gtkdoc-mktmpl (but
3908 #               slightly different).
3909 # Arguments   : $docsfile - the template file to read in.
3910 #               $skip_unused_params - 1 if the unused parameters should be
3911 #                       skipped.
3912 #############################################################################
3914 sub ReadTemplateFile {
3915     my ($docsfile, $skip_unused_params) = @_;
3917     my $template = "$docsfile.sgml";
3918     if (! -f $template) {
3919         #print "File doesn't exist: $template\n";
3920         return 0;
3921     }
3922     #print "Reading $template\n";
3924     # start with empty hashes, we merge the source comment for each file
3925     # afterwards
3926     %SymbolDocs = ();
3927     %SymbolTypes = ();
3928     %SymbolParams = ();
3930     my $current_type = "";      # Type of symbol being read.
3931     my $current_symbol = "";    # Name of symbol being read.
3932     my $symbol_doc = "";                # Description of symbol being read.
3933     my @params;                 # Parameter names and descriptions of current
3934                                 #   function/macro/function typedef.
3935     my $current_param = -1;     # Index of parameter currently being read.
3936                                 #   Note that the param array contains pairs
3937                                 #   of param name & description.
3938     my $in_unused_params = 0;   # True if we are reading in the unused params.
3939     my $in_deprecated = 0;
3940     my $in_since = 0;
3941     my $in_stability = 0;
3943     open (DOCS, "$template")
3944         || die "Can't open $template: $!";
3945     while (<DOCS>) {
3946         if (m/^<!-- ##### ([A-Z_]+) (\S+) ##### -->/) {
3947             my $type = $1;
3948             my $symbol = $2;
3949             if ($symbol eq "Title"
3950                 || $symbol eq "Short_Description"
3951                 || $symbol eq "Long_Description"
3952                 || $symbol eq "See_Also"
3953                 || $symbol eq "Stability_Level"
3954                 || $symbol eq "Include") {
3956                 $symbol = $docsfile . ":" . $symbol;
3957             }
3959             #print "Found symbol: $symbol\n";
3960             # Remember file and line for the symbol
3961             $SymbolSourceFile{$symbol} = $template;
3962             $SymbolSourceLine{$symbol} = $.;
3964             # Store previous symbol, but remove any trailing blank lines.
3965             if ($current_symbol ne "") {
3966                 $symbol_doc =~ s/\s+$//;
3967                 $SymbolTypes{$current_symbol} = $current_type;
3968                 $SymbolDocs{$current_symbol} = $symbol_doc;
3970                 # Check that the stability level is valid.
3971                 if ($StabilityLevel{$current_symbol}) {
3972                     $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
3973                 }
3975                 if ($current_param >= 0) {
3976                     $SymbolParams{$current_symbol} = [ @params ];
3977                 } else {
3978                     # Delete any existing params in case we are overriding a
3979                     # previously read template.
3980                     delete $SymbolParams{$current_symbol};
3981                 }
3982             }
3983             $current_type = $type;
3984             $current_symbol = $symbol;
3985             $current_param = -1;
3986             $in_unused_params = 0;
3987             $in_deprecated = 0;
3988             $in_since = 0;
3989             $in_stability = 0;
3990             $symbol_doc = "";
3991             @params = ();
3993         } elsif (m/^<!-- # Unused Parameters # -->/) {
3994             #print "DEBUG: Found unused parameters\n";
3995             $in_unused_params = 1;
3996             next;
3998         } elsif ($in_unused_params && $skip_unused_params) {
3999             # When outputting the DocBook we skip unused parameters.
4000             #print "DEBUG: Skipping unused param: $_";
4001             next;
4003         } else {
4004             # Check if param found. Need to handle "..." and "format...".
4005             if (s/^\@([\w\.]+):\040?//) {
4006                 my $param_name = $1;
4007                 # Allow variations of 'Returns'
4008                 if ($param_name =~ m/^[Rr]eturns?$/) {
4009                     $param_name = "Returns";
4010                 }
4011                 #print "Found param for symbol $current_symbol : '$param_name'= '$_'\n";
4013                 if ($param_name eq "Deprecated") {
4014                     $in_deprecated = 1;
4015                     $Deprecated{$current_symbol} = $_;
4016                 } elsif ($param_name eq "Since") {
4017                     $in_since = 1;
4018                     $Since{$current_symbol} = $_;
4019                 } elsif ($param_name eq "Stability") {
4020                     $in_stability = 1;
4021                     $StabilityLevel{$current_symbol} = $_;
4022                 } else {
4023                     push (@params, $param_name);
4024                     push (@params, $_);
4025                     $current_param += 2;
4026                 }
4027             } else {
4028                 if ($in_deprecated) {
4029                     $Deprecated{$current_symbol} .= $_;
4030                 } elsif ($in_since) {
4031                     $Since{$current_symbol} .= $_;
4032                 } elsif ($in_stability) {
4033                     $StabilityLevel{$current_symbol} .= $_;
4034                 } elsif ($current_param >= 0) {
4035                     $params[$current_param] .= $_;
4036                 } else {
4037                     $symbol_doc .= $_;
4038                 }
4039             }
4040         }
4041     }
4043     # Remember to finish the current symbol doccs.
4044     if ($current_symbol ne "") {
4046         $symbol_doc =~ s/\s+$//;
4047         $SymbolTypes{$current_symbol} = $current_type;
4048         $SymbolDocs{$current_symbol} = $symbol_doc;
4050         # Check that the stability level is valid.
4051         if ($StabilityLevel{$current_symbol}) {
4052             $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4053         }
4055         if ($current_param >= 0) {
4056             $SymbolParams{$current_symbol} = [ @params ];
4057         } else {
4058             # Delete any existing params in case we are overriding a
4059             # previously read template.
4060             delete $SymbolParams{$current_symbol};
4061         }
4062     }
4064     close (DOCS);
4065     return 1;
4069 #############################################################################
4070 # Function    : ReadObjectHierarchy
4071 # Description : This reads in the $MODULE-hierarchy.txt file containing all
4072 #               the GtkObject subclasses described in this module (and their
4073 #               ancestors).
4074 #               It places them in the @Objects array, and places their level
4075 #               in the widget hierarchy in the @ObjectLevels array, at the
4076 #               same index. GtkObject, the root object, has a level of 1.
4078 #               FIXME: the version in gtkdoc-mkdb also generates tree_index.sgml
4079 #               as it goes along, this should be split out into a separate
4080 #               function.
4082 # Arguments   : none
4083 #############################################################################
4085 sub ReadObjectHierarchy {
4086     @Objects = ();
4087     @ObjectLevels = ();
4089     if (! -f $OBJECT_TREE_FILE) {
4090         return;
4091     }
4092     if (!open (INPUT, $OBJECT_TREE_FILE)) {
4093         warn "Can't open $OBJECT_TREE_FILE - skipping object tree\n";
4094         return;
4095     }
4097     my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.sgml";
4098     my $new_tree_index = "$SGML_OUTPUT_DIR/tree_index.new";
4099     my $tree_header = $doctype_header;
4100     $tree_header =~ s/<!DOCTYPE \w+/<!DOCTYPE screen/;
4102     open (OUTPUT, ">$new_tree_index")
4103         || die "Can't create $new_tree_index: $!";
4104     print (OUTPUT "$tree_header<screen>\n");
4106     # Only emit objects if they are supposed to be documented, or if
4107     # they have documented children. To implement this, we maintain a
4108     # stack of pending objects which will be emitted if a documented
4109     # child turns up.
4110     my @pending_objects = ();
4111     my @pending_levels = ();
4112     while (<INPUT>) {
4113         if (m/\S+/) {
4114             my $object = $&;
4115             my $level = (length($`)) / 2 + 1;
4116             my $xref = "";
4118             while (($#pending_levels >= 0) && ($pending_levels[$#pending_levels] >= $level)) {
4119                 my $pobject = pop(@pending_objects);
4120                 my $plevel = pop(@pending_levels);
4121             }
4123             push (@pending_objects, $object);
4124             push (@pending_levels, $level);
4126             if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4127                 while ($#pending_levels >= 0) {
4128                     $object = shift @pending_objects;
4129                     $level = shift @pending_levels;
4130                     $xref = &MakeXRef ($object);
4132                     print (OUTPUT ' ' x ($level * 4), "$xref\n");
4133                     push (@Objects, $object);
4134                     push (@ObjectLevels, $level);
4135                 }
4136             }
4137         }
4138     }
4139     print (OUTPUT "</screen>\n");
4141     close (INPUT);
4142     close (OUTPUT);
4144     &UpdateFileIfChanged ($old_tree_index, $new_tree_index, 0);
4146     &OutputObjectList;
4149 #############################################################################
4150 # Function    : ReadInterfaces
4151 # Description : This reads in the $MODULE.interfaces file.
4153 # Arguments   : none
4154 #############################################################################
4156 sub ReadInterfaces {
4157     %Interfaces = ();
4159     if (! -f $INTERFACES_FILE) {
4160         return;
4161     }
4162     if (!open (INPUT, $INTERFACES_FILE)) {
4163         warn "Can't open $INTERFACES_FILE - skipping interfaces\n";
4164         return;
4165     }
4167     while (<INPUT>) {
4168        chomp;
4169        my ($object, @ifaces) = split;
4170        if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4171            my @knownIfaces = ();
4173            # filter out private interfaces, but leave foreign interfaces
4174            foreach my $iface (@ifaces) {
4175                if (!exists($KnownSymbols{$iface}) || $KnownSymbols{$iface} == 1) {
4176                    push (@knownIfaces, $iface);
4177                }
4178              }
4180            $Interfaces{$object} = join(' ', @knownIfaces);
4181        }
4182     }
4183     close (INPUT);
4186 #############################################################################
4187 # Function    : ReadPrerequisites
4188 # Description : This reads in the $MODULE.prerequisites file.
4190 # Arguments   : none
4191 #############################################################################
4193 sub ReadPrerequisites {
4194     %Prerequisites = ();
4196     if (! -f $PREREQUISITES_FILE) {
4197         return;
4198     }
4199     if (!open (INPUT, $PREREQUISITES_FILE)) {
4200         warn "Can't open $PREREQUISITES_FILE - skipping prerequisites\n";
4201         return;
4202     }
4204     while (<INPUT>) {
4205        chomp;
4206        my ($iface, @prereqs) = split;
4207        if (exists($KnownSymbols{$iface}) && $KnownSymbols{$iface} == 1) {
4208            my @knownPrereqs = ();
4210            # filter out private prerequisites, but leave foreign prerequisites
4211            foreach my $prereq (@prereqs) {
4212                if (!exists($KnownSymbols{$prereq}) || $KnownSymbols{$prereq} == 1) {
4213                   push (@knownPrereqs, $prereq);
4214                }
4215            }
4217            $Prerequisites{$iface} = join(' ', @knownPrereqs);
4218        }
4219     }
4220     close (INPUT);
4223 #############################################################################
4224 # Function    : ReadArgsFile
4225 # Description : This reads in an existing file which contains information on
4226 #               all GTK args. It creates the arrays @ArgObjects, @ArgNames,
4227 #               @ArgTypes, @ArgFlags, @ArgNicks and @ArgBlurbs containing info
4228 #               on the args.
4229 # Arguments   : $file - the file containing the arg information.
4230 #############################################################################
4232 sub ReadArgsFile {
4233     my ($file) = @_;
4235     my $in_arg = 0;
4236     my $arg_object;
4237     my $arg_name;
4238     my $arg_type;
4239     my $arg_flags;
4240     my $arg_nick;
4241     my $arg_blurb;
4242     my $arg_default;
4243     my $arg_range;
4245     # Reset the args info.
4246     @ArgObjects = ();
4247     @ArgNames = ();
4248     @ArgTypes = ();
4249     @ArgFlags = ();
4250     @ArgNicks = ();
4251     @ArgBlurbs = ();
4252     @ArgDefaults = ();
4253     @ArgRanges = ();
4255     if (! -f $file) {
4256         return;
4257     }
4258     if (!open (INPUT, $file)) {
4259         warn "Can't open $file - skipping args\n";
4260         return;
4261     }
4262     while (<INPUT>) {
4263         if (!$in_arg) {
4264             if (m/^<ARG>/) {
4265                 $in_arg = 1;
4266                 $arg_object = "";
4267                 $arg_name = "";
4268                 $arg_type = "";
4269                 $arg_flags = "";
4270                 $arg_nick = "";
4271                 $arg_blurb = "";
4272                 $arg_default = "";
4273                 $arg_range = "";
4274             }
4275         } else {
4276             if (m/^<NAME>(.*)<\/NAME>/) {
4277                 $arg_name = $1;
4278                 if ($arg_name =~ m/^(.*)::(.*)$/) {
4279                     $arg_object = $1;
4280                     ($arg_name = $2) =~ s/_/-/g;
4281                     #print "Found arg: $arg_name\n";
4282                 } else {
4283                     &LogWarning ($file, $., "Invalid argument name: $arg_name");
4284                 }
4285             } elsif (m/^<TYPE>(.*)<\/TYPE>/) {
4286                 $arg_type = $1;
4287             } elsif (m/^<RANGE>(.*)<\/RANGE>/) {
4288                 $arg_range = $1;
4289             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4290                 $arg_flags = $1;
4291             } elsif (m/^<NICK>(.*)<\/NICK>/) {
4292                 $arg_nick = $1;
4293             } elsif (m/^<BLURB>(.*)<\/BLURB>/) {
4294                 $arg_blurb = $1;
4295                 if ($arg_blurb eq "(null)") {
4296                   $arg_blurb = "";
4297                   &LogWarning ($file, $., "Property ${arg_object}:${arg_name} has no documentation.");
4298                 }
4299             } elsif (m/^<DEFAULT>(.*)<\/DEFAULT>/) {
4300                 $arg_default = $1;
4301             } elsif (m%^</ARG>%) {
4302                 #print "Found end of arg: ${arg_object}::${arg_name}\n${arg_type} : ${arg_flags}\n";
4303                 push (@ArgObjects, $arg_object);
4304                 push (@ArgNames, $arg_name);
4305                 push (@ArgTypes, $arg_type);
4306                 push (@ArgRanges, $arg_range);
4307                 push (@ArgFlags, $arg_flags);
4308                 push (@ArgNicks, $arg_nick);
4309                 push (@ArgBlurbs, $arg_blurb);
4310                 push (@ArgDefaults, $arg_default);
4311                 $in_arg = 0;
4312             }
4313         }
4314     }
4315     close (INPUT);
4319 #############################################################################
4320 # Function    : CheckIsObject
4321 # Description : Returns 1 if the given name is a GtkObject or a subclass.
4322 #               It uses the global @Objects array.
4323 #               Note that the @Objects array only contains classes in the
4324 #               current module and their ancestors - not all GTK classes.
4325 # Arguments   : $name - the name to check.
4326 #############################################################################
4328 sub CheckIsObject {
4329     my ($name) = @_;
4331     my $object;
4332     foreach $object (@Objects) {
4333         if ($object eq $name) {
4334             return 1;
4335         }
4336     }
4337     return 0;
4341 #############################################################################
4342 # Function    : MakeReturnField
4343 # Description : Pads a string to $RETURN_TYPE_FIELD_WIDTH.
4344 # Arguments   : $str - the string to pad.
4345 #############################################################################
4347 sub MakeReturnField {
4348     my ($str) = @_;
4350     return $str . (' ' x ($RETURN_TYPE_FIELD_WIDTH - length ($str)));
4353 #############################################################################
4354 # Function    : GetSymbolSourceFile
4355 # Description : Get the filename where the symbol docs where taken from.
4356 # Arguments   : $symbol - the symbol name
4357 #############################################################################
4359 sub GetSymbolSourceFile {
4360     my ($symbol) = @_;
4362     if (defined($SourceSymbolSourceFile{$symbol})) {
4363         return $SourceSymbolSourceFile{$symbol};
4364     } elsif (defined($SymbolSourceFile{$symbol})) {
4365         return $SymbolSourceFile{$symbol};
4366     } else {
4367         return "";
4368     }
4371 #############################################################################
4372 # Function    : GetSymbolSourceLine
4373 # Description : Get the file line where the symbol docs where taken from.
4374 # Arguments   : $symbol - the symbol name
4375 #############################################################################
4377 sub GetSymbolSourceLine {
4378     my ($symbol) = @_;
4380     if (defined($SourceSymbolSourceLine{$symbol})) {
4381         return $SourceSymbolSourceLine{$symbol};
4382     } elsif (defined($SymbolSourceLine{$symbol})) {
4383         return $SymbolSourceLine{$symbol};
4384     } else {
4385         return 0;
4386     }