3 ## Copyright (c) 1998 Michael Zucchi, All Rights Reserved ##
4 ## hacked to allow -tex option --nmav ##
6 ## This software falls under the GNU Public License. Please read ##
7 ## the COPYING file for more information ##
10 # This will read a 'c' file and scan for embedded comments in the
11 # style of gnome comments (+minor extensions - see below).
14 # Note: This only supports 'c'.
17 # gdoc [ -docbook | -html | -text | -man ]
18 # [ -function funcname [ -function funcname ...] ] c file(s)s > outputfile
20 # Set output format using one of -docbook -html -text or -man. Default is man.
23 # If set, then only generate documentation for the given function(s). All
24 # other functions are ignored.
26 # c files - list of 'c' files to process
28 # All output goes to stdout, with errors to stderr.
32 # In the following table, (...)? signifies optional structure.
33 # (...)* signifies 0 or more structure elements
35 # * function_name(:)? (- short description)?
36 # (* @parameterx: (description of parameter x)?)*
38 # * (Description:)? (Description of function)?
39 # * (section header: (section description)? )*
42 # So .. the trivial example would be:
48 # If the Description: header tag is ommitted, then there must be a blank line
49 # after the last parameter specification.
52 # * my_function - does my stuff
53 # * @my_arg: its mine damnit
55 # * Does my stuff explained.
60 # * my_function - does my stuff
61 # * @my_arg: its mine damnit
62 # * Description: Does my stuff explained.
66 # All descriptions can be multiline, apart from the short function description.
68 # All descriptive text is further processed, scanning for the following special
69 # patterns, which are highlighted appropriately.
71 # 'funcname()' - function
72 # '$ENVVAR' - environmental variable
73 # '&struct_name' - name of a structure
74 # '@parameter' - name of a parameter
75 # '%CONST' - name of a constant.
77 # match expressions used to find embedded type information
78 $type_constant = "\\\%(\\w+)";
79 #$type_func = "(\\w+\\(\\))";
80 $type_func = "(\\(w||\\\\)+\\(\\))";
81 $type_param = "\\\@(\\w+)";
82 $type_struct = "\\\&(\\w+)";
83 $type_env = "(\\\$\\w+)";
86 # Output conversion substitutions.
87 # One for each output format
89 # these work fairly well
90 %highlights_html = ( $type_constant, "<i>\$1</i>",
91 $type_func, "<b>\$1</b>",
92 $type_struct, "<i>\$1</i>",
93 $type_param, "<tt><b>\$1</b></tt>" );
94 $blankline_html = "<p>";
96 %highlights_tex = ( $type_constant, "{\\\\it \$1}",
97 $type_func, "{\\\\bf \$1}",
98 $type_struct, "{\\\\it \$1}",
99 $type_param, "{\\\\bf \$1}" );
100 $blankline_tex = "\\par";
102 # sgml, docbook format
103 %highlights_sgml = ( $type_constant, "<replaceable class=\"option\">\$1</replaceable>",
104 $type_func, "<function>\$1</function>",
105 $type_struct, "<structname>\$1</structname>",
106 $type_env, "<envar>\$1</envar>",
107 $type_param, "<parameter>\$1</parameter>" );
108 $blankline_sgml = "</para><para>\n";
110 # these are pretty rough
111 %highlights_man = ( $type_constant, "\\n.I \\\"\$1\\\"\\n",
112 $type_func, "\\n.B \\\"\$1\\\"\\n",
113 $type_struct, "\\n.I \\\"\$1\\\"\\n",
114 $type_param."([\.\, ]*)\n?", "\\n.I \\\"\$1\$2\\\"\\n" );
118 %highlights_text = ( $type_constant, "\$1",
121 $type_param, "\$1" );
122 $blankline_text = "";
126 print "Usage: $0 [ -v ] [ -docbook | -html | -text | -man | -tex ]\n";
127 print " [ -function funcname [ -function funcname ...] ]\n";
128 print " c source file(s) > outputfile\n";
138 $output_mode = "man";
139 %highlights = %highlights_man;
140 $blankline = $blankline_man;
141 $modulename = "API Documentation";
143 while ($ARGV[0] =~ m/^-(.*)/) {
145 if ($cmd eq "-html") {
146 $output_mode = "html";
147 %highlights = %highlights_html;
148 $blankline = $blankline_html;
149 } elsif ($cmd eq "-man") {
150 $output_mode = "man";
151 %highlights = %highlights_man;
152 $blankline = $blankline_man;
153 } elsif ($cmd eq "-tex") {
154 $output_mode = "tex";
155 %highlights = %highlights_tex;
156 $blankline = $blankline_tex;
157 } elsif ($cmd eq "-text") {
158 $output_mode = "text";
159 %highlights = %highlights_text;
160 $blankline = $blankline_text;
161 } elsif ($cmd eq "-docbook") {
162 $output_mode = "sgml";
163 %highlights = %highlights_sgml;
164 $blankline = $blankline_sgml;
165 } elsif ($cmd eq "-module") { # not needed for sgml, inherits from calling document
166 $modulename = shift @ARGV;
167 } elsif ($cmd eq "-function") { # to only output specific functions
169 $function = shift @ARGV;
170 $function_table{$function} = 1;
171 } elsif ($cmd eq "-v") {
173 } elsif (($cmd eq "-h") || ($cmd eq "--help")) {
179 # generate a sequence of code that will splice in highlighting information
180 # using the s// operator.
182 foreach $pattern (keys %highlights) {
183 # print "scanning pattern $pattern ($highlights{$pattern})\n";
184 $dohighlight .= "\$contents =~ s:$pattern:$highlights{$pattern}:gs;\n";
188 # dumps section contents to arrays/hashes intended for that purpose.
192 my $contents = join "\n", @_;
194 if ($name =~ m/$type_constant/) {
196 # print STDERR "constant section '$1' = '$contents'\n";
197 $constants{$name} = $contents;
198 } elsif ($name =~ m/$type_param/) {
199 # print STDERR "parameter def '$1' = '$contents'\n";
201 $parameters{$name} = $contents;
203 # print STDERR "other section '$name' = '$contents'\n";
204 $sections{$name} = $contents;
205 push @sectionlist, $name;
212 # parameters, a hash.
213 # function => "function name"
214 # parameterlist => @list of parameters
215 # parameters => %parameter descriptions
216 # sectionlist => @list of sections
217 # sections => %descriont descriptions
220 sub output_highlight {
221 my $contents = join "\n", @_;
225 foreach $line (split "\n", $contents) {
227 print $lineprefix, $blankline;
229 print $lineprefix, $line;
239 my ($parameter, $section);
241 print "\n\n<a name=\"". $args{'function'} . "\"> </a><h2>Function</h2>\n";
243 print "<i>".$args{'functiontype'}."</i>\n";
244 print "<b>".$args{'function'}."</b>\n";
247 foreach $parameter (@{$args{'parameterlist'}}) {
248 print "<i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
249 if ($count != $#{$args{'parameterlist'}}) {
256 print "<h3>Arguments</h3>\n";
258 foreach $parameter (@{$args{'parameterlist'}}) {
259 print "<dt><i>".$args{'parametertypes'}{$parameter}."</i> <b>".$parameter."</b>\n";
261 output_highlight($args{'parameters'}{$parameter});
264 foreach $section (@{$args{'sectionlist'}}) {
265 print "<h3>$section</h3>\n";
267 output_highlight($args{'sections'}{$section});
276 my ($parameter, $section);
278 my $func = $args{'function'};
287 print "\n\n\\subsection{". $func . "}\n\\label{" . $args{'function'} . "}\n";
289 $type = $args{'functiontype'};
292 print "{\\it ".$type."}\n";
293 print "{\\bf ".$func."}\n";
296 foreach $parameter (@{$args{'parameterlist'}}) {
297 $param = $args{'parametertypes'}{$parameter};
298 $param2 = $parameter;
300 $param2 =~ s/_/\\_/g;
302 print "{\\it ".$param."} {\\bf ".$param2."}\n";
303 if ($count != $#{$args{'parameterlist'}}) {
310 print "\n{\\large{Arguments}}\n";
312 print "\\begin{itemize}\n";
314 foreach $parameter (@{$args{'parameterlist'}}) {
315 $param = $args{'parametertypes'}{$parameter};
317 $param2 = $parameter;
318 $param2 =~ s/_/\\_/g;
321 print "\\item {\\it ".$param."} {\\bf ".$param2."}\n";
324 output_highlight($param{$parameter});
327 print "\\item void\n";
329 print "\\end{itemize}\n";
331 foreach $section (@{$args{'sectionlist'}}) {
332 print "\n\\par{\\large{$section}}\\par\n";
333 print "\\begin{rmfamily}\n";
335 $sec = $args{'sections'}{$section};
338 output_highlight($sec);
339 print "\\end{rmfamily}\n";
345 # output in sgml DocBook
348 my ($parameter, $section);
352 $id = $args{'module'}."-".$args{'function'};
353 $id =~ s/[^A-Za-z0-9]/-/g;
355 print "<refentry>\n";
357 print "<refentrytitle><phrase id=\"$id\">".$args{'function'}."</phrase></refentrytitle>\n";
358 print "</refmeta>\n";
359 print "<refnamediv>\n";
360 print " <refname>".$args{'function'}."</refname>\n";
361 print " <refpurpose>\n";
362 print " ".$args{'purpose'}."\n";
363 print " </refpurpose>\n";
364 print "</refnamediv>\n";
366 print "<refsynopsisdiv>\n";
367 print " <title>Synopsis</title>\n";
368 print " <funcsynopsis>\n";
369 print " <funcdef>".$args{'functiontype'}." ";
370 print "<function>".$args{'function'}." ";
371 print "</function></funcdef>\n";
373 # print "<refsect1>\n";
374 # print " <title>Synopsis</title>\n";
375 # print " <funcsynopsis>\n";
376 # print " <funcdef>".$args{'functiontype'}." ";
377 # print "<function>".$args{'function'}." ";
378 # print "</function></funcdef>\n";
381 if ($#{$args{'parameterlist'}} >= 0) {
382 foreach $parameter (@{$args{'parameterlist'}}) {
383 print " <paramdef>".$args{'parametertypes'}{$parameter};
384 print " <parameter>$parameter</parameter></paramdef>\n";
389 print " </funcsynopsis>\n";
390 print "</refsynopsisdiv>\n";
391 # print "</refsect1>\n";
394 print "<refsect1>\n <title>Arguments</title>\n";
395 # print "<para>\nArguments\n";
396 if ($#{$args{'parameterlist'}} >= 0) {
397 print " <variablelist>\n";
398 foreach $parameter (@{$args{'parameterlist'}}) {
399 print " <varlistentry>\n <term><parameter>$parameter</parameter></term>\n";
400 print " <listitem>\n <para>\n";
402 output_highlight($args{'parameters'}{$parameter});
403 print " </para>\n </listitem>\n </varlistentry>\n";
405 print " </variablelist>\n";
407 print " <para>\n None\n </para>\n";
409 print "</refsect1>\n";
411 # print out each section
413 foreach $section (@{$args{'sectionlist'}}) {
414 print "<refsect1>\n <title>$section</title>\n <para>\n";
415 # print "<para>\n$section\n";
416 if ($section =~ m/EXAMPLE/i) {
417 print "<example><para>\n";
419 output_highlight($args{'sections'}{$section});
421 if ($section =~ m/EXAMPLE/i) {
422 print "</para></example>\n";
424 print " </para>\n</refsect1>\n";
434 my ($parameter, $section);
437 print ".TH \"$args{'module'}\" \"$args{'function'}\" \"25 May 1998\" \"API Manual\" GNOME\n";
439 print ".SH Function\n";
441 print ".I \"".$args{'functiontype'}."\"\n";
442 print ".B \"".$args{'function'}."\"\n";
445 foreach $parameter (@{$args{'parameterlist'}}) {
446 print ".I \"".$args{'parametertypes'}{$parameter}."\"\n.B \"".$parameter."\"\n";
447 if ($count != $#{$args{'parameterlist'}}) {
454 print ".SH Arguments\n";
455 foreach $parameter (@{$args{'parameterlist'}}) {
456 print ".IP \"".$args{'parametertypes'}{$parameter}." ".$parameter."\" 12\n";
457 output_highlight($args{'parameters'}{$parameter});
459 foreach $section (@{$args{'sectionlist'}}) {
460 print ".SH \"$section\"\n";
461 output_highlight($args{'sections'}{$section});
469 my ($parameter, $section);
471 print "Function = ".$args{'function'}."\n";
472 print " return type: ".$args{'functiontype'}."\n\n";
473 foreach $parameter (@{$args{'parameterlist'}}) {
474 print " ".$args{'parametertypes'}{$parameter}." ".$parameter."\n";
475 print " -> ".$args{'parameters'}{$parameter}."\n";
477 foreach $section (@{$args{'sectionlist'}}) {
478 print " $section:\n";
480 output_highlight($args{'sections'}{$section});
485 # generic output function - calls the right one based
486 # on current output mode.
487 sub output_function {
489 eval "output_".$output_mode."(\@_);";
494 # takes a function prototype and spits out all the details
495 # stored in the global arrays/hsahes.
497 my $prototype = shift @_;
499 if ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
500 $prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
501 $prototype =~ m/^(\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
502 $prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/ ||
503 $prototype =~ m/^(\w+\s+\w+\s*\*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\)]*)\)/) {
508 # print STDERR "ARGS = '$args'\n";
510 foreach $arg (split ',', $args) {
511 # strip leading/trailing spaces
514 # print STDERR "SCAN ARG: '$arg'\n";
515 @args = split('\s', $arg);
517 # print STDERR " -> @args\n";
519 # print STDERR " -> @args\n";
520 if ($param =~ m/^(\*+)(.*)/) {
524 $type = join " ", @args;
526 if ($parameters{$param} eq "") {
527 $parameters{$param} = "-- undescribed --";
528 print STDERR "Warning($lineno): Function parameter '$param' not described in '$function_name'\n";
531 push @parameterlist, $param;
532 $parametertypes{$param} = $type;
534 # print STDERR "param = '$param', type = '$type'\n";
537 print STDERR "Error($lineno): cannot understand prototype: '$prototype'\n";
541 if ($function_only==0 || defined($function_table{$function_name})) {
542 output_function({'function' => $function_name,
543 'module' => $modulename,
544 'functiontype' => $return_type,
545 'parameterlist' => \@parameterlist,
546 'parameters' => \%parameters,
547 'parametertypes' => \%parametertypes,
548 'sectionlist' => \@sectionlist,
549 'sections' => \%sections,
550 'purpose' => $function_purpose
555 ######################################################################
559 # 1 - looking for function name
560 # 2 - scanning field start.
561 # 3 - scanning prototype.
565 $doc_special = "\@\%\$\&";
567 $doc_start = "^/\\*\\*\$";
569 $doc_com = "\\s*\\*\\s*";
570 $doc_func = $doc_com."(\\w+):?";
571 $doc_sect = $doc_com."([".$doc_special."]?[\\w ]+):(.*)";
572 $doc_content = $doc_com."(.*)";
581 $section_default = "Description"; # default section
582 $section = $section_default;
585 foreach $file (@ARGV) {
586 if (!open(IN,"<$file")) {
587 print STDERR "Error: Cannot open file $file\n";
595 $state = 1; # next line is always the function name
597 } elsif ($state == 1) { # this line is the function name (always)
602 $function_purpose = $1;
604 $function_purpose = "";
607 print STDERR "Info($lineno): Scanning doc for $function\n";
610 print STDERR "WARN($lineno): Cannot understand $_ on line $lineno",
611 " - I thought it was a doc line\n";
614 } elsif ($state == 2) { # look for head: lines, and include content
619 if ($contents ne "") {
620 dump_section($section, $contents);
621 $section = $section_default;
624 $contents = $newcontents;
625 if ($contents ne "") {
628 $section = $newsection;
629 } elsif (/$doc_end/) {
631 if ($contents ne "") {
632 dump_section($section, $contents);
633 $section = $section_default;
637 # print STDERR "end of doc comment, looking for prototype\n";
640 } elsif (/$doc_content/) {
641 # miguel-style comment kludge, look for blank lines after
642 # @parameter line to signify start of description
643 if ($1 eq "" && $section =~ m/^@/) {
644 dump_section($section, $contents);
645 $section = $section_default;
648 $contents .= $1."\n";
651 # i dont know - bad line? ignore.
652 print STDERR "WARNING($lineno): bad line: $_";
654 } elsif ($state == 3) { # scanning for function { (end of prototype)
655 if (m#\s*/\*\s+MACDOC\s*#io) {
662 $prototype =~ s@/\*.*?\*/@@gos; # strip comments.
663 $prototype =~ s@[\r\n]+@ @gos; # strip newlines/cr's.
664 $prototype =~ s@^ +@@gos; # strip leading spaces
665 dump_function($prototype);
670 %parametertypes = ();