Tizen 2.0 Release
[external/libgnutls26.git] / doc / scripts / gdoc
1 #!/usr/bin/perl
2
3 ## Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Simon Josefsson
4 ##                    added -texinfo, -listfunc, -pkg-name
5 ##                    man page revamp
6 ##                    various improvements
7 ## Copyright (c) 2001, 2002 Nikos Mavrogiannopoulos
8 ##                    added -tex
9 ## Copyright (c) 1998 Michael Zucchi
10
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 # This will read a C source code file and scan for embedded comments
25 # in the style of gnome comments (+minor extensions - see below).
26
27 # usage:
28 # gdoc [ -docbook | -html | -text | -man | -tex | -texinfo | -listfunc ]
29 #      [ -sourceversion verno ] [ -include file | -includefuncprefix ]
30 #      [ -bugsto address ] [ -pkg-name packagename ]
31 #      [ -seeinfo infonode ] [ -copyright notice ] [ -verbatimcopying ]
32 #      [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
33 #
34 #  Set output format using one of -docbook, -html, -text, -man, -tex,
35 #  -texinfo, or -listfunc.  Default is man.
36 #
37 #  -sourceversion
38 #       Version number for source code, e.g. '1.0.4'.  Used in 'man' headers.
39 #       Defaults to using current date.
40 #
41 #  -include FILE
42 #       For man pages, mention #include <FILE.h> in the synopsis.
43 #
44 #  -includefuncprefix
45 #       For man pages, mention a #include <FILE.h> in the synopsis.
46 #       The FILE derived from the function prefix.  For example, a
47 #       function gss_init_sec_context will generate an include
48 #       statement of #include <gss.h>.
49 #
50 #  -bugsto address
51 #       For man pages, include a section about reporting bugs and mention
52 #       the given e-mail address, e.g 'bug-libidn@gnu.org'.
53 #
54 #  -pkg-name packagename
55 #       For man pages when -bugsto is used, also include help URLs to the
56 #       the project's home page.  For example, "GNU Libidn".
57 #
58 #  -seeinfo infonode
59 #       For man pages, include a section that point to an info manual
60 #       for more information.
61 #
62 #  -copyright notice
63 #       For man pages, include a copyright section with the given
64 #       notice after a preamble.  Use, e.g., '2002, 2003 Simon Josefsson'.
65 #
66 #  -verbatimcopying
67 #       For man pages, and when the -copyright parameter is used,
68 #       add a licensing statement that say verbatim copying is permitted.
69 #
70 #  -function funcname
71 #       If set, then only generate documentation for the given function(s).  All
72 #       other functions are ignored.
73 #
74 #  c files - list of 'c' files to process
75 #
76 #  All output goes to stdout, with errors to stderr.
77
78 #
79 # format of comments.
80 # In the following table, (...)? signifies optional structure.
81 #                         (...)* signifies 0 or more structure elements
82 # /**
83 #  * function_name(:)? (- short description)?
84 # (* @parameterx: (description of parameter x)?)*
85 # (* a blank line)?
86 #  * (Description:)? (Description of function)?
87 #  * (Section header: (section description)? )*
88 #  (*)?*/
89 #
90 # So .. the trivial example would be:
91 #
92 # /**
93 #  * my_function
94 #  **/
95 #
96 # If the Description: header tag is ommitted, then there must be a blank line
97 # after the last parameter specification.
98 # e.g.
99 # /**
100 #  * my_function - does my stuff
101 #  * @my_arg: its mine damnit
102 #  *
103 #  * Does my stuff explained. 
104 #  */
105 #
106 #  or, could also use:
107 # /**
108 #  * my_function - does my stuff
109 #  * @my_arg: its mine damnit
110 #  * Description: Does my stuff explained. 
111 #  */
112 # etc.
113 #
114 # All descriptions can be multiline, apart from the short function description.
115 #
116 # All descriptive text is further processed, scanning for the following special
117 # patterns, which are highlighted appropriately.
118 #
119 # 'funcname()' - function
120 # '$ENVVAR' - environmental variable OBSOLETE (?)
121 # '#struct_name' - name of a structure
122 # '@parameter' - name of a parameter
123 # '%CONST' - name of a constant.
124
125 #
126 # Extensions for LaTeX:
127 #
128 # 1. the symbol '->' will be replaced with a rightarrow
129 # 2. x^y with ${x}^{y}$.
130 # 3. xxx\: with xxx:
131
132 use POSIX qw(strftime);
133
134 # match expressions used to find embedded type information
135 $type_constant = "((?<!\")\\\%(\\w+))";
136 $type_func = "(\\w+\\(\\))";
137 $type_param = "\\\@(\\w+)";
138 $type_struct = "\\\#(\\w+)";
139 $type_env = "(\\\$\\w+)";
140
141
142 # Output conversion substitutions.
143 #  One for each output format
144
145 # these work fairly well
146 %highlights_html = ( $type_constant, "<i>\$2</i>",
147                      $type_func, "<b>\$1</b>",
148                      $type_struct, "<i>\$1</i>",
149                      $type_param, "<tt><b>\$1</b></tt>" );
150 $blankline_html = "<p>";
151
152 %highlights_texinfo = ( $type_constant, "\\\@code{\$2}",
153                         $type_func, "\\\@code{\$1}",
154                         $type_struct, "\\\@code{\$1}",
155                         $type_param, "\\\@code{\$1}" );
156 $blankline_texinfo = "";
157
158 %highlights_tex = ( $type_constant, "{\\\\it \$2}",
159                      $type_func, "{\\\\bf \$1}",
160                      $type_struct, "{\\\\it \$1}",
161                      $type_param, "{\\\\bf \$1}" );
162 $blankline_tex = "\\\\";
163
164 # sgml, docbook format
165 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$2</replaceable>",
166                      $type_func, "<function>\$1</function>",
167                      $type_struct, "<structname>\$1</structname>",
168                      $type_env, "<envar>\$1</envar>",
169                      $type_param, "<parameter>\$1</parameter>" );
170 $blankline_sgml = "</para><para>\n";
171
172 # these are pretty rough
173 %highlights_man = ( $type_constant, "\\\\fB\$2\\\\fP",
174                     $type_func, "\\\\fB\$1\\\\fP",
175                     $type_struct, "\\\\fB\$1\\\\fP",
176                     $type_param, "\\\\fI\$1\\\\fP" );
177 $blankline_man = "";
178
179 # text-mode
180 %highlights_text = ( $type_constant, "\$2",
181                      $type_func, "\$1",
182                      $type_struct, "\$1",
183                      $type_param, "\$1" );
184 $blankline_text = "";
185
186
187 sub usage {
188     print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -tex | -texinfo  -listfunc ]\n";
189     print "         [ -sourceversion verno ] [ -include file | -includefuncprefix ]\n";
190     print "         [ -bugsto address ] [ -seeinfo infonode ] [ -copyright notice]\n";
191     print "         [ -verbatimcopying ] [ -pkg-name packagename ]\n";
192     print "         [ -function funcname [ -function funcname ...] ]\n";
193     print "         c source file(s) > outputfile\n";
194     exit 1;
195 }
196
197 # read arguments
198 if ($#ARGV==-1) {
199     usage();
200 }
201
202 $verbose = 0;
203 $output_mode = "man";
204 %highlights = %highlights_man;
205 $blankline = $blankline_man;
206 $modulename = "API Documentation";
207 $sourceversion = strftime "%Y-%m-%d", localtime;
208 $function_only = 0;
209 while ($ARGV[0] =~ m/^-(.*)/) {
210     $cmd = shift @ARGV;
211     if ($cmd eq "-html") {
212         $output_mode = "html";
213         %highlights = %highlights_html;
214         $blankline = $blankline_html;
215     } elsif ($cmd eq "-man") {
216         $output_mode = "man";
217         %highlights = %highlights_man;
218         $blankline = $blankline_man;
219     } elsif ($cmd eq "-tex") {
220         $output_mode = "tex";
221         %highlights = %highlights_tex;
222         $blankline = $blankline_tex;
223     } elsif ($cmd eq "-texinfo") {
224         $output_mode = "texinfo";
225         %highlights = %highlights_texinfo;
226         $blankline = $blankline_texinfo;
227     } elsif ($cmd eq "-text") {
228         $output_mode = "text";
229         %highlights = %highlights_text;
230         $blankline = $blankline_text;
231     } elsif ($cmd eq "-docbook") {
232         $output_mode = "sgml";
233         %highlights = %highlights_sgml;
234         $blankline = $blankline_sgml;
235     } elsif ($cmd eq "-listfunc") {
236         $output_mode = "listfunc";
237     } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
238         $modulename = shift @ARGV;
239     } elsif ($cmd eq "-sourceversion") {
240         $sourceversion = shift @ARGV;
241     } elsif ($cmd eq "-include") {
242         $include = shift @ARGV;
243     } elsif ($cmd eq "-includefuncprefix") {
244         $includefuncprefix = 1;
245     } elsif ($cmd eq "-bugsto") {
246         $bugsto = shift @ARGV;
247     } elsif ($cmd eq "-pkg-name") {
248         $pkgname = shift @ARGV;
249     } elsif ($cmd eq "-copyright") {
250         $copyright = shift @ARGV;
251     } elsif ($cmd eq "-verbatimcopying") {
252         $verbatimcopying = 1;
253     } elsif ($cmd eq "-seeinfo") {
254         $seeinfo = shift @ARGV;
255     } elsif ($cmd eq "-function") { # to only output specific functions
256         $function_only = 1;
257         $function = shift @ARGV;
258         $function_table{$function} = 1;
259     } elsif ($cmd eq "-v") {
260         $verbose = 1;
261     } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
262         usage();
263     }
264 }
265
266 ##
267 # dumps section contents to arrays/hashes intended for that purpose.
268 #
269 sub dump_section {
270     my $name = shift @_;
271     my $contents = join "\n", @_;
272
273     if ($name =~ m/$type_constant/) {
274         $name = $1;
275 #       print STDERR "constant section '$1' = '$contents'\n";
276         $constants{$name} = $contents;
277     } elsif ($name =~ m/$type_param/) {
278 #       print STDERR "parameter def '$1' = '$contents'\n";
279         $name = $1;
280         $parameters{$name} = $contents;
281     } else {
282 #       print STDERR "other section '$name' = '$contents'\n";
283         $sections{$name} = $contents;
284         push @sectionlist, $name;
285     }
286 }
287
288 ##
289 # output function
290 #
291 # parameters, a hash.
292 #  function => "function name"
293 #  parameterlist => @list of parameters
294 #  parameters => %parameter descriptions
295 #  sectionlist => @list of sections
296 #  sections => %descriont descriptions
297 #  
298
299 sub repstr {
300     $pattern = shift;
301     $repl = shift;
302     $match1 = shift;
303     $match2 = shift;
304     $match3 = shift;
305     $match4 = shift;
306
307     $output = $repl;
308     $output =~ s,\$1,$match1,g;
309     $output =~ s,\$2,$match2,g;
310     $output =~ s,\$3,$match3,g;
311     $output =~ s,\$4,$match4,g;
312
313     eval "\$return = qq/$output/";
314
315 #    print "pattern $pattern matched 1=$match1 2=$match2 3=$match3 4=$match4 replace $repl yielded $output interpolated $return\n";
316
317     $return;
318 }
319
320 sub just_highlight {
321     my $contents = join "\n", @_;
322     my $line;
323     my $ret = "";
324
325     foreach $pattern (keys %highlights) {
326 #       print "scanning pattern $pattern ($highlights{$pattern})\n";
327         $contents =~ s:$pattern:repstr($pattern, $highlights{$pattern}, $1, $2, $3, $4):gse;
328     }
329     foreach $line (split "\n", $contents) {
330         if ($line eq ""){
331             $ret = $ret . $lineprefix . $blankline;
332         } else {
333             $ret = $ret . $lineprefix . $line;
334         }
335         $ret = $ret . "\n";
336     }
337
338     return $ret;
339 }
340
341 sub output_highlight {
342     print (just_highlight (@_));
343 }
344
345 # output in texinfo
346 sub output_texinfo {
347     my %args = %{$_[0]};
348     my ($parameter, $section);
349     my $count;
350
351     print "\@subheading ".$args{'function'}."\n";
352     print "\@anchor{".$args{'function'}."}\n";
353     print "\@deftypefun {" . $args{'functiontype'} . "} ";
354     print "{".$args{'function'}."} ";
355     print "(";
356     $count = 0;
357     foreach $parameter (@{$args{'parameterlist'}}) {
358         print $args{'parametertypes'}{$parameter}." \@var{".$parameter."}";
359         if ($count != $#{$args{'parameterlist'}}) {
360             $count++;
361             print ", ";
362         }
363     }
364     print ")\n";
365     foreach $parameter (@{$args{'parameterlist'}}) {
366         if ($args{'parameters'}{$parameter}) {
367             print "\@var{".$parameter."}: ";
368             output_highlight($args{'parameters'}{$parameter});
369             print "\n";
370         }
371     }
372     foreach $section (@{$args{'sectionlist'}}) {
373         print "\n\@strong{$section:} " if $section ne $section_default;
374         $args{'sections'}{$section} =~ s:([{}]):\@\1:gs;
375         output_highlight($args{'sections'}{$section});
376     }
377     print "\@end deftypefun\n\n";
378 }
379
380 # output in html
381 sub output_html {
382     my %args = %{$_[0]};
383     my ($parameter, $section);
384     my $count;
385     print "\n\n<a name=\"". $args{'function'} . "\">&nbsp</a><h2>Function</h2>\n";
386
387     print "<i>".$args{'functiontype'}."</i>\n";
388     print "<b>".$args{'function'}."</b>\n";
389     print "(";
390     $count = 0;
391     foreach $parameter (@{$args{'parameterlist'}}) {
392         print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
393         if ($count != $#{$args{'parameterlist'}}) {
394             $count++;
395             print ", ";
396         }
397     }
398     print ")\n";
399
400     print "<h3>Arguments</h3>\n";
401     print "<dl>\n";
402     foreach $parameter (@{$args{'parameterlist'}}) {
403         print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
404         print "<dd>";
405         output_highlight($args{'parameters'}{$parameter});
406     }
407     print "</dl>\n";
408     foreach $section (@{$args{'sectionlist'}}) {
409         print "<h3>$section</h3>\n";
410         print "<ul>\n";
411         output_highlight($args{'sections'}{$section});
412         print "</ul>\n";
413     }
414     print "<hr>\n";
415 }
416
417 # output in tex
418 sub output_tex {
419     my %args = %{$_[0]};
420     my ($parameter, $section);
421     my $count;
422     my $func = $args{'function'};
423     my $param;
424     my $param2;
425     my $sec;
426     my $check;
427     my $type;
428
429     $func =~ s/_/\\_/g;
430
431     print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
432
433     $type = $args{'functiontype'};
434     $type =~ s/_/\\_/g;
435
436     print "{\\it ".$type."}\n";
437     print "{\\bf ".$func."}\n";
438     print "(";
439     $count = 0;
440     foreach $parameter (@{$args{'parameterlist'}}) {
441         $param = $args{'parametertypes'}{$parameter};
442         $param2 = $parameter;
443         $param =~ s/_/\\_/g;
444         $param2 =~ s/_/\\_/g;
445
446         print "{\\it ".$param."} {\\bf ".$param2."}";
447         if ($count != $#{$args{'parameterlist'}}) {
448             $count++;
449             print ", ";
450         }
451     }
452     print ")\n";
453
454     print "\n{\\large{Arguments}}\n";
455
456     print "\\begin{itemize}\n";
457     $check=0;
458     foreach $parameter (@{$args{'parameterlist'}}) {
459         $param1 = $args{'parametertypes'}{$parameter};
460         $param1 =~ s/_/\\_/g;
461         $param2 = $parameter;
462         $param2 =~ s/_/\\_/g;
463
464         $check = 1;
465         print "\\item {\\it ".$param1."} {\\bf ".$param2."}: \n";
466 #       print "\n";
467
468         $param3 = $args{'parameters'}{$parameter};
469         $param3 =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
470
471         $out = just_highlight($param3);
472         $out =~ s/_/\\_/g;
473         print $out;
474     }
475     if ($check==0) {
476         print "\\item void\n";
477     }
478     print "\\end{itemize}\n";
479
480     foreach $section (@{$args{'sectionlist'}}) {
481         $sec = $section;
482         $sec =~ s/_/\\_/g;
483         $sec =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
484
485         print "\n{\\large{$sec}}\\\\\n";
486         print "\\begin{rmfamily}\n";
487
488         $sec = $args{'sections'}{$section};
489         $sec =~ s/\\:/:/g;
490         $sec =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
491         $sec =~ s/->/\$\\rightarrow\$/g;
492         $sec =~ s/([0-9]+)\^([0-9]+)/\$\{\1\}\^\{\2\}\$/g;
493
494         $out = just_highlight($sec);
495         $out =~ s/_/\\_/g;
496
497         print $out;
498         print "\\end{rmfamily}\n";
499     }
500     print "\n";
501 }
502
503
504 # output in sgml DocBook
505 sub output_sgml {
506     my %args = %{$_[0]};
507     my ($parameter, $section);
508     my $count;
509     my $id;
510
511     $id = $args{'module'}."-".$args{'function'};
512     $id =~ s/[^A-Za-z0-9]/-/g;
513
514     print "<refentry>\n";
515     print "<refmeta>\n";
516     print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
517     print "</refmeta>\n";
518     print "<refnamediv>\n";
519     print " <refname>".$args{'function'}."</refname>\n";
520     print " <refpurpose>\n";
521     print "  ".$args{'purpose'}."\n";
522     print " </refpurpose>\n";
523     print "</refnamediv>\n";
524
525     print "<refsynopsisdiv>\n";
526     print " <title>Synopsis</title>\n";
527     print "  <funcsynopsis>\n";
528     print "   <funcdef>".$args{'functiontype'}." ";
529     print "<function>".$args{'function'}." ";
530     print "</function></funcdef>\n";
531
532 #    print "<refsect1>\n";
533 #    print " <title>Synopsis</title>\n";
534 #    print "  <funcsynopsis>\n";
535 #    print "   <funcdef>".$args{'functiontype'}." ";
536 #    print "<function>".$args{'function'}." ";
537 #    print "</function></funcdef>\n";
538
539     $count = 0;
540     if ($#{$args{'parameterlist'}} >= 0) {
541         foreach $parameter (@{$args{'parameterlist'}}) {
542             print "   <paramdef>".$args{'parametertypes'}{$parameter};
543             print " <parameter>$parameter</parameter></paramdef>\n";
544         }
545     } else {
546         print "  <void>\n";
547     }
548     print "  </funcsynopsis>\n";
549     print "</refsynopsisdiv>\n";
550 #    print "</refsect1>\n";
551
552     # print parameters
553     print "<refsect1>\n <title>Arguments</title>\n";
554 #    print "<para>\nArguments\n";
555     if ($#{$args{'parameterlist'}} >= 0) {
556         print " <variablelist>\n";
557         foreach $parameter (@{$args{'parameterlist'}}) {
558             print "  <varlistentry>\n   <term><parameter>$parameter</parameter></term>\n";
559             print "   <listitem>\n    <para>\n";
560             $lineprefix="     ";
561             output_highlight($args{'parameters'}{$parameter});
562             print "    </para>\n   </listitem>\n  </varlistentry>\n";
563         }
564         print " </variablelist>\n";
565     } else {
566         print " <para>\n  None\n </para>\n";
567     }
568     print "</refsect1>\n";
569
570     # print out each section
571     $lineprefix="   ";
572     foreach $section (@{$args{'sectionlist'}}) {
573         print "<refsect1>\n <title>$section</title>\n <para>\n";
574 #       print "<para>\n$section\n";
575         if ($section =~ m/EXAMPLE/i) {
576             print "<example><para>\n";
577         }
578         output_highlight($args{'sections'}{$section});
579 #       print "</para>";
580         if ($section =~ m/EXAMPLE/i) {
581             print "</para></example>\n";
582         }
583         print " </para>\n</refsect1>\n";
584     }
585
586     print "\n\n";
587 }
588
589 ##
590 # output in man
591 sub output_man {
592     my %args = %{$_[0]};
593     my ($parameter, $section);
594     my $count;
595
596     print ".\\\" DO NOT MODIFY THIS FILE!  It was generated by gdoc.\n";
597     print ".TH \"$args{'function'}\" 3 \"$args{'sourceversion'}\" \"". $args{'module'} . "\" \"". $args{'module'} . "\"\n";
598
599     print ".SH NAME\n";
600
601     print $args{'function'};
602     if ($args{'purpose'}) {
603         print " \\- " . $args{'purpose'} . "\n";
604     } else {
605         print " \\- API function\n";
606     }
607
608     print ".SH SYNOPSIS\n";
609     print ".B #include <". $args{'include'} . ">\n"
610         if $args{'include'};
611     print ".B #include <". lc((split /_/, $args{'function'})[0]) . ".h>\n"
612         if $args{'includefuncprefix'};
613     print ".sp\n";
614     print ".BI \"".$args{'functiontype'}." ".$args{'function'}."(";
615     $count = 0;
616     foreach $parameter (@{$args{'parameterlist'}}) {
617         print $args{'parametertypes'}{$parameter}." \" ".$parameter." \"";
618         if ($count != $#{$args{'parameterlist'}}) {
619             $count++;
620             print ", ";
621         }
622     }
623     print ");\"\n";
624
625     print ".SH ARGUMENTS\n";
626     foreach $parameter (@{$args{'parameterlist'}}) {
627         print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
628         $param = $args{'parameters'}{$parameter};
629         $param =~ s/-/\\-/g;
630         output_highlight($param);
631     }
632     foreach $section (@{$args{'sectionlist'}}) {
633         print ".SH \"" . uc($section) . "\"\n";
634         $sec = $args{'sections'}{$section};
635         $sec =~ s/-/\\-/g;
636         output_highlight($sec);
637     }
638
639     if ($args{'bugsto'}) {
640         print ".SH \"REPORTING BUGS\"\n";
641         print "Report bugs to <". $args{'bugsto'} . ">.\n";
642         if ($args{'pkgname'}) {
643             print $args{'pkgname'} . " home page: " .
644                 "http://www.gnu.org/software/" . $args{'module'} . "/\n";
645         }
646         print "General help using GNU software: http://www.gnu.org/gethelp/\n";
647     }
648
649     if ($args{'copyright'}) {
650         print ".SH COPYRIGHT\n";
651         print "Copyright \\(co ". $args{'copyright'} . ".\n";
652         if ($args{'verbatimcopying'}) {
653             print ".br\n";
654             print "Copying and distribution of this file, with or without modification,\n";
655             print "are permitted in any medium without royalty provided the copyright\n";
656             print "notice and this notice are preserved.\n";
657         }
658     }
659
660     if ($args{'seeinfo'}) {
661         print ".SH \"SEE ALSO\"\n";
662         print "The full documentation for\n";
663         print ".B " . $args{'module'} . "\n";
664         print "is maintained as a Texinfo manual.  If the\n";
665         print ".B info\n";
666         print "and\n";
667         print ".B " . $args{'module'} . "\n";
668         print "programs are properly installed at your site, the command\n";
669         print ".IP\n";
670         print ".B info " . $args{'seeinfo'} . "\n";
671         print ".PP\n";
672         print "should give you access to the complete manual.\n";
673     }
674 }
675
676 sub output_listfunc {
677     my %args = %{$_[0]};
678     print $args{'function'} . "\n";
679 }
680
681 ##
682 # output in text
683 sub output_text {
684     my %args = %{$_[0]};
685     my ($parameter, $section);
686
687     print "Function = ".$args{'function'}."\n";
688     print "  return type: ".$args{'functiontype'}."\n\n";
689     foreach $parameter (@{$args{'parameterlist'}}) {
690         print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
691         print "    -> ".$args{'parameters'}{$parameter}."\n";
692     }
693     foreach $section (@{$args{'sectionlist'}}) {
694         print " $section:\n";
695         print "    -> ";
696         output_highlight($args{'sections'}{$section});
697     }
698 }
699
700 ##
701 # generic output function - calls the right one based
702 # on current output mode.
703 sub output_function {
704 #    output_html(@_);
705     eval "output_".$output_mode."(\@_);";
706 }
707
708
709 ##
710 # takes a function prototype and spits out all the details
711 # stored in the global arrays/hsahes.
712 sub dump_function {
713     my $prototype = shift @_;
714
715     if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
716         $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
717         $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
718         $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
719         $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/)  {
720         $return_type = $1;
721         $function_name = $2;
722         $args = $3;
723
724 #       print STDERR "ARGS = '$args'\n";
725
726         foreach $arg (split ',', $args) {
727             # strip leading/trailing spaces
728             $arg =~ s/^\s*//;
729             $arg =~ s/\s*$//;
730 #           print STDERR "SCAN ARG: '$arg'\n";
731             @args = split('\s', $arg);
732
733 #           print STDERR " -> @args\n";
734             $param = pop @args;
735 #           print STDERR " -> @args\n";
736             if ($param =~ m/^(\*+)(.*)/) {
737                 $param = $2;
738                 push @args, $1;
739             }
740             if ($param =~ m/^(.*)(\[\])$/) {
741                 $param = $1;
742                 push @args, $2;
743             }
744 #           print STDERR " :> @args\n";
745             $type = join " ", @args;
746
747             if ($parameters{$param} eq "" && $param != "void") {
748                 $parameters{$param} = "-- undescribed --";
749                 print STDERR "warning: $lineno: Function parameter '$param' not described in '$function_name'\n";
750             }
751
752             push @parameterlist, $param;
753             $parametertypes{$param} = $type;
754
755 #           print STDERR "param = '$param', type = '$type'\n";
756         }
757     } else {
758         print STDERR "warning: $lineno: Cannot understand prototype: '$prototype'\n";
759         return;
760     }
761
762     if ($function_only==0 || defined($function_table{$function_name})) {
763         output_function({'function' => $function_name,
764                          'module' => $modulename,
765                          'sourceversion' => $sourceversion,
766                          'include' => $include,
767                          'includefuncprefix' => $includefuncprefix,
768                          'bugsto' => $bugsto,
769                          'pkgname' => $pkgname,
770                          'copyright' => $copyright,
771                          'verbatimcopying' => $verbatimcopying,
772                          'seeinfo' => $seeinfo,
773                          'functiontype' => $return_type,
774                          'parameterlist' => \@parameterlist,
775                          'parameters' => \%parameters,
776                          'parametertypes' => \%parametertypes,
777                          'sectionlist' => \@sectionlist,
778                          'sections' => \%sections,
779                          'purpose' => $function_purpose
780                          });
781     }
782 }
783
784 ######################################################################
785 # main
786 # states
787 # 0 - normal code
788 # 1 - looking for function name
789 # 2 - scanning field start.
790 # 3 - scanning prototype.
791 $state = 0;
792 $section = "";
793
794 $doc_special = "\@\%\$\#";
795
796 $doc_start = "^/\\*\\*\$";
797 $doc_end = "\\*/";
798 $doc_com = "\\s*\\*\\s*";
799 $doc_func = $doc_com."(\\w+):?";
800 $doc_sect = $doc_com."([".$doc_special."[:upper:]][\\w ]+):\\s*(.*)";
801 $doc_content = $doc_com."(.*)";
802
803 %constants = ();
804 %parameters = ();
805 @parameterlist = ();
806 %sections = ();
807 @sectionlist = ();
808
809 $contents = "";
810 $section_default = "Description";       # default section
811 $section = $section_default;
812
813 $lineno = 0;
814 foreach $file (@ARGV) {
815     if (!open(IN,"<$file")) {
816         print STDERR "Error: Cannot open file $file\n";
817         next;
818     }
819     while (<IN>) {
820         $lineno++;
821
822         if ($state == 0) {
823             if (/$doc_start/o) {
824                 $state = 1;             # next line is always the function name
825             }
826         } elsif ($state == 1) { # this line is the function name (always)
827             if (/$doc_func/o) {
828                 $function = $1;
829                 $state = 2;
830                 if (/-\s*(.*)/) {
831                     $function_purpose = $1;
832                 } else {
833                     $function_purpose = "";
834                 }
835                 if ($verbose) {
836                     print STDERR "Info($lineno): Scanning doc for $function\n";
837                 }
838             } else {
839                 print STDERR "warning: $lineno: Cannot understand $_ on line $lineno",
840                 " - I thought it was a doc line\n";
841                 $state = 0;
842             }
843         } elsif ($state == 2) { # look for head: lines, and include content
844             if (/$doc_sect/o) {
845                 $newsection = $1;
846                 $newcontents = $2;
847
848                 if ($contents ne "") {
849                     dump_section($section, $contents);
850                     $section = $section_default;
851                 }
852
853                 $contents = $newcontents;
854                 if ($contents ne "") {
855                     $contents .= "\n";
856                 }
857                 $section = $newsection;
858             } elsif (/$doc_end/) {
859
860                 if ($contents ne "") {
861                     dump_section($section, $contents);
862                     $section = $section_default;
863                     $contents = "";
864                 }
865
866 #           print STDERR "end of doc comment, looking for prototype\n";
867                 $prototype = "";
868                 $state = 3;
869             } elsif (/$doc_content/) {
870                 # miguel-style comment kludge, look for blank lines after
871                 # @parameter line to signify start of description
872                 if ($1 eq "" && $section =~ m/^@/) {
873                     dump_section($section, $contents);
874                     $section = $section_default;
875                     $contents = "";
876                 } else {
877                     $contents .= $1."\n";
878                 }
879             } else {
880                 # i dont know - bad line?  ignore.
881                 print STDERR "warning: $lineno: Bad line: $_";
882             }
883         } elsif ($state == 3) { # scanning for function { (end of prototype)
884             if (m#\s*/\*\s+MACDOC\s*#io) {
885               # do nothing
886             }
887             elsif (/([^\{]*)/) {
888                 $prototype .= $1;
889             }
890             if (/\{/) {
891                 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
892                 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
893                 $prototype =~ s@^ +@@gos; # strip leading spaces
894                 dump_function($prototype);
895
896                 $function = "";
897                 %constants = ();
898                 %parameters = ();
899                 %parametertypes = ();
900                 @parameterlist = ();
901                 %sections = ();
902                 @sectionlist = ();
903                 $prototype = "";
904
905                 $state = 0;
906             }
907         }
908     }
909 }