IOTek

m4 - The Nifty Macro Processor

In the beginning, there was long and complicated code. Then came along functions to make repetitive tasks less repetitive. But not everything can take advantage of functions. Configurations, run-controls, and regular documents seem hopelessly lost. Move over, your now-former best friend, because there is a new tool under your belt.

m4 is more accurately an “old tool”, first appearing all the way back in 1977. Yet another lovechild of UNIX greybeards Brian Kernighan and Dennis Ritchie, m4 provides the user with a way to define new words and what they can be substituted with depending on different contexts. In other words, it functions as a preprocessor. Beats any idea of a sed script to replace keywords to a pulp. The second best part besides the fact m4 exists is that if you run a UNIX-like operating system (GNU/Linux, Macintosh OS X, and other members of the BSD family), it is already installed. Congratulations, you are the proud owner of a macro processor.

If you expected this post to explain how to use m4, you will be sorely disappointed. But you can always check the manuals.

Preprocessing

If you deal with code in C (or dare I say C++), you may have noticed lines such as #include "header.h" or #define THING 42. These lines are handled by a preprocessor specially tailored for C. Theoretically, you could use the C preprocesor for common text files, but that can throw it off by a few measures. This is where m4 shines better than cpp in many cases.

Rule of Generation

The Rule of Generation is one of the (sadly) lesser-known rules of the famed UNIX philosophy. Long story short, aim for programs that create to speed up the development process.

A common use of m4 is to create web pages more efficiently. Too many people have been turned away from automating hypertext markup generation that they insist on writing it all by hand. Thank you very much, Adobe Dreamweaver, for instilling this fear.

Those people are insane in my book to think that manually writing markup is okay. With their new pal m4, they can instead specify how to create markup to their own whims. Saves time and it can stay in their style. Sounds like a win-win deal. At the risk of this next sentence sounding like a shameless plug, I even utilized m4 as my site’s backend for static blog generation. Even managed to cram the template into an eighty by twenty-four cell screen.

But That’s Not All, Folks

There is a lot more m4 is capable of than petty text substitution. It just takes creativity and tinkering to truly grasp its full potential. Further reading can be found at:

by rocx