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

Problems concerning the GNU operating system

Initially announced in ‘83, the GNU operating system1 has gained a lot of momentum, due to it being the default go-to operating system for the Linux kernel.

Even though initially meant as a joke2, the recursive acronym is becoming more of a reality every day.

Lets take a look coreutils package.

sort

sort is obviously a program meant to sort files, yet, GNU has managed to cram in a couple … extra … features.

Example usage of sort would be;

ls -1 logs | sort -n

The above command lists the directory logs/, where we can imagine there being multiple files named, or at least prefixed by the date of which they were created (or last modified). Then pipes these files to sort, which is given the -n3 flag.

sort holds the lines that were piped to it, then spits out the sorted output to stdout.

Seems like a simple processs, with a simple goal, right?

To most people, this is the sane way of going about it.

GNU does not agree.

Over the years, they’ve added so much code to sort, that it’s closing up on 5000 lines.

They’ve done everything from trying to steal uniq’s job, to making shell redirection obsolete.

They even went so far as to add a random feature.

Hold on, what? Isn’t “random” a polar opposite or “sorted”1?

You might think that GNU’s sort is a single instance, but no, this horrifying code quality is pretty uniform among all GNU applications.

Here’s another example.

All this program is supposed to do is to return an exit value of 0.

This list could go on and on, but hopefully you get the gist.

  1. Yes, it is.
  2. GNU is a recursive acronym, GNU is Not UNIX™
  3. -n is shorthand for numerical sort
by dcat