patch by: David Nečas <yeti@physics.muni.cz>
[gtk-doc.git] / gtkdoc-mktmpl.in
blob4fa95f3a7c5021a9a994776bcaab6673a435503a
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  Damon Chaplin
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #############################################################################
23 # Script      : gtkdoc-mktmpl
24 # Description : This creates or updates the template files which contain the
25 #               manually-edited documentation. (A 'template' is a simple text
26 #               form which is filled in with the description of a function,
27 #               macro, enum, or struct. For functions and macros it also
28 #               contains fields for describing the parameters.)
30 #               This script reads in the existing templates, found in
31 #               tmpl/*.sgml, moves these files to tmpl/*.sgml.bak, and then
32 #               recreates the .sgml files according to the structure given in
33 #               the file $MODULE-sections.txt.
35 #               Any new templates added, or new function parameters, are
36 #               marked with 'FIXME' so you can do a grep to see which parts
37 #               need updating.
39 #               Any templates which are no longer used (i.e. they are remove
40 #               from $MODULE-sections.txt) are placed in the file
41 #               tmpl/$MODULE-unused.sgml. If they are included again later
42 #               they are automatically copied back into position.
43 #               If you are certain that these templates will never be used
44 #               again you can delete them from tmpl/$MODULE-unused.sgml.
46 #               Any parameters to functions which are no longer used are
47 #               separated from the rest of the parameters with the line
48 #               '<!-- # Unused Parameters # -->'. It may be that the parameter
49 #               name has just been changed, in which case you can copy the
50 #               description to the parameter with the new name. You can delete
51 #               the unused parameter descriptions when no longer needed.
52 #############################################################################
54 use strict;
55 use Getopt::Long;
57 unshift @INC, '@PACKAGE_DATA_DIR@';
58 require "gtkdoc-common.pl";
60 # Options
62 # name of documentation module
63 my $MODULE;
64 my $TMPL_DIR;
65 my $FLAG_CHANGES;
66 my $PRINT_VERSION;
67 my $PRINT_HELP;
68 my $ONLY_SECTION_TMPL;
70 my %optctl = ('module' => \$MODULE,
71               'flag-changes' => \$FLAG_CHANGES,
72               'output-dir' => \$TMPL_DIR,
73               'only-section-tmpl' => \$ONLY_SECTION_TMPL,
74               'version' => \$PRINT_VERSION,
75               'help' => \$PRINT_HELP);
76 GetOptions(\%optctl, "module=s", "flag-changes!", "output-dir:s", "only-section-tmpl!", "version", "help");
78 if ($PRINT_VERSION) {
79     print "@VERSION@\n";
80     exit 0;
83 if (!$MODULE) {
84     $PRINT_HELP = 1;
87 if ($PRINT_HELP) {
88     print <<EOF;
89 gtkdoc-mktmpl version @VERSION@ - generate documentation templates
91 --module=MODULE_NAME Name of the doc module being parsed
92 --flag-changes       If specified, changes in templates are flagged
93 --output-dir=DIRNAME The directory where the results are stored
94 --only-section-tmpl  Only include section information in templates
95 --version            Print the version of this program
96 --help               Print this help
97 EOF
98     exit 0;
101 my $ROOT_DIR = ".";
103 # The directory containing the template files.
104 $TMPL_DIR = $TMPL_DIR ? $TMPL_DIR : "$ROOT_DIR/tmpl";
106 # This file contains the object hierarchy.
107 my $OBJECT_TREE_FILE = "$ROOT_DIR/$MODULE.hierarchy";
109 # The file containing signal handler prototype information.
110 my $SIGNALS_FILE = "$ROOT_DIR/$MODULE.signals";
112 # The file containing Arg information.
113 my $ARGS_FILE = "$ROOT_DIR/$MODULE.args";
115 # Set the flag to indicate changes, if requested.
116 my $CHANGES_FLAG = $FLAG_CHANGES ? "FIXME" : "";
118 # These global arrays store information on signals. Each signal has an entry
119 # in each of these arrays at the same index, like a multi-dimensional array.
120 my @SignalObjects;      # The GtkObject which emits the signal.
121 my @SignalNames;        # The signal name.
122 my @SignalReturns;      # The return type.
123 my @SignalFlags;        # Flags for the signal
124 my @SignalPrototypes;   # The rest of the prototype of the signal handler.
126 # These global arrays store information on Args. Each Arg has an entry
127 # in each of these arrays at the same index, like a multi-dimensional array.
128 my @ArgObjects;         # The GtkObject which has the Arg.
129 my @ArgNames;           # The Arg name.
130 my @ArgTypes;           # The Arg type - gint, GtkArrowType etc.
131 my @ArgFlags;           # How the Arg can be used - readable/writable etc.
133 # These global hashes store declaration info keyed on a symbol name.
134 my %Declarations;
135 my %DeclarationTypes;
136 my %DeclarationConditional;
137 my %DeclarationOutput;
139 # These global hashes store the existing documentation.
140 my %SymbolDocs;
141 my %SymbolTypes;
142 my %SymbolParams;
143 my %SymbolSourceFile;
144 my %SymbolSourceLine;
146 # These global arrays store GObject and subclasses and the hierarchy.
147 my @Objects;
148 my @ObjectLevels;
150 &ReadSignalsFile ($SIGNALS_FILE);
151 &ReadArgsFile ($ARGS_FILE);
152 &ReadObjectHierarchy;
154 &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-decl.txt", 0);
155 if (-f "$ROOT_DIR/$MODULE-overrides.txt") {
156     &ReadDeclarationsFile ("$ROOT_DIR/$MODULE-overrides.txt", 1);
158 &ReadExistingTemplates;
160 my $changed = 0;
162 if (&UpdateTemplates ("$ROOT_DIR/$MODULE-sections.txt")) {
163   $changed = 1;
165 &OutputUnusedTemplates;
166 if (&CheckAllDeclarationsOutput) {
167   $changed = 1;
170 if ($changed || ! -e "$ROOT_DIR/tmpl.stamp") {
171     open (TIMESTAMP, ">$ROOT_DIR/tmpl.stamp")
172         || die "Can't create $ROOT_DIR/tmpl.stamp";
173     print (TIMESTAMP "timestamp");
174     close (TIMESTAMP);
177 #############################################################################
178 # Function    : ReadExistingTemplates
179 # Description : This reads in all the existing documentation, into the global
180 #               variables %SymbolDocs, %SymbolTypes, and %SymbolParams (a
181 #               hash of arrays).
182 # Arguments   : none
183 #############################################################################
185 sub ReadExistingTemplates {
186     %SymbolDocs = ();
187     %SymbolTypes = ();
188     %SymbolParams = ();
190     # Read the unused docs first, so they get overridden by any real docs.
191     # (though this shouldn't happen often).
192     my $unused_doc = "$TMPL_DIR/$MODULE-unused.sgml";
193     if (-e $unused_doc) {
194         &ReadTemplateFile ($unused_doc, 0);
195     }
197     while (<$TMPL_DIR/*.sgml>) {
198 #       print "Reading $_\n";
199         if ($_ eq $unused_doc) {
200 #           print "skipping $unused_doc\n";
201         } else {
202             &ReadTemplateFile ($_, 0);
203         }
204     }
208 #############################################################################
209 # Function    : UpdateTemplates
210 # Description : This collects the output for each section of the docs, and
211 #               outputs each file when the end of the section is found.
212 # Arguments   : $file - the file containing the sections of the docs.
213 #############################################################################
215 sub UpdateTemplates {
216     my ($file) = @_;
217 #    print "Reading: $file\n";
219     open (INPUT, $file)
220         || die "Can't open $file";
222     # Create the top output directory if it doesn't exist.
223     if (! -e $TMPL_DIR) {
224         mkdir ("$TMPL_DIR", 0777)
225             || die "Can't create directory: $TMPL_DIR";
226     }
228     my $filename = "";
229     my $title = "";
230     my $subsection = "";
231     my $output;
232     my $changed = 0;
233     while (<INPUT>) {
234         if (m/^#/) {
235             next;
237         } elsif (m/^<SECTION>/) {
238             $output = "";
240         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
241             $subsection = $1;
242             next;
244         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
245             $title = $1;
246 #           print "Section: $title\n";
248         } elsif (m/^<FILE>(.*)<\/FILE>/) {
249             $filename = $1;
251         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
252             next;
254         } elsif (m/^<\/SECTION>/) {
255             if ($title eq "") {
256                 $title = $filename;
257             }
258 #           print "End of section: $title\n";
260             $filename =~ s/\s/_/g;
261             $filename .= ".sgml";
263             if (&OutputTemplateFile ($filename, $title, \$output)) {
264               $changed = 1;
265             }
267             $title = "";
268             $subsection = "";
270         } elsif (m/^(\S+)/) {
271             my $symbol = $1;
272 #           print "  Symbol: $symbol\n";
274             my $declaration = $Declarations{$1};
275             if (defined ($declaration)) {
276                 # We don't want templates for standard macros/functions of
277                 # GObjects or private declarations.
278                 if ($subsection ne "Standard" && $subsection ne "Private") {
279                     $output .= &OutputDeclaration ($DeclarationTypes {$symbol},
280                                                    $symbol, $declaration);
282                     $output .= &OutputSignalTemplates ($symbol);
283                     $output .= &OutputArgTemplates ($symbol);
284                 }
286                 # Note that the declaration has been output.
287                 $DeclarationOutput{$symbol} = 1;
289                 if ($declaration eq '##conditional##') {
290 #                   print "Conditional $DeclarationTypes{$symbol}\n";
291                 }
293             } else {
294                 &LogWarning ($file, $., "No declaration found for: $1");
295             }
296         }
297     }
298     close (INPUT);
300     return $changed;
304 #############################################################################
305 # Function    : CheckAllDeclarationsOutput
306 # Description : This steps through all the declarations that were loaded, and
307 #               makes sure that each one has been output, by checking the
308 #               corresponding flag in the %DeclarationOutput hash. It is
309 #               intended to check that any new declarations in new versions
310 #               of the module get added to the $MODULE-sections.txt file.
311 # Arguments   : none
312 #############################################################################
314 sub CheckAllDeclarationsOutput {
315     my $num_unused = 0;
317     my $old_unused_file = "$ROOT_DIR/$MODULE-unused.txt";
318     my $new_unused_file = "$ROOT_DIR/$MODULE-unused.new";
320     open (UNUSED, ">$new_unused_file")
321         || die "Can't open $new_unused_file";
322     my ($symbol);
323     foreach $symbol (sort keys (%Declarations)) {
324         if (!defined ($DeclarationOutput{$symbol})) {
325             print (UNUSED "$symbol\n");
326             $num_unused++;
327         }
328     }
329     close (UNUSED);
330     if ($num_unused != 0) {
331         &LogWarning ($old_unused_file, 1, "$num_unused unused declarations.".
332             "They should be added to $MODULE-sections.txt in the appropriate place.");
333     }
335     return &UpdateFileIfChanged ($old_unused_file, $new_unused_file, 0);
339 #############################################################################
340 # Function    : OutputDeclaration
341 # Description : This returns the template for one symbol & declaration.
342 #               Note that it uses the global %SymbolDocs and %SymbolParams to
343 #               lookup any existing documentation.
344 # Arguments   : $type - the type of the symbol ('FUNCTION'/'MACRO' etc.)
345 #               $symbol - the symbol name.
346 #               $declaration - the declaration of the symbol.
347 #############################################################################
349 sub OutputDeclaration {
350     my ($type, $symbol, $declaration) = @_;
351     my ($output) = "";
353     #print "Outputting $type: $symbol\n";
355     # See if symbol already has a description.
356     my ($symbol_desc) = $SymbolDocs{$symbol};
357     my ($template_exists);
358     if (defined ($symbol_desc)) {
359         $template_exists = 1;
360         $symbol_desc =~ s/\s+$//;
361     } else {
362         $template_exists = 0;
363         $symbol_desc = "<para>\n$CHANGES_FLAG\n</para>";
364     }
366     $output .= <<EOF;
367 <!-- ##### $type $symbol ##### -->
368 $symbol_desc
372     # For functions, function typedefs and macros, we output the arguments.
373     # For functions and function typedefs we also output the return value.
374     if ($type eq "FUNCTION" || $type eq "USER_FUNCTION") {
375         # Take out the return type
376         $declaration =~ s/<RETURNS>\s*((const\s+|G_CONST_RETURN\s+|unsigned\s+|struct\s+|enum\s+)*)(\w+)\s*(\**\s*(const|G_CONST_RETURN)?\s*\**\s*(restrict)?\s*)<\/RETURNS>\n//;
377         my $ret_type_modifier = defined($1) ? $1 : "";
378         my $ret_type = $3;
379         my $ret_type_pointer = $4;
381         my ($param_num) = 0;
382         while ($declaration ne "") {
383             if ($declaration =~ s/^[\s,]+//) {
384                 # skip whitespace and commas
385                 next;
387             } elsif ($declaration =~ s/^void\s*[,\n]//) {
388                 if ($param_num != 0) {
389                     &LogWarning ($SymbolSourceFile{$symbol},$SymbolSourceLine{$symbol}, "void used as parameter in function $symbol");
390                 }
392             } elsif ($declaration =~ s/^...\s*[,\n]//) {
393                 $output .= &OutputParam ($symbol, "Varargs",
394                                          $template_exists, 1, "");
396             # Try to match a standard parameter (keep in sync with gtkdoc-mkdb)
397             #                                $1                                                                                                                                    $2                             $3                                                           $4       $5
398             } 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]//) {
399                 my $pre         = defined($1) ? $1 : "";
400                 my $vtype       = $2;
401                 my $ptr         = defined($3) ? $3 : "";
402                 my $name        = defined($4) ? $4 : "";
404                 $pre   =~ s/\s+/ /g;
405                 $vtype =~ s/\s+/ /g;
406                 $ptr   =~ s/\s+/ /g;
407                 $ptr   =~ s/\s+$//;
408                 if ($ptr && $ptr !~ m/\*$/) { $ptr .= " "; }
410                 if (($name eq "") && $pre =~ m/^((un)?signed .*)\s?/ ) {
411                     $name  = $vtype;
412                     $vtype = "$1";
413                     $pre   = "";
414                 }
416                 #print "$symbol: '$pre' '$vtype' '$ptr' '$name' \n";
418                 if ($name eq "") {
419                     $name = "Param" . ($param_num + 1);
420                 }
421                 $output .= &OutputParam ($symbol, $name, $template_exists, 1, "");
423             # Try to match parameters which are functions (keep in sync with gtkdoc-mkdb)
424             #                              $1                                       $2          $3      $4                      $5                    $7             $8
425             } 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]//) {
426                 my $name = $6;
427                 $output .= &OutputParam ($symbol, $name, $template_exists, 1,
428                                          "");
430             } else {
431                 print "###Can't parse args for function $symbol: $declaration\n";
432                 last;
433             }
434             $param_num++;
435         }
437         if ($ret_type ne "void" || $ret_type_modifier || $ret_type_pointer) {
438             $output .= &OutputParam ($symbol, "Returns", $template_exists, 1,
439                                      "");
440         }
441         $output .= &OutputParam ($symbol, "Deprecated", $template_exists, 0, "");
442         $output .= &OutputParam ($symbol, "Since", $template_exists, 0, "");
443         $output .= &OutputParam ($symbol, "Stability", $template_exists, 0, "");
444         $output .= &OutputOldParams ($symbol);
445         $output .= "\n";
446     }
448     if ($type eq "MACRO") {
449         if ($declaration =~ m/^\s*#\s*define\s+\w+\(([^\)]*)\)/) {
450             my ($params) = $1;
451             my ($param);
453             $params =~ s/\\\n//g;
454             foreach $param (split (/,/, $params)) {
455                 $param =~ s/^\s+//;
456                 $param =~ s/\s*$//;
457                 if ($param =~ m/\S/) {
458                     $output .= &OutputParam ($symbol, $param, $template_exists,
459                                              1, "");
460                 }
461             }
462         }
463         $output .= &OutputParam ($symbol, "Returns", $template_exists, 0, "");
464         $output .= &OutputParam ($symbol, "Deprecated", $template_exists, 0, "");
465         $output .= &OutputParam ($symbol, "Since", $template_exists, 0, "");
466         $output .= &OutputParam ($symbol, "Stability", $template_exists, 0, "");
467         $output .= &OutputOldParams ($symbol);
468         $output .= "\n";
469     }
471     if ($type eq "STRUCT") {
472         my $is_object_struct = CheckIsObject ($symbol);
473         my @fields = ParseStructDeclaration($declaration, $is_object_struct, 1);
475         for (my $i = 0; $i <= $#fields; $i += 2) {
476             my $field_name = $fields[$i];
477             $output .= &OutputParam ($symbol, $field_name, $template_exists, 1, "");
478         }
479         $output .= &OutputParam ($symbol, "Deprecated", $template_exists, 0, "");
480         $output .= &OutputParam ($symbol, "Since", $template_exists, 0, "");
481         $output .= &OutputParam ($symbol, "Stability", $template_exists, 0, "");
482     }
484     if ($type eq "ENUM") {
485         my @members = ParseEnumDeclaration($declaration);
487         for my $member (@members) {
488             $output .= &OutputParam ($symbol, $member, $template_exists, 1, "");
489         }
490         $output .= &OutputParam ($symbol, "Deprecated", $template_exists, 0, "");
491         $output .= &OutputParam ($symbol, "Since", $template_exists, 0, "");
492         $output .= &OutputParam ($symbol, "Stability", $template_exists, 0, "");
493     }
495     $output .= "\n";
497     # Remove the used docs from the hashes.
498     if ($template_exists) {
499         delete $SymbolDocs{$symbol};
500         delete $SymbolParams{$symbol};
501     }
503     return $output;
507 #############################################################################
508 # Function    : OutputParam
509 # Description : This outputs the part of a template for one parameter.
510 #               It first checks if the parameter is already described, and if
511 #               so it uses that description, and clears it so it isn't output
512 #               as an old param.
513 # Arguments   : $symbol - the symbol (function or macro) name.
514 #               $param_to_output - the parameter to add.
515 #               $template_exists - TRUE if the template already existed in
516 #                 template files. If it did, then we will flag any changes
517 #                 with 'FIXME'.
518 #               $force_output - TRUE if the parameter should be output even
519 #                 if it didn't already exist in the template. (The return
520 #                 values of macros are added manually if required, and so we
521 #                 never add it here - we only copy it if it already exists.)
522 #               $default_description - the default description of the
523 #                 parameter to be used if it doesn't already exist. (Signal
524 #                 handlers have a few common parameters.)
525 #############################################################################
527 sub OutputParam {
528     my ($symbol, $param_to_output, $template_exists,
529         $force_output, $default_description) = @_;
530     my ($j);
532     my ($params) = $SymbolParams{$symbol};
533     if (defined ($params)) {
534         for ($j = 0; $j <= $#$params; $j += 2) {
535             my $param_name = $$params[$j];
536             my $param_desc = $$params[$j + 1];
538             if ($param_name eq $param_to_output) {
539                 $param_desc =~ s/\s+$//;
540                 $$params[$j] = "";
541                 $$params[$j + 1] = "";
542                 return "\@$param_name: $param_desc\n";
543             }
544         }
545     }
547     # If the template was already in a file, flag the new parameter.
548     # If not, the template itself will be flagged, so we don't need to flag
549     # all the new parameters as well.
550     if ($force_output) {
551         if ($default_description ne "") {
552             $default_description =~ s/\s+$//;
553             return "\@$param_to_output: $default_description\n";
554         } else {
555             if ($template_exists) {
556                 return "\@$param_to_output: $CHANGES_FLAG\n";
557             } else {
558                 return "\@$param_to_output: \n";
559             }
560         }
561     }
562     return "";
566 #############################################################################
567 # Function    : OutputOldParams
568 # Description : This returns all the existing documentation for parameters of
569 #               the given function/macro/signal symbol which are unused, with
570 #               a comment before them.
571 # Arguments   : $symbol - the symbol (function/macro/signal) name.
572 #############################################################################
574 sub OutputOldParams {
575     my ($symbol) = @_;
576     my $output = "";
578     my ($params) = $SymbolParams{$symbol};
579     if (defined ($params)) {
580         my $j;
581         for ($j = 0; $j <= $#$params; $j += 2) {
582             my $param_name = $$params[$j];
583             my $param_desc = $$params[$j + 1];
585             if ($param_name ne "") {
586                 $param_desc =~ s/\s+$//;
588                 # There's no point keeping it if it has no docs.
589                 if ($param_desc ne "") {
590                   $output .= "\@$param_name: $param_desc\n";
591                 }
592             }
593         }
594     }
595     if ($output) {
596         $output = "<!-- # Unused Parameters # -->\n" . $output;
597     }
598     return $output;
602 #############################################################################
603 # Function    : OutputTemplateFile
604 # Description : This outputs one template file.
605 # Arguments   : $file - the basename of the file to output.
606 #               $title - the title from the $MODULE-sections.txt file. This
607 #                 will be overridden by any title given in the template file.
608 #               $output - reference to the templates to output.
609 #############################################################################
611 sub OutputTemplateFile {
612     my ($file, $title, $output) = @_;
614     my ($short_desc, $long_desc, $see_also, $stability);
616     if (defined ($SymbolDocs{"$TMPL_DIR/$file:Title"})) {
617         $title = $SymbolDocs{"$TMPL_DIR/$file:Title"};
618         delete $SymbolDocs{"$TMPL_DIR/$file:Title"};
619     }
620     if (defined ($SymbolDocs{"$TMPL_DIR/$file:Short_Description"})) {
621         $short_desc = $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
622         delete $SymbolDocs{"$TMPL_DIR/$file:Short_Description"};
623     } else {
624         $short_desc = "";
625     }
626     if (defined ($SymbolDocs{"$TMPL_DIR/$file:Long_Description"})) {
627         $long_desc = $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
628         delete $SymbolDocs{"$TMPL_DIR/$file:Long_Description"};
629     } else {
630         $long_desc = "<para>\n\n</para>\n";
631     }
632     if (defined ($SymbolDocs{"$TMPL_DIR/$file:See_Also"})) {
633         $see_also = $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
634         delete $SymbolDocs{"$TMPL_DIR/$file:See_Also"};
635     } else {
636         $see_also = "<para>\n\n</para>\n";
637     }
638     if (defined ($SymbolDocs{"$TMPL_DIR/$file:Stability_Level"})) {
639         $stability = $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
640         delete $SymbolDocs{"$TMPL_DIR/$file:Stability_Level"};
641     } else {
642         $stability = "";
643     }
646     my $old_tmpl_file = "$TMPL_DIR/$file";
647     my $new_tmpl_file = "$TMPL_DIR/$file.new";
649     open (OUTPUT, ">$new_tmpl_file")
650         || die "Can't create $new_tmpl_file";
652     print (OUTPUT <<EOF);
653 <!-- ##### SECTION Title ##### -->
654 $title
656 <!-- ##### SECTION Short_Description ##### -->
657 $short_desc
659 <!-- ##### SECTION Long_Description ##### -->
660 $long_desc
662 <!-- ##### SECTION See_Also ##### -->
663 $see_also
665 <!-- ##### SECTION Stability_Level ##### -->
666 $stability
670     print (OUTPUT $$output) unless $ONLY_SECTION_TMPL;
671     close (OUTPUT);
673     return &UpdateFileIfChanged ($old_tmpl_file, $new_tmpl_file, 1);
677 #############################################################################
678 # Function    : OutputSignalTemplates
679 # Description : Outputs templates for signal handlers.
680 # Arguments   : $title - the title from the $MODULE-sections.txt file. If the
681 #                 file is describing a GtkObject subclass, the title should
682 #                 be the name of the class, e.g. 'GtkButton'.
683 #############################################################################
685 sub OutputSignalTemplates {
686     my ($title) = @_;
688     my $output = "";
689     my ($i, $template_exists);
690     for ($i = 0; $i <= $#SignalObjects; $i++) {
691         if ($SignalObjects[$i] eq $title) {
692 #           print "Found signal: $SignalObjects[$i]\n";
693             my ($symbol) = "$SignalObjects[$i]::$SignalNames[$i]";
695             # See if symbol already has a description.
696             my ($symbol_desc) = $SymbolDocs{$symbol};
697             if (defined ($symbol_desc)) {
698                 $template_exists = 1;
699                 $symbol_desc =~ s/\s+$//;
700                 delete $SymbolDocs{$symbol};
701             } else {
702                 $template_exists = 0;
703                 $symbol_desc = "<para>\n$CHANGES_FLAG\n</para>";
704             }
706             $output .= <<EOF;
707 <!-- ##### SIGNAL $symbol ##### -->
708 $symbol_desc
711             my $sourceparams = $SymbolParams{$symbol};
712             my @params = split ("[,\n]", $SignalPrototypes[$i]);
713             my ($j, $name);
714             for ($j = 0; $j <= $#params; $j++) {
715                 my $param = $params[$j];
716                 $param =~ s/^\s+//;
717                 $param =~ s/\s*$//;
718                 if ($param =~ m/^\s*$/) { next; }
719                 if ($param =~ m/^void$/) { next; }
721                 if ($param =~ m/^\s*(\w+)\s*(\**)\s*([\w\[\]]+)?\s*$/) {
722                     if (defined ($sourceparams)) {
723                         $name = $$sourceparams[2 * $j];
724                     } else {
725                         $name = $3;
726                     }
728                     if (!defined ($name)) {
729                         $name = "Param" . ($j + 1);
730                     }
732                     if ($j == 0) {
733                         $output .= &OutputParam ($symbol, $name,
734                                                  $template_exists, 1,
735                                                  "the object which received the signal.");
736                     } else {
737                         $output .= &OutputParam ($symbol, $name,
738                                                  $template_exists, 1, "");
739                     }
740                 }
741             }
743             if ($SignalReturns[$i] ne "void") {
744                 $output .= &OutputParam ($symbol, "Returns", $template_exists,
745                                          1, "");
746             }
747             $output .= &OutputOldParams ($symbol);
748             $output .= "\n";
749         }
750     }
751     return $output;
755 #############################################################################
756 # Function    : OutputArgTemplates
757 # Description : Outputs templates for Args.
758 # Arguments   : $title - the title from the $MODULE-sections.txt file. If the
759 #                 file is describing a GtkObject subclass, the title should
760 #                 be the name of the class, e.g. 'GtkButton'.
761 #############################################################################
763 sub OutputArgTemplates {
764     my ($title) = @_;
766     my $output = "";
767     my $i;
768     for ($i = 0; $i <= $#ArgObjects; $i++) {
769         if ($ArgObjects[$i] eq $title) {
770 #           print "Found arg: $ArgObjects[$i]\n";
771             # I've only used one colon so we don't clash with signals.
772             my ($symbol) = "$ArgObjects[$i]:$ArgNames[$i]";
774             # See if symbol already has a description.
775             my ($symbol_desc) = $SymbolDocs{$symbol};
776             if (defined ($symbol_desc)) {
777                 delete $SymbolDocs{$symbol};
778                 $symbol_desc =~ s/\s+$//;
779             } else {
780                 $symbol_desc = "<para>\n$CHANGES_FLAG\n</para>";
781             }
783             $output .= <<EOF;
784 <!-- ##### ARG $symbol ##### -->
785 $symbol_desc
788         }
789     }
790     return $output;
794 #############################################################################
795 # Function    : OutputUnusedTemplates
796 # Description : This saves any unused documentation into $MODULE-unused.sgml.
797 # Arguments   : none
798 #############################################################################
800 sub OutputUnusedTemplates {
801     my ($old_unused_file) = "$TMPL_DIR/$MODULE-unused.sgml";
802     my ($new_unused_file) = "$TMPL_DIR/$MODULE-unused.new";
804     open (UNUSED, ">$new_unused_file")
805         || die "Can't open file: $new_unused_file";
807     my $output = "";
808     my ($symbol, $symbol_desc);
809     for $symbol (sort keys %SymbolDocs) {
810         $symbol_desc = $SymbolDocs{$symbol};
812 #       print "Unused: $symbol\n";
814         my $type = $SymbolTypes{$symbol};
815         if (!defined ($type)) {
816             $type = "UNKNOWN";
817             &LogWarning ($SymbolSourceFile{$symbol},$SymbolSourceLine{$symbol}, "Unused symbol $symbol has unknown type.");
818         }
820     $output .= <<EOF;
821 <!-- ##### $type $symbol ##### -->
822 $symbol_desc
826         my ($params) = $SymbolParams{$symbol};
827         if (defined ($params)) {
828             my $j;
829             for ($j = 0; $j <= $#$params; $j += 2) {
830                 my $param_name = $$params[$j];
831                 my $param_desc = $$params[$j + 1];
832                 $param_desc =~ s/\s+$//;
833                 $output .= "\@$param_name: $param_desc\n";
834             }
835         }
836         $output .= "\n";
837     }
839     print UNUSED $output;
840     close (UNUSED);
842     &UpdateFileIfChanged ($old_unused_file, $new_unused_file, 1);
846 #############################################################################
847 # LIBRARY FUNCTIONS -   These functions are used in both gtkdoc-mkdb and
848 #                       gtkdoc-mktmpl and should eventually be moved to a
849 #                       separate library.
850 #############################################################################
852 #############################################################################
853 # Function    : ReadDeclarationsFile
854 # Description : This reads in a file containing the function/macro/enum etc.
855 #               declarations.
857 #               Note that in some cases there are several declarations with
858 #               the same name, e.g. for conditional macros. In this case we
859 #               set a flag in the %DeclarationConditional hash so the
860 #               declaration is not shown in the docs.
862 #               If a macro and a function have the same name, e.g. for
863 #               gtk_object_ref, the function declaration takes precedence.
865 #               Some opaque structs are just declared with 'typedef struct
866 #               _name name;' in which case the declaration may be empty.
867 #               The structure may have been found later in the header, so
868 #               that overrides the empty declaration.
870 # Arguments   : $file - the declarations file to read
871 #               $override - if declarations in this file should override
872 #                       any current declaration.
873 #############################################################################
875 sub ReadDeclarationsFile {
876     my ($file, $override) = @_;
878     if ($override == 0) {
879         %Declarations = ();
880         %DeclarationTypes = ();
881         %DeclarationConditional = ();
882         %DeclarationOutput = ();
883     }
885     open (INPUT, $file)
886         || die "Can't open $file";
887     my $declaration_type = "";
888     my $declaration_name;
889     my $declaration;
890     while (<INPUT>) {
891         if (!$declaration_type) {
892             if (m/^<([^>]+)>/) {
893                 $declaration_type = $1;
894                 $declaration_name = "";
895 #               print "Found declaration: $declaration_type\n";
896                 $declaration = "";
897             }
898         } else {
899             if (m%^<NAME>(.*)</NAME>%) {
900                 $declaration_name = $1;
901             } elsif (m%<DEPRECATED/>%) {
902                 # do nothing, just skip the line; we handle this
903                 # in mkdb
904             } elsif (m%^</$declaration_type>%) {
905 #               print "Found end of declaration: $declaration_name\n";
906                 # Check that the declaration has a name
907                 if ($declaration_name eq "") {
908                     print "ERROR: $declaration_type has no name $file:$.\n";
909                 }
911                 # Check if the symbol is already defined.
912                 if (defined ($Declarations{$declaration_name})
913                     && $override == 0) {
914                     # Function declarations take precedence.
915                     if ($DeclarationTypes{$declaration_name} eq 'FUNCTION') {
916                         # Ignore it.
917                     } elsif ($declaration_type eq 'FUNCTION') {
918                         $Declarations{$declaration_name} = $declaration;
919                         $DeclarationTypes{$declaration_name} = $declaration_type;
920                     } elsif ($DeclarationTypes{$declaration_name}
921                               eq $declaration_type) {
922                         # If the existing declaration is empty, or is just a
923                         # forward declaration of a struct, override it.
924                         if ($declaration_type eq 'STRUCT') {
925                             if ($Declarations{$declaration_name} =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
926                                 $Declarations{$declaration_name} = $declaration;
927                             } elsif ($declaration =~ m/^\s*(struct\s+\w+\s*;)?\s*$/) {
928                                 # Ignore an empty or forward declaration.
929                             } else {
930                                 &LogWarning ($file, $., "Structure $declaration_name has multiple definitions.");
931                             }
933                         } else {
934                             # set flag in %DeclarationConditional hash for
935                             # multiply defined macros/typedefs.
936                             $DeclarationConditional{$declaration_name} = 1;
937                         }
938                     } else {
939                         &LogWarning ($file, $., "$declaration_name has multiple definitions.");
940                     }
941                 } else {
942                     $Declarations{$declaration_name} = $declaration;
943                     $DeclarationTypes{$declaration_name} = $declaration_type;
944                 }
945                 $declaration_type = "";
946             } else {
947                 $declaration .= $_;
948             }
949         }
950     }
951     close (INPUT);
955 #############################################################################
956 # Function    : ReadSignalsFile
957 # Description : This reads in an existing file which contains information on
958 #               all GObject signals. It creates the arrays @SignalNames and
959 #               @SignalPrototypes containing info on the signals. The first
960 #               line of the SignalPrototype is the return type of the signal
961 #               handler. The remaining lines are the parameters passed to it.
962 #               The last parameter, "gpointer user_data" is always the same
963 #               so is not included.
964 # Arguments   : $file - the file containing the signal handler prototype
965 #                       information.
966 #############################################################################
968 sub ReadSignalsFile {
969     my ($file) = @_;
971     my $in_signal = 0;
972     my $signal_object;
973     my $signal_name;
974     my $signal_returns;
975     my $signal_flags;
976     my $signal_prototype;
978     # Reset the signal info.
979     @SignalObjects = ();
980     @SignalNames = ();
981     @SignalReturns = ();
982     @SignalFlags = ();
983     @SignalPrototypes = ();
985     if (! -f $file) {
986         return;
987     }
988     if (!open (INPUT, $file)) {
989         warn "Can't open $file - skipping signals\n";
990         return;
991     }
992     while (<INPUT>) {
993         if (!$in_signal) {
994             if (m/^<SIGNAL>/) {
995                 $in_signal = 1;
996                 $signal_object = "";
997                 $signal_name = "";
998                 $signal_returns = "";
999                 $signal_prototype = "";
1000             }
1001         } else {
1002             if (m/^<NAME>(.*)<\/NAME>/) {
1003                 $signal_name = $1;
1004                 if ($signal_name =~ m/^(.*)::(.*)$/) {
1005                     $signal_object = $1;
1006                     ($signal_name = $2) =~ s/_/-/g;
1007 #                   print "Found signal: $signal_name\n";
1008                 } else {
1009                     print "Invalid signal name: $signal_name\n";
1010                 }
1011             } elsif (m/^<RETURNS>(.*)<\/RETURNS>/) {
1012                 $signal_returns = $1;
1013             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
1014                 $signal_flags = $1;
1015             } elsif (m%^</SIGNAL>%) {
1016 #               print "Found end of signal: ${signal_object}::${signal_name}\nReturns: ${signal_returns}\n${signal_prototype}";
1017                 push (@SignalObjects, $signal_object);
1018                 push (@SignalNames, $signal_name);
1019                 push (@SignalReturns, $signal_returns);
1020                 push (@SignalFlags, $signal_flags);
1021                 push (@SignalPrototypes, $signal_prototype);
1022                 $in_signal = 0;
1023             } else {
1024                 $signal_prototype .= $_;
1025             }
1026         }
1027     }
1028     close (INPUT);
1032 #############################################################################
1033 # Function    : ReadTemplateFile
1034 # Description : This reads in the manually-edited documentation file
1035 #               corresponding to the file currently being created, so we can
1036 #               insert the documentation at the appropriate places.
1037 #               It outputs %SymbolTypes, %SymbolDocs and %SymbolParams, which
1038 #               is a hash of arrays.
1039 #               NOTE: This function is duplicated in gtkdoc-mkdb (but
1040 #               slightly different).
1041 # Arguments   : $docsfile - the template file to read in.
1042 #               $skip_unused_params - 1 if the unused parameters should be
1043 #                       skipped.
1044 #############################################################################
1046 sub ReadTemplateFile {
1047     my ($docsfile, $skip_unused_params) = @_;
1049 #    print "Reading $docsfile\n";
1050     if (! -f $docsfile) {
1051         print "File doesn't exist: $docsfile\n";
1052         return 0;
1053     }
1055     my $CurrentType = "";       # Type of symbol being read.
1056     my $CurrentSymbol = "";     # Name of symbol being read.
1057     my $SymbolDoc = "";         # Description of symbol being read.
1058     my @Params;                 # Parameter names and descriptions of current
1059                                 #   function/macro/function typedef.
1060     my $CurrentParam = -1;      # Index of parameter currently being read.
1061                                 #   Note that the param array contains pairs
1062                                 #   of param name & description.
1063     my $InUnusedParameters = 0; # True if we are reading in the unused params.
1065     open (DOCS, $docsfile)
1066         || die "Can't open file $docsfile: $!";
1067     while (<DOCS>) {
1068         if (m/^<!-- ##### ([A-Z_]+) (\S+) ##### -->/) {
1069             my $type = $1;
1070             my $symbol = $2;
1071             if ($symbol eq "Title"
1072                 || $symbol eq "Short_Description"
1073                 || $symbol eq "Long_Description"
1074                 || $symbol eq "See_Also"
1075                 || $symbol eq "Stability_Level") {
1076                 $symbol = $docsfile . ":" . $symbol;
1077             }
1079             #print "Found symbol: $symbol\n";
1080             # Remember file and line for the symbol
1081             $SymbolSourceFile{$symbol} = $docsfile;
1082             $SymbolSourceLine{$symbol} = $.;
1084             # Canonicalize signal and argument names to have -, not _
1085             if ($type eq "ARG" || $type eq "SIGNAL") {
1086               $symbol =~ s/_/-/g;
1087             }
1089             # Store previous symbol, but remove any trailing blank lines.
1090             if ($CurrentSymbol ne "") {
1091                 $SymbolDoc =~ s/\s+$//;
1092                 $SymbolTypes{$CurrentSymbol} = $CurrentType;
1093                 $SymbolDocs{$CurrentSymbol} = $SymbolDoc;
1095                 if ($CurrentParam >= 0) {
1096                     $SymbolParams{$CurrentSymbol} = [ @Params ];
1097                 } else {
1098                     # Delete any existing params in case we are overriding a
1099                     # previously read template.
1100                     delete $SymbolParams{$CurrentSymbol};
1101                 }
1102             }
1103             $CurrentType = $type;
1104             $CurrentSymbol = $symbol;
1105             $CurrentParam = -1;
1106             $InUnusedParameters = 0;
1107             $SymbolDoc = "";
1108             @Params = ();
1110         } elsif (m/^<!-- # Unused Parameters # -->/) {
1111             $InUnusedParameters = 1;
1112             next;
1114         } else {
1115             # Workaround for an old bug that left a mess in the templates.
1116             # This happened with macros with args spread over several lines.
1117             if (m/^\@\\$/) {
1118               # Skip the next line.
1119               $_ = <DOCS>;
1120               next;
1121             }
1123             # Workaround for an old bug that left '@:' at start of lines.
1124             if (m/^\@:$/) {
1125               next;
1126             }
1129             # Check if param found. Need to handle "..." and "format...".
1130             if (s/^\@([\w\.]+):\040?//) {
1131                 my $param_name = $1;
1132                 # Allow variations of 'Returns'
1133                 if ($param_name =~ m/^[Rr]eturns?$/) {
1134                     $param_name = "Returns";
1135                 }
1136 #               print "Found param: $param_name\n";
1137                 push (@Params, $param_name);
1138                 push (@Params, $_);
1139                 $CurrentParam += 2;
1140                 next;
1141             }
1143             # When outputting the DocBook we skip unused parameters.
1144             if (!$InUnusedParameters || !$skip_unused_params) {
1145                 if ($CurrentParam >= 0) {
1146                     $Params[$CurrentParam] .= $_;
1147                 } else {
1148                     $SymbolDoc .= $_;
1149                 }
1150             }
1151         }
1152     }
1154     # Remember to finish the current symbol doccs.
1155     if ($CurrentSymbol ne "") {
1157         $SymbolDoc =~ s/\s+$//;
1158         $SymbolTypes{$CurrentSymbol} = $CurrentType;
1159         $SymbolDocs{$CurrentSymbol} = $SymbolDoc;
1161         if ($CurrentParam >= 0) {
1162             $SymbolParams{$CurrentSymbol} = [ @Params ];
1163         } else {
1164             delete $SymbolParams{$CurrentSymbol};
1165         }
1166     }
1168     close (DOCS);
1169     return 1;
1173 #############################################################################
1174 # Function    : ReadObjectHierarchy
1175 # Description : This reads in the $MODULE-hierarchy.txt file containing all
1176 #               the GtkObject subclasses described in this module (and their
1177 #               ancestors).
1178 #               It places them in the @Objects array, and places their level
1179 #               in the widget hierarchy in the @ObjectLevels array, at the
1180 #               same index. GtkObject, the root object, has a level of 1.
1182 #               FIXME: the version in gtkdoc-mkdb also generates tree_index.sgml
1183 #               as it goes along, this should be split out into a separate
1184 #               function.
1186 # Arguments   : none
1187 #############################################################################
1189 sub ReadObjectHierarchy {
1190     @Objects = ();
1191     @ObjectLevels = ();
1193     if (! -f $OBJECT_TREE_FILE) {
1194         return;
1195     }
1196     if (!open (INPUT, $OBJECT_TREE_FILE)) {
1197         warn "Can't open $OBJECT_TREE_FILE - skipping object tree\n";
1198         return;
1199     }
1200     while (<INPUT>) {
1201         if (m/\S+/) {
1202             my $object = $&;
1203             my $level = (length($`)) / 2 + 1;
1204 #            print ("Level: $level  Object: $object\n");
1206             push (@Objects, $object);
1207             push (@ObjectLevels, $level);
1208         }
1209     }
1211     close (INPUT);
1215 #############################################################################
1216 # Function    : ReadArgsFile
1217 # Description : This reads in an existing file which contains information on
1218 #               all GObject args. It creates the arrays @ArgObjects, @ArgNames,
1219 #               @ArgTypes and @ArgFlags containing info on the args.
1220 # Arguments   : $file - the file containing the arg information.
1221 #############################################################################
1223 sub ReadArgsFile {
1224     my ($file) = @_;
1226     my $in_arg = 0;
1227     my $arg_object;
1228     my $arg_name;
1229     my $arg_type;
1230     my $arg_flags;
1232     # Reset the signal info.
1233     @ArgObjects = ();
1234     @ArgNames = ();
1235     @ArgTypes = ();
1236     @ArgFlags = ();
1238     if (! -f $file) {
1239         return;
1240     }
1241     if (!open (INPUT, $file)) {
1242         warn "Can't open $file - skipping args\n";
1243         return;
1244     }
1245     while (<INPUT>) {
1246         if (!$in_arg) {
1247             if (m/^<ARG>/) {
1248                 $in_arg = 1;
1249                 $arg_object = "";
1250                 $arg_name = "";
1251                 $arg_type = "";
1252                 $arg_flags = "";
1253             }
1254         } else {
1255             if (m/^<NAME>(.*)<\/NAME>/) {
1256                 $arg_name = $1;
1257                 if ($arg_name =~ m/^(.*)::(.*)$/) {
1258                     $arg_object = $1;
1259                     ($arg_name = $2) =~ s/_/-/g;
1260 #                   print "Found arg: $arg_name\n";
1261                 } else {
1262                     print "Invalid arg name: $arg_name\n";
1263                 }
1264             } elsif (m/^<TYPE>(.*)<\/TYPE>/) {
1265                 $arg_type = $1;
1266             } elsif (m/^<FLAGS>(.*)<\/FLAGS>/) {
1267                 $arg_flags = $1;
1268             } elsif (m%^</ARG>%) {
1269 #               print "Found end of arg: ${arg_object}::${arg_name}\n${arg_type} : ${arg_flags}\n";
1270                 push (@ArgObjects, $arg_object);
1271                 push (@ArgNames, $arg_name);
1272                 push (@ArgTypes, $arg_type);
1273                 push (@ArgFlags, $arg_flags);
1274                 $in_arg = 0;
1275             }
1276         }
1277     }
1278     close (INPUT);
1282 #############################################################################
1283 # Function    : CheckIsObject
1284 # Description : Returns 1 if the given name is a GObject or a subclass.
1285 #               It uses the global @Objects array.
1286 #               Note that the @Objects array only contains classes in the
1287 #               current module and their ancestors - not all GObject classes.
1288 # Arguments   : $name - the name to check.
1289 #############################################################################
1291 sub CheckIsObject {
1292     my ($name) = @_;
1294     my $object;
1295     foreach $object (@Objects) {
1296         if ($object eq $name) {
1297             return 1;
1298         }
1299     }
1300     return 0;