The m4 Macro Package
April 1st, 2002 by Robert Adams in
When you installed Linux, you installed a lot of cool programs, many of which you use every day. At the same time hundreds of little utilities also got installed, simply because they were required by the bigger, more elaborate applications. However, many of these little utilities are extremely useful in their own right. This article describes one of these second-class utilities: m4.
m4 is a macro processor, meaning that it copies its input to the output, expanding macros along the way. In this regard it's similar to another macro processor you're probably already familiar with: cpp (C Pre-Processor). Like cpp, m4 originally was written as the pre-processor for a programming language (Rational FORTRAN); however, m4 is much more powerful and feature-rich than cpp, which makes it much more useful than just defining constants in programs.
Being a macro processor, m4 certainly provides the ability to define simple macros, but it goes much further. You also can define parameterized macros (macros with arguments), conditionally include text into the output stream, do looping via recursion, run a shell command and insert its output into the output stream, include a file, perform simple string operations (length, substring search, regexp search, etc.), perform simple integer arithmetic and much more.
The examples that follow illustrate many of the features of m4, but this article can't do justice to everything m4 can do for you. Take a look at the info page (see Resources) for a complete description. Also, the examples below are abbreviated versions of m4 macros that I actually use. You can find the full source at my web page listed in Resources.
A word of warning from the m4 info page:
Some people [find] m4 to be fairly addictive...learning how to write complex m4 sets of macros....Once really addicted, users pursue writing of sophisticated m4 applications even to solve simple problems....Beware that m4 may be dangerous for the health of compulsive programmers.
The basic tool of m4 is the macro. Macros are defined with the define command. For example, define(`hello', `Hello, World') defines a macro called hello. Notice the quote characters ` and '. These group words together to form phrases, and when they surround a single word they inhibit macro expansion. Usually, m4 will expand macros within both the macro name and the macro body unless you tell it not to by quoting.
Like cpp, you don't need any special commands or prefix characters to use macros. Everywhere the macro name appears in the input stream, m4 will substitute the macro body.
To run m4, simply give it the name of the file(s) to use as input. It reads through the input, expanding macros as it goes, and generates text on the standard output.
Assume we have the following file called sample.m4:
define(`hello', `Hello, World') hello, welcome to m4!
If we run this file through m4 with the command
m4 sample.m4 > sample.txtit produces the following output:
Hello, World, welcome to m4!
As I said above, m4 goes beyond simple macros. This example demonstrates some of m4's advanced features by defining some macros to ensure that HTML pages all have the same look and feel. HTML purists probably could do this with JavaScript and CSS, but it's so easy with m4 and doesn't require anything special from the browser.
Four macros are defined: one to produce the HTML header, a second to create a banner at the top of the HTML page, a third to create banners within an HTML page (like section headers) and a final one to do some housekeeping at the end of the page. We put these macros into a file called html.m4 and use it as a macro package later.
First, the macro to start an HTML page:
define(`START_HTML',
`<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<meta name="Author" content="D. Robert Adams">
<title>$1</title>
</head>
<body text="#000000"
ifdef(`BACKGROUND_IMAGE',
`background="BACKGROUND_IMAGE"')
bgcolor="#e5e5e5" link="#3333ff"
vlink="#000099"
alink="#ffffff">
')
Line one defines a macro called START_HTML that expands to all the text that follows. Note the use of $1 on line seven. This expands to the first macro argument, which will become the page's title (see the next section for how these macros are used). Also note the use of m4's ifdef macro on line 11. ifdef checks if a macro has been defined. If it has, it includes the text given in the second argument. In this case, ifdef checks if BACKGROUND_IMAGE has been defined. If it has, we include the HTML code to use the image as the web page's background.
Next comes the page header macro:
define(`PAGE_HEADER',
`<table border="0" background="steel.jpg"
width="100%">
<tr>
<td align="left">$1</td>
<td align="right">$2</td>
</tr>
</table>
<div align=right>
Last Modified: esyscmd(`date')
</div>
')
Again, note the use of $1 and $2 on lines four and five that get expanded with macro arguments. Further, note the esyscmd() macro on line nine. esyscmd() tells m4 to run the given shell command and insert its output at the given location. In this case, we run “date” to produce a timestamp in our document.
Next, we create the macro to make a section banner within the page. This uses nothing you haven't seen before:
define(`HTML_BANNER',
`<table border="0"
background="steel.jpg"
width="100%">
<tr>
<td>
<img src="$2">
<h2>$1</h2>
</td>
</tr>
</table>
')
The final piece necessary is the macro to close the HTML “body” and “html” tags:
define(`END_HTML', `</body></html>')Given the above four macros, creating a web page is extraordinarily easy. Create a file (index.m4) that contains calls to our HTML macros. The only thing new is the include macro that inserts our HTML macros:
include(`html.m4') define(`BACKGROUND_IMAGE', `bg.jpg') START_HTML(`Sample Page') PAGE_HEADER(`computer.jpg', `Sample HTML Page') HTML_BANNER(`img1.jpg', `News') HTML_BANNER(`img2.jpg', `Downloads') END_HTMLOnce we've declared the macros and a file that uses them, the final step is to run m4 to create the HTML page. The command is:
m4 index.m4 > index.htmlThat's it! With just seven lines of code we create a fully functional HTML document. By using the macros to create other HTML pages, every page will have the same look and feel. Furthermore, we can change the look by simply changing the macro definitions and re-creating the HTML files.
In this second example, we create a “database” of exam questions for a course. The goals are 1) to manage exam questions in a single repository, 2) to be able to create a LaTeX-format exam by simply choosing which questions to include and 3) to produce an answer key with equally little effort.
The question database consists of an m4 macro for each question. We store the macros in a file called questions.m4. For example:
define(`QUESTION_1', `Why did the chicken cross the road? ANSWER(1in, `To get to the other side') ')
Obviously, this macro will be expanded with the question itself, but note the use of the embedded macro ANSWER. It expands to one inch of vertical space if we're producing the exam, otherwise it expands to the answer if we're producing an answer key. ANSWER is defined as:
define(ANSWER, ifdef(`ANSWER_KEY', `Answer: $2',
`\vspace*{$1}'))
If ANSWER_KEY is defined, we include the answer ($2), otherwise we
include some vertical space ($1) so the student can write in the
answer.
Using the question macros is as easy as using the HTML macros. The complete exam code is stored in a file called exam.m4:
include(examMacros.m4) EXAM_HEADER(`JOKE 101', `Fall 2001', `First Exam') include(questions.m4) QUESTION_1 QUESTION_2 EXAM_TRAILER
The “include” on line one includes the code for EXAM_HEADER and EXAM_TRAILER that generate boilerplate LaTeX at the top and bottom of the exam. Line three includes the question macros we just created. Lines four and five include two questions from the database.
To create an exam, we run the command m4 exam.m4 > exam.tex. Because ANSWER_KEY was never defined, each question will include space for an answer. To create an answer key, we use m4's command-line options to temporarily define an ANSWER_KEY macro:
m4 -DANSWER_KEY exam.m4 > exam.key.tex
m4 is a tool that has applications in an endless number of domains. Anywhere you want to reduce duplication of effort, m4 can help. It is feature-rich enough that you can do almost anything and produce any kind of content you may wish. I hope I've given you enough to whet your appetite for m4 and to give you an appreciation for what m4 can do for you. Happy macro writing!
Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Sorry, offer available in the US only. International orders, click here.
Subscribe now!
Recently Popular
| Linux HOWTO: Video Editing Magic with ffmpeg | Jul-23-08 |
| The new business of free radio | Jul-24-08 |
| Why We Must React to ACTA | Jul-24-08 |
| Chapter 16: Ubuntu and Your iPod | Aug-30-06 |
| Boot with GRUB | May-01-01 |
| Review: HP 2133 Mini-Note | Jul-16-08 |
Featured Videos
Non-linear video editing tools are great, but they're not always the best tool for the job. This is where a powerful tool like ffmpeg becomes useful. This tutorial by Elliot Isaacson covers the basics of transcoding video, as well as more advanced tricks like creating animations, screen captures, and slow motion effects.
Shawn Powers reviews the HP Mini-Note portable computer.
Thanks to our sponsor: Silicon Mechanics
Silicon Mechanics is a leading manufacturer of rackmount servers, storage, and high performance computing hardware. The best warranty offerings available are backed by experts dedicated to customer satisfaction.
From the Magazine
August 2008, #172
There's nuttin like a Cool Project to give you some relief from the summer heat, so get out your parka cuz we got a bunch of em. First up is the BUG, not a bug, The BUG. It's got a GPS, camera and more, in a hand-sized package that's user programmable. The BUG does everything. It's both a floor wax and a dessert topping. Get one now. Need a software version of a Swiss Army knife? Take a look at Billix, and don't leave home without it. Then, chew on this one, an X server on a Gumstix device driving an E-Ink display. Need more storage? How about 16 Terabytes? Can do.
And, of course, we have the usual cast of characters: Marcel, Reuven, Dave, Kyle, Doc, plus the new kid on the block Shawn Powers. But it doesn't stop there: build a MythTV box on a budget, build your own GIS system, set up the tools to monitor your enterprise and more. Finally, remember The War of the Worlds? Now you can play too.

Delicious
Digg
Reddit
Newsvine
Technorati







Re: Take Command: The m4 Macro Package
On October 11th, 2003 Anonymous says:
Extolling the virtues of m4. In 2002.
Wow.
Has anyone noticed that the whole using two different quote
markers is unpleasant, out of use, and hard to read? Not to
mention that nearly all scripting languages like python or perl
provide convenient means of text replacement already and
are generally better known, more well behaved quoting-wise
and end up being easier to read?
Let m4 die.
you can use the following to
On September 2nd, 2005 Anonymous (not verified) says:
you can use the following to redfine quotes:
angequote([,])
Correction
On August 3rd, 2007 Doug (not verified) says:
There's some typo here. The m4 builtin function for this is actually "Changequote()".
Exhaustive documentation is available for m4 -- try running "info m4" on your system. If that doesn't work, then try visiting
http://www.gnu.org/software/m4/manual/html_node/index.html.
m4 is amazingly configurable. If you don't like the way it works, chances are good that you re-configure to work in a way that you do like.
Re: Take Command: The m4 Macro Package
On January 22nd, 2004 Anonymous says:
I disagree to "let m4 die". Look, i can define a macro within a multiline comment of a java file. Since it is comment, java compiler does not protest. This macro is used to generate an aspectj file, compiled together with the ajc compiler. Thus, using m4 i can have a variety of application versions, by only pointing the compilers to the list of automatically generated files. And for production code i can go without aspectj. Try to do it with perl :)
sincerely,
Jacek Kempski