| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# Usage: |
| 4 |
# ./autodoc <C file> |
| 5 |
# ./autodoc -t <C file> |
| 6 |
# |
| 7 |
# Input is a C .c file with my structured function header convention. |
| 8 |
# Output is .tex file suitable for inclusion in my LaTeX documentation. |
| 9 |
# |
| 10 |
# Requirements: |
| 11 |
# 1. .tex file needs to have \sreapi environment defined; |
| 12 |
# see hmmer, squid, infernal, etc. "macro.tex" to get a copy. |
| 13 |
# |
| 14 |
# 2. .tex file needs to have \ccode command defined, to produce |
| 15 |
# courier (or whatever) computerese font for variables, macros, |
| 16 |
# etc. |
| 17 |
# |
| 18 |
# 3. Functions must use headers according to my conventions, for example: |
| 19 |
# (defined as sre-insert-my-function-header() in .emacs LISP) |
| 20 |
# |
| 21 |
# /* Function: function() |
| 22 |
# * Synopsis: A short (half-line) description. |
| 23 |
# * Incept: SRE, Tue Nov 30 19:36:17 2004 [St. Louis] |
| 24 |
# * |
| 25 |
# * Purpose: Given an <argument>, carry out an important |
| 26 |
# * function, and return an answer. |
| 27 |
# * |
| 28 |
# * Args: argument - some text, or NULL |
| 29 |
# * |
| 30 |
# * Returns: <SRE_SUCCESS> on success; <SRE_FAILED> on failure. |
| 31 |
# * |
| 32 |
# * Throws: <SRE_MALLOC_FAILED> on an allocation failure. |
| 33 |
# * |
| 34 |
# * Xref: STL8/p.159. |
| 35 |
# */ |
| 36 |
# int |
| 37 |
# function(char *argument) |
| 38 |
# { |
| 39 |
# etc. |
| 40 |
# |
| 41 |
# The Function and Purpose blocks are required. |
| 42 |
# Only the Function, Synopsis, Purpose, Returns, and Throws blocks are used. |
| 43 |
# |
| 44 |
# The exact spacing of the beginning of each line is important; |
| 45 |
# for example, the parser depends on seeing "/* Function" at the beginning, |
| 46 |
# " * \S" on a line starting a new block of info, and " * " or |
| 47 |
# " */" on a line that continues a previous info block. The exact |
| 48 |
# spacing style of "int\nfunction(char *argument)\n{" is also essential. |
| 49 |
# |
| 50 |
# |
| 51 |
# SRE, Tue Nov 30 19:43:47 2004 |
| 52 |
|
| 53 |
use Getopt::Std; |
| 54 |
|
| 55 |
&getopts('n:t'); |
| 56 |
$cfile = shift; |
| 57 |
|
| 58 |
if ($opt_t) { $show_api_table = 1; } |
| 59 |
if ($opt_n) { $modulename = $opt_n; } else { |
| 60 |
if ($cfile eq "easel") { $modulename = "easel"; } |
| 61 |
elsif ($cfile =~ /esl_(\S+)\.c/) { $modulename = $1; } |
| 62 |
else { $modulename = "foo"; } |
| 63 |
} |
| 64 |
|
| 65 |
$text = `cat $cfile`; |
| 66 |
$nfuncs = &parse_function_documentation($text); |
| 67 |
$nsections = &parse_api_subheads($text); |
| 68 |
|
| 69 |
if ($show_api_table) |
| 70 |
{ |
| 71 |
$j = -1; |
| 72 |
printf("%% Table generated by autodoc -t $cfile (so don't edit here, edit $cfile:)\n"); |
| 73 |
printf("\\begin{table}[hbp]\n"); |
| 74 |
printf("\\begin{center}\n"); |
| 75 |
printf("{\\small\n"); |
| 76 |
printf("\\begin{tabular}{|ll|}\\hline\n"); |
| 77 |
for ($i = 0; $i < $nfuncs; $i++) |
| 78 |
{ |
| 79 |
if ($insection{$function[$i]} != $j) |
| 80 |
{ |
| 81 |
$j = $insection{$function[$i]}; |
| 82 |
printf("\\apisubhead{%s}\\\\\n", $apisubheads[$j]); |
| 83 |
} |
| 84 |
|
| 85 |
if ($synopsis[$i] ne "") { |
| 86 |
printf("\\hyperlink{func:%s()}{\\ccode{%s()}} & %s\\\\\n", |
| 87 |
$function[$i], $pfunction[$i], $synopsis[$i]); |
| 88 |
} else { |
| 89 |
printf("\\hyperlink{func:%s()}{\\ccode{%s()}} & [Description]\\\\\n", |
| 90 |
$function[$i], $pfunction[$i]); |
| 91 |
} |
| 92 |
} |
| 93 |
printf("\\hline\n"); |
| 94 |
printf("\\end{tabular}\n"); |
| 95 |
printf("}\n"); |
| 96 |
printf("\\end{center}\n"); |
| 97 |
printf("\\caption{The \\eslmod{%s} API.}\n", $modulename); |
| 98 |
printf("\\label{tbl:%s_api}\n", $modulename); |
| 99 |
printf("\\end{table}\n"); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
print "\\begin{sreapi}\n"; |
| 104 |
for ($i = 0; $i < $nfuncs; $i++) |
| 105 |
{ |
| 106 |
printf("\\hypertarget{func:%s()}\n{", $function[$i]); |
| 107 |
printf "\\item[%s %s(%s)]", |
| 108 |
$returntype[$i], |
| 109 |
$pfunction[$i], |
| 110 |
$args[$i]; |
| 111 |
printf("}\n"); |
| 112 |
|
| 113 |
|
| 114 |
print "\n"; |
| 115 |
print $purpose[$i], "\n"; |
| 116 |
print "Returns ", $returns[$i], "\n" unless ($returns[$i] eq ""); |
| 117 |
print "Throws ", $throws[$i], "\n" unless ($throws[$i] eq ""); |
| 118 |
print "\n\n"; |
| 119 |
} |
| 120 |
print "\\end{sreapi}\n\n"; |
| 121 |
} |
| 122 |
exit; |
| 123 |
|
| 124 |
# Function: parse_function_documentation |
| 125 |
# |
| 126 |
# Purpose: Given <text> from a .c file, parse it for my structured |
| 127 |
# function documentation. Returns <n>, the number of |
| 128 |
# documented functions found; and it populates the following |
| 129 |
# global arrays: |
| 130 |
# function - verbatim name of the function |
| 131 |
# pfunction - name of the function, protected for LaTeX (\_ not _, for example) |
| 132 |
# returntype - C return type; for example, "int" |
| 133 |
# args - C argument list, from the func def'n |
| 134 |
# purpose - Text documenting the function |
| 135 |
# synopsis - OPTIONAL: short half-line description, else "" |
| 136 |
# incept - OPTIONAL: date/name/place, else "" |
| 137 |
# argdocs - OPTIONAL: table documenting the args, else "" |
| 138 |
# returns - OPTIONAL: documentation of returned |
| 139 |
# information or codes; else ""; |
| 140 |
# throws - OPTIONAL: documentation of returned |
| 141 |
# abnormal error codes |
| 142 |
# |
| 143 |
# Each of these is an array indexed <0..n-1>, for the <n> |
| 144 |
# documented functions. |
| 145 |
# |
| 146 |
sub |
| 147 |
parse_function_documentation |
| 148 |
{ |
| 149 |
my ($text) = @_; |
| 150 |
my ($comment, $n, $first_funcname); |
| 151 |
|
| 152 |
$n = 0; |
| 153 |
# /* Function: text \n double \n foo ( args ) \n{ |
| 154 |
while ($text =~ m|(/\*\s*Function:\s*.+?\*/)\s*\n(.+?)\s*\n(\S+)\((.+?)\)\s*\n\{|gms) { |
| 155 |
$comment = $1; |
| 156 |
$returntype[$n] = $2; |
| 157 |
$function[$n] = $3; |
| 158 |
$args[$n] = $4; |
| 159 |
|
| 160 |
# Delimit end of each block in the comments with a \n@*, for |
| 161 |
# convenience in getting out the individual blocks. |
| 162 |
$comment =~ s|\n \* (\S)|\n@\* \1|g; |
| 163 |
$comment =~ s|\n \*/|\n@\*/|g; |
| 164 |
|
| 165 |
# Remove leading comment symbols and spacing. |
| 166 |
$comment =~ s|\n[ \t]*\*[ \t]*|\n|g; |
| 167 |
|
| 168 |
|
| 169 |
# Now, grab all the individual blocks of info from a structured |
| 170 |
# function header comment. Required fields: |
| 171 |
# Function: |
| 172 |
# Purpose: |
| 173 |
# the function and its args. |
| 174 |
# |
| 175 |
if ($comment =~ m|/\* Function:\s*(.+?)\n@\*|ms) { $first_funcname = $1; } |
| 176 |
else {next;} |
| 177 |
if ($first_funcname =~ /^(\S+)\(\)/) { $first_funcname = $1; } |
| 178 |
if ($first_funcname ne $function[$n]) { die "parse error; $first_funcname != $function[$n]";} |
| 179 |
|
| 180 |
if ($comment =~ m|\n@\* Synopsis:\s*(.+?)\n|ms) { $synopsis[$n] = &process_comment_text($1); } |
| 181 |
else {$synopsis[$n] = ""; } |
| 182 |
|
| 183 |
if ($comment =~ m|\n@\* Incept:\s*(.+?)\n|ms) { $incept[$n] = &process_comment_text($1); } |
| 184 |
else {$incept[$n] = ""; } |
| 185 |
|
| 186 |
if ($comment =~ m|\n@\* Purpose:\s*(.+?)\n@\*|ms) { $purpose[$n] = &process_comment_text($1); } |
| 187 |
else {next;} |
| 188 |
|
| 189 |
if ($comment =~ m|\n@\* Args:\s*(.+?)\n@\*|ms) { $argdocs[$n] = $1; } |
| 190 |
else {$argdocs = ""; } |
| 191 |
|
| 192 |
if ($comment =~ m|\n@\* Returns:\s*(.+?)\n@\*|ms) { $returns[$n] = &process_comment_text($1); } |
| 193 |
else {$returns[$n] = ""; } |
| 194 |
|
| 195 |
if ($comment =~ m|\n@\* Throws:\s*(.+?)\n@\*|ms) { $throws[$n] = &process_comment_text($1); } |
| 196 |
else {$throws[$n] = ""; } |
| 197 |
|
| 198 |
# protect _ characters. $function contains original name, for use as |
| 199 |
# hypertarget tag; $pfunction contains the protected function name, for output. |
| 200 |
$pfunction[$n] = &latex_safe($function[$n]); |
| 201 |
$returntype[$n] = &latex_safe($returntype[$n]); |
| 202 |
$args[$n] = &latex_safe($args[$n]); |
| 203 |
|
| 204 |
$n++; |
| 205 |
} |
| 206 |
$n; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
# Function: process_comment_text |
| 211 |
# |
| 212 |
sub |
| 213 |
process_comment_text |
| 214 |
{ |
| 215 |
my ($text) = @_; |
| 216 |
my (@s); # old text, as chars |
| 217 |
my (@s2); # new text, as chars |
| 218 |
my ($newtext); |
| 219 |
my ($n); |
| 220 |
my ($i); |
| 221 |
my ($state); # 0 = text; 1 = math; 2 = code. Finite automaton. |
| 222 |
|
| 223 |
@s = split(//,$text); |
| 224 |
$n = $#s + 1; |
| 225 |
|
| 226 |
$state = 0; # start in text state |
| 227 |
for ($i = 0; $i < $n; $i++) |
| 228 |
{ |
| 229 |
# State transitions in the text/math/code mode automaton |
| 230 |
# |
| 231 |
if ($state == 0 && $s[$i] eq '$') { $state = 1; push(@s2, '$'); } # enter math state |
| 232 |
elsif ($state == 0 && $s[$i] eq '<') { $state = 2; push(@s2, split(//, "\\ccode{")); } # enter code state |
| 233 |
elsif ($state == 1 && $s[$i] eq '$') { $state = 0; push(@s2, '$'); } # back to text state |
| 234 |
elsif ($state == 2 && $s[$i] eq '>' && $s[$i-1] ne '-') |
| 235 |
{ $state = 0; push(@s2, '}'); } # back to text state on >, unless it was -> |
| 236 |
|
| 237 |
# No state transition; deal with processing other characters according to state. |
| 238 |
# |
| 239 |
elsif ($state == 2 && $s[$i] eq '_') { push(@s2, '\\'); push(@s2, '_'); } |
| 240 |
elsif ($state == 2 && $s[$i] eq '%') { push(@s2, '\\'); push(@s2, '%'); } |
| 241 |
else { push(@s2, $s[$i]); } |
| 242 |
} |
| 243 |
|
| 244 |
$newtext = join('',@s2); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
# Function: latex_safe |
| 249 |
# |
| 250 |
# Purpose: Given a <string>, substitute any unacceptable characters |
| 251 |
# for LaTeX, as follows: |
| 252 |
# _ becomes \_ |
| 253 |
# % becomes \% |
| 254 |
# # becomes \# |
| 255 |
# |
| 256 |
sub |
| 257 |
latex_safe |
| 258 |
{ |
| 259 |
my ($string) = @_; |
| 260 |
|
| 261 |
$string =~ s/_/\\_/g; |
| 262 |
$string =~ s/%/\\%/g; |
| 263 |
$string =~ s/#/\\#/g; |
| 264 |
$string; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
# Function: parse_api_subheads |
| 269 |
# |
| 270 |
# Purpose: Given <text> from a .c file, parse it |
| 271 |
# for structured API subheading documentation: |
| 272 |
# |
| 273 |
# /******************************************** |
| 274 |
# *# 1. The <ESL_RANDOMNESS> object. |
| 275 |
# ********************************************/ |
| 276 |
# |
| 277 |
# Keys off of the /***** line, followed by a line |
| 278 |
# starting with " *#" followed by a number and a period; |
| 279 |
# the rest of the line is taken to be the title. |
| 280 |
# |
| 281 |
# The title is processed, so <> and math mode are allowed. |
| 282 |
# |
| 283 |
# Returns n, the number of subheadings found; |
| 284 |
# and it populates two global arrays: |
| 285 |
# apisubheads<0..n-1> : one-line section subheadings |
| 286 |
# insection{$funcname} : which api subhead $funcname is under |
| 287 |
# |
| 288 |
sub |
| 289 |
parse_api_subheads |
| 290 |
{ |
| 291 |
my ($text) = @_; |
| 292 |
my (@lines) = split(/^/, $text); |
| 293 |
|
| 294 |
$n = 0; |
| 295 |
$armed = 0; |
| 296 |
foreach $line (@lines) |
| 297 |
{ |
| 298 |
if ($armed && $line =~ /^ \*\#\s*\d+\.\s*(.+)$/) |
| 299 |
{ |
| 300 |
$apisubheads[$n] = &process_comment_text($1); |
| 301 |
$n++; |
| 302 |
$armed = 0; |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
if ($line =~ /^\/\* Function:\s*(.+)\(\)/) |
| 307 |
{ |
| 308 |
$insection{$1} = $n-1; |
| 309 |
} |
| 310 |
|
| 311 |
if ($line =~ /^\/\*{12}/) { $armed = 1; } # can expect a subhead line next, |
| 312 |
else { $armed = 0; } # or not. |
| 313 |
} |
| 314 |
$n; |
| 315 |
} |