« Cost reductions, part II | Main | Mushroom »

August 19, 2004

Homemade perl obfuscation

Dennis hasn't finished SolBook 3.0 support for the man command in Solaris 10, yet, but we do have support for SolBook 3.0 RefEntrys inside books. Before I realized that I'd written some RefEntrys in SB 3. Luckily, not much has changed. Olinks got all changed around. (SB 3 has gone to olinks for everything. Very cool.)

The Perl obfuscations you can come up with just processing this stuff are hilarious:

#!/bin/perl -w
# ---------------------------------------------------------
# SolBook 3 is not yet supported by the Solaris man command.
# This script converts the DSRK RefEntrys to SolBook 2.
# This script is *NOT* a general conversion tool.
# Dennis is working on bug 5088606 to fix this.
# ---------------------------------------------------------
 
use Getopt::Std;
getopts("d:");
unless (defined($opt_d)) { die "Usage: " . $0 . " -d <sman1-dir>\n"; }
if (!grep(/\/$/,$opt_d)) { $opt_d = $opt_d . "/"; }
 
opendir(DIR, "$opt_d") || die "Cannot open $opt_d: $!";
@pages = grep(/\.1$/,readdir(DIR));
closedir(DIR);
 
foreach $page (@pages) {
    open(CURR_MANPAGE, $opt_d . $page) || die "Cannot open $page: $!";
    @lines = <CURR_MANPAGE>;
    close(CURR_MANPAGE);
 
    $file = join("", @lines);
    $file =~ s/\n/#~#/g;                # Replace newlines with #~#
     
    # Handle comments, copyright, and processing instructions
    $wcwq = "Hold back the edges of your gowns, Ladies...";
    $file =~ s/<!--.*-->/<!-- $wcwq -->/g;
    $copyright = "Copyright 2005, Sun Microsystems, Inc. All Rights Reserved.";
    $file =~ s/(class\=\"copyright\">).*?(<\/refmiscinfo>)/$1$copyright$2/;
    $file =~ s/<\?.*?>//g; # Probably not necessary
     
    # Handle olinks, which differ significantly from 2 to 3...
    # ... mindless attribute translations...
    $file =~ s/(targetptr\=\")(.*?)(\">)/localinfo\=\"$2$3/g;
    $file =~ s/(targetdoc\=\")(.*?)(\">)/targetdocent\=\"$2$3/g;
    $file =~ s/(remap\=\")(.*?)(\")//g;
    $file =~ s/(type\=\"auto)(.*?)(\")//g;
    # ... $wcwq
    $target = "targetdocent\=\"SUNWDSRKTR\""; # Could we use "." as the value?
    $cite1 = "<citerefentry><refentrytitle>";
    $cite2 = "<\/refentrytitle><manvolnum>";
    $cite3 = "<\/manvolnum><\/citerefentry>";
    $file =~ s/(<olink)[ ]*(localinfo\=\")([-a-z]*?)-1(\">)(<\/olink>)/$1
$target $2$3-1$4$cite1$3$cite2 1 $cite3$5/g; # Dude, are you obfuscated yet?
    $file =~ s/> 1 </>1</g;              # Yes, I hardcoded section 1.
    $file =~ s/(<olink)[ #~]*(localinfo\=\")(.*?)(\">)(.*?)(<\/olink>)/$1
$target $2$3$4$5$6/g;
     
    $file =~ s/#~#/\n/g;                 # Replace #~# with newlines
     
    open(NEW_CONTENT, ">" . $opt_d . $page) || die "Cannot write $page: $!";
    # Fix the doctype declaration, using entities for hand-made .ent files
    print NEW_CONTENT<<HEAD;
<!DOCTYPE REFENTRY PUBLIC "-//Sun Microsystems//DTD DocBook V3.0-Based SolBook Subset V2.0//EN" [
<!ENTITY % commonents SYSTEM "smancommon.ent">
%commonents;
<!ENTITY % booktitles SYSTEM "booktitles.ent">
%booktitles;
]>
HEAD
    print NEW_CONTENT $file;
    close(NEW_CONTENT);
}

Makes even a tech writer's work look complex and secretly meaningful, doesn't it?

Posted by Mark at August 19, 2004 02:26 PM