Using the m4 Macro Processor
Chief among the the unsung heroes of Linux and Unix is m4. Unsung? Well, for
instance, although m4 has been a standard part of Unix since version 7,
no mention is made of it in that great O'Reilly & Associates book, Unix
Power Tools. What is it
about m4 that makes it so useful, and yet so overlooked? m4 -- a macro
processor -- unfortunately has a dry name that disguises a great utility. A
macro processor is basically a program that scans text and looks for
defined symbols, which it replaces with other text or other symbols.
Thus, m4 is a powerful general-purpose utility that can be used to
automate many tasks people often end up doing in sed,
awk, perl, and
even their favorite text editor. Even so, it still doesn't seem like a macro
processor is that big of a deal.
Unix developers already have a built-in macro processor, in the form
of the C preprocessor, in their compiler. Perhaps this is
what accounts for m4's relative neglect. Whatever the case may be, this
article
will show Linux users the power and usefulness of this software tool.
What is m4?
What is macro processing, and what is it good for? In their seminal work,
Software Tools, Kernighan and Plauger have a succinct
definition:
"Macros are used to extend some underlying language -- to perform a
translation from one language to another."
Thus, symbolic constants may be defined so that subsequent occurrences
of the name can be replaced by the defining string of characters,
regardless of the contents of the definition or its context. Such a
definition is called a macro, the replacement process is called
macro expansion, and the program for the process is called a macro
processor. The task performed by any macro processor is
the replacement of text by other text. A macro is defined either by
the m4 program (a built-in) or by the user. In addition to doing macro
expansion, m4 -- with functions that include other files, perform integer
arithmetic, manipulate text, and so forth -- is a perfect example of the
power
of the Unix filter concept.
The contemporary implementation of m4 on a Linux system is GNU
m4, which follows System V Release 3 m4, with extensions. I am
aware of no other version of m4 that has been ported to Linux.
m4 implementations on BSD may differ slightly. However, m4 is m4, and this
article should be useful for other Unix users, too. The latest version is
1.4, which was released in October 1994.
The scanning process
As m4 reads its input, it separates it into tokens. A token is
either a previously defined name, a string, or any single character
that is not a part of either a name or string. The input is then
scanned for recognized macros. This scanning process is recursive, which
means
that scanning continues until no more
Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!
The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!












