The m4 Macro Package
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!
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.
|August 2014 Issue of Linux Journal: Programming||Aug 01, 2014|
|August 2014 Video Preview||Aug 01, 2014|
|Open-Source Space||Jul 31, 2014|
|Silicon Mechanics Gives Back||Jul 30, 2014|
|Reglue: Opening Up the World to Deserving Kids, One Linux Computer at a Time||Jul 29, 2014|
|diff -u: What's New in Kernel Development||Jul 23, 2014|
- August 2014 Issue of Linux Journal: Programming
- Open-Source Space
- Numerical Python
- New Storage Solution is Music to the Ears of Fast-Growing Digital Music Company
- Reglue: Opening Up the World to Deserving Kids, One Linux Computer at a Time
- Silicon Mechanics Gives Back
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer