.\" macros
.de Fm
formatted manual pages\\$1
..
.de Um
unformatted manual pages\\$1
..
.\"
.\" fiddle -ms parameters
.nr PS 12
.ps 12
.nr VS 14
.vs 14
.\"
.TL
nam:
A Manual Page Decompiler
.br
(extracting structure from
.Fm )
.\" title when rejected by Usenix for Nashville:
.\" Manual Pages:
.\" Reviving a Declining Medium of Communication
.AU
Geoff Collyer
.AI
Software Tool & Die
1330 Beacon St. #215
Brookline, MA 02146
geoff@world.std.com
geoff@uunet.uu.net
.AB
.PP
Now that
.SM
AT&T
.NL
is charging a small fortune for
manual page source,
fewer vendors of
.\" bloody utzoo -ms!
.\" .UX
.SM
UNIX
.NL
systems
ship manual page source,
or do so only at exorbitant cost.
As a result,
even sites that
pay for program source code
rarely have manual page source.
Consequently,
even sites with source code
now often have out-of-sync
manual pages,
.\" this bit is important
and this form of one communications medium
(structured text)
is in decline.
.PP
We describe one somewhat bizarre solution to this unacceptable
state of affairs,
.\" this bit is important
which we hope will help resuscitate manual pages:
decompiling or reverse-engineering
manual pages;
that is,
extracting structure from
.Fm .
.AE
.SH
Introduction
.PP
Once upon a time (1979 A.D.),
.UX
systems came with
full on-line source code
(yes, even for the compilers),
full on-line documents
(in
.I /usr/doc ),
and
complete on-line manual pages
(in
.I /usr/man ).
System administrators,
when they changed a program,
would update the on-line
(unformatted)
manual page,
thereby keeping the documentation up-to-date.
Ever since then,
vendors
(notably
.SM
AT&T\c
.NL
)
have apparently been fighting for the honour of
depriving users of as many of these valuable tools as possible.
First they stopped shipping source code
(except to universities or the very rich),
then stopped shipping on-line documents
(presumably to encourage users to pay for
un-grep-able
printed manuals)
and mostly recently
stopped shipping unformatted versions
of the on-line manual pages,
except to the very rich
(at about the same time,
they stopped shipping the formatting programs needed to
read the on-line manuals,
except to the very rich,
but this has been remedied).
.[
%T Awk As A Major Systems Programming Language
%A Henry Spencer
%J Proc. Winter Usenix Conf. Dallas 1991
%D January 1991
%K awk system programming language
.]
.\" TODO: fill in page numbers in the above (%P) after Usenix
Watching this sorry progression (sic),
one can only assume that they intend to stop shipping all forms
of on-line documentation eventually
(or,
equivalently,
price them out of range of all but the rich);
let us hope that the howls of outrage from the user community
would be too great to permit such a violation of trust.
.PP
Though often maligned by those who don't understand their design,
.UX
manual pages are a model of good structured technical documentation.
When written well
(as they were originally),
they form a precise,
concise,
highly-structured
database of documentation.
Unfortunately,
they assume a certain amount of
intelligence and taste in the reader
(and the writer!)
and thus have not found wide acceptance in the world at large.
(In particular,
one could note that a 90-page
``manual page'' is a
.I book ,
not a manual page,
no matter what the author says.)
.PP
As
.SM
AT&T\c
.NL
\&'s
System V
licences make manual page source
(\c
.I troff
input)
increasingly expensive,
increasingly fewer vendors of
.UX
systems
ship manual page source,
or do so only at exorbitant cost.
This means that even sites that
(quite sensibly)
insist on paying for program source code
as a vital maintenance tool
don't always have manual page source,
since it is disproportionately costly
and is perceived by many holding purse-strings as unnecessary,
since
``manual pages never change and no one ever reads them anyway\(dd''.
.FS
\(dd this is known in psychoanalytic circles as ``projection''.
.FE
As a result,
even administrators of sites with source code
now find themselves unable to update the on-line manuals when
they change a program.
The resulting cognitive dissonance
(the manuals say one thing,
the programs act another)
is stressful for the users and
even more for the poor administrator,
who often becomes demoralised
and stops keeping up old standards of maintenance.
.PP
There is now a solution to this problem.
.SH
Initial Attack
.PP
We observed that
.Fm
(the output of
.B "nroff \-man" )
are in a fairly rigid format
and that
.Um
format
is fairly stereotyped too.
Our first thought was to write a
.I yacc
grammar to describe the
format of
.Fm
(to allow for flexibility and easy experimentation 
parsing text formatted with somewhat variable parameters)
and emit equivalent
.Um .
.PP
We next observed that although the sizes of various margins,
indentations and the like vary from vendor to vendor,
the actual
.Fm
format is rigid enough that it can be parsed fairly easily
without the help of
.I yacc .
.PP
The first obvious hurdle was dealing with overstruck characters
produced by
.I nroff .
Though a nuisance to cope with,
they encode font change information.
This problem is independent of macro package
and tricky enough that we solved it by writing a small filter
(a few pages long),
called
.\" TODO: verify this name at the last minute
.I fontch ,
that translates sequences of overstruck characters
into
.I troff
special characters
(\c
.I \e(xx ),
font changes
and non-overstruck characters.
Thus
``NAME''
(suitably overstruck)
becomes
``\efBNAME\efR''.
This does mean that the next stage of processing
must be prepared to deal with
.I troff
escape sequences,
but this turned out to be only a minor nuisance.
.I Fontch
is a classical
.UX
filter:
it attacks a specialised problem thoroughly
and
can be usefully combined with other tools
to multiply their power\(dg.
.FS
\(dg I believe the trendy management buzzword is ``leverage'',
which is apparently at least a noun and a verb.
.FE
.SH
The Hard Part
.PP
This left the problem of
intuiting the original form from the
.Fm .
It is not possible to do so perfectly
since there are cases of
formatted output
that could have been produced from two or more
possible forms of
unformatted input.
On the other hand,
there are various heuristics that can significantly improve
the chances of producing the correct form.
Unfortunately,
the utility of these heuristics correlates highly with their
difficulty of implementation,
so
at some point
one simply has to declare
`this is good enough'.
Further unfortunately,
some of the real-life examples one wants to decompile
require heroic efforts
(e.g. Hewlett-Packard's Xw Widgets manual pages)
due to violations of the
.UX
manual page conventions.
.PP
The process of writing the decompiler,
called
.\" TODO: verify this name at the last minute
.I nam ,
involved exploring the problem by writing code,
running it on existing
.I \-man
macros and
.Fm ,
in a feedback loop.
This iterative evolution has produced a program that is
not as sharp nor as well-structured
as it would have been if the problem had
been better understood and specified at the outset.
On the other hand,
it lints cleanly,
works
and is surprisingly small
(~800 lines,
slightly larger than the
.SM
GNU
.NL
.I cat ).
.PP
.I Nam
parses in a somewhat ad-hoc manner.
It initially reads the first page into memory,
working its way back from the bottom of the page
to find the footer line;
this lets it recognise footers in future
by their position on the page.
It then backs up to the start of the page,
skips empty lines until it sees the header line,
and parses the header line to produce a
.B \&.TH
line.
.PP
From there on,
.I nam
expects to see
repetitions of a section heading
(starting at the left margin)
followed by a section body,
indented.
At the start of each section,
.I nam
produces a
.B \&.SH
macro.
.PP
Each line read must be carefully examined
for changes in margins, indentation, inter-word spacing and the like
since the previous line.
Page footers and headers must be skipped.
Lines that abruptly increase indentation are probably displays;
lines that decrease indentation
(except back to the left margin)
are errors,
but they do occur in real examples.
Lines that start with
.B \&.
or
.B '
must be protected with
.B \e& .
Lines that end with a hyphen probably indicate hyphenation.
.PP
Some post-V6
.I \-man
macros,
notably those found in
.\" TODO: put a reference here?
.I awf ,
still insist on justifying their output,
which is a real nuisance to undo
and interferes with otherwise-straightforward heuristics.
.PP
Following a blank line or a section heading,
.I nam
expects the start of a new paragraph.
New paragraphs come in three flavours:
normal,
indented
and tagged.
A tagged paragraph is an indented paragraph that starts with an
unindented `tag' string.
.PP
One technique required by several heuristics is push-back of
multiple lines (of up to a page of lines).
Being able to peek ahead just one line makes several heuristics
possible,
and full-page lookahead makes footer detection essentially fool-proof.
.SH
Other Directions
.PP
(One hesitates to call this section
``Future Directions''
given the amount of work required.)
This approach
(and possibly some of the code)
could be applied to other formatters
(e.g. Tex)
or macro packages,
subject to the constraint that the input language
must be fairly simple and the output formatting
must be fairly rigid.
The farther a formatter or macro package
deviates from this constraint,
the more work it will take to decompile.
Manual pages
rarely contain equations,
tables
or
pictures,
and only occasionally displays;
attempting to decompile fully-general
.I "nroff \-ms"
output is fairly hard,
particularly since there are often multiple forms of
.I \-ms
input
capable of producing a given effect in the output
(this is less true of
.I \-man
documents that stay away from native
.I troff
requests).
Even decompiling
.I \-mm ,
which is more restrictive about formatting than
.I \-ms ,
would be fairly hard.
(\c
.I \-me
is essentially unused hereabouts
and is known to be quite buggy,
even by macro package standards,
so no estimate is made for it.)
.PP
One hopes that no other organisation will repeat
the mistakes of
.SM
AT&T\c
.NL
\&'s
marketing department
and that this technology will not need to be
applied to any other problems.
.SH
Acknowledgements
.PP
The idea of reverse-engineering
manual page input
from
.Fm
arose during discussions with
Beverly Erlebacher
and
Rayan Zachariassen
about how to deal with the recent influx of
machines from Silicon Graphics
.\" System V: consider it slandered
(and to a lesser extent,
MIPS)
at the University of Toronto,
which has traditionally run Research
.UX
or 4BSD.
.PP
Henry Spencer
prodded the author to write this paper
and contributed valuable comments on it.
Mark Moraes also read and commented on drafts.
.br
.[
$LIST$
.]
.br
