mkdb: remove some unnneded vars from index enhancement patch
[gtk-doc.git] / gtkdoc-mkdb.in
blobd7292dd1d53b11169b21563043a78d5fac16e865
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  Damon Chaplin
6 #               2007,2008,2009  Stefan Kost
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #############################################################################
24 # Script      : gtkdoc-mkdb
25 # Description : This creates the DocBook files from the edited templates.
26 #############################################################################
28 use strict;
29 use Getopt::Long;
31 push @INC, '@PACKAGE_DATA_DIR@';
32 require "gtkdoc-common.pl";
34 # Options
36 # name of documentation module
37 my $MODULE;
38 my $TMPL_DIR;
39 my $SGML_OUTPUT_DIR;
40 my @SOURCE_DIRS;
41 my $SOURCE_SUFFIXES = "";
42 my $IGNORE_FILES = "";
43 my $PRINT_VERSION;
44 my $PRINT_HELP;
45 my $MAIN_SGML_FILE;
46 my $EXPAND_CONTENT_FILES = "";
47 my $SGML_MODE;
48 my $DEFAULT_STABILITY;
49 my $DEFAULT_INCLUDES;
50 my $OUTPUT_FORMAT;
51 my $NAME_SPACE = "";
52 my $OUTPUT_ALL_SYMBOLS;
53 my $OUTPUT_SYMBOLS_WITHOUT_SINCE;
55 my %optctl = ('module' => \$MODULE,
56               'source-dir' => \@SOURCE_DIRS,
57               'source-suffixes' => \$SOURCE_SUFFIXES,
58               'ignore-files' => \$IGNORE_FILES,
59               'output-dir' => \$SGML_OUTPUT_DIR,
60               'tmpl-dir' => \$TMPL_DIR,
61               'version' => \$PRINT_VERSION,
62               'help' => \$PRINT_HELP,
63               'main-sgml-file' => \$MAIN_SGML_FILE,
64               'expand-content-files' => \$EXPAND_CONTENT_FILES,
65               'sgml-mode' => \$SGML_MODE,
66               'default-stability' => \$DEFAULT_STABILITY,
67               'default-includes' => \$DEFAULT_INCLUDES,
68               'output-format' => \$OUTPUT_FORMAT,
69               'name-space' => \$NAME_SPACE,
70               'outputallsymbols' => \$OUTPUT_ALL_SYMBOLS,
71               'outputsymbolswithoutsince' => \$OUTPUT_SYMBOLS_WITHOUT_SINCE
72               );
73 GetOptions(\%optctl, "module=s", "source-dir:s", "source-suffixes:s", 
74     "ignore-files:s", "output-dir:s", "tmpl-dir:s", "version", "outputallsymbols",
75     "outputsymbolswithoutsince",
76     "expand-content-files:s", "main-sgml-file:s", "extra-db-files:s", "help",
77     "sgml-mode", "default-stability:s", "default-includes:s", "output-format:s",
78     "name-space:s");
80 if ($PRINT_VERSION) {
81     print "@VERSION@\n";
82     exit 0;
85 if (!$MODULE) {
86     $PRINT_HELP = 1;
89 if ($DEFAULT_STABILITY && $DEFAULT_STABILITY ne "Stable"
90     && $DEFAULT_STABILITY ne "Private" && $DEFAULT_STABILITY ne "Unstable") {
91     $PRINT_HELP = 1;
94 if ($PRINT_HELP) {
95     print <<EOF;
96 gtkdoc-mkdb version @VERSION@ - generate docbook files
98 --module=MODULE_NAME       Name of the doc module being parsed
99 --source-dir=DIRNAME       Directories which contain inline reference material
100 --source-suffixes=SUFFIXES Suffixes of source files to scan, comma-separated
101 --ignore-files=FILES       Files or directories which should not be scanned
102                            May be used more than once for multiple directories
103 --output-dir=DIRNAME       Directory to put the generated DocBook files in
104 --tmpl-dir=DIRNAME         Directory in which template files may be found
105 --main-sgml-file=FILE      File containing the toplevel DocBook file.
106 --expand-content-files=FILES Extra DocBook files to expand abbreviations in.
107 --output-format=FORMAT     Format to use for the generated docbook, XML or SGML.
108 --sgml-mode                Allow DocBook markup in inline documentation.
109 --default-stability=LEVEL  Specify default stability Level. Valid values are
110                            Stable, Unstable, or Private.
111 --default-includes=FILENAMES Specify default includes for section Synopsis
112 --name-space=NS            Omit namespace in index.
113 --version                  Print the version of this program
114 --help                     Print this help
116     exit 0;
119 my ($empty_element_end, $doctype_header);
121 if (lc($OUTPUT_FORMAT) eq "xml") {
122     if (!$MAIN_SGML_FILE) {
123         # backwards compatibility
124         if (-e "${MODULE}-docs.sgml") {
125             $MAIN_SGML_FILE="${MODULE}-docs.sgml";
126         } else {
127             $MAIN_SGML_FILE="${MODULE}-docs.xml";
128         }
129     }
130     $OUTPUT_FORMAT = "xml";
131     $empty_element_end = "/>";
133     if (-e $MAIN_SGML_FILE) {
134         open(INPUT, "<$MAIN_SGML_FILE") || die "Can't open $MAIN_SGML_FILE";
135         $doctype_header = "";
136         while (<INPUT>) {
137             if (/^\s*<(book|chapter|article)/) {
138                 if ($_ !~ m/http:\/\/www.w3.org\/200[13]\/XInclude/) {
139                     $doctype_header = "";
140                 }
141                 last;
142             }
143             $doctype_header .= $_;
144         }
145         close(INPUT);
146     } else {
147         $doctype_header =
148 "<?xml version=\"1.0\"?>\n" .
149 "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.3//EN\"\n" .
150 "               \"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd\">\n";
151     }
152     $doctype_header =~ s/<!DOCTYPE \w+/<!DOCTYPE refentry/;
153 } else {
154     if (!$MAIN_SGML_FILE) {
155         $MAIN_SGML_FILE="${MODULE}-docs.sgml";
156     }
157     $OUTPUT_FORMAT = "sgml";
158     $doctype_header = "";
159     $empty_element_end = ">";
162 my $ROOT_DIR = ".";
164 # All the files are written in subdirectories beneath here.
165 $TMPL_DIR = $TMPL_DIR ? $TMPL_DIR : "$ROOT_DIR/tmpl";
167 # This is where we put all the DocBook output.
168 $SGML_OUTPUT_DIR = $SGML_OUTPUT_DIR ? $SGML_OUTPUT_DIR : "$ROOT_DIR/$OUTPUT_FORMAT";
170 # This file contains the object hierarchy.
171 my $OBJECT_TREE_FILE = "$ROOT_DIR/$MODULE.hierarchy";
173 # This file contains the interfaces.
174 my $INTERFACES_FILE = "$ROOT_DIR/$MODULE.interfaces";
176 # This file contains the prerequisites.
177 my $PREREQUISITES_FILE = "$ROOT_DIR/$MODULE.prerequisites";
179 # This file contains signal arguments and names.
180 my $SIGNALS_FILE = "$ROOT_DIR/$MODULE.signals";
182 # The file containing Arg information.
183 my $ARGS_FILE = "$ROOT_DIR/$MODULE.args";
185 # These global arrays store information on signals. Each signal has an entry
186 # in each of these arrays at the same index, like a multi-dimensional array.
187 my @SignalObjects;      # The GtkObject which emits the signal.
188 my @SignalNames;        # The signal name.
189 my @SignalReturns;      # The return type.
190 my @SignalFlags;        # Flags for the signal
191 my @SignalPrototypes;   # The rest of the prototype of the signal handler.
193 # These global arrays store information on Args. Each Arg has an entry
194 # in each of these arrays at the same index, like a multi-dimensional array.
195 my @ArgObjects;         # The GtkObject which has the Arg.
196 my @ArgNames;           # The Arg name.
197 my @ArgTypes;           # The Arg type - gint, GtkArrowType etc.
198 my @ArgFlags;           # How the Arg can be used - readable/writable etc.
199 my @ArgNicks;           # The nickname of the Arg.
200 my @ArgBlurbs;          # Docstring of the Arg.
201 my @ArgDefaults;        # Default value of the Arg.
202 my @ArgRanges;          # The range of the Arg type
203 # These global hashes store declaration info keyed on a symbol name.
204 my %Declarations;
205 my %DeclarationTypes;
206 my %DeclarationConditional;
207 my %DeclarationOutput;
208 my %Deprecated;
209 my %Since;
210 my %StabilityLevel;
211 my %StructHasTypedef;
213 # These global hashes store the existing documentation.
214 my %SymbolDocs;
215 my %SymbolTypes;
216 my %SymbolParams;
217 my %SymbolSourceFile;
218 my %SymbolSourceLine;
220 # These global hashes store documentation scanned from the source files.
221 my %SourceSymbolDocs;
222 my %SourceSymbolParams;
223 my %SourceSymbolSourceFile;
224 my %SourceSymbolSourceLine;
226 # all documentation goes in here, so we can do coverage analysis
227 my %AllSymbols;
228 my %AllIncompleteSymbols;
229 my %AllDocumentedSymbols;
231 # Undeclared yet documented symbols
232 my %UndeclaredSymbols;
234 # These global arrays store GObject, subclasses and the hierarchy.
235 my @Objects;
236 my @ObjectLevels;
238 my %Interfaces;
239 my %Prerequisites;
241 # holds the symbols which are mentioned in $MODULE-sections.txt
242 my %KnownSymbols;
244 # collects index entries
245 my %IndexEntriesFull;
246 my %IndexEntriesSince;
247 my %IndexEntriesDeprecated;
249 # Standard C preprocessor directives, which we ignore for '#' abbreviations.
250 my %PreProcessorDirectives;
251 $PreProcessorDirectives{"assert"} = 1;
252 $PreProcessorDirectives{"define"} = 1;
253 $PreProcessorDirectives{"elif"} = 1;
254 $PreProcessorDirectives{"else"} = 1;
255 $PreProcessorDirectives{"endif"} = 1;
256 $PreProcessorDirectives{"error"} = 1;
257 $PreProcessorDirectives{"if"} = 1;
258 $PreProcessorDirectives{"ifdef"} = 1;
259 $PreProcessorDirectives{"ifndef"} = 1;
260 $PreProcessorDirectives{"include"} = 1;
261 $PreProcessorDirectives{"line"} = 1;
262 $PreProcessorDirectives{"pragma"} = 1;
263 $PreProcessorDirectives{"unassert"} = 1;
264 $PreProcessorDirectives{"undef"} = 1;
265 $PreProcessorDirectives{"warning"} = 1;
267 # remember used annotation (to write minimal glossary)
268 my %AnnotationsUsed;
270 my %AnnotationDefinition = (
271     'allow-none' => "NULL is ok, both for passing and for returning.",
272     'array' => "Parameter points to an array of items.",
273     'default' => "Default parameter value (for in case the <acronym>shadows</acronym>-to function has less parameters).",
274     'element-type' => "Generics and defining elements of containers and arrays.",
275     'error-domains' => "Typed errors. Similar to throws in Java.",
276     'in' => "Parameter for input. Default is <acronym>transfer none</acronym>.",
277     'inout' => "Parameter for input and for returning results. Default is <acronym>transfer full</acronym>.",
278     'in-out' => "Parameter for input and for returning results. Default is <acronym>transfer full</acronym>.",
279     'not-error' => "A GError parameter is not to be handled like a normal GError.",
280     'out' => "Parameter for returning results. Default is <acronym>transfer full</acronym>.",
281     'transfer container' => "Free data container after the code is done.",
282     'transfer full' => "Free data after the code is done.",
283     'transfer none' => "Don't free data after the code is done."
286 # Create the root DocBook output directory if it doens't exist.
287 if (! -e $SGML_OUTPUT_DIR) {
288     mkdir ("$SGML_OUTPUT_DIR", 0777)
289         || die "Can't create directory: $SGML_OUTPUT_DIR";
292 # Function and other declaration output settings.
293 my $RETURN_TYPE_FIELD_WIDTH = 20;
294 my $SYMBOL_FIELD_WIDTH = 36;
295 my $SIGNAL_FIELD_WIDTH = 16;
296 my $PARAM_FIELD_COUNT = 2;
298 &ReadKnownSymbols ("$ROOT_DIR/$MODULE-sections.txt");
299 &ReadSignalsFile ($SIGNALS_FILE);
300 &ReadArgsFile ($ARGS_FILE);
301 &ReadObjectHierarchy;
302 &ReadInterfaces;
303 &ReadPrerequisites;
305 &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-decl.txt", 0);
306 if (-f "$ROOT_DIR/$MODULE-overrides.txt") {
307     &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-overrides.txt", 1);
310 for my $dir (@SOURCE_DIRS) {
311     &ReadSourceDocumentation ($dir);
314 my $changed = &OutputSGML ("$ROOT_DIR/$MODULE-sections.txt");
316 # If any of the DocBook SGML files have changed, update the timestamp file (so
317 # it can be used for Makefile dependencies).
318 if ($changed || ! -e "$ROOT_DIR/sgml.stamp") {
320     # try to detect the common prefix
321     # GtkWidget, GTK_WIDGET, gtk_widget -> gtk
322     if ($NAME_SPACE eq "") {
323         $NAME_SPACE="";
324         my $pos=0;
325         my $ratio=0.0;
326         do {
327             my %prefix;
328             my $letter="";
329             foreach my $symbol (keys(%IndexEntriesFull)) {
330                 if(($NAME_SPACE eq "") || $symbol =~ /^$NAME_SPACE/i) {
331                     if (length($symbol)>$pos) {
332                         $letter=substr($symbol,$pos,1);
333                         # stop prefix scanning
334                         if ($letter eq "_") {
335                             # stop on "_"
336                             last;
337                         }
338                         # Should we also stop on a uppercase char, if last was lowercase
339                         #   GtkWidget, if we have the 'W' and had the 't' before
340                         # or should we count upper and lowercase, and stop one 2nd uppercase, if we already had a lowercase
341                         #   GtkWidget, the 'W' would be the 2nd uppercase and with 't','k' we had lowercase chars before
342                         # need to recound each time as this is per symbol
343                         $prefix{uc($letter)}++;
344                     }
345                 }
346             }
347             if ($letter ne "" && $letter ne "_") {
348                 my $maxletter="";
349                 my $maxsymbols=0;
350                 foreach $letter (keys(%prefix)) {
351                     #print "$letter: $prefix{$letter}.\n";
352                     if ($prefix{$letter}>$maxsymbols) {
353                         $maxletter=$letter;
354                         $maxsymbols=$prefix{$letter};
355                     }
356                 }
357                 $ratio = scalar(keys(%IndexEntriesFull)) / $prefix{$maxletter};
358                 #print "most symbols start with $maxletter, that is ". (100 * $ratio) ." %\n";
359                 if ($ratio > 0.9) {
360                     # do another round
361                     $NAME_SPACE .= $maxletter;
362                 }
363                 $pos++;
364             }
365             else {
366                 $ratio=0.0;
367             }
368         } while ($ratio > 0.9);
369         #print "most symbols start with $NAME_SPACE\n";
370     }
372     &OutputIndexFull;
373     &OutputDeprecatedIndex;
374     &OutputSinceIndexes;
375     &OutputAnnotationGlossary;
377     open (TIMESTAMP, ">$ROOT_DIR/sgml.stamp")
378         || die "Can't create $ROOT_DIR/sgml.stamp: $!";
379     print (TIMESTAMP "timestamp");
380     close (TIMESTAMP);
383 #############################################################################
384 # Function    : OutputObjectList
385 # Description : This outputs the alphabetical list of objects, in a columned
386 #               table.
387 #               FIXME: Currently this also outputs ancestor objects
388 #               which may not actually be in this module.
389 # Arguments   : none
390 #############################################################################
392 sub OutputObjectList {
393     my $cols = 3;
394     
395     # FIXME: use $OUTPUT_FORMAT
396     # my $old_object_index = "$SGML_OUTPUT_DIR/object_index.$OUTPUT_FORMAT";
397     my $old_object_index = "$SGML_OUTPUT_DIR/object_index.sgml";
398     my $new_object_index = "$SGML_OUTPUT_DIR/object_index.new";
400     open (OUTPUT, ">$new_object_index")
401         || die "Can't create $new_object_index: $!";
403     if (lc($OUTPUT_FORMAT) eq "xml") {
404         my $header = $doctype_header;
406         $header =~ s/<!DOCTYPE \w+/<!DOCTYPE informaltable/;
407         print (OUTPUT "$header");
408     }
410     print (OUTPUT <<EOF);
411 <informaltable pgwide="1" frame="none">
412 <tgroup cols="$cols">
413 <colspec colwidth="1*"${empty_element_end}
414 <colspec colwidth="1*"${empty_element_end}
415 <colspec colwidth="1*"${empty_element_end}
416 <tbody>
419     my $count = 0;
420     my $object;
421     foreach $object (sort (@Objects)) {
422         my $xref = &MakeXRef ($object);
423         if ($count % $cols == 0) { print (OUTPUT "<row>\n"); }
424         print (OUTPUT "<entry>$xref</entry>\n");
425         if ($count % $cols == ($cols - 1)) { print (OUTPUT "</row>\n"); }
426         $count++;
427     }
428     if ($count == 0) {
429         # emit an empty row, since empty tables are invalid
430         print (OUTPUT "<row><entry> </entry></row>\n");
431     }
432     else {
433         print (OUTPUT "</row>\n");
434     }
436     print (OUTPUT <<EOF);
437 </tbody></tgroup></informaltable>
439     close (OUTPUT);
441     &UpdateFileIfChanged ($old_object_index, $new_object_index, 0);
445 #############################################################################
446 # Function    : OutputSGML
447 # Description : This collects the output for each section of the docs, and
448 #               outputs each file when the end of the section is found.
449 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
450 #               the functions/macros/structs etc. being documented, organised
451 #               into sections and subsections.
452 #############################################################################
454 sub OutputSGML {
455     my ($file) = @_;
457     #print "Reading: $file\n";
458     open (INPUT, $file)
459         || die "Can't open $file: $!";
460     my $filename = "";
461     my $book_top = "";
462     my $book_bottom = "";
463     my $includes = (defined $DEFAULT_INCLUDES) ? $DEFAULT_INCLUDES : "";
464     my $section_includes = "";
465     my $in_section = 0;
466     my $title = "";
467     my $subsection = "";
468     my $synopsis;
469     my $details;
470     my $num_symbols;
471     my $changed = 0;
472     my $signals_synop = "";
473     my $signals_desc = "";
474     my $args_synop = "";
475     my $child_args_synop = "";
476     my $style_args_synop = "";
477     my $args_desc = "";
478     my $child_args_desc = "";
479     my $style_args_desc = "";
480     my $hierarchy = "";
481     my $interfaces = "";
482     my $implementations = "";
483     my $prerequisites = "";
484     my $derived = "";
485     my @file_objects = ();
486     my %templates = ();
487     my %symbols = ();
489     # merge the source docs, in case there are no templates
490     &MergeSourceDocumentation;
492     while (<INPUT>) {
493         if (m/^#/) {
494             next;
496         } elsif (m/^<SECTION>/) {
497             $synopsis = "";
498             $details = "";
499             $num_symbols = 0;
500             $in_section = 1;
501             @file_objects = ();
502             %symbols = ();
504         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
505             $synopsis .= "\n";
506             $subsection = $1;
508         } elsif (m/^<SUBSECTION>/) {
510         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
511             $title = $1;
512             #print "Section: $title\n";
514             # We don't want warnings if object & class structs aren't used.
515             $DeclarationOutput{$title} = 1;
516             $DeclarationOutput{"${title}Class"} = 1;
517             $DeclarationOutput{"${title}Iface"} = 1;
518             $DeclarationOutput{"${title}Interface"} = 1;
520         } elsif (m/^<FILE>(.*)<\/FILE>/) {
521             $filename = $1;
522             if (! defined $templates{$filename}) {
523                if (&ReadTemplateFile ("$TMPL_DIR/$filename", 1)) {
524                    &MergeSourceDocumentation;
525                    $templates{$filename}=$.;
526                }
527             } else {
528                 &LogWarning ($file, $., "Double <FILE>$filename</FILE> entry. ".
529                     "Previous occurrence on line ".$templates{$filename}.".");
530             }
532         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
533             if ($in_section) {
534                 $section_includes = $1;
535             } else {
536                 if (defined $DEFAULT_INCLUDES) {
537                     &LogWarning ($file, $., "Default <INCLUDE> being overridden by command line option.");
538                 }
539                 else {
540                     $includes = $1;
541                 }
542             }
544         } elsif (m/^<\/SECTION>/) {
545             if($title eq "" && $filename eq "") {
546                 &LogWarning ($file, $., "Section has no title and no file.");
547             }
548             # FIXME: one of those would be enough
549             if ($title eq "") {
550                 $title = $filename;
551             }
552             if ($filename eq "") {
553                 $filename = $title;
554             }
555             #print "End of section: $title\n";
557             $filename =~ s/\s/_/g;
559             my $section_id = $SourceSymbolDocs{"$TMPL_DIR/$filename:Section_Id"};
560             if (defined ($section_id) && $section_id !~ m/^\s*$/) {
561                 # Remove trailing blanks and use as is
562                 $section_id =~ s/\s+$//;
563             } elsif (&CheckIsObject ($title)) {
564                 # GtkObjects use their class name as the ID.
565                 $section_id = &CreateValidSGMLID ($title);
566             } else {
567                 $section_id = &CreateValidSGMLID ("$MODULE-$title");
568             }
570             if ($num_symbols > 0) {
571                 # collect documents
572                 if (lc($OUTPUT_FORMAT) eq "xml") {
573                     $book_bottom .= "    <xi:include href=\"xml/$filename.xml\"/>\n";
574                 } else {
575                     $book_top.="<!ENTITY $section_id SYSTEM \"sgml/$filename.sgml\">\n";
576                     $book_bottom .= "    &$section_id;\n";
577                 }
579                 if (defined ($SourceSymbolDocs{"$TMPL_DIR/$filename:Include"})) {
580                     if ($section_includes) {
581                         &LogWarning ($file, $., "Section <INCLUDE> being overridden by inline comments.");
582                     }
583                     $section_includes = $SourceSymbolDocs{"$TMPL_DIR/$filename:Include"};
584                 }
585                 if ($section_includes eq "") {
586                     $section_includes = $includes;
587                 }
589                  $signals_synop =~ s/^\n*//g;
590                  $signals_synop =~ s/\n+$/\n/g;
591                 if ($signals_synop ne '') {
592                     $signals_synop = <<EOF;
593 <refsect1 id="$section_id.signals" role="signal_proto">
594 <title role="signal_proto.title">Signals</title>
595 <synopsis>
596 ${signals_synop}</synopsis>
597 </refsect1>
599                      $signals_desc =~ s/^\n*//g;
600                      $signals_desc =~ s/\n+$/\n/g;
601                      $signals_desc =~ s/(\s|\n)+$//ms;
602                     $signals_desc  = <<EOF;
603 <refsect1 id="$section_id.signal-details" role="signals">
604 <title role="signals.title">Signal Details</title>
605 $signals_desc
606 </refsect1>
608                 }
610                  $args_synop =~ s/^\n*//g;
611                  $args_synop =~ s/\n+$/\n/g;
612                 if ($args_synop ne '') {
613                     $args_synop = <<EOF;
614 <refsect1 id="$section_id.properties" role="properties">
615 <title role="properties.title">Properties</title>
616 <synopsis>
617 ${args_synop}</synopsis>
618 </refsect1>
620                      $args_desc =~ s/^\n*//g;
621                      $args_desc =~ s/\n+$/\n/g;
622                      $args_desc =~ s/(\s|\n)+$//ms;
623                     $args_desc  = <<EOF;
624 <refsect1 id="$section_id.property-details" role="property_details">
625 <title role="property_details.title">Property Details</title>
626 $args_desc
627 </refsect1>
629                 }
631                  $child_args_synop =~ s/^\n*//g;
632                  $child_args_synop =~ s/\n+$/\n/g;
633                 if ($child_args_synop ne '') {
634                     $args_synop .= <<EOF;
635 <refsect1 id="$section_id.child-properties" role="child_properties">
636 <title role="child_properties.title">Child Properties</title>
637 <synopsis>
638 ${child_args_synop}</synopsis>
639 </refsect1>
641                      $child_args_desc =~ s/^\n*//g;
642                      $child_args_desc =~ s/\n+$/\n/g;
643                      $child_args_desc =~ s/(\s|\n)+$//ms;
644                     $args_desc .= <<EOF;
645 <refsect1 id="$section_id.child-property-details" role="child_property_details">
646 <title role="child_property_details.title">Child Property Details</title>
647 $child_args_desc
648 </refsect1>
650                 }
652                  $style_args_synop =~ s/^\n*//g;
653                  $style_args_synop =~ s/\n+$/\n/g;
654                 if ($style_args_synop ne '') {
655                     $args_synop .= <<EOF;
656 <refsect1 id="$section_id.style-properties" role="style_properties">
657 <title role="style_properties.title">Style Properties</title>
658 <synopsis>
659 ${style_args_synop}</synopsis>
660 </refsect1>
662                      $style_args_desc =~ s/^\n*//g;
663                      $style_args_desc =~ s/\n+$/\n/g;
664                      $style_args_desc =~ s/(\s|\n)+$//ms;
665                     $args_desc .= <<EOF;
666 <refsect1 id="$section_id.style-property-details" role="style_properties_details">
667 <title role="style_properties_details.title">Style Property Details</title>
668 $style_args_desc
669 </refsect1>
671                 }
673                  $hierarchy =~ s/^\n*//g;
674                  $hierarchy =~ s/\n+$/\n/g;
675                  $hierarchy =~ s/(\s|\n)+$//ms;
676                 if ($hierarchy ne "") {
677                     $hierarchy = <<EOF;
678 <refsect1 id="$section_id.object-hierarchy" role="object_hierarchy">
679 <title role="object_hierarchy.title">Object Hierarchy</title>
680 $hierarchy
681 </refsect1>
683                 }
685                  $interfaces =~ s/^\n*//g;
686                  $interfaces =~ s/\n+$/\n/g;
687                  $interfaces =~ s/(\s|\n)+$//ms;
688                 if ($interfaces ne "") {
689                     $interfaces = <<EOF;
690 <refsect1 id="$section_id.implemented-interfaces" role="impl_interfaces">
691 <title role="impl_interfaces.title">Implemented Interfaces</title>
692 $interfaces
693 </refsect1>
695                 }
697                  $implementations =~ s/^\n*//g;
698                  $implementations =~ s/\n+$/\n/g;
699                  $implementations =~ s/(\s|\n)+$//ms;
700                 if ($implementations ne "") {
701                     $implementations = <<EOF;
702 <refsect1 id="$section_id.implementations" role="implementations">
703 <title role="implementations.title">Known Implementations</title>
704 $implementations
705 </refsect1>
707                 }
709                  $prerequisites =~ s/^\n*//g;
710                  $prerequisites =~ s/\n+$/\n/g;
711                  $prerequisites =~ s/(\s|\n)+$//ms;
712                 if ($prerequisites ne "") {
713                     $prerequisites = <<EOF;
714 <refsect1 id="$section_id.prerequisites" role="prerequisites">
715 <title role="prerequisites.title">Prerequisites</title>
716 $prerequisites
717 </refsect1>
719                 }
721                  $derived =~ s/^\n*//g;
722                  $derived =~ s/\n+$/\n/g;
723                  $derived =~ s/(\s|\n)+$//ms;
724                 if ($derived ne "") {
725                     $derived = <<EOF;
726 <refsect1 id="$section_id.derived-interfaces" role="derived_interfaces">
727 <title role="derived_interfaces.title">Known Derived Interfaces</title>
728 $derived
729 </refsect1>
731                 }
733                 $synopsis =~ s/^\n*//g;
734                 $synopsis =~ s/\n+$/\n/g;
735                 my $file_changed = &OutputSGMLFile ($filename, $title, $section_id,
736                                                     $section_includes,
737                                                     \$synopsis, \$details,
738                                                     \$signals_synop, \$signals_desc,
739                                                     \$args_synop, \$args_desc,
740                                                     \$hierarchy, \$interfaces,
741                                                     \$implementations,
742                                                     \$prerequisites, \$derived,
743                                                     \@file_objects);
744                 if ($file_changed) {
745                     $changed = 1;
746                 }
747             }
748             $title = "";
749             $subsection = "";
750             $in_section = 0;
751             $section_includes = "";
752             $signals_synop = "";
753             $signals_desc = "";
754             $args_synop = "";
755             $child_args_synop = "";
756             $style_args_synop = "";
757             $args_desc = "";
758             $child_args_desc = "";
759             $style_args_desc = "";
760             $hierarchy = "";
761             $interfaces = "";
762             $implementations = "";
763             $prerequisites = "";
764             $derived = "";
766         } elsif (m/^(\S+)/) {
767             my $symbol = $1;
768             #print "  Symbol: $symbol\n";
770             # FIXME: check for duplicate entries
771             if (! defined $symbols{$symbol}) {
772                 my $declaration = $Declarations{$symbol};
773                 if (defined ($declaration)) {
774                     # We don't want standard macros/functions of GtkObjects,
775                     # or private declarations.
776                     if ($subsection ne "Standard" && $subsection ne "Private") {
777                         if (&CheckIsObject ($symbol)) {
778                             push @file_objects, $symbol;
779                         }
780                         my ($synop, $desc) = &OutputDeclaration ($symbol,
781                                                                  $declaration);
782                         my ($sig_synop, $sig_desc) = &GetSignals ($symbol);
783                         my ($arg_synop, $child_arg_synop, $style_arg_synop,
784                             $arg_desc, $child_arg_desc, $style_arg_desc) = &GetArgs ($symbol);
785                         my $hier = &GetHierarchy ($symbol);
786                         my $ifaces = &GetInterfaces ($symbol);
787                         my $impls = &GetImplementations ($symbol);
788                         my $prereqs = &GetPrerequisites ($symbol);
789                         my $der = &GetDerived ($symbol);
790                         $synopsis .= $synop;
791                         $details .= $desc;
792                         $signals_synop .= $sig_synop;
793                         $signals_desc .= $sig_desc;
794                         $args_synop .= $arg_synop;
795                         $child_args_synop .= $child_arg_synop;
796                         $style_args_synop .= $style_arg_synop;
797                         $args_desc .= $arg_desc;
798                         $child_args_desc .= $child_arg_desc;
799                         $style_args_desc .= $style_arg_desc;
800                         $hierarchy .= $hier;
801                         $interfaces .= $ifaces;
802                         $implementations .= $impls;
803                         $prerequisites .= $prereqs;
804                         $derived .= $der;
805                     }
806     
807                     # Note that the declaration has been output.
808                     $DeclarationOutput{$symbol} = 1;
809                 } elsif ($subsection ne "Standard" && $subsection ne "Private") {
810                     $UndeclaredSymbols{$symbol} = 1;
811                     &LogWarning ($file, $., "No declaration found for $symbol.");
812                 }
813                 $num_symbols++;
814                 $symbols{$symbol}=$.;
815             }
816             else {
817                 &LogWarning ($file, $., "Double symbol entry for $symbol. ".
818                     "Previous occurrence on line ".$symbols{$symbol}.".");
819             }
820         }
821     }
822     close (INPUT);
824     &OutputMissingDocumentation;
825     &OutputUndeclaredSymbols;
827     if ($OUTPUT_ALL_SYMBOLS) {
828         &OutputAllSymbols;
829     }
830     if ($OUTPUT_SYMBOLS_WITHOUT_SINCE) {
831         &OutputSymbolsWithoutSince;
832     }
834     for $filename (split (' ', $EXPAND_CONTENT_FILES)) {
835         my $file_changed = &OutputExtraFile ($filename);
836         if ($file_changed) {
837             $changed = 1;
838         }
839     }
841     &OutputBook ($book_top, $book_bottom);
843     return $changed;
846 #############################################################################
847 # Function    : OutputIndex
848 # Description : This writes an indexlist that can be included into the main-
849 #               document into an <index> tag.
850 #############################################################################
852 sub OutputIndex {
853     my ($basename, $apiindexref ) = @_;
854     my %apiindex = %{$apiindexref};
855     my $old_index = "$SGML_OUTPUT_DIR/$basename.xml";
856     my $new_index = "$SGML_OUTPUT_DIR/$basename.new";
857     my $lastletter = " ";
858     my $divopen = 0;
859     my $symbol;
860     my $short_symbol;
862     open (OUTPUT, ">$new_index")
863         || die "Can't create $new_index";
865     my $header = $doctype_header;
866     $header =~ s/<!DOCTYPE \w+/<!DOCTYPE indexdiv/;
868     print (OUTPUT "$header<indexdiv>\n");
870     #print "generate $basename index (".%apiindex." entries)\n";
871     
872     # do a case insensitive sort while chopping off the prefix
873     foreach my $hash (
874         sort { $$a{criteria} cmp $$b{criteria} }
875         map { my $x = uc($_); $x =~ s/^$NAME_SPACE\_?(.*)/$1/i; { criteria => $x, original => $_, short => $1 } } 
876         keys %apiindex) {
878         $symbol = $$hash{original};
879         if (defined($$hash{short})) {
880             $short_symbol = $$hash{short};
881         } else {
882             $short_symbol = $symbol;
883         }
885         # generate a short symbol description
886         my $symbol_desc = "";
887         my $symbol_type = lc($DeclarationTypes{$symbol});
888         if ($symbol_type eq "") {
889             if ($symbol =~ m/.*?::(.*)/) {
890                 my $osym = $1;
891                 my $i;
892                 for ($i = 0; $i <= $#ArgNames; $i++) {
893                     if ($ArgNames[$i] eq $osym) {
894                         $symbol_type = "object property";
895                         last;
896                     }
897                 }
898             } elsif ($symbol =~ m/.*?:(.*)/) {
899                 my $osym = $1;
900                 my $i;
901                 for ($i = 0; $i <= $#SignalNames; $i++) {
902                     if ($SignalNames[$i] eq $osym) {
903                         $symbol_type = "object signal";
904                         last;
905                     }
906                 }
907             }
908         }
909         if ($symbol_type ne "") {
910            $symbol_desc=", $symbol_type";
911         }
913         my $curletter = uc(substr($short_symbol,0,1));
914         my $id = $apiindex{$symbol};
916         #print "  add symbol $symbol with $id to index in section $curletter\n";
918         if ($curletter ne $lastletter) {
919             $lastletter = $curletter;
921             if ($divopen == 1) {
922                 print (OUTPUT "</indexdiv>\n");
923             }
924             print (OUTPUT "<indexdiv><title>$curletter</title>\n");
925             $divopen = 1;
926         }
928         print (OUTPUT <<EOF);
929 <indexentry><primaryie linkends="$id"><link linkend="$id">$symbol</link>$symbol_desc</primaryie></indexentry>
931     }
933     if ($divopen == 1) {
934         print (OUTPUT "</indexdiv>\n");
935     }
936     print (OUTPUT "</indexdiv>\n");
937     close (OUTPUT);
939     &UpdateFileIfChanged ($old_index, $new_index, 0);
943 #############################################################################
944 # Function    : OutputIndexFull
945 # Description : This writes the full api indexlist that can be included into the
946 #               main document into an <index> tag.
947 #############################################################################
949 sub OutputIndexFull {
950     &OutputIndex ("api-index-full", \%IndexEntriesFull);
954 #############################################################################
955 # Function    : OutputDeprecatedIndex
956 # Description : This writes the deprecated api indexlist that can be included
957 #               into the main document into an <index> tag.
958 #############################################################################
960 sub OutputDeprecatedIndex {
961     &OutputIndex ("api-index-deprecated", \%IndexEntriesDeprecated);
965 #############################################################################
966 # Function    : OutputSinceIndexes
967 # Description : This writes the 'since' api indexlists that can be included into
968 #               the main document into an <index> tag.
969 #############################################################################
971 sub OutputSinceIndexes {
972     my @sinces = keys %{{ map { $_ => 1 } values %Since }};
974     foreach my $version (@sinces) {
975         #print "Since : [$version]\n";
976         # todo make filtered hash
977         #my %index = grep { $Since{$_} eq $version } %IndexEntriesSince;
978         my %index = map { $_ => $IndexEntriesSince{$_} } grep { $Since{$_} eq $version } keys %IndexEntriesSince;
980         &OutputIndex ("api-index-$version", \%index);
981     }
984 #############################################################################
985 # Function    : OutputAnnotationGlossary
986 # Description : This writes a glossary of the used annotation terms into a
987 #               separate glossary file that can be included into the main
988 #               document.
989 #############################################################################
991 sub OutputAnnotationGlossary {
992     my $old_glossary = "$SGML_OUTPUT_DIR/annotation-glossary.xml";
993     my $new_glossary = "$SGML_OUTPUT_DIR/annotation-glossary.new";
994     my $lastletter = " ";
995     my $divopen = 0;
997     # if there are no annotations used return
998     return if (! keys(%AnnotationsUsed));
1000     # add acronyms that are referenced from acronym text
1001 rerun:
1002     foreach my $annotation (keys(%AnnotationsUsed)) {
1003         if($AnnotationDefinition{$annotation} =~ m/<acronym>([\w ]+)<\/acronym>/) {
1004             if (!exists($AnnotationsUsed{$1})) {
1005                 $AnnotationsUsed{$1} = 1;
1006                 goto rerun;
1007             }
1008         }
1009     }
1011     open (OUTPUT, ">$new_glossary")
1012         || die "Can't create $new_glossary";
1014     my $header = $doctype_header;
1015     $header =~ s/<!DOCTYPE \w+/<!DOCTYPE glossary/;
1017     print (OUTPUT  <<EOF);
1018 $header
1019 <glossary id="annotation-glossary">
1020   <title>Annotation Glossary</title>
1023     foreach my $annotation (keys(%AnnotationsUsed)) {
1024         my $def = $AnnotationDefinition{$annotation};
1025         my $curletter = uc(substr($annotation,0,1));
1027         if ($curletter ne $lastletter) {
1028             $lastletter = $curletter;
1029       
1030             if ($divopen == 1) {
1031                 print (OUTPUT "</glossdiv>\n");
1032             }
1033             print (OUTPUT "<glossdiv><title>$curletter</title>\n");
1034             $divopen = 1;
1035         }
1036         print (OUTPUT <<EOF);
1037     <glossentry>
1038       <glossterm><anchor id="annotation-glossterm-$annotation"/>$annotation</glossterm>
1039       <glossdef>
1040         <para>$def</para>
1041       </glossdef>
1042     </glossentry>
1044     }
1046     if ($divopen == 1) {
1047         print (OUTPUT "</glossdiv>\n");
1048     }
1049     print (OUTPUT "</glossary>\n");
1050     close (OUTPUT);
1052     &UpdateFileIfChanged ($old_glossary, $new_glossary, 0);
1055 #############################################################################
1056 # Function    : ReadKnownSymbols
1057 # Description : This collects the names of non-private symbols from the
1058 #               $MODULE-sections.txt file.
1059 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
1060 #               the functions/macros/structs etc. being documented, organised
1061 #               into sections and subsections.
1062 #############################################################################
1064 sub ReadKnownSymbols {
1065     my ($file) = @_;
1067     my $subsection = "";
1069     #print "Reading: $file\n";
1070     open (INPUT, $file)
1071         || die "Can't open $file: $!";
1073     while (<INPUT>) {
1074         if (m/^#/) {
1075             next;
1077         } elsif (m/^<SECTION>/) {
1078             $subsection = "";
1080         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
1081             $subsection = $1;
1083         } elsif (m/^<SUBSECTION>/) {
1084             next;
1086         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
1087             next;
1089         } elsif (m/^<FILE>(.*)<\/FILE>/) {
1090             $KnownSymbols{"$TMPL_DIR/$1:Long_Description"} = 1;
1091             $KnownSymbols{"$TMPL_DIR/$1:Short_Description"} = 1;
1092             next;
1094         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
1095             next;
1097         } elsif (m/^<\/SECTION>/) {
1098             next;
1100         } elsif (m/^(\S+)/) {
1101             my $symbol = $1;
1103             if ($subsection ne "Standard" && $subsection ne "Private") {
1104                 $KnownSymbols{$symbol} = 1;
1105             }
1106             else {
1107                 $KnownSymbols{$symbol} = 0;
1108             }
1109         }
1110     }
1111     close (INPUT);
1115 #############################################################################
1116 # Function    : OutputDeclaration
1117 # Description : Returns the synopsis and detailed description DocBook
1118 #               describing one function/macro etc.
1119 # Arguments   : $symbol - the name of the function/macro begin described.
1120 #               $declaration - the declaration of the function/macro.
1121 #############################################################################
1123 sub OutputDeclaration {
1124     my ($symbol, $declaration) = @_;
1126     my $type = $DeclarationTypes {$symbol};
1127     if ($type eq 'MACRO') {
1128         return &OutputMacro ($symbol, $declaration);
1129     } elsif ($type eq 'TYPEDEF') {
1130         return &OutputTypedef ($symbol, $declaration);
1131     } elsif ($type eq 'STRUCT') {
1132         return &OutputStruct ($symbol, $declaration);
1133     } elsif ($type eq 'ENUM') {
1134         return &OutputEnum ($symbol, $declaration);
1135     } elsif ($type eq 'UNION') {
1136         return &OutputUnion ($symbol, $declaration);
1137     } elsif ($type eq 'VARIABLE') {
1138         return &OutputVariable ($symbol, $declaration);
1139     } elsif ($type eq 'FUNCTION') {
1140         return &OutputFunction ($symbol, $declaration, $type);
1141     } elsif ($type eq 'USER_FUNCTION') {
1142         return &OutputFunction ($symbol, $declaration, $type);
1143     } else {
1144         die "Unknown symbol type";
1145     }
1149 #############################################################################
1150 # Function    : OutputSymbolTraits
1151 # Description : Returns the Since and StabilityLevel paragraphs for a symbol.
1152 # Arguments   : $symbol - the name of the function/macro begin described.
1153 #############################################################################
1155 sub OutputSymbolTraits {
1156     my ($symbol) = @_;
1157     my $desc = "";
1159     if (exists $Since{$symbol}) {
1160         $desc .= "<para role=\"since\">Since $Since{$symbol}</para>";
1161     }
1162     if (exists $StabilityLevel{$symbol}) {
1163         $desc .= "<para role=\"stability\">Stability Level: $StabilityLevel{$symbol}</para>";
1164     }
1165     return $desc;
1168 #############################################################################
1169 # Function    : Outpu{Symbol,Section}ExtraLinks
1170 # Description : Returns extralinks for the symbol (if enabled).
1171 # Arguments   : $symbol - the name of the function/macro begin described.
1172 #############################################################################
1174 sub uri_escape {
1175     my $text = $_[0];
1176     return undef unless defined $text;
1178     # Build a char to hex map
1179     my %escapes = ();
1180     for (0..255) {
1181             $escapes{chr($_)} = sprintf("%%%02X", $_);
1182     }
1184     # Default unsafe characters.  RFC 2732 ^(uric - reserved)
1185     $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
1187     return $text;
1190 sub OutputSymbolExtraLinks {
1191     my ($symbol) = @_;
1192     my $desc = "";
1194     if (0) { # NEW FEATURE: needs configurability
1195     my $sstr = &uri_escape($symbol);
1196     my $mstr = &uri_escape($MODULE);
1197     $desc .= <<EOF;
1198 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1199 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$sstr">edit documentation</ulink>
1201     }
1202     return $desc;
1205 sub OutputSectionExtraLinks {
1206     my ($symbol,$docsymbol) = @_;
1207     my $desc = "";
1209     if (0) { # NEW FEATURE: needs configurability
1210     my $sstr = &uri_escape($symbol);
1211     my $mstr = &uri_escape($MODULE);
1212     my $dsstr = &uri_escape($docsymbol);
1213     $desc .= <<EOF;
1214 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1215 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$dsstr">edit documentation</ulink>
1217     }
1218     return $desc;
1222 #############################################################################
1223 # Function    : OutputMacro
1224 # Description : Returns the synopsis and detailed description of a macro.
1225 # Arguments   : $symbol - the macro.
1226 #               $declaration - the declaration of the macro.
1227 #############################################################################
1229 sub OutputMacro {
1230     my ($symbol, $declaration) = @_;
1231     my $id = &CreateValidSGMLID ($symbol);
1232     my $condition = &MakeConditionDescription ($symbol);
1233     my $synop = &MakeReturnField("#define") . "<link linkend=\"$id\">$symbol</link>";
1234     my $desc;
1235     my $padding = "";
1236     my $args = "";
1237     my $pad;
1239     if ($declaration =~ m/^\s*#\s*define(\s+)\w+(\([^\)]*\))/) {
1240         $padding = $1;
1241         $args = $2;
1243         if (length ($symbol) < $SYMBOL_FIELD_WIDTH) {
1244             $synop .= (' ' x ($SYMBOL_FIELD_WIDTH - length ($symbol)));
1245         }
1247         # Try to align all the lines of args correctly.
1248         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH + $SYMBOL_FIELD_WIDTH + 1);
1249         my $args_padded = $args;
1250         $args_padded =~ s/ *\\\n */\n$pad/gm;
1251         $synop .= &CreateValidSGML ($args_padded);
1252     }
1253     $synop .= "\n";
1255     my $title = $symbol . ($args ? "()" : "");
1256     $desc = "<refsect2 id=\"$id\" role=\"macro\"$condition>\n<title>$title</title>\n";
1257     $desc .= MakeIndexterms($symbol, $id);
1258     $desc .= "\n";
1259     $desc .= OutputSymbolExtraLinks($symbol);
1261     # Don't output the macro definition if is is a conditional macro or it
1262     # looks like a function, i.e. starts with "g_" or "_?gnome_", or it is
1263     # longer than 2 lines, otherwise we get lots of complicated macros like
1264     # g_assert.
1265     if (!defined ($DeclarationConditional{$symbol}) && ($symbol !~ m/^g_/)
1266         && ($symbol !~ m/^_?gnome_/) && (($declaration =~ tr/\n//) < 2)) {
1267         my $decl_out = &CreateValidSGML ($declaration);
1268         $desc .= "<programlisting>$decl_out</programlisting>\n";
1269     } else {
1270         $desc .= "<programlisting>" . &MakeReturnField("#define") . "$symbol";
1271         # Align each line so that if should all line up OK.
1272         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH - length ("#define "));
1273         $args =~ s/\n/\n$pad/gm;
1274         $desc .= &CreateValidSGML ($args);
1275         $desc .= "</programlisting>\n";
1276     }
1278     $desc .= &MakeDeprecationNote($symbol);
1280     my $parameters = &OutputParamDescriptions ("MACRO", $symbol);
1281     my $parameters_output = 0;
1283     if (defined ($SymbolDocs{$symbol})) {
1284         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1286         # Try to insert the parameter table at the author's desired position.
1287         # Otherwise we need to tag it onto the end.
1288         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1289           $parameters_output = 1;
1290         }
1291         $desc .= $symbol_docs;
1292     }
1294     if ($parameters_output == 0) {
1295         $desc .= $parameters;
1296     }
1298     $desc .= OutputSymbolTraits ($symbol);
1299     $desc .= "</refsect2>\n";
1300     return ($synop, $desc);
1304 #############################################################################
1305 # Function    : OutputTypedef
1306 # Description : Returns the synopsis and detailed description of a typedef.
1307 # Arguments   : $symbol - the typedef.
1308 #               $declaration - the declaration of the typedef,
1309 #                 e.g. 'typedef unsigned int guint;'
1310 #############################################################################
1312 sub OutputTypedef {
1313     my ($symbol, $declaration) = @_;
1314     my $id = &CreateValidSGMLID ($symbol);
1315     my $condition = &MakeConditionDescription ($symbol);
1316     my $synop = &MakeReturnField("typedef") . "<link linkend=\"$id\">$symbol</link>;\n";
1317     my $desc = "<refsect2 id=\"$id\" role=\"typedef\"$condition>\n<title>$symbol</title>\n";
1319     $desc .= MakeIndexterms($symbol, $id);
1320     $desc .= "\n";
1321     $desc .= OutputSymbolExtraLinks($symbol);
1323     if (!defined ($DeclarationConditional{$symbol})) {
1324         my $decl_out = &CreateValidSGML ($declaration);
1325         $desc .= "<programlisting>$decl_out</programlisting>\n";
1326     }
1328     $desc .= &MakeDeprecationNote($symbol);
1330     if (defined ($SymbolDocs{$symbol})) {
1331         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1332     }
1333     $desc .= OutputSymbolTraits ($symbol);
1334     $desc .= "</refsect2>\n";
1335     return ($synop, $desc);
1339 #############################################################################
1340 # Function    : OutputStruct
1341 # Description : Returns the synopsis and detailed description of a struct.
1342 #               We check if it is a widget struct, and if so we only output
1343 #               parts of it that are noted as public fields.
1344 #               We also use a different SGML ID for widget structs, since the
1345 #               original ID is used for the entire RefEntry.
1346 # Arguments   : $symbol - the struct.
1347 #               $declaration - the declaration of the struct.
1348 #############################################################################
1350 sub OutputStruct {
1351     my ($symbol, $declaration) = @_;
1353     my $is_widget_struct = 0;
1354     my $default_to_public = 1;
1355     if (&CheckIsObject ($symbol)) {
1356     #print "Found widget struct: $symbol\n";
1357     $is_widget_struct = 1;
1358         $default_to_public = 0;
1359     }
1361     my $id;
1362     my $condition;
1363     if ($is_widget_struct) {
1364         $id = &CreateValidSGMLID ($symbol . "_struct");
1365         $condition = &MakeConditionDescription ($symbol . "_struct");
1366     } else {
1367         $id = &CreateValidSGMLID ($symbol);
1368         $condition = &MakeConditionDescription ($symbol);
1369     }
1371     # Determine if it is a simple struct or it also has a typedef.
1372     my $has_typedef = 0;
1373     if ($StructHasTypedef{$symbol} || $declaration =~ m/^\s*typedef\s+/) {
1374       $has_typedef = 1;
1375     }
1377     my $synop;
1378     my $desc;
1379     if ($has_typedef) {
1380         # For structs with typedefs we just output the struct name.
1381         $synop = &MakeReturnField("") . "<link linkend=\"$id\">$symbol</link>;\n";
1382         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>$symbol</title>\n";
1383     } else {
1384         $synop = &MakeReturnField("struct") . "<link linkend=\"$id\">$symbol</link>;\n";
1385         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>struct $symbol</title>\n";
1386     }
1388     $desc .= MakeIndexterms($symbol, $id);
1389     $desc .= "\n";
1390     $desc .= OutputSymbolExtraLinks($symbol);
1392     # Form a pretty-printed, private-data-removed form of the declaration
1394     my $decl_out = "";
1395     if ($declaration =~ m/^\s*$/) {
1396         #print "Found opaque struct: $symbol\n";
1397         $decl_out = "typedef struct _$symbol $symbol;";
1398     } elsif ($declaration =~ m/^\s*struct\s+\w+\s*;\s*$/) {
1399         #print "Found opaque struct: $symbol\n";
1400         $decl_out = "struct $symbol;";
1401     } else {
1402         my $public = $default_to_public;
1403         my $new_declaration = "";
1404         my $decl_line;
1405         my $decl = $declaration;
1407         if ($decl =~ m/^\s*(typedef\s+)?struct\s*\w*\s*(?:\/\*.*\*\/)?\s*{(.*)}\s*\w*\s*;\s*$/s) {
1408             my $struct_contents = $2;
1410             foreach $decl_line (split (/\n/, $struct_contents)) {
1411                 #print "Struct line: $decl_line\n";
1412                 if ($decl_line =~ m%/\*\s*<\s*public\s*>\s*\*/%) {
1413                     $public = 1;
1414                 } elsif ($decl_line =~ m%/\*\s*<\s*(private|protected)\s*>\s*\*/%) {
1415                     $public = 0;
1416                 } elsif ($public) {
1417                     $new_declaration .= $decl_line . "\n";
1418                 }
1419             }
1421             if ($new_declaration) {
1422                 # Strip any blank lines off the ends.
1423                 $new_declaration =~ s/^\s*\n//;
1424                 $new_declaration =~ s/\n\s*$/\n/;
1426                 if ($has_typedef) {
1427                     $decl_out = "typedef struct {\n" . $new_declaration
1428                       . "} $symbol;\n";
1429                 } else {
1430                     $decl_out = "struct $symbol {\n" . $new_declaration
1431                       . "};\n";
1432                 }
1433             }
1434         } else {
1435             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1436                 "Couldn't parse struct:\n$declaration");
1437         }
1439         # If we couldn't parse the struct or it was all private, output an
1440         # empty struct declaration.
1441         if ($decl_out eq "") {
1442             if ($has_typedef) {
1443                 $decl_out = "typedef struct _$symbol $symbol;";
1444             } else {
1445                 $decl_out = "struct $symbol;";
1446             }
1447         }
1448     }
1450     $decl_out = &CreateValidSGML ($decl_out);
1451     $desc .= "<programlisting>$decl_out</programlisting>\n";
1453     $desc .= &MakeDeprecationNote($symbol);
1455     if (defined ($SymbolDocs{$symbol})) {
1456         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1457     }
1459     # Create a table of fields and descriptions
1461     # FIXME: Inserting &#160's into the produced type declarations here would
1462     #        improve the output in most situations ... except for function
1463     #        members of structs!
1464     my @fields = ParseStructDeclaration($declaration, !$default_to_public,
1465                                         0, \&MakeXRef,
1466                                         sub {
1467                                             "<structfield id=\"".&CreateValidSGMLID("$id.$_[0]")."\">$_[0]</structfield>";
1468                                         });
1469     my $params = $SymbolParams{$symbol};
1471     # If no parameters are filled in, we don't generate the description
1472     # table, for backwards compatibility
1474     my $found = 0;
1475     if (defined $params) {
1476         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1477             if ($params->[$i] =~ /\S/) {
1478                 $found = 1;
1479                 last;
1480             }
1481         }
1482     }
1484     if ($found) {
1485         my %field_descrs = @$params;
1487             $desc .= <<EOF;
1488 <variablelist role="struct">
1490         while (@fields) {
1491             my $field_name = shift @fields;
1492             my $text = shift @fields;
1493             my $field_descr = $field_descrs{$field_name};
1494             my $param_annotations = "";
1496             $desc .= "<varlistentry><term>$text</term>\n";
1497             if (defined $field_descr) {
1498                 ($field_descr,$param_annotations) = &ExpandAnnotation($symbol, $field_descr);
1499                 $field_descr = &ExpandAbbreviations($symbol, $field_descr);
1500                 $desc .= "<listitem><simpara>$field_descr$param_annotations</simpara></listitem>\n";
1501             } else {
1502                 $desc .= "<listitem><simpara /></listitem>\n";
1503             }
1504             $desc .= "</varlistentry>\n";
1505         }
1507         $desc .= "</variablelist>";
1508     }
1509     $desc .= OutputSymbolTraits ($symbol);
1510     $desc .= "</refsect2>\n";
1511     return ($synop, $desc);
1515 #############################################################################
1516 # Function    : OutputEnum
1517 # Description : Returns the synopsis and detailed description of a enum.
1518 # Arguments   : $symbol - the enum.
1519 #               $declaration - the declaration of the enum.
1520 #############################################################################
1522 sub OutputEnum {
1523     my ($symbol, $declaration) = @_;
1524     my $id = &CreateValidSGMLID ($symbol);
1525     my $condition = &MakeConditionDescription ($symbol);
1526     my $synop = &MakeReturnField("enum") . "<link linkend=\"$id\">$symbol</link>;\n";
1527     my $desc = "<refsect2 id=\"$id\" role=\"enum\"$condition>\n<title>enum $symbol</title>\n";
1529     $desc .= MakeIndexterms($symbol, $id);
1530     $desc .= "\n";
1531     $desc .= OutputSymbolExtraLinks($symbol);
1533     my $decl_out = &CreateValidSGML ($declaration);
1534     $desc .= "<programlisting>$decl_out</programlisting>\n";
1536     $desc .= &MakeDeprecationNote($symbol);
1538     if (defined ($SymbolDocs{$symbol})) {
1539         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1540     }
1542     # Create a table of fields and descriptions
1544     my @members = ParseEnumDeclaration($declaration);
1545     my $params = $SymbolParams{$symbol};
1547     # If no parameters are filled in, we don't generate the description
1548     # table, for backwards compatibility
1550     my $found = 0;
1551     if (defined $params) {
1552         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1553             if ($params->[$i] =~ /\S/) {
1554                 $found = 1;
1555                 last;
1556             }
1557         }
1558     }
1560     if ($found) {
1561         my %member_descrs = @$params;
1563             $desc .= <<EOF;
1564 <variablelist role="enum">
1566         for my $member_name (@members) {
1567             my $member_descr = $member_descrs{$member_name};
1568             my $param_annotations = "";
1570             $id = &CreateValidSGMLID ($member_name);
1571             $condition = &MakeConditionDescription ($member_name);
1572             $desc .= "<varlistentry id=\"$id\" role=\"constant\"$condition>\n<term><literal>$member_name</literal></term>\n";
1573             if (defined $member_descr) {
1574                 ($member_descr,$param_annotations) = &ExpandAnnotation($symbol, $member_descr);
1575                 $member_descr = &ExpandAbbreviations($symbol, $member_descr);
1576                 $desc .= "<listitem><simpara>$member_descr$param_annotations</simpara></listitem>\n";
1577             } else {
1578                 $desc .= "<listitem></listitem>\n";
1579             }
1580             $desc .= "</varlistentry>\n";
1581         }
1583         $desc .= "</variablelist>";
1584     }
1586     $desc .= OutputSymbolTraits ($symbol);
1587     $desc .= "</refsect2>\n";
1588     return ($synop, $desc);
1592 #############################################################################
1593 # Function    : OutputUnion
1594 # Description : Returns the synopsis and detailed description of a union.
1595 # Arguments   : $symbol - the union.
1596 #               $declaration - the declaration of the union.
1597 #############################################################################
1599 sub OutputUnion {
1600     my ($symbol, $declaration) = @_;
1601     my $id = &CreateValidSGMLID ($symbol);
1602     my $condition = &MakeConditionDescription ($symbol);
1603     my $synop = &MakeReturnField("union") . "<link linkend=\"$id\">$symbol</link>;\n";
1604     my $desc = "<refsect2 id=\"$id\" role=\"union\"$condition>\n<title>union $symbol</title>\n";
1606     $desc .= MakeIndexterms($symbol, $id);
1607     $desc .= "\n";
1608     $desc .= OutputSymbolExtraLinks($symbol);
1610     my $decl_out = &CreateValidSGML ($declaration);
1611     $desc .= "<programlisting>$decl_out</programlisting>\n";
1613     $desc .= &MakeDeprecationNote($symbol);
1615     if (defined ($SymbolDocs{$symbol})) {
1616         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1617     }
1618     $desc .= OutputSymbolTraits ($symbol);
1619     $desc .= "</refsect2>\n";
1620     return ($synop, $desc);
1624 #############################################################################
1625 # Function    : OutputVariable
1626 # Description : Returns the synopsis and detailed description of a variable.
1627 # Arguments   : $symbol - the extern'ed variable.
1628 #               $declaration - the declaration of the variable.
1629 #############################################################################
1631 sub OutputVariable {
1632     my ($symbol, $declaration) = @_;
1633     my $id = &CreateValidSGMLID ($symbol);
1634     my $condition = &MakeConditionDescription ($symbol);
1636     my $synop;
1637     if ($declaration =~ m/^\s*extern\s+((const\s+|unsigned\s+)*\w+)(\s+\*+|\*+|\s)(\s*)([A-Za-z]\w*)\s*;/) {
1638         my $mod = defined ($1) ? $1 : "";
1639         my $ptr = defined ($3) ? $3 : "";
1640         my $space = defined ($4) ? $4 : "";
1641         $synop = &MakeReturnField("extern") . "$mod$ptr$space<link linkend=\"$id\">$symbol</link>;\n";
1643     } else {
1644         $synop = &MakeReturnField("extern") . "<link linkend=\"$id\">$symbol</link>;\n";
1645     }
1647     my $desc = "<refsect2 id=\"$id\" role=\"variable\"$condition>\n<title>$symbol</title>\n";
1649     $desc .= MakeIndexterms($symbol, $id);
1650     $desc .= "\n";
1651     $desc .= OutputSymbolExtraLinks($symbol);
1653     my $decl_out = &CreateValidSGML ($declaration);
1654     $desc .= "<programlisting>$decl_out</programlisting>\n";
1656     $desc .= &MakeDeprecationNote($symbol);
1658     if (defined ($SymbolDocs{$symbol})) {
1659         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1660     }
1661     $desc .= OutputSymbolTraits ($symbol);
1662     $desc .= "</refsect2>\n";
1663     return ($synop, $desc);
1667 #############################################################################
1668 # Function    : OutputFunction
1669 # Description : Returns the synopsis and detailed description of a function.
1670 # Arguments   : $symbol - the function.
1671 #               $declaration - the declaration of the function.
1672 #############################################################################
1674 sub OutputFunction {
1675     my ($symbol, $declaration, $symbol_type) = @_;
1676     my $id = &CreateValidSGMLID ($symbol);
1677     my $condition = &MakeConditionDescription ($symbol);
1679     # Take out the return type     $1                                                                           $3   $4
1680     $declaration =~ s/<RETURNS>\s*((const\s+|G_CONST_RETURN\s+|unsigned\s+|long\s+|short\s+|struct\s+|enum\s+)*)(\w+)(\s*\**\s*(const|G_CONST_RETURN)?\s*\**\s*(restrict)?\s*)<\/RETURNS>\n//;
1681     my $type_modifier = defined($1) ? $1 : "";
1682     my $type = $3;
1683     my $pointer = $4;
1684     #print "$symbol pointer is $pointer\n";
1685     my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1686     my $start = "";
1687     #if ($symbol_type eq 'USER_FUNCTION') {
1688     #    $start = "typedef ";
1689     #}
1691     # We output const rather than G_CONST_RETURN.
1692     $type_modifier =~ s/G_CONST_RETURN/const/g;
1693     $pointer =~ s/G_CONST_RETURN/const/g;
1694     $pointer =~ s/^\s+/ /g;
1696     my $ret_type_len = length ($start) + length ($type_modifier)
1697         + length ($pointer) + length ($type);
1698     my $ret_type_output;
1699     my $symbol_len;
1700     if ($ret_type_len < $RETURN_TYPE_FIELD_WIDTH) {
1701         $ret_type_output = "$start$type_modifier$xref$pointer"
1702             . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
1703         $symbol_len = 0;
1704     } else {
1705 #       $ret_type_output = "$start$type_modifier$xref$pointer\n"
1706 #           . (' ' x $RETURN_TYPE_FIELD_WIDTH);
1708         $ret_type_output = "$start$type_modifier$xref$pointer ";
1709         $symbol_len = $ret_type_len + 1 - $RETURN_TYPE_FIELD_WIDTH;
1710     }
1712     $symbol_len += length ($symbol);
1713     my $char1 = my $char2 = my $char3 = "";
1714     if ($symbol_type eq 'USER_FUNCTION') {
1715         $symbol_len += 3;
1716         $char1 = "(";
1717         $char2 = "*";
1718         $char3 = ")";
1719     }
1721     my ($symbol_output, $symbol_desc_output);
1722     if ($symbol_len < $SYMBOL_FIELD_WIDTH) {
1723         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3"
1724             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1725         $symbol_desc_output = "$char1$char2$symbol$char3"
1726             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1727     } else {
1728         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3\n"
1729             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1730         $symbol_desc_output = "$char1$char2$symbol$char3\n"
1731             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1732     }
1734     my $synop = $ret_type_output . $symbol_output . '(';
1735     my $desc = "<refsect2 id=\"$id\" role=\"function\"$condition>\n<title>${symbol} ()</title>\n";
1737     $desc .= MakeIndexterms($symbol, $id);
1738     $desc .= "\n";
1739     $desc .= OutputSymbolExtraLinks($symbol);
1741     $desc  .= "<programlisting>${ret_type_output}$symbol_desc_output(";
1743     my $param_num = 0;
1744     while ($declaration ne "") {
1745         #print "$declaration";
1747         if ($declaration =~ s/^[\s,]+//) {
1748             # skip whitespace and commas
1749             next;
1751         } elsif ($declaration =~ s/^void\s*[,\n]//) {
1752             $synop .= "void";
1753             $desc  .= "void";
1755         } elsif ($declaration =~ s/^...\s*[,\n]//) {
1756             if ($param_num == 0) {
1757                 $synop .= "...";
1758                 $desc  .= "...";
1759             } else {
1760                 $synop .= ",\n"
1761                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1762                     . " ...";
1763                 $desc  .= ",\n"
1764                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1765                     . " ...";
1766             }
1768         # allow alphanumerics, '_', '[' & ']' in param names
1769         # Try to match a standard parameter (keep in sync with gtkdoc-mktmpl)
1770         #                                $1                                                                                                                                    $2                             $3                                                           $4       $5
1771         } 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]//) {
1772             my $pre     = defined($1) ? $1 : "";
1773             my $type    = $2;
1774             my $ptr     = defined($3) ? $3 : "";
1775             my $name    = defined($4) ? $4 : "";
1776             my $array   = defined($5) ? $5 : "";
1778             $pre  =~ s/\s+/ /g;
1779             $type =~ s/\s+/ /g;
1780             $ptr  =~ s/\s+/ /g;
1781             $ptr  =~ s/\s+$//;
1782             if ($ptr && $ptr !~ m/\*$/) { $ptr .= " "; }
1784             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1786             if (($name eq "") && $pre =~ m/^((un)?signed .*)\s?/ ) {
1787                 $name = $type;
1788                 $type = "$1";
1789                 $pre = "";
1790             }
1792             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1793             my $label   = "$pre$xref $ptr$name$array";
1795             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1797             if ($param_num == 0) {
1798                 $synop .= "$label";
1799                 $desc  .= "$label";
1800             } else {
1801                 $synop .= ",\n"
1802                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1803                     . " $label";
1804                 $desc  .= ",\n"
1805                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1806                     . " $label";
1807             }
1809         # Try to match parameters which are functions (keep in sync with gtkdoc-mktmpl)
1810         #                              $1                                       $2          $3      $4                        $5                    $7             $8
1811         } 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]//) {
1812             my $mod1 = defined($1) ? $1 : "";
1813             if (defined($2)) { $mod1 .= $2; }
1814             my $type = $3;
1815             my $ptr1 = $4;
1816             my $mod2 = defined($5) ? $5 : "";
1817             my $func_ptr = $6;
1818             my $name = $7;
1819             my $func_params = defined($8) ? $8 : "";
1820             
1821             #if (!defined($type)) { print "## no type\n"; };
1822             #if (!defined($ptr1)) { print "## no ptr1\n"; };
1823             #if (!defined($func_ptr)) { print "## no func_ptr\n"; };
1824             #if (!defined($name)) { print "## no name\n"; };
1826             if ($ptr1 && $ptr1 !~ m/\*$/) { $ptr1 .= " "; }
1827             $func_ptr  =~ s/\s+//g;
1828             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1829             my $label = "$mod1$xref$ptr1$mod2 ($func_ptr$name) ($func_params)";
1831             #print "Type: [$mod1][$xref][$ptr1][$mod2] ([$func_ptr][$name]) ($func_params)\n";
1832             if ($param_num == 0) {
1833                 $synop .= "$label";
1834                 $desc  .= "$label";
1835             } else {
1836                 $synop .= ",\n"
1837                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1838                     . " $label";
1839                 $desc  .= ",\n"
1840                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1841                     . " $label";
1842             }
1844         } else {
1845             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1846                 "Can't parse args for function $symbol: $declaration");
1847             last;
1848         }
1849         $param_num++;
1850     }
1851     $synop .= ");\n";
1852     $desc  .= ");</programlisting>\n";
1854     $desc .= &MakeDeprecationNote($symbol);
1856     my $parameters = &OutputParamDescriptions ("FUNCTION", $symbol);
1857     my $parameters_output = 0;
1859     if (defined ($SymbolDocs{$symbol})) {
1860         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1862         # Try to insert the parameter table at the author's desired position.
1863         # Otherwise we need to tag it onto the end.
1864         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1865           $parameters_output = 1;
1866         }
1867         $desc .= $symbol_docs;
1868     }
1870     if ($parameters_output == 0) {
1871         $desc .= $parameters;
1872     }
1874     $desc .= OutputSymbolTraits ($symbol);
1875     $desc .= "</refsect2>\n";
1876     return ($synop, $desc);
1880 #############################################################################
1881 # Function    : OutputParamDescriptions
1882 # Description : Returns the DocBook output describing the parameters of a
1883 #               function, macro or signal handler.
1884 # Arguments   : $symbol_type - 'FUNCTION', 'MACRO' or 'SIGNAL'. Signal
1885 #                 handlers have an implicit user_data parameter last.
1886 #               $symbol - the name of the function/macro being described.
1887 #############################################################################
1889 sub OutputParamDescriptions {
1890     my ($symbol_type, $symbol) = @_;
1891     my $output = "";
1892     if (defined ($SymbolParams{$symbol})) {
1893         my $returns = "";
1894         my $params = $SymbolParams{$symbol};
1895         my $params_desc = "";
1896         my $j;
1897         for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
1898             my $param_name = $$params[$j];
1899             my $param_desc = $$params[$j + 1];
1900             my $param_annotations = "";
1902             ($param_desc,$param_annotations) = & ExpandAnnotation($symbol, $param_desc);
1903             $param_desc = &ExpandAbbreviations($symbol, $param_desc);
1904             if ($param_name eq "Returns") {
1905                 $returns = "$param_desc$param_annotations";
1906             } else {
1907                 if ($param_name eq "Varargs") {
1908                     $param_name = "...";
1909                 }
1910                 $params_desc .= "<varlistentry><term><parameter>$param_name</parameter>&#160;:</term>\n<listitem><simpara>$param_desc$param_annotations</simpara></listitem></varlistentry>\n";
1911             }
1912         }
1914         # Signals have an implicit user_data parameter which we describe.
1915         if ($symbol_type eq "SIGNAL") {
1916             $params_desc .= "<varlistentry><term><parameter>user_data</parameter>&#160;:</term>\n<listitem><simpara>user data set when the signal handler was connected.</simpara></listitem></varlistentry>\n";
1917         }
1919         # Start a table if we need one.
1920         if ($params_desc || $returns) {
1921             $output .= "<variablelist role=\"params\">\n";
1922             if ($params_desc ne "") {
1923                 #$output .= "<varlistentry><term>Parameters:</term><listitem></listitem></varlistentry>\n";
1924                 $output .= $params_desc;
1925             }
1927             # Output the returns info last.
1928             if ($returns) {
1929                 $output .= "<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>$returns</simpara></listitem></varlistentry>\n";
1930             }
1932             # Finish the table.
1933             $output .= "</variablelist>";
1934         }
1935     }
1936     return $output;
1940 #############################################################################
1941 # Function    : ParseStabilityLevel
1942 # Description : Parses a stability level and outputs a warning if it isn't
1943 #               valid.
1944 # Arguments   : $stability - the stability text.
1945 #               $file, $line - context for error message
1946 #               $message - description of where the level is from, to use in
1947 #               any error message.
1948 # Returns     : The parsed stability level string.
1949 #############################################################################
1951 sub ParseStabilityLevel {
1952     my ($stability, $file, $line, $message) = @_;
1954     $stability =~ s/^\s*//;
1955     $stability =~ s/\s*$//;
1956     if ($stability =~ m/^stable$/i) {
1957         $stability = "Stable";
1958     } elsif ($stability =~ m/^unstable$/i) {
1959         $stability = "Unstable";
1960     } elsif ($stability =~ m/^private$/i) {
1961         $stability = "Private";
1962     } else {
1963         &LogWarning ($file, $line, "$message is $stability.".
1964             "It should be one of these: Stable, Unstable, or Private.");
1965     }
1966     return $stability;
1970 #############################################################################
1971 # Function    : OutputSGMLFile
1972 # Description : Outputs the final DocBook file for one section.
1973 # Arguments   : $file - the name of the file.
1974 #               $title - the title from the $MODULE-sections.txt file, which
1975 #                 will be overridden by the title in the template file.
1976 #               $section_id - the SGML id to use for the toplevel tag.
1977 #               $includes - comma-separates list of include files added at top
1978 #                 of synopsis, with '<' '>' around them (if not already enclosed in "").
1979 #               $synopsis - reference to the DocBook for the Synopsis part.
1980 #               $details - reference to the DocBook for the Details part.
1981 #               $signal_synop - reference to the DocBook for the Signal Synopsis part
1982 #               $signal_desc - reference to the DocBook for the Signal Description part
1983 #               $args_synop - reference to the DocBook for the Arg Synopsis part
1984 #               $args_desc - reference to the DocBook for the Arg Description part
1985 #               $hierarchy - reference to the DocBook for the Object Hierarchy part
1986 #               $interfaces - reference to the DocBook for the Interfaces part
1987 #               $implementations - reference to the DocBook for the Known Implementations part
1988 #               $prerequisites - reference to the DocBook for the Prerequisites part
1989 #               $derived - reference to the DocBook for the Derived Interfaces part
1990 #               $file_objects - reference to an array of objects in this file
1991 #############################################################################
1993 sub OutputSGMLFile {
1994     my ($file, $title, $section_id, $includes, $synopsis, $details, $signals_synop, $signals_desc, $args_synop, $args_desc, $hierarchy, $interfaces, $implementations, $prerequisites, $derived, $file_objects) = @_;
1996     #print "Output sgml for file $file with title '$title'\n";
1997     
1998     # The edited title overrides the one from the sections file.
1999     my $new_title = $SymbolDocs{"$TMPL_DIR/$file:Title"};
2000     if (defined ($new_title) && $new_title !~ m/^\s*$/) {
2001         $title = $new_title;
2002         #print "Found title: $title\n";
2003     }
2004     my $short_desc = $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
2005     if (!defined ($short_desc) || $short_desc =~ m/^\s*$/) {
2006         $short_desc = "";
2007     } else {
2008         $short_desc = &ExpandAbbreviations("$title:Short_description",
2009                                            $short_desc);
2010         #print "Found short_desc: $short_desc";
2011     }
2012     my $long_desc = $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
2013     if (!defined ($long_desc) || $long_desc =~ m/^\s*$/) {
2014         $long_desc = "";
2015     } else {
2016         $long_desc = &ExpandAbbreviations("$title:Long_description",
2017                                           $long_desc);
2018         #print "Found long_desc: $long_desc";
2019     }
2020     my $see_also = $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
2021     if (!defined ($see_also) || $see_also =~ m%^\s*(<para>)?\s*(</para>)?\s*$%) {
2022         $see_also = "";
2023     } else {
2024         $see_also = &ExpandAbbreviations("$title:See_Also", $see_also);
2025         #print "Found see_also: $see_also";
2026     }
2027     if ($see_also) {
2028         $see_also = "<refsect1 id=\"$section_id.see-also\">\n<title>See Also</title>\n$see_also\n</refsect1>\n";
2029     }
2030     my $stability = $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
2031     if (!defined ($stability) || $stability =~ m/^\s*$/) {
2032         $stability = "";
2033     } else {
2034         $stability = &ParseStabilityLevel($stability, $file, $., "Section stability level");
2035         #print "Found stability: $stability";
2036     }
2037     if ($stability) {
2038         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$stability, unless otherwise indicated\n</refsect1>\n";
2039     } elsif ($DEFAULT_STABILITY) {
2040         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$DEFAULT_STABILITY, unless otherwise indicated\n</refsect1>\n";
2041     }
2043     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
2044         gmtime (time);
2045     my $month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
2046     $year += 1900;
2048     my $include_output = "";
2049     my $include;
2050     foreach $include (split (/,/, $includes)) {
2051         if ($include =~ m/^\".+\"$/) {
2052             $include_output .= "#include ${include}\n";
2053         }
2054         else {
2055             $include =~ s/^\s+|\s+$//gs;
2056             $include_output .= "#include &lt;${include}&gt;\n";
2057         }
2058     }
2059     if ($include_output ne '') {
2060         $include_output = "\n$include_output\n";
2061     }
2062     
2063     my $extralinks = OutputSectionExtraLinks($title,"Section:$file");
2065     my $old_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT";
2066     my $new_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT.new";
2068     open (OUTPUT, ">$new_sgml_file")
2069         || die "Can't create $new_sgml_file: $!";
2071     my $object_anchors = "";
2072     foreach my $object (@$file_objects) {
2073         next if ($object eq $section_id);
2074         my $id = CreateValidSGMLID($object);
2075         #print "Debug: Adding anchor for $object\n";
2076         $object_anchors .= "<anchor id=\"$id\"$empty_element_end";
2077     }
2079     # We used to output this, but is messes up our UpdateFileIfChanged code
2080     # since it changes every day (and it is only used in the man pages):
2081     # "<refentry id="$section_id" revision="$mday $month $year">"
2083     if (lc($OUTPUT_FORMAT) eq "xml") {
2084         print OUTPUT $doctype_header;
2085     }
2087     print OUTPUT <<EOF;
2088 <refentry id="$section_id">
2089 <refmeta>
2090 <refentrytitle role="top_of_page" id="$section_id.top_of_page">$title</refentrytitle>
2091 <manvolnum>3</manvolnum>
2092 <refmiscinfo>\U$MODULE\E Library</refmiscinfo>
2093 </refmeta>
2094 <refnamediv>
2095 <refname>$title</refname>
2096 <refpurpose>$short_desc</refpurpose>
2097 </refnamediv>
2098 $stability
2099 <refsynopsisdiv id="$section_id.synopsis" role="synopsis">
2100 <title role="synopsis.title">Synopsis</title>
2101 $object_anchors
2102 <synopsis>$include_output$${synopsis}</synopsis>
2103 </refsynopsisdiv>
2104 $$hierarchy$$prerequisites$$derived$$interfaces$$implementations$$args_synop$$signals_synop
2105 <refsect1 id="$section_id.description" role="desc">
2106 <title role="desc.title">Description</title>
2107 $extralinks$long_desc
2108 </refsect1>
2109 <refsect1 id="$section_id.details" role="details">
2110 <title role="details.title">Details</title>
2111 $$details
2112 </refsect1>
2113 $$args_desc$$signals_desc$see_also
2114 </refentry>
2116     close (OUTPUT);
2118     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2122 #############################################################################
2123 # Function    : OutputExtraFile
2124 # Description : Copies an "extra" DocBook file into the output directory,
2125 #               expanding abbreviations
2126 # Arguments   : $file - the source file.
2127 #############################################################################
2128 sub OutputExtraFile {
2129     my ($file) = @_;
2131     my $basename;
2133     ($basename = $file) =~ s!^.*/!!;
2135     my $old_sgml_file = "$SGML_OUTPUT_DIR/$basename";
2136     my $new_sgml_file = "$SGML_OUTPUT_DIR/$basename.new";
2138     my $contents;
2140     open(EXTRA_FILE, "<$file") || die "Can't open $file";
2142     {
2143         local $/;
2144         $contents = <EXTRA_FILE>;
2145     }
2147     open (OUTPUT, ">$new_sgml_file")
2148         || die "Can't create $new_sgml_file: $!";
2150     print OUTPUT &ExpandAbbreviations ("$basename file", $contents);
2151     close (OUTPUT);
2153     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2155 #############################################################################
2156 # Function    : OutputBook
2157 # Description : Outputs the SGML entities that need to be included into the
2158 #               main SGML file for the module.
2159 # Arguments   : $book_top - the declarations of the entities, which are added
2160 #                 at the top of the main SGML file.
2161 #               $book_bottom - the references to the entities, which are
2162 #                 added in the main SGML file at the desired position.
2163 #############################################################################
2165 sub OutputBook {
2166     my ($book_top, $book_bottom) = @_;
2168     my $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top";
2169     my $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top.new";
2171     open (OUTPUT, ">$new_file")
2172         || die "Can't create $new_file: $!";
2173     print OUTPUT $book_top;
2174     close (OUTPUT);
2176     &UpdateFileIfChanged ($old_file, $new_file, 0);
2179     $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom";
2180     $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom.new";
2182     open (OUTPUT, ">$new_file")
2183         || die "Can't create $new_file: $!";
2184     print OUTPUT $book_bottom;
2185     close (OUTPUT);
2187     &UpdateFileIfChanged ($old_file, $new_file, 0);
2190     # If the main SGML file hasn't been created yet, we create it here.
2191     # The user can tweak it later.
2192     if ($MAIN_SGML_FILE && ! -e $MAIN_SGML_FILE) {
2193       open (OUTPUT, ">$MAIN_SGML_FILE")
2194         || die "Can't create $MAIN_SGML_FILE: $!";
2196       if (lc($OUTPUT_FORMAT) eq "xml") {
2197           print OUTPUT <<EOF;
2198 <?xml version="1.0"?>
2199 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
2200                "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"
2202   <!ENTITY % local.common.attrib "xmlns:xi  CDATA  #FIXED 'http://www.w3.org/2003/XInclude'">
2204 <book id="index">
2206       } else {
2207         print OUTPUT <<EOF;
2208 <!doctype book PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
2209 $book_top
2211 <book id="index">
2213       }
2215 print OUTPUT <<EOF;
2216   <bookinfo>
2217     <title>$MODULE Reference Manual</title>
2218     <releaseinfo>
2219       for $MODULE [VERSION].
2220       The latest version of this documentation can be found on-line at
2221       <ulink role="online-location" url="http://[SERVER]/$MODULE/index.html">http://[SERVER]/$MODULE/</ulink>.
2222     </releaseinfo>
2223   </bookinfo>
2225   <chapter>
2226     <title>[Insert title here]</title>
2227     $book_bottom
2228   </chapter>
2230   if (-e $OBJECT_TREE_FILE) {
2231     print OUTPUT <<EOF;
2232   <chapter id="object-tree">
2233     <title>Object Hierarchy</title>
2234      <xi:include href="xml/tree_index.sgml"/>
2235   </chapter>
2237   }
2239 print OUTPUT <<EOF;
2240   <index id="api-index-full">
2241     <title>API Index</title>
2242     <xi:include href="xml/api-index-full.xml"><xi:fallback /></xi:include>
2243   </index>
2245   <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
2246 </book>
2249       close (OUTPUT);
2250     }
2254 #############################################################################
2255 # Function    : CreateValidSGML
2256 # Description : This turns any chars which are used in SGML into entities,
2257 #               e.g. '<' into '&lt;'
2258 # Arguments   : $text - the text to turn into proper SGML.
2259 #############################################################################
2261 sub CreateValidSGML {
2262     my ($text) = @_;
2263     $text =~ s/&/&amp;/g;       # Do this first, or the others get messed up.
2264     $text =~ s/</&lt;/g;
2265     $text =~ s/>/&gt;/g;
2266     # browers render single tabs inconsistently
2267     $text =~ s/([^\s])\t([^\s])/$1&#160;$2/g;
2268     return $text;
2271 #############################################################################
2272 # Function    : ConvertSGMLChars
2273 # Description : This is used for text in source code comment blocks, to turn
2274 #               chars which are used in SGML into entities, e.g. '<' into
2275 #               '&lt;'. Depending on $SGML_MODE, this is done
2276 #               unconditionally or only if the character doesn't seem to be
2277 #               part of an SGML construct (tag or entity reference).
2278 # Arguments   : $text - the text to turn into proper SGML.
2279 #############################################################################
2281 sub ConvertSGMLChars {
2282     my ($symbol, $text) = @_;
2284     if ($SGML_MODE) {
2285         # For the SGML mode only convert to entities outside CDATA sections.
2286         return &ModifyXMLElements ($text, $symbol,
2287                                    "<!\\[CDATA\\[|<programlisting[^>]*>",
2288                                    \&ConvertSGMLCharsEndTag,
2289                                    \&ConvertSGMLCharsCallback);
2290     } else {
2291         # For the simple non-sgml mode, convert to entities everywhere.
2292         $text =~ s/&/&amp;/g;   # Do this first, or the others get messed up.
2293         $text =~ s/</&lt;/g;
2294         $text =~ s/>/&gt;/g;
2295         return $text;
2296     }
2300 sub ConvertSGMLCharsEndTag {
2301   if ($_[0] eq "<!\[CDATA\[") {
2302     return "]]>";
2303   } else {
2304     return "</programlisting>";
2305   }
2308 sub ConvertSGMLCharsCallback {
2309   my ($text, $symbol, $tag) = @_;
2311   if ($tag =~ m/^<programlisting/) {
2312     # We can handle <programlisting> specially here.
2313     return &ModifyXMLElements ($text, $symbol,
2314                                "<!\\[CDATA\\[",
2315                                \&ConvertSGMLCharsEndTag,
2316                                \&ConvertSGMLCharsCallback2);
2317   } elsif ($tag eq "") {
2318     # If we're not in CDATA convert to entities.
2319     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2320     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2321     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2323     # Handle "#include <xxxxx>"
2324     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2325   }
2327   return $text;
2330 sub ConvertSGMLCharsCallback2 {
2331   my ($text, $symbol, $tag) = @_;
2333   # If we're not in CDATA convert to entities.
2334   # We could handle <programlisting> differently, though I'm not sure it helps.
2335   if ($tag eq "") {
2336     # replace only if its not a tag
2337     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2338     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2339     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2341     # Handle "#include <xxxxx>"
2342     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2343   }
2345   return $text;
2348 #############################################################################
2349 # Function    : ExpandAnnotation
2350 # Description : This turns annotations into acrony tags.
2351 # Arguments   : $symbol - the symbol being documented, for error messages.
2352 #               $text - the text to expand.
2353 #############################################################################
2354 sub ExpandAnnotation {
2355     my ($symbol, $param_desc) = @_;
2356     my $param_annotations = "";
2357     
2358     if ($param_desc =~ m%\s*\((.*)\):%) {
2359         my @annotations;
2360         my $annotation;
2361         my $annotation_extra = "";
2362         $param_desc = $';
2363     
2364         @annotations = split(/\)\s*\(/,$1);
2365         foreach $annotation (@annotations) {
2366             # need to search for the longest key-match in %AnnotationDefinition
2367             my $match_length=0;
2368             my $match_annotation="";
2369             my $annotationdef;
2370             foreach $annotationdef (keys %AnnotationDefinition) {
2371                 if ($annotation =~ m/^$annotationdef/) {
2372                     if (length($annotationdef)>$match_length) {
2373                         $match_length=length($annotationdef);
2374                         $match_annotation=$annotationdef;
2375                     }
2376                 }
2377             }
2378             if ($match_annotation ne "") {
2379                 if ($annotation =~ m%$match_annotation\s+(.*)%) {
2380                     $annotation_extra = " $1";
2381                 }
2382                 $AnnotationsUsed{$match_annotation} = 1;
2383                 $param_annotations .= "<acronym>$match_annotation</acronym>$annotation_extra. ";
2384             }
2385             else {
2386                 &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2387                     "unknown annotation \"$annotation\" in documentation for $symbol.");
2388                 $param_annotations=$annotation;
2389             }
2390         }
2391         chomp($param_desc);
2392         $param_desc =~ m/^(.*)\.*\s*$/;
2393         $param_desc = "$1. ";
2394     }    
2395     return ($param_desc, $param_annotations);
2398 #############################################################################
2399 # Function    : ExpandAbbreviations
2400 # Description : This turns the abbreviations function(), macro(), @param,
2401 #               %constant, and #symbol into appropriate DocBook markup.
2402 #               CDATA sections and <programlisting> parts are skipped.
2403 # Arguments   : $symbol - the symbol being documented, for error messages.
2404 #               $text - the text to expand.
2405 #############################################################################
2407 sub ExpandAbbreviations {
2408   my ($symbol, $text) = @_;
2410   # Convert "|[" and "]|" into the start and end of program listing examples.
2411   $text =~ s%\|\[%<informalexample><programlisting>%g;
2412   $text =~ s%\]\|%</programlisting></informalexample>%g;
2413   # TODO: check for a xml comment after |[ and pick the language attribute from
2414   # that
2416   # keep CDATA unmodified, preserve ulink tags (ideally we preseve all tags
2417   # as such)
2418   return &ModifyXMLElements ($text, $symbol,
2419                              "<!\\[CDATA\\[|<ulink[^>]*>|<programlisting[^>]*>|<!DOCTYPE",
2420                              \&ExpandAbbreviationsEndTag,
2421                              \&ExpandAbbreviationsCallback);
2425 # Returns the end tag corresponding to the given start tag.
2426 sub ExpandAbbreviationsEndTag {
2427   my ($start_tag) = @_;
2429   if ($start_tag eq "<!\[CDATA\[") {
2430     return "]]>";
2431   } elsif ($start_tag eq "<!DOCTYPE") {
2432     return "]>";
2433   } elsif ($start_tag =~ m/<(\w+)/) {
2434     return "</$1>";
2435   }
2438 # Called inside or outside each CDATA or <programlisting> section.
2439 sub ExpandAbbreviationsCallback {
2440   my ($text, $symbol, $tag) = @_;
2442   if ($tag =~ m/^<programlisting/) {
2443     # Handle any embedded CDATA sections.
2444     return &ModifyXMLElements ($text, $symbol,
2445                                "<!\\[CDATA\\[",
2446                                \&ExpandAbbreviationsEndTag,
2447                                \&ExpandAbbreviationsCallback2);
2448   } elsif ($tag eq "") {
2449     # We are outside any CDATA or <programlisting> sections, so we expand
2450     # any gtk-doc abbreviations.
2452     # Convert 'function()' or 'macro()'.
2453     # if there is abc_*_def() we don't want to make a link to _def()
2454     # FIXME: also handle abc(....) : but that would need to be done recursively :/
2455     $text =~ s/([^\*.\w])(\w+)\s*\(\)/$1.&MakeXRef($2, &tagify($2 . "()", "function"));/eg;
2456     # handle #Object.func()
2457     $text =~ s/(\A|[^\\])#([\w\-:\.]+[\w]+)\s*\(\)/$1.&MakeXRef($2, &tagify($2 . "()", "function"));/eg;
2459     # Convert '@param', but not '\@param'.
2460     $text =~ s/(\A|[^\\])\@(\w+((\.|->)\w+)*)/$1<parameter>$2<\/parameter>/g;
2461     $text =~ s/\\\@/\@/g;
2463     # Convert '%constant', but not '\%constant'.
2464     # Also allow negative numbers, e.g. %-1.
2465     $text =~ s/(\A|[^\\])\%(-?\w+)/$1.&MakeXRef($2, &tagify($2, "literal"));/eg;
2466     $text =~ s/\\\%/\%/g;
2468     # Convert '#symbol', but not '\#symbol'.
2469     $text =~ s/(\A|[^\\])#([\w\-:\.]+[\w]+)/$1.&MakeHashXRef($2, "type");/eg;
2470     $text =~ s/\\#/#/g;
2471     
2472     # Expand urls
2473     # FIXME: should we skip urls that are already tagged? (e.g. <literal>http://...</literal>)
2474     # this is apparently also called for markup and not just for plain text
2475     # disable for now.
2476     #$text =~ s%(http|https|ftp)://(.*?)((?:\s|,|\)|\]|\<|\.\s))%<ulink url="$1://$2">$2</ulink>$3%g;
2477   }
2479   return $text;
2482 # This is called inside a <programlisting>
2483 sub ExpandAbbreviationsCallback2 {
2484   my ($text, $symbol, $tag) = @_;
2486   if ($tag eq "") {
2487     # We are inside a <programlisting> but outside any CDATA sections,
2488     # so we expand any gtk-doc abbreviations.
2489     # FIXME: why is this different from &ExpandAbbreviationsCallback(),
2490     #        why not just call it
2491     $text =~ s/#(\w+)/&MakeHashXRef($1, "");/eg;
2492   }
2494   return $text;
2497 sub MakeHashXRef {
2498     my ($symbol, $tag) = @_;;
2499     my $text = $symbol;
2501     # Check for things like '#include', '#define', and skip them.
2502     if ($PreProcessorDirectives{$symbol}) {
2503       return "#$symbol";
2504     }
2506     # Get rid of any special '-struct' suffix.
2507     $text =~ s/-struct$//;
2509     # If the symbol is in the form "Object::signal", then change the symbol to
2510     # "Object-signal" and use "signal" as the text.
2511     if ($symbol =~ s/::/-/) {
2512       $text = "\"$'\"";
2513     }
2515     # If the symbol is in the form "Object:property", then change the symbol to
2516     # "Object--property" and use "property" as the text.
2517     if ($symbol =~ s/:/--/) {
2518       $text = "\"$'\"";
2519     }
2521     if ($tag ne "") {
2522       $text = tagify ($text, $tag);
2523     }
2524     
2525     return &MakeXRef($symbol, $text);
2529 #############################################################################
2530 # Function    : ModifyXMLElements
2531 # Description : Looks for given XML element tags within the text, and calls
2532 #               the callback on pieces of text inside & outside those elements.
2533 #               Used for special handling of text inside things like CDATA
2534 #               and <programlisting>.
2535 # Arguments   : $text - the text.
2536 #               $symbol - the symbol currently being documented (only used for
2537 #                      error messages).
2538 #               $start_tag_regexp - the regular expression to match start tags.
2539 #                      e.g. "<!\\[CDATA\\[|<programlisting[^>]*>" to match
2540 #                      CDATA sections or programlisting elements.
2541 #               $end_tag_func - function which is passed the matched start tag
2542 #                      and should return the appropriate end tag string.
2543 #               $callback - callback called with each part of the text. It is
2544 #                      called with a piece of text, the symbol being
2545 #                      documented, and the matched start tag or "" if the text
2546 #                      is outside the XML elements being matched.
2547 #############################################################################
2548 sub ModifyXMLElements {
2549     my ($text, $symbol, $start_tag_regexp, $end_tag_func, $callback) = @_;
2550     my ($before_tag, $start_tag, $end_tag_regexp, $end_tag);
2551     my $result = "";
2553     while ($text =~ m/$start_tag_regexp/s) {
2554       $before_tag = $`; # Prematch for last successful match string
2555       $start_tag = $&;  # Last successful match
2556       $text = $';       # Postmatch for last successful match string
2558       $result .= &$callback ($before_tag, $symbol, "");
2559       $result .= $start_tag;
2561       # get the mathing end-tag for current tag
2562       $end_tag_regexp = &$end_tag_func ($start_tag);
2564       if ($text =~ m/$end_tag_regexp/s) {
2565         $before_tag = $`;
2566         $end_tag = $&;
2567         $text = $';
2569         $result .= &$callback ($before_tag, $symbol, $start_tag);
2570         $result .= $end_tag;
2571       } else {
2572         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2573             "Can't find tag end: $end_tag_regexp in docs for: $symbol.");
2574         # Just assume it is all inside the tag.
2575         $result .= &$callback ($text, $symbol, $start_tag);
2576         $text = "";
2577       }
2578     }
2580     # Handle any remaining text outside the tags.
2581     $result .= &$callback ($text, $symbol, "");
2583     return $result;
2586 sub noop {
2587   return $_[0];
2590 # Adds a tag around some text.
2591 # e.g tagify("Text", "literal") => "<literal>Text</literal>".
2592 sub tagify {
2593    my ($text, $elem) = @_;
2594    return "<" . $elem . ">" . $text . "</" . $elem . ">";
2598 #############################################################################
2599 # Function    : MakeXRef
2600 # Description : This returns a cross-reference link to the given symbol.
2601 #               Though it doesn't try to do this for a few standard C types
2602 #               that it knows won't be in the documentation.
2603 # Arguments   : $symbol - the symbol to try to create a XRef to.
2604 #               $text - text text to put inside the XRef, defaults to $symbol
2605 #############################################################################
2607 sub MakeXRef {
2608     my ($symbol, $text) = ($_[0], $_[1]);
2610     $symbol =~ s/^\s+//;
2611     $symbol =~ s/\s+$//;
2613     if (!defined($text)) {
2614         $text = $symbol;
2616         # Get rid of special '-struct' suffix.
2617         $text =~ s/-struct$//;
2618     }
2620     if ($symbol =~ m/ /) {
2621         return "$text";
2622     }
2624     #print "Getting type link for $symbol -> $text\n";
2626     my $symbol_id = &CreateValidSGMLID ($symbol);
2627     return "<link linkend=\"$symbol_id\">$text</link>";
2631 #############################################################################
2632 # Function    : MakeIndexterms
2633 # Description : This returns a indexterm elements for the given symbol
2634 # Arguments   : $symbol - the symbol to create indexterms for
2635 #############################################################################
2637 sub MakeIndexterms {
2638   my ($symbol, $id) = @_;
2639   my $terms =  "";
2640   my $sortas = "";
2641   
2642   # make the index useful, by ommiting the namespace when sorting
2643   if ($NAME_SPACE ne "") {
2644     if ($symbol =~ m/^$NAME_SPACE\_?(.*)/i) {
2645        $sortas=" sortas=\"$1\"";
2646     }
2647   }
2649   if (exists $Deprecated{$symbol}) {
2650       $terms .= "<indexterm zone=\"$id\" role=\"deprecated\"><primary$sortas>$symbol</primary></indexterm>";
2651       $IndexEntriesDeprecated{$symbol}=$id;
2652       $IndexEntriesFull{$symbol}=$id;
2653   }
2654   if (exists $Since{$symbol}) {
2655      my $since = $Since{$symbol};
2656      $since =~ s/^\s+//;
2657      $since =~ s/\s+$//;
2658      if ($since ne "") {
2659          $terms .= "<indexterm zone=\"$id\" role=\"$since\"><primary$sortas>$symbol</primary></indexterm>";
2660      }
2661      $IndexEntriesSince{$symbol}=$id;
2662      $IndexEntriesFull{$symbol}=$id;
2663   }
2664   if ($terms eq "") {
2665      $terms .= "<indexterm zone=\"$id\"><primary$sortas>$symbol</primary></indexterm>";
2666      $IndexEntriesFull{$symbol}=$id;
2667   }
2669   return $terms;
2672 #############################################################################
2673 # Function    : MakeDeprecationNote
2674 # Description : This returns a deprecation warning for the given symbol.
2675 # Arguments   : $symbol - the symbol to try to create a warning for.
2676 #############################################################################
2678 sub MakeDeprecationNote {
2679     my ($symbol) = $_[0];
2680     my $desc = "";
2681     my $note = "";
2682     if (exists $Deprecated{$symbol}) {
2683         $desc .= "<warning>";
2685         if ($Deprecated{$symbol} =~ /^\s*([0-9\.]+)\s*:/) {
2686                 $desc .= "<para><literal>$symbol</literal> has been deprecated since version $1 and should not be used in newly-written code.";
2687         } else {
2688                 $desc .= "<para><literal>$symbol</literal> is deprecated and should not be used in newly-written code.";
2689         }
2690         if ($Deprecated{$symbol} ne "") {
2691             $note = &ExpandAbbreviations($symbol, $Deprecated{$symbol});
2692             $note =~ s/^\s*([0-9\.]+)\s*:\s*//;
2693             $note =~ s/^\s+//;
2694             $note =~ s/\s+$//;
2695             $note =~ s%\n{2,}%\n</para>\n<para>\n%g;
2696             $desc .= " " . $note;
2697         }
2698         $desc .= "</para></warning>\n";
2699     }
2700     return $desc;
2703 #############################################################################
2704 # Function    : MakeConditionDescription
2705 # Description : This returns a sumary of conditions for the given symbol.
2706 # Arguments   : $symbol - the symbol to try to create the sumary.
2707 #############################################################################
2709 sub MakeConditionDescription {
2710     my ($symbol) = $_[0];
2711     my $desc = "";
2713     if (exists $Deprecated{$symbol}) {
2714         if ($desc ne "") {
2715             $desc .= "|";
2716         }
2718         if ($Deprecated{$symbol} =~ /^\s*(.*?)\s*$/) {
2719                 $desc .= "deprecated:$1";
2720         } else {
2721                 $desc .= "deprecated";
2722         }
2723     }
2725     if (exists $Since{$symbol}) {
2726         if ($desc ne "") {
2727             $desc .= "|";
2728         }
2730         if ($Since{$symbol} =~ /^\s*(.*?)\s*$/) {
2731                 $desc .= "since:$1";
2732         } else {
2733                 $desc .= "since";
2734         }
2735     }
2737     if (exists $StabilityLevel{$symbol}) {
2738         if ($desc ne "") {
2739             $desc .= "|";
2740         }
2741         $desc .= "stability:".$StabilityLevel{$symbol};
2742     }
2744     if ($desc ne "") {
2745         $desc=" condition=\"".$desc."\"";
2746         #print "condition for '$symbol' = '$desc'\n";
2747     }
2748     return $desc;
2751 #############################################################################
2752 # Function    : GetHierarchy
2753 # Description : Returns the DocBook output describing the ancestors and
2754 #               immediate children of a GObject subclass. It uses the
2755 #               global @Objects and @ObjectLevels arrays to walk the tree.
2756 # Arguments   : $object - the GtkObject subclass.
2757 #############################################################################
2759 sub GetHierarchy {
2760     my ($object) = @_;
2762     # Find object in the objects array.
2763     my $found = 0;
2764     my @children = ();
2765     my $i;
2766     my $level;
2767     my $j;
2768     for ($i = 0; $i < @Objects; $i++) {
2769         if ($found) {
2770             if ($ObjectLevels[$i] <= $level) {
2771             last;
2772         }
2773             elsif ($ObjectLevels[$i] == $level + 1) {
2774                 push (@children, $Objects[$i]);
2775             }
2776         }
2777         elsif ($Objects[$i] eq $object) {
2778             $found = 1;
2779             $j = $i;
2780             $level = $ObjectLevels[$i];
2781         }
2782     }
2783     if (!$found) {
2784         return "";
2785     }
2787     # Walk up the hierarchy, pushing ancestors onto the ancestors array.
2788     my @ancestors = ();
2789     push (@ancestors, $object);
2790     #print "Level: $level\n";
2791     while ($level > 1) {
2792         $j--;
2793         if ($ObjectLevels[$j] < $level) {
2794             push (@ancestors, $Objects[$j]);
2795             $level = $ObjectLevels[$j];
2796             #print "Level: $level\n";
2797         }
2798     }
2800     # Output the ancestors list, indented and with links.
2801     my $hierarchy = "<synopsis>\n";
2802     $level = 0;
2803     for ($i = $#ancestors; $i >= 0; $i--) {
2804         my $link_text;
2805         # Don't add a link to the current widget, i.e. when i == 0.
2806         if ($i > 0) {
2807             my $ancestor_id = &CreateValidSGMLID ($ancestors[$i]);
2808             $link_text = "<link linkend=\"$ancestor_id\">$ancestors[$i]</link>";
2809         } else {
2810             $link_text = "$ancestors[$i]";
2811         }
2812         if ($level == 0) {
2813             $hierarchy .= "  $link_text\n";
2814         } else {
2815 #           $hierarchy .= ' ' x ($level * 6 - 3) . "|\n";
2816             $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2817         }
2818         $level++;
2819     }
2820     for ($i = 0; $i <= $#children; $i++) {
2821       my $id = &CreateValidSGMLID ($children[$i]);
2822       my $link_text = "<link linkend=\"$id\">$children[$i]</link>";
2823       $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2824     }
2825     $hierarchy .= "</synopsis>\n";
2827     return $hierarchy;
2831 #############################################################################
2832 # Function    : GetInterfaces
2833 # Description : Returns the DocBook output describing the interfaces
2834 #               implemented by a class. It uses the global %Interfaces hash.
2835 # Arguments   : $object - the GtkObject subclass.
2836 #############################################################################
2838 sub GetInterfaces {
2839     my ($object) = @_;
2840     my $text = "";
2841     my $i;
2843     # Find object in the objects array.
2844     if (exists($Interfaces{$object})) {
2845         my @ifaces = split(' ', $Interfaces{$object});
2846         $text = <<EOF;
2847 <para>
2848 $object implements
2850         for ($i = 0; $i <= $#ifaces; $i++) {
2851             my $id = &CreateValidSGMLID ($ifaces[$i]);
2852             $text .= " <link linkend=\"$id\">$ifaces[$i]</link>";
2853             if ($i < $#ifaces - 1) {
2854                 $text .= ', ';
2855             }
2856             elsif ($i < $#ifaces) {
2857                 $text .= ' and ';
2858             }
2859             else {
2860                 $text .= '.';
2861             }
2862         }
2863         $text .= <<EOF;
2864 </para>
2866     }
2868     return $text;
2871 #############################################################################
2872 # Function    : GetImplementations
2873 # Description : Returns the DocBook output describing the implementations
2874 #               of an interface. It uses the global %Interfaces hash.
2875 # Arguments   : $object - the GtkObject subclass.
2876 #############################################################################
2878 sub GetImplementations {
2879     my ($object) = @_;
2880     my @impls = ();
2881     my $text = "";
2882     my $i;
2883     foreach my $key (keys %Interfaces) {
2884         if ($Interfaces{$key} =~ /\b$object\b/) {
2885             push (@impls, $key);
2886         }
2887     }
2888     if ($#impls >= 0) {
2889         $text = <<EOF;
2890 <para>
2891 $object is implemented by
2893         for ($i = 0; $i <= $#impls; $i++) {
2894             my $id = &CreateValidSGMLID ($impls[$i]);
2895             $text .= " <link linkend=\"$id\">$impls[$i]</link>";
2896             if ($i < $#impls - 1) {
2897                 $text .= ', ';
2898             }
2899             elsif ($i < $#impls) {
2900                 $text .= ' and ';
2901             }
2902             else {
2903                 $text .= '.';
2904             }
2905         }
2906         $text .= <<EOF;
2907 </para>
2909     }
2910     return $text;
2914 #############################################################################
2915 # Function    : GetPrerequisites
2916 # Description : Returns the DocBook output describing the prerequisites
2917 #               of an interface. It uses the global %Prerequisites hash.
2918 # Arguments   : $iface - the interface.
2919 #############################################################################
2921 sub GetPrerequisites {
2922     my ($iface) = @_;
2923     my $text = "";
2924     my $i;
2926     if (exists($Prerequisites{$iface})) {
2927         $text = <<EOF;
2928 <para>
2929 $iface requires
2931         my @prereqs = split(' ', $Prerequisites{$iface});
2932         for ($i = 0; $i <= $#prereqs; $i++) {
2933             my $id = &CreateValidSGMLID ($prereqs[$i]);
2934             $text .= " <link linkend=\"$id\">$prereqs[$i]</link>";
2935             if ($i < $#prereqs - 1) {
2936                 $text .= ', ';
2937             }
2938             elsif ($i < $#prereqs) {
2939                 $text .= ' and ';
2940             }
2941             else {
2942                 $text .= '.';
2943             }
2944         }
2945         $text .= <<EOF;
2946 </para>
2948     }
2949     return $text;
2952 #############################################################################
2953 # Function    : GetDerived
2954 # Description : Returns the DocBook output describing the derived interfaces
2955 #               of an interface. It uses the global %Prerequisites hash.
2956 # Arguments   : $iface - the interface.
2957 #############################################################################
2959 sub GetDerived {
2960     my ($iface) = @_;
2961     my $text = "";
2962     my $i;
2964     my @derived = ();
2965     foreach my $key (keys %Prerequisites) {
2966         if ($Prerequisites{$key} =~ /\b$iface\b/) {
2967             push (@derived, $key);
2968         }
2969     }
2970     if ($#derived >= 0) {
2971         $text = <<EOF;
2972 <para>
2973 $iface is required by
2975         for ($i = 0; $i <= $#derived; $i++) {
2976             my $id = &CreateValidSGMLID ($derived[$i]);
2977             $text .= " <link linkend=\"$id\">$derived[$i]</link>";
2978             if ($i < $#derived - 1) {
2979                 $text .= ', ';
2980             }
2981             elsif ($i < $#derived) {
2982                 $text .= ' and ';
2983             }
2984             else {
2985                 $text .= '.';
2986             }
2987         }
2988         $text .= <<EOF;
2989 </para>
2991     }
2992     return $text;
2996 #############################################################################
2997 # Function    : GetSignals
2998 # Description : Returns the synopsis and detailed description DocBook output
2999 #               for the signal handlers of a given GtkObject subclass.
3000 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
3001 #############################################################################
3003 sub GetSignals {
3004     my ($object) = @_;
3005     my $synop = "";
3006     my $desc = "";
3008     my $i;
3009     for ($i = 0; $i <= $#SignalObjects; $i++) {
3010         if ($SignalObjects[$i] eq $object) {
3011             #print "Found signal: $SignalNames[$i]\n";
3012             my $name = $SignalNames[$i];
3013             my $symbol = "${object}::${name}";
3014             my $id = &CreateValidSGMLID ("$object-$name");
3016             my $pad = ' ' x (46 - length($name));
3017             $synop .= "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad ";
3019             $desc .= "<refsect2 id=\"$id\" role=\"signal\"><title>The <literal>&quot;$name&quot;</literal> signal</title>\n";
3020             $desc .= MakeIndexterms($symbol, $id);
3021             $desc .= "\n";
3022             $desc .= OutputSymbolExtraLinks($symbol);
3024             $desc .= "<programlisting>";
3026             $SignalReturns[$i] =~ m/\s*(const\s+)?(\w+)\s*(\**)/;
3027             my $type_modifier = defined($1) ? $1 : "";
3028             my $type = $2;
3029             my $pointer = $3;
3030             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
3032             my $ret_type_len = length ($type_modifier) + length ($pointer)
3033                 + length ($type);
3034             my $ret_type_output = "$type_modifier$xref$pointer"
3035                 . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
3037             $desc  .= "${ret_type_output}user_function " . &MakeReturnField("") . " (";
3039             my $sourceparams = $SourceSymbolParams{$symbol};
3040             my @params = split ("\n", $SignalPrototypes[$i]);
3041             my $j;
3042             my $l;
3043             my $type_len = length("gpointer");
3044             my $name_len = length("user_data");
3045             # do two passes, the first one is to calculate padding
3046             for ($l = 0; $l < 2; $l++) {
3047                 for ($j = 0; $j <= $#params; $j++) {
3048                     # allow alphanumerics, '_', '[' & ']' in param names
3049                     if ($params[$j] =~ m/^\s*(\w+)\s*(\**)\s*([\w\[\]]+)\s*$/) {
3050                         $type = $1;
3051                         $pointer = $2;
3052                         if (defined($sourceparams)) {
3053                             $name = $$sourceparams[$PARAM_FIELD_COUNT * $j];
3054                         }
3055                         else {
3056                             $name = $3;
3057                         }
3058                         if (!defined($name)) {
3059                             $name = "arg$j";
3060                         }
3061                         if ($l == 0) {
3062                             if (length($type) + length($pointer) > $type_len) {
3063                                 $type_len = length($type) + length($pointer);
3064                             }
3065                             if (length($name) > $name_len) {
3066                                 $name_len = length($name);
3067                             }
3068                         }
3069                         else {
3070                             $xref = &MakeXRef ($type, &tagify($type, "type"));
3071                             $pad = ' ' x ($type_len - length($type) - length($pointer));
3072                             $desc .= "$xref$pad $pointer$name,\n";
3073                             $desc .= (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
3074                         }
3075                     } else {
3076                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3077                              "Can't parse arg: $params[$j]\nArgs:$SignalPrototypes[$i]");
3078                     }
3079                 }
3080             }
3081             $xref = &MakeXRef ("gpointer", &tagify("gpointer", "type"));
3082             $pad = ' ' x ($type_len - length("gpointer"));
3083             $desc  .= "$xref$pad user_data)";
3085             my $flags = $SignalFlags[$i];
3086             my $flags_string = "";
3088             if (defined ($flags)) {
3089               if ($flags =~ m/f/) {
3090                 $flags_string = "Run First";
3091               }
3092               elsif ($flags =~ m/l/) {
3093                 $flags_string = "Run Last";
3094               }
3095               elsif ($flags =~ m/c/) {
3096                 $flags_string = "Cleanup";
3097               }
3098               if ($flags =~ m/r/) {
3099                 if ($flags_string) { $flags_string .= " / "; }
3100                 $flags_string .= "No Recursion";
3101               }
3102               if ($flags =~ m/d/) {
3103                 if ($flags_string) { $flags_string .= " / "; }
3104                 $flags_string .= "Has Details";
3105               }
3106               if ($flags =~ m/a/) {
3107                 if ($flags_string) { $flags_string .= " / "; }
3108                 $flags_string .= "Action";
3109               }
3110               if ($flags =~ m/h/) {
3111                 if ($flags_string) { $flags_string .= " / "; }
3112                 $flags_string .= "No Hooks";
3113               }
3114             }
3116             if ($flags_string)
3117               {
3118                 $synop .= ": $flags_string\n";
3120                 $pad = ' ' x (5 + $name_len - length("user_data"));
3121                 $desc  .= "$pad : $flags_string</programlisting>\n";
3122               }
3123             else
3124               {
3125                 $synop .= "\n";
3126                 $desc  .= "</programlisting>\n";
3127               }
3129             $desc .= &MakeDeprecationNote($symbol);
3131             my $parameters = &OutputParamDescriptions ("SIGNAL", $symbol);
3132             my $parameters_output = 0;
3134             $AllSymbols{$symbol} = 1;
3135             if (defined ($SymbolDocs{$symbol})) {
3136                 my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3138                 # Try to insert the parameter table at the author's desired
3139                 # position. Otherwise we need to tag it onto the end.
3140                 if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
3141                   $parameters_output = 1;
3142                 }
3143                 $desc .= $symbol_docs;
3145                 if (!IsEmptyDoc($SymbolDocs{$symbol})) {
3146                     $AllDocumentedSymbols{$symbol} = 1;
3147                 }
3148             }
3150             if ($parameters_output == 0) {
3151                 $desc .= $parameters;
3152               }
3153             $desc .= OutputSymbolTraits ($symbol);
3154             $desc .= "</refsect2>";
3155         }
3156     }
3157     return ($synop, $desc);
3161 #############################################################################
3162 # Function    : GetArgs
3163 # Description : Returns the synopsis and detailed description DocBook output
3164 #               for the Args of a given GtkObject subclass.
3165 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
3166 #############################################################################
3168 sub GetArgs {
3169     my ($object) = @_;
3170     my $synop = "";
3171     my $desc = "";
3172     my $child_synop = "";
3173     my $child_desc = "";
3174     my $style_synop = "";
3175     my $style_desc = "";
3177     my $i;
3178     for ($i = 0; $i <= $#ArgObjects; $i++) {
3179         if ($ArgObjects[$i] eq $object) {
3180             #print "Found arg: $ArgNames[$i]\n";
3181             my $name = $ArgNames[$i];
3182             my $flags = $ArgFlags[$i];
3183             my $flags_string = "";
3184             my $kind = "";
3185             my $id_sep = "";
3187             if ($flags =~ m/c/) {
3188                 $kind = "child property";
3189                 $id_sep = "c-";
3190             }
3191             elsif ($flags =~ m/s/) {
3192                 $kind = "style property";
3193                 $id_sep = "s-";
3194             }
3195             else {
3196                 $kind = "property";
3197             }
3199             # Remember only one colon so we don't clash with signals.
3200             my $symbol = "${object}:${name}";
3201             # use two dashes and ev. an extra separator here for the same reason.
3202             my $id = &CreateValidSGMLID ("$object--$id_sep$name");
3204             my $type = $ArgTypes[$i];
3205             my $type_output;
3206             my $range = $ArgRanges[$i];
3207             my $range_output = CreateValidSGML ($range);
3208             my $default = $ArgDefaults[$i];
3209             my $default_output = CreateValidSGML ($default);
3211             if ($type eq "GtkString") {
3212                 $type = "char*";
3213             }
3214             if ($type eq "GtkSignal") {
3215                 $type = "GtkSignalFunc, gpointer";
3216                 $type_output = &MakeXRef ("GtkSignalFunc") . ", "
3217                     . &MakeXRef ("gpointer");
3218             } elsif ($type =~ m/^(\w+)\*$/) {
3219                 $type_output = &MakeXRef ($1, &tagify($1, "type")) . "*";
3220             } else {
3221                 $type_output = &MakeXRef ($type, &tagify($type, "type"));
3222             }
3224             if ($flags =~ m/r/) {
3225                 $flags_string = "Read";
3226             }
3227             if ($flags =~ m/w/) {
3228                 if ($flags_string) { $flags_string .= " / "; }
3229                 $flags_string .= "Write";
3230             }
3231             if ($flags =~ m/x/) {
3232                 if ($flags_string) { $flags_string .= " / "; }
3233                 $flags_string .= "Construct";
3234             }
3235             if ($flags =~ m/X/) {
3236                 if ($flags_string) { $flags_string .= " / "; }
3237                 $flags_string .= "Construct Only";
3238             }
3240             $AllSymbols{$symbol} = 1;
3241             my $blurb;
3242             if (defined($SymbolDocs{$symbol}) &&
3243                 !IsEmptyDoc($SymbolDocs{$symbol})) {
3244                 $blurb = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3245                 #print ".. [$SymbolDocs{$symbol}][$blurb]\n";
3246                 $AllDocumentedSymbols{$symbol} = 1;
3247             }
3248             else {
3249                 if (!($ArgBlurbs[$i] eq "")) {
3250                     $AllDocumentedSymbols{$symbol} = 1;
3251                 } else {
3252                     # FIXME: print a warning?
3253                     #print ".. no description\n";
3254                 }
3255                 $blurb = "<para>" . &CreateValidSGML ($ArgBlurbs[$i]) . "</para>";
3256             }
3258             my $pad1 = " " x (24 - length ($name));
3259             my $pad2 = " " x (20 - length ($type));
3261             my $arg_synop = "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad1 $type_output $pad2 : $flags_string\n";
3262             my $arg_desc = "<refsect2 id=\"$id\" role=\"property\"><title>The <literal>&quot;$name&quot;</literal> $kind</title>\n";
3263             $arg_desc .= MakeIndexterms($symbol, $id);
3264             $arg_desc .= "\n";
3265             $arg_desc .= OutputSymbolExtraLinks($symbol);
3267             $arg_desc .= "<programlisting>  &quot;$name&quot;$pad1 $type_output $pad2 : $flags_string</programlisting>\n";
3268             $arg_desc .= &MakeDeprecationNote($symbol);
3269             $arg_desc .= $blurb;
3270             if ($range ne "") {
3271                 $arg_desc .= "<para>Allowed values: $range_output</para>\n";
3272             }
3273             if ($default ne "") {
3274                 $arg_desc .= "<para>Default value: $default_output</para>\n";
3275             }
3276             $arg_desc .= OutputSymbolTraits ($symbol);
3277             $arg_desc .= "</refsect2>\n";
3279             if ($flags =~ m/c/) {
3280                 $child_synop .= $arg_synop;
3281                 $child_desc .= $arg_desc;
3282             }
3283             elsif ($flags =~ m/s/) {
3284                 $style_synop .= $arg_synop;
3285                 $style_desc .= $arg_desc;
3286             }
3287             else {
3288                 $synop .= $arg_synop;
3289                 $desc .= $arg_desc;
3290             }
3291         }
3292     }
3293     return ($synop, $child_synop, $style_synop, $desc, $child_desc, $style_desc);
3297 #############################################################################
3298 # Function    : ReadSourceDocumentation
3299 # Description : This reads in the documentation embedded in comment blocks
3300 #               in the source code (for Gnome).
3302 #               Parameter descriptions override any in the template files.
3303 #               Function descriptions are placed before any description from
3304 #               the template files.
3306 #               It recursively descends the source directory looking for .c
3307 #               files and scans them looking for specially-formatted comment
3308 #               blocks.
3310 # Arguments   : $source_dir - the directory to scan.
3311 #############m###############################################################
3313 sub ReadSourceDocumentation {
3314     my ($source_dir) = @_;
3315     my ($file, $dir, @suffix_list, $suffix);
3316     #print "Scanning source directory: $source_dir\n";
3318     # This array holds any subdirectories found.
3319     my (@subdirs) = ();
3321     @suffix_list = split (/,/, $SOURCE_SUFFIXES);
3323     opendir (SRCDIR, $source_dir)
3324         || die "Can't open source directory $source_dir: $!";
3326     foreach $file (readdir (SRCDIR)) {
3327       if ($file =~ /^\./) {
3328         next;
3329       } elsif (-d "$source_dir/$file") {
3330         push (@subdirs, $file);
3331       } elsif (@suffix_list) {
3332         foreach $suffix (@suffix_list) {
3333           if ($file =~ m/\.\Q${suffix}\E$/) {
3334             &ScanSourceFile ("$source_dir/$file");
3335           }
3336         }
3337       } elsif ($file =~ m/\.[ch]$/) {
3338         &ScanSourceFile ("$source_dir/$file");
3339       }
3340     }
3341     closedir (SRCDIR);
3343     # Now recursively scan the subdirectories.
3344     foreach $dir (@subdirs) {
3345         next if ($IGNORE_FILES =~ m/(\s|^)\Q${dir}\E(\s|$)/);
3346         &ReadSourceDocumentation ("$source_dir/$dir");
3347     }
3351 #############################################################################
3352 # Function    : ScanSourceFile
3353 # Description : Scans one source file looking for specially-formatted comment
3354 #               blocks. Later &MergeSourceDocumentation is used to merge any
3355 #               documentation found with the documentation already read in
3356 #               from the template files.
3358 # Arguments   : $file - the file to scan.
3359 #############################################################################
3361 sub ScanSourceFile {
3362     my ($file) = @_;
3363     my $basename;
3365     if ($file =~ m/^.*[\/\\]([^\/\\]*)$/) {
3366         $basename = $1;
3367     } else {
3368         &LogWarning ($file, 1, "Can't find basename for this filename.");
3369         $basename = $file;
3370     }
3372     # Check if the basename is in the list of files to ignore.
3373     if ($IGNORE_FILES =~ m/(\s|^)\Q${basename}\E(\s|$)/) {
3374         return;
3375     }
3377     #print "DEBUG: Scanning $file\n";
3379     open (SRCFILE, $file)
3380         || die "Can't open $file: $!";
3381     my $in_comment_block = 0;
3382     my $symbol;
3383     my ($in_description, $in_return, $in_since, $in_stability, $in_deprecated);
3384     my ($description, $return_desc, $return_start, $return_style);
3385     my ($since_desc, $stability_desc, $deprecated_desc);
3386     my $current_param;
3387     my $ignore_broken_returns;
3388     my @params;
3389     while (<SRCFILE>) {
3390         # Look for the start of a comment block.
3391         if (!$in_comment_block) {
3392             if (m%^\s*/\*.*\*/%) {
3393                 #one-line comment - not gtkdoc
3394             } elsif (m%^\s*/\*\*\s%) {
3395                 #print "Found comment block start\n";
3397                 $in_comment_block = 1;
3399                 # Reset all the symbol data.
3400                 $symbol = "";
3401                 $in_description = 0;
3402                 $in_return = 0;
3403                 $in_since = 0;
3404                 $in_deprecated = 0;
3405                 $in_stability = 0;
3406                 $description = "";
3407                 $return_desc = "";
3408                 $return_style = "";
3409                 $since_desc = "";
3410                 $deprecated_desc = "";
3411                 $stability_desc = "";
3412                 $current_param = -1;
3413                 $ignore_broken_returns = 0;
3414                 @params = ();
3415             }
3416             next;
3417         }
3419         # We're in a comment block. Check if we've found the end of it.
3420         if (m%^\s*\*+/%) {
3421             if (!$symbol) {
3422                 # maybe its not even meant to be a gtk-doc comment?
3423                 &LogWarning ($file, $., "Symbol name not found at the start of the comment block.");
3424             } else {
3425                 # Add the return value description onto the end of the params.
3426                 if ($return_desc) {
3427                     push (@params, "Returns");
3428                     push (@params, $return_desc);
3429                     if ($return_style eq 'broken') {
3430                         &LogWarning ($file, $., "Free-form return value description in $symbol. Use `Returns:' to avoid ambiguities.");
3431                     }
3432                 }
3433                 # Convert special SGML characters
3434                 $description = &ConvertSGMLChars ($symbol, $description);
3435                 my $k;
3436                 for ($k = 1; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3437                     $params[$k] = &ConvertSGMLChars ($symbol, $params[$k]);
3438                 }
3440                 # Handle Section docs
3441                 if ($symbol =~ m/SECTION:\s*(.*)/) {
3442                     my $real_symbol=$1;
3443                     my $key;
3445                     if (scalar %KnownSymbols) {
3446                         if ((! defined($KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"})) || $KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"} != 1) {
3447                             &LogWarning ($file, $., "Section $real_symbol is not defined in the $MODULE-section.txt file.");
3448                         }
3449                     }
3451                     #print "SECTION DOCS found in source for : '$real_symbol'\n";
3452                     $ignore_broken_returns = 1;
3453                     for ($k = 0; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3454                         #print "   '".$params[$k]."'\n";
3455                         $params[$k] = "\L$params[$k]";
3456                         undef $key;
3457                         if ($params[$k] eq "short_description") {
3458                             $key = "$TMPL_DIR/$real_symbol:Short_Description";
3459                         } elsif ($params[$k] eq "see_also") {
3460                             $key = "$TMPL_DIR/$real_symbol:See_Also";
3461                         } elsif ($params[$k] eq "title") {
3462                             $key = "$TMPL_DIR/$real_symbol:Title";
3463                         } elsif ($params[$k] eq "stability") {
3464                             $key = "$TMPL_DIR/$real_symbol:Stability_Level";
3465                         } elsif ($params[$k] eq "section_id") {
3466                             $key = "$TMPL_DIR/$real_symbol:Section_Id";
3467                         } elsif ($params[$k] eq "include") {
3468                             $key = "$TMPL_DIR/$real_symbol:Include";
3469                         }
3470                         if (defined($key)) {
3471                             $SourceSymbolDocs{$key}=$params[$k+1];
3472                             $SourceSymbolSourceFile{$key} = $file;
3473                             $SourceSymbolSourceLine{$key} = $.;
3474                         }
3475                     }
3476                     $SourceSymbolDocs{"$TMPL_DIR/$real_symbol:Long_Description"}=$description;
3477                     $SourceSymbolSourceFile{"$TMPL_DIR/$real_symbol:Long_Description"} = $file;
3478                     $SourceSymbolSourceLine{"$TMPL_DIR/$real_symbol:Long_Description"} = $.;
3479                     #$SourceSymbolTypes{$symbol} = "SECTION";
3480                 } else {
3481                     #print "SYMBOL DOCS found in source for : '$symbol' ",length($description), "\n";
3482                     $SourceSymbolDocs{$symbol} = $description;
3483                     $SourceSymbolParams{$symbol} = [ @params ];
3484                     # FIXME $SourceSymbolTypes{$symbol} = "STRUCT,SIGNAL,ARG,FUNCTION,MACRO";
3485                     #if (defined $DeclarationTypes{$symbol}) {
3486                     #    $SourceSymbolTypes{$symbol} = $DeclarationTypes{$symbol}
3487                     #}
3488                     $SourceSymbolSourceFile{$symbol} = $file;
3489                     $SourceSymbolSourceLine{$symbol} = $.;
3490                 }
3492                 if ($since_desc) {
3493                      ($since_desc, my @extra_lines) = split ("\n", $since_desc);
3494                      $since_desc =~ s/^\s+//;
3495                      $since_desc =~ s/\s+$//;
3496                      #print "Since($symbol) : [$since_desc]\n";
3497                      $Since{$symbol} = &ConvertSGMLChars ($symbol, $since_desc);
3498                      if(scalar @extra_lines) {
3499                          &LogWarning ($file, $., "multi-line since docs found");
3500                      }
3501                 }
3503                 if ($stability_desc) {
3504                     $stability_desc = &ParseStabilityLevel($stability_desc, $file, $., "Stability level for $symbol");
3505                     $StabilityLevel{$symbol} = &ConvertSGMLChars ($symbol, $stability_desc);
3506                 }
3508                 if ($deprecated_desc) {
3509                     if (exists $Deprecated{$symbol}) {
3510                     }
3511                     else {
3512                          # don't warn for signals and properties
3513                          #if ($symbol !~ m/::?(.*)/) {
3514                          if (defined $DeclarationTypes{$symbol}) {
3515                              &LogWarning ($file, $., 
3516                                  "$symbol is deprecated in the inline comments, but no deprecation guards were found around the declaration.".
3517                                  " (See the --deprecated-guards option for gtkdoc-scan.)");
3518                          }
3519                     }
3520                     $Deprecated{$symbol} = &ConvertSGMLChars ($symbol, $deprecated_desc);
3521                 }
3522             }
3524             $in_comment_block = 0;
3525             next;
3526         }
3528         # Get rid of ' * ' at start of every line in the comment block.
3529         s%^\s*\*\s?%%;
3530         # But make sure we don't get rid of the newline at the end.
3531         if (!$_) {
3532             $_ = "\n";
3533         }
3534         #print "DEBUG: scanning :$_";
3536         # If we haven't found the symbol name yet, look for it.
3537         if (!$symbol) {
3538             if (m%^\s*(SECTION:\s*\S+)%) {
3539                 $symbol = $1;
3540                 #print "SECTION DOCS found in source for : '$symbol'\n";
3541                 $ignore_broken_returns = 1;
3542             } elsif (m%^\s*([\w:-]*\w)\s*:?\s*$%) {
3543                 $symbol = $1;
3544                 #print "SYMBOL DOCS found in source for : '$symbol'\n";
3545             }
3546             next;
3547         }
3549         # If we're in the return value description, add it to the end.
3550         if ($in_return) {
3551             # If we find another valid returns line, we assume that the first
3552             # one was really part of the description.
3553             if (m/^\s*(returns:|return\s+value:)/i) {
3554                 if ($return_style eq 'broken') {
3555                     $description .= $return_start . $return_desc;
3556                 }
3557                 $return_start = $1;
3558                 if ($return_style eq 'sane') {
3559                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3560                 }
3561                 $return_style = 'sane';
3562                 $ignore_broken_returns = 1;
3563                 $return_desc = $';
3564             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3565                 $description .= $return_start . $return_desc;
3566                 $return_start = $1;
3567                 $return_style = 'broken';
3568                 $return_desc = $';
3569             } elsif (m%^\s*since:%i) {
3570                 $since_desc = $';
3571                 $in_since = 1;
3572                 $in_return = 0;
3573             } elsif (m%^\s*stability:%i) {
3574                 $stability_desc = $';
3575                 $in_stability = 1;
3576                 $in_return = 0;
3577             } elsif (m%^\s*deprecated:%i) {
3578                 $deprecated_desc = $';
3579                 $in_deprecated = 1;
3580                 $in_return = 0;
3581             } else {
3582                 $return_desc .= $_;
3583             }
3584             next;
3585         }
3587         if ($in_since) {
3588             if (m/^\s*(returns:|return\s+value:)/i) {
3589                 if ($return_style eq 'broken') {
3590                     $description .= $return_start . $return_desc;
3591                 }
3592                 $return_start = $1;
3593                 if ($return_style eq 'sane') {
3594                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3595                 }
3596                 $return_style = 'sane';
3597                 $ignore_broken_returns = 1;
3598                 $return_desc = $';
3599                 $in_return = 1;
3600                 $in_since = 0;
3601             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3602                 $return_start = $1;
3603                 $return_style = 'broken';
3604                 $return_desc = $';
3605                 $in_return = 1;
3606                 $in_since = 0;
3607             } elsif (m%^\s*deprecated:%i) {
3608                 $deprecated_desc = $';
3609                 $in_deprecated = 1;
3610                 $in_since = 0;
3611             } elsif (m%^\s*stability:%i) {
3612                 $stability_desc = $';
3613                 $in_stability = 1;
3614                 $in_since = 0;
3615             } else {
3616                 $since_desc .= $_;
3617             }
3618             next;
3619         }
3621         if ($in_stability) {
3622             if (m/^\s*(returns:|return\s+value:)/i) {
3623                 if ($return_style eq 'broken') {
3624                     $description .= $return_start . $return_desc;
3625                 }
3626                 $return_start = $1;
3627                 if ($return_style eq 'sane') {
3628                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3629                 }
3630                 $return_style = 'sane';
3631                 $ignore_broken_returns = 1;
3632                 $return_desc = $';
3633                 $in_return = 1;
3634                 $in_stability = 0;
3635             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3636                 $return_start = $1;
3637                 $return_style = 'broken';
3638                 $return_desc = $';
3639                 $in_return = 1;
3640                 $in_stability = 0;
3641             } elsif (m%^\s*deprecated:%i) {
3642                 $deprecated_desc = $';
3643                 $in_deprecated = 1;
3644                 $in_stability = 0;
3645             } elsif (m%^\s*since:%i) {
3646                 $since_desc = $';
3647                 $in_since = 1;
3648                 $in_stability = 0;
3649             } else {
3650                 $stability_desc .= $_;
3651             }
3652             next;
3653         }
3655         if ($in_deprecated) {
3656             if (m/^\s*(returns:|return\s+value:)/i) {
3657                 if ($return_style eq 'broken') {
3658                     $description .= $return_start . $return_desc;
3659                 }
3660                 $return_start = $1;
3661                 if ($return_style eq 'sane') {
3662                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3663                 }
3664                 $return_style = 'sane';
3665                 $ignore_broken_returns = 1;
3666                 $return_desc = $';
3667                 $in_return = 1;
3668                 $in_deprecated = 0;
3669             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3670                 $return_start = $1;
3671                 $return_style = 'broken';
3672                 $return_desc = $';
3673                 $in_return = 1;
3674                 $in_deprecated = 0;
3675             } elsif (m%^\s*since:%i) {
3676                 $since_desc = $';
3677                 $in_since = 1;
3678                 $in_deprecated = 0;
3679             } elsif (m%^\s*stability:%i) {
3680                 $stability_desc = $';
3681                 $in_stability = 1;
3682                 $in_deprecated = 0;
3683             } else {
3684                 $deprecated_desc .= $_;
3685             }
3686             next;
3687         }
3689         # If we're in the description part, check for the 'Returns:' line.
3690         # If that isn't found, add the text to the end.
3691         if ($in_description) {
3692             # Get rid of 'Description:'
3693             s%^\s*Description:%%;
3695             if (m/^\s*(returns:|return\s+value:)/i) {
3696                 if ($return_style eq 'broken') {
3697                     $description .= $return_start . $return_desc;
3698                 }
3699                 $return_start = $1;
3700                 if ($return_style eq 'sane') {
3701                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3702                 }
3703                 $return_style = 'sane';
3704                 $ignore_broken_returns = 1;
3705                 $return_desc = $';
3706                 $in_return = 1;
3707                 next;
3708             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3709                 $return_start = $1;
3710                 $return_style = 'broken';
3711                 $return_desc = $';
3712                 $in_return = 1;
3713                 next;
3714             } elsif (m%^\s*since:%i) {
3715                 $since_desc = $';
3716                 $in_since = 1;
3717                 next;
3718             } elsif (m%^\s*deprecated:%i) {
3719                 $deprecated_desc = $';
3720                 $in_deprecated = 1;
3721                 next;
3722             } elsif (m%^\s*stability:%i) {
3723                 $stability_desc = $';
3724                 $in_stability = 1;
3725                 next;
3726             }
3728             $description .= $_;
3729             next;
3730         }
3732         # We must be in the parameters. Check for the empty line below them.
3733         if (m%^\s*$%) {
3734             $in_description = 1;
3735             next;
3736         }
3738         # Look for a parameter name.
3739         if (m%^\s*@(\S+)\s*:%) {
3740             my $param_name = $1;
3741             my $param_desc = $';
3742             #print "Found parameter: $param_name\n";
3743             # Allow '...' as the Varargs parameter.
3744             if ($param_name eq "...") {
3745                 $param_name = "Varargs";
3746             }
3747             if ("\L$param_name" eq "returns") {
3748                 $return_style = 'sane';
3749                 $ignore_broken_returns = 1;
3750             }
3751             push (@params, $param_name);
3752             push (@params, $param_desc);
3753             $current_param += $PARAM_FIELD_COUNT;
3754             next;
3755         }
3757         # We must be in the middle of a parameter description, so add it on
3758         # to the last element in @params.
3759         if ($current_param == -1) {
3760             &LogWarning ($file, $., "Parsing comment block file : parameter expected.");
3761         } else {
3762             $params[$#params] .= $_;
3763         }
3764     }
3765     close (SRCFILE);
3768 #############################################################################
3769 # Function    : OutputMissingDocumentation
3770 # Description : Outputs report of documentation coverage to a file
3772 # Arguments   : none
3773 #############################################################################
3775 sub OutputMissingDocumentation {
3776     my $n_documented = 0;
3777     my $n_incomplete = 0;
3778     my $total = 0;
3779     my $symbol;
3780     my $percent;
3781     my $msg;
3782     my $buffer = "";
3783     my $buffer_deprecated = "";
3784     my $buffer_descriptions = "";
3785     
3786     open (UNDOCUMENTED, ">$ROOT_DIR/$MODULE-undocumented.txt")
3787       || die "Can't create $ROOT_DIR/$MODULE-undocumented.txt: $!";
3788     
3789     foreach $symbol (sort (keys (%AllSymbols))) {
3790         # FIXME: should we print LogWarnings for undocumented stuff?
3791         # DEBUG
3792         #my $ssfile = &GetSymbolSourceFile($symbol);
3793         #my $ssline = &GetSymbolSourceLine($symbol);
3794         #my $location = "defined at " . (defined($ssfile)?$ssfile:"?") . ":" . (defined($ssline)?$ssline:"0") . "\n";
3795         # DEBUG
3796         if ($symbol !~ /:(Title|Long_Description|Short_Description|See_Also|Stability_Level|Include|Section_Id)/) {
3797             $total++;
3798             if (exists ($AllDocumentedSymbols{$symbol})) {
3799                 $n_documented++;
3800                 if (exists ($AllIncompleteSymbols{$symbol})) {
3801                     $n_incomplete++;
3802                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3803                     #$buffer .= "\t0: ".$location;
3804                 }
3805             } elsif (exists $Deprecated{$symbol}) {
3806                 if (exists ($AllIncompleteSymbols{$symbol})) {
3807                     $n_incomplete++;
3808                     $buffer_deprecated .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3809                     #$buffer .= "\t1a: ".$location;
3810                 } else {
3811                     $buffer_deprecated .= $symbol . "\n";
3812                     #$buffer .= "\t1b: ".$location;
3813                 }
3814             } else {
3815                 if (exists ($AllIncompleteSymbols{$symbol})) {
3816                     $n_incomplete++;
3817                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3818                     #$buffer .= "\t2a: ".$location;
3819                 } else {
3820                     $buffer .= $symbol . "\n";
3821                     #$buffer .= "\t2b: ".$location;
3822                 }
3823             }
3824         } elsif ($symbol =~ /:(Long_Description|Short_Description)/) {
3825             $total++;
3826             #my $len1=(exists($SymbolDocs{$symbol}))?length($SymbolDocs{$symbol}):-1;
3827             #my $len2=(exists($AllDocumentedSymbols{$symbol}))?length($AllDocumentedSymbols{$symbol}):-1;
3828             #print "%%%% $symbol : $len1,$len2\n";
3829             if (((exists ($SymbolDocs{$symbol})) && (length ($SymbolDocs{$symbol}) > 0))
3830             || ((exists ($AllDocumentedSymbols{$symbol})) && (length ($AllDocumentedSymbols{$symbol}) > 0))) {
3831               $n_documented++;
3832             } else {
3833               # cut off the leading namespace ($TMPL_DIR)
3834               $symbol =~ m/^.*\/(.*)$/;
3835               $buffer_descriptions .= $1 . "\n";
3836             }
3837         }
3838     }
3839     
3840     $buffer .= "\n" . $buffer_deprecated . "\n" . $buffer_descriptions;
3841     
3842     if ($total == 0) {
3843       $percent = 100;
3844     } else {
3845       $percent = ($n_documented / $total) * 100.0;
3846     }
3847     
3848     printf UNDOCUMENTED "%.0f%% symbol docs coverage.\n", $percent;
3849     print UNDOCUMENTED "$n_documented symbols documented.\n";
3850     print UNDOCUMENTED "$n_incomplete symbols incomplete.\n";
3851     print UNDOCUMENTED ($total - $n_documented) . " not documented.\n\n\n";
3852     
3853     print UNDOCUMENTED $buffer;
3854     
3855     close (UNDOCUMENTED);
3856     
3857     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);
3861 #############################################################################
3862 # Function    : OutputUndeclaredSymbols
3863 # Description : Outputs symbols that are undeclared yet documented to a file
3865 # Arguments   : none
3866 #############################################################################
3868 sub OutputUndeclaredSymbols {
3869     open(UNDECLARED, ">$ROOT_DIR/$MODULE-undeclared.txt")
3870         || die "Can't create $ROOT_DIR/$MODULE-undeclared.txt";
3872     if (%UndeclaredSymbols) {
3873         print UNDECLARED (join("\n", sort keys %UndeclaredSymbols));
3874         print UNDECLARED "\n";
3875         print "See $MODULE-undeclared.txt for the list of undeclared symbols.\n"
3876     }
3877     close(UNDECLARED);
3881 #############################################################################
3882 # Function    : OutputAllSymbols
3883 # Description : Outputs list of all symbols to a file
3885 # Arguments   : none
3886 #############################################################################
3888 sub OutputAllSymbols {
3889      my $n_documented = 0;
3890      my $total = 0;
3891      my $symbol;
3892      my $percent;
3893      my $msg;
3895      open (SYMBOLS, ">$ROOT_DIR/$MODULE-symbols.txt")
3896           || die "Can't create $ROOT_DIR/$MODULE-symbols.txt: $!";
3898      foreach $symbol (sort (keys (%AllSymbols))) {
3899           print SYMBOLS $symbol . "\n";
3900      }
3902      close (SYMBOLS);
3905 #############################################################################
3906 # Function    : OutputSymbolsWithoutSince
3907 # Description : Outputs list of all symbols without a since tag to a file
3909 # Arguments   : none
3910 #############################################################################
3912 sub OutputSymbolsWithoutSince {
3913      my $n_documented = 0;
3914      my $total = 0;
3915      my $symbol;
3916      my $percent;
3917      my $msg;
3919      open (SYMBOLS, ">$ROOT_DIR/$MODULE-nosince.txt")
3920           || die "Can't create $ROOT_DIR/$MODULE-nosince.txt: $!";
3922      foreach $symbol (sort (keys (%SourceSymbolDocs))) {
3923          if (!defined $Since{$symbol}) {
3924              print SYMBOLS $symbol . "\n";
3925          }
3926      }
3928      close (SYMBOLS);
3932 #############################################################################
3933 # Function    : MergeSourceDocumentation
3934 # Description : This merges documentation read from a source file into the
3935 #               documentation read in from a template file.
3937 #               Parameter descriptions override any in the template files.
3938 #               Function descriptions are placed before any description from
3939 #               the template files.
3941 # Arguments   : none
3942 #############################################################################
3944 sub MergeSourceDocumentation {
3945     my $symbol;
3946     my @Symbols;
3948     if (scalar %SymbolDocs) {
3949         @Symbols=keys (%SymbolDocs);
3950         #print "num existing entries: ".(scalar @Symbols)."\n";
3951         #print "  ",$Symbols[0], " ",$Symbols[1],"\n";
3952     }
3953     else {
3954         # filter scanned declarations, with what we suppress from -sections.txt
3955         my %tmp = ();
3956         foreach $symbol (keys (%Declarations)) {
3957             if (defined($KnownSymbols{$symbol}) && $KnownSymbols{$symbol} == 1) {
3958                 $tmp{$symbol}=1;
3959             }
3960         }
3961         # , add the rest from -sections.txt
3962         foreach $symbol (keys (%KnownSymbols)) {
3963             if ($KnownSymbols{$symbol} == 1) {
3964                 $tmp{$symbol}=1;
3965             }
3966         }
3967         # and add whats found in the source
3968         foreach $symbol (keys (%SourceSymbolDocs)) {
3969             $tmp{$symbol}=1;
3970         }
3971         @Symbols = keys (%tmp);
3972         #print "num source entries: ".(scalar @Symbols)."\n";
3973     }
3974     foreach $symbol (@Symbols) {
3975         $AllSymbols{$symbol} = 1;
3977         my $have_tmpl_docs = 0;
3979         ## See if the symbol is documented in template
3980         my $tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
3981         my $check_tmpl_doc =$tmpl_doc;
3982         # remove all xml-tags and whitespaces
3983         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
3984         $check_tmpl_doc =~ s/<.*?>//g;
3985         $check_tmpl_doc =~ s/\s//g;
3986         # anything left ?
3987         if ($check_tmpl_doc ne "") {
3988             $have_tmpl_docs = 1;
3989             #print "## [$check_tmpl_doc]\n";
3990         } else {
3991             $tmpl_doc = "";
3992         }
3994         if (exists ($SourceSymbolDocs{$symbol})) {
3995             my $type = $DeclarationTypes {$symbol};
3997             #print "merging [$symbol] from source\n";
3999             my $item = "Parameter";
4000             if (defined ($type)) {
4001                 if ($type eq 'STRUCT') {
4002                     $item = "Field";
4003                 } elsif ($type eq 'ENUM') {
4004                     $item = "Value";
4005                 } elsif ($type eq 'UNION') {
4006                     $item = "Field";
4007                 }
4008             } else {
4009                 $type="SIGNAL";
4010             }
4012             my $src_doc = $SourceSymbolDocs{$symbol};
4013             # remove leading and training whitespaces
4014             $src_doc =~ s/^\s+//;
4015             $src_doc =~ s/\s+$//;
4017             # Don't output warnings for overridden titles as titles are
4018             # automatically generated in the -sections.txt file, and thus they
4019             # are often overridden.
4020             if ($have_tmpl_docs && $symbol !~ m/:Title$/) {
4021                 # check if content is different
4022                 if ($tmpl_doc ne $src_doc) {
4023                     #print "[$tmpl_doc] [$src_doc]\n";
4024                     &LogWarning ($SourceSymbolSourceFile{$symbol}, $SourceSymbolSourceLine{$symbol},
4025                         "Documentation in template ".$SymbolSourceFile{$symbol}.":".$SymbolSourceLine{$symbol}." for $symbol being overridden by inline comments.");
4026                 }
4027             }
4029             if ($src_doc ne "") {
4030                  $AllDocumentedSymbols{$symbol} = 1;
4031             }
4033             # Convert <!--PARAMETERS--> with any blank lines around it to
4034             # a </para> followed by <!--PARAMETERS--> followed by <para>.
4035             $src_doc =~ s%\n+\s*<!--PARAMETERS-->\s*\n+%\n</para>\n<!--PARAMETERS-->\n<para>\n%g;
4037             # If there is a blank line, finish the paragraph and start another.
4038             $src_doc = &ConvertBlankLines ($src_doc, $symbol);
4039             # Do not add <para> to nothing, it breaks missing docs checks.
4040             my $src_doc_para = "";
4041             if ($src_doc) {
4042                 $src_doc_para = "<para>\n$src_doc</para>\n";
4043                 #print "$symbol : [$src_doc][$src_doc_para]\n";
4044             }
4046             if ($symbol =~ m/$TMPL_DIR\/.+:Long_Description/) {
4047                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4048             } elsif ($symbol =~ m/$TMPL_DIR\/.+:.+/) {
4049                 # For the title/summary/see also section docs we don't want to
4050                 # add any <para> tags.
4051                 $SymbolDocs{$symbol} = "$src_doc"
4052             } else {
4053                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4054             }
4056             # merge parameters
4057             if ($symbol =~ m/.*::.*/) {
4058                 # For signals we prefer the param names from the source docs,
4059                 # since the ones from the templates are likely to contain the
4060                 # artificial argn names which are generated by gtkdoc-scangobj.
4061                 $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4062                 # FIXME: we need to check for empty docs here as well!
4063             } else {
4064                 # The templates contain the definitive parameter names and order,
4065                 # so we will not change that. We only override the actual text.
4066                 my $tmpl_params = $SymbolParams{$symbol};
4067                 if (!defined ($tmpl_params)) {
4068                     #print "No merge needed for $symbol\n";
4069                     $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4070                     #  FIXME: we still like to get the number of params and merge
4071                     #  1) we would noticed that params have been removed/renamed
4072                     #  2) we would catch undocumented params
4073                     #  params are not (yet) exported in -decl.txt so that we
4074                     #  could easily grab them :/
4075                 } else {
4076                     my $params = $SourceSymbolParams{$symbol};
4077                     my $j;
4078                     #print "Merge needed for $symbol, tmpl_params: ",$#$tmpl_params,", source_params: ",$#$params," \n";
4079                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4080                         my $tmpl_param_name = $$tmpl_params[$j];
4082                         # Allow '...' as the Varargs parameter.
4083                         if ($tmpl_param_name eq "...") {
4084                             $tmpl_param_name = "Varargs";
4085                         }
4087                         # Try to find the param in the source comment documentation.
4088                         my $found = 0;
4089                         my $k;
4090                         #print "  try merge param $tmpl_param_name\n";
4091                         for ($k = 0; $k <= $#$params; $k += $PARAM_FIELD_COUNT) {
4092                             my $param_name = $$params[$k];
4093                             my $param_desc = $$params[$k + 1];
4095                             #print "    test param  $param_name\n";
4096                             # We accept changes in case, since the Gnome source
4097                             # docs contain a lot of these.
4098                             if ("\L$param_name" eq "\L$tmpl_param_name") {
4099                                 $found = 1;
4101                                 # Override the description.
4102                                 $$tmpl_params[$j + 1] = $param_desc;
4104                                 # Set the name to "" to mark it as used.
4105                                 $$params[$k] = "";
4106                                 last;
4107                             }
4108                         }
4110                         # If it looks like the parameters are there, but not
4111                         # in the right place, try to explain a bit better.
4112                         if ((!$found) && ($src_doc =~ m/\@$tmpl_param_name:/)) {
4113                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4114                                 "Parameters for $symbol must start on the line immediately after the function or macro name.");
4115                         }
4116                     }
4118                     # Now we output a warning if parameters have been described which
4119                     # do not exist.
4120                     for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
4121                         my $param_name = $$params[$j];
4122                         if ($param_name) {
4123                             # the template builder cannot detect if a macro returns
4124                             # a result or not
4125                             if(($type eq "MACRO") && ($param_name eq "Returns")) {
4126                                 # FIXME: do we need to add it then to tmpl_params[] ?
4127                                 my $num=$#$tmpl_params;
4128                                 #print "  adding Returns: to macro docs for $symbol.\n";
4129                                 $$tmpl_params[$num+1]="Returns";
4130                                 $$tmpl_params[$num+2]=$$params[$j+1];
4131                                 next;
4132                             }
4133                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4134                                 "$item described in source code comment block but does not exist. $type: $symbol $item: $param_name.");
4135                         }
4136                     }
4137                 }
4138             }
4139         } else {
4140             if ($have_tmpl_docs) {
4141                 $AllDocumentedSymbols{$symbol} = 1;
4142                 #print "merging [$symbol] from template\n";
4143             }
4144             else {
4145                 #print "[$symbol] undocumented\n";
4146             }
4147         }
4149         # if this symbol is documented, check if docs are complete
4150         $check_tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
4151         # remove all xml-tags and whitespaces
4152         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
4153         $check_tmpl_doc =~ s/<.*?>//g;
4154         $check_tmpl_doc =~ s/\s//g;
4155         if ($check_tmpl_doc ne "") {
4156             my $tmpl_params = $SymbolParams{$symbol};
4157             if (defined ($tmpl_params)) {
4158                 my $type = $DeclarationTypes {$symbol};
4160                 my $item = "Parameter";
4161                 if (defined ($type)) {
4162                     if ($type eq 'STRUCT') {
4163                         $item = "Field";
4164                     } elsif ($type eq 'ENUM') {
4165                         $item = "Value";
4166                     } elsif ($type eq 'UNION') {
4167                         $item = "Field";
4168                     }
4169                 } else {
4170                     $type="SIGNAL";
4171                 }
4173                 #print "Check param docs for $symbol, tmpl_params: ",$#$tmpl_params," entries, type=$type\n";
4175                 if ($#$tmpl_params > 0) {
4176                     my $j;
4177                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4178                         # Output a warning if the parameter is empty and
4179                         # remember for stats.
4180                         my $tmpl_param_name = $$tmpl_params[$j];
4181                         my $tmpl_param_desc = $$tmpl_params[$j + 1];
4182                         if ($tmpl_param_desc !~ m/\S/) {
4183                             if (exists ($AllIncompleteSymbols{$symbol})) {
4184                                 $AllIncompleteSymbols{$symbol}.=", ".$tmpl_param_name;
4185                             } else {
4186                                 $AllIncompleteSymbols{$symbol}=$tmpl_param_name;
4187                             }
4188                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4189                                 "$item description for $symbol"."::"."$tmpl_param_name is missing in source code comment block.");
4190                         }
4191                     }
4192                 }
4193                 else {
4194                     if ($#$tmpl_params == 0) {
4195                         $AllIncompleteSymbols{$symbol}="<items>";
4196                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4197                             "$item descriptions for $symbol are missing in source code comment block.");
4198                     }
4199                     # $#$tmpl_params==-1 means we don't know about parameters
4200                     # this unfortunately does not tell if there should be some
4201                 }
4202             }
4203         }
4204    }
4205    #print "num doc entries: ".(scalar %SymbolDocs)."\n";
4208 #############################################################################
4209 # Function    : IsEmptyDoc
4210 # Description : Check if a doc-string is empty. Its also regarded as empty if
4211 #               it only consist of whitespace or e.g. FIXME.
4212 # Arguments   : the doc-string
4213 #############################################################################
4214 sub IsEmptyDoc {
4215     my ($doc) = @_;
4216     
4217     if ($doc =~ /^\s*$/) {
4218         return 1;
4219     }
4221     if ($doc =~ /^\s*<para>\s*(FIXME)?\s*<\/para>\s*$/) {
4222         return 1;
4223     }
4225     return 0;
4229 # This converts blank lines to "</para><para>", but only outside CDATA and
4230 # <programlisting> tags.
4231 sub ConvertBlankLines {
4232     return &ModifyXMLElements ($_[0], $_[1],
4233                                "<!\\[CDATA\\[|<programlisting[^>]*>|\\|\\[",
4234                                \&ConvertBlankLinesEndTag,
4235                                \&ConvertBlankLinesCallback);
4239 sub ConvertBlankLinesEndTag {
4240   if ($_[0] eq "<!\[CDATA\[") {
4241     return "]]>";
4242   } elsif ($_[0] eq "|[") {
4243     return "]\\|";
4244   } else {
4245     return "</programlisting>";
4246   }
4249 sub ConvertBlankLinesCallback {
4250   my ($text, $symbol, $tag) = @_;
4252   # If we're not in CDATA or a <programlisting> we convert blank lines so
4253   # they start a new <para>.
4254   if ($tag eq "") {
4255     $text =~ s%\n{2,}%\n</para>\n<para>\n%g;
4256   }
4258   return $text;
4262 #############################################################################
4263 # LIBRARY FUNCTIONS -   These functions are used in both gtkdoc-mkdb and
4264 #                       gtkdoc-mktmpl and should eventually be moved to a
4265 #                       separate library.
4266 #############################################################################
4268 #############################################################################
4269 # Function    : ReadDeclarationsFile
4270 # Description : This reads in a file containing the function/macro/enum etc.
4271 #               declarations.
4273 #               Note that in some cases there are several declarations with
4274 #               the same name, e.g. for conditional macros. In this case we
4275 #               set a flag in the %DeclarationConditional hash so the
4276 #               declaration is not shown in the docs.
4278 #               If a macro and a function have the same name, e.g. for
4279 #               gtk_object_ref, the function declaration takes precedence.
4281 #               Some opaque structs are just declared with 'typedef struct
4282 #               _name name;' in which case the declaration may be empty.
4283 #               The structure may have been found later in the header, so
4284 #               that overrides the empty declaration.
4286 # Arguments   : $file - the declarations file to read
4287 #               $override - if declarations in this file should override
4288 #                       any current declaration.
4289 #############################################################################
4291 sub ReadDeclarationsFile {
4292     my ($file, $override) = @_;
4294     if ($override == 0) {
4295         %Declarations = ();
4296         %DeclarationTypes = ();
4297         %DeclarationConditional = ();
4298         %DeclarationOutput = ();
4299     }
4301     open (INPUT, $file)
4302         || die "Can't open $file: $!";
4303     my $declaration_type = "";
4304     my $declaration_name;
4305     my $declaration;
4306     my $is_deprecated = 0;
4307     while (<INPUT>) {
4308         if (!$declaration_type) {
4309             if (m/^<([^>]+)>/) {
4310                 $declaration_type = $1;
4311                 $declaration_name = "";
4312                 #print "Found declaration: $declaration_type\n";
4313                 $declaration = "";
4314             }
4315         } else {
4316             if (m%^<NAME>(.*)</NAME>%) {
4317                 $declaration_name = $1;
4318             } elsif (m%^<DEPRECATED/>%) {
4319                 $is_deprecated = 1;
4320             } elsif (m%^</$declaration_type>%) {
4321                 #print "Found end of declaration: $declaration_name\n";
4322                 # Check that the declaration has a name
4323                 if ($declaration_name eq "") {
4324                     print "ERROR: $declaration_type has no name $file:$.\n";
4325                 }
4327                 # If the declaration is an empty typedef struct _XXX XXX
4328                 # set the flag to indicate the struct has a typedef.
4329                 if ($declaration_type eq 'STRUCT'
4330                     && $declaration =~ m/^\s*$/) {
4331                     #print "Struct has typedef: $declaration_name\n";
4332                     $StructHasTypedef{$declaration_name} = 1;
4333                 }
4335                 # Check if the symbol is already defined.
4336                 if (defined ($Declarations{$declaration_name})
4337                     && $override == 0) {
4338                     # Function declarations take precedence.
4339                     if ($DeclarationTypes{$declaration_name} eq 'FUNCTION') {
4340                         # Ignore it.
4341                     } elsif ($declaration_type eq 'FUNCTION') {
4342                         if ($is_deprecated) {
4343                             $Deprecated{$declaration_name} = "";
4344                         }
4345                         $Declarations{$declaration_name} = $declaration;
4346                         $DeclarationTypes{$declaration_name} = $declaration_type;
4347                     } elsif ($DeclarationTypes{$declaration_name}
4348                               eq $declaration_type) {
4349                         # If the existing declaration is empty, or is just a
4350                         # forward declaration of a struct, override it.
4351                         if ($declaration_type eq 'STRUCT') {
4352                             if ($Declarations{$declaration_name} =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4353                                 if ($is_deprecated) {
4354                                     $Deprecated{$declaration_name} = "";
4355                                 }
4356                                 $Declarations{$declaration_name} = $declaration;
4357                             } elsif ($declaration =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4358                                 # Ignore an empty or forward declaration.
4359                             } else {
4360                                 &LogWarning ($file, $., "Structure $declaration_name has multiple definitions.");
4361                             }
4362                         } else {
4363                             # set flag in %DeclarationConditional hash for
4364                             # multiply defined macros/typedefs.
4365                             $DeclarationConditional{$declaration_name} = 1;
4366                         }
4367                     } else {
4368                         &LogWarning ($file, $., "$declaration_name has multiple definitions.");
4369                     }
4370                 } else {
4371                     if ($is_deprecated) {
4372                         $Deprecated{$declaration_name} = "";
4373                     }
4374                     $Declarations{$declaration_name} = $declaration;
4375                     $DeclarationTypes{$declaration_name} = $declaration_type;
4376                 }
4378                 $declaration_type = "";
4379                 $is_deprecated = 0;
4380             } else {
4381                 $declaration .= $_;
4382             }
4383         }
4384     }
4385     close (INPUT);
4389 #############################################################################
4390 # Function    : ReadSignalsFile
4391 # Description : This reads in an existing file which contains information on
4392 #               all GTK signals. It creates the arrays @SignalNames and
4393 #               @SignalPrototypes containing info on the signals. The first
4394 #               line of the SignalPrototype is the return type of the signal
4395 #               handler. The remaining lines are the parameters passed to it.
4396 #               The last parameter, "gpointer user_data" is always the same
4397 #               so is not included.
4398 # Arguments   : $file - the file containing the signal handler prototype
4399 #                       information.
4400 #############################################################################
4402 sub ReadSignalsFile {
4403     my ($file) = @_;
4405     my $in_signal = 0;
4406     my $signal_object;
4407     my $signal_name;
4408     my $signal_returns;
4409     my $signal_flags;
4410     my $signal_prototype;
4412     # Reset the signal info.
4413     @SignalObjects = ();
4414     @SignalNames = ();
4415     @SignalReturns = ();
4416     @SignalFlags = ();
4417     @SignalPrototypes = ();
4419     if (! -f $file) {
4420         return;
4421     }
4422     if (!open (INPUT, $file)) {
4423         warn "Can't open $file - skipping signals\n";
4424         return;
4425     }
4426     while (<INPUT>) {
4427         if (!$in_signal) {
4428             if (m/^<SIGNAL>/) {
4429                 $in_signal = 1;
4430                 $signal_object = "";
4431                 $signal_name = "";
4432                 $signal_returns = "";
4433                 $signal_prototype = "";
4434             }
4435         } else {
4436             if (m/^<NAME>(.*)<\/NAME>/) {
4437                 $signal_name = $1;
4438                 if ($signal_name =~ m/^(.*)::(.*)$/) {
4439                     $signal_object = $1;
4440                     ($signal_name = $2) =~ s/_/-/g;
4441                     #print "Found signal: $signal_name\n";
4442                 } else {
4443                     &LogWarning ($file, $., "Invalid signal name: $signal_name.");
4444                 }
4445             } elsif (m/^<RETURNS>(.*)<\/RETURNS>/) {
4446                 $signal_returns = $1;
4447             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4448                 $signal_flags = $1;
4449             } elsif (m%^</SIGNAL>%) {
4450                 #print "Found end of signal: ${signal_object}::${signal_name}\nReturns: ${signal_returns}\n${signal_prototype}";
4451                 push (@SignalObjects, $signal_object);
4452                 push (@SignalNames, $signal_name);
4453                 push (@SignalReturns, $signal_returns);
4454                 push (@SignalFlags, $signal_flags);
4455                 push (@SignalPrototypes, $signal_prototype);
4456                 $in_signal = 0;
4457             } else {
4458                 $signal_prototype .= $_;
4459             }
4460         }
4461     }
4462     close (INPUT);
4466 #############################################################################
4467 # Function    : ReadTemplateFile
4468 # Description : This reads in the manually-edited documentation file
4469 #               corresponding to the file currently being created, so we can
4470 #               insert the documentation at the appropriate places.
4471 #               It outputs %SymbolTypes, %SymbolDocs and %SymbolParams, which
4472 #               is a hash of arrays.
4473 #               NOTE: This function is duplicated in gtkdoc-mktmpl (but
4474 #               slightly different).
4475 # Arguments   : $docsfile - the template file to read in.
4476 #               $skip_unused_params - 1 if the unused parameters should be
4477 #                       skipped.
4478 #############################################################################
4480 sub ReadTemplateFile {
4481     my ($docsfile, $skip_unused_params) = @_;
4483     my $template = "$docsfile.sgml";
4484     if (! -f $template) {
4485         #print "File doesn't exist: $template\n";
4486         return 0;
4487     }
4488     #print "Reading $template\n";
4490     # start with empty hashes, we merge the source comment for each file
4491     # afterwards
4492     %SymbolDocs = ();
4493     %SymbolTypes = ();
4494     %SymbolParams = ();
4496     my $current_type = "";      # Type of symbol being read.
4497     my $current_symbol = "";    # Name of symbol being read.
4498     my $symbol_doc = "";                # Description of symbol being read.
4499     my @params;                 # Parameter names and descriptions of current
4500                                 #   function/macro/function typedef.
4501     my $current_param = -1;     # Index of parameter currently being read.
4502                                 #   Note that the param array contains pairs
4503                                 #   of param name & description.
4504     my $in_unused_params = 0;   # True if we are reading in the unused params.
4505     my $in_deprecated = 0;
4506     my $in_since = 0;
4507     my $in_stability = 0;
4509     open (DOCS, "$template")
4510         || die "Can't open $template: $!";
4511     while (<DOCS>) {
4512         if (m/^<!-- ##### ([A-Z_]+) (\S+) ##### -->/) {
4513             my $type = $1;
4514             my $symbol = $2;
4515             if ($symbol eq "Title"
4516                 || $symbol eq "Short_Description"
4517                 || $symbol eq "Long_Description"
4518                 || $symbol eq "See_Also"
4519                 || $symbol eq "Stability_Level"
4520                 || $symbol eq "Include") {
4522                 $symbol = $docsfile . ":" . $symbol;
4523             }
4525             #print "Found symbol: $symbol\n";
4526             # Remember file and line for the symbol
4527             $SymbolSourceFile{$symbol} = $template;
4528             $SymbolSourceLine{$symbol} = $.;
4530             # Store previous symbol, but remove any trailing blank lines.
4531             if ($current_symbol ne "") {
4532                 $symbol_doc =~ s/\s+$//;
4533                 $SymbolTypes{$current_symbol} = $current_type;
4534                 $SymbolDocs{$current_symbol} = $symbol_doc;
4536                 # Check that the stability level is valid.
4537                 if ($StabilityLevel{$current_symbol}) {
4538                     $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4539                 }
4541                 if ($current_param >= 0) {
4542                     $SymbolParams{$current_symbol} = [ @params ];
4543                 } else {
4544                     # Delete any existing params in case we are overriding a
4545                     # previously read template.
4546                     delete $SymbolParams{$current_symbol};
4547                 }
4548             }
4549             $current_type = $type;
4550             $current_symbol = $symbol;
4551             $current_param = -1;
4552             $in_unused_params = 0;
4553             $in_deprecated = 0;
4554             $in_since = 0;
4555             $in_stability = 0;
4556             $symbol_doc = "";
4557             @params = ();
4559         } elsif (m/^<!-- # Unused Parameters # -->/) {
4560             #print "DEBUG: Found unused parameters\n";
4561             $in_unused_params = 1;
4562             next;
4564         } elsif ($in_unused_params && $skip_unused_params) {
4565             # When outputting the DocBook we skip unused parameters.
4566             #print "DEBUG: Skipping unused param: $_";
4567             next;
4569         } else {
4570             # Check if param found. Need to handle "..." and "format...".
4571             if (s/^\@([\w\.]+):\040?//) {
4572                 my $param_name = $1;
4573                 my $param_desc = $_;
4574                 # Allow variations of 'Returns'
4575                 if ($param_name =~ m/^[Rr]eturns?$/) {
4576                     $param_name = "Returns";
4577                 }
4579                 # strip trailing whitespaces and blank lines
4580                 s/\s+\n$/\n/m;
4581                 s/\n+$/\n/sm;
4582                 #print "Found param for symbol $current_symbol : '$param_name'= '$_'";
4584                 if ($param_name eq "Deprecated") {
4585                     $in_deprecated = 1;
4586                     $Deprecated{$current_symbol} = $_;
4587                 } elsif ($param_name eq "Since") {
4588                     $in_since = 1;
4589                     chomp;
4590                     $Since{$current_symbol} = $_;
4591                 } elsif ($param_name eq "Stability") {
4592                     $in_stability = 1;
4593                     $StabilityLevel{$current_symbol} = $_;
4594                 } else {
4595                     push (@params, $param_name);
4596                     push (@params, $param_desc);
4597                     $current_param += $PARAM_FIELD_COUNT;
4598                 }
4599             } else {
4600                 # strip trailing whitespaces and blank lines
4601                 s/\s+\n$/\n/m;
4602                 s/\n+$/\n/sm;
4603                 
4604                 if (!m/^\s+$/) {
4605                     if ($in_deprecated) {
4606                         $Deprecated{$current_symbol} .= $_;
4607                     } elsif ($in_since) {
4608                         &LogWarning ($template, $., "multi-line since docs found");
4609                         #$Since{$current_symbol} .= $_;
4610                     } elsif ($in_stability) {
4611                         $StabilityLevel{$current_symbol} .= $_;
4612                     } elsif ($current_param >= 0) {
4613                         $params[$current_param] .= $_;
4614                     } else {
4615                         $symbol_doc .= $_;
4616                     }
4617                 }
4618             }
4619         }
4620     }
4622     # Remember to finish the current symbol doccs.
4623     if ($current_symbol ne "") {
4625         $symbol_doc =~ s/\s+$//;
4626         $SymbolTypes{$current_symbol} = $current_type;
4627         $SymbolDocs{$current_symbol} = $symbol_doc;
4629         # Check that the stability level is valid.
4630         if ($StabilityLevel{$current_symbol}) {
4631             $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4632         }
4634         if ($current_param >= 0) {
4635             $SymbolParams{$current_symbol} = [ @params ];
4636         } else {
4637             # Delete any existing params in case we are overriding a
4638             # previously read template.
4639             delete $SymbolParams{$current_symbol};
4640         }
4641     }
4643     close (DOCS);
4644     return 1;
4648 #############################################################################
4649 # Function    : ReadObjectHierarchy
4650 # Description : This reads in the $MODULE-hierarchy.txt file containing all
4651 #               the GtkObject subclasses described in this module (and their
4652 #               ancestors).
4653 #               It places them in the @Objects array, and places their level
4654 #               in the widget hierarchy in the @ObjectLevels array, at the
4655 #               same index. GtkObject, the root object, has a level of 1.
4657 #               FIXME: the version in gtkdoc-mkdb also generates tree_index.sgml
4658 #               as it goes along, this should be split out into a separate
4659 #               function.
4661 # Arguments   : none
4662 #############################################################################
4664 sub ReadObjectHierarchy {
4665     @Objects = ();
4666     @ObjectLevels = ();
4668     if (! -f $OBJECT_TREE_FILE) {
4669         return;
4670     }
4671     if (!open (INPUT, $OBJECT_TREE_FILE)) {
4672         warn "Can't open $OBJECT_TREE_FILE - skipping object tree\n";
4673         return;
4674     }
4676     # FIXME: use $OUTPUT_FORMAT
4677     # my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.$OUTPUT_FORMAT";
4678     my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.sgml";
4679     my $new_tree_index = "$SGML_OUTPUT_DIR/tree_index.new";
4681     open (OUTPUT, ">$new_tree_index")
4682         || die "Can't create $new_tree_index: $!";
4684     if (lc($OUTPUT_FORMAT) eq "xml") {
4685         my $tree_header = $doctype_header;
4687         $tree_header =~ s/<!DOCTYPE \w+/<!DOCTYPE screen/;
4688         print (OUTPUT "$tree_header");
4689     }
4690     print (OUTPUT "<screen>\n");
4692     # Only emit objects if they are supposed to be documented, or if
4693     # they have documented children. To implement this, we maintain a
4694     # stack of pending objects which will be emitted if a documented
4695     # child turns up.
4696     my @pending_objects = ();
4697     my @pending_levels = ();
4698     while (<INPUT>) {
4699         if (m/\S+/) {
4700             my $object = $&;
4701             my $level = (length($`)) / 2 + 1;
4702             my $xref = "";
4704             while (($#pending_levels >= 0) && ($pending_levels[$#pending_levels] >= $level)) {
4705                 my $pobject = pop(@pending_objects);
4706                 my $plevel = pop(@pending_levels);
4707             }
4709             push (@pending_objects, $object);
4710             push (@pending_levels, $level);
4712             if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4713                 while ($#pending_levels >= 0) {
4714                     $object = shift @pending_objects;
4715                     $level = shift @pending_levels;
4716                     $xref = &MakeXRef ($object);
4718                     print (OUTPUT ' ' x ($level * 4), "$xref\n");
4719                     push (@Objects, $object);
4720                     push (@ObjectLevels, $level);
4721                 }
4722             }
4723         }
4724     }
4725     print (OUTPUT "</screen>\n");
4727     close (INPUT);
4728     close (OUTPUT);
4730     &UpdateFileIfChanged ($old_tree_index, $new_tree_index, 0);
4732     &OutputObjectList;
4735 #############################################################################
4736 # Function    : ReadInterfaces
4737 # Description : This reads in the $MODULE.interfaces file.
4739 # Arguments   : none
4740 #############################################################################
4742 sub ReadInterfaces {
4743     %Interfaces = ();
4745     if (! -f $INTERFACES_FILE) {
4746         return;
4747     }
4748     if (!open (INPUT, $INTERFACES_FILE)) {
4749         warn "Can't open $INTERFACES_FILE - skipping interfaces\n";
4750         return;
4751     }
4753     while (<INPUT>) {
4754        chomp;
4755        my ($object, @ifaces) = split;
4756        if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4757            my @knownIfaces = ();
4759            # filter out private interfaces, but leave foreign interfaces
4760            foreach my $iface (@ifaces) {
4761                if (!exists($KnownSymbols{$iface}) || $KnownSymbols{$iface} == 1) {
4762                    push (@knownIfaces, $iface);
4763                }
4764              }
4766            $Interfaces{$object} = join(' ', @knownIfaces);
4767        }
4768     }
4769     close (INPUT);
4772 #############################################################################
4773 # Function    : ReadPrerequisites
4774 # Description : This reads in the $MODULE.prerequisites file.
4776 # Arguments   : none
4777 #############################################################################
4779 sub ReadPrerequisites {
4780     %Prerequisites = ();
4782     if (! -f $PREREQUISITES_FILE) {
4783         return;
4784     }
4785     if (!open (INPUT, $PREREQUISITES_FILE)) {
4786         warn "Can't open $PREREQUISITES_FILE - skipping prerequisites\n";
4787         return;
4788     }
4790     while (<INPUT>) {
4791        chomp;
4792        my ($iface, @prereqs) = split;
4793        if (exists($KnownSymbols{$iface}) && $KnownSymbols{$iface} == 1) {
4794            my @knownPrereqs = ();
4796            # filter out private prerequisites, but leave foreign prerequisites
4797            foreach my $prereq (@prereqs) {
4798                if (!exists($KnownSymbols{$prereq}) || $KnownSymbols{$prereq} == 1) {
4799                   push (@knownPrereqs, $prereq);
4800                }
4801            }
4803            $Prerequisites{$iface} = join(' ', @knownPrereqs);
4804        }
4805     }
4806     close (INPUT);
4809 #############################################################################
4810 # Function    : ReadArgsFile
4811 # Description : This reads in an existing file which contains information on
4812 #               all GTK args. It creates the arrays @ArgObjects, @ArgNames,
4813 #               @ArgTypes, @ArgFlags, @ArgNicks and @ArgBlurbs containing info
4814 #               on the args.
4815 # Arguments   : $file - the file containing the arg information.
4816 #############################################################################
4818 sub ReadArgsFile {
4819     my ($file) = @_;
4821     my $in_arg = 0;
4822     my $arg_object;
4823     my $arg_name;
4824     my $arg_type;
4825     my $arg_flags;
4826     my $arg_nick;
4827     my $arg_blurb;
4828     my $arg_default;
4829     my $arg_range;
4831     # Reset the args info.
4832     @ArgObjects = ();
4833     @ArgNames = ();
4834     @ArgTypes = ();
4835     @ArgFlags = ();
4836     @ArgNicks = ();
4837     @ArgBlurbs = ();
4838     @ArgDefaults = ();
4839     @ArgRanges = ();
4841     if (! -f $file) {
4842         return;
4843     }
4844     if (!open (INPUT, $file)) {
4845         warn "Can't open $file - skipping args\n";
4846         return;
4847     }
4848     while (<INPUT>) {
4849         if (!$in_arg) {
4850             if (m/^<ARG>/) {
4851                 $in_arg = 1;
4852                 $arg_object = "";
4853                 $arg_name = "";
4854                 $arg_type = "";
4855                 $arg_flags = "";
4856                 $arg_nick = "";
4857                 $arg_blurb = "";
4858                 $arg_default = "";
4859                 $arg_range = "";
4860             }
4861         } else {
4862             if (m/^<NAME>(.*)<\/NAME>/) {
4863                 $arg_name = $1;
4864                 if ($arg_name =~ m/^(.*)::(.*)$/) {
4865                     $arg_object = $1;
4866                     ($arg_name = $2) =~ s/_/-/g;
4867                     #print "Found arg: $arg_name\n";
4868                 } else {
4869                     &LogWarning ($file, $., "Invalid argument name: $arg_name");
4870                 }
4871             } elsif (m/^<TYPE>(.*)<\/TYPE>/) {
4872                 $arg_type = $1;
4873             } elsif (m/^<RANGE>(.*)<\/RANGE>/) {
4874                 $arg_range = $1;
4875             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4876                 $arg_flags = $1;
4877             } elsif (m/^<NICK>(.*)<\/NICK>/) {
4878                 $arg_nick = $1;
4879             } elsif (m/^<BLURB>(.*)<\/BLURB>/) {
4880                 $arg_blurb = $1;
4881                 if ($arg_blurb eq "(null)") {
4882                   $arg_blurb = "";
4883                   &LogWarning ($file, $., "Property ${arg_object}:${arg_name} has no documentation.");
4884                 }
4885             } elsif (m/^<DEFAULT>(.*)<\/DEFAULT>/) {
4886                 $arg_default = $1;
4887             } elsif (m%^</ARG>%) {
4888                 #print "Found end of arg: ${arg_object}::${arg_name}\n${arg_type} : ${arg_flags}\n";
4889                 push (@ArgObjects, $arg_object);
4890                 push (@ArgNames, $arg_name);
4891                 push (@ArgTypes, $arg_type);
4892                 push (@ArgRanges, $arg_range);
4893                 push (@ArgFlags, $arg_flags);
4894                 push (@ArgNicks, $arg_nick);
4895                 push (@ArgBlurbs, $arg_blurb);
4896                 push (@ArgDefaults, $arg_default);
4897                 $in_arg = 0;
4898             }
4899         }
4900     }
4901     close (INPUT);
4905 #############################################################################
4906 # Function    : CheckIsObject
4907 # Description : Returns 1 if the given name is a GtkObject or a subclass.
4908 #               It uses the global @Objects array.
4909 #               Note that the @Objects array only contains classes in the
4910 #               current module and their ancestors - not all GTK classes.
4911 # Arguments   : $name - the name to check.
4912 #############################################################################
4914 sub CheckIsObject {
4915     my ($name) = @_;
4917     my $object;
4918     foreach $object (@Objects) {
4919         if ($object eq $name) {
4920             return 1;
4921         }
4922     }
4923     return 0;
4927 #############################################################################
4928 # Function    : MakeReturnField
4929 # Description : Pads a string to $RETURN_TYPE_FIELD_WIDTH.
4930 # Arguments   : $str - the string to pad.
4931 #############################################################################
4933 sub MakeReturnField {
4934     my ($str) = @_;
4936     return $str . (' ' x ($RETURN_TYPE_FIELD_WIDTH - length ($str)));
4939 #############################################################################
4940 # Function    : GetSymbolSourceFile
4941 # Description : Get the filename where the symbol docs where taken from.
4942 # Arguments   : $symbol - the symbol name
4943 #############################################################################
4945 sub GetSymbolSourceFile {
4946     my ($symbol) = @_;
4948     if (defined($SourceSymbolSourceFile{$symbol})) {
4949         return $SourceSymbolSourceFile{$symbol};
4950     } elsif (defined($SymbolSourceFile{$symbol})) {
4951         return $SymbolSourceFile{$symbol};
4952     } else {
4953         return "";
4954     }
4957 #############################################################################
4958 # Function    : GetSymbolSourceLine
4959 # Description : Get the file line where the symbol docs where taken from.
4960 # Arguments   : $symbol - the symbol name
4961 #############################################################################
4963 sub GetSymbolSourceLine {
4964     my ($symbol) = @_;
4966     if (defined($SourceSymbolSourceLine{$symbol})) {
4967         return $SourceSymbolSourceLine{$symbol};
4968     } elsif (defined($SymbolSourceLine{$symbol})) {
4969         return $SymbolSourceLine{$symbol};
4970     } else {
4971         return 0;
4972     }