scanobj: fix signedness comparission warnings from gcc. Fixes #596730
[gtk-doc.git] / gtkdoc-mkdb.in
blobfea19c5ebb6815d51ad0da8a25107d1ee30495cc
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  Damon Chaplin
6 #               2007,2008  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.
27 #               NOTE: When creating SGML IDS, we append ":CAPS" to all
28 #               all-caps identifiers to prevent name clashes. (It basically
29 #               never is the case that mixed-case identifiers would collide.)
30 #               See the CreateValidSGMLID function.
31 #############################################################################
33 use strict;
34 use Getopt::Long;
36 unshift @INC, '@PACKAGE_DATA_DIR@';
37 require "gtkdoc-common.pl";
39 # Options
41 # name of documentation module
42 my $MODULE;
43 my $TMPL_DIR;
44 my $SGML_OUTPUT_DIR;
45 my @SOURCE_DIRS;
46 my $SOURCE_SUFFIXES = "";
47 my $IGNORE_FILES = "";
48 my $PRINT_VERSION;
49 my $PRINT_HELP;
50 my $MAIN_SGML_FILE;
51 my $EXPAND_CONTENT_FILES = "";
52 my $SGML_MODE;
53 my $DEFAULT_STABILITY;
54 my $DEFAULT_INCLUDES;
55 my $OUTPUT_FORMAT;
56 my $NAME_SPACE = "";
57 my $OUTPUT_ALL_SYMBOLS;
58 my $OUTPUT_SYMBOLS_WITHOUT_SINCE;
60 my %optctl = ('module' => \$MODULE,
61               'source-dir' => \@SOURCE_DIRS,
62               'source-suffixes' => \$SOURCE_SUFFIXES,
63               'ignore-files' => \$IGNORE_FILES,
64               'output-dir' => \$SGML_OUTPUT_DIR,
65               'tmpl-dir' => \$TMPL_DIR,
66               'version' => \$PRINT_VERSION,
67               'help' => \$PRINT_HELP,
68               'main-sgml-file' => \$MAIN_SGML_FILE,
69               'expand-content-files' => \$EXPAND_CONTENT_FILES,
70               'sgml-mode' => \$SGML_MODE,
71               'default-stability' => \$DEFAULT_STABILITY,
72               'default-includes' => \$DEFAULT_INCLUDES,
73               'output-format' => \$OUTPUT_FORMAT,
74               'name-space' => \$NAME_SPACE,
75               'outputallsymbols' => \$OUTPUT_ALL_SYMBOLS,
76               'outputsymbolswithoutsince' => \$OUTPUT_SYMBOLS_WITHOUT_SINCE
77               );
78 GetOptions(\%optctl, "module=s", "source-dir:s", "source-suffixes:s", 
79     "ignore-files:s", "output-dir:s", "tmpl-dir:s", "version", "outputallsymbols",
80     "outputsymbolswithoutsince",
81     "expand-content-files:s", "main-sgml-file:s", "extra-db-files:s", "help",
82     "sgml-mode", "default-stability:s", "default-includes:s", "output-format:s",
83     "name-space:s");
85 if ($PRINT_VERSION) {
86     print "@VERSION@\n";
87     exit 0;
90 if (!$MODULE) {
91     $PRINT_HELP = 1;
94 if ($DEFAULT_STABILITY && $DEFAULT_STABILITY ne "Stable"
95     && $DEFAULT_STABILITY ne "Private" && $DEFAULT_STABILITY ne "Unstable") {
96     $PRINT_HELP = 1;
99 if ($PRINT_HELP) {
100     print <<EOF;
101 gtkdoc-mkdb version @VERSION@ - generate docbook files
103 --module=MODULE_NAME       Name of the doc module being parsed
104 --source-dir=DIRNAME       Directories which contain inline reference material
105 --source-suffixes=SUFFIXES Suffixes of source files to scan, comma-separated
106 --ignore-files=FILES       Files or directories which should not be scanned
107                            May be used more than once for multiple directories
108 --output-dir=DIRNAME       Directory to put the generated DocBook files in
109 --tmpl-dir=DIRNAME         Directory in which template files may be found
110 --main-sgml-file=FILE      File containing the toplevel DocBook file.
111 --expand-content-files=FILES Extra DocBook files to expand abbreviations in.
112 --output-format=FORMAT     Format to use for the generated docbook, XML or SGML.
113 --sgml-mode                Allow DocBook markup in inline documentation.
114 --default-stability=LEVEL  Specify default stability Level. Valid values are
115                            Stable, Unstable, or Private.
116 --default-includes=FILENAMES Specify default includes for section Synopsis
117 --name-space=NS            Omit namespace in index.
118 --version                  Print the version of this program
119 --help                     Print this help
121     exit 0;
124 my ($empty_element_end, $doctype_header);
126 if (lc($OUTPUT_FORMAT) eq "xml") {
127     if (!$MAIN_SGML_FILE) {
128         # backwards compatibility
129         if (-e "${MODULE}-docs.sgml") {
130             $MAIN_SGML_FILE="${MODULE}-docs.sgml";
131         } else {
132             $MAIN_SGML_FILE="${MODULE}-docs.xml";
133         }
134     }
135     $OUTPUT_FORMAT = "xml";
136     $empty_element_end = "/>";
138     if (-e $MAIN_SGML_FILE) {
139         open(INPUT, "<$MAIN_SGML_FILE") || die "Can't open $MAIN_SGML_FILE";
140         $doctype_header = "";
141         while (<INPUT>) {
142             if (/^\s*<(book|chapter|article)/) {
143                 if ($_ !~ m/http:\/\/www.w3.org\/200[13]\/XInclude/) {
144                     $doctype_header = "";
145                 }
146                 last;
147             }
148             $doctype_header .= $_;
149         }
150         close(INPUT);
151     } else {
152         $doctype_header =
153 "<?xml version=\"1.0\"?>\n" .
154 "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.3//EN\"\n" .
155 "               \"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd\">\n";
156     }
157     $doctype_header =~ s/<!DOCTYPE \w+/<!DOCTYPE refentry/;
158 } else {
159     if (!$MAIN_SGML_FILE) {
160         $MAIN_SGML_FILE="${MODULE}-docs.sgml";
161     }
162     $OUTPUT_FORMAT = "sgml";
163     $doctype_header = "";
164     $empty_element_end = ">";
167 my $ROOT_DIR = ".";
169 # All the files are written in subdirectories beneath here.
170 $TMPL_DIR = $TMPL_DIR ? $TMPL_DIR : "$ROOT_DIR/tmpl";
172 # This is where we put all the DocBook output.
173 $SGML_OUTPUT_DIR = $SGML_OUTPUT_DIR ? $SGML_OUTPUT_DIR : "$ROOT_DIR/$OUTPUT_FORMAT";
175 # This file contains the object hierarchy.
176 my $OBJECT_TREE_FILE = "$ROOT_DIR/$MODULE.hierarchy";
178 # This file contains the interfaces.
179 my $INTERFACES_FILE = "$ROOT_DIR/$MODULE.interfaces";
181 # This file contains the prerequisites.
182 my $PREREQUISITES_FILE = "$ROOT_DIR/$MODULE.prerequisites";
184 # This file contains signal arguments and names.
185 my $SIGNALS_FILE = "$ROOT_DIR/$MODULE.signals";
187 # The file containing Arg information.
188 my $ARGS_FILE = "$ROOT_DIR/$MODULE.args";
190 # These global arrays store information on signals. Each signal has an entry
191 # in each of these arrays at the same index, like a multi-dimensional array.
192 my @SignalObjects;      # The GtkObject which emits the signal.
193 my @SignalNames;        # The signal name.
194 my @SignalReturns;      # The return type.
195 my @SignalFlags;        # Flags for the signal
196 my @SignalPrototypes;   # The rest of the prototype of the signal handler.
198 # These global arrays store information on Args. Each Arg has an entry
199 # in each of these arrays at the same index, like a multi-dimensional array.
200 my @ArgObjects;         # The GtkObject which has the Arg.
201 my @ArgNames;           # The Arg name.
202 my @ArgTypes;           # The Arg type - gint, GtkArrowType etc.
203 my @ArgFlags;           # How the Arg can be used - readable/writable etc.
204 my @ArgNicks;           # The nickname of the Arg.
205 my @ArgBlurbs;          # Docstring of the Arg.
206 my @ArgDefaults;        # Default value of the Arg.
207 my @ArgRanges;          # The range of the Arg type
208 # These global hashes store declaration info keyed on a symbol name.
209 my %Declarations;
210 my %DeclarationTypes;
211 my %DeclarationConditional;
212 my %DeclarationOutput;
213 my %Deprecated;
214 my %Since;
215 my %StabilityLevel;
216 my %StructHasTypedef;
218 # These global hashes store the existing documentation.
219 my %SymbolDocs;
220 my %SymbolTypes;
221 my %SymbolParams;
222 my %SymbolSourceFile;
223 my %SymbolSourceLine;
225 # These global hashes store documentation scanned from the source files.
226 my %SourceSymbolDocs;
227 my %SourceSymbolParams;
228 my %SourceSymbolSourceFile;
229 my %SourceSymbolSourceLine;
231 # all documentation goes in here, so we can do coverage analysis
232 my %AllSymbols;
233 my %AllIncompleteSymbols;
234 my %AllDocumentedSymbols;
236 # Undeclared yet documented symbols
237 my %UndeclaredSymbols;
239 # These global arrays store GObject, subclasses and the hierarchy.
240 my @Objects;
241 my @ObjectLevels;
243 my %Interfaces;
244 my %Prerequisites;
246 # holds the symbols which are mentioned in $MODULE-sections.txt
247 my %KnownSymbols;
249 # collects index entries
250 my %IndexEntriesFull;
251 my %IndexEntriesSince;
252 my %IndexEntriesDeprecated;
254 # Standard C preprocessor directives, which we ignore for '#' abbreviations.
255 my %PreProcessorDirectives;
256 $PreProcessorDirectives{"assert"} = 1;
257 $PreProcessorDirectives{"define"} = 1;
258 $PreProcessorDirectives{"elif"} = 1;
259 $PreProcessorDirectives{"else"} = 1;
260 $PreProcessorDirectives{"endif"} = 1;
261 $PreProcessorDirectives{"error"} = 1;
262 $PreProcessorDirectives{"if"} = 1;
263 $PreProcessorDirectives{"ifdef"} = 1;
264 $PreProcessorDirectives{"ifndef"} = 1;
265 $PreProcessorDirectives{"include"} = 1;
266 $PreProcessorDirectives{"line"} = 1;
267 $PreProcessorDirectives{"pragma"} = 1;
268 $PreProcessorDirectives{"unassert"} = 1;
269 $PreProcessorDirectives{"undef"} = 1;
270 $PreProcessorDirectives{"warning"} = 1;
272 # remember used annotation (to write minimal glossary)
273 my %AnnotationsUsed;
275 my %AnnotationDefinition = (
276     'allow-none' => "NULL is ok, both for passing and for returning.",
277     'array' => "Parameter points to an array of items.",
278     'default' => "Default parameter value (for in case the <acronym>shadows</acronym>-to function has less parameters).",
279     'element-type' => "Generics and defining elements of containers and arrays.",
280     'error-domains' => "Typed errors. Similar to throws in Java.",
281     'in' => "Parameter for input. Default is <acronym>transfer none</acronym>.",
282     'inout' => "Parameter for input and for returning results. Default is <acronym>transfer full</acronym>.",
283     'in-out' => "Parameter for input and for returning results. Default is <acronym>transfer full</acronym>.",
284     'not-error' => "A GError parameter is not to be handled like a normal GError.",
285     'out' => "Parameter for returning results. Default is <acronym>transfer full</acronym>.",
286     'transfer container' => "Free data container after the code is done.",
287     'transfer full' => "Free data after the code is done.",
288     'transfer none' => "Don't free data after the code is done."
291 # Create the root DocBook output directory if it doens't exist.
292 if (! -e $SGML_OUTPUT_DIR) {
293     mkdir ("$SGML_OUTPUT_DIR", 0777)
294         || die "Can't create directory: $SGML_OUTPUT_DIR";
297 # Function and other declaration output settings.
298 my $RETURN_TYPE_FIELD_WIDTH = 20;
299 my $SYMBOL_FIELD_WIDTH = 36;
300 my $SIGNAL_FIELD_WIDTH = 16;
301 my $PARAM_FIELD_COUNT = 2;
303 &ReadKnownSymbols ("$ROOT_DIR/$MODULE-sections.txt");
304 &ReadSignalsFile ($SIGNALS_FILE);
305 &ReadArgsFile ($ARGS_FILE);
306 &ReadObjectHierarchy;
307 &ReadInterfaces;
308 &ReadPrerequisites;
310 &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-decl.txt", 0);
311 if (-f "$ROOT_DIR/$MODULE-overrides.txt") {
312     &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-overrides.txt", 1);
315 for my $dir (@SOURCE_DIRS) {
316     &ReadSourceDocumentation ($dir);
319 my $changed = &OutputSGML ("$ROOT_DIR/$MODULE-sections.txt");
321 # If any of the DocBook SGML files have changed, update the timestamp file (so
322 # it can be used for Makefile dependencies).
323 if ($changed || ! -e "$ROOT_DIR/sgml.stamp") {
325     # try to detect the common prefix
326     # GtkWidget, GTK_WIDGET, gtk_widget -> gtk
327     if ($NAME_SPACE eq "") {
328         $NAME_SPACE="";
329         my $pos=0;
330         my $ratio=0.0;
331         do {
332             my %prefix;
333             my $letter="";
334             foreach my $symbol (keys(%IndexEntriesFull)) {
335                 if(($NAME_SPACE eq "") || $symbol =~ /^$NAME_SPACE/i) {
336                     if (length($symbol)>$pos) {
337                         $letter=substr($symbol,$pos,1);
338                         # stop prefix scanning
339                         if ($letter eq "_") {
340                             # stop on "_"
341                             last;
342                         }
343                         # Should we also stop on a uppercase char, if last was lowercase
344                         #   GtkWidget, if we have the 'W' and had the 't' before
345                         # or should we count upper and lowercase, and stop one 2nd uppercase, if we already had a lowercase
346                         #   GtkWidget, the 'W' would be the 2nd uppercase and with 't','k' we had lowercase chars before
347                         # need to recound each time as this is per symbol
348                         $prefix{uc($letter)}++;
349                     }
350                 }
351             }
352             if ($letter ne "" && $letter ne "_") {
353                 my $maxletter="";
354                 my $maxsymbols=0;
355                 foreach $letter (keys(%prefix)) {
356                     #print "$letter: $prefix{$letter}.\n";
357                     if ($prefix{$letter}>$maxsymbols) {
358                         $maxletter=$letter;
359                         $maxsymbols=$prefix{$letter};
360                     }
361                 }
362                 $ratio = scalar(keys(%IndexEntriesFull)) / $prefix{$maxletter};
363                 #print "most symbols start with $maxletter, that is ". (100 * $ratio) ." %\n";
364                 if ($ratio > 0.9) {
365                     # do another round
366                     $NAME_SPACE .= $maxletter;
367                 }
368                 $pos++;
369             }
370             else {
371                 $ratio=0.0;
372             }
373         } while ($ratio > 0.9);
374         #print "most symbols start with $NAME_SPACE\n";
375     }
377     &OutputIndexFull;
378     &OutputDeprecatedIndex;
379     &OutputSinceIndexes;
380     &OutputAnnotationGlossary;
382     open (TIMESTAMP, ">$ROOT_DIR/sgml.stamp")
383         || die "Can't create $ROOT_DIR/sgml.stamp: $!";
384     print (TIMESTAMP "timestamp");
385     close (TIMESTAMP);
388 #############################################################################
389 # Function    : OutputObjectList
390 # Description : This outputs the alphabetical list of objects, in a columned
391 #               table.
392 #               FIXME: Currently this also outputs ancestor objects
393 #               which may not actually be in this module.
394 # Arguments   : none
395 #############################################################################
397 sub OutputObjectList {
398     my $cols = 3;
399     
400     # FIXME: use $OUTPUT_FORMAT
401     # my $old_object_index = "$SGML_OUTPUT_DIR/object_index.$OUTPUT_FORMAT";
402     my $old_object_index = "$SGML_OUTPUT_DIR/object_index.sgml";
403     my $new_object_index = "$SGML_OUTPUT_DIR/object_index.new";
405     open (OUTPUT, ">$new_object_index")
406         || die "Can't create $new_object_index: $!";
408     if (lc($OUTPUT_FORMAT) eq "xml") {
409         my $header = $doctype_header;
411         $header =~ s/<!DOCTYPE \w+/<!DOCTYPE informaltable/;
412         print (OUTPUT "$header");
413     }
415     print (OUTPUT <<EOF);
416 <informaltable pgwide="1" frame="none">
417 <tgroup cols="$cols">
418 <colspec colwidth="1*"${empty_element_end}
419 <colspec colwidth="1*"${empty_element_end}
420 <colspec colwidth="1*"${empty_element_end}
421 <tbody>
424     my $count = 0;
425     my $object;
426     foreach $object (sort (@Objects)) {
427         my $xref = &MakeXRef ($object);
428         if ($count % $cols == 0) { print (OUTPUT "<row>\n"); }
429         print (OUTPUT "<entry>$xref</entry>\n");
430         if ($count % $cols == ($cols - 1)) { print (OUTPUT "</row>\n"); }
431         $count++;
432     }
433     if ($count == 0) {
434         # emit an empty row, since empty tables are invalid
435         print (OUTPUT "<row><entry> </entry></row>\n");
436     }
437     else {
438         print (OUTPUT "</row>\n");
439     }
441     print (OUTPUT <<EOF);
442 </tbody></tgroup></informaltable>
444     close (OUTPUT);
446     &UpdateFileIfChanged ($old_object_index, $new_object_index, 0);
450 #############################################################################
451 # Function    : OutputSGML
452 # Description : This collects the output for each section of the docs, and
453 #               outputs each file when the end of the section is found.
454 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
455 #               the functions/macros/structs etc. being documented, organised
456 #               into sections and subsections.
457 #############################################################################
459 sub OutputSGML {
460     my ($file) = @_;
462     #print "Reading: $file\n";
463     open (INPUT, $file)
464         || die "Can't open $file: $!";
465     my $filename = "";
466     my $book_top = "";
467     my $book_bottom = "";
468     my $includes = (defined $DEFAULT_INCLUDES) ? $DEFAULT_INCLUDES : "";
469     my $section_includes = "";
470     my $in_section = 0;
471     my $title = "";
472     my $subsection = "";
473     my $synopsis;
474     my $details;
475     my $num_symbols;
476     my $changed = 0;
477     my $signals_synop = "";
478     my $signals_desc = "";
479     my $args_synop = "";
480     my $child_args_synop = "";
481     my $style_args_synop = "";
482     my $args_desc = "";
483     my $child_args_desc = "";
484     my $style_args_desc = "";
485     my $hierarchy = "";
486     my $interfaces = "";
487     my $implementations = "";
488     my $prerequisites = "";
489     my $derived = "";
490     my @file_objects = ();
491     my %templates = ();
492     my %symbols = ();
494     # merge the source docs, in case there are no templates
495     &MergeSourceDocumentation;
497     while (<INPUT>) {
498         if (m/^#/) {
499             next;
501         } elsif (m/^<SECTION>/) {
502             $synopsis = "";
503             $details = "";
504             $num_symbols = 0;
505             $in_section = 1;
506             @file_objects = ();
507             %symbols = ();
509         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
510             $synopsis .= "\n";
511             $subsection = $1;
513         } elsif (m/^<SUBSECTION>/) {
515         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
516             $title = $1;
517             #print "Section: $title\n";
519             # We don't want warnings if object & class structs aren't used.
520             $DeclarationOutput{$title} = 1;
521             $DeclarationOutput{"${title}Class"} = 1;
522             $DeclarationOutput{"${title}Iface"} = 1;
523             $DeclarationOutput{"${title}Interface"} = 1;
525         } elsif (m/^<FILE>(.*)<\/FILE>/) {
526             $filename = $1;
527             if (! defined $templates{$filename}) {
528                if (&ReadTemplateFile ("$TMPL_DIR/$filename", 1)) {
529                    &MergeSourceDocumentation;
530                    $templates{$filename}=$.;
531                }
532             } else {
533                 &LogWarning ($file, $., "Double <FILE>$filename</FILE> entry. ".
534                     "Previous occurrence on line ".$templates{$filename}.".");
535             }
537         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
538             if ($in_section) {
539                 $section_includes = $1;
540             } else {
541                 if (defined $DEFAULT_INCLUDES) {
542                     &LogWarning ($file, $., "Default <INCLUDE> being overridden by command line option.");
543                 }
544                 else {
545                     $includes = $1;
546                 }
547             }
549         } elsif (m/^<\/SECTION>/) {
550             if($title eq "" && $filename eq "") {
551                 &LogWarning ($file, $., "Section has no title and no file.");
552             }
553             # FIXME: one of those would be enough
554             if ($title eq "") {
555                 $title = $filename;
556             }
557             if ($filename eq "") {
558                 $filename = $title;
559             }
560             #print "End of section: $title\n";
562             $filename =~ s/\s/_/g;
564             my $section_id = $SourceSymbolDocs{"$TMPL_DIR/$filename:Section_Id"};
565             if (defined ($section_id) && $section_id !~ m/^\s*$/) {
566                 # Do nothing. Use section_id as it is.
567             } elsif (&CheckIsObject ($title)) {
568                 # GtkObjects use their class name as the ID.
569                 $section_id = &CreateValidSGMLID ($title);
570             } else {
571                 $section_id = &CreateValidSGMLID ("$MODULE-$title");
572             }
574             if ($num_symbols > 0) {
575                 # collect documents
576                 if (lc($OUTPUT_FORMAT) eq "xml") {
577                     $book_bottom .= "    <xi:include href=\"xml/$filename.xml\"/>\n";
578                 } else {
579                     $book_top.="<!ENTITY $section_id SYSTEM \"sgml/$filename.sgml\">\n";
580                     $book_bottom .= "    &$section_id;\n";
581                 }
583                 if (defined ($SourceSymbolDocs{"$TMPL_DIR/$filename:Include"})) {
584                     if ($section_includes) {
585                         &LogWarning ($file, $., "Section <INCLUDE> being overridden by inline comments.");
586                     }
587                     $section_includes = $SourceSymbolDocs{"$TMPL_DIR/$filename:Include"};
588                 }
589                 if ($section_includes eq "") {
590                     $section_includes = $includes;
591                 }
593                  $signals_synop =~ s/^\n*//g;
594                  $signals_synop =~ s/\n+$/\n/g;
595                 if ($signals_synop ne '') {
596                     $signals_synop = <<EOF;
597 <refsect1 id="$section_id.signals" role="signal_proto">
598 <title role="signal_proto.title">Signals</title>
599 <synopsis>
600 ${signals_synop}</synopsis>
601 </refsect1>
603                      $signals_desc =~ s/^\n*//g;
604                      $signals_desc =~ s/\n+$/\n/g;
605                      $signals_desc =~ s/(\s|\n)+$//ms;
606                     $signals_desc  = <<EOF;
607 <refsect1 id="$section_id.signal-details" role="signals">
608 <title role="signals.title">Signal Details</title>
609 $signals_desc
610 </refsect1>
612                 }
614                  $args_synop =~ s/^\n*//g;
615                  $args_synop =~ s/\n+$/\n/g;
616                 if ($args_synop ne '') {
617                     $args_synop = <<EOF;
618 <refsect1 id="$section_id.properties" role="properties">
619 <title role="properties.title">Properties</title>
620 <synopsis>
621 ${args_synop}</synopsis>
622 </refsect1>
624                      $args_desc =~ s/^\n*//g;
625                      $args_desc =~ s/\n+$/\n/g;
626                      $args_desc =~ s/(\s|\n)+$//ms;
627                     $args_desc  = <<EOF;
628 <refsect1 id="$section_id.property-details" role="property_details">
629 <title role="property_details.title">Property Details</title>
630 $args_desc
631 </refsect1>
633                 }
635                  $child_args_synop =~ s/^\n*//g;
636                  $child_args_synop =~ s/\n+$/\n/g;
637                 if ($child_args_synop ne '') {
638                     $args_synop .= <<EOF;
639 <refsect1 id="$section_id.child-properties" role="child_properties">
640 <title role="child_properties.title">Child Properties</title>
641 <synopsis>
642 ${child_args_synop}</synopsis>
643 </refsect1>
645                      $child_args_desc =~ s/^\n*//g;
646                      $child_args_desc =~ s/\n+$/\n/g;
647                      $child_args_desc =~ s/(\s|\n)+$//ms;
648                     $args_desc .= <<EOF;
649 <refsect1 id="$section_id.child-property-details" role="child_property_details">
650 <title role="child_property_details.title">Child Property Details</title>
651 $child_args_desc
652 </refsect1>
654                 }
656                  $style_args_synop =~ s/^\n*//g;
657                  $style_args_synop =~ s/\n+$/\n/g;
658                 if ($style_args_synop ne '') {
659                     $args_synop .= <<EOF;
660 <refsect1 id="$section_id.style-properties" role="style_properties">
661 <title role="style_properties.title">Style Properties</title>
662 <synopsis>
663 ${style_args_synop}</synopsis>
664 </refsect1>
666                      $style_args_desc =~ s/^\n*//g;
667                      $style_args_desc =~ s/\n+$/\n/g;
668                      $style_args_desc =~ s/(\s|\n)+$//ms;
669                     $args_desc .= <<EOF;
670 <refsect1 id="$section_id.style-property-details" role="style_properties_details">
671 <title role="style_properties_details.title">Style Property Details</title>
672 $style_args_desc
673 </refsect1>
675                 }
677                  $hierarchy =~ s/^\n*//g;
678                  $hierarchy =~ s/\n+$/\n/g;
679                  $hierarchy =~ s/(\s|\n)+$//ms;
680                 if ($hierarchy ne "") {
681                     $hierarchy = <<EOF;
682 <refsect1 id="$section_id.object-hierarchy" role="object_hierarchy">
683 <title role="object_hierarchy.title">Object Hierarchy</title>
684 $hierarchy
685 </refsect1>
687                 }
689                  $interfaces =~ s/^\n*//g;
690                  $interfaces =~ s/\n+$/\n/g;
691                  $interfaces =~ s/(\s|\n)+$//ms;
692                 if ($interfaces ne "") {
693                     $interfaces = <<EOF;
694 <refsect1 id="$section_id.implemented-interfaces" role="impl_interfaces">
695 <title role="impl_interfaces.title">Implemented Interfaces</title>
696 $interfaces
697 </refsect1>
699                 }
701                  $implementations =~ s/^\n*//g;
702                  $implementations =~ s/\n+$/\n/g;
703                  $implementations =~ s/(\s|\n)+$//ms;
704                 if ($implementations ne "") {
705                     $implementations = <<EOF;
706 <refsect1 id="$section_id.implementations" role="implementations">
707 <title role="implementations.title">Known Implementations</title>
708 $implementations
709 </refsect1>
711                 }
713                  $prerequisites =~ s/^\n*//g;
714                  $prerequisites =~ s/\n+$/\n/g;
715                  $prerequisites =~ s/(\s|\n)+$//ms;
716                 if ($prerequisites ne "") {
717                     $prerequisites = <<EOF;
718 <refsect1 id="$section_id.prerequisites" role="prerequisites">
719 <title role="prerequisites.title">Prerequisites</title>
720 $prerequisites
721 </refsect1>
723                 }
725                  $derived =~ s/^\n*//g;
726                  $derived =~ s/\n+$/\n/g;
727                  $derived =~ s/(\s|\n)+$//ms;
728                 if ($derived ne "") {
729                     $derived = <<EOF;
730 <refsect1 id="$section_id.derived-interfaces" role="derived_interfaces">
731 <title role="derived_interfaces.title">Known Derived Interfaces</title>
732 $derived
733 </refsect1>
735                 }
737                 $synopsis =~ s/^\n*//g;
738                 $synopsis =~ s/\n+$/\n/g;
739                 my $file_changed = &OutputSGMLFile ($filename, $title, $section_id,
740                                                     $section_includes,
741                                                     \$synopsis, \$details,
742                                                     \$signals_synop, \$signals_desc,
743                                                     \$args_synop, \$args_desc,
744                                                     \$hierarchy, \$interfaces,
745                                                     \$implementations,
746                                                     \$prerequisites, \$derived,
747                                                     \@file_objects);
748                 if ($file_changed) {
749                     $changed = 1;
750                 }
751             }
752             $title = "";
753             $subsection = "";
754             $in_section = 0;
755             $section_includes = "";
756             $signals_synop = "";
757             $signals_desc = "";
758             $args_synop = "";
759             $child_args_synop = "";
760             $style_args_synop = "";
761             $args_desc = "";
762             $child_args_desc = "";
763             $style_args_desc = "";
764             $hierarchy = "";
765             $interfaces = "";
766             $implementations = "";
767             $prerequisites = "";
768             $derived = "";
770         } elsif (m/^(\S+)/) {
771             my $symbol = $1;
772             #print "  Symbol: $symbol\n";
774             # FIXME: check for duplicate entries
775             if (! defined $symbols{$symbol}) {
776                 my $declaration = $Declarations{$symbol};
777                 if (defined ($declaration)) {
778                     # We don't want standard macros/functions of GtkObjects,
779                     # or private declarations.
780                     if ($subsection ne "Standard" && $subsection ne "Private") {
781                         if (&CheckIsObject ($symbol)) {
782                             push @file_objects, $symbol;
783                         }
784                         my ($synop, $desc) = &OutputDeclaration ($symbol,
785                                                                  $declaration);
786                         my ($sig_synop, $sig_desc) = &GetSignals ($symbol);
787                         my ($arg_synop, $child_arg_synop, $style_arg_synop,
788                             $arg_desc, $child_arg_desc, $style_arg_desc) = &GetArgs ($symbol);
789                         my $hier = &GetHierarchy ($symbol);
790                         my $ifaces = &GetInterfaces ($symbol);
791                         my $impls = &GetImplementations ($symbol);
792                         my $prereqs = &GetPrerequisites ($symbol);
793                         my $der = &GetDerived ($symbol);
794                         $synopsis .= $synop;
795                         $details .= $desc;
796                         $signals_synop .= $sig_synop;
797                         $signals_desc .= $sig_desc;
798                         $args_synop .= $arg_synop;
799                         $child_args_synop .= $child_arg_synop;
800                         $style_args_synop .= $style_arg_synop;
801                         $args_desc .= $arg_desc;
802                         $child_args_desc .= $child_arg_desc;
803                         $style_args_desc .= $style_arg_desc;
804                         $hierarchy .= $hier;
805                         $interfaces .= $ifaces;
806                         $implementations .= $impls;
807                         $prerequisites .= $prereqs;
808                         $derived .= $der;
809                     }
810     
811                     # Note that the declaration has been output.
812                     $DeclarationOutput{$symbol} = 1;
813                 } elsif ($subsection ne "Standard" && $subsection ne "Private") {
814                     $UndeclaredSymbols{$symbol} = 1;
815                     &LogWarning ($file, $., "No declaration found for $symbol.");
816                 }
817                 $num_symbols++;
818                 $symbols{$symbol}=$.;
819             }
820             else {
821                 &LogWarning ($file, $., "Double symbol entry for $symbol. ".
822                     "Previous occurrence on line ".$symbols{$symbol}.".");
823             }
824         }
825     }
826     close (INPUT);
828     &OutputMissingDocumentation;
829     &OutputUndeclaredSymbols;
831     if ($OUTPUT_ALL_SYMBOLS) {
832         &OutputAllSymbols;
833     }
834     if ($OUTPUT_SYMBOLS_WITHOUT_SINCE) {
835         &OutputSymbolsWithoutSince;
836     }
838     for $filename (split (' ', $EXPAND_CONTENT_FILES)) {
839         my $file_changed = &OutputExtraFile ($filename);
840         if ($file_changed) {
841             $changed = 1;
842         }
843     }
845     &OutputBook ($book_top, $book_bottom);
847     return $changed;
850 #############################################################################
851 # Function    : OutputIndex
852 # Description : This writes an indexlist that can be included into the main-
853 #               document into an <index> tag.
854 #############################################################################
856 sub OutputIndex {
857     my ($basename, $apiindexref ) = @_;
858     my %apiindex = %{$apiindexref};
859     my $old_index = "$SGML_OUTPUT_DIR/$basename.xml";
860     my $new_index = "$SGML_OUTPUT_DIR/$basename.new";
861     my $lastletter = " ";
862     my $divopen = 0;
863     my $symbol;
864     my $short_symbol;
866     open (OUTPUT, ">$new_index")
867         || die "Can't create $new_index";
869     my $header = $doctype_header;
870     $header =~ s/<!DOCTYPE \w+/<!DOCTYPE indexdiv/;
872     print (OUTPUT "$header<indexdiv>\n");
874     #print "generate $basename index (".%apiindex." entries)\n";
875     
876     # do a case insensitive sort while chopping off the prefix
877     foreach my $hash (
878         sort { $$a{criteria} cmp $$b{criteria} }
879         map { my $x = uc($_); $x =~ s/^$NAME_SPACE\_?(.*)/$1/i; { criteria => $x, original => $_, short => $1 } } 
880         keys %apiindex) {
882         $symbol = $$hash{original};
883         if(defined($$hash{short})) {
884             $short_symbol = $$hash{short};
885         } else {
886             $short_symbol = $symbol;
887         }
889         my $curletter = uc(substr($short_symbol,0,1));
890         my $id = $apiindex{$symbol};
892         #print "  add symbol $symbol with $id to index in section $curletter\n";
894         if ($curletter ne $lastletter) {
895             $lastletter = $curletter;
897             if ($divopen == 1) {
898                 print (OUTPUT "</indexdiv>\n");
899             }
900             print (OUTPUT "<indexdiv><title>$curletter</title>\n");
901             $divopen = 1;
902         }
904         print (OUTPUT <<EOF);
905 <indexentry><primaryie linkends="$id"><link linkend="$id">$symbol</link></primaryie></indexentry>
907     }
909     if ($divopen == 1) {
910         print (OUTPUT "</indexdiv>\n");
911     }
912     print (OUTPUT "</indexdiv>\n");
913     close (OUTPUT);
915     &UpdateFileIfChanged ($old_index, $new_index, 0);
919 #############################################################################
920 # Function    : OutputIndexFull
921 # Description : This writes the full api indexlist that can be included into the
922 #               main document into an <index> tag.
923 #############################################################################
925 sub OutputIndexFull {
926     &OutputIndex ("api-index-full", \%IndexEntriesFull);
930 #############################################################################
931 # Function    : OutputDeprecatedIndex
932 # Description : This writes the deprecated api indexlist that can be included
933 #               into the main document into an <index> tag.
934 #############################################################################
936 sub OutputDeprecatedIndex {
937     &OutputIndex ("api-index-deprecated", \%IndexEntriesDeprecated);
941 #############################################################################
942 # Function    : OutputSinceIndexes
943 # Description : This writes the 'since' api indexlists that can be included into
944 #               the main document into an <index> tag.
945 #############################################################################
947 sub OutputSinceIndexes {
948     my @sinces = keys %{{ map { $_ => 1 } values %Since }};
950     foreach my $version (@sinces) {
951         #print "Since : [$version]\n";
952         # todo make filtered hash
953         #my %index = grep { $Since{$_} eq $version } %IndexEntriesSince;
954         my %index = map { $_ => $IndexEntriesSince{$_} } grep { $Since{$_} eq $version } keys %IndexEntriesSince;
956         &OutputIndex ("api-index-$version", \%index);
957     }
960 #############################################################################
961 # Function    : OutputAnnotationGlossary
962 # Description : This writes a glossary of the used annotation terms into a
963 #               separate glossary file that can be included into the main
964 #               document.
965 #############################################################################
967 sub OutputAnnotationGlossary {
968     my $old_glossary = "$SGML_OUTPUT_DIR/annotation-glossary.xml";
969     my $new_glossary = "$SGML_OUTPUT_DIR/annotation-glossary.new";
970     my $lastletter = " ";
971     my $divopen = 0;
973     # TODO: if there are no annotations used return
974     return if (! keys(%AnnotationsUsed));
976     open (OUTPUT, ">$new_glossary")
977         || die "Can't create $new_glossary";
979     my $header = $doctype_header;
980     $header =~ s/<!DOCTYPE \w+/<!DOCTYPE glossary/;
982     print (OUTPUT  <<EOF);
983 $header
984 <glossary id="annotation-glossary">
985   <title>Annotation Glossary</title>
988     foreach my $annotation (keys(%AnnotationsUsed)) {
989         my $def = $AnnotationDefinition{$annotation};
990         my $curletter = uc(substr($annotation,0,1));
992         if ($curletter ne $lastletter) {
993             $lastletter = $curletter;
994       
995             if ($divopen == 1) {
996                 print (OUTPUT "</glossdiv>\n");
997             }
998             print (OUTPUT "<glossdiv><title>$curletter</title>\n");
999             $divopen = 1;
1000         }
1001         print (OUTPUT <<EOF);
1002     <glossentry>
1003       <glossterm><anchor id="annotation-glossterm-$annotation"/>$annotation</glossterm>
1004       <glossdef>
1005         <para>$def</para>
1006       </glossdef>
1007     </glossentry>
1009     }
1011     if ($divopen == 1) {
1012         print (OUTPUT "</glossdiv>\n");
1013     }
1014     print (OUTPUT "</glossary>\n");
1015     close (OUTPUT);
1017     &UpdateFileIfChanged ($old_glossary, $new_glossary, 0);
1020 #############################################################################
1021 # Function    : ReadKnownSymbols
1022 # Description : This collects the names of non-private symbols from the
1023 #               $MODULE-sections.txt file.
1024 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
1025 #               the functions/macros/structs etc. being documented, organised
1026 #               into sections and subsections.
1027 #############################################################################
1029 sub ReadKnownSymbols {
1030     my ($file) = @_;
1032     my $subsection = "";
1034     #print "Reading: $file\n";
1035     open (INPUT, $file)
1036         || die "Can't open $file: $!";
1038     while (<INPUT>) {
1039         if (m/^#/) {
1040             next;
1042         } elsif (m/^<SECTION>/) {
1043             $subsection = "";
1045         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
1046             $subsection = $1;
1048         } elsif (m/^<SUBSECTION>/) {
1049             next;
1051         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
1052             next;
1054         } elsif (m/^<FILE>(.*)<\/FILE>/) {
1055             $KnownSymbols{"$TMPL_DIR/$1:Long_Description"} = 1;
1056             $KnownSymbols{"$TMPL_DIR/$1:Short_Description"} = 1;
1057             next;
1059         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
1060             next;
1062         } elsif (m/^<\/SECTION>/) {
1063             next;
1065         } elsif (m/^(\S+)/) {
1066             my $symbol = $1;
1068             if ($subsection ne "Standard" && $subsection ne "Private") {
1069                 $KnownSymbols{$symbol} = 1;
1070             }
1071             else {
1072                 $KnownSymbols{$symbol} = 0;
1073             }
1074         }
1075     }
1076     close (INPUT);
1080 #############################################################################
1081 # Function    : OutputDeclaration
1082 # Description : Returns the synopsis and detailed description DocBook
1083 #               describing one function/macro etc.
1084 # Arguments   : $symbol - the name of the function/macro begin described.
1085 #               $declaration - the declaration of the function/macro.
1086 #############################################################################
1088 sub OutputDeclaration {
1089     my ($symbol, $declaration) = @_;
1091     my $type = $DeclarationTypes {$symbol};
1092     if ($type eq 'MACRO') {
1093         return &OutputMacro ($symbol, $declaration);
1094     } elsif ($type eq 'TYPEDEF') {
1095         return &OutputTypedef ($symbol, $declaration);
1096     } elsif ($type eq 'STRUCT') {
1097         return &OutputStruct ($symbol, $declaration);
1098     } elsif ($type eq 'ENUM') {
1099         return &OutputEnum ($symbol, $declaration);
1100     } elsif ($type eq 'UNION') {
1101         return &OutputUnion ($symbol, $declaration);
1102     } elsif ($type eq 'VARIABLE') {
1103         return &OutputVariable ($symbol, $declaration);
1105     } elsif ($type eq 'FUNCTION') {
1106         return &OutputFunction ($symbol, $declaration, $type);
1107     } elsif ($type eq 'USER_FUNCTION') {
1108         return &OutputFunction ($symbol, $declaration, $type);
1109     } else {
1110         die "Unknown symbol type";
1111     }
1115 #############################################################################
1116 # Function    : OutputSymbolTraits
1117 # Description : Returns the Since and StabilityLevel paragraphs for a symbol.
1118 # Arguments   : $symbol - the name of the function/macro begin described.
1119 #############################################################################
1121 sub OutputSymbolTraits {
1122     my ($symbol) = @_;
1123     my $desc = "";
1125     if (exists $Since{$symbol}) {
1126         $desc .= "<para role=\"since\">Since $Since{$symbol}</para>";
1127     }
1128     if (exists $StabilityLevel{$symbol}) {
1129         $desc .= "<para role=\"stability\">Stability Level: $StabilityLevel{$symbol}</para>";
1130     }
1131     return $desc;
1134 #############################################################################
1135 # Function    : Outpu{Symbol,Section}ExtraLinks
1136 # Description : Returns extralinks for the symbol (if enabled).
1137 # Arguments   : $symbol - the name of the function/macro begin described.
1138 #############################################################################
1140 sub uri_escape {
1141     my $text = $_[0];
1142     return undef unless defined $text;
1144     # Build a char to hex map
1145     my %escapes = ();
1146     for (0..255) {
1147             $escapes{chr($_)} = sprintf("%%%02X", $_);
1148     }
1150     # Default unsafe characters.  RFC 2732 ^(uric - reserved)
1151     $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
1153     return $text;
1156 sub OutputSymbolExtraLinks {
1157     my ($symbol) = @_;
1158     my $desc = "";
1160     if (0) { # NEW FEATURE: needs configurability
1161     my $sstr = &uri_escape($symbol);
1162     my $mstr = &uri_escape($MODULE);
1163     $desc .= <<EOF;
1164 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1165 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$sstr">edit documentation</ulink>
1167     }
1168     return $desc;
1171 sub OutputSectionExtraLinks {
1172     my ($symbol,$docsymbol) = @_;
1173     my $desc = "";
1175     if (0) { # NEW FEATURE: needs configurability
1176     my $sstr = &uri_escape($symbol);
1177     my $mstr = &uri_escape($MODULE);
1178     my $dsstr = &uri_escape($docsymbol);
1179     $desc .= <<EOF;
1180 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1181 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$dsstr">edit documentation</ulink>
1183     }
1184     return $desc;
1188 #############################################################################
1189 # Function    : OutputMacro
1190 # Description : Returns the synopsis and detailed description of a macro.
1191 # Arguments   : $symbol - the macro.
1192 #               $declaration - the declaration of the macro.
1193 #############################################################################
1195 sub OutputMacro {
1196     my ($symbol, $declaration) = @_;
1197     my $id = &CreateValidSGMLID ($symbol);
1198     my $condition = &MakeConditionDescription ($symbol);
1199     my $synop = &MakeReturnField("#define") . "<link linkend=\"$id\">$symbol</link>";
1200     my $desc;
1201     my $padding = "";
1202     my $args = "";
1203     my $pad;
1205     if ($declaration =~ m/^\s*#\s*define(\s+)\w+(\([^\)]*\))/) {
1206         $padding = $1;
1207         $args = $2;
1209         if (length ($symbol) < $SYMBOL_FIELD_WIDTH) {
1210             $synop .= (' ' x ($SYMBOL_FIELD_WIDTH - length ($symbol)));
1211         }
1213         # Try to align all the lines of args correctly.
1214         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH + $SYMBOL_FIELD_WIDTH + 1);
1215         my $args_padded = $args;
1216         $args_padded =~ s/ *\\\n */\n$pad/gm;
1217         $synop .= &CreateValidSGML ($args_padded);
1218     }
1219     $synop .= "\n";
1221     my $title = $symbol . ($args ? "()" : "");
1222     $desc = "<refsect2 id=\"$id\" role=\"macro\"$condition>\n<title>$title</title>\n";
1223     $desc .= MakeIndexterms($symbol, $id);
1224     $desc .= "\n";
1225     $desc .= OutputSymbolExtraLinks($symbol);
1227     # Don't output the macro definition if is is a conditional macro or it
1228     # looks like a function, i.e. starts with "g_" or "_?gnome_", or it is
1229     # longer than 2 lines, otherwise we get lots of complicated macros like
1230     # g_assert.
1231     if (!defined ($DeclarationConditional{$symbol}) && ($symbol !~ m/^g_/)
1232         && ($symbol !~ m/^_?gnome_/) && (($declaration =~ tr/\n//) < 2)) {
1233         my $decl_out = &CreateValidSGML ($declaration);
1234         $desc .= "<programlisting>$decl_out</programlisting>\n";
1235     } else {
1236         $desc .= "<programlisting>" . &MakeReturnField("#define") . "$symbol";
1237         # Align each line so that if should all line up OK.
1238         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH - length ("#define "));
1239         $args =~ s/\n/\n$pad/gm;
1240         $desc .= &CreateValidSGML ($args);
1241         $desc .= "</programlisting>\n";
1242     }
1244     $desc .= &MakeDeprecationNote($symbol);
1246     my $parameters = &OutputParamDescriptions ("MACRO", $symbol);
1247     my $parameters_output = 0;
1249     if (defined ($SymbolDocs{$symbol})) {
1250         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1252         # Try to insert the parameter table at the author's desired position.
1253         # Otherwise we need to tag it onto the end.
1254         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1255           $parameters_output = 1;
1256         }
1257         $desc .= $symbol_docs;
1258     }
1260     if ($parameters_output == 0) {
1261         $desc .= $parameters;
1262     }
1264     $desc .= OutputSymbolTraits ($symbol);
1265     $desc .= "</refsect2>\n";
1266     return ($synop, $desc);
1270 #############################################################################
1271 # Function    : OutputTypedef
1272 # Description : Returns the synopsis and detailed description of a typedef.
1273 # Arguments   : $symbol - the typedef.
1274 #               $declaration - the declaration of the typedef,
1275 #                 e.g. 'typedef unsigned int guint;'
1276 #############################################################################
1278 sub OutputTypedef {
1279     my ($symbol, $declaration) = @_;
1280     my $id = &CreateValidSGMLID ($symbol);
1281     my $condition = &MakeConditionDescription ($symbol);
1282     my $synop = &MakeReturnField("typedef") . "<link linkend=\"$id\">$symbol</link>;\n";
1283     my $desc = "<refsect2 id=\"$id\" role=\"typedef\"$condition>\n<title>$symbol</title>\n";
1285     $desc .= MakeIndexterms($symbol, $id);
1286     $desc .= "\n";
1287     $desc .= OutputSymbolExtraLinks($symbol);
1289     if (!defined ($DeclarationConditional{$symbol})) {
1290         my $decl_out = &CreateValidSGML ($declaration);
1291         $desc .= "<programlisting>$decl_out</programlisting>\n";
1292     }
1294     $desc .= &MakeDeprecationNote($symbol);
1296     if (defined ($SymbolDocs{$symbol})) {
1297         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1298     }
1299     $desc .= OutputSymbolTraits ($symbol);
1300     $desc .= "</refsect2>\n";
1301     return ($synop, $desc);
1305 #############################################################################
1306 # Function    : OutputStruct
1307 # Description : Returns the synopsis and detailed description of a struct.
1308 #               We check if it is a widget struct, and if so we only output
1309 #               parts of it that are noted as public fields.
1310 #               We also use a different SGML ID for widget structs, since the
1311 #               original ID is used for the entire RefEntry.
1312 # Arguments   : $symbol - the struct.
1313 #               $declaration - the declaration of the struct.
1314 #############################################################################
1316 sub OutputStruct {
1317     my ($symbol, $declaration) = @_;
1319     my $is_widget_struct = 0;
1320     my $default_to_public = 1;
1321     if (&CheckIsObject ($symbol)) {
1322     #print "Found widget struct: $symbol\n";
1323     $is_widget_struct = 1;
1324         $default_to_public = 0;
1325     }
1327     my $id;
1328     my $condition;
1329     if ($is_widget_struct) {
1330         $id = &CreateValidSGMLID ($symbol . "_struct");
1331         $condition = &MakeConditionDescription ($symbol . "_struct");
1332     } else {
1333         $id = &CreateValidSGMLID ($symbol);
1334         $condition = &MakeConditionDescription ($symbol);
1335     }
1337     # Determine if it is a simple struct or it also has a typedef.
1338     my $has_typedef = 0;
1339     if ($StructHasTypedef{$symbol} || $declaration =~ m/^\s*typedef\s+/) {
1340       $has_typedef = 1;
1341     }
1343     my $synop;
1344     my $desc;
1345     if ($has_typedef) {
1346         # For structs with typedefs we just output the struct name.
1347         $synop = &MakeReturnField("") . "<link linkend=\"$id\">$symbol</link>;\n";
1348         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>$symbol</title>\n";
1349     } else {
1350         $synop = &MakeReturnField("struct") . "<link linkend=\"$id\">$symbol</link>;\n";
1351         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>struct $symbol</title>\n";
1352     }
1354     $desc .= MakeIndexterms($symbol, $id);
1355     $desc .= "\n";
1356     $desc .= OutputSymbolExtraLinks($symbol);
1358     # Form a pretty-printed, private-data-removed form of the declaration
1360     my $decl_out = "";
1361     if ($declaration =~ m/^\s*$/) {
1362         #print "Found opaque struct: $symbol\n";
1363         $decl_out = "typedef struct _$symbol $symbol;";
1364     } elsif ($declaration =~ m/^\s*struct\s+\w+\s*;\s*$/) {
1365         #print "Found opaque struct: $symbol\n";
1366         $decl_out = "struct $symbol;";
1367     } else {
1368         my $public = $default_to_public;
1369         my $new_declaration = "";
1370         my $decl_line;
1371         my $decl = $declaration;
1373         if ($decl =~ m/^\s*(typedef\s+)?struct\s*\w*\s*(?:\/\*.*\*\/)?\s*{(.*)}\s*\w*\s*;\s*$/s) {
1374             my $struct_contents = $2;
1376             foreach $decl_line (split (/\n/, $struct_contents)) {
1377                 #print "Struct line: $decl_line\n";
1378                 if ($decl_line =~ m%/\*\s*<\s*public\s*>\s*\*/%) {
1379                     $public = 1;
1380                 } elsif ($decl_line =~ m%/\*\s*<\s*(private|protected)\s*>\s*\*/%) {
1381                     $public = 0;
1382                 } elsif ($public) {
1383                     $new_declaration .= $decl_line . "\n";
1384                 }
1385             }
1387             if ($new_declaration) {
1388                 # Strip any blank lines off the ends.
1389                 $new_declaration =~ s/^\s*\n//;
1390                 $new_declaration =~ s/\n\s*$/\n/;
1392                 if ($has_typedef) {
1393                     $decl_out = "typedef struct {\n" . $new_declaration
1394                       . "} $symbol;\n";
1395                 } else {
1396                     $decl_out = "struct $symbol {\n" . $new_declaration
1397                       . "};\n";
1398                 }
1399             }
1400         } else {
1401             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1402                 "Couldn't parse struct:\n$declaration");
1403         }
1405         # If we couldn't parse the struct or it was all private, output an
1406         # empty struct declaration.
1407         if ($decl_out eq "") {
1408             if ($has_typedef) {
1409                 $decl_out = "typedef struct _$symbol $symbol;";
1410             } else {
1411                 $decl_out = "struct $symbol;";
1412             }
1413         }
1414     }
1416     $decl_out = &CreateValidSGML ($decl_out);
1417     $desc .= "<programlisting>$decl_out</programlisting>\n";
1419     $desc .= &MakeDeprecationNote($symbol);
1421     if (defined ($SymbolDocs{$symbol})) {
1422         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1423     }
1425     # Create a table of fields and descriptions
1427     # FIXME: Inserting &#160's into the produced type declarations here would
1428     #        improve the output in most situations ... except for function
1429     #        members of structs!
1430     my @fields = ParseStructDeclaration($declaration, !$default_to_public,
1431                                         0, \&MakeXRef,
1432                                         sub {
1433                                             "<structfield id=\"$id.$_[0]\">$_[0]</structfield>";
1434                                         });
1435     my $params = $SymbolParams{$symbol};
1437     # If no parameters are filled in, we don't generate the description
1438     # table, for backwards compatibility
1440     my $found = 0;
1441     if (defined $params) {
1442         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1443             if ($params->[$i] =~ /\S/) {
1444                 $found = 1;
1445                 last;
1446             }
1447         }
1448     }
1450     if ($found) {
1451         my %field_descrs = @$params;
1453             $desc .= <<EOF;
1454 <variablelist role="struct">
1456         while (@fields) {
1457             my $field_name = shift @fields;
1458             my $text = shift @fields;
1459             my $field_descr = $field_descrs{$field_name};
1460             my $param_annotations = "";
1462             $desc .= "<varlistentry><term>$text</term>\n";
1463             if (defined $field_descr) {
1464                 ($field_descr,$param_annotations) = &ExpandAnnotation($symbol, $field_descr);
1465                 $field_descr = &ExpandAbbreviations($symbol, $field_descr);
1466                 $desc .= "<listitem><simpara>$field_descr$param_annotations</simpara></listitem>\n";
1467             } else {
1468                 $desc .= "<listitem><simpara /></listitem>\n";
1469             }
1470             $desc .= "</varlistentry>\n";
1471         }
1473         $desc .= "</variablelist>";
1474     }
1475     $desc .= OutputSymbolTraits ($symbol);
1476     $desc .= "</refsect2>\n";
1477     return ($synop, $desc);
1481 #############################################################################
1482 # Function    : OutputEnum
1483 # Description : Returns the synopsis and detailed description of a enum.
1484 # Arguments   : $symbol - the enum.
1485 #               $declaration - the declaration of the enum.
1486 #############################################################################
1488 sub OutputEnum {
1489     my ($symbol, $declaration) = @_;
1490     my $id = &CreateValidSGMLID ($symbol);
1491     my $condition = &MakeConditionDescription ($symbol);
1492     my $synop = &MakeReturnField("enum") . "<link linkend=\"$id\">$symbol</link>;\n";
1493     my $desc = "<refsect2 id=\"$id\" role=\"enum\"$condition>\n<title>enum $symbol</title>\n";
1495     $desc .= MakeIndexterms($symbol, $id);
1496     $desc .= "\n";
1497     $desc .= OutputSymbolExtraLinks($symbol);
1499     my $decl_out = &CreateValidSGML ($declaration);
1500     $desc .= "<programlisting>$decl_out</programlisting>\n";
1502     $desc .= &MakeDeprecationNote($symbol);
1504     if (defined ($SymbolDocs{$symbol})) {
1505         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1506     }
1508     # Create a table of fields and descriptions
1510     my @members = ParseEnumDeclaration($declaration);
1511     my $params = $SymbolParams{$symbol};
1513     # If no parameters are filled in, we don't generate the description
1514     # table, for backwards compatibility
1516     my $found = 0;
1517     if (defined $params) {
1518         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1519             if ($params->[$i] =~ /\S/) {
1520                 $found = 1;
1521                 last;
1522             }
1523         }
1524     }
1526     if ($found) {
1527         my %member_descrs = @$params;
1529             $desc .= <<EOF;
1530 <variablelist role="enum">
1532         for my $member_name (@members) {
1533             my $member_descr = $member_descrs{$member_name};
1534             my $param_annotations = "";
1536             $id = &CreateValidSGMLID ($member_name);
1537             $condition = &MakeConditionDescription ($member_name);
1538             $desc .= "<varlistentry id=\"$id\" role=\"constant\"$condition>\n<term><literal>$member_name</literal></term>\n";
1539             if (defined $member_descr) {
1540                 ($member_descr,$param_annotations) = &ExpandAnnotation($symbol, $member_descr);
1541                 $member_descr = &ExpandAbbreviations($symbol, $member_descr);
1542                 $desc .= "<listitem><simpara>$member_descr$param_annotations</simpara></listitem>\n";
1543             } else {
1544                 $desc .= "<listitem></listitem>\n";
1545             }
1546             $desc .= "</varlistentry>\n";
1547         }
1549         $desc .= "</variablelist>";
1550     }
1552     $desc .= OutputSymbolTraits ($symbol);
1553     $desc .= "</refsect2>\n";
1554     return ($synop, $desc);
1558 #############################################################################
1559 # Function    : OutputUnion
1560 # Description : Returns the synopsis and detailed description of a union.
1561 # Arguments   : $symbol - the union.
1562 #               $declaration - the declaration of the union.
1563 #############################################################################
1565 sub OutputUnion {
1566     my ($symbol, $declaration) = @_;
1567     my $id = &CreateValidSGMLID ($symbol);
1568     my $condition = &MakeConditionDescription ($symbol);
1569     my $synop = &MakeReturnField("union") . "<link linkend=\"$id\">$symbol</link>;\n";
1570     my $desc = "<refsect2 id=\"$id\" role=\"union\"$condition>\n<title>union $symbol</title>\n";
1572     $desc .= MakeIndexterms($symbol, $id);
1573     $desc .= "\n";
1574     $desc .= OutputSymbolExtraLinks($symbol);
1576     my $decl_out = &CreateValidSGML ($declaration);
1577     $desc .= "<programlisting>$decl_out</programlisting>\n";
1579     $desc .= &MakeDeprecationNote($symbol);
1581     if (defined ($SymbolDocs{$symbol})) {
1582         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1583     }
1584     $desc .= OutputSymbolTraits ($symbol);
1585     $desc .= "</refsect2>\n";
1586     return ($synop, $desc);
1590 #############################################################################
1591 # Function    : OutputVariable
1592 # Description : Returns the synopsis and detailed description of a variable.
1593 # Arguments   : $symbol - the extern'ed variable.
1594 #               $declaration - the declaration of the variable.
1595 #############################################################################
1597 sub OutputVariable {
1598     my ($symbol, $declaration) = @_;
1599     my $id = &CreateValidSGMLID ($symbol);
1600     my $condition = &MakeConditionDescription ($symbol);
1602     my $synop;
1603     if ($declaration =~ m/^\s*extern\s+((const\s+|unsigned\s+)*\w+)(\s+\*+|\*+|\s)(\s*)([A-Za-z]\w*)\s*;/) {
1604         my $mod = defined ($1) ? $1 : "";
1605         my $ptr = defined ($3) ? $3 : "";
1606         my $space = defined ($4) ? $4 : "";
1607         $synop = &MakeReturnField("extern") . "$mod$ptr$space<link linkend=\"$id\">$symbol</link>;\n";
1609     } else {
1610         $synop = &MakeReturnField("extern") . "<link linkend=\"$id\">$symbol</link>;\n";
1611     }
1613     my $desc = "<refsect2 id=\"$id\" role=\"variable\"$condition>\n<title>$symbol</title>\n";
1615     $desc .= MakeIndexterms($symbol, $id);
1616     $desc .= "\n";
1617     $desc .= OutputSymbolExtraLinks($symbol);
1619     my $decl_out = &CreateValidSGML ($declaration);
1620     $desc .= "<programlisting>$decl_out</programlisting>\n";
1622     $desc .= &MakeDeprecationNote($symbol);
1624     if (defined ($SymbolDocs{$symbol})) {
1625         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1626     }
1627     $desc .= OutputSymbolTraits ($symbol);
1628     $desc .= "</refsect2>\n";
1629     return ($synop, $desc);
1633 #############################################################################
1634 # Function    : OutputFunction
1635 # Description : Returns the synopsis and detailed description of a function.
1636 # Arguments   : $symbol - the function.
1637 #               $declaration - the declaration of the function.
1638 #############################################################################
1640 sub OutputFunction {
1641     my ($symbol, $declaration, $symbol_type) = @_;
1642     my $id = &CreateValidSGMLID ($symbol);
1643     my $condition = &MakeConditionDescription ($symbol);
1645     # Take out the return type     $1                                                           $3   $4
1646     $declaration =~ s/<RETURNS>\s*((const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|enum\s+)*)(\w+)(\s*\**\s*(const|G_CONST_RETURN)?\s*\**\s*(restrict)?\s*)<\/RETURNS>\n//;
1647     my $type_modifier = defined($1) ? $1 : "";
1648     my $type = $3;
1649     my $pointer = $4;
1650     #print "$symbol pointer is $pointer\n";
1651     my $xref = &MakeXRef ($type);
1652     my $start = "";
1653     #if ($symbol_type eq 'USER_FUNCTION') {
1654     #    $start = "typedef ";
1655     #}
1657     # We output const rather than G_CONST_RETURN.
1658     $type_modifier =~ s/G_CONST_RETURN/const/g;
1659     $pointer =~ s/G_CONST_RETURN/const/g;
1660     $pointer =~ s/^\s+/ /g;
1662     my $ret_type_len = length ($start) + length ($type_modifier)
1663         + length ($pointer) + length ($type);
1664     my $ret_type_output;
1665     my $symbol_len;
1666     if ($ret_type_len < $RETURN_TYPE_FIELD_WIDTH) {
1667         $ret_type_output = "$start$type_modifier$xref$pointer"
1668             . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
1669         $symbol_len = 0;
1670     } else {
1671 #       $ret_type_output = "$start$type_modifier$xref$pointer\n"
1672 #           . (' ' x $RETURN_TYPE_FIELD_WIDTH);
1674         $ret_type_output = "$start$type_modifier$xref$pointer ";
1675         $symbol_len = $ret_type_len + 1 - $RETURN_TYPE_FIELD_WIDTH;
1676     }
1678     $symbol_len += length ($symbol);
1679     my $char1 = my $char2 = my $char3 = "";
1680     if ($symbol_type eq 'USER_FUNCTION') {
1681         $symbol_len += 3;
1682         $char1 = "(";
1683         $char2 = "*";
1684         $char3 = ")";
1685     }
1687     my ($symbol_output, $symbol_desc_output);
1688     if ($symbol_len < $SYMBOL_FIELD_WIDTH) {
1689         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3"
1690             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1691         $symbol_desc_output = "$char1$char2$symbol$char3"
1692             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1693     } else {
1694         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3\n"
1695             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1696         $symbol_desc_output = "$char1$char2$symbol$char3\n"
1697             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1698     }
1700     my $synop = $ret_type_output . $symbol_output . '(';
1701     my $desc = "<refsect2 id=\"$id\" role=\"function\"$condition>\n<title>${symbol} ()</title>\n";
1703     $desc .= MakeIndexterms($symbol, $id);
1704     $desc .= "\n";
1705     $desc .= OutputSymbolExtraLinks($symbol);
1707     $desc  .= "<programlisting>${ret_type_output}$symbol_desc_output(";
1709     my $param_num = 0;
1710     while ($declaration ne "") {
1711         #print "$declaration";
1713         if ($declaration =~ s/^[\s,]+//) {
1714             # skip whitespace and commas
1715             next;
1717         } elsif ($declaration =~ s/^void\s*[,\n]//) {
1718             $synop .= "void";
1719             $desc  .= "void";
1721         } elsif ($declaration =~ s/^...\s*[,\n]//) {
1722             if ($param_num == 0) {
1723                 $synop .= "...";
1724                 $desc  .= "...";
1725             } else {
1726                 $synop .= ",\n"
1727                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1728                     . " ...";
1729                 $desc  .= ",\n"
1730                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1731                     . " ...";
1732             }
1734         # allow alphanumerics, '_', '[' & ']' in param names
1735         # Try to match a standard parameter (keep in sync with gtkdoc-mktmpl)
1736         #                                $1                                                                                                                                    $2                             $3                                                           $4       $5
1737         } 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]//) {
1738             my $pre     = defined($1) ? $1 : "";
1739             my $type    = $2;
1740             my $ptr     = defined($3) ? $3 : "";
1741             my $name    = defined($4) ? $4 : "";
1742             my $array   = defined($5) ? $5 : "";
1744             $pre  =~ s/\s+/ /g;
1745             $type =~ s/\s+/ /g;
1746             $ptr  =~ s/\s+/ /g;
1747             $ptr  =~ s/\s+$//;
1748             if ($ptr && $ptr !~ m/\*$/) { $ptr .= " "; }
1750             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1752             if (($name eq "") && $pre =~ m/^((un)?signed .*)\s?/ ) {
1753                 $name = $type;
1754                 $type = "$1";
1755                 $pre = "";
1756             }
1758             my $xref = &MakeXRef ($type);
1759             my $label   = "$pre$xref $ptr$name$array";
1761             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1763             if ($param_num == 0) {
1764                 $synop .= "$label";
1765                 $desc  .= "$label";
1766             } else {
1767                 $synop .= ",\n"
1768                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1769                     . " $label";
1770                 $desc  .= ",\n"
1771                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1772                     . " $label";
1773             }
1775         # Try to match parameters which are functions (keep in sync with gtkdoc-mktmpl)
1776         #                              $1                                       $2          $3      $4                        $5                    $7             $8
1777         } 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]//) {
1778             my $mod1 = defined($1) ? $1 : "";
1779             if (defined($2)) { $mod1 .= $2; }
1780             my $type = $3;
1781             my $ptr1 = $4;
1782             my $mod2 = defined($5) ? $5 : "";
1783             my $func_ptr = $6;
1784             my $name = $7;
1785             my $func_params = defined($8) ? $8 : "";
1786             
1787             #if (!defined($type)) { print "## no type\n"; };
1788             #if (!defined($ptr1)) { print "## no ptr1\n"; };
1789             #if (!defined($func_ptr)) { print "## no func_ptr\n"; };
1790             #if (!defined($name)) { print "## no name\n"; };
1792             if ($ptr1 && $ptr1 !~ m/\*$/) { $ptr1 .= " "; }
1793             $func_ptr  =~ s/\s+//g;
1794             my $xref = &MakeXRef ($type);
1795             my $label = "$mod1$xref$ptr1$mod2 ($func_ptr$name) ($func_params)";
1797             #print "Type: [$mod1][$xref][$ptr1][$mod2] ([$func_ptr][$name]) ($func_params)\n";
1798             if ($param_num == 0) {
1799                 $synop .= "$label";
1800                 $desc  .= "$label";
1801             } else {
1802                 $synop .= ",\n"
1803                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1804                     . " $label";
1805                 $desc  .= ",\n"
1806                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1807                     . " $label";
1808             }
1810         } else {
1811             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1812                 "Can't parse args for function $symbol: $declaration");
1813             last;
1814         }
1815         $param_num++;
1816     }
1817     $synop .= ");\n";
1818     $desc  .= ");</programlisting>\n";
1820     $desc .= &MakeDeprecationNote($symbol);
1822     my $parameters = &OutputParamDescriptions ("FUNCTION", $symbol);
1823     my $parameters_output = 0;
1825     if (defined ($SymbolDocs{$symbol})) {
1826         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1828         # Try to insert the parameter table at the author's desired position.
1829         # Otherwise we need to tag it onto the end.
1830         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1831           $parameters_output = 1;
1832         }
1833         $desc .= $symbol_docs;
1834     }
1836     if ($parameters_output == 0) {
1837         $desc .= $parameters;
1838     }
1840     $desc .= OutputSymbolTraits ($symbol);
1841     $desc .= "</refsect2>\n";
1842     return ($synop, $desc);
1846 #############################################################################
1847 # Function    : OutputParamDescriptions
1848 # Description : Returns the DocBook output describing the parameters of a
1849 #               function, macro or signal handler.
1850 # Arguments   : $symbol_type - 'FUNCTION', 'MACRO' or 'SIGNAL'. Signal
1851 #                 handlers have an implicit user_data parameter last.
1852 #               $symbol - the name of the function/macro being described.
1853 #############################################################################
1855 sub OutputParamDescriptions {
1856     my ($symbol_type, $symbol) = @_;
1857     my $output = "";
1858     if (defined ($SymbolParams{$symbol})) {
1859         my $returns = "";
1860         my $params = $SymbolParams{$symbol};
1861         my $params_desc = "";
1862         my $j;
1863         for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
1864             my $param_name = $$params[$j];
1865             my $param_desc = $$params[$j + 1];
1866             my $param_annotations = "";
1868             ($param_desc,$param_annotations) = & ExpandAnnotation($symbol, $param_desc);
1869             $param_desc = &ExpandAbbreviations($symbol, $param_desc);
1870             if ($param_name eq "Returns") {
1871                 $returns = "$param_desc$param_annotations";
1872             } else {
1873                 if ($param_name eq "Varargs") {
1874                     $param_name = "...";
1875                 }
1876                 $params_desc .= "<varlistentry><term><parameter>$param_name</parameter>&#160;:</term>\n<listitem><simpara>$param_desc$param_annotations</simpara></listitem></varlistentry>\n";
1877             }
1878         }
1880         # Signals have an implicit user_data parameter which we describe.
1881         if ($symbol_type eq "SIGNAL") {
1882             $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";
1883         }
1885         # Start a table if we need one.
1886         if ($params_desc || $returns) {
1887             $output .= "<variablelist role=\"params\">\n";
1888             if ($params_desc ne "") {
1889                 #$output .= "<varlistentry><term>Parameters:</term><listitem></listitem></varlistentry>\n";
1890                 $output .= $params_desc;
1891             }
1893             # Output the returns info last.
1894             if ($returns) {
1895                 $output .= "<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>$returns</simpara></listitem></varlistentry>\n";
1896             }
1898             # Finish the table.
1899             $output .= "</variablelist>";
1900         }
1901     }
1902     return $output;
1906 #############################################################################
1907 # Function    : ParseStabilityLevel
1908 # Description : Parses a stability level and outputs a warning if it isn't
1909 #               valid.
1910 # Arguments   : $stability - the stability text.
1911 #               $file, $line - context for error message
1912 #               $message - description of where the level is from, to use in
1913 #               any error message.
1914 # Returns     : The parsed stability level string.
1915 #############################################################################
1917 sub ParseStabilityLevel {
1918     my ($stability, $file, $line, $message) = @_;
1920     $stability =~ s/^\s*//;
1921     $stability =~ s/\s*$//;
1922     if ($stability =~ m/^stable$/i) {
1923         $stability = "Stable";
1924     } elsif ($stability =~ m/^unstable$/i) {
1925         $stability = "Unstable";
1926     } elsif ($stability =~ m/^private$/i) {
1927         $stability = "Private";
1928     } else {
1929         &LogWarning ($file, $line, "$message is $stability.".
1930             "It should be one of these: Stable, Unstable, or Private.");
1931     }
1932     return $stability;
1936 #############################################################################
1937 # Function    : OutputSGMLFile
1938 # Description : Outputs the final DocBook file for one section.
1939 # Arguments   : $file - the name of the file.
1940 #               $title - the title from the $MODULE-sections.txt file, which
1941 #                 will be overridden by the title in the template file.
1942 #               $section_id - the SGML id to use for the toplevel tag.
1943 #               $includes - comma-separates list of include files added at top
1944 #                 of synopsis, with '<' '>' around them (if not already enclosed in "").
1945 #               $synopsis - reference to the DocBook for the Synopsis part.
1946 #               $details - reference to the DocBook for the Details part.
1947 #               $signal_synop - reference to the DocBook for the Signal Synopsis part
1948 #               $signal_desc - reference to the DocBook for the Signal Description part
1949 #               $args_synop - reference to the DocBook for the Arg Synopsis part
1950 #               $args_desc - reference to the DocBook for the Arg Description part
1951 #               $hierarchy - reference to the DocBook for the Object Hierarchy part
1952 #               $interfaces - reference to the DocBook for the Interfaces part
1953 #               $implementations - reference to the DocBook for the Known Implementations part
1954 #               $prerequisites - reference to the DocBook for the Prerequisites part
1955 #               $derived - reference to the DocBook for the Derived Interfaces part
1956 #               $file_objects - reference to an array of objects in this file
1957 #############################################################################
1959 sub OutputSGMLFile {
1960     my ($file, $title, $section_id, $includes, $synopsis, $details, $signals_synop, $signals_desc, $args_synop, $args_desc, $hierarchy, $interfaces, $implementations, $prerequisites, $derived, $file_objects) = @_;
1962     #print "Output sgml for file $file with title '$title'\n";
1963     
1964     # The edited title overrides the one from the sections file.
1965     my $new_title = $SymbolDocs{"$TMPL_DIR/$file:Title"};
1966     if (defined ($new_title) && $new_title !~ m/^\s*$/) {
1967         $title = $new_title;
1968         #print "Found title: $title\n";
1969     }
1970     my $short_desc = $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
1971     if (!defined ($short_desc) || $short_desc =~ m/^\s*$/) {
1972         $short_desc = "";
1973     } else {
1974         $short_desc = &ExpandAbbreviations("$title:Short_description",
1975                                            $short_desc);
1976         #print "Found short_desc: $short_desc";
1977     }
1978     my $long_desc = $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
1979     if (!defined ($long_desc) || $long_desc =~ m/^\s*$/) {
1980         $long_desc = "";
1981     } else {
1982         $long_desc = &ExpandAbbreviations("$title:Long_description",
1983                                           $long_desc);
1984         #print "Found long_desc: $long_desc";
1985     }
1986     my $see_also = $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
1987     if (!defined ($see_also) || $see_also =~ m%^\s*(<para>)?\s*(</para>)?\s*$%) {
1988         $see_also = "";
1989     } else {
1990         $see_also = &ExpandAbbreviations("$title:See_Also", $see_also);
1991         #print "Found see_also: $see_also";
1992     }
1993     if ($see_also) {
1994         $see_also = "<refsect1 id=\"$section_id.see-also\">\n<title>See Also</title>\n$see_also\n</refsect1>\n";
1995     }
1996     my $stability = $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
1997     if (!defined ($stability) || $stability =~ m/^\s*$/) {
1998         $stability = "";
1999     } else {
2000         $stability = &ParseStabilityLevel($stability, $file, $., "Section stability level");
2001         #print "Found stability: $stability";
2002     }
2003     if ($stability) {
2004         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$stability, unless otherwise indicated\n</refsect1>\n";
2005     } elsif ($DEFAULT_STABILITY) {
2006         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$DEFAULT_STABILITY, unless otherwise indicated\n</refsect1>\n";
2007     }
2009     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
2010         gmtime (time);
2011     my $month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
2012     $year += 1900;
2014     my $include_output = "";
2015     my $include;
2016     foreach $include (split (/,/, $includes)) {
2017         if ($include =~ m/^\".+\"$/) {
2018             $include_output .= "#include ${include}\n";
2019         }
2020         else {
2021             $include =~ s/^\s+|\s+$//gs;
2022             $include_output .= "#include &lt;${include}&gt;\n";
2023         }
2024     }
2025     if ($include_output ne '') {
2026         $include_output = "\n$include_output\n";
2027     }
2028     
2029     my $extralinks = OutputSectionExtraLinks($title,"Section:$file");
2031     my $old_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT";
2032     my $new_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT.new";
2034     open (OUTPUT, ">$new_sgml_file")
2035         || die "Can't create $new_sgml_file: $!";
2037     my $object_anchors = "";
2038     foreach my $object (@$file_objects) {
2039         next if ($object eq $section_id);
2040         my $id = CreateValidSGMLID($object);
2041         #print "Debug: Adding anchor for $object\n";
2042         $object_anchors .= "<anchor id=\"$id\"$empty_element_end";
2043     }
2045     # We used to output this, but is messes up our UpdateFileIfChanged code
2046     # since it changes every day (and it is only used in the man pages):
2047     # "<refentry id="$section_id" revision="$mday $month $year">"
2049     if (lc($OUTPUT_FORMAT) eq "xml") {
2050         print OUTPUT $doctype_header;
2051     }
2053     print OUTPUT <<EOF;
2054 <refentry id="$section_id">
2055 <refmeta>
2056 <refentrytitle role="top_of_page" id="$section_id.top_of_page">$title</refentrytitle>
2057 <manvolnum>3</manvolnum>
2058 <refmiscinfo>\U$MODULE\E Library</refmiscinfo>
2059 </refmeta>
2060 <refnamediv>
2061 <refname>$title</refname>
2062 <refpurpose>$short_desc</refpurpose>
2063 </refnamediv>
2064 $stability
2065 <refsynopsisdiv id="$section_id.synopsis" role="synopsis">
2066 <title role="synopsis.title">Synopsis</title>
2067 $object_anchors
2068 <synopsis>$include_output$${synopsis}</synopsis>
2069 </refsynopsisdiv>
2070 $$hierarchy$$prerequisites$$derived$$interfaces$$implementations$$args_synop$$signals_synop
2071 <refsect1 id="$section_id.description" role="desc">
2072 <title role="desc.title">Description</title>
2073 $extralinks$long_desc
2074 </refsect1>
2075 <refsect1 id="$section_id.details" role="details">
2076 <title role="details.title">Details</title>
2077 $$details
2078 </refsect1>
2079 $$args_desc$$signals_desc$see_also
2080 </refentry>
2082     close (OUTPUT);
2084     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2088 #############################################################################
2089 # Function    : OutputExtraFile
2090 # Description : Copies an "extra" DocBook file into the output directory,
2091 #               expanding abbreviations
2092 # Arguments   : $file - the source file.
2093 #############################################################################
2094 sub OutputExtraFile {
2095     my ($file) = @_;
2097     my $basename;
2099     ($basename = $file) =~ s!^.*/!!;
2101     my $old_sgml_file = "$SGML_OUTPUT_DIR/$basename";
2102     my $new_sgml_file = "$SGML_OUTPUT_DIR/$basename.new";
2104     my $contents;
2106     open(EXTRA_FILE, "<$file") || die "Can't open $file";
2108     {
2109         local $/;
2110         $contents = <EXTRA_FILE>;
2111     }
2113     open (OUTPUT, ">$new_sgml_file")
2114         || die "Can't create $new_sgml_file: $!";
2116     print OUTPUT &ExpandAbbreviations ("$basename file", $contents);
2117     close (OUTPUT);
2119     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2121 #############################################################################
2122 # Function    : OutputBook
2123 # Description : Outputs the SGML entities that need to be included into the
2124 #               main SGML file for the module.
2125 # Arguments   : $book_top - the declarations of the entities, which are added
2126 #                 at the top of the main SGML file.
2127 #               $book_bottom - the references to the entities, which are
2128 #                 added in the main SGML file at the desired position.
2129 #############################################################################
2131 sub OutputBook {
2132     my ($book_top, $book_bottom) = @_;
2134     my $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top";
2135     my $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top.new";
2137     open (OUTPUT, ">$new_file")
2138         || die "Can't create $new_file: $!";
2139     print OUTPUT $book_top;
2140     close (OUTPUT);
2142     &UpdateFileIfChanged ($old_file, $new_file, 0);
2145     $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom";
2146     $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom.new";
2148     open (OUTPUT, ">$new_file")
2149         || die "Can't create $new_file: $!";
2150     print OUTPUT $book_bottom;
2151     close (OUTPUT);
2153     &UpdateFileIfChanged ($old_file, $new_file, 0);
2156     # If the main SGML file hasn't been created yet, we create it here.
2157     # The user can tweak it later.
2158     if ($MAIN_SGML_FILE && ! -e $MAIN_SGML_FILE) {
2159       open (OUTPUT, ">$MAIN_SGML_FILE")
2160         || die "Can't create $MAIN_SGML_FILE: $!";
2162       if (lc($OUTPUT_FORMAT) eq "xml") {
2163           print OUTPUT <<EOF;
2164 <?xml version="1.0"?>
2165 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
2166                "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"
2168   <!ENTITY % local.common.attrib "xmlns:xi  CDATA  #FIXED 'http://www.w3.org/2003/XInclude'">
2170 <book id="index">
2172       } else {
2173         print OUTPUT <<EOF;
2174 <!doctype book PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
2175 $book_top
2177 <book id="index">
2179       }
2181 print OUTPUT <<EOF;
2182   <bookinfo>
2183     <title>$MODULE Reference Manual</title>
2184     <releaseinfo>
2185       for $MODULE [VERSION].
2186       The latest version of this documentation can be found on-line at
2187       <ulink role="online-location" url="http://[SERVER]/$MODULE/index.html">http://[SERVER]/$MODULE/</ulink>.
2188     </releaseinfo>
2189   </bookinfo>
2191   <chapter>
2192     <title>[Insert title here]</title>
2193     $book_bottom
2194   </chapter>
2196   if (-e $OBJECT_TREE_FILE) {
2197     print OUTPUT <<EOF;
2198   <chapter id="object-tree">
2199     <title>Object Hierarchy</title>
2200      <xi:include href="xml/tree_index.sgml"/>
2201   </chapter>
2203   }
2205 print OUTPUT <<EOF;
2206   <index id="api-index-full">
2207     <title>API Index</title>
2208     <xi:include href="xml/api-index-full.xml"><xi:fallback /></xi:include>
2209   </index>
2211   <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
2212 </book>
2215       close (OUTPUT);
2216     }
2220 #############################################################################
2221 # Function    : CreateValidSGMLID
2222 # Description : Creates a valid SGML 'id' from the given string.
2223 #               According to http://www.w3.org/TR/html4/types.html#type-id
2224 #                 "ID and NAME tokens must begin with a letter ([A-Za-z]) and
2225 #                  may be followed by any number of letters, digits ([0-9]),
2226 #                  hyphens ("-"), underscores ("_"), colons (":"), and
2227 #                  periods (".")."
2229 #               NOTE: SGML ids are case-insensitive, so we have a few special
2230 #                     cases to avoid clashes of ids.
2231 # Arguments   : $id - the string to be converted into a valid SGML id.
2232 #############################################################################
2234 sub CreateValidSGMLID {
2235     my ($id) = $_[0];
2237     # Special case, '_' would end up as '' so we use 'gettext-macro' instead.
2238     if ($id eq "_") { return "gettext-macro"; }
2240     $id =~ s/[_ ]/-/g;
2241     $id =~ s/[,;]//g;
2242     $id =~ s/^-*//;
2243     $id =~ s/::/-/g;
2244     $id =~ s/:/--/g;
2246     # Append ":CAPS" to all all-caps identifiers
2247     if ($id !~ /[a-z]/ && $id !~ /-CAPS$/) { $id .= ":CAPS" };
2249     return $id;
2253 #############################################################################
2254 # Function    : CreateValidSGML
2255 # Description : This turns any chars which are used in SGML into entities,
2256 #               e.g. '<' into '&lt;'
2257 # Arguments   : $text - the text to turn into proper SGML.
2258 #############################################################################
2260 sub CreateValidSGML {
2261     my ($text) = @_;
2262     $text =~ s/&/&amp;/g;       # Do this first, or the others get messed up.
2263     $text =~ s/</&lt;/g;
2264     $text =~ s/>/&gt;/g;
2265     # browers render single tabs inconsistently
2266     $text =~ s/([^\s])\t([^\s])/$1&#160;$2/g;
2267     return $text;
2270 #############################################################################
2271 # Function    : ConvertSGMLChars
2272 # Description : This is used for text in source code comment blocks, to turn
2273 #               chars which are used in SGML into entities, e.g. '<' into
2274 #               '&lt;'. Depending on $SGML_MODE, this is done
2275 #               unconditionally or only if the character doesn't seem to be
2276 #               part of an SGML construct (tag or entity reference).
2277 # Arguments   : $text - the text to turn into proper SGML.
2278 #############################################################################
2280 sub ConvertSGMLChars {
2281     my ($symbol, $text) = @_;
2283     if ($SGML_MODE) {
2284         # For the SGML mode only convert to entities outside CDATA sections.
2285         return &ModifyXMLElements ($text, $symbol,
2286                                    "<!\\[CDATA\\[|<programlisting[^>]*>",
2287                                    \&ConvertSGMLCharsEndTag,
2288                                    \&ConvertSGMLCharsCallback);
2289     } else {
2290         # For the simple non-sgml mode, convert to entities everywhere.
2291         $text =~ s/&/&amp;/g;   # Do this first, or the others get messed up.
2292         $text =~ s/</&lt;/g;
2293         $text =~ s/>/&gt;/g;
2294         return $text;
2295     }
2299 sub ConvertSGMLCharsEndTag {
2300   if ($_[0] eq "<!\[CDATA\[") {
2301     return "]]>";
2302   } else {
2303     return "</programlisting>";
2304   }
2307 sub ConvertSGMLCharsCallback {
2308   my ($text, $symbol, $tag) = @_;
2310   if ($tag =~ m/^<programlisting/) {
2311     # We can handle <programlisting> specially here.
2312     return &ModifyXMLElements ($text, $symbol,
2313                                "<!\\[CDATA\\[",
2314                                \&ConvertSGMLCharsEndTag,
2315                                \&ConvertSGMLCharsCallback2);
2316   } elsif ($tag eq "") {
2317     # If we're not in CDATA convert to entities.
2318     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2319     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2320     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2322     # Handle "#include <xxxxx>"
2323     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2324   }
2326   return $text;
2329 sub ConvertSGMLCharsCallback2 {
2330   my ($text, $symbol, $tag) = @_;
2332   # If we're not in CDATA convert to entities.
2333   # We could handle <programlisting> differently, though I'm not sure it helps.
2334   if ($tag eq "") {
2335     # replace only if its not a tag
2336     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2337     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2338     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2340     # Handle "#include <xxxxx>"
2341     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2342   }
2344   return $text;
2347 #############################################################################
2348 # Function    : ExpandAnnotation
2349 # Description : This turns annotations into acrony tags.
2350 # Arguments   : $symbol - the symbol being documented, for error messages.
2351 #               $text - the text to expand.
2352 #############################################################################
2353 sub ExpandAnnotation {
2354     my ($symbol, $param_desc) = @_;
2355     my $param_annotations = "";
2356     
2357     if ($param_desc =~ m%\s*\((.*)\):%) {
2358         my @annotations;
2359         my $annotation;
2360         my $annotation_extra = "";
2361         $param_desc = $';
2362     
2363         @annotations = split(/\)\s*\(/,$1);
2364         foreach $annotation (@annotations) {
2365             # need to search for the longest key-match in %AnnotationDefinition
2366             my $match_length=0;
2367             my $match_annotation="";
2368             my $annotationdef;
2369             foreach $annotationdef (keys %AnnotationDefinition) {
2370                 if ($annotation =~ m/^$annotationdef/) {
2371                     if (length($annotationdef)>$match_length) {
2372                         $match_length=length($annotationdef);
2373                         $match_annotation=$annotationdef;
2374                     }
2375                 }
2376             }
2377             if ($match_annotation ne "") {
2378                 if ($annotation =~ m%$match_annotation\s+(.*)%) {
2379                     $annotation_extra = " $1";
2380                 }
2381                 $AnnotationsUsed{$match_annotation} = 1;
2382                 $param_annotations .= "<acronym>$match_annotation</acronym>$annotation_extra. ";
2383             }
2384             else {
2385                 &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2386                     "unknown annotation \"$annotation\" in documentation for $symbol.");
2387                 $param_annotations=$annotation;
2388             }
2389         }
2390         chomp($param_desc);
2391         $param_desc =~ m/^(.*)\.*\s*$/;
2392         $param_desc = "$1. ";
2393     }    
2394     return ($param_desc, $param_annotations);
2397 #############################################################################
2398 # Function    : ExpandAbbreviations
2399 # Description : This turns the abbreviations function(), macro(), @param,
2400 #               %constant, and #symbol into appropriate DocBook markup.
2401 #               CDATA sections and <programlisting> parts are skipped.
2402 # Arguments   : $symbol - the symbol being documented, for error messages.
2403 #               $text - the text to expand.
2404 #############################################################################
2406 sub ExpandAbbreviations {
2407   my ($symbol, $text) = @_;
2409   # Convert "|[" and "]|" into the start and end of program listing examples.
2410   $text =~ s%\|\[%<informalexample><programlisting>%g;
2411   $text =~ s%\]\|%</programlisting></informalexample>%g;
2412   # TODO: check for a xml comment after |[ and pick the language attribute from
2413   # that
2415   # keep CDATA unmodified, preserve ulink tags (ideally we preseve all tags
2416   # as such)
2417   return &ModifyXMLElements ($text, $symbol,
2418                              "<!\\[CDATA\\[|<ulink[^>]*>|<programlisting[^>]*>|<!DOCTYPE",
2419                              \&ExpandAbbreviationsEndTag,
2420                              \&ExpandAbbreviationsCallback);
2424 # Returns the end tag corresponding to the given start tag.
2425 sub ExpandAbbreviationsEndTag {
2426   my ($start_tag) = @_;
2428   if ($start_tag eq "<!\[CDATA\[") {
2429     return "]]>";
2430   } elsif ($start_tag eq "<!DOCTYPE") {
2431     return "]>";
2432   } elsif ($start_tag =~ m/<(\w+)/) {
2433     return "</$1>";
2434   }
2437 # Called inside or outside each CDATA or <programlisting> section.
2438 sub ExpandAbbreviationsCallback {
2439   my ($text, $symbol, $tag) = @_;
2441   if ($tag =~ m/^<programlisting/) {
2442     # Handle any embedded CDATA sections.
2443     return &ModifyXMLElements ($text, $symbol,
2444                                "<!\\[CDATA\\[",
2445                                \&ExpandAbbreviationsEndTag,
2446                                \&ExpandAbbreviationsCallback2);
2447   } elsif ($tag eq "") {
2448     # We are outside any CDATA or <programlisting> sections, so we expand
2449     # any gtk-doc abbreviations.
2451     # Convert 'function()' or 'macro()'.
2452     $text =~ s/(\w+)\s*\(\)/&MakeXRef($1, &tagify($1 . "()", "function"));/eg;
2454     # Convert '@param', but not '\@param'.
2455     $text =~ s/(\A|[^\\])\@(\w+((\.|->)\w+)*)/$1<parameter>$2<\/parameter>/g;
2456     $text =~ s/\\\@/\@/g;
2458     # Convert '%constant', but not '\%constant'.
2459     # Also allow negative numbers, e.g. %-1.
2460     $text =~ s/(\A|[^\\])\%(-?\w+)/$1.&MakeXRef($2, &tagify($2, "literal"));/eg;
2461     $text =~ s/\\\%/\%/g;
2463     # Convert '#symbol', but not '\#symbol'.
2464     $text =~ s/(\A|[^\\])#([\w\-:\.]+[\w]+)/$1.&MakeHashXRef($2, "type");/eg;
2465     $text =~ s/\\#/#/g;
2466     
2467     # Expand urls
2468     # FIXME: should we skip urls that are already tagged? (e.g. <literal>http://...</literal>)
2469     # this is apparently also called for markup and not just for plain text
2470     # disable for now.
2471     #$text =~ s%(http|https|ftp)://(.*?)((?:\s|,|\)|\]|\<|\.\s))%<ulink url="$1://$2">$2</ulink>$3%g;
2472   }
2474   return $text;
2477 # This is called inside a <programlisting>
2478 sub ExpandAbbreviationsCallback2 {
2479   my ($text, $symbol, $tag) = @_;
2481   if ($tag eq "") {
2482     # We are inside a <programlisting> but outside any CDATA sections,
2483     # so we expand any gtk-doc abbreviations.
2484     # FIXME: why is this different from &ExpandAbbreviationsCallback(),
2485     #        why not just call it
2486     $text =~ s/#(\w+)/&MakeHashXRef($1, "");/eg;
2487   }
2489   return $text;
2492 sub MakeHashXRef {
2493     my ($symbol, $tag) = @_;;
2494     my $text = $symbol;
2496     # Check for things like '#include', '#define', and skip them.
2497     if ($PreProcessorDirectives{$symbol}) {
2498       return "#$symbol";
2499     }
2501     # Get rid of any special '-struct' suffix.
2502     $text =~ s/-struct$//;
2504     # If the symbol is in the form "Object::signal", then change the symbol to
2505     # "Object-signal" and use "signal" as the text.
2506     if ($symbol =~ s/::/-/) {
2507       $text = "\"$'\"";
2508     }
2510     # If the symbol is in the form "Object:property", then change the symbol to
2511     # "Object--property" and use "property" as the text.
2512     if ($symbol =~ s/:/--/) {
2513       $text = "\"$'\"";
2514     }
2516     if ($tag ne "") {
2517       $text = tagify ($text, $tag);
2518     }
2519     
2520     return &MakeXRef($symbol, $text);
2524 #############################################################################
2525 # Function    : ModifyXMLElements
2526 # Description : Looks for given XML element tags within the text, and calls
2527 #               the callback on pieces of text inside & outside those elements.
2528 #               Used for special handling of text inside things like CDATA
2529 #               and <programlisting>.
2530 # Arguments   : $text - the text.
2531 #               $symbol - the symbol currently being documented (only used for
2532 #                      error messages).
2533 #               $start_tag_regexp - the regular expression to match start tags.
2534 #                      e.g. "<!\\[CDATA\\[|<programlisting[^>]*>" to match
2535 #                      CDATA sections or programlisting elements.
2536 #               $end_tag_func - function which is passed the matched start tag
2537 #                      and should return the appropriate end tag string.
2538 #               $callback - callback called with each part of the text. It is
2539 #                      called with a piece of text, the symbol being
2540 #                      documented, and the matched start tag or "" if the text
2541 #                      is outside the XML elements being matched.
2542 #############################################################################
2543 sub ModifyXMLElements {
2544     my ($text, $symbol, $start_tag_regexp, $end_tag_func, $callback) = @_;
2545     my ($before_tag, $start_tag, $end_tag_regexp, $end_tag);
2546     my $result = "";
2548     while ($text =~ m/$start_tag_regexp/s) {
2549       $before_tag = $`; # Prematch for last successful match string
2550       $start_tag = $&;  # Last successful match
2551       $text = $';       # Postmatch for last successful match string
2553       $result .= &$callback ($before_tag, $symbol, "");
2554       $result .= $start_tag;
2556       # get the mathing end-tag for current tag
2557       $end_tag_regexp = &$end_tag_func ($start_tag);
2559       if ($text =~ m/$end_tag_regexp/s) {
2560         $before_tag = $`;
2561         $end_tag = $&;
2562         $text = $';
2564         $result .= &$callback ($before_tag, $symbol, $start_tag);
2565         $result .= $end_tag;
2566       } else {
2567         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2568             "Can't find tag end: $end_tag_regexp in docs for: $symbol.");
2569         # Just assume it is all inside the tag.
2570         $result .= &$callback ($text, $symbol, $start_tag);
2571         $text = "";
2572       }
2573     }
2575     # Handle any remaining text outside the tags.
2576     $result .= &$callback ($text, $symbol, "");
2578     return $result;
2581 sub noop {
2582   return $_[0];
2585 # Adds a tag around some text.
2586 # e.g tagify("Text", "literal") => "<literal>Text</literal>".
2587 sub tagify {
2588    my ($text, $elem) = @_;
2589    return "<" . $elem . ">" . $text . "</" . $elem . ">";
2593 #############################################################################
2594 # Function    : MakeXRef
2595 # Description : This returns a cross-reference link to the given symbol.
2596 #               Though it doesn't try to do this for a few standard C types
2597 #               that it knows won't be in the documentation.
2598 # Arguments   : $symbol - the symbol to try to create a XRef to.
2599 #               $text - text text to put inside the XRef, defaults to $symbol
2600 #############################################################################
2602 sub MakeXRef {
2603     my ($symbol, $text) = ($_[0], $_[1]);
2604     if (!defined($text)) {
2605         $text = $symbol;
2607         # Get rid of special '-struct' suffix.
2608         $text =~ s/-struct$//;
2609     }
2611     #print "Getting type link for $symbol -> $text\n";
2613     my $symbol_id = &CreateValidSGMLID ($symbol);
2614     return "<link linkend=\"$symbol_id\">$text</link>";
2618 #############################################################################
2619 # Function    : MakeIndexterms
2620 # Description : This returns a indexterm elements for the given symbol
2621 # Arguments   : $symbol - the symbol to create indexterms for
2622 #############################################################################
2624 sub MakeIndexterms {
2625   my ($symbol, $id) = @_;
2626   my $terms =  "";
2627   my $sortas = "";
2628   
2629   # make the index useful, by ommiting the namespace when sorting
2630   if ($NAME_SPACE ne "") {
2631     if ($symbol =~ m/^$NAME_SPACE\_?(.*)/i) {
2632        $sortas=" sortas=\"$1\"";
2633     }
2634   }
2636   if (exists $Deprecated{$symbol}) {
2637       $terms .= "<indexterm zone=\"$id\" role=\"deprecated\"><primary$sortas>$symbol</primary></indexterm>";
2638       $IndexEntriesDeprecated{$symbol}=$id;
2639       $IndexEntriesFull{$symbol}=$id;
2640   }
2641   if (exists $Since{$symbol}) {
2642      my $since = $Since{$symbol};
2643      $since =~ s/^\s+//;
2644      $since =~ s/\s+$//;
2645      if ($since ne "") {
2646          $terms .= "<indexterm zone=\"$id\" role=\"$since\"><primary$sortas>$symbol</primary></indexterm>";
2647      }
2648      $IndexEntriesSince{$symbol}=$id;
2649      $IndexEntriesFull{$symbol}=$id;
2650   }
2651   if ($terms eq "") {
2652      $terms .= "<indexterm zone=\"$id\"><primary$sortas>$symbol</primary></indexterm>";
2653      $IndexEntriesFull{$symbol}=$id;
2654   }
2656   return $terms;
2659 #############################################################################
2660 # Function    : MakeDeprecationNote
2661 # Description : This returns a deprecation warning for the given symbol.
2662 # Arguments   : $symbol - the symbol to try to create a warning for.
2663 #############################################################################
2665 sub MakeDeprecationNote {
2666     my ($symbol) = $_[0];
2667     my $desc = "";
2668     my $note = "";
2669     if (exists $Deprecated{$symbol}) {
2670         $desc .= "<warning>";
2672         if ($Deprecated{$symbol} =~ /^\s*([0-9\.]+)\s*:/) {
2673                 $desc .= "<para><literal>$symbol</literal> has been deprecated since version $1 and should not be used in newly-written code.";
2674         } else {
2675                 $desc .= "<para><literal>$symbol</literal> is deprecated and should not be used in newly-written code.";
2676         }
2677         if ($Deprecated{$symbol} ne "") {
2678             $note = &ExpandAbbreviations($symbol, $Deprecated{$symbol});
2679             $note =~ s/^\s*([0-9\.]+)\s*:\s*//;
2680             $note =~ s/^\s+//;
2681             $note =~ s/\s+$//;
2682             $note =~ s%\n{2,}%\n</para>\n<para>\n%g;
2683             $desc .= " " . $note;
2684         }
2685         $desc .= "</para></warning>\n";
2686     }
2687     return $desc;
2690 #############################################################################
2691 # Function    : MakeConditionDescription
2692 # Description : This returns a sumary of conditions for the given symbol.
2693 # Arguments   : $symbol - the symbol to try to create the sumary.
2694 #############################################################################
2696 sub MakeConditionDescription {
2697     my ($symbol) = $_[0];
2698     my $desc = "";
2700     if (exists $Deprecated{$symbol}) {
2701         if ($desc ne "") {
2702             $desc .= "|";
2703         }
2705         if ($Deprecated{$symbol} =~ /^\s*(.*?)\s*$/) {
2706                 $desc .= "deprecated:$1";
2707         } else {
2708                 $desc .= "deprecated";
2709         }
2710     }
2712     if (exists $Since{$symbol}) {
2713         if ($desc ne "") {
2714             $desc .= "|";
2715         }
2717         if ($Since{$symbol} =~ /^\s*(.*?)\s*$/) {
2718                 $desc .= "since:$1";
2719         } else {
2720                 $desc .= "since";
2721         }
2722     }
2724     if (exists $StabilityLevel{$symbol}) {
2725         if ($desc ne "") {
2726             $desc .= "|";
2727         }
2728         $desc .= "stability:".$StabilityLevel{$symbol};
2729     }
2731     if ($desc ne "") {
2732         $desc=" condition=\"".$desc."\"";
2733         #print "condition for '$symbol' = '$desc'\n";
2734     }
2735     return $desc;
2738 #############################################################################
2739 # Function    : GetHierarchy
2740 # Description : Returns the DocBook output describing the ancestors and
2741 #               immediate children of a GObject subclass. It uses the
2742 #               global @Objects and @ObjectLevels arrays to walk the tree.
2743 # Arguments   : $object - the GtkObject subclass.
2744 #############################################################################
2746 sub GetHierarchy {
2747     my ($object) = @_;
2749     # Find object in the objects array.
2750     my $found = 0;
2751     my @children = ();
2752     my $i;
2753     my $level;
2754     my $j;
2755     for ($i = 0; $i < @Objects; $i++) {
2756         if ($found) {
2757             if ($ObjectLevels[$i] <= $level) {
2758             last;
2759         }
2760             elsif ($ObjectLevels[$i] == $level + 1) {
2761                 push (@children, $Objects[$i]);
2762             }
2763         }
2764         elsif ($Objects[$i] eq $object) {
2765             $found = 1;
2766             $j = $i;
2767             $level = $ObjectLevels[$i];
2768         }
2769     }
2770     if (!$found) {
2771         return "";
2772     }
2774     # Walk up the hierarchy, pushing ancestors onto the ancestors array.
2775     my @ancestors = ();
2776     push (@ancestors, $object);
2777     #print "Level: $level\n";
2778     while ($level > 1) {
2779         $j--;
2780         if ($ObjectLevels[$j] < $level) {
2781             push (@ancestors, $Objects[$j]);
2782             $level = $ObjectLevels[$j];
2783             #print "Level: $level\n";
2784         }
2785     }
2787     # Output the ancestors list, indented and with links.
2788     my $hierarchy = "<synopsis>\n";
2789     $level = 0;
2790     for ($i = $#ancestors; $i >= 0; $i--) {
2791         my $link_text;
2792         # Don't add a link to the current widget, i.e. when i == 0.
2793         if ($i > 0) {
2794             my $ancestor_id = &CreateValidSGMLID ($ancestors[$i]);
2795             $link_text = "<link linkend=\"$ancestor_id\">$ancestors[$i]</link>";
2796         } else {
2797             $link_text = "$ancestors[$i]";
2798         }
2799         if ($level == 0) {
2800             $hierarchy .= "  $link_text\n";
2801         } else {
2802 #           $hierarchy .= ' ' x ($level * 6 - 3) . "|\n";
2803             $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2804         }
2805         $level++;
2806     }
2807     for ($i = 0; $i <= $#children; $i++) {
2808       my $id = &CreateValidSGMLID ($children[$i]);
2809       my $link_text = "<link linkend=\"$id\">$children[$i]</link>";
2810       $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2811     }
2812     $hierarchy .= "</synopsis>\n";
2814     return $hierarchy;
2818 #############################################################################
2819 # Function    : GetInterfaces
2820 # Description : Returns the DocBook output describing the interfaces
2821 #               implemented by a class. It uses the global %Interfaces hash.
2822 # Arguments   : $object - the GtkObject subclass.
2823 #############################################################################
2825 sub GetInterfaces {
2826     my ($object) = @_;
2827     my $text = "";
2828     my $i;
2830     # Find object in the objects array.
2831     if (exists($Interfaces{$object})) {
2832         my @ifaces = split(' ', $Interfaces{$object});
2833         $text = <<EOF;
2834 <para>
2835 $object implements
2837         for ($i = 0; $i <= $#ifaces; $i++) {
2838             my $id = &CreateValidSGMLID ($ifaces[$i]);
2839             $text .= " <link linkend=\"$id\">$ifaces[$i]</link>";
2840             if ($i < $#ifaces - 1) {
2841                 $text .= ', ';
2842             }
2843             elsif ($i < $#ifaces) {
2844                 $text .= ' and ';
2845             }
2846             else {
2847                 $text .= '.';
2848             }
2849         }
2850         $text .= <<EOF;
2851 </para>
2853     }
2855     return $text;
2858 #############################################################################
2859 # Function    : GetImplementations
2860 # Description : Returns the DocBook output describing the implementations
2861 #               of an interface. It uses the global %Interfaces hash.
2862 # Arguments   : $object - the GtkObject subclass.
2863 #############################################################################
2865 sub GetImplementations {
2866     my ($object) = @_;
2867     my @impls = ();
2868     my $text = "";
2869     my $i;
2870     foreach my $key (keys %Interfaces) {
2871         if ($Interfaces{$key} =~ /\b$object\b/) {
2872             push (@impls, $key);
2873         }
2874     }
2875     if ($#impls >= 0) {
2876         $text = <<EOF;
2877 <para>
2878 $object is implemented by
2880         for ($i = 0; $i <= $#impls; $i++) {
2881             my $id = &CreateValidSGMLID ($impls[$i]);
2882             $text .= " <link linkend=\"$id\">$impls[$i]</link>";
2883             if ($i < $#impls - 1) {
2884                 $text .= ', ';
2885             }
2886             elsif ($i < $#impls) {
2887                 $text .= ' and ';
2888             }
2889             else {
2890                 $text .= '.';
2891             }
2892         }
2893         $text .= <<EOF;
2894 </para>
2896     }
2897     return $text;
2901 #############################################################################
2902 # Function    : GetPrerequisites
2903 # Description : Returns the DocBook output describing the prerequisites
2904 #               of an interface. It uses the global %Prerequisites hash.
2905 # Arguments   : $iface - the interface.
2906 #############################################################################
2908 sub GetPrerequisites {
2909     my ($iface) = @_;
2910     my $text = "";
2911     my $i;
2913     if (exists($Prerequisites{$iface})) {
2914         $text = <<EOF;
2915 <para>
2916 $iface requires
2918         my @prereqs = split(' ', $Prerequisites{$iface});
2919         for ($i = 0; $i <= $#prereqs; $i++) {
2920             my $id = &CreateValidSGMLID ($prereqs[$i]);
2921             $text .= " <link linkend=\"$id\">$prereqs[$i]</link>";
2922             if ($i < $#prereqs - 1) {
2923                 $text .= ', ';
2924             }
2925             elsif ($i < $#prereqs) {
2926                 $text .= ' and ';
2927             }
2928             else {
2929                 $text .= '.';
2930             }
2931         }
2932         $text .= <<EOF;
2933 </para>
2935     }
2936     return $text;
2939 #############################################################################
2940 # Function    : GetDerived
2941 # Description : Returns the DocBook output describing the derived interfaces
2942 #               of an interface. It uses the global %Prerequisites hash.
2943 # Arguments   : $iface - the interface.
2944 #############################################################################
2946 sub GetDerived {
2947     my ($iface) = @_;
2948     my $text = "";
2949     my $i;
2951     my @derived = ();
2952     foreach my $key (keys %Prerequisites) {
2953         if ($Prerequisites{$key} =~ /\b$iface\b/) {
2954             push (@derived, $key);
2955         }
2956     }
2957     if ($#derived >= 0) {
2958         $text = <<EOF;
2959 <para>
2960 $iface is required by
2962         for ($i = 0; $i <= $#derived; $i++) {
2963             my $id = &CreateValidSGMLID ($derived[$i]);
2964             $text .= " <link linkend=\"$id\">$derived[$i]</link>";
2965             if ($i < $#derived - 1) {
2966                 $text .= ', ';
2967             }
2968             elsif ($i < $#derived) {
2969                 $text .= ' and ';
2970             }
2971             else {
2972                 $text .= '.';
2973             }
2974         }
2975         $text .= <<EOF;
2976 </para>
2978     }
2979     return $text;
2983 #############################################################################
2984 # Function    : GetSignals
2985 # Description : Returns the synopsis and detailed description DocBook output
2986 #               for the signal handlers of a given GtkObject subclass.
2987 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
2988 #############################################################################
2990 sub GetSignals {
2991     my ($object) = @_;
2992     my $synop = "";
2993     my $desc = "";
2995     my $i;
2996     for ($i = 0; $i <= $#SignalObjects; $i++) {
2997         if ($SignalObjects[$i] eq $object) {
2998             #print "Found signal: $SignalNames[$i]\n";
2999             my $name = $SignalNames[$i];
3000             my $symbol = "${object}::${name}";
3001             my $id = &CreateValidSGMLID ("$object-$name");
3003             my $pad = ' ' x (46 - length($name));
3004             $synop .= "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad ";
3006             $desc .= "<refsect2 id=\"$id\" role=\"signal\"><title>The <literal>&quot;$name&quot;</literal> signal</title>\n";
3007             $desc .= MakeIndexterms($symbol, $id);
3008             $desc .= "\n";
3009             $desc .= OutputSymbolExtraLinks($symbol);
3011             $desc .= "<programlisting>";
3013             $SignalReturns[$i] =~ m/\s*(const\s+)?(\w+)\s*(\**)/;
3014             my $type_modifier = defined($1) ? $1 : "";
3015             my $type = $2;
3016             my $pointer = $3;
3017             my $xref = &MakeXRef ($type);
3019             my $ret_type_len = length ($type_modifier) + length ($pointer)
3020                 + length ($type);
3021             my $ret_type_output = "$type_modifier$xref$pointer"
3022                 . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
3024             $desc  .= "${ret_type_output}user_function " . &MakeReturnField("") . " (";
3026             my $sourceparams = $SourceSymbolParams{$symbol};
3027             my @params = split ("\n", $SignalPrototypes[$i]);
3028             my $j;
3029             my $l;
3030             my $type_len = length("gpointer");
3031             my $name_len = length("user_data");
3032             # do two passes, the first one is to calculate padding
3033             for ($l = 0; $l < 2; $l++) {
3034                 for ($j = 0; $j <= $#params; $j++) {
3035                     # allow alphanumerics, '_', '[' & ']' in param names
3036                     if ($params[$j] =~ m/^\s*(\w+)\s*(\**)\s*([\w\[\]]+)\s*$/) {
3037                         $type = $1;
3038                         $pointer = $2;
3039                         if (defined($sourceparams)) {
3040                             $name = $$sourceparams[$PARAM_FIELD_COUNT * $j];
3041                         }
3042                         else {
3043                             $name = $3;
3044                         }
3045                         if (!defined($name)) {
3046                             $name = "arg$j";
3047                         }
3048                         if ($l == 0) {
3049                             if (length($type) + length($pointer) > $type_len) {
3050                                 $type_len = length($type) + length($pointer);
3051                             }
3052                             if (length($name) > $name_len) {
3053                                 $name_len = length($name);
3054                             }
3055                         }
3056                         else {
3057                             $xref = &MakeXRef ($type);
3058                             $pad = ' ' x ($type_len - length($type) - length($pointer));
3059                             $desc .= "$xref$pad $pointer$name,\n";
3060                             $desc .= (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
3061                         }
3062                     } else {
3063                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3064                              "Can't parse arg: $params[$j]\nArgs:$SignalPrototypes[$i]");
3065                     }
3066                 }
3067             }
3068             $xref = &MakeXRef ("gpointer");
3069             $pad = ' ' x ($type_len - length("gpointer"));
3070             $desc  .= "$xref$pad user_data)";
3072             my $flags = $SignalFlags[$i];
3073             my $flags_string = "";
3075             if (defined ($flags)) {
3076               if ($flags =~ m/f/) {
3077                 $flags_string = "Run First";
3078               }
3079               elsif ($flags =~ m/l/) {
3080                 $flags_string = "Run Last";
3081               }
3082               elsif ($flags =~ m/c/) {
3083                 $flags_string = "Cleanup";
3084               }
3085               if ($flags =~ m/r/) {
3086                 if ($flags_string) { $flags_string .= " / "; }
3087                 $flags_string .= "No Recursion";
3088               }
3089               if ($flags =~ m/d/) {
3090                 if ($flags_string) { $flags_string .= " / "; }
3091                 $flags_string .= "Has Details";
3092               }
3093               if ($flags =~ m/a/) {
3094                 if ($flags_string) { $flags_string .= " / "; }
3095                 $flags_string .= "Action";
3096               }
3097               if ($flags =~ m/h/) {
3098                 if ($flags_string) { $flags_string .= " / "; }
3099                 $flags_string .= "No Hooks";
3100               }
3101             }
3103             if ($flags_string)
3104               {
3105                 $synop .= ": $flags_string\n";
3107                 $pad = ' ' x (5 + $name_len - length("user_data"));
3108                 $desc  .= "$pad : $flags_string</programlisting>\n";
3109               }
3110             else
3111               {
3112                 $synop .= "\n";
3113                 $desc  .= "</programlisting>\n";
3114               }
3116             $desc .= &MakeDeprecationNote($symbol);
3118             my $parameters = &OutputParamDescriptions ("SIGNAL", $symbol);
3119             my $parameters_output = 0;
3121             $AllSymbols{$symbol} = 1;
3122             if (defined ($SymbolDocs{$symbol})) {
3123                 my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3125                 # Try to insert the parameter table at the author's desired
3126                 # position. Otherwise we need to tag it onto the end.
3127                 if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
3128                   $parameters_output = 1;
3129                 }
3130                 $desc .= $symbol_docs;
3132                 if (!IsEmptyDoc($SymbolDocs{$symbol})) {
3133                     $AllDocumentedSymbols{$symbol} = 1;
3134                 }
3135             }
3137             if ($parameters_output == 0) {
3138                 $desc .= $parameters;
3139               }
3140             $desc .= OutputSymbolTraits ($symbol);
3141             $desc .= "</refsect2>";
3142         }
3143     }
3144     return ($synop, $desc);
3148 #############################################################################
3149 # Function    : GetArgs
3150 # Description : Returns the synopsis and detailed description DocBook output
3151 #               for the Args of a given GtkObject subclass.
3152 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
3153 #############################################################################
3155 sub GetArgs {
3156     my ($object) = @_;
3157     my $synop = "";
3158     my $desc = "";
3159     my $child_synop = "";
3160     my $child_desc = "";
3161     my $style_synop = "";
3162     my $style_desc = "";
3164     my $i;
3165     for ($i = 0; $i <= $#ArgObjects; $i++) {
3166         if ($ArgObjects[$i] eq $object) {
3167             #print "Found arg: $ArgNames[$i]\n";
3168             my $name = $ArgNames[$i];
3169             my $flags = $ArgFlags[$i];
3170             my $flags_string = "";
3171             my $kind = "";
3172             my $id_sep = "";
3174             if ($flags =~ m/c/) {
3175                 $kind = "child property";
3176                 $id_sep = "c-";
3177             }
3178             elsif ($flags =~ m/s/) {
3179                 $kind = "style property";
3180                 $id_sep = "s-";
3181             }
3182             else {
3183                 $kind = "property";
3184             }
3186             # Remember only one colon so we don't clash with signals.
3187             my $symbol = "${object}:${name}";
3188             # use two dashes and ev. an extra separator here for the same reason.
3189             my $id = &CreateValidSGMLID ("$object--$id_sep$name");
3191             my $type = $ArgTypes[$i];
3192             my $type_output;
3193             my $range = $ArgRanges[$i];
3194             my $range_output = CreateValidSGML ($range);
3195             my $default = $ArgDefaults[$i];
3196             my $default_output = CreateValidSGML ($default);
3198             if ($type eq "GtkString") {
3199                 $type = "char*";
3200             }
3201             if ($type eq "GtkSignal") {
3202                 $type = "GtkSignalFunc, gpointer";
3203                 $type_output = &MakeXRef ("GtkSignalFunc") . ", "
3204                     . &MakeXRef ("gpointer");
3205             } elsif ($type =~ m/^(\w+)\*$/) {
3206                 $type_output = &MakeXRef ($1) . "*";
3207             } else {
3208                 $type_output = &MakeXRef ($type);
3209             }
3211             if ($flags =~ m/r/) {
3212                 $flags_string = "Read";
3213             }
3214             if ($flags =~ m/w/) {
3215                 if ($flags_string) { $flags_string .= " / "; }
3216                 $flags_string .= "Write";
3217             }
3218             if ($flags =~ m/x/) {
3219                 if ($flags_string) { $flags_string .= " / "; }
3220                 $flags_string .= "Construct";
3221             }
3222             if ($flags =~ m/X/) {
3223                 if ($flags_string) { $flags_string .= " / "; }
3224                 $flags_string .= "Construct Only";
3225             }
3227             $AllSymbols{$symbol} = 1;
3228             my $blurb;
3229             if (defined($SymbolDocs{$symbol}) &&
3230                 !IsEmptyDoc($SymbolDocs{$symbol})) {
3231                 $blurb = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3232                 #print ".. [$SymbolDocs{$symbol}][$blurb]\n";
3233                 $AllDocumentedSymbols{$symbol} = 1;
3234             }
3235             else {
3236                 if (!($ArgBlurbs[$i] eq "")) {
3237                     $AllDocumentedSymbols{$symbol} = 1;
3238                 } else {
3239                     # FIXME: print a warning?
3240                     #print ".. no description\n";
3241                 }
3242                 $blurb = "<para>" . &CreateValidSGML ($ArgBlurbs[$i]) . "</para>";
3243             }
3245             my $pad1 = " " x (24 - length ($name));
3246             my $pad2 = " " x (20 - length ($type));
3248             my $arg_synop = "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad1 $type_output $pad2 : $flags_string\n";
3249             my $arg_desc = "<refsect2 id=\"$id\" role=\"property\"><title>The <literal>&quot;$name&quot;</literal> $kind</title>\n";
3250             $arg_desc .= MakeIndexterms($symbol, $id);
3251             $arg_desc .= "\n";
3252             $arg_desc .= OutputSymbolExtraLinks($symbol);
3254             $arg_desc .= "<programlisting>  &quot;$name&quot;$pad1 $type_output $pad2 : $flags_string</programlisting>\n";
3255             $arg_desc .= &MakeDeprecationNote($symbol);
3256             $arg_desc .= $blurb;
3257             if ($range ne "") {
3258                 $arg_desc .= "<para>Allowed values: $range_output</para>\n";
3259             }
3260             if ($default ne "") {
3261                 $arg_desc .= "<para>Default value: $default_output</para>\n";
3262             }
3263             $arg_desc .= OutputSymbolTraits ($symbol);
3264             $arg_desc .= "</refsect2>\n";
3266             if ($flags =~ m/c/) {
3267                 $child_synop .= $arg_synop;
3268                 $child_desc .= $arg_desc;
3269             }
3270             elsif ($flags =~ m/s/) {
3271                 $style_synop .= $arg_synop;
3272                 $style_desc .= $arg_desc;
3273             }
3274             else {
3275                 $synop .= $arg_synop;
3276                 $desc .= $arg_desc;
3277             }
3278         }
3279     }
3280     return ($synop, $child_synop, $style_synop, $desc, $child_desc, $style_desc);
3284 #############################################################################
3285 # Function    : ReadSourceDocumentation
3286 # Description : This reads in the documentation embedded in comment blocks
3287 #               in the source code (for Gnome).
3289 #               Parameter descriptions override any in the template files.
3290 #               Function descriptions are placed before any description from
3291 #               the template files.
3293 #               It recursively descends the source directory looking for .c
3294 #               files and scans them looking for specially-formatted comment
3295 #               blocks.
3297 # Arguments   : $source_dir - the directory to scan.
3298 #############m###############################################################
3300 sub ReadSourceDocumentation {
3301     my ($source_dir) = @_;
3302     my ($file, $dir, @suffix_list, $suffix);
3303     #print "Scanning source directory: $source_dir\n";
3305     # This array holds any subdirectories found.
3306     my (@subdirs) = ();
3308     @suffix_list = split (/,/, $SOURCE_SUFFIXES);
3310     opendir (SRCDIR, $source_dir)
3311         || die "Can't open source directory $source_dir: $!";
3313     foreach $file (readdir (SRCDIR)) {
3314       if ($file =~ /^\./) {
3315         next;
3316       } elsif (-d "$source_dir/$file") {
3317         push (@subdirs, $file);
3318       } elsif (@suffix_list) {
3319         foreach $suffix (@suffix_list) {
3320           if ($file =~ m/\.\Q${suffix}\E$/) {
3321             &ScanSourceFile ("$source_dir/$file");
3322           }
3323         }
3324       } elsif ($file =~ m/\.[ch]$/) {
3325         &ScanSourceFile ("$source_dir/$file");
3326       }
3327     }
3328     closedir (SRCDIR);
3330     # Now recursively scan the subdirectories.
3331     foreach $dir (@subdirs) {
3332         next if ($IGNORE_FILES =~ m/(\s|^)\Q${dir}\E(\s|$)/);
3333         &ReadSourceDocumentation ("$source_dir/$dir");
3334     }
3338 #############################################################################
3339 # Function    : ScanSourceFile
3340 # Description : Scans one source file looking for specially-formatted comment
3341 #               blocks. Later &MergeSourceDocumentation is used to merge any
3342 #               documentation found with the documentation already read in
3343 #               from the template files.
3345 # Arguments   : $file - the file to scan.
3346 #############################################################################
3348 sub ScanSourceFile {
3349     my ($file) = @_;
3350     my $basename;
3352     if ($file =~ m/^.*[\/\\]([^\/\\]*)$/) {
3353         $basename = $1;
3354     } else {
3355         &LogWarning ($file, 1, "Can't find basename for this filename.");
3356         $basename = $file;
3357     }
3359     # Check if the basename is in the list of files to ignore.
3360     if ($IGNORE_FILES =~ m/(\s|^)\Q${basename}\E(\s|$)/) {
3361         return;
3362     }
3364     #print "DEBUG: Scanning $file\n";
3366     open (SRCFILE, $file)
3367         || die "Can't open $file: $!";
3368     my $in_comment_block = 0;
3369     my $symbol;
3370     my ($in_description, $in_return, $in_since, $in_stability, $in_deprecated);
3371     my ($description, $return_desc, $return_start, $return_style);
3372     my ($since_desc, $stability_desc, $deprecated_desc);
3373     my $current_param;
3374     my $ignore_broken_returns;
3375     my @params;
3376     while (<SRCFILE>) {
3377         # Look for the start of a comment block.
3378         if (!$in_comment_block) {
3379             if (m%^\s*/\*.*\*/%) {
3380                 #one-line comment - not gtkdoc
3381             } elsif (m%^\s*/\*\*\s%) {
3382                 #print "Found comment block start\n";
3384                 $in_comment_block = 1;
3386                 # Reset all the symbol data.
3387                 $symbol = "";
3388                 $in_description = 0;
3389                 $in_return = 0;
3390                 $in_since = 0;
3391                 $in_deprecated = 0;
3392                 $in_stability = 0;
3393                 $description = "";
3394                 $return_desc = "";
3395                 $return_style = "";
3396                 $since_desc = "";
3397                 $deprecated_desc = "";
3398                 $stability_desc = "";
3399                 $current_param = -1;
3400                 $ignore_broken_returns = 0;
3401                 @params = ();
3402             }
3403             next;
3404         }
3406         # We're in a comment block. Check if we've found the end of it.
3407         if (m%^\s*\*+/%) {
3408             if (!$symbol) {
3409                 # maybe its not even meant to be a gtk-doc comment?
3410                 &LogWarning ($file, $., "Symbol name not found at the start of the comment block.");
3411             } else {
3412                 # Add the return value description onto the end of the params.
3413                 if ($return_desc) {
3414                     push (@params, "Returns");
3415                     push (@params, $return_desc);
3416                     if ($return_style eq 'broken') {
3417                         &LogWarning ($file, $., "Free-form return value description in $symbol. Use `Returns:' to avoid ambiguities.");
3418                     }
3419                 }
3420                 # Convert special SGML characters
3421                 $description = &ConvertSGMLChars ($symbol, $description);
3422                 my $k;
3423                 for ($k = 1; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3424                     $params[$k] = &ConvertSGMLChars ($symbol, $params[$k]);
3425                 }
3427                 # Handle Section docs
3428                 if ($symbol =~ m/SECTION:\s*(.*)/) {
3429                     my $real_symbol=$1;
3430                     my $key;
3432                     if (scalar %KnownSymbols) {
3433                         if ((! defined($KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"})) || $KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"} != 1) {
3434                             &LogWarning ($file, $., "Section $real_symbol is not defined in the $MODULE-section.txt file.");
3435                         }
3436                     }
3438                     #print "SECTION DOCS found in source for : '$real_symbol'\n";
3439                     $ignore_broken_returns = 1;
3440                     for ($k = 0; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3441                         #print "   '".$params[$k]."'\n";
3442                         $params[$k] = "\L$params[$k]";
3443                         undef $key;
3444                         if ($params[$k] eq "short_description") {
3445                             $key = "$TMPL_DIR/$real_symbol:Short_Description";
3446                         } elsif ($params[$k] eq "see_also") {
3447                             $key = "$TMPL_DIR/$real_symbol:See_Also";
3448                         } elsif ($params[$k] eq "title") {
3449                             $key = "$TMPL_DIR/$real_symbol:Title";
3450                         } elsif ($params[$k] eq "stability") {
3451                             $key = "$TMPL_DIR/$real_symbol:Stability_Level";
3452                         } elsif ($params[$k] eq "section_id") {
3453                             $key = "$TMPL_DIR/$real_symbol:Section_Id";
3454                         } elsif ($params[$k] eq "include") {
3455                             $key = "$TMPL_DIR/$real_symbol:Include";
3456                         }
3457                         if (defined($key)) {
3458                             $SourceSymbolDocs{$key}=$params[$k+1];
3459                             $SourceSymbolSourceFile{$key} = $file;
3460                             $SourceSymbolSourceLine{$key} = $.;
3461                         }
3462                     }
3463                     $SourceSymbolDocs{"$TMPL_DIR/$real_symbol:Long_Description"}=$description;
3464                     $SourceSymbolSourceFile{"$TMPL_DIR/$real_symbol:Long_Description"} = $file;
3465                     $SourceSymbolSourceLine{"$TMPL_DIR/$real_symbol:Long_Description"} = $.;
3466                     #$SourceSymbolTypes{$symbol} = "SECTION";
3467                 } else {
3468                     #print "SYMBOL DOCS found in source for : '$symbol' ",length($description), "\n";
3469                     $SourceSymbolDocs{$symbol} = $description;
3470                     $SourceSymbolParams{$symbol} = [ @params ];
3471                     # FIXME $SourceSymbolTypes{$symbol} = "STRUCT,SIGNAL,ARG,FUNCTION,MACRO";
3472                     #if (defined $DeclarationTypes{$symbol}) {
3473                     #    $SourceSymbolTypes{$symbol} = $DeclarationTypes{$symbol}
3474                     #}
3475                     $SourceSymbolSourceFile{$symbol} = $file;
3476                     $SourceSymbolSourceLine{$symbol} = $.;
3477                 }
3479                 if ($since_desc) {
3480                      ($since_desc, my @extra_lines) = split ("\n", $since_desc);
3481                      $since_desc =~ s/^\s+//;
3482                      $since_desc =~ s/\s+$//;
3483                      #print "Since($symbol) : [$since_desc]\n";
3484                      $Since{$symbol} = &ConvertSGMLChars ($symbol, $since_desc);
3485                      if(scalar @extra_lines) {
3486                          &LogWarning ($file, $., "multi-line since docs found");
3487                      }
3488                 }
3490                 if ($stability_desc) {
3491                     $stability_desc = &ParseStabilityLevel($stability_desc, $file, $., "Stability level for $symbol");
3492                     $StabilityLevel{$symbol} = &ConvertSGMLChars ($symbol, $stability_desc);
3493                 }
3495                 if ($deprecated_desc) {
3496                     if (exists $Deprecated{$symbol}) {
3497                     }
3498                     else {
3499                          # don't warn for signals and properties
3500                          #if ($symbol !~ m/::?(.*)/) {
3501                          if (defined $DeclarationTypes{$symbol}) {
3502                              &LogWarning ($file, $., 
3503                                  "$symbol is deprecated in the inline comments, but no deprecation guards were found around the declaration.".
3504                                  " (See the --deprecated-guards option for gtkdoc-scan.)");
3505                          }
3506                     }
3507                     $Deprecated{$symbol} = &ConvertSGMLChars ($symbol, $deprecated_desc);
3508                 }
3509             }
3511             $in_comment_block = 0;
3512             next;
3513         }
3515         # Get rid of ' * ' at start of every line in the comment block.
3516         s%^\s*\*\s?%%;
3517         # But make sure we don't get rid of the newline at the end.
3518         if (!$_) {
3519             $_ = "\n";
3520         }
3521         #print "DEBUG: scanning :$_";
3523         # If we haven't found the symbol name yet, look for it.
3524         if (!$symbol) {
3525             if (m%^\s*(SECTION:\s*\S+)%) {
3526                 $symbol = $1;
3527                 #print "SECTION DOCS found in source for : '$symbol'\n";
3528                 $ignore_broken_returns = 1;
3529             } elsif (m%^\s*([\w:-]*\w)\s*:?\s*$%) {
3530                 $symbol = $1;
3531                 #print "SYMBOL DOCS found in source for : '$symbol'\n";
3532             }
3533             next;
3534         }
3536         # If we're in the return value description, add it to the end.
3537         if ($in_return) {
3538             # If we find another valid returns line, we assume that the first
3539             # one was really part of the description.
3540             if (m/^\s*(returns:|return\s+value:)/i) {
3541                 if ($return_style eq 'broken') {
3542                     $description .= $return_start . $return_desc;
3543                 }
3544                 $return_start = $1;
3545                 if ($return_style eq 'sane') {
3546                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3547                 }
3548                 $return_style = 'sane';
3549                 $ignore_broken_returns = 1;
3550                 $return_desc = $';
3551             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3552                 $description .= $return_start . $return_desc;
3553                 $return_start = $1;
3554                 $return_style = 'broken';
3555                 $return_desc = $';
3556             } elsif (m%^\s*since:%i) {
3557                 $since_desc = $';
3558                 $in_since = 1;
3559                 $in_return = 0;
3560             } elsif (m%^\s*stability:%i) {
3561                 $stability_desc = $';
3562                 $in_stability = 1;
3563                 $in_return = 0;
3564             } elsif (m%^\s*deprecated:%i) {
3565                 $deprecated_desc = $';
3566                 $in_deprecated = 1;
3567                 $in_return = 0;
3568             } else {
3569                 $return_desc .= $_;
3570             }
3571             next;
3572         }
3574         if ($in_since) {
3575             if (m/^\s*(returns:|return\s+value:)/i) {
3576                 if ($return_style eq 'broken') {
3577                     $description .= $return_start . $return_desc;
3578                 }
3579                 $return_start = $1;
3580                 if ($return_style eq 'sane') {
3581                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3582                 }
3583                 $return_style = 'sane';
3584                 $ignore_broken_returns = 1;
3585                 $return_desc = $';
3586                 $in_return = 1;
3587                 $in_since = 0;
3588             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3589                 $return_start = $1;
3590                 $return_style = 'broken';
3591                 $return_desc = $';
3592                 $in_return = 1;
3593                 $in_since = 0;
3594             } elsif (m%^\s*deprecated:%i) {
3595                 $deprecated_desc = $';
3596                 $in_deprecated = 1;
3597                 $in_since = 0;
3598             } elsif (m%^\s*stability:%i) {
3599                 $stability_desc = $';
3600                 $in_stability = 1;
3601                 $in_since = 0;
3602             } else {
3603                 $since_desc .= $_;
3604             }
3605             next;
3606         }
3608         if ($in_stability) {
3609             if (m/^\s*(returns:|return\s+value:)/i) {
3610                 if ($return_style eq 'broken') {
3611                     $description .= $return_start . $return_desc;
3612                 }
3613                 $return_start = $1;
3614                 if ($return_style eq 'sane') {
3615                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3616                 }
3617                 $return_style = 'sane';
3618                 $ignore_broken_returns = 1;
3619                 $return_desc = $';
3620                 $in_return = 1;
3621                 $in_stability = 0;
3622             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3623                 $return_start = $1;
3624                 $return_style = 'broken';
3625                 $return_desc = $';
3626                 $in_return = 1;
3627                 $in_stability = 0;
3628             } elsif (m%^\s*deprecated:%i) {
3629                 $deprecated_desc = $';
3630                 $in_deprecated = 1;
3631                 $in_stability = 0;
3632             } elsif (m%^\s*since:%i) {
3633                 $since_desc = $';
3634                 $in_since = 1;
3635                 $in_stability = 0;
3636             } else {
3637                 $stability_desc .= $_;
3638             }
3639             next;
3640         }
3642         if ($in_deprecated) {
3643             if (m/^\s*(returns:|return\s+value:)/i) {
3644                 if ($return_style eq 'broken') {
3645                     $description .= $return_start . $return_desc;
3646                 }
3647                 $return_start = $1;
3648                 if ($return_style eq 'sane') {
3649                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3650                 }
3651                 $return_style = 'sane';
3652                 $ignore_broken_returns = 1;
3653                 $return_desc = $';
3654                 $in_return = 1;
3655                 $in_deprecated = 0;
3656             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3657                 $return_start = $1;
3658                 $return_style = 'broken';
3659                 $return_desc = $';
3660                 $in_return = 1;
3661                 $in_deprecated = 0;
3662             } elsif (m%^\s*since:%i) {
3663                 $since_desc = $';
3664                 $in_since = 1;
3665                 $in_deprecated = 0;
3666             } elsif (m%^\s*stability:%i) {
3667                 $stability_desc = $';
3668                 $in_stability = 1;
3669                 $in_deprecated = 0;
3670             } else {
3671                 $deprecated_desc .= $_;
3672             }
3673             next;
3674         }
3676         # If we're in the description part, check for the 'Returns:' line.
3677         # If that isn't found, add the text to the end.
3678         if ($in_description) {
3679             # Get rid of 'Description:'
3680             s%^\s*Description:%%;
3682             if (m/^\s*(returns:|return\s+value:)/i) {
3683                 if ($return_style eq 'broken') {
3684                     $description .= $return_start . $return_desc;
3685                 }
3686                 $return_start = $1;
3687                 if ($return_style eq 'sane') {
3688                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3689                 }
3690                 $return_style = 'sane';
3691                 $ignore_broken_returns = 1;
3692                 $return_desc = $';
3693                 $in_return = 1;
3694                 next;
3695             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3696                 $return_start = $1;
3697                 $return_style = 'broken';
3698                 $return_desc = $';
3699                 $in_return = 1;
3700                 next;
3701             } elsif (m%^\s*since:%i) {
3702                 $since_desc = $';
3703                 $in_since = 1;
3704                 next;
3705             } elsif (m%^\s*deprecated:%i) {
3706                 $deprecated_desc = $';
3707                 $in_deprecated = 1;
3708                 next;
3709             } elsif (m%^\s*stability:%i) {
3710                 $stability_desc = $';
3711                 $in_stability = 1;
3712                 next;
3713             }
3715             $description .= $_;
3716             next;
3717         }
3719         # We must be in the parameters. Check for the empty line below them.
3720         if (m%^\s*$%) {
3721             $in_description = 1;
3722             next;
3723         }
3725         # Look for a parameter name.
3726         if (m%^\s*@(\S+)\s*:%) {
3727             my $param_name = $1;
3728             my $param_desc = $';
3729             #print "Found parameter: $param_name\n";
3730             # Allow '...' as the Varargs parameter.
3731             if ($param_name eq "...") {
3732                 $param_name = "Varargs";
3733             }
3734             if ("\L$param_name" eq "returns") {
3735                 $return_style = 'sane';
3736                 $ignore_broken_returns = 1;
3737             }
3738             push (@params, $param_name);
3739             push (@params, $param_desc);
3740             $current_param += $PARAM_FIELD_COUNT;
3741             next;
3742         }
3744         # We must be in the middle of a parameter description, so add it on
3745         # to the last element in @params.
3746         if ($current_param == -1) {
3747             &LogWarning ($file, $., "Parsing comment block file : parameter expected.");
3748         } else {
3749             $params[$#params] .= $_;
3750         }
3751     }
3752     close (SRCFILE);
3755 #############################################################################
3756 # Function    : OutputMissingDocumentation
3757 # Description : Outputs report of documentation coverage to a file
3759 # Arguments   : none
3760 #############################################################################
3762 sub OutputMissingDocumentation {
3763     my $n_documented = 0;
3764     my $n_incomplete = 0;
3765     my $total = 0;
3766     my $symbol;
3767     my $percent;
3768     my $msg;
3769     my $buffer = "";
3770     my $buffer_deprecated = "";
3771     my $buffer_descriptions = "";
3772     
3773     open (UNDOCUMENTED, ">$ROOT_DIR/$MODULE-undocumented.txt")
3774       || die "Can't create $ROOT_DIR/$MODULE-undocumented.txt: $!";
3775     
3776     foreach $symbol (sort (keys (%AllSymbols))) {
3777         # FIXME: should we print LogWarnings for undocumented stuff?
3778         # DEBUG
3779         #my $ssfile = &GetSymbolSourceFile($symbol);
3780         #my $ssline = &GetSymbolSourceLine($symbol);
3781         #my $location = "defined at " . (defined($ssfile)?$ssfile:"?") . ":" . (defined($ssline)?$ssline:"0") . "\n";
3782         # DEBUG
3783         if ($symbol !~ /:(Title|Long_Description|Short_Description|See_Also|Stability_Level|Include|Section_Id)/) {
3784             $total++;
3785             if (exists ($AllDocumentedSymbols{$symbol})) {
3786                 $n_documented++;
3787                 if (exists ($AllIncompleteSymbols{$symbol})) {
3788                     $n_incomplete++;
3789                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3790                     #$buffer .= "\t0: ".$location;
3791                 }
3792             } elsif (exists $Deprecated{$symbol}) {
3793                 if (exists ($AllIncompleteSymbols{$symbol})) {
3794                     $n_incomplete++;
3795                     $buffer_deprecated .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3796                     #$buffer .= "\t1a: ".$location;
3797                 } else {
3798                     $buffer_deprecated .= $symbol . "\n";
3799                     #$buffer .= "\t1b: ".$location;
3800                 }
3801             } else {
3802                 if (exists ($AllIncompleteSymbols{$symbol})) {
3803                     $n_incomplete++;
3804                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3805                     #$buffer .= "\t2a: ".$location;
3806                 } else {
3807                     $buffer .= $symbol . "\n";
3808                     #$buffer .= "\t2b: ".$location;
3809                 }
3810             }
3811         } elsif ($symbol =~ /:(Long_Description|Short_Description)/) {
3812             $total++;
3813             #my $len1=(exists($SymbolDocs{$symbol}))?length($SymbolDocs{$symbol}):-1;
3814             #my $len2=(exists($AllDocumentedSymbols{$symbol}))?length($AllDocumentedSymbols{$symbol}):-1;
3815             #print "%%%% $symbol : $len1,$len2\n";
3816             if (((exists ($SymbolDocs{$symbol})) && (length ($SymbolDocs{$symbol}) > 0))
3817             || ((exists ($AllDocumentedSymbols{$symbol})) && (length ($AllDocumentedSymbols{$symbol}) > 0))) {
3818               $n_documented++;
3819             } else {
3820               # cut off the leading namespace ($TMPL_DIR)
3821               $symbol =~ m/^.*\/(.*)$/;
3822               $buffer_descriptions .= $1 . "\n";
3823             }
3824         }
3825     }
3826     
3827     $buffer .= "\n" . $buffer_deprecated . "\n" . $buffer_descriptions;
3828     
3829     if ($total == 0) {
3830       $percent = 100;
3831     } else {
3832       $percent = ($n_documented / $total) * 100.0;
3833     }
3834     
3835     printf UNDOCUMENTED "%.0f%% symbol docs coverage.\n", $percent;
3836     print UNDOCUMENTED "$n_documented symbols documented.\n";
3837     print UNDOCUMENTED "$n_incomplete symbols incomplete.\n";
3838     print UNDOCUMENTED ($total - $n_documented) . " not documented.\n\n\n";
3839     
3840     print UNDOCUMENTED $buffer;
3841     
3842     close (UNDOCUMENTED);
3843     
3844     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);
3848 #############################################################################
3849 # Function    : OutputUndeclaredSymbols
3850 # Description : Outputs symbols that are undeclared yet documented to a file
3852 # Arguments   : none
3853 #############################################################################
3855 sub OutputUndeclaredSymbols {
3856     open(UNDECLARED, ">$ROOT_DIR/$MODULE-undeclared.txt")
3857         || die "Can't create $ROOT_DIR/$MODULE-undeclared.txt";
3859     if (%UndeclaredSymbols) {
3860         print UNDECLARED (join("\n", sort keys %UndeclaredSymbols));
3861         print UNDECLARED "\n";
3862         print "See $MODULE-undeclared.txt for the list of undeclared symbols.\n"
3863     }
3864     close(UNDECLARED);
3868 #############################################################################
3869 # Function    : OutputAllSymbols
3870 # Description : Outputs list of all symbols to a file
3872 # Arguments   : none
3873 #############################################################################
3875 sub OutputAllSymbols {
3876      my $n_documented = 0;
3877      my $total = 0;
3878      my $symbol;
3879      my $percent;
3880      my $msg;
3882      open (SYMBOLS, ">$ROOT_DIR/$MODULE-symbols.txt")
3883           || die "Can't create $ROOT_DIR/$MODULE-symbols.txt: $!";
3885      foreach $symbol (sort (keys (%AllSymbols))) {
3886           print SYMBOLS $symbol . "\n";
3887      }
3889      close (SYMBOLS);
3892 #############################################################################
3893 # Function    : OutputSymbolsWithoutSince
3894 # Description : Outputs list of all symbols without a since tag to a file
3896 # Arguments   : none
3897 #############################################################################
3899 sub OutputSymbolsWithoutSince {
3900      my $n_documented = 0;
3901      my $total = 0;
3902      my $symbol;
3903      my $percent;
3904      my $msg;
3906      open (SYMBOLS, ">$ROOT_DIR/$MODULE-nosince.txt")
3907           || die "Can't create $ROOT_DIR/$MODULE-nosince.txt: $!";
3909      foreach $symbol (sort (keys (%SourceSymbolDocs))) {
3910          if (!defined $Since{$symbol}) {
3911              print SYMBOLS $symbol . "\n";
3912          }
3913      }
3915      close (SYMBOLS);
3919 #############################################################################
3920 # Function    : MergeSourceDocumentation
3921 # Description : This merges documentation read from a source file into the
3922 #               documentation read in from a template file.
3924 #               Parameter descriptions override any in the template files.
3925 #               Function descriptions are placed before any description from
3926 #               the template files.
3928 # Arguments   : none
3929 #############################################################################
3931 sub MergeSourceDocumentation {
3932     my $symbol;
3933     my @Symbols;
3935     if (scalar %SymbolDocs) {
3936         @Symbols=keys (%SymbolDocs);
3937         #print "num existing entries: ".(scalar @Symbols)."\n";
3938         #print "  ",$Symbols[0], " ",$Symbols[1],"\n";
3939     }
3940     else {
3941         # filter scanned declarations, with what we suppress from -sections.txt
3942         my %tmp = ();
3943         foreach $symbol (keys (%Declarations)) {
3944             if (defined($KnownSymbols{$symbol}) && $KnownSymbols{$symbol} == 1) {
3945                 $tmp{$symbol}=1;
3946             }
3947         }
3948         # , add the rest from -sections.txt
3949         foreach $symbol (keys (%KnownSymbols)) {
3950             if ($KnownSymbols{$symbol} == 1) {
3951                 $tmp{$symbol}=1;
3952             }
3953         }
3954         # and add whats found in the source
3955         foreach $symbol (keys (%SourceSymbolDocs)) {
3956             $tmp{$symbol}=1;
3957         }
3958         @Symbols = keys (%tmp);
3959         #print "num source entries: ".(scalar @Symbols)."\n";
3960     }
3961     foreach $symbol (@Symbols) {
3962         $AllSymbols{$symbol} = 1;
3964         my $have_tmpl_docs = 0;
3966         ## See if the symbol is documented in template
3967         my $tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
3968         my $check_tmpl_doc =$tmpl_doc;
3969         # remove all xml-tags and whitespaces
3970         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
3971         $check_tmpl_doc =~ s/<.*?>//g;
3972         $check_tmpl_doc =~ s/\s//g;
3973         # anything left ?
3974         if ($check_tmpl_doc ne "") {
3975             $have_tmpl_docs = 1;
3976             #print "## [$check_tmpl_doc]\n";
3977         } else {
3978             $tmpl_doc = "";
3979         }
3981         if (exists ($SourceSymbolDocs{$symbol})) {
3982             my $type = $DeclarationTypes {$symbol};
3984             #print "merging [$symbol] from source\n";
3986             my $item = "Parameter";
3987             if (defined ($type)) {
3988                 if ($type eq 'STRUCT') {
3989                     $item = "Field";
3990                 } elsif ($type eq 'ENUM') {
3991                     $item = "Value";
3992                 } elsif ($type eq 'UNION') {
3993                     $item = "Field";
3994                 }
3995             } else {
3996                 $type="SIGNAL";
3997             }
3999             my $src_doc = $SourceSymbolDocs{$symbol};
4000             # remove leading and training whitespaces
4001             $src_doc =~ s/^\s+//;
4002             $src_doc =~ s/\s+$//;
4004             # Don't output warnings for overridden titles as titles are
4005             # automatically generated in the -sections.txt file, and thus they
4006             # are often overridden.
4007             if ($have_tmpl_docs && $symbol !~ m/:Title$/) {
4008                 # check if content is different
4009                 if ($tmpl_doc ne $src_doc) {
4010                     #print "[$tmpl_doc] [$src_doc]\n";
4011                     &LogWarning ($SourceSymbolSourceFile{$symbol}, $SourceSymbolSourceLine{$symbol},
4012                         "Documentation in template ".$SymbolSourceFile{$symbol}.":".$SymbolSourceLine{$symbol}." for $symbol being overridden by inline comments.");
4013                 }
4014             }
4016             if ($src_doc ne "") {
4017                  $AllDocumentedSymbols{$symbol} = 1;
4018             }
4020             # Convert <!--PARAMETERS--> with any blank lines around it to
4021             # a </para> followed by <!--PARAMETERS--> followed by <para>.
4022             $src_doc =~ s%\n+\s*<!--PARAMETERS-->\s*\n+%\n</para>\n<!--PARAMETERS-->\n<para>\n%g;
4024             # If there is a blank line, finish the paragraph and start another.
4025             $src_doc = &ConvertBlankLines ($src_doc, $symbol);
4026             # Do not add <para> to nothing, it breaks missing docs checks.
4027             my $src_doc_para = "";
4028             if ($src_doc) {
4029                 $src_doc_para = "<para>\n$src_doc</para>\n";
4030                 #print "$symbol : [$src_doc][$src_doc_para]\n";
4031             }
4033             if ($symbol =~ m/$TMPL_DIR\/.+:Long_Description/) {
4034                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4035             } elsif ($symbol =~ m/$TMPL_DIR\/.+:.+/) {
4036                 # For the title/summary/see also section docs we don't want to
4037                 # add any <para> tags.
4038                 $SymbolDocs{$symbol} = "$src_doc"
4039             } else {
4040                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4041             }
4043             # merge parameters
4044             if ($symbol =~ m/.*::.*/) {
4045                 # For signals we prefer the param names from the source docs,
4046                 # since the ones from the templates are likely to contain the
4047                 # artificial argn names which are generated by gtkdoc-scangobj.
4048                 $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4049                 # FIXME: we need to check for empty docs here as well!
4050             } else {
4051                 # The templates contain the definitive parameter names and order,
4052                 # so we will not change that. We only override the actual text.
4053                 my $tmpl_params = $SymbolParams{$symbol};
4054                 if (!defined ($tmpl_params)) {
4055                     #print "No merge needed for $symbol\n";
4056                     $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4057                     #  FIXME: we still like to get the number of params and merge
4058                     #  1) we would noticed that params have been removed/renamed
4059                     #  2) we would catch undocumented params
4060                     #  params are not (yet) exported in -decl.txt so that we
4061                     #  could easily grab them :/
4062                 } else {
4063                     my $params = $SourceSymbolParams{$symbol};
4064                     my $j;
4065                     #print "Merge needed for $symbol, tmpl_params: ",$#$tmpl_params,", source_params: ",$#$params," \n";
4066                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4067                         my $tmpl_param_name = $$tmpl_params[$j];
4069                         # Allow '...' as the Varargs parameter.
4070                         if ($tmpl_param_name eq "...") {
4071                             $tmpl_param_name = "Varargs";
4072                         }
4074                         # Try to find the param in the source comment documentation.
4075                         my $found = 0;
4076                         my $k;
4077                         #print "  try merge param $tmpl_param_name\n";
4078                         for ($k = 0; $k <= $#$params; $k += $PARAM_FIELD_COUNT) {
4079                             my $param_name = $$params[$k];
4080                             my $param_desc = $$params[$k + 1];
4082                             #print "    test param  $param_name\n";
4083                             # We accept changes in case, since the Gnome source
4084                             # docs contain a lot of these.
4085                             if ("\L$param_name" eq "\L$tmpl_param_name") {
4086                                 $found = 1;
4088                                 # Override the description.
4089                                 $$tmpl_params[$j + 1] = $param_desc;
4091                                 # Set the name to "" to mark it as used.
4092                                 $$params[$k] = "";
4093                                 last;
4094                             }
4095                         }
4097                         # If it looks like the parameters are there, but not
4098                         # in the right place, try to explain a bit better.
4099                         if ((!$found) && ($src_doc =~ m/\@$tmpl_param_name:/)) {
4100                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4101                                 "Parameters for $symbol must start on the line immediately after the function or macro name.");
4102                         }
4103                     }
4105                     # Now we output a warning if parameters have been described which
4106                     # do not exist.
4107                     for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
4108                         my $param_name = $$params[$j];
4109                         if ($param_name) {
4110                             # the template builder cannot detect if a macro returns
4111                             # a result or not
4112                             if(($type eq "MACRO") && ($param_name eq "Returns")) {
4113                                 # FIXME: do we need to add it then to tmpl_params[] ?
4114                                 my $num=$#$tmpl_params;
4115                                 #print "  adding Returns: to macro docs for $symbol.\n";
4116                                 $$tmpl_params[$num+1]="Returns";
4117                                 $$tmpl_params[$num+2]=$$params[$j+1];
4118                                 next;
4119                             }
4120                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4121                                 "$item described in source code comment block but does not exist. $type: $symbol $item: $param_name.");
4122                         }
4123                     }
4124                 }
4125             }
4126         } else {
4127             if ($have_tmpl_docs) {
4128                 $AllDocumentedSymbols{$symbol} = 1;
4129                 #print "merging [$symbol] from template\n";
4130             }
4131             else {
4132                 #print "[$symbol] undocumented\n";
4133             }
4134         }
4136         # if this symbol is documented, check if docs are complete
4137         $check_tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
4138         # remove all xml-tags and whitespaces
4139         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
4140         $check_tmpl_doc =~ s/<.*?>//g;
4141         $check_tmpl_doc =~ s/\s//g;
4142         if ($check_tmpl_doc ne "") {
4143             my $tmpl_params = $SymbolParams{$symbol};
4144             if (defined ($tmpl_params)) {
4145                 my $type = $DeclarationTypes {$symbol};
4147                 my $item = "Parameter";
4148                 if (defined ($type)) {
4149                     if ($type eq 'STRUCT') {
4150                         $item = "Field";
4151                     } elsif ($type eq 'ENUM') {
4152                         $item = "Value";
4153                     } elsif ($type eq 'UNION') {
4154                         $item = "Field";
4155                     }
4156                 } else {
4157                     $type="SIGNAL";
4158                 }
4160                 #print "Check param docs for $symbol, tmpl_params: ",$#$tmpl_params," entries, type=$type\n";
4162                 if ($#$tmpl_params > 0) {
4163                     my $j;
4164                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4165                         # Output a warning if the parameter is empty and
4166                         # remember for stats.
4167                         my $tmpl_param_name = $$tmpl_params[$j];
4168                         my $tmpl_param_desc = $$tmpl_params[$j + 1];
4169                         if ($tmpl_param_desc !~ m/\S/) {
4170                             if (exists ($AllIncompleteSymbols{$symbol})) {
4171                                 $AllIncompleteSymbols{$symbol}.=", ".$tmpl_param_name;
4172                             } else {
4173                                 $AllIncompleteSymbols{$symbol}=$tmpl_param_name;
4174                             }
4175                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4176                                 "$item description for $symbol"."::"."$tmpl_param_name is missing in source code comment block.");
4177                         }
4178                     }
4179                 }
4180                 else {
4181                     if ($#$tmpl_params == 0) {
4182                         $AllIncompleteSymbols{$symbol}="<items>";
4183                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4184                             "$item descriptions for $symbol are missing in source code comment block.");
4185                     }
4186                     # $#$tmpl_params==-1 means we don't know about parameters
4187                     # this unfortunately does not tell if there should be some
4188                 }
4189             }
4190         }
4191    }
4192    #print "num doc entries: ".(scalar %SymbolDocs)."\n";
4195 #############################################################################
4196 # Function    : IsEmptyDoc
4197 # Description : Check if a doc-string is empty. Its also regarded as empty if
4198 #               it only consist of whitespace or e.g. FIXME.
4199 # Arguments   : the doc-string
4200 #############################################################################
4201 sub IsEmptyDoc {
4202     my ($doc) = @_;
4203     
4204     if ($doc =~ /^\s*$/) {
4205         return 1;
4206     }
4208     if ($doc =~ /^\s*<para>\s*(FIXME)?\s*<\/para>\s*$/) {
4209         return 1;
4210     }
4212     return 0;
4216 # This converts blank lines to "</para><para>", but only outside CDATA and
4217 # <programlisting> tags.
4218 sub ConvertBlankLines {
4219     return &ModifyXMLElements ($_[0], $_[1],
4220                                "<!\\[CDATA\\[|<programlisting[^>]*>|\\|\\[",
4221                                \&ConvertBlankLinesEndTag,
4222                                \&ConvertBlankLinesCallback);
4226 sub ConvertBlankLinesEndTag {
4227   if ($_[0] eq "<!\[CDATA\[") {
4228     return "]]>";
4229   } elsif ($_[0] eq "|[") {
4230     return "]\\|";
4231   } else {
4232     return "</programlisting>";
4233   }
4236 sub ConvertBlankLinesCallback {
4237   my ($text, $symbol, $tag) = @_;
4239   # If we're not in CDATA or a <programlisting> we convert blank lines so
4240   # they start a new <para>.
4241   if ($tag eq "") {
4242     $text =~ s%\n{2,}%\n</para>\n<para>\n%g;
4243   }
4245   return $text;
4249 #############################################################################
4250 # LIBRARY FUNCTIONS -   These functions are used in both gtkdoc-mkdb and
4251 #                       gtkdoc-mktmpl and should eventually be moved to a
4252 #                       separate library.
4253 #############################################################################
4255 #############################################################################
4256 # Function    : ReadDeclarationsFile
4257 # Description : This reads in a file containing the function/macro/enum etc.
4258 #               declarations.
4260 #               Note that in some cases there are several declarations with
4261 #               the same name, e.g. for conditional macros. In this case we
4262 #               set a flag in the %DeclarationConditional hash so the
4263 #               declaration is not shown in the docs.
4265 #               If a macro and a function have the same name, e.g. for
4266 #               gtk_object_ref, the function declaration takes precedence.
4268 #               Some opaque structs are just declared with 'typedef struct
4269 #               _name name;' in which case the declaration may be empty.
4270 #               The structure may have been found later in the header, so
4271 #               that overrides the empty declaration.
4273 # Arguments   : $file - the declarations file to read
4274 #               $override - if declarations in this file should override
4275 #                       any current declaration.
4276 #############################################################################
4278 sub ReadDeclarationsFile {
4279     my ($file, $override) = @_;
4281     if ($override == 0) {
4282         %Declarations = ();
4283         %DeclarationTypes = ();
4284         %DeclarationConditional = ();
4285         %DeclarationOutput = ();
4286     }
4288     open (INPUT, $file)
4289         || die "Can't open $file: $!";
4290     my $declaration_type = "";
4291     my $declaration_name;
4292     my $declaration;
4293     my $is_deprecated = 0;
4294     while (<INPUT>) {
4295         if (!$declaration_type) {
4296             if (m/^<([^>]+)>/) {
4297                 $declaration_type = $1;
4298                 $declaration_name = "";
4299                 #print "Found declaration: $declaration_type\n";
4300                 $declaration = "";
4301             }
4302         } else {
4303             if (m%^<NAME>(.*)</NAME>%) {
4304                 $declaration_name = $1;
4305             } elsif (m%^<DEPRECATED/>%) {
4306                 $is_deprecated = 1;
4307             } elsif (m%^</$declaration_type>%) {
4308                 #print "Found end of declaration: $declaration_name\n";
4309                 # Check that the declaration has a name
4310                 if ($declaration_name eq "") {
4311                     print "ERROR: $declaration_type has no name $file:$.\n";
4312                 }
4314                 # If the declaration is an empty typedef struct _XXX XXX
4315                 # set the flag to indicate the struct has a typedef.
4316                 if ($declaration_type eq 'STRUCT'
4317                     && $declaration =~ m/^\s*$/) {
4318                     #print "Struct has typedef: $declaration_name\n";
4319                     $StructHasTypedef{$declaration_name} = 1;
4320                 }
4322                 # Check if the symbol is already defined.
4323                 if (defined ($Declarations{$declaration_name})
4324                     && $override == 0) {
4325                     # Function declarations take precedence.
4326                     if ($DeclarationTypes{$declaration_name} eq 'FUNCTION') {
4327                         # Ignore it.
4328                     } elsif ($declaration_type eq 'FUNCTION') {
4329                         if ($is_deprecated) {
4330                             $Deprecated{$declaration_name} = "";
4331                         }
4332                         $Declarations{$declaration_name} = $declaration;
4333                         $DeclarationTypes{$declaration_name} = $declaration_type;
4334                     } elsif ($DeclarationTypes{$declaration_name}
4335                               eq $declaration_type) {
4336                         # If the existing declaration is empty, or is just a
4337                         # forward declaration of a struct, override it.
4338                         if ($declaration_type eq 'STRUCT') {
4339                             if ($Declarations{$declaration_name} =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4340                                 if ($is_deprecated) {
4341                                     $Deprecated{$declaration_name} = "";
4342                                 }
4343                                 $Declarations{$declaration_name} = $declaration;
4344                             } elsif ($declaration =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4345                                 # Ignore an empty or forward declaration.
4346                             } else {
4347                                 &LogWarning ($file, $., "Structure $declaration_name has multiple definitions.");
4348                             }
4349                         } else {
4350                             # set flag in %DeclarationConditional hash for
4351                             # multiply defined macros/typedefs.
4352                             $DeclarationConditional{$declaration_name} = 1;
4353                         }
4354                     } else {
4355                         &LogWarning ($file, $., "$declaration_name has multiple definitions.");
4356                     }
4357                 } else {
4358                     if ($is_deprecated) {
4359                         $Deprecated{$declaration_name} = "";
4360                     }
4361                     $Declarations{$declaration_name} = $declaration;
4362                     $DeclarationTypes{$declaration_name} = $declaration_type;
4363                 }
4365                 $declaration_type = "";
4366                 $is_deprecated = 0;
4367             } else {
4368                 $declaration .= $_;
4369             }
4370         }
4371     }
4372     close (INPUT);
4376 #############################################################################
4377 # Function    : ReadSignalsFile
4378 # Description : This reads in an existing file which contains information on
4379 #               all GTK signals. It creates the arrays @SignalNames and
4380 #               @SignalPrototypes containing info on the signals. The first
4381 #               line of the SignalPrototype is the return type of the signal
4382 #               handler. The remaining lines are the parameters passed to it.
4383 #               The last parameter, "gpointer user_data" is always the same
4384 #               so is not included.
4385 # Arguments   : $file - the file containing the signal handler prototype
4386 #                       information.
4387 #############################################################################
4389 sub ReadSignalsFile {
4390     my ($file) = @_;
4392     my $in_signal = 0;
4393     my $signal_object;
4394     my $signal_name;
4395     my $signal_returns;
4396     my $signal_flags;
4397     my $signal_prototype;
4399     # Reset the signal info.
4400     @SignalObjects = ();
4401     @SignalNames = ();
4402     @SignalReturns = ();
4403     @SignalFlags = ();
4404     @SignalPrototypes = ();
4406     if (! -f $file) {
4407         return;
4408     }
4409     if (!open (INPUT, $file)) {
4410         warn "Can't open $file - skipping signals\n";
4411         return;
4412     }
4413     while (<INPUT>) {
4414         if (!$in_signal) {
4415             if (m/^<SIGNAL>/) {
4416                 $in_signal = 1;
4417                 $signal_object = "";
4418                 $signal_name = "";
4419                 $signal_returns = "";
4420                 $signal_prototype = "";
4421             }
4422         } else {
4423             if (m/^<NAME>(.*)<\/NAME>/) {
4424                 $signal_name = $1;
4425                 if ($signal_name =~ m/^(.*)::(.*)$/) {
4426                     $signal_object = $1;
4427                     ($signal_name = $2) =~ s/_/-/g;
4428                     #print "Found signal: $signal_name\n";
4429                 } else {
4430                     &LogWarning ($file, $., "Invalid signal name: $signal_name.");
4431                 }
4432             } elsif (m/^<RETURNS>(.*)<\/RETURNS>/) {
4433                 $signal_returns = $1;
4434             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4435                 $signal_flags = $1;
4436             } elsif (m%^</SIGNAL>%) {
4437                 #print "Found end of signal: ${signal_object}::${signal_name}\nReturns: ${signal_returns}\n${signal_prototype}";
4438                 push (@SignalObjects, $signal_object);
4439                 push (@SignalNames, $signal_name);
4440                 push (@SignalReturns, $signal_returns);
4441                 push (@SignalFlags, $signal_flags);
4442                 push (@SignalPrototypes, $signal_prototype);
4443                 $in_signal = 0;
4444             } else {
4445                 $signal_prototype .= $_;
4446             }
4447         }
4448     }
4449     close (INPUT);
4453 #############################################################################
4454 # Function    : ReadTemplateFile
4455 # Description : This reads in the manually-edited documentation file
4456 #               corresponding to the file currently being created, so we can
4457 #               insert the documentation at the appropriate places.
4458 #               It outputs %SymbolTypes, %SymbolDocs and %SymbolParams, which
4459 #               is a hash of arrays.
4460 #               NOTE: This function is duplicated in gtkdoc-mktmpl (but
4461 #               slightly different).
4462 # Arguments   : $docsfile - the template file to read in.
4463 #               $skip_unused_params - 1 if the unused parameters should be
4464 #                       skipped.
4465 #############################################################################
4467 sub ReadTemplateFile {
4468     my ($docsfile, $skip_unused_params) = @_;
4470     my $template = "$docsfile.sgml";
4471     if (! -f $template) {
4472         #print "File doesn't exist: $template\n";
4473         return 0;
4474     }
4475     #print "Reading $template\n";
4477     # start with empty hashes, we merge the source comment for each file
4478     # afterwards
4479     %SymbolDocs = ();
4480     %SymbolTypes = ();
4481     %SymbolParams = ();
4483     my $current_type = "";      # Type of symbol being read.
4484     my $current_symbol = "";    # Name of symbol being read.
4485     my $symbol_doc = "";                # Description of symbol being read.
4486     my @params;                 # Parameter names and descriptions of current
4487                                 #   function/macro/function typedef.
4488     my $current_param = -1;     # Index of parameter currently being read.
4489                                 #   Note that the param array contains pairs
4490                                 #   of param name & description.
4491     my $in_unused_params = 0;   # True if we are reading in the unused params.
4492     my $in_deprecated = 0;
4493     my $in_since = 0;
4494     my $in_stability = 0;
4496     open (DOCS, "$template")
4497         || die "Can't open $template: $!";
4498     while (<DOCS>) {
4499         if (m/^<!-- ##### ([A-Z_]+) (\S+) ##### -->/) {
4500             my $type = $1;
4501             my $symbol = $2;
4502             if ($symbol eq "Title"
4503                 || $symbol eq "Short_Description"
4504                 || $symbol eq "Long_Description"
4505                 || $symbol eq "See_Also"
4506                 || $symbol eq "Stability_Level"
4507                 || $symbol eq "Include") {
4509                 $symbol = $docsfile . ":" . $symbol;
4510             }
4512             #print "Found symbol: $symbol\n";
4513             # Remember file and line for the symbol
4514             $SymbolSourceFile{$symbol} = $template;
4515             $SymbolSourceLine{$symbol} = $.;
4517             # Store previous symbol, but remove any trailing blank lines.
4518             if ($current_symbol ne "") {
4519                 $symbol_doc =~ s/\s+$//;
4520                 $SymbolTypes{$current_symbol} = $current_type;
4521                 $SymbolDocs{$current_symbol} = $symbol_doc;
4523                 # Check that the stability level is valid.
4524                 if ($StabilityLevel{$current_symbol}) {
4525                     $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4526                 }
4528                 if ($current_param >= 0) {
4529                     $SymbolParams{$current_symbol} = [ @params ];
4530                 } else {
4531                     # Delete any existing params in case we are overriding a
4532                     # previously read template.
4533                     delete $SymbolParams{$current_symbol};
4534                 }
4535             }
4536             $current_type = $type;
4537             $current_symbol = $symbol;
4538             $current_param = -1;
4539             $in_unused_params = 0;
4540             $in_deprecated = 0;
4541             $in_since = 0;
4542             $in_stability = 0;
4543             $symbol_doc = "";
4544             @params = ();
4546         } elsif (m/^<!-- # Unused Parameters # -->/) {
4547             #print "DEBUG: Found unused parameters\n";
4548             $in_unused_params = 1;
4549             next;
4551         } elsif ($in_unused_params && $skip_unused_params) {
4552             # When outputting the DocBook we skip unused parameters.
4553             #print "DEBUG: Skipping unused param: $_";
4554             next;
4556         } else {
4557             # Check if param found. Need to handle "..." and "format...".
4558             if (s/^\@([\w\.]+):\040?//) {
4559                 my $param_name = $1;
4560                 my $param_desc = $_;
4561                 # Allow variations of 'Returns'
4562                 if ($param_name =~ m/^[Rr]eturns?$/) {
4563                     $param_name = "Returns";
4564                 }
4566                 # strip trailing whitespaces and blank lines
4567                 s/\s+\n$/\n/m;
4568                 s/\n+$/\n/sm;
4569                 #print "Found param for symbol $current_symbol : '$param_name'= '$_'";
4571                 if ($param_name eq "Deprecated") {
4572                     $in_deprecated = 1;
4573                     $Deprecated{$current_symbol} = $_;
4574                 } elsif ($param_name eq "Since") {
4575                     $in_since = 1;
4576                     chomp;
4577                     $Since{$current_symbol} = $_;
4578                 } elsif ($param_name eq "Stability") {
4579                     $in_stability = 1;
4580                     $StabilityLevel{$current_symbol} = $_;
4581                 } else {
4582                     push (@params, $param_name);
4583                     push (@params, $param_desc);
4584                     $current_param += $PARAM_FIELD_COUNT;
4585                 }
4586             } else {
4587                 # strip trailing whitespaces and blank lines
4588                 s/\s+\n$/\n/m;
4589                 s/\n+$/\n/sm;
4590                 
4591                 if (!m/^\s+$/) {
4592                     if ($in_deprecated) {
4593                         $Deprecated{$current_symbol} .= $_;
4594                     } elsif ($in_since) {
4595                         &LogWarning ($template, $., "multi-line since docs found");
4596                         #$Since{$current_symbol} .= $_;
4597                     } elsif ($in_stability) {
4598                         $StabilityLevel{$current_symbol} .= $_;
4599                     } elsif ($current_param >= 0) {
4600                         $params[$current_param] .= $_;
4601                     } else {
4602                         $symbol_doc .= $_;
4603                     }
4604                 }
4605             }
4606         }
4607     }
4609     # Remember to finish the current symbol doccs.
4610     if ($current_symbol ne "") {
4612         $symbol_doc =~ s/\s+$//;
4613         $SymbolTypes{$current_symbol} = $current_type;
4614         $SymbolDocs{$current_symbol} = $symbol_doc;
4616         # Check that the stability level is valid.
4617         if ($StabilityLevel{$current_symbol}) {
4618             $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4619         }
4621         if ($current_param >= 0) {
4622             $SymbolParams{$current_symbol} = [ @params ];
4623         } else {
4624             # Delete any existing params in case we are overriding a
4625             # previously read template.
4626             delete $SymbolParams{$current_symbol};
4627         }
4628     }
4630     close (DOCS);
4631     return 1;
4635 #############################################################################
4636 # Function    : ReadObjectHierarchy
4637 # Description : This reads in the $MODULE-hierarchy.txt file containing all
4638 #               the GtkObject subclasses described in this module (and their
4639 #               ancestors).
4640 #               It places them in the @Objects array, and places their level
4641 #               in the widget hierarchy in the @ObjectLevels array, at the
4642 #               same index. GtkObject, the root object, has a level of 1.
4644 #               FIXME: the version in gtkdoc-mkdb also generates tree_index.sgml
4645 #               as it goes along, this should be split out into a separate
4646 #               function.
4648 # Arguments   : none
4649 #############################################################################
4651 sub ReadObjectHierarchy {
4652     @Objects = ();
4653     @ObjectLevels = ();
4655     if (! -f $OBJECT_TREE_FILE) {
4656         return;
4657     }
4658     if (!open (INPUT, $OBJECT_TREE_FILE)) {
4659         warn "Can't open $OBJECT_TREE_FILE - skipping object tree\n";
4660         return;
4661     }
4663     # FIXME: use $OUTPUT_FORMAT
4664     # my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.$OUTPUT_FORMAT";
4665     my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.sgml";
4666     my $new_tree_index = "$SGML_OUTPUT_DIR/tree_index.new";
4668     open (OUTPUT, ">$new_tree_index")
4669         || die "Can't create $new_tree_index: $!";
4671     if (lc($OUTPUT_FORMAT) eq "xml") {
4672         my $tree_header = $doctype_header;
4674         $tree_header =~ s/<!DOCTYPE \w+/<!DOCTYPE screen/;
4675         print (OUTPUT "$tree_header");
4676     }
4677     print (OUTPUT "<screen>\n");
4679     # Only emit objects if they are supposed to be documented, or if
4680     # they have documented children. To implement this, we maintain a
4681     # stack of pending objects which will be emitted if a documented
4682     # child turns up.
4683     my @pending_objects = ();
4684     my @pending_levels = ();
4685     while (<INPUT>) {
4686         if (m/\S+/) {
4687             my $object = $&;
4688             my $level = (length($`)) / 2 + 1;
4689             my $xref = "";
4691             while (($#pending_levels >= 0) && ($pending_levels[$#pending_levels] >= $level)) {
4692                 my $pobject = pop(@pending_objects);
4693                 my $plevel = pop(@pending_levels);
4694             }
4696             push (@pending_objects, $object);
4697             push (@pending_levels, $level);
4699             if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4700                 while ($#pending_levels >= 0) {
4701                     $object = shift @pending_objects;
4702                     $level = shift @pending_levels;
4703                     $xref = &MakeXRef ($object);
4705                     print (OUTPUT ' ' x ($level * 4), "$xref\n");
4706                     push (@Objects, $object);
4707                     push (@ObjectLevels, $level);
4708                 }
4709             }
4710         }
4711     }
4712     print (OUTPUT "</screen>\n");
4714     close (INPUT);
4715     close (OUTPUT);
4717     &UpdateFileIfChanged ($old_tree_index, $new_tree_index, 0);
4719     &OutputObjectList;
4722 #############################################################################
4723 # Function    : ReadInterfaces
4724 # Description : This reads in the $MODULE.interfaces file.
4726 # Arguments   : none
4727 #############################################################################
4729 sub ReadInterfaces {
4730     %Interfaces = ();
4732     if (! -f $INTERFACES_FILE) {
4733         return;
4734     }
4735     if (!open (INPUT, $INTERFACES_FILE)) {
4736         warn "Can't open $INTERFACES_FILE - skipping interfaces\n";
4737         return;
4738     }
4740     while (<INPUT>) {
4741        chomp;
4742        my ($object, @ifaces) = split;
4743        if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4744            my @knownIfaces = ();
4746            # filter out private interfaces, but leave foreign interfaces
4747            foreach my $iface (@ifaces) {
4748                if (!exists($KnownSymbols{$iface}) || $KnownSymbols{$iface} == 1) {
4749                    push (@knownIfaces, $iface);
4750                }
4751              }
4753            $Interfaces{$object} = join(' ', @knownIfaces);
4754        }
4755     }
4756     close (INPUT);
4759 #############################################################################
4760 # Function    : ReadPrerequisites
4761 # Description : This reads in the $MODULE.prerequisites file.
4763 # Arguments   : none
4764 #############################################################################
4766 sub ReadPrerequisites {
4767     %Prerequisites = ();
4769     if (! -f $PREREQUISITES_FILE) {
4770         return;
4771     }
4772     if (!open (INPUT, $PREREQUISITES_FILE)) {
4773         warn "Can't open $PREREQUISITES_FILE - skipping prerequisites\n";
4774         return;
4775     }
4777     while (<INPUT>) {
4778        chomp;
4779        my ($iface, @prereqs) = split;
4780        if (exists($KnownSymbols{$iface}) && $KnownSymbols{$iface} == 1) {
4781            my @knownPrereqs = ();
4783            # filter out private prerequisites, but leave foreign prerequisites
4784            foreach my $prereq (@prereqs) {
4785                if (!exists($KnownSymbols{$prereq}) || $KnownSymbols{$prereq} == 1) {
4786                   push (@knownPrereqs, $prereq);
4787                }
4788            }
4790            $Prerequisites{$iface} = join(' ', @knownPrereqs);
4791        }
4792     }
4793     close (INPUT);
4796 #############################################################################
4797 # Function    : ReadArgsFile
4798 # Description : This reads in an existing file which contains information on
4799 #               all GTK args. It creates the arrays @ArgObjects, @ArgNames,
4800 #               @ArgTypes, @ArgFlags, @ArgNicks and @ArgBlurbs containing info
4801 #               on the args.
4802 # Arguments   : $file - the file containing the arg information.
4803 #############################################################################
4805 sub ReadArgsFile {
4806     my ($file) = @_;
4808     my $in_arg = 0;
4809     my $arg_object;
4810     my $arg_name;
4811     my $arg_type;
4812     my $arg_flags;
4813     my $arg_nick;
4814     my $arg_blurb;
4815     my $arg_default;
4816     my $arg_range;
4818     # Reset the args info.
4819     @ArgObjects = ();
4820     @ArgNames = ();
4821     @ArgTypes = ();
4822     @ArgFlags = ();
4823     @ArgNicks = ();
4824     @ArgBlurbs = ();
4825     @ArgDefaults = ();
4826     @ArgRanges = ();
4828     if (! -f $file) {
4829         return;
4830     }
4831     if (!open (INPUT, $file)) {
4832         warn "Can't open $file - skipping args\n";
4833         return;
4834     }
4835     while (<INPUT>) {
4836         if (!$in_arg) {
4837             if (m/^<ARG>/) {
4838                 $in_arg = 1;
4839                 $arg_object = "";
4840                 $arg_name = "";
4841                 $arg_type = "";
4842                 $arg_flags = "";
4843                 $arg_nick = "";
4844                 $arg_blurb = "";
4845                 $arg_default = "";
4846                 $arg_range = "";
4847             }
4848         } else {
4849             if (m/^<NAME>(.*)<\/NAME>/) {
4850                 $arg_name = $1;
4851                 if ($arg_name =~ m/^(.*)::(.*)$/) {
4852                     $arg_object = $1;
4853                     ($arg_name = $2) =~ s/_/-/g;
4854                     #print "Found arg: $arg_name\n";
4855                 } else {
4856                     &LogWarning ($file, $., "Invalid argument name: $arg_name");
4857                 }
4858             } elsif (m/^<TYPE>(.*)<\/TYPE>/) {
4859                 $arg_type = $1;
4860             } elsif (m/^<RANGE>(.*)<\/RANGE>/) {
4861                 $arg_range = $1;
4862             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4863                 $arg_flags = $1;
4864             } elsif (m/^<NICK>(.*)<\/NICK>/) {
4865                 $arg_nick = $1;
4866             } elsif (m/^<BLURB>(.*)<\/BLURB>/) {
4867                 $arg_blurb = $1;
4868                 if ($arg_blurb eq "(null)") {
4869                   $arg_blurb = "";
4870                   &LogWarning ($file, $., "Property ${arg_object}:${arg_name} has no documentation.");
4871                 }
4872             } elsif (m/^<DEFAULT>(.*)<\/DEFAULT>/) {
4873                 $arg_default = $1;
4874             } elsif (m%^</ARG>%) {
4875                 #print "Found end of arg: ${arg_object}::${arg_name}\n${arg_type} : ${arg_flags}\n";
4876                 push (@ArgObjects, $arg_object);
4877                 push (@ArgNames, $arg_name);
4878                 push (@ArgTypes, $arg_type);
4879                 push (@ArgRanges, $arg_range);
4880                 push (@ArgFlags, $arg_flags);
4881                 push (@ArgNicks, $arg_nick);
4882                 push (@ArgBlurbs, $arg_blurb);
4883                 push (@ArgDefaults, $arg_default);
4884                 $in_arg = 0;
4885             }
4886         }
4887     }
4888     close (INPUT);
4892 #############################################################################
4893 # Function    : CheckIsObject
4894 # Description : Returns 1 if the given name is a GtkObject or a subclass.
4895 #               It uses the global @Objects array.
4896 #               Note that the @Objects array only contains classes in the
4897 #               current module and their ancestors - not all GTK classes.
4898 # Arguments   : $name - the name to check.
4899 #############################################################################
4901 sub CheckIsObject {
4902     my ($name) = @_;
4904     my $object;
4905     foreach $object (@Objects) {
4906         if ($object eq $name) {
4907             return 1;
4908         }
4909     }
4910     return 0;
4914 #############################################################################
4915 # Function    : MakeReturnField
4916 # Description : Pads a string to $RETURN_TYPE_FIELD_WIDTH.
4917 # Arguments   : $str - the string to pad.
4918 #############################################################################
4920 sub MakeReturnField {
4921     my ($str) = @_;
4923     return $str . (' ' x ($RETURN_TYPE_FIELD_WIDTH - length ($str)));
4926 #############################################################################
4927 # Function    : GetSymbolSourceFile
4928 # Description : Get the filename where the symbol docs where taken from.
4929 # Arguments   : $symbol - the symbol name
4930 #############################################################################
4932 sub GetSymbolSourceFile {
4933     my ($symbol) = @_;
4935     if (defined($SourceSymbolSourceFile{$symbol})) {
4936         return $SourceSymbolSourceFile{$symbol};
4937     } elsif (defined($SymbolSourceFile{$symbol})) {
4938         return $SymbolSourceFile{$symbol};
4939     } else {
4940         return "";
4941     }
4944 #############################################################################
4945 # Function    : GetSymbolSourceLine
4946 # Description : Get the file line where the symbol docs where taken from.
4947 # Arguments   : $symbol - the symbol name
4948 #############################################################################
4950 sub GetSymbolSourceLine {
4951     my ($symbol) = @_;
4953     if (defined($SourceSymbolSourceLine{$symbol})) {
4954         return $SourceSymbolSourceLine{$symbol};
4955     } elsif (defined($SymbolSourceLine{$symbol})) {
4956         return $SymbolSourceLine{$symbol};
4957     } else {
4958         return 0;
4959     }