package APIdict; # # Package to deal with API documentation defined by entries in a flat # editable file. # # Edit history: # # 2001-07-19 Ken Coar # Add the ability to write the dictionary (sans comments) to # an XML file. The DTD to which we are hewing is still # unpublished. # # 2001-06-19 Ken Coar # Added some documentation, and added xref-counts to the stats result. # # 2001-05-04 Ken Coar # Add 'hyperlink' Boolean setting. # # 2000-09-15 Ken Coar # Pull the relevant parts out of the mkapidict script so that # other tools can use them (like a source scanner). # # This is the first Perl 'module' I've written, so be gentle.. # use HTML::Entities; use POSIX; # # Public methods # $hyperlinks = 1; $embed = 1; # # Package constructor # # $dict = new APIdict [('dictionary-file')]; # sub new { my($self) = bless {}, shift; $self->init(@_); $self; } # # All other public methods appear in lexical order. # # # Replace $a_text with whatever to which it links. sub insert_link { my($self, $a_text, $prefix, $postfix, $a_target, $ml) = @_; my($ref); my($xml) = ($ml =~ /xml/i); # # This is an incredibly brute-force O(n^2) algorithm, but at least it's # clear and only run when updating the docco. # if (! $a_target) { $a_target = $a_text; } if ($hyperlinks) { if ($xml) { $ref = "$a_text"; } else { $ref = "$prefix$a_text$postfix"; } } else { $ref = "$prefix$a_text$postfix"; } return $ref; } # # Turn any inline references to other entities into links. # sub add_links { my($self, $wip, $rname, $a_prefix, $a_postfix, $ml, %HREF) = @_; my($ref); # # This is an incredibly brute-force O(n^2) algorithm, but at least it's # clear and only run when updating the docco. # foreach $ref (keys(%Entity)) { local($href) = $HREF{"$ref"}; $href = ($href ? "$href" : "apidoc_$ref.html"); if ($ref ne $rname) { $nref = &insert_link($self, $ref, $prefix, $postfix, $href, $ml); $wip =~ s:([^-); close DFILE; } else { $Description{$field[1]} = ""; } } } } } # # Return statistics about the dictionary. The return value is an # associative array with the following keys: # # "entries" # "routines" # "cells" # "lines" # "macros" # "structures" # "constants" # "reference_lists" # sub statistics { my(@counter, %stats) = (); @counter = keys(%Entity); $stats{"entries"} = $#counter + 1; @counter = keys(%Macro); $stats{"macros"} = $#counter + 1; @counter = keys(%Constant); $stats{"constants"} = $#counter + 1; @counter = keys(%Routine); $stats{"routines"} = $#counter + 1; @counter = keys(%Cell); $stats{"cells"} = $#counter + 1; @counter = keys(%Structure); $stats{"structures"} = $#counter + 1; @counter = keys(%Xref); $stats{"reference_lists"} = $#counter + 1; $stats{"lines"} = $#Dictionary + 1; return %stats; } # # Perform repair editing on a description # sub repair_desc { my($self, $desc) = @_; my($last) = $desc . " "; my($count) = 0; my($i, $inpara); my(@paras, @in, @out, $line); $desc = "" . encode_entities(decode_entities($desc)) . ""; # # Convert the newlines to something that does not have special RE # significance. # $desc =~ s:\n:\001:gs; $desc =~ s:^\s+::g; $desc =~ s:\s+$::g; $desc =~ s:\s+\001:\001:g; # # Normalise the para tags to the start of the line. # $desc =~ s:():\001$1\001:gsi; $desc =~ s:():\001$1\001:gsi; # # Get rid of blanks.. # $desc =~ s:\001\s*\001:\001:g; # # Turn

s into s # $desc =~ s:\<([/]*)[Pp]\>:<${1}para>:gs; # # Now walk through the lines. # $inpara = 0; @in = split(m:\001:, $desc); foreach (@in) { if (! $_) { next; } if (m::) { if ($inpara) { push(@out, ""); } $inpara = 1; push(@out, $_); } elsif (m::) { if (! $inpara) { # # Uh, what to do? # push(@out, ""); } $inpara = 0; push(@out, $_); } else { if (! $inpara) { push(@out, ""); $inpara = 1; } push(@out, $_); } } $desc = join("\001", @out); # # Get rid of blanks.. # $desc =~ s:\001::gsi; $desc =~ s:^[\001\s]*::gs; $desc =~ s:[\001\s]*$::gs; while ($desc =~ m:\001\s*\001:) { $desc =~ s:\001\s*\001:\001:gsi; } # # Indent the tags. # $desc =~ s:: :gs; $desc =~ s:: :gs; # # Restore the newlines. # $desc =~ s:\001:\n:gs; return $desc; } # # Format an entry for output, in either our custom format or XML. # sub format_entry { my($self, $item, $format) = @_; my($record); if ($format !~ /xml/i) { $record = "$rtype|$item\\\n"; $record .= " |" . $Definition{$item} . "\\\n"; $record .= " |" . $Example{$item} . "\\\n"; $record .= " |" . $SeeAlso{$item} . "\\\n"; $record .= " |" . ($Description_file{$item} || $Description{$item}) . "\n"; } else { my($sealso, $decl, $desc, $example, $id); my(@list); $id = ($item =~ m:^\w+$:) ? $item : &make_item($self, substr($item, 0, 8), "ITM"); if ($Definition{$item}) { my($val) = encode_entities(decode_entities($Definition{$item})); if ($Rtype{$item} =~ m:X:i) { @list = split(m:,\s*:, $val); $val = join("\n ", @list); $val = "\n $val\n "; } else { $val = &add_links($self, $val, $item, "", "", "xml", %Name); } $decl = " " . $val . "\n"; } else { $decl = " \n"; } $desc_pre = " \n"; } elsif ($Description{$item}) { $desc_pre .= ">\n "; $desc_post = "\n \n"; # # Enclose the description in # $desc = $Description{$item}; # # Now start repairs.. # $desc = repair_desc($self, $desc); $desc = &add_links($self, $desc, "\001", "", "", "xml", %Name); } else { $desc_pre .= " />\n"; } } $example = ""; if ($Example{$item}) { $example = " \n " . encode_entities(decode_entities($Example{$item})) . "\n \n"; } $seealso = ""; if ($SeeAlso{$item}) { @list = split(/,\s*/, $SeeAlso{$item}); $seealso = join("\n ", @list); $seealso = " \n" . " $seealso\n" . " \n"; } $record = " \n" . " $item\n" . " \n" . $decl . $desc_pre . $desc . $desc_post . $example . $seealso; $record .= " \n" . " \n"; } return $record; } # # Make a backup of the specified output file (if it exists), and # write our current dictionary into the original file. # sub writefile { my($self, $outfile) = @_; my($xml); $outfile = $DictFile if (! $outfile); $xml = ($outfile =~ m:\.xml$:i); &backup($outfile); open *NEWDICT, ">${outfile}" || die("Can't open new dictionary file"); if ($xml) { print NEWDICT "\n" . "\n" . "\n" . "\n"; foreach (sort(keys(%Entity))) { print NEWDICT &format_entry($self, $_, "xml"); } } else { foreach (@Dictionary) { print NEWDICT $_; } } if ($dirty) { $msg = "Definitions added by APIdict at " . strftime("%Y-%m-%d %H:%M", localtime()); if ($xml) { $msg = "\n $msg\n\n"; } else { $msg = "#;\n#; $msg\n#;\n"; } print NEWDICT $msg; foreach $entry (sort(@uncommitted)) { my($rtype, $item) = split(m:=:, $entry); my($record); $record = &format_entry($self, $item, ($xml ? "xml" : "custom")); print NEWDICT $record; if ($xml) { $record = &format_entry($self, $item, "custom"); } push(@Dictionary, $record); } $dirty = 0; @uncommitted = (); } if ($xml) { print NEWDICT "\n"; } close *NEWDICT; return 0; } # # Private methods # # # Make a backup of the specified file by renaming it to *.1. If # a *.1 file exists, rename *that* to *.2 and so on, until we can # move the original one to *.1. # sub backup { my($filename) = @_; my($index, $newindex, $basename) = ("", 1, $filename); $filename =~ /(.*)\.([0-9]+$)/; ($basename, $index) = ($1, $2); if ($index) { $newindex = $index + 1; } else { $basename = $filename; } if (-e "$basename.$newindex") { &backup("$basename.$newindex"); } rename($filename, "$basename.$newindex"); } sub err_is_defined { my($item) = @_; print STDERR "Item $item is already defined!\n"; return 0; } # # Initialiser method. Read in the specified file (if one # was mentioned) and create the associative arrays and package-private # data cells. # sub init { my($self, @args) = @_; $dirty = 0; # Whether the dict has changed since read @uncommitted = (); # List of items added @Dictionary = (); # Raw text read from the dictionary file $DictFile = ""; # Name of dictionary file (for checkpoints) %Prefix = ('R' => 'Routine', 'M' => 'Macro', 'C' => 'Constant', 'S' => 'Structure', 'X' => 'Cross-reference', 'D' => 'Global Data Cell'); $prefix_chars = join("", keys(%Prefix)); %Rtype = (); %Entity = (); # Array of Booleans for name-is-defined tests %Name = (); %Routine = (); # Array of routine names %Macro = (); # Array of macro names %Callable = (); # Array of active items (routines + macros) %Constant = (); # Array of constant names %Structure = (); # Array of structure names %Cell = (); # Array of names of global data cells %Xref = (); # Array of related-names lists %Example = (); # Array of examples for entities %Definition = (); # Array of item definitions %rDefinition = (); # Reverse-map definition to item name %Description = (); # Item descriptions %Description_file = (); # Names of description files %SeeAlso = (); # Array of references for items if (@args) { return &readfile(@_); } return 1; } # # Private method to add an entry to the dictionary. # sub make_entry { my($rtype, $item, $def, $example, $xref, $desc) = @_; if ($Entity{$item}) { return &err_is_defined($item); } $item = &sanitise($item); $def = &sanitise($item, $def); $example = &sanitise($item, $example); $xref = &sanitise($item, $xref); $desc = &sanitise($item, $desc); $Definition{$item} = $def; $rDefinition{$def} = $item; $Example{$item} = $example; $SeeAlso{$item} = $xref; $Description{$item} = ""; if ($desc =~ /^<(.*)/) { $Description_file{$item} = $desc; $dfile = $1; if (-r $dfile) { if (open *DFILE, $desc) { my(@lines) = ; close DFILE; $Description{$item} = join("", @lines); } } } else { $Description{$item} = $desc; $Description_file{$item} = ""; } $desc = ($Description_file{$item} ? $Description_file{$item} : $Description{$item}); $rec = "$rtype|$item|$def|$example|$xref|desc"; $Rtype{$item} = $rtype; if ($rtype eq 'R') { $Routine{$item} = $rec; } elsif ($rtype eq 'M') { $Macro{$item} = $rec; } elsif ($rtype eq 'C') { $Constant{$item} = $rec; } elsif ($rtype eq 'S') { $Structure{$item} = $rec; } elsif ($rtype eq 'X') { $Xref{$item} = $rec; } elsif ($rtype eq 'D') { $Cell{$item} = $rec; } $Entity{$item} = 1; push(@uncommitted, "$rtype=$item"); $dirty++; } # # Check the argument to see if it can be expanded to any additional # cross-references. # sub references { my($ref) = @_; my($key, @list, @result) = (); $key = $Xref{$ref}; if (! $key) { return $ref; } @list = split(m:,:, $key); foreach (@list) { push(@result, &references($_)); } return @result; } # # Do any necessary substitutions, such as replacing '$*' with the item name # and '|' with '|'. # sub sanitise { local($item, $_) = @_; if (! &defined(0, $_)) { $_ = $item; } else { s:\$\*:$item:g; } s:\|:\|:g; return $_; } # # Read a record from the input stream. Comments are trimmed, as is # trailing whitespace. Leading whitespace is trimmed IFF the argument to the # functional evaluates as true. A terminal slosh indicates that the next # line should be read and the record re-processed. All '\n' occurrences # in the input are converted to newlines before being returned. # sub get_record { local ($trim_leading_ws, $continued) = (@_, 0); local ($trim_next) = 1; my ($line); $line = || return ""; push(@Dictionary, $line); $DataLine++; chomp ($line); $line =~ s/#;.*//; $line =~ s/\s+$//; if ($trim_leading_ws) { $line =~ s/^\s+//; } if ($line =~ m:\\$:) { $line =~ s:\\n\s+\\$:\\n\\:; $line =~ s:\\$::; $trim_next = 0 if ($line =~ m:\\n$:); $line .= &get_record($trim_next, 1); } if ($line && (! $continued)) { $RecordNum++; $line =~ s:([^\\])\\n:$1\n:g; $line =~ s:\\\\:\\:g; } return ($line); } sub verbose { my(@lines) = @_; my($suffix); if ($Verbose) { foreach (@lines) { if (m:\\c$:) { $suffix = ""; s:\\c$::; } else { $suffix = "\n"; } print STDERR $_, $suffix; } } return 1; } sub debug { my(@lines) = @_; my($suffix); if ($Debug) { foreach (@lines) { if (m:\\c$:) { $suffix = ""; s:\\c$::; } else { $suffix = "\n"; } print STDERR $_, $suffix; } } return 1; } # # {Sigh} Modules must have values, and this is ours. # 1;