3 ## Copyright (c) 2002, 2003, 2004, 2005, 2006 Simon Josefsson ##
4 ## added -texinfo, -listfunc ##
6 ## various improvements ##
7 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
8 ## hacked to allow -tex option --nmav ##
10 ## This software falls under the GNU Public License. Please read ##
11 ## the COPYING file for more information ##
14 # This will read a 'c' file and scan for embedded comments in the
15 # style of gnome comments (+minor extensions - see below).
17 # This program is modified by Nikos Mavroyanopoulos, for the gnutls
20 # Note: This only supports 'c'.
23 # gdoc [ -docbook | -html | -text | -man | -tex | -texinfo | -listfunc ]
24 # [ -sourceversion verno ] [ -include file | -includefuncprefix ]
26 # [ -seeinfo infonode ] [ -copyright notice ] [ -verbatimcopying ]
27 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
29 # Set output format using one of -docbook, -html, -text, -man, -tex,
30 # -texinfo, or -listfunc. Default is man.
33 # Version number for source code, e.g. '1.0.4'. Used in 'man' headers.
34 # Defaults to using current date.
37 # For man pages, mention #include <FILE.h> in the synopsis.
40 # For man pages, mention a #include <FILE.h> in the synopsis.
41 # The FILE derived from the function prefix. For example, a
42 # function gss_init_sec_context will generate an include
43 # statement of #include <gss.h>.
46 # For man pages, include a section about reporting bugs and mention
47 # the given e-mail address, e.g 'bug-libidn@gnu.org'.
50 # For man pages, include a section that point to an info manual
51 # for more information.
54 # For man pages, include a copyright section with the given
55 # notice after a preamble. Use, e.g., '2002, 2003 Simon Josefsson'.
58 # For man pages, and when the -copyright parameter is used,
59 # add a licensing statement that say verbatim copying is permitted.
62 # If set, then only generate documentation for the given function(s). All
63 # other functions are ignored.
65 # c files - list of 'c' files to process
67 # All output goes to stdout, with errors to stderr.
71 # In the following table, (...)? signifies optional structure.
72 # (...)* signifies 0 or more structure elements
74 # * function_name(:)? (- short description)?
75 # (* @parameterx: (description of parameter x)?)*
77 # * (Description:)? (Description of function)?
78 # * (Section header: (section description)? )*
81 # So .. the trivial example would be:
87 # If the Description: header tag is ommitted, then there must be a blank line
88 # after the last parameter specification.
91 # * my_function - does my stuff
92 # * @my_arg: its mine damnit
94 # * Does my stuff explained.
99 # * my_function - does my stuff
100 # * @my_arg: its mine damnit
101 # * Description: Does my stuff explained.
105 # All descriptions can be multiline, apart from the short function description.
107 # All descriptive text is further processed, scanning for the following special
108 # patterns, which are highlighted appropriately.
110 # 'funcname()' - function
111 # '$ENVVAR' - environmental variable OBSOLETE (?)
112 # '#struct_name' - name of a structure
113 # '@parameter' - name of a parameter
114 # '%CONST' - name of a constant.
117 # Extensions for LaTeX:
119 # 1. the symbol '->' will be replaced with a rightarrow
120 # 2. x^y with ${x}^{y}$.
123 use POSIX
qw(strftime);
125 # match expressions used to find embedded type information
126 $type_constant = "\\\%(\\w+)";
127 $type_func = "(\\w+\\(\\))";
128 $type_param = "\\\@(\\w+)";
129 $type_struct = "\\\#(\\w+)";
130 $type_env = "(\\\$\\w+)";
133 # Output conversion substitutions.
134 # One for each output format
136 # these work fairly well
137 %highlights_html = ( $type_constant, "<i>\$1</i>",
138 $type_func, "<b>\$1</b>",
139 $type_struct, "<i>\$1</i>",
140 $type_param, "<tt><b>\$1</b></tt>" );
141 $blankline_html = "<p>";
143 %highlights_texinfo = ( $type_constant, "\\\@code{\$1}",
144 $type_func, "\\\@code{\$1}",
145 $type_struct, "\\\@code{\$1}",
146 $type_param, "\\\@code{\$1}" );
147 $blankline_texinfo = "";
149 %highlights_tex = ( $type_constant, "{\\\\it \$1}",
150 $type_func, "{\\\\bf \$1}",
151 $type_struct, "{\\\\it \$1}",
152 $type_param, "{\\\\bf \$1}" );
153 $blankline_tex = "\\\\";
155 # sgml, docbook format
156 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
157 $type_func, "<function>\$1</function>",
158 $type_struct, "<structname>\$1</structname>",
159 $type_env, "<envar>\$1</envar>",
160 $type_param, "<parameter>\$1</parameter>" );
161 $blankline_sgml = "</para><para>\n";
163 # these are pretty rough
164 %highlights_man = ( $type_constant, "\\\\fB\$1\\\\fP",
165 $type_func, "\\\\fB\$1\\\\fP",
166 $type_struct, "\\\\fB\$1\\\\fP",
167 $type_param, "\\\\fI\$1\\\\fP" );
171 %highlights_text = ( $type_constant, "\$1",
174 $type_param, "\$1" );
175 $blankline_text = "";
179 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -tex | -texinfo -listfunc ]\n";
180 print " [ -sourceversion verno ] [ -include file | -includefuncprefix ]\n";
181 print " [ -bugsto address ] [ -seeinfo infonode ] [ -copyright notice]\n";
182 print " [ -verbatimcopying ]\n";
183 print " [ -function funcname [ -function funcname ...] ]\n";
184 print " c source file(s) > outputfile\n";
194 $output_mode = "man";
195 %highlights = %highlights_man;
196 $blankline = $blankline_man;
197 $modulename = "API Documentation";
198 $sourceversion = strftime
"%Y-%m-%d", localtime;
200 while ($ARGV[0] =~ m/^-(.*)/) {
202 if ($cmd eq "-html") {
203 $output_mode = "html";
204 %highlights = %highlights_html;
205 $blankline = $blankline_html;
206 } elsif ($cmd eq "-man") {
207 $output_mode = "man";
208 %highlights = %highlights_man;
209 $blankline = $blankline_man;
210 } elsif ($cmd eq "-tex") {
211 $output_mode = "tex";
212 %highlights = %highlights_tex;
213 $blankline = $blankline_tex;
214 } elsif ($cmd eq "-texinfo") {
215 $output_mode = "texinfo";
216 %highlights = %highlights_texinfo;
217 $blankline = $blankline_texinfo;
218 } elsif ($cmd eq "-text") {
219 $output_mode = "text";
220 %highlights = %highlights_text;
221 $blankline = $blankline_text;
222 } elsif ($cmd eq "-docbook") {
223 $output_mode = "sgml";
224 %highlights = %highlights_sgml;
225 $blankline = $blankline_sgml;
226 } elsif ($cmd eq "-listfunc") {
227 $output_mode = "listfunc";
228 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
229 $modulename = shift @ARGV;
230 } elsif ($cmd eq "-sourceversion") {
231 $sourceversion = shift @ARGV;
232 } elsif ($cmd eq "-include") {
233 $include = shift @ARGV;
234 } elsif ($cmd eq "-includefuncprefix") {
235 $includefuncprefix = 1;
236 } elsif ($cmd eq "-bugsto") {
237 $bugsto = shift @ARGV;
238 } elsif ($cmd eq "-copyright") {
239 $copyright = shift @ARGV;
240 } elsif ($cmd eq "-verbatimcopying") {
241 $verbatimcopying = 1;
242 } elsif ($cmd eq "-seeinfo") {
243 $seeinfo = shift @ARGV;
244 } elsif ($cmd eq "-function") { # to only output specific functions
246 $function = shift @ARGV;
247 $function_table{$function} = 1;
248 } elsif ($cmd eq "-v") {
250 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
256 # dumps section contents to arrays/hashes intended for that purpose.
260 my $contents = join "\n", @_;
262 if ($name =~ m/$type_constant/) {
264 # print STDERR "constant section '$1' = '$contents'\n";
265 $constants{$name} = $contents;
266 } elsif ($name =~ m/$type_param/) {
267 # print STDERR "parameter def '$1' = '$contents'\n";
269 $parameters{$name} = $contents;
271 # print STDERR "other section '$name' = '$contents'\n";
272 $sections{$name} = $contents;
273 push @sectionlist, $name;
280 # parameters, a hash.
281 # function => "function name"
282 # parameterlist => @list of parameters
283 # parameters => %parameter descriptions
284 # sectionlist => @list of sections
285 # sections => %descriont descriptions
297 $output =~ s
,\
$1,$match1,g
;
298 $output =~ s
,\
$2,$match2,g
;
299 $output =~ s
,\
$3,$match3,g
;
300 $output =~ s
,\
$4,$match4,g
;
302 eval "\$return = qq/$output/";
304 # print "pattern $pattern matched 1=$match1 2=$match2 3=$match3 4=$match4 replace $repl yielded $output interpolated $return\n";
309 sub output_highlight
{
310 my $contents = join "\n", @_;
313 foreach $pattern (keys %highlights) {
314 # print "scanning pattern $pattern ($highlights{$pattern})\n";
315 $contents =~ s
:$pattern:repstr
($pattern, $highlights{$pattern}, $1, $2, $3, $4):gse
;
317 foreach $line (split "\n", $contents) {
319 print $lineprefix, $blankline;
321 print $lineprefix, $line;
328 my $contents = join "\n", @_;
332 foreach $pattern (keys %highlights) {
333 # print "scanning pattern $pattern ($highlights{$pattern})\n";
334 $contents =~ s
:$pattern:repstr
($pattern, $highlights{$pattern}, $1, $2, $3, $4):gse
;
336 foreach $line (split "\n", $contents) {
338 $ret = $ret . $lineprefix . $blankline;
340 $ret = $ret . $lineprefix . $line;
351 my ($parameter, $section);
354 print "\@subheading ".$args{'function'}."\n";
355 print "\@anchor{".$args{'function'}."}\n";
356 print "\@deftypefun {" . $args{'functiontype'} . "} ";
357 print "{".$args{'function'}."} ";
360 foreach $parameter (@
{$args{'parameterlist'}}) {
361 print $args{'parametertypes'}{$parameter}." \@var{".$parameter."}";
362 if ($count != $#{$args{'parameterlist'}}) {
368 foreach $parameter (@
{$args{'parameterlist'}}) {
369 if ($args{'parameters'}{$parameter}) {
370 print "\@var{".$parameter."}: ";
371 output_highlight
($args{'parameters'}{$parameter});
375 foreach $section (@
{$args{'sectionlist'}}) {
376 print "\n\@strong{$section:} " if $section ne $section_default;
377 $args{'sections'}{$section} =~ s
:([{}]):\@\
1:gs
;
378 output_highlight
($args{'sections'}{$section});
380 print "\@end deftypefun\n\n";
386 my ($parameter, $section);
388 print "\n\n<a name=\"". $args{'function'} . "\"> </a><h2>Function</h2>\n";
390 print "<i>".$args{'functiontype'}."</i>\n";
391 print "<b>".$args{'function'}."</b>\n";
394 foreach $parameter (@
{$args{'parameterlist'}}) {
395 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
396 if ($count != $#{$args{'parameterlist'}}) {
403 print "<h3>Arguments</h3>\n";
405 foreach $parameter (@
{$args{'parameterlist'}}) {
406 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
408 output_highlight
($args{'parameters'}{$parameter});
411 foreach $section (@
{$args{'sectionlist'}}) {
412 print "<h3>$section</h3>\n";
414 output_highlight
($args{'sections'}{$section});
423 my ($parameter, $section);
425 my $func = $args{'function'};
434 print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
436 $type = $args{'functiontype'};
439 print "{\\it ".$type."}\n";
440 print "{\\bf ".$func."}\n";
443 foreach $parameter (@
{$args{'parameterlist'}}) {
444 $param = $args{'parametertypes'}{$parameter};
445 $param2 = $parameter;
447 $param2 =~ s/_/\\_/g;
449 print "{\\it ".$param."} {\\bf ".$param2."}";
450 if ($count != $#{$args{'parameterlist'}}) {
457 print "\n{\\large{Arguments}}\n";
459 print "\\begin{itemize}\n";
461 foreach $parameter (@
{$args{'parameterlist'}}) {
462 $param1 = $args{'parametertypes'}{$parameter};
463 $param1 =~ s/_/\\_/g;
464 $param2 = $parameter;
465 $param2 =~ s/_/\\_/g;
468 print "\\item {\\it ".$param1."} {\\bf ".$param2."}: \n";
471 $param3 = $args{'parameters'}{$parameter};
472 $param3 =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
474 $out = just_highlight
($param3);
479 print "\\item void\n";
481 print "\\end{itemize}\n";
483 foreach $section (@
{$args{'sectionlist'}}) {
486 $sec =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
488 print "\n{\\large{$sec}}\\\\\n";
489 print "\\begin{rmfamily}\n";
491 $sec = $args{'sections'}{$section};
493 $sec =~ s/#([a-zA-Z\_]+)/{\\it \1}/g;
494 $sec =~ s/->/\$\\rightarrow\$/g;
495 $sec =~ s/([0-9]+)\^([0-9]+)/\$\{\1\}\^\{\2\}\$/g;
497 $out = just_highlight
($sec);
501 print "\\end{rmfamily}\n";
507 # output in sgml DocBook
510 my ($parameter, $section);
514 $id = $args{'module'}."-".$args{'function'};
515 $id =~ s/[^A-Za-z0-9]/-/g;
517 print "<refentry>\n";
519 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
520 print "</refmeta>\n";
521 print "<refnamediv>\n";
522 print " <refname>".$args{'function'}."</refname>\n";
523 print " <refpurpose>\n";
524 print " ".$args{'purpose'}."\n";
525 print " </refpurpose>\n";
526 print "</refnamediv>\n";
528 print "<refsynopsisdiv>\n";
529 print " <title>Synopsis</title>\n";
530 print " <funcsynopsis>\n";
531 print " <funcdef>".$args{'functiontype'}." ";
532 print "<function>".$args{'function'}." ";
533 print "</function></funcdef>\n";
535 # print "<refsect1>\n";
536 # print " <title>Synopsis</title>\n";
537 # print " <funcsynopsis>\n";
538 # print " <funcdef>".$args{'functiontype'}." ";
539 # print "<function>".$args{'function'}." ";
540 # print "</function></funcdef>\n";
543 if ($#{$args{'parameterlist'}} >= 0) {
544 foreach $parameter (@
{$args{'parameterlist'}}) {
545 print " <paramdef>".$args{'parametertypes'}{$parameter};
546 print " <parameter>$parameter</parameter></paramdef>\n";
551 print " </funcsynopsis>\n";
552 print "</refsynopsisdiv>\n";
553 # print "</refsect1>\n";
556 print "<refsect1>\n <title>Arguments</title>\n";
557 # print "<para>\nArguments\n";
558 if ($#{$args{'parameterlist'}} >= 0) {
559 print " <variablelist>\n";
560 foreach $parameter (@
{$args{'parameterlist'}}) {
561 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
562 print " <listitem>\n <para>\n";
564 output_highlight
($args{'parameters'}{$parameter});
565 print " </para>\n </listitem>\n </varlistentry>\n";
567 print " </variablelist>\n";
569 print " <para>\n None\n </para>\n";
571 print "</refsect1>\n";
573 # print out each section
575 foreach $section (@
{$args{'sectionlist'}}) {
576 print "<refsect1>\n <title>$section</title>\n <para>\n";
577 # print "<para>\n$section\n";
578 if ($section =~ m/EXAMPLE/i) {
579 print "<example><para>\n";
581 output_highlight
($args{'sections'}{$section});
583 if ($section =~ m/EXAMPLE/i) {
584 print "</para></example>\n";
586 print " </para>\n</refsect1>\n";
596 my ($parameter, $section);
599 print ".\\\" DO NOT MODIFY THIS FILE! It was generated by gdoc.\n";
600 print ".TH \"$args{'function'}\" 3 \"$args{'sourceversion'}\" \"". $args{'module'} . "\" \"". $args{'module'} . "\"\n";
604 print $args{'function'};
605 if ($args{'purpose'}) {
606 print " \\- " . $args{'purpose'} . "\n";
608 print " \\- API function\n";
611 print ".SH SYNOPSIS\n";
612 print ".B #include <". $args{'include'} . ">\n"
614 print ".B #include <". lc((split /_/, $args{'function'})[0]) . ".h>\n"
615 if $args{'includefuncprefix'};
617 print ".BI \"".$args{'functiontype'}." ".$args{'function'}."(";
619 foreach $parameter (@
{$args{'parameterlist'}}) {
620 print $args{'parametertypes'}{$parameter}." \" ".$parameter." \"";
621 if ($count != $#{$args{'parameterlist'}}) {
628 print ".SH ARGUMENTS\n";
629 foreach $parameter (@
{$args{'parameterlist'}}) {
630 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
631 output_highlight
($args{'parameters'}{$parameter});
633 foreach $section (@
{$args{'sectionlist'}}) {
634 print ".SH \"" . uc($section) . "\"\n";
635 $sec = $args{'sections'}{$section};
637 output_highlight
($sec);
640 if ($args{'bugsto'}) {
641 print ".SH \"REPORTING BUGS\"\n";
642 print "Report bugs to <". $args{'bugsto'} . ">.\n";
645 if ($args{'copyright'}) {
646 print ".SH COPYRIGHT\n";
647 print "Copyright \\(co ". $args{'copyright'} . ".\n";
648 if ($args{'verbatimcopying'}) {
650 print "Permission is granted to make and distribute verbatim copies of this\n";
651 print "manual provided the copyright notice and this permission notice are\n";
652 print "preserved on all copies.\n";
656 if ($args{'seeinfo'}) {
657 print ".SH \"SEE ALSO\"\n";
658 print "The full documentation for\n";
659 print ".B " . $args{'module'} . "\n";
660 print "is maintained as a Texinfo manual. If the\n";
663 print ".B " . $args{'module'} . "\n";
664 print "programs are properly installed at your site, the command\n";
666 print ".B info " . $args{'seeinfo'} . "\n";
668 print "should give you access to the complete manual.\n";
672 sub output_listfunc
{
674 print $args{'function'} . "\n";
681 my ($parameter, $section);
683 print "Function = ".$args{'function'}."\n";
684 print " return type: ".$args{'functiontype'}."\n\n";
685 foreach $parameter (@
{$args{'parameterlist'}}) {
686 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
687 print " -> ".$args{'parameters'}{$parameter}."\n";
689 foreach $section (@
{$args{'sectionlist'}}) {
690 print " $section:\n";
692 output_highlight
($args{'sections'}{$section});
697 # generic output function - calls the right one based
698 # on current output mode.
699 sub output_function
{
701 eval "output_".$output_mode."(\@_);";
706 # takes a function prototype and spits out all the details
707 # stored in the global arrays/hsahes.
709 my $prototype = shift @_;
711 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
712 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
713 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
714 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
715 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
720 # print STDERR "ARGS = '$args'\n";
722 foreach $arg (split ',', $args) {
723 # strip leading/trailing spaces
726 # print STDERR "SCAN ARG: '$arg'\n";
727 @args = split('\s', $arg);
729 # print STDERR " -> @args\n";
731 # print STDERR " -> @args\n";
732 if ($param =~ m/^(\*+)(.*)/) {
736 if ($param =~ m/^(.*)(\[\])$/) {
740 # print STDERR " :> @args\n";
741 $type = join " ", @args;
743 if ($parameters{$param} eq "" && $param != "void") {
744 $parameters{$param} = "-- undescribed --";
745 print STDERR
"warning: $lineno: Function parameter '$param' not described in '$function_name'\n";
748 push @parameterlist, $param;
749 $parametertypes{$param} = $type;
751 # print STDERR "param = '$param', type = '$type'\n";
754 print STDERR
"warning: $lineno: Cannot understand prototype: '$prototype'\n";
758 if ($function_only==0 || defined($function_table{$function_name})) {
759 output_function
({'function' => $function_name,
760 'module' => $modulename,
761 'sourceversion' => $sourceversion,
762 'include' => $include,
763 'includefuncprefix' => $includefuncprefix,
765 'copyright' => $copyright,
766 'verbatimcopying' => $verbatimcopying,
767 'seeinfo' => $seeinfo,
768 'functiontype' => $return_type,
769 'parameterlist' => \
@parameterlist,
770 'parameters' => \
%parameters,
771 'parametertypes' => \
%parametertypes,
772 'sectionlist' => \
@sectionlist,
773 'sections' => \
%sections,
774 'purpose' => $function_purpose
779 ######################################################################
783 # 1 - looking for function name
784 # 2 - scanning field start.
785 # 3 - scanning prototype.
789 $doc_special = "\@\%\$\#";
791 $doc_start = "^/\\*\\*\$";
793 $doc_com = "\\s*\\*\\s*";
794 $doc_func = $doc_com."(\\w+):?";
795 $doc_sect = $doc_com."([".$doc_special."[:upper:]][\\w ]+):\\s*(.*)";
796 $doc_content = $doc_com."(.*)";
805 $section_default = "Description"; # default section
806 $section = $section_default;
809 foreach $file (@ARGV) {
810 if (!open(IN
,"<$file")) {
811 print STDERR
"Error: Cannot open file $file\n";
819 $state = 1; # next line is always the function name
821 } elsif ($state == 1) { # this line is the function name (always)
826 $function_purpose = $1;
828 $function_purpose = "";
831 print STDERR
"Info($lineno): Scanning doc for $function\n";
834 print STDERR
"warning: $lineno: Cannot understand $_ on line $lineno",
835 " - I thought it was a doc line\n";
838 } elsif ($state == 2) { # look for head: lines, and include content
843 if ($contents ne "") {
844 dump_section
($section, $contents);
845 $section = $section_default;
848 $contents = $newcontents;
849 if ($contents ne "") {
852 $section = $newsection;
853 } elsif (/$doc_end/) {
855 if ($contents ne "") {
856 dump_section
($section, $contents);
857 $section = $section_default;
861 # print STDERR "end of doc comment, looking for prototype\n";
864 } elsif (/$doc_content/) {
865 # miguel-style comment kludge, look for blank lines after
866 # @parameter line to signify start of description
867 if ($1 eq "" && $section =~ m/^@/) {
868 dump_section
($section, $contents);
869 $section = $section_default;
872 $contents .= $1."\n";
875 # i dont know - bad line? ignore.
876 print STDERR
"warning: $lineno: Bad line: $_";
878 } elsif ($state == 3) { # scanning for function { (end of prototype)
879 if (m
#\s*/\*\s+MACDOC\s*#io) {
886 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
887 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
888 $prototype =~ s@
^ +@
@gos; # strip leading spaces
889 dump_function
($prototype);
894 %parametertypes = ();