3 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
4 ## hacked to allow -tex option --nmav ##
5 ## hacked to allow -texinfo option --jas ##
7 ## This software falls under the GNU Public License. Please read ##
8 ## the COPYING file for more information ##
11 # This will read a 'c' file and scan for embedded comments in the
12 # style of gnome comments (+minor extensions - see below).
14 # This program is modified by Nikos Mavroyanopoulos, for the gnutls
17 # Note: This only supports 'c'.
20 # gdoc [ -docbook | -html | -text | -man | -tex | -texinfo ]
21 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
23 # Set output format using one of -docbook -html -text -man -tex or
24 # -texinfo. Default is man.
27 # If set, then only generate documentation for the given
28 # function(s). All other functions are ignored.
30 # c files - list of 'c' files to process
32 # All output goes to stdout, with errors to stderr.
36 # In the following table, (...)? signifies optional structure.
37 # (...)* signifies 0 or more structure elements
39 # * function_name(:)? (- short description)?
40 # (* @parameterx: (description of parameter x)?)*
42 # * (Description:)? (Description of function)?
43 # * (section header: (section description)? )*
46 # So .. the trivial example would be:
52 # If the Description: header tag is ommitted, then there must be a blank line
53 # after the last parameter specification.
56 # * my_function - does my stuff
57 # * @my_arg: its mine damnit
59 # * Does my stuff explained.
64 # * my_function - does my stuff
65 # * @my_arg: its mine damnit
66 # * Description: Does my stuff explained.
70 # All descriptions can be multiline, apart from the short function description.
72 # All descriptive text is further processed, scanning for the following special
73 # patterns, which are highlighted appropriately.
75 # 'funcname()' - function
76 # '$ENVVAR' - environmental variable
77 # '&struct_name' - name of a structure
78 # '@parameter' - name of a parameter
79 # '%CONST' - name of a constant.
82 # Extensions for LaTeX:
84 # 1. the symbol '->' will be replaced with a rightarrow
85 # 2. x^y with ${x}^{y}$.
89 # match expressions used to find embedded type information
90 $type_constant = "\\\%(\\w+)";
91 #$type_func = "(\\w+\\(\\))";
92 $type_func = "(\\(w||\\\\)+\\(\\))";
93 $type_param = "\\\@(\\w+)";
94 $type_struct = "\\\&(\\w+)";
95 $type_env = "(\\\$\\w+)";
98 # Output conversion substitutions.
99 # One for each output format
101 # these work fairly well
102 %highlights_html = ( $type_constant, "<i>\$1</i>",
103 $type_func, "<b>\$1</b>",
104 $type_struct, "<i>\$1</i>",
105 $type_param, "<tt><b>\$1</b></tt>" );
106 $blankline_html = "<p>";
108 %highlights_texinfo = ( $type_constant, "\@var{\$1}",
109 $type_func, "\@code{\$1}",
110 $type_struct, "\@code{\$1}",
111 $type_param, "\@code{\$1}" );
112 $blankline_texinfo = "";
114 %highlights_tex = ( $type_constant, "{\\\\it \$1}",
115 $type_func, "{\\\\bf \$1}",
116 $type_struct, "{\\\\it \$1}",
117 $type_param, "{\\\\bf \$1}" );
118 $blankline_tex = "\\par";
120 # sgml, docbook format
121 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
122 $type_func, "<function>\$1</function>",
123 $type_struct, "<structname>\$1</structname>",
124 $type_env, "<envar>\$1</envar>",
125 $type_param, "<parameter>\$1</parameter>" );
126 $blankline_sgml = "</para><para>\n";
128 # these are pretty rough
129 %highlights_man = ( $type_constant, "\\n.I \\\"\$1\\\"\\n",
130 $type_func, "\\n.B \\\"\$1\\\"\\n",
131 $type_struct, "\\n.I \\\"\$1\\\"\\n",
132 $type_param."([\.\, ]*)\n?", "\\n.I \\\"\$1\$2\\\"\\n" );
136 %highlights_text = ( $type_constant, "\$1",
139 $type_param, "\$1" );
140 $blankline_text = "";
144 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -tex | -texinfo ]\n";
145 print " [ -function funcname [ -function funcname ...] ]\n";
146 print " c source file(s) > outputfile\n";
156 $output_mode = "man";
157 %highlights = %highlights_man;
158 $blankline = $blankline_man;
159 $modulename = "API Documentation";
161 while ($ARGV[0] =~ m/^-(.*)/) {
163 if ($cmd eq "-html") {
164 $output_mode = "html";
165 %highlights = %highlights_html;
166 $blankline = $blankline_html;
167 } elsif ($cmd eq "-man") {
168 $output_mode = "man";
169 %highlights = %highlights_man;
170 $blankline = $blankline_man;
171 } elsif ($cmd eq "-tex") {
172 $output_mode = "tex";
173 %highlights = %highlights_tex;
174 $blankline = $blankline_tex;
175 } elsif ($cmd eq "-texinfo") {
176 $output_mode = "texinfo";
177 %highlights = %highlights_texinfo;
178 $blankline = $blankline_texinfo;
179 } elsif ($cmd eq "-text") {
180 $output_mode = "text";
181 %highlights = %highlights_text;
182 $blankline = $blankline_text;
183 } elsif ($cmd eq "-docbook") {
184 $output_mode = "sgml";
185 %highlights = %highlights_sgml;
186 $blankline = $blankline_sgml;
187 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
188 $modulename = shift @ARGV;
189 } elsif ($cmd eq "-function") { # to only output specific functions
191 $function = shift @ARGV;
192 $function_table{$function} = 1;
193 } elsif ($cmd eq "-v") {
195 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
201 # generate a sequence of code that will splice in highlighting information
202 # using the s// operator.
204 foreach $pattern (keys %highlights) {
205 # print STDERR "scanning pattern $pattern ($highlights{$pattern})\n";
206 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
210 # dumps section contents to arrays/hashes intended for that purpose.
214 my $contents = join "\n", @_;
216 if ($name =~ m/$type_constant/) {
218 # print STDERR "constant section '$1' = '$contents'\n";
219 $constants{$name} = $contents;
220 } elsif ($name =~ m/$type_param/) {
221 # print STDERR "parameter def '$1' = '$contents'\n";
223 $parameters{$name} = $contents;
225 # print STDERR "other section '$name' = '$contents'\n";
226 $sections{$name} = $contents;
227 push @sectionlist, $name;
234 # parameters, a hash.
235 # function => "function name"
236 # parameterlist => @list of parameters
237 # parameters => %parameter descriptions
238 # sectionlist => @list of sections
239 # sections => %descriont descriptions
242 sub output_highlight
{
243 my $contents = join "\n", @_;
247 foreach $line (split "\n", $contents) {
249 print $lineprefix, $blankline;
251 print $lineprefix, $line;
260 my ($parameter, $section);
263 print "\@deftypefun {";
264 print $args{'functiontype'};
265 print "} ".$args{'function'}." ";
268 foreach $parameter (@
{$args{'parameterlist'}}) {
269 print $args{'parametertypes'}{$parameter}." \@var{".$parameter."}";
270 if ($count != $#{$args{'parameterlist'}}) {
276 foreach $parameter (@
{$args{'parameterlist'}}) {
277 if ($args{'parameters'}{$parameter}) {
278 print "\@var{".$parameter."}: ";
279 output_highlight
($args{'parameters'}{$parameter});
283 foreach $section (@
{$args{'sectionlist'}}) {
284 output_highlight
($args{'sections'}{$section});
287 print "\@end deftypefun\n\n";
293 my ($parameter, $section);
295 print "\n\n<a name=\"". $args{'function'} . "\"> </a><h2>Function</h2>\n";
297 print "<i>".$args{'functiontype'}."</i>\n";
298 print "<b>".$args{'function'}."</b>\n";
301 foreach $parameter (@
{$args{'parameterlist'}}) {
302 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
303 if ($count != $#{$args{'parameterlist'}}) {
310 print "<h3>Arguments</h3>\n";
312 foreach $parameter (@
{$args{'parameterlist'}}) {
313 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
315 output_highlight
($args{'parameters'}{$parameter});
318 foreach $section (@
{$args{'sectionlist'}}) {
319 print "<h3>$section</h3>\n";
321 output_highlight
($args{'sections'}{$section});
330 my ($parameter, $section);
332 my $func = $args{'function'};
341 print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
343 $type = $args{'functiontype'};
346 print "{\\it ".$type."}\n";
347 print "{\\bf ".$func."}\n";
350 foreach $parameter (@
{$args{'parameterlist'}}) {
351 $param = $args{'parametertypes'}{$parameter};
352 $param2 = $parameter;
354 $param2 =~ s/_/\\_/g;
356 print "{\\it ".$param."} {\\bf ".$param2."}\n";
357 if ($count != $#{$args{'parameterlist'}}) {
364 print "\n{\\large{Arguments}}\n";
366 print "\\begin{itemize}\n";
368 foreach $parameter (@
{$args{'parameterlist'}}) {
369 $param1 = $args{'parametertypes'}{$parameter};
370 $param1 =~ s/_/\\_/g;
371 $param2 = $parameter;
372 $param2 =~ s/_/\\_/g;
375 print "\\item {\\it ".$param1."} {\\bf ".$param2."}: \n";
378 $param3 = $args{'parameters'}{$parameter};
379 $param3 =~ s/_/\\_/g;
380 $param3 =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
382 output_highlight
($param3);
385 print "\\item void\n";
387 print "\\end{itemize}\n";
389 foreach $section (@
{$args{'sectionlist'}}) {
392 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
394 print "\n\\par{\\large{$sec}}\\par\n";
395 print "\\begin{rmfamily}\n";
397 $sec = $args{'sections'}{$section};
400 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
401 $sec =~ s/->/\$\\rightarrow\$/g;
402 $sec =~ s/([0-9]+)\^([0-9]+)/\$\{\1\}\^\{\2\}\$/g;
404 output_highlight
($sec);
405 print "\\end{rmfamily}\n";
411 # output in sgml DocBook
414 my ($parameter, $section);
418 $id = $args{'module'}."-".$args{'function'};
419 $id =~ s/[^A-Za-z0-9]/-/g;
421 print "<refentry>\n";
423 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
424 print "</refmeta>\n";
425 print "<refnamediv>\n";
426 print " <refname>".$args{'function'}."</refname>\n";
427 print " <refpurpose>\n";
428 print " ".$args{'purpose'}."\n";
429 print " </refpurpose>\n";
430 print "</refnamediv>\n";
432 print "<refsynopsisdiv>\n";
433 print " <title>Synopsis</title>\n";
434 print " <funcsynopsis>\n";
435 print " <funcdef>".$args{'functiontype'}." ";
436 print "<function>".$args{'function'}." ";
437 print "</function></funcdef>\n";
439 # print "<refsect1>\n";
440 # print " <title>Synopsis</title>\n";
441 # print " <funcsynopsis>\n";
442 # print " <funcdef>".$args{'functiontype'}." ";
443 # print "<function>".$args{'function'}." ";
444 # print "</function></funcdef>\n";
447 if ($#{$args{'parameterlist'}} >= 0) {
448 foreach $parameter (@
{$args{'parameterlist'}}) {
449 print " <paramdef>".$args{'parametertypes'}{$parameter};
450 print " <parameter>$parameter</parameter></paramdef>\n";
455 print " </funcsynopsis>\n";
456 print "</refsynopsisdiv>\n";
457 # print "</refsect1>\n";
460 print "<refsect1>\n <title>Arguments</title>\n";
461 # print "<para>\nArguments\n";
462 if ($#{$args{'parameterlist'}} >= 0) {
463 print " <variablelist>\n";
464 foreach $parameter (@
{$args{'parameterlist'}}) {
465 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
466 print " <listitem>\n <para>\n";
468 output_highlight
($args{'parameters'}{$parameter});
469 print " </para>\n </listitem>\n </varlistentry>\n";
471 print " </variablelist>\n";
473 print " <para>\n None\n </para>\n";
475 print "</refsect1>\n";
477 # print out each section
479 foreach $section (@
{$args{'sectionlist'}}) {
480 print "<refsect1>\n <title>$section</title>\n <para>\n";
481 # print "<para>\n$section\n";
482 if ($section =~ m/EXAMPLE/i) {
483 print "<example><para>\n";
485 output_highlight
($args{'sections'}{$section});
487 if ($section =~ m/EXAMPLE/i) {
488 print "</para></example>\n";
490 print " </para>\n</refsect1>\n";
500 my ($parameter, $section);
503 print ".TH \"$args{'module'}\" \"$args{'function'}\" \"25 May 1998\" \"API Manual\" GNOME\n";
505 print ".SH Function\n";
507 print ".I \"".$args{'functiontype'}."\"\n";
508 print ".B \"".$args{'function'}."\"\n";
511 foreach $parameter (@
{$args{'parameterlist'}}) {
512 print ".I \"".$args{'parametertypes'}{$parameter}."\"\n.B \"".$parameter."\"\n";
513 if ($count != $#{$args{'parameterlist'}}) {
520 print ".SH Arguments\n";
521 foreach $parameter (@
{$args{'parameterlist'}}) {
522 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
523 output_highlight
($args{'parameters'}{$parameter});
525 foreach $section (@
{$args{'sectionlist'}}) {
526 print ".SH \"$section\"\n";
527 output_highlight
($args{'sections'}{$section});
535 my ($parameter, $section);
537 print "Function = ".$args{'function'}."\n";
538 print " return type: ".$args{'functiontype'}."\n\n";
539 foreach $parameter (@
{$args{'parameterlist'}}) {
540 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
541 print " -> ".$args{'parameters'}{$parameter}."\n";
543 foreach $section (@
{$args{'sectionlist'}}) {
544 print " $section:\n";
546 output_highlight
($args{'sections'}{$section});
551 # generic output function - calls the right one based
552 # on current output mode.
553 sub output_function
{
555 eval "output_".$output_mode."(\@_);";
560 # takes a function prototype and spits out all the details
561 # stored in the global arrays/hsahes.
563 my $prototype = shift @_;
565 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
566 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
567 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
568 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
569 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
574 # print STDERR "ARGS = '$args'\n";
576 foreach $arg (split ',', $args) {
577 # strip leading/trailing spaces
580 # print STDERR "SCAN ARG: '$arg'\n";
581 @args = split('\s', $arg);
583 # print STDERR " -> @args\n";
585 # print STDERR " -> @args\n";
586 if ($param =~ m/^(\*+)(.*)/) {
590 $type = join " ", @args;
592 if ($parameters{$param} eq "" && $param != "void") {
593 $parameters{$param} = "-- undescribed --";
594 print STDERR
"Warning($lineno): Function parameter '$param' not described in '$function_name'\n";
597 push @parameterlist, $param;
598 $parametertypes{$param} = $type;
600 # print STDERR "param = '$param', type = '$type'\n";
603 print STDERR
"Error($lineno): cannot understand prototype: '$prototype'\n";
607 if ($function_only==0 || defined($function_table{$function_name})) {
608 output_function
({'function' => $function_name,
609 'module' => $modulename,
610 'functiontype' => $return_type,
611 'parameterlist' => \
@parameterlist,
612 'parameters' => \
%parameters,
613 'parametertypes' => \
%parametertypes,
614 'sectionlist' => \
@sectionlist,
615 'sections' => \
%sections,
616 'purpose' => $function_purpose
621 ######################################################################
625 # 1 - looking for function name
626 # 2 - scanning field start.
627 # 3 - scanning prototype.
631 $doc_special = "\@\%\$\&";
633 $doc_start = "^/\\*\\*\$";
635 $doc_com = "\\s*\\*\\s*";
636 $doc_func = $doc_com."(\\w+):?";
637 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
638 $doc_content = $doc_com."(.*)";
647 $section_default = "Description"; # default section
648 $section = $section_default;
651 foreach $file (@ARGV) {
652 if (!open(IN
,"<$file")) {
653 print STDERR
"Error: Cannot open file $file\n";
661 $state = 1; # next line is always the function name
663 } elsif ($state == 1) { # this line is the function name (always)
668 $function_purpose = $1;
670 $function_purpose = "";
673 print STDERR
"Info($lineno): Scanning doc for $function\n";
676 print STDERR
"WARN($lineno): Cannot understand $_ on line $lineno",
677 " - I thought it was a doc line\n";
680 } elsif ($state == 2) { # look for head: lines, and include content
685 if ($contents ne "") {
686 dump_section
($section, $contents);
687 $section = $section_default;
690 $contents = $newcontents;
691 if ($contents ne "") {
694 $section = $newsection;
695 } elsif (/$doc_end/) {
697 if ($contents ne "") {
698 dump_section
($section, $contents);
699 $section = $section_default;
703 # print STDERR "end of doc comment, looking for prototype\n";
706 } elsif (/$doc_content/) {
707 # miguel-style comment kludge, look for blank lines after
708 # @parameter line to signify start of description
709 if ($1 eq "" && $section =~ m/^@/) {
710 dump_section
($section, $contents);
711 $section = $section_default;
714 $contents .= $1."\n";
717 # i dont know - bad line? ignore.
718 print STDERR
"WARNING($lineno): bad line: $_";
720 } elsif ($state == 3) { # scanning for function { (end of prototype)
721 if (m
#\s*/\*\s+MACDOC\s*#io) {
728 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
729 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
730 $prototype =~ s@
^ +@
@gos; # strip leading spaces
731 dump_function
($prototype);
736 %parametertypes = ();