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>";
109 %highlights_texinfo = ( $type_constant, "CMDvar{\$1}",
110 $type_func, "CMDcode{\$1}",
111 $type_struct, "CMDcode{\$1}",
112 $type_param, "CMDcode\{\$1\}" );
113 $blankline_texinfo = "";
115 %highlights_tex = ( $type_constant, "{\\\\it \$1}",
116 $type_func, "{\\\\bf \$1}",
117 $type_struct, "{\\\\it \$1}",
118 $type_param, "{\\\\bf \$1}" );
119 $blankline_tex = "\\par";
121 # sgml, docbook format
122 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
123 $type_func, "<function>\$1</function>",
124 $type_struct, "<structname>\$1</structname>",
125 $type_env, "<envar>\$1</envar>",
126 $type_param, "<parameter>\$1</parameter>" );
127 $blankline_sgml = "</para><para>\n";
129 # these are pretty rough
130 %highlights_man = ( $type_constant, "\\n.I \\\"\$1\\\"\\n",
131 $type_func, "\\n.B \\\"\$1\\\"\\n",
132 $type_struct, "\\n.I \\\"\$1\\\"\\n",
133 $type_param."([\.\, ]*)\n?", "\\n.I \\\"\$1\$2\\\"\\n" );
137 %highlights_text = ( $type_constant, "\$1",
140 $type_param, "\$1" );
141 $blankline_text = "";
145 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -tex | -texinfo ]\n";
146 print " [ -function funcname [ -function funcname ...] ]\n";
147 print " c source file(s) > outputfile\n";
157 $output_mode = "man";
158 %highlights = %highlights_man;
159 $blankline = $blankline_man;
160 $modulename = "API Documentation";
162 while ($ARGV[0] =~ m/^-(.*)/) {
164 if ($cmd eq "-html") {
165 $output_mode = "html";
166 %highlights = %highlights_html;
167 $blankline = $blankline_html;
168 } elsif ($cmd eq "-man") {
169 $output_mode = "man";
170 %highlights = %highlights_man;
171 $blankline = $blankline_man;
172 } elsif ($cmd eq "-tex") {
173 $output_mode = "tex";
174 %highlights = %highlights_tex;
175 $blankline = $blankline_tex;
176 } elsif ($cmd eq "-texinfo") {
177 $output_mode = "texinfo";
178 %highlights = %highlights_texinfo;
179 $blankline = $blankline_texinfo;
180 } elsif ($cmd eq "-text") {
181 $output_mode = "text";
182 %highlights = %highlights_text;
183 $blankline = $blankline_text;
184 } elsif ($cmd eq "-docbook") {
185 $output_mode = "sgml";
186 %highlights = %highlights_sgml;
187 $blankline = $blankline_sgml;
188 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
189 $modulename = shift @ARGV;
190 } elsif ($cmd eq "-function") { # to only output specific functions
192 $function = shift @ARGV;
193 $function_table{$function} = 1;
194 } elsif ($cmd eq "-v") {
196 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
202 # generate a sequence of code that will splice in highlighting information
203 # using the s// operator.
205 foreach $pattern (keys %highlights) {
206 # print STDERR "scanning pattern $pattern ($highlights{$pattern})\n";
207 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
211 # dumps section contents to arrays/hashes intended for that purpose.
215 my $contents = join "\n", @_;
217 if ($name =~ m/$type_constant/) {
219 # print STDERR "constant section '$1' = '$contents'\n";
220 $constants{$name} = $contents;
221 } elsif ($name =~ m/$type_param/) {
222 # print STDERR "parameter def '$1' = '$contents'\n";
224 $parameters{$name} = $contents;
226 # print STDERR "other section '$name' = '$contents'\n";
227 $sections{$name} = $contents;
228 push @sectionlist, $name;
235 # parameters, a hash.
236 # function => "function name"
237 # parameterlist => @list of parameters
238 # parameters => %parameter descriptions
239 # sectionlist => @list of sections
240 # sections => %descriont descriptions
243 sub output_highlight
{
244 my $contents = join "\n", @_;
248 $contents =~ s
:CMD
:@
:gs
if ($output_mode eq "texinfo");
249 foreach $line (split "\n", $contents) {
251 print $lineprefix, $blankline;
253 print $lineprefix, $line;
262 my ($parameter, $section);
265 print "\@deftypefun {";
266 print $args{'functiontype'};
267 print "} ".$args{'function'}." ";
270 foreach $parameter (@
{$args{'parameterlist'}}) {
271 print $args{'parametertypes'}{$parameter}." \@var{".$parameter."}";
272 if ($count != $#{$args{'parameterlist'}}) {
278 foreach $parameter (@
{$args{'parameterlist'}}) {
279 if ($args{'parameters'}{$parameter}) {
280 print "\@var{".$parameter."}: ";
281 output_highlight
($args{'parameters'}{$parameter});
285 foreach $section (@
{$args{'sectionlist'}}) {
286 output_highlight
($args{'sections'}{$section});
289 print "\@end deftypefun\n\n";
295 my ($parameter, $section);
297 print "\n\n<a name=\"". $args{'function'} . "\"> </a><h2>Function</h2>\n";
299 print "<i>".$args{'functiontype'}."</i>\n";
300 print "<b>".$args{'function'}."</b>\n";
303 foreach $parameter (@
{$args{'parameterlist'}}) {
304 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
305 if ($count != $#{$args{'parameterlist'}}) {
312 print "<h3>Arguments</h3>\n";
314 foreach $parameter (@
{$args{'parameterlist'}}) {
315 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
317 output_highlight
($args{'parameters'}{$parameter});
320 foreach $section (@
{$args{'sectionlist'}}) {
321 print "<h3>$section</h3>\n";
323 output_highlight
($args{'sections'}{$section});
332 my ($parameter, $section);
334 my $func = $args{'function'};
343 print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
345 $type = $args{'functiontype'};
348 print "{\\it ".$type."}\n";
349 print "{\\bf ".$func."}\n";
352 foreach $parameter (@
{$args{'parameterlist'}}) {
353 $param = $args{'parametertypes'}{$parameter};
354 $param2 = $parameter;
356 $param2 =~ s/_/\\_/g;
358 print "{\\it ".$param."} {\\bf ".$param2."}\n";
359 if ($count != $#{$args{'parameterlist'}}) {
366 print "\n{\\large{Arguments}}\n";
368 print "\\begin{itemize}\n";
370 foreach $parameter (@
{$args{'parameterlist'}}) {
371 $param1 = $args{'parametertypes'}{$parameter};
372 $param1 =~ s/_/\\_/g;
373 $param2 = $parameter;
374 $param2 =~ s/_/\\_/g;
377 print "\\item {\\it ".$param1."} {\\bf ".$param2."}: \n";
380 $param3 = $args{'parameters'}{$parameter};
381 $param3 =~ s/_/\\_/g;
382 $param3 =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
384 output_highlight
($param3);
387 print "\\item void\n";
389 print "\\end{itemize}\n";
391 foreach $section (@
{$args{'sectionlist'}}) {
394 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
396 print "\n\\par{\\large{$sec}}\\par\n";
397 print "\\begin{rmfamily}\n";
399 $sec = $args{'sections'}{$section};
402 $sec =~ s/&([a-zA-Z\_]+)/{\\it \1}/g;
403 $sec =~ s/->/\$\\rightarrow\$/g;
404 $sec =~ s/([0-9]+)\^([0-9]+)/\$\{\1\}\^\{\2\}\$/g;
406 output_highlight
($sec);
407 print "\\end{rmfamily}\n";
413 # output in sgml DocBook
416 my ($parameter, $section);
420 $id = $args{'module'}."-".$args{'function'};
421 $id =~ s/[^A-Za-z0-9]/-/g;
423 print "<refentry>\n";
425 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
426 print "</refmeta>\n";
427 print "<refnamediv>\n";
428 print " <refname>".$args{'function'}."</refname>\n";
429 print " <refpurpose>\n";
430 print " ".$args{'purpose'}."\n";
431 print " </refpurpose>\n";
432 print "</refnamediv>\n";
434 print "<refsynopsisdiv>\n";
435 print " <title>Synopsis</title>\n";
436 print " <funcsynopsis>\n";
437 print " <funcdef>".$args{'functiontype'}." ";
438 print "<function>".$args{'function'}." ";
439 print "</function></funcdef>\n";
441 # print "<refsect1>\n";
442 # print " <title>Synopsis</title>\n";
443 # print " <funcsynopsis>\n";
444 # print " <funcdef>".$args{'functiontype'}." ";
445 # print "<function>".$args{'function'}." ";
446 # print "</function></funcdef>\n";
449 if ($#{$args{'parameterlist'}} >= 0) {
450 foreach $parameter (@
{$args{'parameterlist'}}) {
451 print " <paramdef>".$args{'parametertypes'}{$parameter};
452 print " <parameter>$parameter</parameter></paramdef>\n";
457 print " </funcsynopsis>\n";
458 print "</refsynopsisdiv>\n";
459 # print "</refsect1>\n";
462 print "<refsect1>\n <title>Arguments</title>\n";
463 # print "<para>\nArguments\n";
464 if ($#{$args{'parameterlist'}} >= 0) {
465 print " <variablelist>\n";
466 foreach $parameter (@
{$args{'parameterlist'}}) {
467 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
468 print " <listitem>\n <para>\n";
470 output_highlight
($args{'parameters'}{$parameter});
471 print " </para>\n </listitem>\n </varlistentry>\n";
473 print " </variablelist>\n";
475 print " <para>\n None\n </para>\n";
477 print "</refsect1>\n";
479 # print out each section
481 foreach $section (@
{$args{'sectionlist'}}) {
482 print "<refsect1>\n <title>$section</title>\n <para>\n";
483 # print "<para>\n$section\n";
484 if ($section =~ m/EXAMPLE/i) {
485 print "<example><para>\n";
487 output_highlight
($args{'sections'}{$section});
489 if ($section =~ m/EXAMPLE/i) {
490 print "</para></example>\n";
492 print " </para>\n</refsect1>\n";
502 my ($parameter, $section);
505 print ".TH \"$args{'module'}\" \"$args{'function'}\" \"25 May 1998\" \"API Manual\" GNOME\n";
507 print ".SH Function\n";
509 print ".I \"".$args{'functiontype'}."\"\n";
510 print ".B \"".$args{'function'}."\"\n";
513 foreach $parameter (@
{$args{'parameterlist'}}) {
514 print ".I \"".$args{'parametertypes'}{$parameter}."\"\n.B \"".$parameter."\"\n";
515 if ($count != $#{$args{'parameterlist'}}) {
522 print ".SH Arguments\n";
523 foreach $parameter (@
{$args{'parameterlist'}}) {
524 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
525 output_highlight
($args{'parameters'}{$parameter});
527 foreach $section (@
{$args{'sectionlist'}}) {
528 print ".SH \"$section\"\n";
529 output_highlight
($args{'sections'}{$section});
537 my ($parameter, $section);
539 print "Function = ".$args{'function'}."\n";
540 print " return type: ".$args{'functiontype'}."\n\n";
541 foreach $parameter (@
{$args{'parameterlist'}}) {
542 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
543 print " -> ".$args{'parameters'}{$parameter}."\n";
545 foreach $section (@
{$args{'sectionlist'}}) {
546 print " $section:\n";
548 output_highlight
($args{'sections'}{$section});
553 # generic output function - calls the right one based
554 # on current output mode.
555 sub output_function
{
557 eval "output_".$output_mode."(\@_);";
562 # takes a function prototype and spits out all the details
563 # stored in the global arrays/hsahes.
565 my $prototype = shift @_;
567 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
568 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
569 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
570 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
571 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
576 # print STDERR "ARGS = '$args'\n";
578 foreach $arg (split ',', $args) {
579 # strip leading/trailing spaces
582 # print STDERR "SCAN ARG: '$arg'\n";
583 @args = split('\s', $arg);
585 # print STDERR " -> @args\n";
587 # print STDERR " -> @args\n";
588 if ($param =~ m/^(\*+)(.*)/) {
592 $type = join " ", @args;
594 if ($parameters{$param} eq "" && $param != "void") {
595 $parameters{$param} = "-- undescribed --";
596 print STDERR
"Warning($lineno): Function parameter '$param' not described in '$function_name'\n";
599 push @parameterlist, $param;
600 $parametertypes{$param} = $type;
602 # print STDERR "param = '$param', type = '$type'\n";
605 print STDERR
"Error($lineno): cannot understand prototype: '$prototype'\n";
609 if ($function_only==0 || defined($function_table{$function_name})) {
610 output_function
({'function' => $function_name,
611 'module' => $modulename,
612 'functiontype' => $return_type,
613 'parameterlist' => \
@parameterlist,
614 'parameters' => \
%parameters,
615 'parametertypes' => \
%parametertypes,
616 'sectionlist' => \
@sectionlist,
617 'sections' => \
%sections,
618 'purpose' => $function_purpose
623 ######################################################################
627 # 1 - looking for function name
628 # 2 - scanning field start.
629 # 3 - scanning prototype.
633 $doc_special = "\@\%\$\&";
635 $doc_start = "^/\\*\\*\$";
637 $doc_com = "\\s*\\*\\s*";
638 $doc_func = $doc_com."(\\w+):?";
639 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
640 $doc_content = $doc_com."(.*)";
649 $section_default = "Description"; # default section
650 $section = $section_default;
653 foreach $file (@ARGV) {
654 if (!open(IN
,"<$file")) {
655 print STDERR
"Error: Cannot open file $file\n";
663 $state = 1; # next line is always the function name
665 } elsif ($state == 1) { # this line is the function name (always)
670 $function_purpose = $1;
672 $function_purpose = "";
675 print STDERR
"Info($lineno): Scanning doc for $function\n";
678 print STDERR
"WARN($lineno): Cannot understand $_ on line $lineno",
679 " - I thought it was a doc line\n";
682 } elsif ($state == 2) { # look for head: lines, and include content
687 if ($contents ne "") {
688 dump_section
($section, $contents);
689 $section = $section_default;
692 $contents = $newcontents;
693 if ($contents ne "") {
696 $section = $newsection;
697 } elsif (/$doc_end/) {
699 if ($contents ne "") {
700 dump_section
($section, $contents);
701 $section = $section_default;
705 # print STDERR "end of doc comment, looking for prototype\n";
708 } elsif (/$doc_content/) {
709 # miguel-style comment kludge, look for blank lines after
710 # @parameter line to signify start of description
711 if ($1 eq "" && $section =~ m/^@/) {
712 dump_section
($section, $contents);
713 $section = $section_default;
716 $contents .= $1."\n";
719 # i dont know - bad line? ignore.
720 print STDERR
"WARNING($lineno): bad line: $_";
722 } elsif ($state == 3) { # scanning for function { (end of prototype)
723 if (m
#\s*/\*\s+MACDOC\s*#io) {
730 $prototype =~ s@
/\*.*?\*/@
@gos; # strip comments.
731 $prototype =~ s@
[\r\n]+@
@gos; # strip newlines/cr's.
732 $prototype =~ s@
^ +@
@gos; # strip leading spaces
733 dump_function
($prototype);
738 %parametertypes = ();