The m4 Macro Package

Robert Adams

Issue #96, April 2002

Author will write an article discussing m4, and illustrating it's use in two domains: building dynamic web pages, and using it to build a simple “database”.

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.

Defining and Using Macros

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.txt
it produces the following output:
Hello, World, welcome to m4!

HTML Example

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_HTML
Once 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.html
That'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.

A Simple Database Example

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

Conclusion

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!

Resources

email: adams@csis.gvsu.edu

Robert Adams is a professor of computer science at GVSU. When he's not teaching he enjoys playing the fiddle, dancing and spending time with his daughter Turah.