tools: append to the perl path so that it stays overrideable. Fixes #604892
[gtk-doc.git] / gtkdoc-mkdb.in
blob36e2f8bf080653357f8bac1e7b266c8c16148c2c
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 push @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     # if there are no annotations used return
974     return if (! keys(%AnnotationsUsed));
976     # add acronyms that are referenced from acronym text
977 rerun:
978     foreach my $annotation (keys(%AnnotationsUsed)) {
979         if($AnnotationDefinition{$annotation} =~ m/<acronym>([\w ]+)<\/acronym>/) {
980             if (!exists($AnnotationsUsed{$1})) {
981                 $AnnotationsUsed{$1} = 1;
982                 goto rerun;
983             }
984         }
985     }
987     open (OUTPUT, ">$new_glossary")
988         || die "Can't create $new_glossary";
990     my $header = $doctype_header;
991     $header =~ s/<!DOCTYPE \w+/<!DOCTYPE glossary/;
993     print (OUTPUT  <<EOF);
994 $header
995 <glossary id="annotation-glossary">
996   <title>Annotation Glossary</title>
999     foreach my $annotation (keys(%AnnotationsUsed)) {
1000         my $def = $AnnotationDefinition{$annotation};
1001         my $curletter = uc(substr($annotation,0,1));
1003         if ($curletter ne $lastletter) {
1004             $lastletter = $curletter;
1005       
1006             if ($divopen == 1) {
1007                 print (OUTPUT "</glossdiv>\n");
1008             }
1009             print (OUTPUT "<glossdiv><title>$curletter</title>\n");
1010             $divopen = 1;
1011         }
1012         print (OUTPUT <<EOF);
1013     <glossentry>
1014       <glossterm><anchor id="annotation-glossterm-$annotation"/>$annotation</glossterm>
1015       <glossdef>
1016         <para>$def</para>
1017       </glossdef>
1018     </glossentry>
1020     }
1022     if ($divopen == 1) {
1023         print (OUTPUT "</glossdiv>\n");
1024     }
1025     print (OUTPUT "</glossary>\n");
1026     close (OUTPUT);
1028     &UpdateFileIfChanged ($old_glossary, $new_glossary, 0);
1031 #############################################################################
1032 # Function    : ReadKnownSymbols
1033 # Description : This collects the names of non-private symbols from the
1034 #               $MODULE-sections.txt file.
1035 # Arguments   : $file - the $MODULE-sections.txt file which contains all of
1036 #               the functions/macros/structs etc. being documented, organised
1037 #               into sections and subsections.
1038 #############################################################################
1040 sub ReadKnownSymbols {
1041     my ($file) = @_;
1043     my $subsection = "";
1045     #print "Reading: $file\n";
1046     open (INPUT, $file)
1047         || die "Can't open $file: $!";
1049     while (<INPUT>) {
1050         if (m/^#/) {
1051             next;
1053         } elsif (m/^<SECTION>/) {
1054             $subsection = "";
1056         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
1057             $subsection = $1;
1059         } elsif (m/^<SUBSECTION>/) {
1060             next;
1062         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
1063             next;
1065         } elsif (m/^<FILE>(.*)<\/FILE>/) {
1066             $KnownSymbols{"$TMPL_DIR/$1:Long_Description"} = 1;
1067             $KnownSymbols{"$TMPL_DIR/$1:Short_Description"} = 1;
1068             next;
1070         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
1071             next;
1073         } elsif (m/^<\/SECTION>/) {
1074             next;
1076         } elsif (m/^(\S+)/) {
1077             my $symbol = $1;
1079             if ($subsection ne "Standard" && $subsection ne "Private") {
1080                 $KnownSymbols{$symbol} = 1;
1081             }
1082             else {
1083                 $KnownSymbols{$symbol} = 0;
1084             }
1085         }
1086     }
1087     close (INPUT);
1091 #############################################################################
1092 # Function    : OutputDeclaration
1093 # Description : Returns the synopsis and detailed description DocBook
1094 #               describing one function/macro etc.
1095 # Arguments   : $symbol - the name of the function/macro begin described.
1096 #               $declaration - the declaration of the function/macro.
1097 #############################################################################
1099 sub OutputDeclaration {
1100     my ($symbol, $declaration) = @_;
1102     my $type = $DeclarationTypes {$symbol};
1103     if ($type eq 'MACRO') {
1104         return &OutputMacro ($symbol, $declaration);
1105     } elsif ($type eq 'TYPEDEF') {
1106         return &OutputTypedef ($symbol, $declaration);
1107     } elsif ($type eq 'STRUCT') {
1108         return &OutputStruct ($symbol, $declaration);
1109     } elsif ($type eq 'ENUM') {
1110         return &OutputEnum ($symbol, $declaration);
1111     } elsif ($type eq 'UNION') {
1112         return &OutputUnion ($symbol, $declaration);
1113     } elsif ($type eq 'VARIABLE') {
1114         return &OutputVariable ($symbol, $declaration);
1116     } elsif ($type eq 'FUNCTION') {
1117         return &OutputFunction ($symbol, $declaration, $type);
1118     } elsif ($type eq 'USER_FUNCTION') {
1119         return &OutputFunction ($symbol, $declaration, $type);
1120     } else {
1121         die "Unknown symbol type";
1122     }
1126 #############################################################################
1127 # Function    : OutputSymbolTraits
1128 # Description : Returns the Since and StabilityLevel paragraphs for a symbol.
1129 # Arguments   : $symbol - the name of the function/macro begin described.
1130 #############################################################################
1132 sub OutputSymbolTraits {
1133     my ($symbol) = @_;
1134     my $desc = "";
1136     if (exists $Since{$symbol}) {
1137         $desc .= "<para role=\"since\">Since $Since{$symbol}</para>";
1138     }
1139     if (exists $StabilityLevel{$symbol}) {
1140         $desc .= "<para role=\"stability\">Stability Level: $StabilityLevel{$symbol}</para>";
1141     }
1142     return $desc;
1145 #############################################################################
1146 # Function    : Outpu{Symbol,Section}ExtraLinks
1147 # Description : Returns extralinks for the symbol (if enabled).
1148 # Arguments   : $symbol - the name of the function/macro begin described.
1149 #############################################################################
1151 sub uri_escape {
1152     my $text = $_[0];
1153     return undef unless defined $text;
1155     # Build a char to hex map
1156     my %escapes = ();
1157     for (0..255) {
1158             $escapes{chr($_)} = sprintf("%%%02X", $_);
1159     }
1161     # Default unsafe characters.  RFC 2732 ^(uric - reserved)
1162     $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
1164     return $text;
1167 sub OutputSymbolExtraLinks {
1168     my ($symbol) = @_;
1169     my $desc = "";
1171     if (0) { # NEW FEATURE: needs configurability
1172     my $sstr = &uri_escape($symbol);
1173     my $mstr = &uri_escape($MODULE);
1174     $desc .= <<EOF;
1175 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1176 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$sstr">edit documentation</ulink>
1178     }
1179     return $desc;
1182 sub OutputSectionExtraLinks {
1183     my ($symbol,$docsymbol) = @_;
1184     my $desc = "";
1186     if (0) { # NEW FEATURE: needs configurability
1187     my $sstr = &uri_escape($symbol);
1188     my $mstr = &uri_escape($MODULE);
1189     my $dsstr = &uri_escape($docsymbol);
1190     $desc .= <<EOF;
1191 <ulink role="extralinks" url="http://www.google.com/codesearch?q=$sstr">code search</ulink>
1192 <ulink role="extralinks" url="http://library.gnome.org/edit?module=$mstr&amp;symbol=$dsstr">edit documentation</ulink>
1194     }
1195     return $desc;
1199 #############################################################################
1200 # Function    : OutputMacro
1201 # Description : Returns the synopsis and detailed description of a macro.
1202 # Arguments   : $symbol - the macro.
1203 #               $declaration - the declaration of the macro.
1204 #############################################################################
1206 sub OutputMacro {
1207     my ($symbol, $declaration) = @_;
1208     my $id = &CreateValidSGMLID ($symbol);
1209     my $condition = &MakeConditionDescription ($symbol);
1210     my $synop = &MakeReturnField("#define") . "<link linkend=\"$id\">$symbol</link>";
1211     my $desc;
1212     my $padding = "";
1213     my $args = "";
1214     my $pad;
1216     if ($declaration =~ m/^\s*#\s*define(\s+)\w+(\([^\)]*\))/) {
1217         $padding = $1;
1218         $args = $2;
1220         if (length ($symbol) < $SYMBOL_FIELD_WIDTH) {
1221             $synop .= (' ' x ($SYMBOL_FIELD_WIDTH - length ($symbol)));
1222         }
1224         # Try to align all the lines of args correctly.
1225         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH + $SYMBOL_FIELD_WIDTH + 1);
1226         my $args_padded = $args;
1227         $args_padded =~ s/ *\\\n */\n$pad/gm;
1228         $synop .= &CreateValidSGML ($args_padded);
1229     }
1230     $synop .= "\n";
1232     my $title = $symbol . ($args ? "()" : "");
1233     $desc = "<refsect2 id=\"$id\" role=\"macro\"$condition>\n<title>$title</title>\n";
1234     $desc .= MakeIndexterms($symbol, $id);
1235     $desc .= "\n";
1236     $desc .= OutputSymbolExtraLinks($symbol);
1238     # Don't output the macro definition if is is a conditional macro or it
1239     # looks like a function, i.e. starts with "g_" or "_?gnome_", or it is
1240     # longer than 2 lines, otherwise we get lots of complicated macros like
1241     # g_assert.
1242     if (!defined ($DeclarationConditional{$symbol}) && ($symbol !~ m/^g_/)
1243         && ($symbol !~ m/^_?gnome_/) && (($declaration =~ tr/\n//) < 2)) {
1244         my $decl_out = &CreateValidSGML ($declaration);
1245         $desc .= "<programlisting>$decl_out</programlisting>\n";
1246     } else {
1247         $desc .= "<programlisting>" . &MakeReturnField("#define") . "$symbol";
1248         # Align each line so that if should all line up OK.
1249         $pad = ' ' x ($RETURN_TYPE_FIELD_WIDTH - length ("#define "));
1250         $args =~ s/\n/\n$pad/gm;
1251         $desc .= &CreateValidSGML ($args);
1252         $desc .= "</programlisting>\n";
1253     }
1255     $desc .= &MakeDeprecationNote($symbol);
1257     my $parameters = &OutputParamDescriptions ("MACRO", $symbol);
1258     my $parameters_output = 0;
1260     if (defined ($SymbolDocs{$symbol})) {
1261         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1263         # Try to insert the parameter table at the author's desired position.
1264         # Otherwise we need to tag it onto the end.
1265         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1266           $parameters_output = 1;
1267         }
1268         $desc .= $symbol_docs;
1269     }
1271     if ($parameters_output == 0) {
1272         $desc .= $parameters;
1273     }
1275     $desc .= OutputSymbolTraits ($symbol);
1276     $desc .= "</refsect2>\n";
1277     return ($synop, $desc);
1281 #############################################################################
1282 # Function    : OutputTypedef
1283 # Description : Returns the synopsis and detailed description of a typedef.
1284 # Arguments   : $symbol - the typedef.
1285 #               $declaration - the declaration of the typedef,
1286 #                 e.g. 'typedef unsigned int guint;'
1287 #############################################################################
1289 sub OutputTypedef {
1290     my ($symbol, $declaration) = @_;
1291     my $id = &CreateValidSGMLID ($symbol);
1292     my $condition = &MakeConditionDescription ($symbol);
1293     my $synop = &MakeReturnField("typedef") . "<link linkend=\"$id\">$symbol</link>;\n";
1294     my $desc = "<refsect2 id=\"$id\" role=\"typedef\"$condition>\n<title>$symbol</title>\n";
1296     $desc .= MakeIndexterms($symbol, $id);
1297     $desc .= "\n";
1298     $desc .= OutputSymbolExtraLinks($symbol);
1300     if (!defined ($DeclarationConditional{$symbol})) {
1301         my $decl_out = &CreateValidSGML ($declaration);
1302         $desc .= "<programlisting>$decl_out</programlisting>\n";
1303     }
1305     $desc .= &MakeDeprecationNote($symbol);
1307     if (defined ($SymbolDocs{$symbol})) {
1308         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1309     }
1310     $desc .= OutputSymbolTraits ($symbol);
1311     $desc .= "</refsect2>\n";
1312     return ($synop, $desc);
1316 #############################################################################
1317 # Function    : OutputStruct
1318 # Description : Returns the synopsis and detailed description of a struct.
1319 #               We check if it is a widget struct, and if so we only output
1320 #               parts of it that are noted as public fields.
1321 #               We also use a different SGML ID for widget structs, since the
1322 #               original ID is used for the entire RefEntry.
1323 # Arguments   : $symbol - the struct.
1324 #               $declaration - the declaration of the struct.
1325 #############################################################################
1327 sub OutputStruct {
1328     my ($symbol, $declaration) = @_;
1330     my $is_widget_struct = 0;
1331     my $default_to_public = 1;
1332     if (&CheckIsObject ($symbol)) {
1333     #print "Found widget struct: $symbol\n";
1334     $is_widget_struct = 1;
1335         $default_to_public = 0;
1336     }
1338     my $id;
1339     my $condition;
1340     if ($is_widget_struct) {
1341         $id = &CreateValidSGMLID ($symbol . "_struct");
1342         $condition = &MakeConditionDescription ($symbol . "_struct");
1343     } else {
1344         $id = &CreateValidSGMLID ($symbol);
1345         $condition = &MakeConditionDescription ($symbol);
1346     }
1348     # Determine if it is a simple struct or it also has a typedef.
1349     my $has_typedef = 0;
1350     if ($StructHasTypedef{$symbol} || $declaration =~ m/^\s*typedef\s+/) {
1351       $has_typedef = 1;
1352     }
1354     my $synop;
1355     my $desc;
1356     if ($has_typedef) {
1357         # For structs with typedefs we just output the struct name.
1358         $synop = &MakeReturnField("") . "<link linkend=\"$id\">$symbol</link>;\n";
1359         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>$symbol</title>\n";
1360     } else {
1361         $synop = &MakeReturnField("struct") . "<link linkend=\"$id\">$symbol</link>;\n";
1362         $desc = "<refsect2 id=\"$id\" role=\"struct\"$condition>\n<title>struct $symbol</title>\n";
1363     }
1365     $desc .= MakeIndexterms($symbol, $id);
1366     $desc .= "\n";
1367     $desc .= OutputSymbolExtraLinks($symbol);
1369     # Form a pretty-printed, private-data-removed form of the declaration
1371     my $decl_out = "";
1372     if ($declaration =~ m/^\s*$/) {
1373         #print "Found opaque struct: $symbol\n";
1374         $decl_out = "typedef struct _$symbol $symbol;";
1375     } elsif ($declaration =~ m/^\s*struct\s+\w+\s*;\s*$/) {
1376         #print "Found opaque struct: $symbol\n";
1377         $decl_out = "struct $symbol;";
1378     } else {
1379         my $public = $default_to_public;
1380         my $new_declaration = "";
1381         my $decl_line;
1382         my $decl = $declaration;
1384         if ($decl =~ m/^\s*(typedef\s+)?struct\s*\w*\s*(?:\/\*.*\*\/)?\s*{(.*)}\s*\w*\s*;\s*$/s) {
1385             my $struct_contents = $2;
1387             foreach $decl_line (split (/\n/, $struct_contents)) {
1388                 #print "Struct line: $decl_line\n";
1389                 if ($decl_line =~ m%/\*\s*<\s*public\s*>\s*\*/%) {
1390                     $public = 1;
1391                 } elsif ($decl_line =~ m%/\*\s*<\s*(private|protected)\s*>\s*\*/%) {
1392                     $public = 0;
1393                 } elsif ($public) {
1394                     $new_declaration .= $decl_line . "\n";
1395                 }
1396             }
1398             if ($new_declaration) {
1399                 # Strip any blank lines off the ends.
1400                 $new_declaration =~ s/^\s*\n//;
1401                 $new_declaration =~ s/\n\s*$/\n/;
1403                 if ($has_typedef) {
1404                     $decl_out = "typedef struct {\n" . $new_declaration
1405                       . "} $symbol;\n";
1406                 } else {
1407                     $decl_out = "struct $symbol {\n" . $new_declaration
1408                       . "};\n";
1409                 }
1410             }
1411         } else {
1412             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1413                 "Couldn't parse struct:\n$declaration");
1414         }
1416         # If we couldn't parse the struct or it was all private, output an
1417         # empty struct declaration.
1418         if ($decl_out eq "") {
1419             if ($has_typedef) {
1420                 $decl_out = "typedef struct _$symbol $symbol;";
1421             } else {
1422                 $decl_out = "struct $symbol;";
1423             }
1424         }
1425     }
1427     $decl_out = &CreateValidSGML ($decl_out);
1428     $desc .= "<programlisting>$decl_out</programlisting>\n";
1430     $desc .= &MakeDeprecationNote($symbol);
1432     if (defined ($SymbolDocs{$symbol})) {
1433         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1434     }
1436     # Create a table of fields and descriptions
1438     # FIXME: Inserting &#160's into the produced type declarations here would
1439     #        improve the output in most situations ... except for function
1440     #        members of structs!
1441     my @fields = ParseStructDeclaration($declaration, !$default_to_public,
1442                                         0, \&MakeXRef,
1443                                         sub {
1444                                             "<structfield id=\"".&CreateValidSGMLID("$id.$_[0]")."\">$_[0]</structfield>";
1445                                         });
1446     my $params = $SymbolParams{$symbol};
1448     # If no parameters are filled in, we don't generate the description
1449     # table, for backwards compatibility
1451     my $found = 0;
1452     if (defined $params) {
1453         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1454             if ($params->[$i] =~ /\S/) {
1455                 $found = 1;
1456                 last;
1457             }
1458         }
1459     }
1461     if ($found) {
1462         my %field_descrs = @$params;
1464             $desc .= <<EOF;
1465 <variablelist role="struct">
1467         while (@fields) {
1468             my $field_name = shift @fields;
1469             my $text = shift @fields;
1470             my $field_descr = $field_descrs{$field_name};
1471             my $param_annotations = "";
1473             $desc .= "<varlistentry><term>$text</term>\n";
1474             if (defined $field_descr) {
1475                 ($field_descr,$param_annotations) = &ExpandAnnotation($symbol, $field_descr);
1476                 $field_descr = &ExpandAbbreviations($symbol, $field_descr);
1477                 $desc .= "<listitem><simpara>$field_descr$param_annotations</simpara></listitem>\n";
1478             } else {
1479                 $desc .= "<listitem><simpara /></listitem>\n";
1480             }
1481             $desc .= "</varlistentry>\n";
1482         }
1484         $desc .= "</variablelist>";
1485     }
1486     $desc .= OutputSymbolTraits ($symbol);
1487     $desc .= "</refsect2>\n";
1488     return ($synop, $desc);
1492 #############################################################################
1493 # Function    : OutputEnum
1494 # Description : Returns the synopsis and detailed description of a enum.
1495 # Arguments   : $symbol - the enum.
1496 #               $declaration - the declaration of the enum.
1497 #############################################################################
1499 sub OutputEnum {
1500     my ($symbol, $declaration) = @_;
1501     my $id = &CreateValidSGMLID ($symbol);
1502     my $condition = &MakeConditionDescription ($symbol);
1503     my $synop = &MakeReturnField("enum") . "<link linkend=\"$id\">$symbol</link>;\n";
1504     my $desc = "<refsect2 id=\"$id\" role=\"enum\"$condition>\n<title>enum $symbol</title>\n";
1506     $desc .= MakeIndexterms($symbol, $id);
1507     $desc .= "\n";
1508     $desc .= OutputSymbolExtraLinks($symbol);
1510     my $decl_out = &CreateValidSGML ($declaration);
1511     $desc .= "<programlisting>$decl_out</programlisting>\n";
1513     $desc .= &MakeDeprecationNote($symbol);
1515     if (defined ($SymbolDocs{$symbol})) {
1516         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1517     }
1519     # Create a table of fields and descriptions
1521     my @members = ParseEnumDeclaration($declaration);
1522     my $params = $SymbolParams{$symbol};
1524     # If no parameters are filled in, we don't generate the description
1525     # table, for backwards compatibility
1527     my $found = 0;
1528     if (defined $params) {
1529         for (my $i = 1; $i <= $#$params; $i += $PARAM_FIELD_COUNT) {
1530             if ($params->[$i] =~ /\S/) {
1531                 $found = 1;
1532                 last;
1533             }
1534         }
1535     }
1537     if ($found) {
1538         my %member_descrs = @$params;
1540             $desc .= <<EOF;
1541 <variablelist role="enum">
1543         for my $member_name (@members) {
1544             my $member_descr = $member_descrs{$member_name};
1545             my $param_annotations = "";
1547             $id = &CreateValidSGMLID ($member_name);
1548             $condition = &MakeConditionDescription ($member_name);
1549             $desc .= "<varlistentry id=\"$id\" role=\"constant\"$condition>\n<term><literal>$member_name</literal></term>\n";
1550             if (defined $member_descr) {
1551                 ($member_descr,$param_annotations) = &ExpandAnnotation($symbol, $member_descr);
1552                 $member_descr = &ExpandAbbreviations($symbol, $member_descr);
1553                 $desc .= "<listitem><simpara>$member_descr$param_annotations</simpara></listitem>\n";
1554             } else {
1555                 $desc .= "<listitem></listitem>\n";
1556             }
1557             $desc .= "</varlistentry>\n";
1558         }
1560         $desc .= "</variablelist>";
1561     }
1563     $desc .= OutputSymbolTraits ($symbol);
1564     $desc .= "</refsect2>\n";
1565     return ($synop, $desc);
1569 #############################################################################
1570 # Function    : OutputUnion
1571 # Description : Returns the synopsis and detailed description of a union.
1572 # Arguments   : $symbol - the union.
1573 #               $declaration - the declaration of the union.
1574 #############################################################################
1576 sub OutputUnion {
1577     my ($symbol, $declaration) = @_;
1578     my $id = &CreateValidSGMLID ($symbol);
1579     my $condition = &MakeConditionDescription ($symbol);
1580     my $synop = &MakeReturnField("union") . "<link linkend=\"$id\">$symbol</link>;\n";
1581     my $desc = "<refsect2 id=\"$id\" role=\"union\"$condition>\n<title>union $symbol</title>\n";
1583     $desc .= MakeIndexterms($symbol, $id);
1584     $desc .= "\n";
1585     $desc .= OutputSymbolExtraLinks($symbol);
1587     my $decl_out = &CreateValidSGML ($declaration);
1588     $desc .= "<programlisting>$decl_out</programlisting>\n";
1590     $desc .= &MakeDeprecationNote($symbol);
1592     if (defined ($SymbolDocs{$symbol})) {
1593         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1594     }
1595     $desc .= OutputSymbolTraits ($symbol);
1596     $desc .= "</refsect2>\n";
1597     return ($synop, $desc);
1601 #############################################################################
1602 # Function    : OutputVariable
1603 # Description : Returns the synopsis and detailed description of a variable.
1604 # Arguments   : $symbol - the extern'ed variable.
1605 #               $declaration - the declaration of the variable.
1606 #############################################################################
1608 sub OutputVariable {
1609     my ($symbol, $declaration) = @_;
1610     my $id = &CreateValidSGMLID ($symbol);
1611     my $condition = &MakeConditionDescription ($symbol);
1613     my $synop;
1614     if ($declaration =~ m/^\s*extern\s+((const\s+|unsigned\s+)*\w+)(\s+\*+|\*+|\s)(\s*)([A-Za-z]\w*)\s*;/) {
1615         my $mod = defined ($1) ? $1 : "";
1616         my $ptr = defined ($3) ? $3 : "";
1617         my $space = defined ($4) ? $4 : "";
1618         $synop = &MakeReturnField("extern") . "$mod$ptr$space<link linkend=\"$id\">$symbol</link>;\n";
1620     } else {
1621         $synop = &MakeReturnField("extern") . "<link linkend=\"$id\">$symbol</link>;\n";
1622     }
1624     my $desc = "<refsect2 id=\"$id\" role=\"variable\"$condition>\n<title>$symbol</title>\n";
1626     $desc .= MakeIndexterms($symbol, $id);
1627     $desc .= "\n";
1628     $desc .= OutputSymbolExtraLinks($symbol);
1630     my $decl_out = &CreateValidSGML ($declaration);
1631     $desc .= "<programlisting>$decl_out</programlisting>\n";
1633     $desc .= &MakeDeprecationNote($symbol);
1635     if (defined ($SymbolDocs{$symbol})) {
1636         $desc .= &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1637     }
1638     $desc .= OutputSymbolTraits ($symbol);
1639     $desc .= "</refsect2>\n";
1640     return ($synop, $desc);
1644 #############################################################################
1645 # Function    : OutputFunction
1646 # Description : Returns the synopsis and detailed description of a function.
1647 # Arguments   : $symbol - the function.
1648 #               $declaration - the declaration of the function.
1649 #############################################################################
1651 sub OutputFunction {
1652     my ($symbol, $declaration, $symbol_type) = @_;
1653     my $id = &CreateValidSGMLID ($symbol);
1654     my $condition = &MakeConditionDescription ($symbol);
1656     # Take out the return type     $1                                                                           $3   $4
1657     $declaration =~ s/<RETURNS>\s*((const\s+|G_CONST_RETURN\s+|unsigned\s+|long\s+|short\s+|struct\s+|enum\s+)*)(\w+)(\s*\**\s*(const|G_CONST_RETURN)?\s*\**\s*(restrict)?\s*)<\/RETURNS>\n//;
1658     my $type_modifier = defined($1) ? $1 : "";
1659     my $type = $3;
1660     my $pointer = $4;
1661     #print "$symbol pointer is $pointer\n";
1662     my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1663     my $start = "";
1664     #if ($symbol_type eq 'USER_FUNCTION') {
1665     #    $start = "typedef ";
1666     #}
1668     # We output const rather than G_CONST_RETURN.
1669     $type_modifier =~ s/G_CONST_RETURN/const/g;
1670     $pointer =~ s/G_CONST_RETURN/const/g;
1671     $pointer =~ s/^\s+/ /g;
1673     my $ret_type_len = length ($start) + length ($type_modifier)
1674         + length ($pointer) + length ($type);
1675     my $ret_type_output;
1676     my $symbol_len;
1677     if ($ret_type_len < $RETURN_TYPE_FIELD_WIDTH) {
1678         $ret_type_output = "$start$type_modifier$xref$pointer"
1679             . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
1680         $symbol_len = 0;
1681     } else {
1682 #       $ret_type_output = "$start$type_modifier$xref$pointer\n"
1683 #           . (' ' x $RETURN_TYPE_FIELD_WIDTH);
1685         $ret_type_output = "$start$type_modifier$xref$pointer ";
1686         $symbol_len = $ret_type_len + 1 - $RETURN_TYPE_FIELD_WIDTH;
1687     }
1689     $symbol_len += length ($symbol);
1690     my $char1 = my $char2 = my $char3 = "";
1691     if ($symbol_type eq 'USER_FUNCTION') {
1692         $symbol_len += 3;
1693         $char1 = "(";
1694         $char2 = "*";
1695         $char3 = ")";
1696     }
1698     my ($symbol_output, $symbol_desc_output);
1699     if ($symbol_len < $SYMBOL_FIELD_WIDTH) {
1700         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3"
1701             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1702         $symbol_desc_output = "$char1$char2$symbol$char3"
1703             . (' ' x ($SYMBOL_FIELD_WIDTH - $symbol_len));
1704     } else {
1705         $symbol_output = "$char1<link linkend=\"$id\">$char2$symbol</link>$char3\n"
1706             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1707         $symbol_desc_output = "$char1$char2$symbol$char3\n"
1708             . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
1709     }
1711     my $synop = $ret_type_output . $symbol_output . '(';
1712     my $desc = "<refsect2 id=\"$id\" role=\"function\"$condition>\n<title>${symbol} ()</title>\n";
1714     $desc .= MakeIndexterms($symbol, $id);
1715     $desc .= "\n";
1716     $desc .= OutputSymbolExtraLinks($symbol);
1718     $desc  .= "<programlisting>${ret_type_output}$symbol_desc_output(";
1720     my $param_num = 0;
1721     while ($declaration ne "") {
1722         #print "$declaration";
1724         if ($declaration =~ s/^[\s,]+//) {
1725             # skip whitespace and commas
1726             next;
1728         } elsif ($declaration =~ s/^void\s*[,\n]//) {
1729             $synop .= "void";
1730             $desc  .= "void";
1732         } elsif ($declaration =~ s/^...\s*[,\n]//) {
1733             if ($param_num == 0) {
1734                 $synop .= "...";
1735                 $desc  .= "...";
1736             } else {
1737                 $synop .= ",\n"
1738                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1739                     . " ...";
1740                 $desc  .= ",\n"
1741                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1742                     . " ...";
1743             }
1745         # allow alphanumerics, '_', '[' & ']' in param names
1746         # Try to match a standard parameter (keep in sync with gtkdoc-mktmpl)
1747         #                                $1                                                                                                                                    $2                             $3                                                           $4       $5
1748         } 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]//) {
1749             my $pre     = defined($1) ? $1 : "";
1750             my $type    = $2;
1751             my $ptr     = defined($3) ? $3 : "";
1752             my $name    = defined($4) ? $4 : "";
1753             my $array   = defined($5) ? $5 : "";
1755             $pre  =~ s/\s+/ /g;
1756             $type =~ s/\s+/ /g;
1757             $ptr  =~ s/\s+/ /g;
1758             $ptr  =~ s/\s+$//;
1759             if ($ptr && $ptr !~ m/\*$/) { $ptr .= " "; }
1761             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1763             if (($name eq "") && $pre =~ m/^((un)?signed .*)\s?/ ) {
1764                 $name = $type;
1765                 $type = "$1";
1766                 $pre = "";
1767             }
1769             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1770             my $label   = "$pre$xref $ptr$name$array";
1772             #print "$symbol: '$pre' '$type' '$ptr' '$name' '$array'\n";
1774             if ($param_num == 0) {
1775                 $synop .= "$label";
1776                 $desc  .= "$label";
1777             } else {
1778                 $synop .= ",\n"
1779                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1780                     . " $label";
1781                 $desc  .= ",\n"
1782                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1783                     . " $label";
1784             }
1786         # Try to match parameters which are functions (keep in sync with gtkdoc-mktmpl)
1787         #                              $1                                       $2          $3      $4                        $5                    $7             $8
1788         } 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]//) {
1789             my $mod1 = defined($1) ? $1 : "";
1790             if (defined($2)) { $mod1 .= $2; }
1791             my $type = $3;
1792             my $ptr1 = $4;
1793             my $mod2 = defined($5) ? $5 : "";
1794             my $func_ptr = $6;
1795             my $name = $7;
1796             my $func_params = defined($8) ? $8 : "";
1797             
1798             #if (!defined($type)) { print "## no type\n"; };
1799             #if (!defined($ptr1)) { print "## no ptr1\n"; };
1800             #if (!defined($func_ptr)) { print "## no func_ptr\n"; };
1801             #if (!defined($name)) { print "## no name\n"; };
1803             if ($ptr1 && $ptr1 !~ m/\*$/) { $ptr1 .= " "; }
1804             $func_ptr  =~ s/\s+//g;
1805             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
1806             my $label = "$mod1$xref$ptr1$mod2 ($func_ptr$name) ($func_params)";
1808             #print "Type: [$mod1][$xref][$ptr1][$mod2] ([$func_ptr][$name]) ($func_params)\n";
1809             if ($param_num == 0) {
1810                 $synop .= "$label";
1811                 $desc  .= "$label";
1812             } else {
1813                 $synop .= ",\n"
1814                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1815                     . " $label";
1816                 $desc  .= ",\n"
1817                     . (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH))
1818                     . " $label";
1819             }
1821         } else {
1822             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
1823                 "Can't parse args for function $symbol: $declaration");
1824             last;
1825         }
1826         $param_num++;
1827     }
1828     $synop .= ");\n";
1829     $desc  .= ");</programlisting>\n";
1831     $desc .= &MakeDeprecationNote($symbol);
1833     my $parameters = &OutputParamDescriptions ("FUNCTION", $symbol);
1834     my $parameters_output = 0;
1836     if (defined ($SymbolDocs{$symbol})) {
1837         my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
1839         # Try to insert the parameter table at the author's desired position.
1840         # Otherwise we need to tag it onto the end.
1841         if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
1842           $parameters_output = 1;
1843         }
1844         $desc .= $symbol_docs;
1845     }
1847     if ($parameters_output == 0) {
1848         $desc .= $parameters;
1849     }
1851     $desc .= OutputSymbolTraits ($symbol);
1852     $desc .= "</refsect2>\n";
1853     return ($synop, $desc);
1857 #############################################################################
1858 # Function    : OutputParamDescriptions
1859 # Description : Returns the DocBook output describing the parameters of a
1860 #               function, macro or signal handler.
1861 # Arguments   : $symbol_type - 'FUNCTION', 'MACRO' or 'SIGNAL'. Signal
1862 #                 handlers have an implicit user_data parameter last.
1863 #               $symbol - the name of the function/macro being described.
1864 #############################################################################
1866 sub OutputParamDescriptions {
1867     my ($symbol_type, $symbol) = @_;
1868     my $output = "";
1869     if (defined ($SymbolParams{$symbol})) {
1870         my $returns = "";
1871         my $params = $SymbolParams{$symbol};
1872         my $params_desc = "";
1873         my $j;
1874         for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
1875             my $param_name = $$params[$j];
1876             my $param_desc = $$params[$j + 1];
1877             my $param_annotations = "";
1879             ($param_desc,$param_annotations) = & ExpandAnnotation($symbol, $param_desc);
1880             $param_desc = &ExpandAbbreviations($symbol, $param_desc);
1881             if ($param_name eq "Returns") {
1882                 $returns = "$param_desc$param_annotations";
1883             } else {
1884                 if ($param_name eq "Varargs") {
1885                     $param_name = "...";
1886                 }
1887                 $params_desc .= "<varlistentry><term><parameter>$param_name</parameter>&#160;:</term>\n<listitem><simpara>$param_desc$param_annotations</simpara></listitem></varlistentry>\n";
1888             }
1889         }
1891         # Signals have an implicit user_data parameter which we describe.
1892         if ($symbol_type eq "SIGNAL") {
1893             $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";
1894         }
1896         # Start a table if we need one.
1897         if ($params_desc || $returns) {
1898             $output .= "<variablelist role=\"params\">\n";
1899             if ($params_desc ne "") {
1900                 #$output .= "<varlistentry><term>Parameters:</term><listitem></listitem></varlistentry>\n";
1901                 $output .= $params_desc;
1902             }
1904             # Output the returns info last.
1905             if ($returns) {
1906                 $output .= "<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>$returns</simpara></listitem></varlistentry>\n";
1907             }
1909             # Finish the table.
1910             $output .= "</variablelist>";
1911         }
1912     }
1913     return $output;
1917 #############################################################################
1918 # Function    : ParseStabilityLevel
1919 # Description : Parses a stability level and outputs a warning if it isn't
1920 #               valid.
1921 # Arguments   : $stability - the stability text.
1922 #               $file, $line - context for error message
1923 #               $message - description of where the level is from, to use in
1924 #               any error message.
1925 # Returns     : The parsed stability level string.
1926 #############################################################################
1928 sub ParseStabilityLevel {
1929     my ($stability, $file, $line, $message) = @_;
1931     $stability =~ s/^\s*//;
1932     $stability =~ s/\s*$//;
1933     if ($stability =~ m/^stable$/i) {
1934         $stability = "Stable";
1935     } elsif ($stability =~ m/^unstable$/i) {
1936         $stability = "Unstable";
1937     } elsif ($stability =~ m/^private$/i) {
1938         $stability = "Private";
1939     } else {
1940         &LogWarning ($file, $line, "$message is $stability.".
1941             "It should be one of these: Stable, Unstable, or Private.");
1942     }
1943     return $stability;
1947 #############################################################################
1948 # Function    : OutputSGMLFile
1949 # Description : Outputs the final DocBook file for one section.
1950 # Arguments   : $file - the name of the file.
1951 #               $title - the title from the $MODULE-sections.txt file, which
1952 #                 will be overridden by the title in the template file.
1953 #               $section_id - the SGML id to use for the toplevel tag.
1954 #               $includes - comma-separates list of include files added at top
1955 #                 of synopsis, with '<' '>' around them (if not already enclosed in "").
1956 #               $synopsis - reference to the DocBook for the Synopsis part.
1957 #               $details - reference to the DocBook for the Details part.
1958 #               $signal_synop - reference to the DocBook for the Signal Synopsis part
1959 #               $signal_desc - reference to the DocBook for the Signal Description part
1960 #               $args_synop - reference to the DocBook for the Arg Synopsis part
1961 #               $args_desc - reference to the DocBook for the Arg Description part
1962 #               $hierarchy - reference to the DocBook for the Object Hierarchy part
1963 #               $interfaces - reference to the DocBook for the Interfaces part
1964 #               $implementations - reference to the DocBook for the Known Implementations part
1965 #               $prerequisites - reference to the DocBook for the Prerequisites part
1966 #               $derived - reference to the DocBook for the Derived Interfaces part
1967 #               $file_objects - reference to an array of objects in this file
1968 #############################################################################
1970 sub OutputSGMLFile {
1971     my ($file, $title, $section_id, $includes, $synopsis, $details, $signals_synop, $signals_desc, $args_synop, $args_desc, $hierarchy, $interfaces, $implementations, $prerequisites, $derived, $file_objects) = @_;
1973     #print "Output sgml for file $file with title '$title'\n";
1974     
1975     # The edited title overrides the one from the sections file.
1976     my $new_title = $SymbolDocs{"$TMPL_DIR/$file:Title"};
1977     if (defined ($new_title) && $new_title !~ m/^\s*$/) {
1978         $title = $new_title;
1979         #print "Found title: $title\n";
1980     }
1981     my $short_desc = $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
1982     if (!defined ($short_desc) || $short_desc =~ m/^\s*$/) {
1983         $short_desc = "";
1984     } else {
1985         $short_desc = &ExpandAbbreviations("$title:Short_description",
1986                                            $short_desc);
1987         #print "Found short_desc: $short_desc";
1988     }
1989     my $long_desc = $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
1990     if (!defined ($long_desc) || $long_desc =~ m/^\s*$/) {
1991         $long_desc = "";
1992     } else {
1993         $long_desc = &ExpandAbbreviations("$title:Long_description",
1994                                           $long_desc);
1995         #print "Found long_desc: $long_desc";
1996     }
1997     my $see_also = $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
1998     if (!defined ($see_also) || $see_also =~ m%^\s*(<para>)?\s*(</para>)?\s*$%) {
1999         $see_also = "";
2000     } else {
2001         $see_also = &ExpandAbbreviations("$title:See_Also", $see_also);
2002         #print "Found see_also: $see_also";
2003     }
2004     if ($see_also) {
2005         $see_also = "<refsect1 id=\"$section_id.see-also\">\n<title>See Also</title>\n$see_also\n</refsect1>\n";
2006     }
2007     my $stability = $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
2008     if (!defined ($stability) || $stability =~ m/^\s*$/) {
2009         $stability = "";
2010     } else {
2011         $stability = &ParseStabilityLevel($stability, $file, $., "Section stability level");
2012         #print "Found stability: $stability";
2013     }
2014     if ($stability) {
2015         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$stability, unless otherwise indicated\n</refsect1>\n";
2016     } elsif ($DEFAULT_STABILITY) {
2017         $stability = "<refsect1 id=\"$section_id.stability-level\">\n<title>Stability Level</title>\n$DEFAULT_STABILITY, unless otherwise indicated\n</refsect1>\n";
2018     }
2020     my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
2021         gmtime (time);
2022     my $month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
2023     $year += 1900;
2025     my $include_output = "";
2026     my $include;
2027     foreach $include (split (/,/, $includes)) {
2028         if ($include =~ m/^\".+\"$/) {
2029             $include_output .= "#include ${include}\n";
2030         }
2031         else {
2032             $include =~ s/^\s+|\s+$//gs;
2033             $include_output .= "#include &lt;${include}&gt;\n";
2034         }
2035     }
2036     if ($include_output ne '') {
2037         $include_output = "\n$include_output\n";
2038     }
2039     
2040     my $extralinks = OutputSectionExtraLinks($title,"Section:$file");
2042     my $old_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT";
2043     my $new_sgml_file = "$SGML_OUTPUT_DIR/$file.$OUTPUT_FORMAT.new";
2045     open (OUTPUT, ">$new_sgml_file")
2046         || die "Can't create $new_sgml_file: $!";
2048     my $object_anchors = "";
2049     foreach my $object (@$file_objects) {
2050         next if ($object eq $section_id);
2051         my $id = CreateValidSGMLID($object);
2052         #print "Debug: Adding anchor for $object\n";
2053         $object_anchors .= "<anchor id=\"$id\"$empty_element_end";
2054     }
2056     # We used to output this, but is messes up our UpdateFileIfChanged code
2057     # since it changes every day (and it is only used in the man pages):
2058     # "<refentry id="$section_id" revision="$mday $month $year">"
2060     if (lc($OUTPUT_FORMAT) eq "xml") {
2061         print OUTPUT $doctype_header;
2062     }
2064     print OUTPUT <<EOF;
2065 <refentry id="$section_id">
2066 <refmeta>
2067 <refentrytitle role="top_of_page" id="$section_id.top_of_page">$title</refentrytitle>
2068 <manvolnum>3</manvolnum>
2069 <refmiscinfo>\U$MODULE\E Library</refmiscinfo>
2070 </refmeta>
2071 <refnamediv>
2072 <refname>$title</refname>
2073 <refpurpose>$short_desc</refpurpose>
2074 </refnamediv>
2075 $stability
2076 <refsynopsisdiv id="$section_id.synopsis" role="synopsis">
2077 <title role="synopsis.title">Synopsis</title>
2078 $object_anchors
2079 <synopsis>$include_output$${synopsis}</synopsis>
2080 </refsynopsisdiv>
2081 $$hierarchy$$prerequisites$$derived$$interfaces$$implementations$$args_synop$$signals_synop
2082 <refsect1 id="$section_id.description" role="desc">
2083 <title role="desc.title">Description</title>
2084 $extralinks$long_desc
2085 </refsect1>
2086 <refsect1 id="$section_id.details" role="details">
2087 <title role="details.title">Details</title>
2088 $$details
2089 </refsect1>
2090 $$args_desc$$signals_desc$see_also
2091 </refentry>
2093     close (OUTPUT);
2095     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2099 #############################################################################
2100 # Function    : OutputExtraFile
2101 # Description : Copies an "extra" DocBook file into the output directory,
2102 #               expanding abbreviations
2103 # Arguments   : $file - the source file.
2104 #############################################################################
2105 sub OutputExtraFile {
2106     my ($file) = @_;
2108     my $basename;
2110     ($basename = $file) =~ s!^.*/!!;
2112     my $old_sgml_file = "$SGML_OUTPUT_DIR/$basename";
2113     my $new_sgml_file = "$SGML_OUTPUT_DIR/$basename.new";
2115     my $contents;
2117     open(EXTRA_FILE, "<$file") || die "Can't open $file";
2119     {
2120         local $/;
2121         $contents = <EXTRA_FILE>;
2122     }
2124     open (OUTPUT, ">$new_sgml_file")
2125         || die "Can't create $new_sgml_file: $!";
2127     print OUTPUT &ExpandAbbreviations ("$basename file", $contents);
2128     close (OUTPUT);
2130     return &UpdateFileIfChanged ($old_sgml_file, $new_sgml_file, 0);
2132 #############################################################################
2133 # Function    : OutputBook
2134 # Description : Outputs the SGML entities that need to be included into the
2135 #               main SGML file for the module.
2136 # Arguments   : $book_top - the declarations of the entities, which are added
2137 #                 at the top of the main SGML file.
2138 #               $book_bottom - the references to the entities, which are
2139 #                 added in the main SGML file at the desired position.
2140 #############################################################################
2142 sub OutputBook {
2143     my ($book_top, $book_bottom) = @_;
2145     my $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top";
2146     my $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.top.new";
2148     open (OUTPUT, ">$new_file")
2149         || die "Can't create $new_file: $!";
2150     print OUTPUT $book_top;
2151     close (OUTPUT);
2153     &UpdateFileIfChanged ($old_file, $new_file, 0);
2156     $old_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom";
2157     $new_file = "$SGML_OUTPUT_DIR/$MODULE-doc.bottom.new";
2159     open (OUTPUT, ">$new_file")
2160         || die "Can't create $new_file: $!";
2161     print OUTPUT $book_bottom;
2162     close (OUTPUT);
2164     &UpdateFileIfChanged ($old_file, $new_file, 0);
2167     # If the main SGML file hasn't been created yet, we create it here.
2168     # The user can tweak it later.
2169     if ($MAIN_SGML_FILE && ! -e $MAIN_SGML_FILE) {
2170       open (OUTPUT, ">$MAIN_SGML_FILE")
2171         || die "Can't create $MAIN_SGML_FILE: $!";
2173       if (lc($OUTPUT_FORMAT) eq "xml") {
2174           print OUTPUT <<EOF;
2175 <?xml version="1.0"?>
2176 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
2177                "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"
2179   <!ENTITY % local.common.attrib "xmlns:xi  CDATA  #FIXED 'http://www.w3.org/2003/XInclude'">
2181 <book id="index">
2183       } else {
2184         print OUTPUT <<EOF;
2185 <!doctype book PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
2186 $book_top
2188 <book id="index">
2190       }
2192 print OUTPUT <<EOF;
2193   <bookinfo>
2194     <title>$MODULE Reference Manual</title>
2195     <releaseinfo>
2196       for $MODULE [VERSION].
2197       The latest version of this documentation can be found on-line at
2198       <ulink role="online-location" url="http://[SERVER]/$MODULE/index.html">http://[SERVER]/$MODULE/</ulink>.
2199     </releaseinfo>
2200   </bookinfo>
2202   <chapter>
2203     <title>[Insert title here]</title>
2204     $book_bottom
2205   </chapter>
2207   if (-e $OBJECT_TREE_FILE) {
2208     print OUTPUT <<EOF;
2209   <chapter id="object-tree">
2210     <title>Object Hierarchy</title>
2211      <xi:include href="xml/tree_index.sgml"/>
2212   </chapter>
2214   }
2216 print OUTPUT <<EOF;
2217   <index id="api-index-full">
2218     <title>API Index</title>
2219     <xi:include href="xml/api-index-full.xml"><xi:fallback /></xi:include>
2220   </index>
2222   <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
2223 </book>
2226       close (OUTPUT);
2227     }
2231 #############################################################################
2232 # Function    : CreateValidSGML
2233 # Description : This turns any chars which are used in SGML into entities,
2234 #               e.g. '<' into '&lt;'
2235 # Arguments   : $text - the text to turn into proper SGML.
2236 #############################################################################
2238 sub CreateValidSGML {
2239     my ($text) = @_;
2240     $text =~ s/&/&amp;/g;       # Do this first, or the others get messed up.
2241     $text =~ s/</&lt;/g;
2242     $text =~ s/>/&gt;/g;
2243     # browers render single tabs inconsistently
2244     $text =~ s/([^\s])\t([^\s])/$1&#160;$2/g;
2245     return $text;
2248 #############################################################################
2249 # Function    : ConvertSGMLChars
2250 # Description : This is used for text in source code comment blocks, to turn
2251 #               chars which are used in SGML into entities, e.g. '<' into
2252 #               '&lt;'. Depending on $SGML_MODE, this is done
2253 #               unconditionally or only if the character doesn't seem to be
2254 #               part of an SGML construct (tag or entity reference).
2255 # Arguments   : $text - the text to turn into proper SGML.
2256 #############################################################################
2258 sub ConvertSGMLChars {
2259     my ($symbol, $text) = @_;
2261     if ($SGML_MODE) {
2262         # For the SGML mode only convert to entities outside CDATA sections.
2263         return &ModifyXMLElements ($text, $symbol,
2264                                    "<!\\[CDATA\\[|<programlisting[^>]*>",
2265                                    \&ConvertSGMLCharsEndTag,
2266                                    \&ConvertSGMLCharsCallback);
2267     } else {
2268         # For the simple non-sgml mode, convert to entities everywhere.
2269         $text =~ s/&/&amp;/g;   # Do this first, or the others get messed up.
2270         $text =~ s/</&lt;/g;
2271         $text =~ s/>/&gt;/g;
2272         return $text;
2273     }
2277 sub ConvertSGMLCharsEndTag {
2278   if ($_[0] eq "<!\[CDATA\[") {
2279     return "]]>";
2280   } else {
2281     return "</programlisting>";
2282   }
2285 sub ConvertSGMLCharsCallback {
2286   my ($text, $symbol, $tag) = @_;
2288   if ($tag =~ m/^<programlisting/) {
2289     # We can handle <programlisting> specially here.
2290     return &ModifyXMLElements ($text, $symbol,
2291                                "<!\\[CDATA\\[",
2292                                \&ConvertSGMLCharsEndTag,
2293                                \&ConvertSGMLCharsCallback2);
2294   } elsif ($tag eq "") {
2295     # If we're not in CDATA convert to entities.
2296     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2297     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2298     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2300     # Handle "#include <xxxxx>"
2301     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2302   }
2304   return $text;
2307 sub ConvertSGMLCharsCallback2 {
2308   my ($text, $symbol, $tag) = @_;
2310   # If we're not in CDATA convert to entities.
2311   # We could handle <programlisting> differently, though I'm not sure it helps.
2312   if ($tag eq "") {
2313     # replace only if its not a tag
2314     $text =~ s/&(?![a-zA-Z#]+;)/&amp;/g;        # Do this first, or the others get messed up.
2315     $text =~ s/<(?![a-zA-Z\/!])/&lt;/g;
2316     $text =~ s/(?<![a-zA-Z0-9"'\/-])>/&gt;/g;
2318     # Handle "#include <xxxxx>"
2319     $text =~ s/#include(\s+)<([^>]+)>/#include$1&lt;$2&gt;/g;
2320   }
2322   return $text;
2325 #############################################################################
2326 # Function    : ExpandAnnotation
2327 # Description : This turns annotations into acrony tags.
2328 # Arguments   : $symbol - the symbol being documented, for error messages.
2329 #               $text - the text to expand.
2330 #############################################################################
2331 sub ExpandAnnotation {
2332     my ($symbol, $param_desc) = @_;
2333     my $param_annotations = "";
2334     
2335     if ($param_desc =~ m%\s*\((.*)\):%) {
2336         my @annotations;
2337         my $annotation;
2338         my $annotation_extra = "";
2339         $param_desc = $';
2340     
2341         @annotations = split(/\)\s*\(/,$1);
2342         foreach $annotation (@annotations) {
2343             # need to search for the longest key-match in %AnnotationDefinition
2344             my $match_length=0;
2345             my $match_annotation="";
2346             my $annotationdef;
2347             foreach $annotationdef (keys %AnnotationDefinition) {
2348                 if ($annotation =~ m/^$annotationdef/) {
2349                     if (length($annotationdef)>$match_length) {
2350                         $match_length=length($annotationdef);
2351                         $match_annotation=$annotationdef;
2352                     }
2353                 }
2354             }
2355             if ($match_annotation ne "") {
2356                 if ($annotation =~ m%$match_annotation\s+(.*)%) {
2357                     $annotation_extra = " $1";
2358                 }
2359                 $AnnotationsUsed{$match_annotation} = 1;
2360                 $param_annotations .= "<acronym>$match_annotation</acronym>$annotation_extra. ";
2361             }
2362             else {
2363                 &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2364                     "unknown annotation \"$annotation\" in documentation for $symbol.");
2365                 $param_annotations=$annotation;
2366             }
2367         }
2368         chomp($param_desc);
2369         $param_desc =~ m/^(.*)\.*\s*$/;
2370         $param_desc = "$1. ";
2371     }    
2372     return ($param_desc, $param_annotations);
2375 #############################################################################
2376 # Function    : ExpandAbbreviations
2377 # Description : This turns the abbreviations function(), macro(), @param,
2378 #               %constant, and #symbol into appropriate DocBook markup.
2379 #               CDATA sections and <programlisting> parts are skipped.
2380 # Arguments   : $symbol - the symbol being documented, for error messages.
2381 #               $text - the text to expand.
2382 #############################################################################
2384 sub ExpandAbbreviations {
2385   my ($symbol, $text) = @_;
2387   # Convert "|[" and "]|" into the start and end of program listing examples.
2388   $text =~ s%\|\[%<informalexample><programlisting>%g;
2389   $text =~ s%\]\|%</programlisting></informalexample>%g;
2390   # TODO: check for a xml comment after |[ and pick the language attribute from
2391   # that
2393   # keep CDATA unmodified, preserve ulink tags (ideally we preseve all tags
2394   # as such)
2395   return &ModifyXMLElements ($text, $symbol,
2396                              "<!\\[CDATA\\[|<ulink[^>]*>|<programlisting[^>]*>|<!DOCTYPE",
2397                              \&ExpandAbbreviationsEndTag,
2398                              \&ExpandAbbreviationsCallback);
2402 # Returns the end tag corresponding to the given start tag.
2403 sub ExpandAbbreviationsEndTag {
2404   my ($start_tag) = @_;
2406   if ($start_tag eq "<!\[CDATA\[") {
2407     return "]]>";
2408   } elsif ($start_tag eq "<!DOCTYPE") {
2409     return "]>";
2410   } elsif ($start_tag =~ m/<(\w+)/) {
2411     return "</$1>";
2412   }
2415 # Called inside or outside each CDATA or <programlisting> section.
2416 sub ExpandAbbreviationsCallback {
2417   my ($text, $symbol, $tag) = @_;
2419   if ($tag =~ m/^<programlisting/) {
2420     # Handle any embedded CDATA sections.
2421     return &ModifyXMLElements ($text, $symbol,
2422                                "<!\\[CDATA\\[",
2423                                \&ExpandAbbreviationsEndTag,
2424                                \&ExpandAbbreviationsCallback2);
2425   } elsif ($tag eq "") {
2426     # We are outside any CDATA or <programlisting> sections, so we expand
2427     # any gtk-doc abbreviations.
2429     # Convert 'function()' or 'macro()'.
2430     # if there is abc_*_def() we don't want to make a link to _def()
2431     # FIXME: also handle abc(....) : but that would need to be done recursively :/
2432     $text =~ s/([^\*.\w])(\w+)\s*\(\)/$1.&MakeXRef($2, &tagify($2 . "()", "function"));/eg;
2433     # handle #Object.func()
2434     $text =~ s/(\A|[^\\])#([\w\-:\.]+[\w]+)\s*\(\)/$1.&MakeXRef($2, &tagify($2 . "()", "function"));/eg;
2436     # Convert '@param', but not '\@param'.
2437     $text =~ s/(\A|[^\\])\@(\w+((\.|->)\w+)*)/$1<parameter>$2<\/parameter>/g;
2438     $text =~ s/\\\@/\@/g;
2440     # Convert '%constant', but not '\%constant'.
2441     # Also allow negative numbers, e.g. %-1.
2442     $text =~ s/(\A|[^\\])\%(-?\w+)/$1.&MakeXRef($2, &tagify($2, "literal"));/eg;
2443     $text =~ s/\\\%/\%/g;
2445     # Convert '#symbol', but not '\#symbol'.
2446     $text =~ s/(\A|[^\\])#([\w\-:\.]+[\w]+)/$1.&MakeHashXRef($2, "type");/eg;
2447     $text =~ s/\\#/#/g;
2448     
2449     # Expand urls
2450     # FIXME: should we skip urls that are already tagged? (e.g. <literal>http://...</literal>)
2451     # this is apparently also called for markup and not just for plain text
2452     # disable for now.
2453     #$text =~ s%(http|https|ftp)://(.*?)((?:\s|,|\)|\]|\<|\.\s))%<ulink url="$1://$2">$2</ulink>$3%g;
2454   }
2456   return $text;
2459 # This is called inside a <programlisting>
2460 sub ExpandAbbreviationsCallback2 {
2461   my ($text, $symbol, $tag) = @_;
2463   if ($tag eq "") {
2464     # We are inside a <programlisting> but outside any CDATA sections,
2465     # so we expand any gtk-doc abbreviations.
2466     # FIXME: why is this different from &ExpandAbbreviationsCallback(),
2467     #        why not just call it
2468     $text =~ s/#(\w+)/&MakeHashXRef($1, "");/eg;
2469   }
2471   return $text;
2474 sub MakeHashXRef {
2475     my ($symbol, $tag) = @_;;
2476     my $text = $symbol;
2478     # Check for things like '#include', '#define', and skip them.
2479     if ($PreProcessorDirectives{$symbol}) {
2480       return "#$symbol";
2481     }
2483     # Get rid of any special '-struct' suffix.
2484     $text =~ s/-struct$//;
2486     # If the symbol is in the form "Object::signal", then change the symbol to
2487     # "Object-signal" and use "signal" as the text.
2488     if ($symbol =~ s/::/-/) {
2489       $text = "\"$'\"";
2490     }
2492     # If the symbol is in the form "Object:property", then change the symbol to
2493     # "Object--property" and use "property" as the text.
2494     if ($symbol =~ s/:/--/) {
2495       $text = "\"$'\"";
2496     }
2498     if ($tag ne "") {
2499       $text = tagify ($text, $tag);
2500     }
2501     
2502     return &MakeXRef($symbol, $text);
2506 #############################################################################
2507 # Function    : ModifyXMLElements
2508 # Description : Looks for given XML element tags within the text, and calls
2509 #               the callback on pieces of text inside & outside those elements.
2510 #               Used for special handling of text inside things like CDATA
2511 #               and <programlisting>.
2512 # Arguments   : $text - the text.
2513 #               $symbol - the symbol currently being documented (only used for
2514 #                      error messages).
2515 #               $start_tag_regexp - the regular expression to match start tags.
2516 #                      e.g. "<!\\[CDATA\\[|<programlisting[^>]*>" to match
2517 #                      CDATA sections or programlisting elements.
2518 #               $end_tag_func - function which is passed the matched start tag
2519 #                      and should return the appropriate end tag string.
2520 #               $callback - callback called with each part of the text. It is
2521 #                      called with a piece of text, the symbol being
2522 #                      documented, and the matched start tag or "" if the text
2523 #                      is outside the XML elements being matched.
2524 #############################################################################
2525 sub ModifyXMLElements {
2526     my ($text, $symbol, $start_tag_regexp, $end_tag_func, $callback) = @_;
2527     my ($before_tag, $start_tag, $end_tag_regexp, $end_tag);
2528     my $result = "";
2530     while ($text =~ m/$start_tag_regexp/s) {
2531       $before_tag = $`; # Prematch for last successful match string
2532       $start_tag = $&;  # Last successful match
2533       $text = $';       # Postmatch for last successful match string
2535       $result .= &$callback ($before_tag, $symbol, "");
2536       $result .= $start_tag;
2538       # get the mathing end-tag for current tag
2539       $end_tag_regexp = &$end_tag_func ($start_tag);
2541       if ($text =~ m/$end_tag_regexp/s) {
2542         $before_tag = $`;
2543         $end_tag = $&;
2544         $text = $';
2546         $result .= &$callback ($before_tag, $symbol, $start_tag);
2547         $result .= $end_tag;
2548       } else {
2549         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
2550             "Can't find tag end: $end_tag_regexp in docs for: $symbol.");
2551         # Just assume it is all inside the tag.
2552         $result .= &$callback ($text, $symbol, $start_tag);
2553         $text = "";
2554       }
2555     }
2557     # Handle any remaining text outside the tags.
2558     $result .= &$callback ($text, $symbol, "");
2560     return $result;
2563 sub noop {
2564   return $_[0];
2567 # Adds a tag around some text.
2568 # e.g tagify("Text", "literal") => "<literal>Text</literal>".
2569 sub tagify {
2570    my ($text, $elem) = @_;
2571    return "<" . $elem . ">" . $text . "</" . $elem . ">";
2575 #############################################################################
2576 # Function    : MakeXRef
2577 # Description : This returns a cross-reference link to the given symbol.
2578 #               Though it doesn't try to do this for a few standard C types
2579 #               that it knows won't be in the documentation.
2580 # Arguments   : $symbol - the symbol to try to create a XRef to.
2581 #               $text - text text to put inside the XRef, defaults to $symbol
2582 #############################################################################
2584 sub MakeXRef {
2585     my ($symbol, $text) = ($_[0], $_[1]);
2587     $symbol =~ s/^\s+//;
2588     $symbol =~ s/\s+$//;
2590     if (!defined($text)) {
2591         $text = $symbol;
2593         # Get rid of special '-struct' suffix.
2594         $text =~ s/-struct$//;
2595     }
2597     if ($symbol =~ m/ /) {
2598         return "$text";
2599     }
2601     #print "Getting type link for $symbol -> $text\n";
2603     my $symbol_id = &CreateValidSGMLID ($symbol);
2604     return "<link linkend=\"$symbol_id\">$text</link>";
2608 #############################################################################
2609 # Function    : MakeIndexterms
2610 # Description : This returns a indexterm elements for the given symbol
2611 # Arguments   : $symbol - the symbol to create indexterms for
2612 #############################################################################
2614 sub MakeIndexterms {
2615   my ($symbol, $id) = @_;
2616   my $terms =  "";
2617   my $sortas = "";
2618   
2619   # make the index useful, by ommiting the namespace when sorting
2620   if ($NAME_SPACE ne "") {
2621     if ($symbol =~ m/^$NAME_SPACE\_?(.*)/i) {
2622        $sortas=" sortas=\"$1\"";
2623     }
2624   }
2626   if (exists $Deprecated{$symbol}) {
2627       $terms .= "<indexterm zone=\"$id\" role=\"deprecated\"><primary$sortas>$symbol</primary></indexterm>";
2628       $IndexEntriesDeprecated{$symbol}=$id;
2629       $IndexEntriesFull{$symbol}=$id;
2630   }
2631   if (exists $Since{$symbol}) {
2632      my $since = $Since{$symbol};
2633      $since =~ s/^\s+//;
2634      $since =~ s/\s+$//;
2635      if ($since ne "") {
2636          $terms .= "<indexterm zone=\"$id\" role=\"$since\"><primary$sortas>$symbol</primary></indexterm>";
2637      }
2638      $IndexEntriesSince{$symbol}=$id;
2639      $IndexEntriesFull{$symbol}=$id;
2640   }
2641   if ($terms eq "") {
2642      $terms .= "<indexterm zone=\"$id\"><primary$sortas>$symbol</primary></indexterm>";
2643      $IndexEntriesFull{$symbol}=$id;
2644   }
2646   return $terms;
2649 #############################################################################
2650 # Function    : MakeDeprecationNote
2651 # Description : This returns a deprecation warning for the given symbol.
2652 # Arguments   : $symbol - the symbol to try to create a warning for.
2653 #############################################################################
2655 sub MakeDeprecationNote {
2656     my ($symbol) = $_[0];
2657     my $desc = "";
2658     my $note = "";
2659     if (exists $Deprecated{$symbol}) {
2660         $desc .= "<warning>";
2662         if ($Deprecated{$symbol} =~ /^\s*([0-9\.]+)\s*:/) {
2663                 $desc .= "<para><literal>$symbol</literal> has been deprecated since version $1 and should not be used in newly-written code.";
2664         } else {
2665                 $desc .= "<para><literal>$symbol</literal> is deprecated and should not be used in newly-written code.";
2666         }
2667         if ($Deprecated{$symbol} ne "") {
2668             $note = &ExpandAbbreviations($symbol, $Deprecated{$symbol});
2669             $note =~ s/^\s*([0-9\.]+)\s*:\s*//;
2670             $note =~ s/^\s+//;
2671             $note =~ s/\s+$//;
2672             $note =~ s%\n{2,}%\n</para>\n<para>\n%g;
2673             $desc .= " " . $note;
2674         }
2675         $desc .= "</para></warning>\n";
2676     }
2677     return $desc;
2680 #############################################################################
2681 # Function    : MakeConditionDescription
2682 # Description : This returns a sumary of conditions for the given symbol.
2683 # Arguments   : $symbol - the symbol to try to create the sumary.
2684 #############################################################################
2686 sub MakeConditionDescription {
2687     my ($symbol) = $_[0];
2688     my $desc = "";
2690     if (exists $Deprecated{$symbol}) {
2691         if ($desc ne "") {
2692             $desc .= "|";
2693         }
2695         if ($Deprecated{$symbol} =~ /^\s*(.*?)\s*$/) {
2696                 $desc .= "deprecated:$1";
2697         } else {
2698                 $desc .= "deprecated";
2699         }
2700     }
2702     if (exists $Since{$symbol}) {
2703         if ($desc ne "") {
2704             $desc .= "|";
2705         }
2707         if ($Since{$symbol} =~ /^\s*(.*?)\s*$/) {
2708                 $desc .= "since:$1";
2709         } else {
2710                 $desc .= "since";
2711         }
2712     }
2714     if (exists $StabilityLevel{$symbol}) {
2715         if ($desc ne "") {
2716             $desc .= "|";
2717         }
2718         $desc .= "stability:".$StabilityLevel{$symbol};
2719     }
2721     if ($desc ne "") {
2722         $desc=" condition=\"".$desc."\"";
2723         #print "condition for '$symbol' = '$desc'\n";
2724     }
2725     return $desc;
2728 #############################################################################
2729 # Function    : GetHierarchy
2730 # Description : Returns the DocBook output describing the ancestors and
2731 #               immediate children of a GObject subclass. It uses the
2732 #               global @Objects and @ObjectLevels arrays to walk the tree.
2733 # Arguments   : $object - the GtkObject subclass.
2734 #############################################################################
2736 sub GetHierarchy {
2737     my ($object) = @_;
2739     # Find object in the objects array.
2740     my $found = 0;
2741     my @children = ();
2742     my $i;
2743     my $level;
2744     my $j;
2745     for ($i = 0; $i < @Objects; $i++) {
2746         if ($found) {
2747             if ($ObjectLevels[$i] <= $level) {
2748             last;
2749         }
2750             elsif ($ObjectLevels[$i] == $level + 1) {
2751                 push (@children, $Objects[$i]);
2752             }
2753         }
2754         elsif ($Objects[$i] eq $object) {
2755             $found = 1;
2756             $j = $i;
2757             $level = $ObjectLevels[$i];
2758         }
2759     }
2760     if (!$found) {
2761         return "";
2762     }
2764     # Walk up the hierarchy, pushing ancestors onto the ancestors array.
2765     my @ancestors = ();
2766     push (@ancestors, $object);
2767     #print "Level: $level\n";
2768     while ($level > 1) {
2769         $j--;
2770         if ($ObjectLevels[$j] < $level) {
2771             push (@ancestors, $Objects[$j]);
2772             $level = $ObjectLevels[$j];
2773             #print "Level: $level\n";
2774         }
2775     }
2777     # Output the ancestors list, indented and with links.
2778     my $hierarchy = "<synopsis>\n";
2779     $level = 0;
2780     for ($i = $#ancestors; $i >= 0; $i--) {
2781         my $link_text;
2782         # Don't add a link to the current widget, i.e. when i == 0.
2783         if ($i > 0) {
2784             my $ancestor_id = &CreateValidSGMLID ($ancestors[$i]);
2785             $link_text = "<link linkend=\"$ancestor_id\">$ancestors[$i]</link>";
2786         } else {
2787             $link_text = "$ancestors[$i]";
2788         }
2789         if ($level == 0) {
2790             $hierarchy .= "  $link_text\n";
2791         } else {
2792 #           $hierarchy .= ' ' x ($level * 6 - 3) . "|\n";
2793             $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2794         }
2795         $level++;
2796     }
2797     for ($i = 0; $i <= $#children; $i++) {
2798       my $id = &CreateValidSGMLID ($children[$i]);
2799       my $link_text = "<link linkend=\"$id\">$children[$i]</link>";
2800       $hierarchy .= ' ' x ($level * 6 - 3) . "+----$link_text\n";
2801     }
2802     $hierarchy .= "</synopsis>\n";
2804     return $hierarchy;
2808 #############################################################################
2809 # Function    : GetInterfaces
2810 # Description : Returns the DocBook output describing the interfaces
2811 #               implemented by a class. It uses the global %Interfaces hash.
2812 # Arguments   : $object - the GtkObject subclass.
2813 #############################################################################
2815 sub GetInterfaces {
2816     my ($object) = @_;
2817     my $text = "";
2818     my $i;
2820     # Find object in the objects array.
2821     if (exists($Interfaces{$object})) {
2822         my @ifaces = split(' ', $Interfaces{$object});
2823         $text = <<EOF;
2824 <para>
2825 $object implements
2827         for ($i = 0; $i <= $#ifaces; $i++) {
2828             my $id = &CreateValidSGMLID ($ifaces[$i]);
2829             $text .= " <link linkend=\"$id\">$ifaces[$i]</link>";
2830             if ($i < $#ifaces - 1) {
2831                 $text .= ', ';
2832             }
2833             elsif ($i < $#ifaces) {
2834                 $text .= ' and ';
2835             }
2836             else {
2837                 $text .= '.';
2838             }
2839         }
2840         $text .= <<EOF;
2841 </para>
2843     }
2845     return $text;
2848 #############################################################################
2849 # Function    : GetImplementations
2850 # Description : Returns the DocBook output describing the implementations
2851 #               of an interface. It uses the global %Interfaces hash.
2852 # Arguments   : $object - the GtkObject subclass.
2853 #############################################################################
2855 sub GetImplementations {
2856     my ($object) = @_;
2857     my @impls = ();
2858     my $text = "";
2859     my $i;
2860     foreach my $key (keys %Interfaces) {
2861         if ($Interfaces{$key} =~ /\b$object\b/) {
2862             push (@impls, $key);
2863         }
2864     }
2865     if ($#impls >= 0) {
2866         $text = <<EOF;
2867 <para>
2868 $object is implemented by
2870         for ($i = 0; $i <= $#impls; $i++) {
2871             my $id = &CreateValidSGMLID ($impls[$i]);
2872             $text .= " <link linkend=\"$id\">$impls[$i]</link>";
2873             if ($i < $#impls - 1) {
2874                 $text .= ', ';
2875             }
2876             elsif ($i < $#impls) {
2877                 $text .= ' and ';
2878             }
2879             else {
2880                 $text .= '.';
2881             }
2882         }
2883         $text .= <<EOF;
2884 </para>
2886     }
2887     return $text;
2891 #############################################################################
2892 # Function    : GetPrerequisites
2893 # Description : Returns the DocBook output describing the prerequisites
2894 #               of an interface. It uses the global %Prerequisites hash.
2895 # Arguments   : $iface - the interface.
2896 #############################################################################
2898 sub GetPrerequisites {
2899     my ($iface) = @_;
2900     my $text = "";
2901     my $i;
2903     if (exists($Prerequisites{$iface})) {
2904         $text = <<EOF;
2905 <para>
2906 $iface requires
2908         my @prereqs = split(' ', $Prerequisites{$iface});
2909         for ($i = 0; $i <= $#prereqs; $i++) {
2910             my $id = &CreateValidSGMLID ($prereqs[$i]);
2911             $text .= " <link linkend=\"$id\">$prereqs[$i]</link>";
2912             if ($i < $#prereqs - 1) {
2913                 $text .= ', ';
2914             }
2915             elsif ($i < $#prereqs) {
2916                 $text .= ' and ';
2917             }
2918             else {
2919                 $text .= '.';
2920             }
2921         }
2922         $text .= <<EOF;
2923 </para>
2925     }
2926     return $text;
2929 #############################################################################
2930 # Function    : GetDerived
2931 # Description : Returns the DocBook output describing the derived interfaces
2932 #               of an interface. It uses the global %Prerequisites hash.
2933 # Arguments   : $iface - the interface.
2934 #############################################################################
2936 sub GetDerived {
2937     my ($iface) = @_;
2938     my $text = "";
2939     my $i;
2941     my @derived = ();
2942     foreach my $key (keys %Prerequisites) {
2943         if ($Prerequisites{$key} =~ /\b$iface\b/) {
2944             push (@derived, $key);
2945         }
2946     }
2947     if ($#derived >= 0) {
2948         $text = <<EOF;
2949 <para>
2950 $iface is required by
2952         for ($i = 0; $i <= $#derived; $i++) {
2953             my $id = &CreateValidSGMLID ($derived[$i]);
2954             $text .= " <link linkend=\"$id\">$derived[$i]</link>";
2955             if ($i < $#derived - 1) {
2956                 $text .= ', ';
2957             }
2958             elsif ($i < $#derived) {
2959                 $text .= ' and ';
2960             }
2961             else {
2962                 $text .= '.';
2963             }
2964         }
2965         $text .= <<EOF;
2966 </para>
2968     }
2969     return $text;
2973 #############################################################################
2974 # Function    : GetSignals
2975 # Description : Returns the synopsis and detailed description DocBook output
2976 #               for the signal handlers of a given GtkObject subclass.
2977 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
2978 #############################################################################
2980 sub GetSignals {
2981     my ($object) = @_;
2982     my $synop = "";
2983     my $desc = "";
2985     my $i;
2986     for ($i = 0; $i <= $#SignalObjects; $i++) {
2987         if ($SignalObjects[$i] eq $object) {
2988             #print "Found signal: $SignalNames[$i]\n";
2989             my $name = $SignalNames[$i];
2990             my $symbol = "${object}::${name}";
2991             my $id = &CreateValidSGMLID ("$object-$name");
2993             my $pad = ' ' x (46 - length($name));
2994             $synop .= "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad ";
2996             $desc .= "<refsect2 id=\"$id\" role=\"signal\"><title>The <literal>&quot;$name&quot;</literal> signal</title>\n";
2997             $desc .= MakeIndexterms($symbol, $id);
2998             $desc .= "\n";
2999             $desc .= OutputSymbolExtraLinks($symbol);
3001             $desc .= "<programlisting>";
3003             $SignalReturns[$i] =~ m/\s*(const\s+)?(\w+)\s*(\**)/;
3004             my $type_modifier = defined($1) ? $1 : "";
3005             my $type = $2;
3006             my $pointer = $3;
3007             my $xref = &MakeXRef ($type, &tagify($type, "returnvalue"));
3009             my $ret_type_len = length ($type_modifier) + length ($pointer)
3010                 + length ($type);
3011             my $ret_type_output = "$type_modifier$xref$pointer"
3012                 . (' ' x ($RETURN_TYPE_FIELD_WIDTH - $ret_type_len));
3014             $desc  .= "${ret_type_output}user_function " . &MakeReturnField("") . " (";
3016             my $sourceparams = $SourceSymbolParams{$symbol};
3017             my @params = split ("\n", $SignalPrototypes[$i]);
3018             my $j;
3019             my $l;
3020             my $type_len = length("gpointer");
3021             my $name_len = length("user_data");
3022             # do two passes, the first one is to calculate padding
3023             for ($l = 0; $l < 2; $l++) {
3024                 for ($j = 0; $j <= $#params; $j++) {
3025                     # allow alphanumerics, '_', '[' & ']' in param names
3026                     if ($params[$j] =~ m/^\s*(\w+)\s*(\**)\s*([\w\[\]]+)\s*$/) {
3027                         $type = $1;
3028                         $pointer = $2;
3029                         if (defined($sourceparams)) {
3030                             $name = $$sourceparams[$PARAM_FIELD_COUNT * $j];
3031                         }
3032                         else {
3033                             $name = $3;
3034                         }
3035                         if (!defined($name)) {
3036                             $name = "arg$j";
3037                         }
3038                         if ($l == 0) {
3039                             if (length($type) + length($pointer) > $type_len) {
3040                                 $type_len = length($type) + length($pointer);
3041                             }
3042                             if (length($name) > $name_len) {
3043                                 $name_len = length($name);
3044                             }
3045                         }
3046                         else {
3047                             $xref = &MakeXRef ($type, &tagify($type, "type"));
3048                             $pad = ' ' x ($type_len - length($type) - length($pointer));
3049                             $desc .= "$xref$pad $pointer$name,\n";
3050                             $desc .= (' ' x ($SYMBOL_FIELD_WIDTH + $RETURN_TYPE_FIELD_WIDTH));
3051                         }
3052                     } else {
3053                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
3054                              "Can't parse arg: $params[$j]\nArgs:$SignalPrototypes[$i]");
3055                     }
3056                 }
3057             }
3058             $xref = &MakeXRef ("gpointer", &tagify("gpointer", "type"));
3059             $pad = ' ' x ($type_len - length("gpointer"));
3060             $desc  .= "$xref$pad user_data)";
3062             my $flags = $SignalFlags[$i];
3063             my $flags_string = "";
3065             if (defined ($flags)) {
3066               if ($flags =~ m/f/) {
3067                 $flags_string = "Run First";
3068               }
3069               elsif ($flags =~ m/l/) {
3070                 $flags_string = "Run Last";
3071               }
3072               elsif ($flags =~ m/c/) {
3073                 $flags_string = "Cleanup";
3074               }
3075               if ($flags =~ m/r/) {
3076                 if ($flags_string) { $flags_string .= " / "; }
3077                 $flags_string .= "No Recursion";
3078               }
3079               if ($flags =~ m/d/) {
3080                 if ($flags_string) { $flags_string .= " / "; }
3081                 $flags_string .= "Has Details";
3082               }
3083               if ($flags =~ m/a/) {
3084                 if ($flags_string) { $flags_string .= " / "; }
3085                 $flags_string .= "Action";
3086               }
3087               if ($flags =~ m/h/) {
3088                 if ($flags_string) { $flags_string .= " / "; }
3089                 $flags_string .= "No Hooks";
3090               }
3091             }
3093             if ($flags_string)
3094               {
3095                 $synop .= ": $flags_string\n";
3097                 $pad = ' ' x (5 + $name_len - length("user_data"));
3098                 $desc  .= "$pad : $flags_string</programlisting>\n";
3099               }
3100             else
3101               {
3102                 $synop .= "\n";
3103                 $desc  .= "</programlisting>\n";
3104               }
3106             $desc .= &MakeDeprecationNote($symbol);
3108             my $parameters = &OutputParamDescriptions ("SIGNAL", $symbol);
3109             my $parameters_output = 0;
3111             $AllSymbols{$symbol} = 1;
3112             if (defined ($SymbolDocs{$symbol})) {
3113                 my $symbol_docs = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3115                 # Try to insert the parameter table at the author's desired
3116                 # position. Otherwise we need to tag it onto the end.
3117                 if ($symbol_docs =~ s/<!--PARAMETERS-->/$parameters/) {
3118                   $parameters_output = 1;
3119                 }
3120                 $desc .= $symbol_docs;
3122                 if (!IsEmptyDoc($SymbolDocs{$symbol})) {
3123                     $AllDocumentedSymbols{$symbol} = 1;
3124                 }
3125             }
3127             if ($parameters_output == 0) {
3128                 $desc .= $parameters;
3129               }
3130             $desc .= OutputSymbolTraits ($symbol);
3131             $desc .= "</refsect2>";
3132         }
3133     }
3134     return ($synop, $desc);
3138 #############################################################################
3139 # Function    : GetArgs
3140 # Description : Returns the synopsis and detailed description DocBook output
3141 #               for the Args of a given GtkObject subclass.
3142 # Arguments   : $object - the GtkObject subclass, e.g. 'GtkButton'.
3143 #############################################################################
3145 sub GetArgs {
3146     my ($object) = @_;
3147     my $synop = "";
3148     my $desc = "";
3149     my $child_synop = "";
3150     my $child_desc = "";
3151     my $style_synop = "";
3152     my $style_desc = "";
3154     my $i;
3155     for ($i = 0; $i <= $#ArgObjects; $i++) {
3156         if ($ArgObjects[$i] eq $object) {
3157             #print "Found arg: $ArgNames[$i]\n";
3158             my $name = $ArgNames[$i];
3159             my $flags = $ArgFlags[$i];
3160             my $flags_string = "";
3161             my $kind = "";
3162             my $id_sep = "";
3164             if ($flags =~ m/c/) {
3165                 $kind = "child property";
3166                 $id_sep = "c-";
3167             }
3168             elsif ($flags =~ m/s/) {
3169                 $kind = "style property";
3170                 $id_sep = "s-";
3171             }
3172             else {
3173                 $kind = "property";
3174             }
3176             # Remember only one colon so we don't clash with signals.
3177             my $symbol = "${object}:${name}";
3178             # use two dashes and ev. an extra separator here for the same reason.
3179             my $id = &CreateValidSGMLID ("$object--$id_sep$name");
3181             my $type = $ArgTypes[$i];
3182             my $type_output;
3183             my $range = $ArgRanges[$i];
3184             my $range_output = CreateValidSGML ($range);
3185             my $default = $ArgDefaults[$i];
3186             my $default_output = CreateValidSGML ($default);
3188             if ($type eq "GtkString") {
3189                 $type = "char*";
3190             }
3191             if ($type eq "GtkSignal") {
3192                 $type = "GtkSignalFunc, gpointer";
3193                 $type_output = &MakeXRef ("GtkSignalFunc") . ", "
3194                     . &MakeXRef ("gpointer");
3195             } elsif ($type =~ m/^(\w+)\*$/) {
3196                 $type_output = &MakeXRef ($1, &tagify($1, "type")) . "*";
3197             } else {
3198                 $type_output = &MakeXRef ($type, &tagify($type, "type"));
3199             }
3201             if ($flags =~ m/r/) {
3202                 $flags_string = "Read";
3203             }
3204             if ($flags =~ m/w/) {
3205                 if ($flags_string) { $flags_string .= " / "; }
3206                 $flags_string .= "Write";
3207             }
3208             if ($flags =~ m/x/) {
3209                 if ($flags_string) { $flags_string .= " / "; }
3210                 $flags_string .= "Construct";
3211             }
3212             if ($flags =~ m/X/) {
3213                 if ($flags_string) { $flags_string .= " / "; }
3214                 $flags_string .= "Construct Only";
3215             }
3217             $AllSymbols{$symbol} = 1;
3218             my $blurb;
3219             if (defined($SymbolDocs{$symbol}) &&
3220                 !IsEmptyDoc($SymbolDocs{$symbol})) {
3221                 $blurb = &ExpandAbbreviations($symbol, $SymbolDocs{$symbol});
3222                 #print ".. [$SymbolDocs{$symbol}][$blurb]\n";
3223                 $AllDocumentedSymbols{$symbol} = 1;
3224             }
3225             else {
3226                 if (!($ArgBlurbs[$i] eq "")) {
3227                     $AllDocumentedSymbols{$symbol} = 1;
3228                 } else {
3229                     # FIXME: print a warning?
3230                     #print ".. no description\n";
3231                 }
3232                 $blurb = "<para>" . &CreateValidSGML ($ArgBlurbs[$i]) . "</para>";
3233             }
3235             my $pad1 = " " x (24 - length ($name));
3236             my $pad2 = " " x (20 - length ($type));
3238             my $arg_synop = "  &quot;<link linkend=\"$id\">$name</link>&quot;$pad1 $type_output $pad2 : $flags_string\n";
3239             my $arg_desc = "<refsect2 id=\"$id\" role=\"property\"><title>The <literal>&quot;$name&quot;</literal> $kind</title>\n";
3240             $arg_desc .= MakeIndexterms($symbol, $id);
3241             $arg_desc .= "\n";
3242             $arg_desc .= OutputSymbolExtraLinks($symbol);
3244             $arg_desc .= "<programlisting>  &quot;$name&quot;$pad1 $type_output $pad2 : $flags_string</programlisting>\n";
3245             $arg_desc .= &MakeDeprecationNote($symbol);
3246             $arg_desc .= $blurb;
3247             if ($range ne "") {
3248                 $arg_desc .= "<para>Allowed values: $range_output</para>\n";
3249             }
3250             if ($default ne "") {
3251                 $arg_desc .= "<para>Default value: $default_output</para>\n";
3252             }
3253             $arg_desc .= OutputSymbolTraits ($symbol);
3254             $arg_desc .= "</refsect2>\n";
3256             if ($flags =~ m/c/) {
3257                 $child_synop .= $arg_synop;
3258                 $child_desc .= $arg_desc;
3259             }
3260             elsif ($flags =~ m/s/) {
3261                 $style_synop .= $arg_synop;
3262                 $style_desc .= $arg_desc;
3263             }
3264             else {
3265                 $synop .= $arg_synop;
3266                 $desc .= $arg_desc;
3267             }
3268         }
3269     }
3270     return ($synop, $child_synop, $style_synop, $desc, $child_desc, $style_desc);
3274 #############################################################################
3275 # Function    : ReadSourceDocumentation
3276 # Description : This reads in the documentation embedded in comment blocks
3277 #               in the source code (for Gnome).
3279 #               Parameter descriptions override any in the template files.
3280 #               Function descriptions are placed before any description from
3281 #               the template files.
3283 #               It recursively descends the source directory looking for .c
3284 #               files and scans them looking for specially-formatted comment
3285 #               blocks.
3287 # Arguments   : $source_dir - the directory to scan.
3288 #############m###############################################################
3290 sub ReadSourceDocumentation {
3291     my ($source_dir) = @_;
3292     my ($file, $dir, @suffix_list, $suffix);
3293     #print "Scanning source directory: $source_dir\n";
3295     # This array holds any subdirectories found.
3296     my (@subdirs) = ();
3298     @suffix_list = split (/,/, $SOURCE_SUFFIXES);
3300     opendir (SRCDIR, $source_dir)
3301         || die "Can't open source directory $source_dir: $!";
3303     foreach $file (readdir (SRCDIR)) {
3304       if ($file =~ /^\./) {
3305         next;
3306       } elsif (-d "$source_dir/$file") {
3307         push (@subdirs, $file);
3308       } elsif (@suffix_list) {
3309         foreach $suffix (@suffix_list) {
3310           if ($file =~ m/\.\Q${suffix}\E$/) {
3311             &ScanSourceFile ("$source_dir/$file");
3312           }
3313         }
3314       } elsif ($file =~ m/\.[ch]$/) {
3315         &ScanSourceFile ("$source_dir/$file");
3316       }
3317     }
3318     closedir (SRCDIR);
3320     # Now recursively scan the subdirectories.
3321     foreach $dir (@subdirs) {
3322         next if ($IGNORE_FILES =~ m/(\s|^)\Q${dir}\E(\s|$)/);
3323         &ReadSourceDocumentation ("$source_dir/$dir");
3324     }
3328 #############################################################################
3329 # Function    : ScanSourceFile
3330 # Description : Scans one source file looking for specially-formatted comment
3331 #               blocks. Later &MergeSourceDocumentation is used to merge any
3332 #               documentation found with the documentation already read in
3333 #               from the template files.
3335 # Arguments   : $file - the file to scan.
3336 #############################################################################
3338 sub ScanSourceFile {
3339     my ($file) = @_;
3340     my $basename;
3342     if ($file =~ m/^.*[\/\\]([^\/\\]*)$/) {
3343         $basename = $1;
3344     } else {
3345         &LogWarning ($file, 1, "Can't find basename for this filename.");
3346         $basename = $file;
3347     }
3349     # Check if the basename is in the list of files to ignore.
3350     if ($IGNORE_FILES =~ m/(\s|^)\Q${basename}\E(\s|$)/) {
3351         return;
3352     }
3354     #print "DEBUG: Scanning $file\n";
3356     open (SRCFILE, $file)
3357         || die "Can't open $file: $!";
3358     my $in_comment_block = 0;
3359     my $symbol;
3360     my ($in_description, $in_return, $in_since, $in_stability, $in_deprecated);
3361     my ($description, $return_desc, $return_start, $return_style);
3362     my ($since_desc, $stability_desc, $deprecated_desc);
3363     my $current_param;
3364     my $ignore_broken_returns;
3365     my @params;
3366     while (<SRCFILE>) {
3367         # Look for the start of a comment block.
3368         if (!$in_comment_block) {
3369             if (m%^\s*/\*.*\*/%) {
3370                 #one-line comment - not gtkdoc
3371             } elsif (m%^\s*/\*\*\s%) {
3372                 #print "Found comment block start\n";
3374                 $in_comment_block = 1;
3376                 # Reset all the symbol data.
3377                 $symbol = "";
3378                 $in_description = 0;
3379                 $in_return = 0;
3380                 $in_since = 0;
3381                 $in_deprecated = 0;
3382                 $in_stability = 0;
3383                 $description = "";
3384                 $return_desc = "";
3385                 $return_style = "";
3386                 $since_desc = "";
3387                 $deprecated_desc = "";
3388                 $stability_desc = "";
3389                 $current_param = -1;
3390                 $ignore_broken_returns = 0;
3391                 @params = ();
3392             }
3393             next;
3394         }
3396         # We're in a comment block. Check if we've found the end of it.
3397         if (m%^\s*\*+/%) {
3398             if (!$symbol) {
3399                 # maybe its not even meant to be a gtk-doc comment?
3400                 &LogWarning ($file, $., "Symbol name not found at the start of the comment block.");
3401             } else {
3402                 # Add the return value description onto the end of the params.
3403                 if ($return_desc) {
3404                     push (@params, "Returns");
3405                     push (@params, $return_desc);
3406                     if ($return_style eq 'broken') {
3407                         &LogWarning ($file, $., "Free-form return value description in $symbol. Use `Returns:' to avoid ambiguities.");
3408                     }
3409                 }
3410                 # Convert special SGML characters
3411                 $description = &ConvertSGMLChars ($symbol, $description);
3412                 my $k;
3413                 for ($k = 1; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3414                     $params[$k] = &ConvertSGMLChars ($symbol, $params[$k]);
3415                 }
3417                 # Handle Section docs
3418                 if ($symbol =~ m/SECTION:\s*(.*)/) {
3419                     my $real_symbol=$1;
3420                     my $key;
3422                     if (scalar %KnownSymbols) {
3423                         if ((! defined($KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"})) || $KnownSymbols{"$TMPL_DIR/$real_symbol:Long_Description"} != 1) {
3424                             &LogWarning ($file, $., "Section $real_symbol is not defined in the $MODULE-section.txt file.");
3425                         }
3426                     }
3428                     #print "SECTION DOCS found in source for : '$real_symbol'\n";
3429                     $ignore_broken_returns = 1;
3430                     for ($k = 0; $k <= $#params; $k += $PARAM_FIELD_COUNT) {
3431                         #print "   '".$params[$k]."'\n";
3432                         $params[$k] = "\L$params[$k]";
3433                         undef $key;
3434                         if ($params[$k] eq "short_description") {
3435                             $key = "$TMPL_DIR/$real_symbol:Short_Description";
3436                         } elsif ($params[$k] eq "see_also") {
3437                             $key = "$TMPL_DIR/$real_symbol:See_Also";
3438                         } elsif ($params[$k] eq "title") {
3439                             $key = "$TMPL_DIR/$real_symbol:Title";
3440                         } elsif ($params[$k] eq "stability") {
3441                             $key = "$TMPL_DIR/$real_symbol:Stability_Level";
3442                         } elsif ($params[$k] eq "section_id") {
3443                             $key = "$TMPL_DIR/$real_symbol:Section_Id";
3444                         } elsif ($params[$k] eq "include") {
3445                             $key = "$TMPL_DIR/$real_symbol:Include";
3446                         }
3447                         if (defined($key)) {
3448                             $SourceSymbolDocs{$key}=$params[$k+1];
3449                             $SourceSymbolSourceFile{$key} = $file;
3450                             $SourceSymbolSourceLine{$key} = $.;
3451                         }
3452                     }
3453                     $SourceSymbolDocs{"$TMPL_DIR/$real_symbol:Long_Description"}=$description;
3454                     $SourceSymbolSourceFile{"$TMPL_DIR/$real_symbol:Long_Description"} = $file;
3455                     $SourceSymbolSourceLine{"$TMPL_DIR/$real_symbol:Long_Description"} = $.;
3456                     #$SourceSymbolTypes{$symbol} = "SECTION";
3457                 } else {
3458                     #print "SYMBOL DOCS found in source for : '$symbol' ",length($description), "\n";
3459                     $SourceSymbolDocs{$symbol} = $description;
3460                     $SourceSymbolParams{$symbol} = [ @params ];
3461                     # FIXME $SourceSymbolTypes{$symbol} = "STRUCT,SIGNAL,ARG,FUNCTION,MACRO";
3462                     #if (defined $DeclarationTypes{$symbol}) {
3463                     #    $SourceSymbolTypes{$symbol} = $DeclarationTypes{$symbol}
3464                     #}
3465                     $SourceSymbolSourceFile{$symbol} = $file;
3466                     $SourceSymbolSourceLine{$symbol} = $.;
3467                 }
3469                 if ($since_desc) {
3470                      ($since_desc, my @extra_lines) = split ("\n", $since_desc);
3471                      $since_desc =~ s/^\s+//;
3472                      $since_desc =~ s/\s+$//;
3473                      #print "Since($symbol) : [$since_desc]\n";
3474                      $Since{$symbol} = &ConvertSGMLChars ($symbol, $since_desc);
3475                      if(scalar @extra_lines) {
3476                          &LogWarning ($file, $., "multi-line since docs found");
3477                      }
3478                 }
3480                 if ($stability_desc) {
3481                     $stability_desc = &ParseStabilityLevel($stability_desc, $file, $., "Stability level for $symbol");
3482                     $StabilityLevel{$symbol} = &ConvertSGMLChars ($symbol, $stability_desc);
3483                 }
3485                 if ($deprecated_desc) {
3486                     if (exists $Deprecated{$symbol}) {
3487                     }
3488                     else {
3489                          # don't warn for signals and properties
3490                          #if ($symbol !~ m/::?(.*)/) {
3491                          if (defined $DeclarationTypes{$symbol}) {
3492                              &LogWarning ($file, $., 
3493                                  "$symbol is deprecated in the inline comments, but no deprecation guards were found around the declaration.".
3494                                  " (See the --deprecated-guards option for gtkdoc-scan.)");
3495                          }
3496                     }
3497                     $Deprecated{$symbol} = &ConvertSGMLChars ($symbol, $deprecated_desc);
3498                 }
3499             }
3501             $in_comment_block = 0;
3502             next;
3503         }
3505         # Get rid of ' * ' at start of every line in the comment block.
3506         s%^\s*\*\s?%%;
3507         # But make sure we don't get rid of the newline at the end.
3508         if (!$_) {
3509             $_ = "\n";
3510         }
3511         #print "DEBUG: scanning :$_";
3513         # If we haven't found the symbol name yet, look for it.
3514         if (!$symbol) {
3515             if (m%^\s*(SECTION:\s*\S+)%) {
3516                 $symbol = $1;
3517                 #print "SECTION DOCS found in source for : '$symbol'\n";
3518                 $ignore_broken_returns = 1;
3519             } elsif (m%^\s*([\w:-]*\w)\s*:?\s*$%) {
3520                 $symbol = $1;
3521                 #print "SYMBOL DOCS found in source for : '$symbol'\n";
3522             }
3523             next;
3524         }
3526         # If we're in the return value description, add it to the end.
3527         if ($in_return) {
3528             # If we find another valid returns line, we assume that the first
3529             # one was really part of the description.
3530             if (m/^\s*(returns:|return\s+value:)/i) {
3531                 if ($return_style eq 'broken') {
3532                     $description .= $return_start . $return_desc;
3533                 }
3534                 $return_start = $1;
3535                 if ($return_style eq 'sane') {
3536                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3537                 }
3538                 $return_style = 'sane';
3539                 $ignore_broken_returns = 1;
3540                 $return_desc = $';
3541             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3542                 $description .= $return_start . $return_desc;
3543                 $return_start = $1;
3544                 $return_style = 'broken';
3545                 $return_desc = $';
3546             } elsif (m%^\s*since:%i) {
3547                 $since_desc = $';
3548                 $in_since = 1;
3549                 $in_return = 0;
3550             } elsif (m%^\s*stability:%i) {
3551                 $stability_desc = $';
3552                 $in_stability = 1;
3553                 $in_return = 0;
3554             } elsif (m%^\s*deprecated:%i) {
3555                 $deprecated_desc = $';
3556                 $in_deprecated = 1;
3557                 $in_return = 0;
3558             } else {
3559                 $return_desc .= $_;
3560             }
3561             next;
3562         }
3564         if ($in_since) {
3565             if (m/^\s*(returns:|return\s+value:)/i) {
3566                 if ($return_style eq 'broken') {
3567                     $description .= $return_start . $return_desc;
3568                 }
3569                 $return_start = $1;
3570                 if ($return_style eq 'sane') {
3571                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3572                 }
3573                 $return_style = 'sane';
3574                 $ignore_broken_returns = 1;
3575                 $return_desc = $';
3576                 $in_return = 1;
3577                 $in_since = 0;
3578             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3579                 $return_start = $1;
3580                 $return_style = 'broken';
3581                 $return_desc = $';
3582                 $in_return = 1;
3583                 $in_since = 0;
3584             } elsif (m%^\s*deprecated:%i) {
3585                 $deprecated_desc = $';
3586                 $in_deprecated = 1;
3587                 $in_since = 0;
3588             } elsif (m%^\s*stability:%i) {
3589                 $stability_desc = $';
3590                 $in_stability = 1;
3591                 $in_since = 0;
3592             } else {
3593                 $since_desc .= $_;
3594             }
3595             next;
3596         }
3598         if ($in_stability) {
3599             if (m/^\s*(returns:|return\s+value:)/i) {
3600                 if ($return_style eq 'broken') {
3601                     $description .= $return_start . $return_desc;
3602                 }
3603                 $return_start = $1;
3604                 if ($return_style eq 'sane') {
3605                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3606                 }
3607                 $return_style = 'sane';
3608                 $ignore_broken_returns = 1;
3609                 $return_desc = $';
3610                 $in_return = 1;
3611                 $in_stability = 0;
3612             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3613                 $return_start = $1;
3614                 $return_style = 'broken';
3615                 $return_desc = $';
3616                 $in_return = 1;
3617                 $in_stability = 0;
3618             } elsif (m%^\s*deprecated:%i) {
3619                 $deprecated_desc = $';
3620                 $in_deprecated = 1;
3621                 $in_stability = 0;
3622             } elsif (m%^\s*since:%i) {
3623                 $since_desc = $';
3624                 $in_since = 1;
3625                 $in_stability = 0;
3626             } else {
3627                 $stability_desc .= $_;
3628             }
3629             next;
3630         }
3632         if ($in_deprecated) {
3633             if (m/^\s*(returns:|return\s+value:)/i) {
3634                 if ($return_style eq 'broken') {
3635                     $description .= $return_start . $return_desc;
3636                 }
3637                 $return_start = $1;
3638                 if ($return_style eq 'sane') {
3639                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3640                 }
3641                 $return_style = 'sane';
3642                 $ignore_broken_returns = 1;
3643                 $return_desc = $';
3644                 $in_return = 1;
3645                 $in_deprecated = 0;
3646             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3647                 $return_start = $1;
3648                 $return_style = 'broken';
3649                 $return_desc = $';
3650                 $in_return = 1;
3651                 $in_deprecated = 0;
3652             } elsif (m%^\s*since:%i) {
3653                 $since_desc = $';
3654                 $in_since = 1;
3655                 $in_deprecated = 0;
3656             } elsif (m%^\s*stability:%i) {
3657                 $stability_desc = $';
3658                 $in_stability = 1;
3659                 $in_deprecated = 0;
3660             } else {
3661                 $deprecated_desc .= $_;
3662             }
3663             next;
3664         }
3666         # If we're in the description part, check for the 'Returns:' line.
3667         # If that isn't found, add the text to the end.
3668         if ($in_description) {
3669             # Get rid of 'Description:'
3670             s%^\s*Description:%%;
3672             if (m/^\s*(returns:|return\s+value:)/i) {
3673                 if ($return_style eq 'broken') {
3674                     $description .= $return_start . $return_desc;
3675                 }
3676                 $return_start = $1;
3677                 if ($return_style eq 'sane') {
3678                     &LogWarning ($file, $., "Multiple Returns for $symbol.");
3679                 }
3680                 $return_style = 'sane';
3681                 $ignore_broken_returns = 1;
3682                 $return_desc = $';
3683                 $in_return = 1;
3684                 next;
3685             } elsif (!$ignore_broken_returns && m/^\s*(returns\b\s*)/i) {
3686                 $return_start = $1;
3687                 $return_style = 'broken';
3688                 $return_desc = $';
3689                 $in_return = 1;
3690                 next;
3691             } elsif (m%^\s*since:%i) {
3692                 $since_desc = $';
3693                 $in_since = 1;
3694                 next;
3695             } elsif (m%^\s*deprecated:%i) {
3696                 $deprecated_desc = $';
3697                 $in_deprecated = 1;
3698                 next;
3699             } elsif (m%^\s*stability:%i) {
3700                 $stability_desc = $';
3701                 $in_stability = 1;
3702                 next;
3703             }
3705             $description .= $_;
3706             next;
3707         }
3709         # We must be in the parameters. Check for the empty line below them.
3710         if (m%^\s*$%) {
3711             $in_description = 1;
3712             next;
3713         }
3715         # Look for a parameter name.
3716         if (m%^\s*@(\S+)\s*:%) {
3717             my $param_name = $1;
3718             my $param_desc = $';
3719             #print "Found parameter: $param_name\n";
3720             # Allow '...' as the Varargs parameter.
3721             if ($param_name eq "...") {
3722                 $param_name = "Varargs";
3723             }
3724             if ("\L$param_name" eq "returns") {
3725                 $return_style = 'sane';
3726                 $ignore_broken_returns = 1;
3727             }
3728             push (@params, $param_name);
3729             push (@params, $param_desc);
3730             $current_param += $PARAM_FIELD_COUNT;
3731             next;
3732         }
3734         # We must be in the middle of a parameter description, so add it on
3735         # to the last element in @params.
3736         if ($current_param == -1) {
3737             &LogWarning ($file, $., "Parsing comment block file : parameter expected.");
3738         } else {
3739             $params[$#params] .= $_;
3740         }
3741     }
3742     close (SRCFILE);
3745 #############################################################################
3746 # Function    : OutputMissingDocumentation
3747 # Description : Outputs report of documentation coverage to a file
3749 # Arguments   : none
3750 #############################################################################
3752 sub OutputMissingDocumentation {
3753     my $n_documented = 0;
3754     my $n_incomplete = 0;
3755     my $total = 0;
3756     my $symbol;
3757     my $percent;
3758     my $msg;
3759     my $buffer = "";
3760     my $buffer_deprecated = "";
3761     my $buffer_descriptions = "";
3762     
3763     open (UNDOCUMENTED, ">$ROOT_DIR/$MODULE-undocumented.txt")
3764       || die "Can't create $ROOT_DIR/$MODULE-undocumented.txt: $!";
3765     
3766     foreach $symbol (sort (keys (%AllSymbols))) {
3767         # FIXME: should we print LogWarnings for undocumented stuff?
3768         # DEBUG
3769         #my $ssfile = &GetSymbolSourceFile($symbol);
3770         #my $ssline = &GetSymbolSourceLine($symbol);
3771         #my $location = "defined at " . (defined($ssfile)?$ssfile:"?") . ":" . (defined($ssline)?$ssline:"0") . "\n";
3772         # DEBUG
3773         if ($symbol !~ /:(Title|Long_Description|Short_Description|See_Also|Stability_Level|Include|Section_Id)/) {
3774             $total++;
3775             if (exists ($AllDocumentedSymbols{$symbol})) {
3776                 $n_documented++;
3777                 if (exists ($AllIncompleteSymbols{$symbol})) {
3778                     $n_incomplete++;
3779                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3780                     #$buffer .= "\t0: ".$location;
3781                 }
3782             } elsif (exists $Deprecated{$symbol}) {
3783                 if (exists ($AllIncompleteSymbols{$symbol})) {
3784                     $n_incomplete++;
3785                     $buffer_deprecated .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3786                     #$buffer .= "\t1a: ".$location;
3787                 } else {
3788                     $buffer_deprecated .= $symbol . "\n";
3789                     #$buffer .= "\t1b: ".$location;
3790                 }
3791             } else {
3792                 if (exists ($AllIncompleteSymbols{$symbol})) {
3793                     $n_incomplete++;
3794                     $buffer .= $symbol . " (" . $AllIncompleteSymbols{$symbol} . ")\n";
3795                     #$buffer .= "\t2a: ".$location;
3796                 } else {
3797                     $buffer .= $symbol . "\n";
3798                     #$buffer .= "\t2b: ".$location;
3799                 }
3800             }
3801         } elsif ($symbol =~ /:(Long_Description|Short_Description)/) {
3802             $total++;
3803             #my $len1=(exists($SymbolDocs{$symbol}))?length($SymbolDocs{$symbol}):-1;
3804             #my $len2=(exists($AllDocumentedSymbols{$symbol}))?length($AllDocumentedSymbols{$symbol}):-1;
3805             #print "%%%% $symbol : $len1,$len2\n";
3806             if (((exists ($SymbolDocs{$symbol})) && (length ($SymbolDocs{$symbol}) > 0))
3807             || ((exists ($AllDocumentedSymbols{$symbol})) && (length ($AllDocumentedSymbols{$symbol}) > 0))) {
3808               $n_documented++;
3809             } else {
3810               # cut off the leading namespace ($TMPL_DIR)
3811               $symbol =~ m/^.*\/(.*)$/;
3812               $buffer_descriptions .= $1 . "\n";
3813             }
3814         }
3815     }
3816     
3817     $buffer .= "\n" . $buffer_deprecated . "\n" . $buffer_descriptions;
3818     
3819     if ($total == 0) {
3820       $percent = 100;
3821     } else {
3822       $percent = ($n_documented / $total) * 100.0;
3823     }
3824     
3825     printf UNDOCUMENTED "%.0f%% symbol docs coverage.\n", $percent;
3826     print UNDOCUMENTED "$n_documented symbols documented.\n";
3827     print UNDOCUMENTED "$n_incomplete symbols incomplete.\n";
3828     print UNDOCUMENTED ($total - $n_documented) . " not documented.\n\n\n";
3829     
3830     print UNDOCUMENTED $buffer;
3831     
3832     close (UNDOCUMENTED);
3833     
3834     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);
3838 #############################################################################
3839 # Function    : OutputUndeclaredSymbols
3840 # Description : Outputs symbols that are undeclared yet documented to a file
3842 # Arguments   : none
3843 #############################################################################
3845 sub OutputUndeclaredSymbols {
3846     open(UNDECLARED, ">$ROOT_DIR/$MODULE-undeclared.txt")
3847         || die "Can't create $ROOT_DIR/$MODULE-undeclared.txt";
3849     if (%UndeclaredSymbols) {
3850         print UNDECLARED (join("\n", sort keys %UndeclaredSymbols));
3851         print UNDECLARED "\n";
3852         print "See $MODULE-undeclared.txt for the list of undeclared symbols.\n"
3853     }
3854     close(UNDECLARED);
3858 #############################################################################
3859 # Function    : OutputAllSymbols
3860 # Description : Outputs list of all symbols to a file
3862 # Arguments   : none
3863 #############################################################################
3865 sub OutputAllSymbols {
3866      my $n_documented = 0;
3867      my $total = 0;
3868      my $symbol;
3869      my $percent;
3870      my $msg;
3872      open (SYMBOLS, ">$ROOT_DIR/$MODULE-symbols.txt")
3873           || die "Can't create $ROOT_DIR/$MODULE-symbols.txt: $!";
3875      foreach $symbol (sort (keys (%AllSymbols))) {
3876           print SYMBOLS $symbol . "\n";
3877      }
3879      close (SYMBOLS);
3882 #############################################################################
3883 # Function    : OutputSymbolsWithoutSince
3884 # Description : Outputs list of all symbols without a since tag to a file
3886 # Arguments   : none
3887 #############################################################################
3889 sub OutputSymbolsWithoutSince {
3890      my $n_documented = 0;
3891      my $total = 0;
3892      my $symbol;
3893      my $percent;
3894      my $msg;
3896      open (SYMBOLS, ">$ROOT_DIR/$MODULE-nosince.txt")
3897           || die "Can't create $ROOT_DIR/$MODULE-nosince.txt: $!";
3899      foreach $symbol (sort (keys (%SourceSymbolDocs))) {
3900          if (!defined $Since{$symbol}) {
3901              print SYMBOLS $symbol . "\n";
3902          }
3903      }
3905      close (SYMBOLS);
3909 #############################################################################
3910 # Function    : MergeSourceDocumentation
3911 # Description : This merges documentation read from a source file into the
3912 #               documentation read in from a template file.
3914 #               Parameter descriptions override any in the template files.
3915 #               Function descriptions are placed before any description from
3916 #               the template files.
3918 # Arguments   : none
3919 #############################################################################
3921 sub MergeSourceDocumentation {
3922     my $symbol;
3923     my @Symbols;
3925     if (scalar %SymbolDocs) {
3926         @Symbols=keys (%SymbolDocs);
3927         #print "num existing entries: ".(scalar @Symbols)."\n";
3928         #print "  ",$Symbols[0], " ",$Symbols[1],"\n";
3929     }
3930     else {
3931         # filter scanned declarations, with what we suppress from -sections.txt
3932         my %tmp = ();
3933         foreach $symbol (keys (%Declarations)) {
3934             if (defined($KnownSymbols{$symbol}) && $KnownSymbols{$symbol} == 1) {
3935                 $tmp{$symbol}=1;
3936             }
3937         }
3938         # , add the rest from -sections.txt
3939         foreach $symbol (keys (%KnownSymbols)) {
3940             if ($KnownSymbols{$symbol} == 1) {
3941                 $tmp{$symbol}=1;
3942             }
3943         }
3944         # and add whats found in the source
3945         foreach $symbol (keys (%SourceSymbolDocs)) {
3946             $tmp{$symbol}=1;
3947         }
3948         @Symbols = keys (%tmp);
3949         #print "num source entries: ".(scalar @Symbols)."\n";
3950     }
3951     foreach $symbol (@Symbols) {
3952         $AllSymbols{$symbol} = 1;
3954         my $have_tmpl_docs = 0;
3956         ## See if the symbol is documented in template
3957         my $tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
3958         my $check_tmpl_doc =$tmpl_doc;
3959         # remove all xml-tags and whitespaces
3960         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
3961         $check_tmpl_doc =~ s/<.*?>//g;
3962         $check_tmpl_doc =~ s/\s//g;
3963         # anything left ?
3964         if ($check_tmpl_doc ne "") {
3965             $have_tmpl_docs = 1;
3966             #print "## [$check_tmpl_doc]\n";
3967         } else {
3968             $tmpl_doc = "";
3969         }
3971         if (exists ($SourceSymbolDocs{$symbol})) {
3972             my $type = $DeclarationTypes {$symbol};
3974             #print "merging [$symbol] from source\n";
3976             my $item = "Parameter";
3977             if (defined ($type)) {
3978                 if ($type eq 'STRUCT') {
3979                     $item = "Field";
3980                 } elsif ($type eq 'ENUM') {
3981                     $item = "Value";
3982                 } elsif ($type eq 'UNION') {
3983                     $item = "Field";
3984                 }
3985             } else {
3986                 $type="SIGNAL";
3987             }
3989             my $src_doc = $SourceSymbolDocs{$symbol};
3990             # remove leading and training whitespaces
3991             $src_doc =~ s/^\s+//;
3992             $src_doc =~ s/\s+$//;
3994             # Don't output warnings for overridden titles as titles are
3995             # automatically generated in the -sections.txt file, and thus they
3996             # are often overridden.
3997             if ($have_tmpl_docs && $symbol !~ m/:Title$/) {
3998                 # check if content is different
3999                 if ($tmpl_doc ne $src_doc) {
4000                     #print "[$tmpl_doc] [$src_doc]\n";
4001                     &LogWarning ($SourceSymbolSourceFile{$symbol}, $SourceSymbolSourceLine{$symbol},
4002                         "Documentation in template ".$SymbolSourceFile{$symbol}.":".$SymbolSourceLine{$symbol}." for $symbol being overridden by inline comments.");
4003                 }
4004             }
4006             if ($src_doc ne "") {
4007                  $AllDocumentedSymbols{$symbol} = 1;
4008             }
4010             # Convert <!--PARAMETERS--> with any blank lines around it to
4011             # a </para> followed by <!--PARAMETERS--> followed by <para>.
4012             $src_doc =~ s%\n+\s*<!--PARAMETERS-->\s*\n+%\n</para>\n<!--PARAMETERS-->\n<para>\n%g;
4014             # If there is a blank line, finish the paragraph and start another.
4015             $src_doc = &ConvertBlankLines ($src_doc, $symbol);
4016             # Do not add <para> to nothing, it breaks missing docs checks.
4017             my $src_doc_para = "";
4018             if ($src_doc) {
4019                 $src_doc_para = "<para>\n$src_doc</para>\n";
4020                 #print "$symbol : [$src_doc][$src_doc_para]\n";
4021             }
4023             if ($symbol =~ m/$TMPL_DIR\/.+:Long_Description/) {
4024                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4025             } elsif ($symbol =~ m/$TMPL_DIR\/.+:.+/) {
4026                 # For the title/summary/see also section docs we don't want to
4027                 # add any <para> tags.
4028                 $SymbolDocs{$symbol} = "$src_doc"
4029             } else {
4030                 $SymbolDocs{$symbol} = "$src_doc_para$tmpl_doc";
4031             }
4033             # merge parameters
4034             if ($symbol =~ m/.*::.*/) {
4035                 # For signals we prefer the param names from the source docs,
4036                 # since the ones from the templates are likely to contain the
4037                 # artificial argn names which are generated by gtkdoc-scangobj.
4038                 $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4039                 # FIXME: we need to check for empty docs here as well!
4040             } else {
4041                 # The templates contain the definitive parameter names and order,
4042                 # so we will not change that. We only override the actual text.
4043                 my $tmpl_params = $SymbolParams{$symbol};
4044                 if (!defined ($tmpl_params)) {
4045                     #print "No merge needed for $symbol\n";
4046                     $SymbolParams{$symbol} = $SourceSymbolParams{$symbol};
4047                     #  FIXME: we still like to get the number of params and merge
4048                     #  1) we would noticed that params have been removed/renamed
4049                     #  2) we would catch undocumented params
4050                     #  params are not (yet) exported in -decl.txt so that we
4051                     #  could easily grab them :/
4052                 } else {
4053                     my $params = $SourceSymbolParams{$symbol};
4054                     my $j;
4055                     #print "Merge needed for $symbol, tmpl_params: ",$#$tmpl_params,", source_params: ",$#$params," \n";
4056                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4057                         my $tmpl_param_name = $$tmpl_params[$j];
4059                         # Allow '...' as the Varargs parameter.
4060                         if ($tmpl_param_name eq "...") {
4061                             $tmpl_param_name = "Varargs";
4062                         }
4064                         # Try to find the param in the source comment documentation.
4065                         my $found = 0;
4066                         my $k;
4067                         #print "  try merge param $tmpl_param_name\n";
4068                         for ($k = 0; $k <= $#$params; $k += $PARAM_FIELD_COUNT) {
4069                             my $param_name = $$params[$k];
4070                             my $param_desc = $$params[$k + 1];
4072                             #print "    test param  $param_name\n";
4073                             # We accept changes in case, since the Gnome source
4074                             # docs contain a lot of these.
4075                             if ("\L$param_name" eq "\L$tmpl_param_name") {
4076                                 $found = 1;
4078                                 # Override the description.
4079                                 $$tmpl_params[$j + 1] = $param_desc;
4081                                 # Set the name to "" to mark it as used.
4082                                 $$params[$k] = "";
4083                                 last;
4084                             }
4085                         }
4087                         # If it looks like the parameters are there, but not
4088                         # in the right place, try to explain a bit better.
4089                         if ((!$found) && ($src_doc =~ m/\@$tmpl_param_name:/)) {
4090                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4091                                 "Parameters for $symbol must start on the line immediately after the function or macro name.");
4092                         }
4093                     }
4095                     # Now we output a warning if parameters have been described which
4096                     # do not exist.
4097                     for ($j = 0; $j <= $#$params; $j += $PARAM_FIELD_COUNT) {
4098                         my $param_name = $$params[$j];
4099                         if ($param_name) {
4100                             # the template builder cannot detect if a macro returns
4101                             # a result or not
4102                             if(($type eq "MACRO") && ($param_name eq "Returns")) {
4103                                 # FIXME: do we need to add it then to tmpl_params[] ?
4104                                 my $num=$#$tmpl_params;
4105                                 #print "  adding Returns: to macro docs for $symbol.\n";
4106                                 $$tmpl_params[$num+1]="Returns";
4107                                 $$tmpl_params[$num+2]=$$params[$j+1];
4108                                 next;
4109                             }
4110                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4111                                 "$item described in source code comment block but does not exist. $type: $symbol $item: $param_name.");
4112                         }
4113                     }
4114                 }
4115             }
4116         } else {
4117             if ($have_tmpl_docs) {
4118                 $AllDocumentedSymbols{$symbol} = 1;
4119                 #print "merging [$symbol] from template\n";
4120             }
4121             else {
4122                 #print "[$symbol] undocumented\n";
4123             }
4124         }
4126         # if this symbol is documented, check if docs are complete
4127         $check_tmpl_doc = defined ($SymbolDocs{$symbol}) ? $SymbolDocs{$symbol} : "";
4128         # remove all xml-tags and whitespaces
4129         #$check_tmpl_doc =~ s/<\/?[a-z]+>//g;
4130         $check_tmpl_doc =~ s/<.*?>//g;
4131         $check_tmpl_doc =~ s/\s//g;
4132         if ($check_tmpl_doc ne "") {
4133             my $tmpl_params = $SymbolParams{$symbol};
4134             if (defined ($tmpl_params)) {
4135                 my $type = $DeclarationTypes {$symbol};
4137                 my $item = "Parameter";
4138                 if (defined ($type)) {
4139                     if ($type eq 'STRUCT') {
4140                         $item = "Field";
4141                     } elsif ($type eq 'ENUM') {
4142                         $item = "Value";
4143                     } elsif ($type eq 'UNION') {
4144                         $item = "Field";
4145                     }
4146                 } else {
4147                     $type="SIGNAL";
4148                 }
4150                 #print "Check param docs for $symbol, tmpl_params: ",$#$tmpl_params," entries, type=$type\n";
4152                 if ($#$tmpl_params > 0) {
4153                     my $j;
4154                     for ($j = 0; $j <= $#$tmpl_params; $j += $PARAM_FIELD_COUNT) {
4155                         # Output a warning if the parameter is empty and
4156                         # remember for stats.
4157                         my $tmpl_param_name = $$tmpl_params[$j];
4158                         my $tmpl_param_desc = $$tmpl_params[$j + 1];
4159                         if ($tmpl_param_desc !~ m/\S/) {
4160                             if (exists ($AllIncompleteSymbols{$symbol})) {
4161                                 $AllIncompleteSymbols{$symbol}.=", ".$tmpl_param_name;
4162                             } else {
4163                                 $AllIncompleteSymbols{$symbol}=$tmpl_param_name;
4164                             }
4165                             &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4166                                 "$item description for $symbol"."::"."$tmpl_param_name is missing in source code comment block.");
4167                         }
4168                     }
4169                 }
4170                 else {
4171                     if ($#$tmpl_params == 0) {
4172                         $AllIncompleteSymbols{$symbol}="<items>";
4173                         &LogWarning (&GetSymbolSourceFile ($symbol), &GetSymbolSourceLine($symbol),
4174                             "$item descriptions for $symbol are missing in source code comment block.");
4175                     }
4176                     # $#$tmpl_params==-1 means we don't know about parameters
4177                     # this unfortunately does not tell if there should be some
4178                 }
4179             }
4180         }
4181    }
4182    #print "num doc entries: ".(scalar %SymbolDocs)."\n";
4185 #############################################################################
4186 # Function    : IsEmptyDoc
4187 # Description : Check if a doc-string is empty. Its also regarded as empty if
4188 #               it only consist of whitespace or e.g. FIXME.
4189 # Arguments   : the doc-string
4190 #############################################################################
4191 sub IsEmptyDoc {
4192     my ($doc) = @_;
4193     
4194     if ($doc =~ /^\s*$/) {
4195         return 1;
4196     }
4198     if ($doc =~ /^\s*<para>\s*(FIXME)?\s*<\/para>\s*$/) {
4199         return 1;
4200     }
4202     return 0;
4206 # This converts blank lines to "</para><para>", but only outside CDATA and
4207 # <programlisting> tags.
4208 sub ConvertBlankLines {
4209     return &ModifyXMLElements ($_[0], $_[1],
4210                                "<!\\[CDATA\\[|<programlisting[^>]*>|\\|\\[",
4211                                \&ConvertBlankLinesEndTag,
4212                                \&ConvertBlankLinesCallback);
4216 sub ConvertBlankLinesEndTag {
4217   if ($_[0] eq "<!\[CDATA\[") {
4218     return "]]>";
4219   } elsif ($_[0] eq "|[") {
4220     return "]\\|";
4221   } else {
4222     return "</programlisting>";
4223   }
4226 sub ConvertBlankLinesCallback {
4227   my ($text, $symbol, $tag) = @_;
4229   # If we're not in CDATA or a <programlisting> we convert blank lines so
4230   # they start a new <para>.
4231   if ($tag eq "") {
4232     $text =~ s%\n{2,}%\n</para>\n<para>\n%g;
4233   }
4235   return $text;
4239 #############################################################################
4240 # LIBRARY FUNCTIONS -   These functions are used in both gtkdoc-mkdb and
4241 #                       gtkdoc-mktmpl and should eventually be moved to a
4242 #                       separate library.
4243 #############################################################################
4245 #############################################################################
4246 # Function    : ReadDeclarationsFile
4247 # Description : This reads in a file containing the function/macro/enum etc.
4248 #               declarations.
4250 #               Note that in some cases there are several declarations with
4251 #               the same name, e.g. for conditional macros. In this case we
4252 #               set a flag in the %DeclarationConditional hash so the
4253 #               declaration is not shown in the docs.
4255 #               If a macro and a function have the same name, e.g. for
4256 #               gtk_object_ref, the function declaration takes precedence.
4258 #               Some opaque structs are just declared with 'typedef struct
4259 #               _name name;' in which case the declaration may be empty.
4260 #               The structure may have been found later in the header, so
4261 #               that overrides the empty declaration.
4263 # Arguments   : $file - the declarations file to read
4264 #               $override - if declarations in this file should override
4265 #                       any current declaration.
4266 #############################################################################
4268 sub ReadDeclarationsFile {
4269     my ($file, $override) = @_;
4271     if ($override == 0) {
4272         %Declarations = ();
4273         %DeclarationTypes = ();
4274         %DeclarationConditional = ();
4275         %DeclarationOutput = ();
4276     }
4278     open (INPUT, $file)
4279         || die "Can't open $file: $!";
4280     my $declaration_type = "";
4281     my $declaration_name;
4282     my $declaration;
4283     my $is_deprecated = 0;
4284     while (<INPUT>) {
4285         if (!$declaration_type) {
4286             if (m/^<([^>]+)>/) {
4287                 $declaration_type = $1;
4288                 $declaration_name = "";
4289                 #print "Found declaration: $declaration_type\n";
4290                 $declaration = "";
4291             }
4292         } else {
4293             if (m%^<NAME>(.*)</NAME>%) {
4294                 $declaration_name = $1;
4295             } elsif (m%^<DEPRECATED/>%) {
4296                 $is_deprecated = 1;
4297             } elsif (m%^</$declaration_type>%) {
4298                 #print "Found end of declaration: $declaration_name\n";
4299                 # Check that the declaration has a name
4300                 if ($declaration_name eq "") {
4301                     print "ERROR: $declaration_type has no name $file:$.\n";
4302                 }
4304                 # If the declaration is an empty typedef struct _XXX XXX
4305                 # set the flag to indicate the struct has a typedef.
4306                 if ($declaration_type eq 'STRUCT'
4307                     && $declaration =~ m/^\s*$/) {
4308                     #print "Struct has typedef: $declaration_name\n";
4309                     $StructHasTypedef{$declaration_name} = 1;
4310                 }
4312                 # Check if the symbol is already defined.
4313                 if (defined ($Declarations{$declaration_name})
4314                     && $override == 0) {
4315                     # Function declarations take precedence.
4316                     if ($DeclarationTypes{$declaration_name} eq 'FUNCTION') {
4317                         # Ignore it.
4318                     } elsif ($declaration_type eq 'FUNCTION') {
4319                         if ($is_deprecated) {
4320                             $Deprecated{$declaration_name} = "";
4321                         }
4322                         $Declarations{$declaration_name} = $declaration;
4323                         $DeclarationTypes{$declaration_name} = $declaration_type;
4324                     } elsif ($DeclarationTypes{$declaration_name}
4325                               eq $declaration_type) {
4326                         # If the existing declaration is empty, or is just a
4327                         # forward declaration of a struct, override it.
4328                         if ($declaration_type eq 'STRUCT') {
4329                             if ($Declarations{$declaration_name} =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4330                                 if ($is_deprecated) {
4331                                     $Deprecated{$declaration_name} = "";
4332                                 }
4333                                 $Declarations{$declaration_name} = $declaration;
4334                             } elsif ($declaration =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
4335                                 # Ignore an empty or forward declaration.
4336                             } else {
4337                                 &LogWarning ($file, $., "Structure $declaration_name has multiple definitions.");
4338                             }
4339                         } else {
4340                             # set flag in %DeclarationConditional hash for
4341                             # multiply defined macros/typedefs.
4342                             $DeclarationConditional{$declaration_name} = 1;
4343                         }
4344                     } else {
4345                         &LogWarning ($file, $., "$declaration_name has multiple definitions.");
4346                     }
4347                 } else {
4348                     if ($is_deprecated) {
4349                         $Deprecated{$declaration_name} = "";
4350                     }
4351                     $Declarations{$declaration_name} = $declaration;
4352                     $DeclarationTypes{$declaration_name} = $declaration_type;
4353                 }
4355                 $declaration_type = "";
4356                 $is_deprecated = 0;
4357             } else {
4358                 $declaration .= $_;
4359             }
4360         }
4361     }
4362     close (INPUT);
4366 #############################################################################
4367 # Function    : ReadSignalsFile
4368 # Description : This reads in an existing file which contains information on
4369 #               all GTK signals. It creates the arrays @SignalNames and
4370 #               @SignalPrototypes containing info on the signals. The first
4371 #               line of the SignalPrototype is the return type of the signal
4372 #               handler. The remaining lines are the parameters passed to it.
4373 #               The last parameter, "gpointer user_data" is always the same
4374 #               so is not included.
4375 # Arguments   : $file - the file containing the signal handler prototype
4376 #                       information.
4377 #############################################################################
4379 sub ReadSignalsFile {
4380     my ($file) = @_;
4382     my $in_signal = 0;
4383     my $signal_object;
4384     my $signal_name;
4385     my $signal_returns;
4386     my $signal_flags;
4387     my $signal_prototype;
4389     # Reset the signal info.
4390     @SignalObjects = ();
4391     @SignalNames = ();
4392     @SignalReturns = ();
4393     @SignalFlags = ();
4394     @SignalPrototypes = ();
4396     if (! -f $file) {
4397         return;
4398     }
4399     if (!open (INPUT, $file)) {
4400         warn "Can't open $file - skipping signals\n";
4401         return;
4402     }
4403     while (<INPUT>) {
4404         if (!$in_signal) {
4405             if (m/^<SIGNAL>/) {
4406                 $in_signal = 1;
4407                 $signal_object = "";
4408                 $signal_name = "";
4409                 $signal_returns = "";
4410                 $signal_prototype = "";
4411             }
4412         } else {
4413             if (m/^<NAME>(.*)<\/NAME>/) {
4414                 $signal_name = $1;
4415                 if ($signal_name =~ m/^(.*)::(.*)$/) {
4416                     $signal_object = $1;
4417                     ($signal_name = $2) =~ s/_/-/g;
4418                     #print "Found signal: $signal_name\n";
4419                 } else {
4420                     &LogWarning ($file, $., "Invalid signal name: $signal_name.");
4421                 }
4422             } elsif (m/^<RETURNS>(.*)<\/RETURNS>/) {
4423                 $signal_returns = $1;
4424             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4425                 $signal_flags = $1;
4426             } elsif (m%^</SIGNAL>%) {
4427                 #print "Found end of signal: ${signal_object}::${signal_name}\nReturns: ${signal_returns}\n${signal_prototype}";
4428                 push (@SignalObjects, $signal_object);
4429                 push (@SignalNames, $signal_name);
4430                 push (@SignalReturns, $signal_returns);
4431                 push (@SignalFlags, $signal_flags);
4432                 push (@SignalPrototypes, $signal_prototype);
4433                 $in_signal = 0;
4434             } else {
4435                 $signal_prototype .= $_;
4436             }
4437         }
4438     }
4439     close (INPUT);
4443 #############################################################################
4444 # Function    : ReadTemplateFile
4445 # Description : This reads in the manually-edited documentation file
4446 #               corresponding to the file currently being created, so we can
4447 #               insert the documentation at the appropriate places.
4448 #               It outputs %SymbolTypes, %SymbolDocs and %SymbolParams, which
4449 #               is a hash of arrays.
4450 #               NOTE: This function is duplicated in gtkdoc-mktmpl (but
4451 #               slightly different).
4452 # Arguments   : $docsfile - the template file to read in.
4453 #               $skip_unused_params - 1 if the unused parameters should be
4454 #                       skipped.
4455 #############################################################################
4457 sub ReadTemplateFile {
4458     my ($docsfile, $skip_unused_params) = @_;
4460     my $template = "$docsfile.sgml";
4461     if (! -f $template) {
4462         #print "File doesn't exist: $template\n";
4463         return 0;
4464     }
4465     #print "Reading $template\n";
4467     # start with empty hashes, we merge the source comment for each file
4468     # afterwards
4469     %SymbolDocs = ();
4470     %SymbolTypes = ();
4471     %SymbolParams = ();
4473     my $current_type = "";      # Type of symbol being read.
4474     my $current_symbol = "";    # Name of symbol being read.
4475     my $symbol_doc = "";                # Description of symbol being read.
4476     my @params;                 # Parameter names and descriptions of current
4477                                 #   function/macro/function typedef.
4478     my $current_param = -1;     # Index of parameter currently being read.
4479                                 #   Note that the param array contains pairs
4480                                 #   of param name & description.
4481     my $in_unused_params = 0;   # True if we are reading in the unused params.
4482     my $in_deprecated = 0;
4483     my $in_since = 0;
4484     my $in_stability = 0;
4486     open (DOCS, "$template")
4487         || die "Can't open $template: $!";
4488     while (<DOCS>) {
4489         if (m/^<!-- ##### ([A-Z_]+) (\S+) ##### -->/) {
4490             my $type = $1;
4491             my $symbol = $2;
4492             if ($symbol eq "Title"
4493                 || $symbol eq "Short_Description"
4494                 || $symbol eq "Long_Description"
4495                 || $symbol eq "See_Also"
4496                 || $symbol eq "Stability_Level"
4497                 || $symbol eq "Include") {
4499                 $symbol = $docsfile . ":" . $symbol;
4500             }
4502             #print "Found symbol: $symbol\n";
4503             # Remember file and line for the symbol
4504             $SymbolSourceFile{$symbol} = $template;
4505             $SymbolSourceLine{$symbol} = $.;
4507             # Store previous symbol, but remove any trailing blank lines.
4508             if ($current_symbol ne "") {
4509                 $symbol_doc =~ s/\s+$//;
4510                 $SymbolTypes{$current_symbol} = $current_type;
4511                 $SymbolDocs{$current_symbol} = $symbol_doc;
4513                 # Check that the stability level is valid.
4514                 if ($StabilityLevel{$current_symbol}) {
4515                     $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4516                 }
4518                 if ($current_param >= 0) {
4519                     $SymbolParams{$current_symbol} = [ @params ];
4520                 } else {
4521                     # Delete any existing params in case we are overriding a
4522                     # previously read template.
4523                     delete $SymbolParams{$current_symbol};
4524                 }
4525             }
4526             $current_type = $type;
4527             $current_symbol = $symbol;
4528             $current_param = -1;
4529             $in_unused_params = 0;
4530             $in_deprecated = 0;
4531             $in_since = 0;
4532             $in_stability = 0;
4533             $symbol_doc = "";
4534             @params = ();
4536         } elsif (m/^<!-- # Unused Parameters # -->/) {
4537             #print "DEBUG: Found unused parameters\n";
4538             $in_unused_params = 1;
4539             next;
4541         } elsif ($in_unused_params && $skip_unused_params) {
4542             # When outputting the DocBook we skip unused parameters.
4543             #print "DEBUG: Skipping unused param: $_";
4544             next;
4546         } else {
4547             # Check if param found. Need to handle "..." and "format...".
4548             if (s/^\@([\w\.]+):\040?//) {
4549                 my $param_name = $1;
4550                 my $param_desc = $_;
4551                 # Allow variations of 'Returns'
4552                 if ($param_name =~ m/^[Rr]eturns?$/) {
4553                     $param_name = "Returns";
4554                 }
4556                 # strip trailing whitespaces and blank lines
4557                 s/\s+\n$/\n/m;
4558                 s/\n+$/\n/sm;
4559                 #print "Found param for symbol $current_symbol : '$param_name'= '$_'";
4561                 if ($param_name eq "Deprecated") {
4562                     $in_deprecated = 1;
4563                     $Deprecated{$current_symbol} = $_;
4564                 } elsif ($param_name eq "Since") {
4565                     $in_since = 1;
4566                     chomp;
4567                     $Since{$current_symbol} = $_;
4568                 } elsif ($param_name eq "Stability") {
4569                     $in_stability = 1;
4570                     $StabilityLevel{$current_symbol} = $_;
4571                 } else {
4572                     push (@params, $param_name);
4573                     push (@params, $param_desc);
4574                     $current_param += $PARAM_FIELD_COUNT;
4575                 }
4576             } else {
4577                 # strip trailing whitespaces and blank lines
4578                 s/\s+\n$/\n/m;
4579                 s/\n+$/\n/sm;
4580                 
4581                 if (!m/^\s+$/) {
4582                     if ($in_deprecated) {
4583                         $Deprecated{$current_symbol} .= $_;
4584                     } elsif ($in_since) {
4585                         &LogWarning ($template, $., "multi-line since docs found");
4586                         #$Since{$current_symbol} .= $_;
4587                     } elsif ($in_stability) {
4588                         $StabilityLevel{$current_symbol} .= $_;
4589                     } elsif ($current_param >= 0) {
4590                         $params[$current_param] .= $_;
4591                     } else {
4592                         $symbol_doc .= $_;
4593                     }
4594                 }
4595             }
4596         }
4597     }
4599     # Remember to finish the current symbol doccs.
4600     if ($current_symbol ne "") {
4602         $symbol_doc =~ s/\s+$//;
4603         $SymbolTypes{$current_symbol} = $current_type;
4604         $SymbolDocs{$current_symbol} = $symbol_doc;
4606         # Check that the stability level is valid.
4607         if ($StabilityLevel{$current_symbol}) {
4608             $StabilityLevel{$current_symbol} = &ParseStabilityLevel($StabilityLevel{$current_symbol}, $template, $., "Stability level for $current_symbol");
4609         }
4611         if ($current_param >= 0) {
4612             $SymbolParams{$current_symbol} = [ @params ];
4613         } else {
4614             # Delete any existing params in case we are overriding a
4615             # previously read template.
4616             delete $SymbolParams{$current_symbol};
4617         }
4618     }
4620     close (DOCS);
4621     return 1;
4625 #############################################################################
4626 # Function    : ReadObjectHierarchy
4627 # Description : This reads in the $MODULE-hierarchy.txt file containing all
4628 #               the GtkObject subclasses described in this module (and their
4629 #               ancestors).
4630 #               It places them in the @Objects array, and places their level
4631 #               in the widget hierarchy in the @ObjectLevels array, at the
4632 #               same index. GtkObject, the root object, has a level of 1.
4634 #               FIXME: the version in gtkdoc-mkdb also generates tree_index.sgml
4635 #               as it goes along, this should be split out into a separate
4636 #               function.
4638 # Arguments   : none
4639 #############################################################################
4641 sub ReadObjectHierarchy {
4642     @Objects = ();
4643     @ObjectLevels = ();
4645     if (! -f $OBJECT_TREE_FILE) {
4646         return;
4647     }
4648     if (!open (INPUT, $OBJECT_TREE_FILE)) {
4649         warn "Can't open $OBJECT_TREE_FILE - skipping object tree\n";
4650         return;
4651     }
4653     # FIXME: use $OUTPUT_FORMAT
4654     # my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.$OUTPUT_FORMAT";
4655     my $old_tree_index = "$SGML_OUTPUT_DIR/tree_index.sgml";
4656     my $new_tree_index = "$SGML_OUTPUT_DIR/tree_index.new";
4658     open (OUTPUT, ">$new_tree_index")
4659         || die "Can't create $new_tree_index: $!";
4661     if (lc($OUTPUT_FORMAT) eq "xml") {
4662         my $tree_header = $doctype_header;
4664         $tree_header =~ s/<!DOCTYPE \w+/<!DOCTYPE screen/;
4665         print (OUTPUT "$tree_header");
4666     }
4667     print (OUTPUT "<screen>\n");
4669     # Only emit objects if they are supposed to be documented, or if
4670     # they have documented children. To implement this, we maintain a
4671     # stack of pending objects which will be emitted if a documented
4672     # child turns up.
4673     my @pending_objects = ();
4674     my @pending_levels = ();
4675     while (<INPUT>) {
4676         if (m/\S+/) {
4677             my $object = $&;
4678             my $level = (length($`)) / 2 + 1;
4679             my $xref = "";
4681             while (($#pending_levels >= 0) && ($pending_levels[$#pending_levels] >= $level)) {
4682                 my $pobject = pop(@pending_objects);
4683                 my $plevel = pop(@pending_levels);
4684             }
4686             push (@pending_objects, $object);
4687             push (@pending_levels, $level);
4689             if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4690                 while ($#pending_levels >= 0) {
4691                     $object = shift @pending_objects;
4692                     $level = shift @pending_levels;
4693                     $xref = &MakeXRef ($object);
4695                     print (OUTPUT ' ' x ($level * 4), "$xref\n");
4696                     push (@Objects, $object);
4697                     push (@ObjectLevels, $level);
4698                 }
4699             }
4700         }
4701     }
4702     print (OUTPUT "</screen>\n");
4704     close (INPUT);
4705     close (OUTPUT);
4707     &UpdateFileIfChanged ($old_tree_index, $new_tree_index, 0);
4709     &OutputObjectList;
4712 #############################################################################
4713 # Function    : ReadInterfaces
4714 # Description : This reads in the $MODULE.interfaces file.
4716 # Arguments   : none
4717 #############################################################################
4719 sub ReadInterfaces {
4720     %Interfaces = ();
4722     if (! -f $INTERFACES_FILE) {
4723         return;
4724     }
4725     if (!open (INPUT, $INTERFACES_FILE)) {
4726         warn "Can't open $INTERFACES_FILE - skipping interfaces\n";
4727         return;
4728     }
4730     while (<INPUT>) {
4731        chomp;
4732        my ($object, @ifaces) = split;
4733        if (exists($KnownSymbols{$object}) && $KnownSymbols{$object} == 1) {
4734            my @knownIfaces = ();
4736            # filter out private interfaces, but leave foreign interfaces
4737            foreach my $iface (@ifaces) {
4738                if (!exists($KnownSymbols{$iface}) || $KnownSymbols{$iface} == 1) {
4739                    push (@knownIfaces, $iface);
4740                }
4741              }
4743            $Interfaces{$object} = join(' ', @knownIfaces);
4744        }
4745     }
4746     close (INPUT);
4749 #############################################################################
4750 # Function    : ReadPrerequisites
4751 # Description : This reads in the $MODULE.prerequisites file.
4753 # Arguments   : none
4754 #############################################################################
4756 sub ReadPrerequisites {
4757     %Prerequisites = ();
4759     if (! -f $PREREQUISITES_FILE) {
4760         return;
4761     }
4762     if (!open (INPUT, $PREREQUISITES_FILE)) {
4763         warn "Can't open $PREREQUISITES_FILE - skipping prerequisites\n";
4764         return;
4765     }
4767     while (<INPUT>) {
4768        chomp;
4769        my ($iface, @prereqs) = split;
4770        if (exists($KnownSymbols{$iface}) && $KnownSymbols{$iface} == 1) {
4771            my @knownPrereqs = ();
4773            # filter out private prerequisites, but leave foreign prerequisites
4774            foreach my $prereq (@prereqs) {
4775                if (!exists($KnownSymbols{$prereq}) || $KnownSymbols{$prereq} == 1) {
4776                   push (@knownPrereqs, $prereq);
4777                }
4778            }
4780            $Prerequisites{$iface} = join(' ', @knownPrereqs);
4781        }
4782     }
4783     close (INPUT);
4786 #############################################################################
4787 # Function    : ReadArgsFile
4788 # Description : This reads in an existing file which contains information on
4789 #               all GTK args. It creates the arrays @ArgObjects, @ArgNames,
4790 #               @ArgTypes, @ArgFlags, @ArgNicks and @ArgBlurbs containing info
4791 #               on the args.
4792 # Arguments   : $file - the file containing the arg information.
4793 #############################################################################
4795 sub ReadArgsFile {
4796     my ($file) = @_;
4798     my $in_arg = 0;
4799     my $arg_object;
4800     my $arg_name;
4801     my $arg_type;
4802     my $arg_flags;
4803     my $arg_nick;
4804     my $arg_blurb;
4805     my $arg_default;
4806     my $arg_range;
4808     # Reset the args info.
4809     @ArgObjects = ();
4810     @ArgNames = ();
4811     @ArgTypes = ();
4812     @ArgFlags = ();
4813     @ArgNicks = ();
4814     @ArgBlurbs = ();
4815     @ArgDefaults = ();
4816     @ArgRanges = ();
4818     if (! -f $file) {
4819         return;
4820     }
4821     if (!open (INPUT, $file)) {
4822         warn "Can't open $file - skipping args\n";
4823         return;
4824     }
4825     while (<INPUT>) {
4826         if (!$in_arg) {
4827             if (m/^<ARG>/) {
4828                 $in_arg = 1;
4829                 $arg_object = "";
4830                 $arg_name = "";
4831                 $arg_type = "";
4832                 $arg_flags = "";
4833                 $arg_nick = "";
4834                 $arg_blurb = "";
4835                 $arg_default = "";
4836                 $arg_range = "";
4837             }
4838         } else {
4839             if (m/^<NAME>(.*)<\/NAME>/) {
4840                 $arg_name = $1;
4841                 if ($arg_name =~ m/^(.*)::(.*)$/) {
4842                     $arg_object = $1;
4843                     ($arg_name = $2) =~ s/_/-/g;
4844                     #print "Found arg: $arg_name\n";
4845                 } else {
4846                     &LogWarning ($file, $., "Invalid argument name: $arg_name");
4847                 }
4848             } elsif (m/^<TYPE>(.*)<\/TYPE>/) {
4849                 $arg_type = $1;
4850             } elsif (m/^<RANGE>(.*)<\/RANGE>/) {
4851                 $arg_range = $1;
4852             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
4853                 $arg_flags = $1;
4854             } elsif (m/^<NICK>(.*)<\/NICK>/) {
4855                 $arg_nick = $1;
4856             } elsif (m/^<BLURB>(.*)<\/BLURB>/) {
4857                 $arg_blurb = $1;
4858                 if ($arg_blurb eq "(null)") {
4859                   $arg_blurb = "";
4860                   &LogWarning ($file, $., "Property ${arg_object}:${arg_name} has no documentation.");
4861                 }
4862             } elsif (m/^<DEFAULT>(.*)<\/DEFAULT>/) {
4863                 $arg_default = $1;
4864             } elsif (m%^</ARG>%) {
4865                 #print "Found end of arg: ${arg_object}::${arg_name}\n${arg_type} : ${arg_flags}\n";
4866                 push (@ArgObjects, $arg_object);
4867                 push (@ArgNames, $arg_name);
4868                 push (@ArgTypes, $arg_type);
4869                 push (@ArgRanges, $arg_range);
4870                 push (@ArgFlags, $arg_flags);
4871                 push (@ArgNicks, $arg_nick);
4872                 push (@ArgBlurbs, $arg_blurb);
4873                 push (@ArgDefaults, $arg_default);
4874                 $in_arg = 0;
4875             }
4876         }
4877     }
4878     close (INPUT);
4882 #############################################################################
4883 # Function    : CheckIsObject
4884 # Description : Returns 1 if the given name is a GtkObject or a subclass.
4885 #               It uses the global @Objects array.
4886 #               Note that the @Objects array only contains classes in the
4887 #               current module and their ancestors - not all GTK classes.
4888 # Arguments   : $name - the name to check.
4889 #############################################################################
4891 sub CheckIsObject {
4892     my ($name) = @_;
4894     my $object;
4895     foreach $object (@Objects) {
4896         if ($object eq $name) {
4897             return 1;
4898         }
4899     }
4900     return 0;
4904 #############################################################################
4905 # Function    : MakeReturnField
4906 # Description : Pads a string to $RETURN_TYPE_FIELD_WIDTH.
4907 # Arguments   : $str - the string to pad.
4908 #############################################################################
4910 sub MakeReturnField {
4911     my ($str) = @_;
4913     return $str . (' ' x ($RETURN_TYPE_FIELD_WIDTH - length ($str)));
4916 #############################################################################
4917 # Function    : GetSymbolSourceFile
4918 # Description : Get the filename where the symbol docs where taken from.
4919 # Arguments   : $symbol - the symbol name
4920 #############################################################################
4922 sub GetSymbolSourceFile {
4923     my ($symbol) = @_;
4925     if (defined($SourceSymbolSourceFile{$symbol})) {
4926         return $SourceSymbolSourceFile{$symbol};
4927     } elsif (defined($SymbolSourceFile{$symbol})) {
4928         return $SymbolSourceFile{$symbol};
4929     } else {
4930         return "";
4931     }
4934 #############################################################################
4935 # Function    : GetSymbolSourceLine
4936 # Description : Get the file line where the symbol docs where taken from.
4937 # Arguments   : $symbol - the symbol name
4938 #############################################################################
4940 sub GetSymbolSourceLine {
4941     my ($symbol) = @_;
4943     if (defined($SourceSymbolSourceLine{$symbol})) {
4944         return $SourceSymbolSourceLine{$symbol};
4945     } elsif (defined($SymbolSourceLine{$symbol})) {
4946         return $SymbolSourceLine{$symbol};
4947     } else {
4948         return 0;
4949     }