Applications
According to Kernighan, "Macroprocessors are appealing. They're simple to implement, and you can get all kinds of wondrous effects. Unfortunately, there's no way to tell what those effects are going to be."
A toy example illustrates some simple uses of m1. Here's a form letter I've often been tempted to use:
@default MYNAME Jon Bentley @default TASK respond to your\ special offer @default EXCUSE the dog ate my \ homework Dear @NAME@: Although I would dearly love to @TASK@, I am afraid that I am unable to do so because @EXCUSE@. I am sure that you have been in this situation many times yourself. Sincerely, @MYNAME@
If that file is named sayno.mac, it might be invoked with this text:
@define NAME Mr. Smith @define TASK subscribe to your \ magazine @define EXCUSE I suddenly forgot how \ to read @include sayno.mac
Recall that a @default
takes effect only if its variable was not previously @defined
.
I've found m1 to be a handy Troff preprocessor. Many of my text files (including this one) start with m1 definitions like:
@define ArrayFig @[email protected] &@define HashTabFig @[email protected] @define TreeFig @[email protected] @define ProblemSize 100
Even a simple form of arithmetic would be useful in numeric sequences of definitions. The longer m1 variables get around Troffs dreadful two-character limit on string names. These variables are also available to Troff preprocessors like Pic and Eqn. Various forms of the @define
, @if
, and @include
facilities are present in some of the Troff-family languages (Pic and Troff) but not others (Tbl). m1 provides a consistent mechanism.
I include figures in documents with lines like this:
@define FIGNUM @FIGMFMOVIE@ @define FIGTITLE The Multiple \ Fragment heuristic. @FIGSTART@ .PS <@THISDIR@/mfmovie.pic @FIGEND@
The two @defines
supply the two parameters of number and title to the figure. The figure might be set off by horizontal lines or enclosed in a box, the number and title might be printed at the top or bottom, and the figures might be graphs, pictures, or animations of algorithms. All figures, though, are presented in the consistent format defined by FIGSTART and FIGEND.
I have also used m1 as a preprocessor for Awk programs. The @include
statement lets you build simple libraries of Awk functions (though some but not all Awk implementations provide this facility by allowing multiple program files). File inclusion was used in an earlier draft of this article to include individual functions in the text and then wrap them all together into the complete m1 program. The conditional statements let you customize a program with macros rather than run-time if
statements, which can reduce both run time and compile time.
The most interesting application for which I've used this macro language is unfortunately too complicated to describe in detail. I wrote the original version of m1 to control a set of experiments. The experiments were described in a language with a lexical structure that forced me to make substitutions inside text strings; that was the original reason for bracketing substitutions by @ characters. The experiments are currently controlled by text files that contain descriptions in the experiment language, data extraction programs written in Awk, and graphical displays of data written in Grap. All the programs are tailored by m1 commands.
Most experiments are driven by short files that set a few keys' parameters and then @include a large file with many @defaults
. Separate files describe the fields of shared databases:
@define N ($1) @define NODES ($2) @define CPU ($3) ...
These files are include
d in both the experiment and Troff files that display data from the databases. I had tried to conduct a similar set of experiments before I built m1 and got mired in muck. But the few hours I spent building the tool were paid back handsomely in the first days I used it.
Looking Back
I've found m1 to be a useful tool in several applications. If it is close to what you want but doesn't meet your exact needs, the code is small enough to be tailored to your application.
Building m1 has been a fun exercise in programming. I started with a tiny program and grew it as applications demanded. It uses several useful Awk techniques and even had some interesting performance problems. All in all, m1 provided me with an almost painless way to learn more about macros, one of the grand old problems of computing.
I am grateful for the helpful comments of Brian Kernighan and Doug McIlroy.
Jon Bentley is the author of Writing Efficient Programs, Programming Pearls, and More Programming Pearls: Confessions of a Coder, among other books and papers