3 ## Copyright (c) 2003 Simon Josefsson ##
4 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
5 ## hacked to allow -tex option --nmav ##
6 ## hacked to allow -texinfo option --jas ##
8 ## This software falls under the GNU Public License. Please read ##
9 ## the COPYING file for more information ##
12 # This will read a 'c' file and scan for embedded comments in the
13 # style of gnome comments (+minor extensions - see below).
15 # This program is modified by Nikos Mavroyanopoulos, for the gnutls
18 # This program is modified by Simon Josefsson, for the Libidn and
21 # Note: This only supports 'c'.
24 # gdoc [ -docbook | -html | -text | -man | -tex | -texinfo | -listfunc ]
25 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
27 # Set output format using one of -docbook -html -text -man -tex or
28 # -texinfo. Default is man.
31 # If set, then only generate documentation for the given
32 # function(s). All other functions are ignored.
34 # c files - list of 'c' files to process
36 # All output goes to stdout, with errors to stderr.
40 # In the following table, (...)? signifies optional structure.
41 # (...)* signifies 0 or more structure elements
43 # * function_name(:)? (- short description)?
44 # (* @parameterx: (description of parameter x)?)*
46 # * (Description:)? (Description of function)?
47 # * (section header: (section description)? )*
50 # So .. the trivial example would be:
56 # If the Description: header tag is ommitted, then there must be a blank line
57 # after the last parameter specification.
60 # * my_function - does my stuff
61 # * @my_arg: its mine damnit
63 # * Does my stuff explained.
68 # * my_function - does my stuff
69 # * @my_arg: its mine damnit
70 # * Description: Does my stuff explained.
74 # All descriptions can be multiline, apart from the short function description.
76 # All descriptive text is further processed, scanning for the following special
77 # patterns, which are highlighted appropriately.
79 # 'funcname()' - function
80 # '$ENVVAR' - environmental variable
81 # '&struct_name' - name of a structure
82 # '@parameter' - name of a parameter
83 # '%CONST' - name of a constant.
86 # Extensions for LaTeX:
88 # 1. the symbol '->' will be replaced with a rightarrow
89 # 2. x^y with ${x}^{y}$.
92 use POSIX
qw(strftime);
94 # match expressions used to find embedded type information
95 $type_constant = "\\\%(\\w+)";
96 $type_func = "(\\w+\\(\\))";
97 #$type_func = "(\\(w||\\\\)+\\(\\))";
98 $type_param = "\\\@(\\w+)";
99 $type_struct = "\\\&(\\w+)";
100 $type_env = "(\\\$\\w+)";
103 # Output conversion substitutions.
104 # One for each output format
106 # these work fairly well
107 %highlights_html = ( $type_constant, "<i>\$1</i>",
108 $type_func, "<b>\$1</b>",
109 $type_struct, "<i>\$1</i>",
110 $type_param, "<tt><b>\$1</b></tt>" );
111 $blankline_html = "<p>";
114 %highlights_texinfo = ( $type_constant, "CMDvar{\$1}",
115 $type_func, "CMDcode{\$1}",
116 $type_struct, "CMDcode{\$1}",
117 $type_param, "CMDcode\{\$1\}" );
118 $blankline_texinfo = "";
120 %highlights_tex = ( $type_constant, "{\\\\it \$1}",
121 $type_func, "{\\\\bf \$1}",
122 $type_struct, "{\\\\it \$1}",
123 $type_param, "{\\\\bf \$1}" );
124 $blankline_tex = "\\par";
126 # sgml, docbook format
127 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
128 $type_func, "<function>\$1</function>",
129 $type_struct, "<structname>\$1</structname>",
130 $type_env, "<envar>\$1</envar>",
131 $type_param, "<parameter>\$1</parameter>" );
132 $blankline_sgml = "</para><para>\n";
134 # these are pretty rough
135 %highlights_man = ( $type_constant, "\\n.I \\\"\$1\\\"\\n",
136 $type_func, "\\n.B \\\"\$1\\\"\\n",
137 $type_struct, "\\n.I \\\"\$1\\\"\\n",
138 $type_param."([\.\, ]*)\n?", "\\n.I \\\"\$1\$2\\\"\\n" );
142 %highlights_text = ( $type_constant, "\$1",
145 $type_param, "\$1" );
146 $blankline_text = "";
150 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man |\n";
151 print " -tex | -texinfo | -listfunc ]\n";
152 print " [ -function funcname [ -function funcname ...] ]\n";
153 print " c source file(s) > outputfile\n";
163 $output_mode = "man";
164 %highlights = %highlights_man;
165 $blankline = $blankline_man;
166 $modulename = "API Documentation";
167 $version = strftime
"%Y-%m-%d", localtime;
169 while ($ARGV[0] =~ m/^-(.*)/) {
171 if ($cmd eq "-html") {
172 $output_mode = "html";
173 %highlights = %highlights_html;
174 $blankline = $blankline_html;
175 } elsif ($cmd eq "-man") {
176 $output_mode = "man";
177 %highlights = %highlights_man;
178 $blankline = $blankline_man;
179 } elsif ($cmd eq "-tex") {
180 $output_mode = "tex";
181 %highlights = %highlights_tex;
182 $blankline = $blankline_tex;
183 } elsif ($cmd eq "-texinfo") {
184 $output_mode = "texinfo";
185 %highlights = %highlights_texinfo;
186 $blankline = $blankline_texinfo;
187 } elsif ($cmd eq "-text") {
188 $output_mode = "text";
189 %highlights = %highlights_text;
190 $blankline = $blankline_text;
191 } elsif ($cmd eq "-docbook") {
192 $output_mode = "sgml";
193 %highlights = %highlights_sgml;
194 $blankline = $blankline_sgml;
195 } elsif ($cmd eq "-listfunc") {
196 $output_mode = "listfunc";
197 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
198 $modulename = shift @ARGV;
199 } elsif ($cmd eq "-version") {
200 $version = shift @ARGV;
201 } elsif ($cmd eq "-function") { # to only output specific functions
203 $function = shift @ARGV;
204 $function_table{$function} = 1;
205 } elsif ($cmd eq "-v") {
207 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
213 # generate a sequence of code that will splice in highlighting information
214 # using the s// operator.
216 foreach $pattern (keys %highlights) {
217 # print STDERR "scanning pattern $pattern ($highlights{$pattern})\n";
218 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
222 # dumps section contents to arrays/hashes intended for that purpose.
226 my $contents = join "\n", @_;
228 if ($name =~ m/$type_constant/) {
230 # print STDERR "constant section '$1' = '$contents'\n";
231 $constants{$name} = $contents;
232 } elsif ($name =~ m/$type_param/) {
233 # print STDERR "parameter def '$1' = '$contents'\n";
235 $parameters{$name} = $contents;
237 # print STDERR "other section '$name' = '$contents'\n";
238 $sections{$name} = $contents;
239 push @sectionlist, $name;
246 # parameters, a hash.
247 # function => "function name"
248 # parameterlist => @list of parameters
249 # parameters => %parameter descriptions
250 # sectionlist => @list of sections
251 # sections => %descriont descriptions
254 sub output_highlight
{
255 my $contents = join "\n", @_;
258 if (substr($contents, 0, 1) eq " ") {
259 $contents = substr($contents, 1);
262 $contents =~ s
:{:\@
{:gs
if ($output_mode eq "texinfo");
263 $contents =~ s
:}:\@
}:gs
if ($output_mode eq "texinfo");
265 $contents =~ s
:CMD
:@
:gs
if ($output_mode eq "texinfo");
266 foreach $line (split "\n", $contents) {
268 print $lineprefix, $blankline;
270 print $lineprefix, $line;
279 my ($parameter, $section);
282 print "\@deftypefun {";
283 print $args{'functiontype'};
284 print "} ".$args{'function'}." ";
287 foreach $parameter (@
{$args{'parameterlist'}}) {
288 print $args{'parametertypes'}{$parameter}." \@var{".$parameter."}";
289 if ($count != $#{$args{'parameterlist'}}) {
295 foreach $parameter (@
{$args{'parameterlist'}}) {
296 if ($args{'parameters'}{$parameter}) {
297 print "\@var{".$parameter."}: ";
298 output_highlight
($args{'parameters'}{$parameter});
302 foreach $section (@
{$args{'sectionlist'}}) {
303 output_highlight
($args{'sections'}{$section});
306 print "\@end deftypefun\n\n";
312 my ($parameter, $section);
314 print "\n\n<a name=\"". $args{'function'} . "\"> </a><h2>Function</h2>\n";
316 print "<i>".$args{'functiontype'}."</i>\n";
317 print "<b>".$args{'function'}."</b>\n";
320 foreach $parameter (@
{$args{'parameterlist'}}) {
321 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
322 if ($count != $#{$args{'parameterlist'}}) {
329 print "<h3>Arguments</h3>\n";
331 foreach $parameter (@
{$args{'parameterlist'}}) {
332 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
334 output_highlight
($args{'parameters'}{$parameter});
337 foreach $section (@
{$args{'sectionlist'}}) {
338 print "<h3>$section</h3>\n";
340 output_highlight
($args{'sections'}{$section});
349 my ($parameter, $section);
351 my $func = $args{'function'};
360 print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
362 $type = $args{'functiontype'};
365 print "{\\it ".$type."}\n";
366 print "{\\bf ".$func."}\n";
369 foreach $parameter (@
{$args{'parameterlist'}}) {
370 $param = $args{'parametertypes'}{$parameter};
371 $param2 = $parameter;
373 $param2 =~ s/_/\\_/g;
375 print "{\\it ".$param."} {\\bf ".$param2."}\n";
376 if ($count != $#{$args{'parameterlist'}}) {
383 print "\n{\\large{Arguments}}\n";
385 print "\\begin{itemize}\n";
387 foreach $parameter (@
{$args{'parameterlist'}}) {
388 $param1 = $args{'parametertypes'}{$parameter};
389 $param1 =~ s/_/\\_/g;
390 $param2 = $parameter;
391 $param2 =~ s/_/\\_/g;
394 print "\\item {\\it ".$param1."} {\\bf ".$param2."}: \n";
397 $param3 = $args{'parameters'}{$parameter};
398 $param3 =~ s/_/\\_/g;
399 $param3 =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
401 output_highlight
($param3);
404 print "\\item void\n";
406 print "\\end{itemize}\n";
408 foreach $section (@
{$args{'sectionlist'}}) {
411 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
413 print "\n\\par{\\large{$sec}}\\par\n";
414 print "\\begin{rmfamily}\n";
416 $sec = $args{'sections'}{$section};
419 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
420 $sec =~ s/->/\$\\rightarrow\$/g;
421 $sec =~ s/([0-9]+)\^([0-9]+)/\$\{\1\}\^\{\2\}\$/g;
423 output_highlight
($sec);
424 print "\\end{rmfamily}\n";
430 # output in sgml DocBook
433 my ($parameter, $section);
437 $id = $args{'module'}."-".$args{'function'};
438 $id =~ s/[^A-Za-z0-9]/-/g;
440 print "<refentry>\n";
442 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
443 print "</refmeta>\n";
444 print "<refnamediv>\n";
445 print " <refname>".$args{'function'}."</refname>\n";
446 print " <refpurpose>\n";
447 print " ".$args{'purpose'}."\n";
448 print " </refpurpose>\n";
449 print "</refnamediv>\n";
451 print "<refsynopsisdiv>\n";
452 print " <title>Synopsis</title>\n";
453 print " <funcsynopsis>\n";
454 print " <funcdef>".$args{'functiontype'}." ";
455 print "<function>".$args{'function'}." ";
456 print "</function></funcdef>\n";
458 # print "<refsect1>\n";
459 # print " <title>Synopsis</title>\n";
460 # print " <funcsynopsis>\n";
461 # print " <funcdef>".$args{'functiontype'}." ";
462 # print "<function>".$args{'function'}." ";
463 # print "</function></funcdef>\n";
466 if ($#{$args{'parameterlist'}} >= 0) {
467 foreach $parameter (@
{$args{'parameterlist'}}) {
468 print " <paramdef>".$args{'parametertypes'}{$parameter};
469 print " <parameter>$parameter</parameter></paramdef>\n";
474 print " </funcsynopsis>\n";
475 print "</refsynopsisdiv>\n";
476 # print "</refsect1>\n";
479 print "<refsect1>\n <title>Arguments</title>\n";
480 # print "<para>\nArguments\n";
481 if ($#{$args{'parameterlist'}} >= 0) {
482 print " <variablelist>\n";
483 foreach $parameter (@
{$args{'parameterlist'}}) {
484 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
485 print " <listitem>\n <para>\n";
487 output_highlight
($args{'parameters'}{$parameter});
488 print " </para>\n </listitem>\n </varlistentry>\n";
490 print " </variablelist>\n";
492 print " <para>\n None\n </para>\n";
494 print "</refsect1>\n";
496 # print out each section
498 foreach $section (@
{$args{'sectionlist'}}) {
499 print "<refsect1>\n <title>$section</title>\n <para>\n";
500 # print "<para>\n$section\n";
501 if ($section =~ m/EXAMPLE/i) {
502 print "<example><para>\n";
504 output_highlight
($args{'sections'}{$section});
506 if ($section =~ m/EXAMPLE/i) {
507 print "</para></example>\n";
509 print " </para>\n</refsect1>\n";
519 my ($parameter, $section);
522 print ".TH \"$args{'function'}\" 3 \"$args{'version'}\" \"GNU\" \"". uc($args{'module'}) . "\"\n";
526 print $args{'function'}."\n";
528 print ".SH SYNOPSIS\n";
529 print ".B #include <". lc($args{'module'}) . ".h>\n";
531 print ".BI \"".$args{'functiontype'}." ".$args{'function'}."(";
533 foreach $parameter (@
{$args{'parameterlist'}}) {
534 print $args{'parametertypes'}{$parameter}." \" ".$parameter." \"";
535 if ($count != $#{$args{'parameterlist'}}) {
542 print ".SH ARGUMENTS\n";
543 foreach $parameter (@
{$args{'parameterlist'}}) {
544 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
545 output_highlight
($args{'parameters'}{$parameter});
547 foreach $section (@
{$args{'sectionlist'}}) {
548 print ".SH \"" . uc($section) . "\"\n";
549 output_highlight
($args{'sections'}{$section});
551 print ".SH \"REPORTING BUGS\"\n";
552 print "Report bugs to <bug-". lc($args{'module'}) . "\@josefsson.org>.\n";
553 print ".SH COPYRIGHT\n";
554 print "Copyright \\(co 2002, 2003 Simon Josefsson.\n";
556 print "Permission is granted to copy, distribute and/or modify this document\n";
557 print "under the terms of the GNU Free Documentation License, Version 1.1\n";
558 print "or any later version published by the Free Software Foundation.\n";
559 print ".SH \"SEE ALSO\"\n";
560 print "The full documentation for\n";
561 print ".B " . ucfirst($args{'module'}) . "\n";
562 print "is maintained as a Texinfo manual. If the\n";
565 print ".B " . lc($args{'module'}) . "\n";
566 print "programs are properly installed at your site, the command\n";
568 print ".B info " . lc($args{'module'}) . "\n";
570 print "should give you access to the complete manual.\n";
573 sub output_listfunc
{
575 print $args{'function'} . "\n";
582 my ($parameter, $section);
584 print "Function = ".$args{'function'}."\n";
585 print " return type: ".$args{'functiontype'}."\n\n";
586 foreach $parameter (@
{$args{'parameterlist'}}) {
587 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
588 print " -> ".$args{'parameters'}{$parameter}."\n";
590 foreach $section (@
{$args{'sectionlist'}}) {
591 print " $section:\n";
593 output_highlight
($args{'sections'}{$section});
598 # generic output function - calls the right one based
599 # on current output mode.
600 sub output_function
{
602 eval "output_".$output_mode."(\@_);";
607 # takes a function prototype and spits out all the details
608 # stored in the global arrays/hsahes.
610 my $prototype = shift @_;
612 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
613 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
614 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
615 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
616 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
621 # print STDERR "ARGS = '$args'\n";
623 foreach $arg (split ',', $args) {
624 # strip leading/trailing spaces
627 # print STDERR "SCAN ARG: '$arg'\n";
628 @args = split('\s', $arg);
630 # print STDERR " -> @args\n";
632 # print STDERR " -> @args\n";
633 if ($param =~ m/^(\*+)(.*)/) {
637 $type = join " ", @args;
639 if ($parameters{$param} eq "" && $param != "void") {
640 $parameters{$param} = "-- undescribed --";
641 print STDERR
"Warning($lineno): Function parameter '$param' not described in '$function_name'\n";
644 push @parameterlist, $param;
645 $parametertypes{$param} = $type;
647 # print STDERR "param = '$param', type = '$type'\n";
650 print STDERR
"Error($lineno): cannot understand prototype: '$prototype'\n";
654 if ($function_only==0 || defined($function_table{$function_name})) {
655 output_function
({'function' => $function_name,
656 'module' => $modulename,
657 'version' => $version,
658 'functiontype' => $return_type,
659 'parameterlist' => \
@parameterlist,
660 'parameters' => \
%parameters,
661 'parametertypes' => \
%parametertypes,
662 'sectionlist' => \
@sectionlist,
663 'sections' => \
%sections,
664 'purpose' => $function_purpose
669 ######################################################################
673 # 1 - looking for function name
674 # 2 - scanning field start.
675 # 3 - scanning prototype.
679 $doc_special = "\@\%\$\&";
681 $doc_start = "^/\\*\\*\$";
683 $doc_com = "\\s*\\*\\s*";
684 $doc_func = $doc_com."(\\w+):?";
685 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
686 $doc_content = $doc_com."(.*)";
695 $section_default = "Description"; # default section
696 $section = $section_default;
699 foreach $file (@ARGV) {
700 if (!open(IN
,"<$file")) {
701 print STDERR
"Error: Cannot open file $file\n";
709 $state = 1; # next line is always the function name
711 } elsif ($state == 1) { # this line is the function name (always)
716 $function_purpose = $1;
718 $function_purpose = "";
721 print STDERR
"Info($lineno): Scanning doc for $function\n";
724 print STDERR
"WARN($lineno): Cannot understand $_ on line $lineno",
725 " - I thought it was a doc line\n";
728 } elsif ($state == 2) { # look for head: lines, and include content
733 if ($contents ne "") {
734 dump_section
($section, $contents);
735 $section = $section_default;
738 $contents = $newcontents;
739 if ($contents ne "") {
742 $section = $newsection;
743 } elsif (/$doc_end/) {
745 if ($contents ne "") {
746 dump_section
($section, $contents);
747 $section = $section_default;
751 # print STDERR "end of doc comment, looking for prototype\n";
754 } elsif (/$doc_content/) {
755 # miguel-style comment kludge, look for blank lines after
756 # @parameter line to signify start of description
757 if ($1 eq "" && $section =~ m/^@/) {
758 dump_section
($section, $contents);
759 $section = $section_default;
762 $contents .= $1."\n";
765 # i dont know - bad line? ignore.
766 print STDERR
"WARNING($lineno): bad line: $_";
768 } elsif ($state == 3) { # scanning for function { (end of prototype)
769 if (m
#\s*/\*\s+MACDOC\s*#io) {
776 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
777 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
778 $prototype =~ s@
^ +@
@gos; # strip leading spaces
779 dump_function
($prototype);
784 %parametertypes = ();