m4
Process text files; expand macros
TLDR
Process macros in a file
Define a macro before processing files
SYNOPSIS
m4 [options] [file...]
PARAMETERS
-D name[=value]
Define the macro name to value, or to null if value is omitted.
-U name
Undefine the macro name.
-e
Exit immediately if any error occurs.
-g
Enable interactive debugging.
-i file
Include file at the beginning of the input.
-I directory
Search directory for include files.
-P
Inhibit all standard predefinitions.
-Q
Suppress warning messages.
-s
Enable line syncing.
-W number
Limit the number of expansion passes (default: 1000).
-H
Display help information and exit.
-v
Display version information and exit.
file...
Input files to process. If none are given, reads from standard input.
DESCRIPTION
m4 is a powerful macro processor, predating the C preprocessor, primarily used to generate code, text, or other program fragments. It reads input, expands macros, and writes the output. Macros can be defined to replace certain text with other text. M4 is Turing-complete, making it suitable for complex tasks, though its primary use is text manipulation. It's frequently employed in build systems, configuration file generation, and other situations where code or text needs to be parameterized and dynamically assembled.
Its strengths lie in its string manipulation capabilities and its ability to define and expand macros recursively. Unlike simple text replacement, m4 understands basic arithmetic and string operations, allowing for conditional execution and looping within the macro definitions. While it may seem archaic compared to modern scripting languages, m4 remains a useful tool in specific niches where its strengths are particularly advantageous.
CAVEATS
M4's syntax can be tricky and error messages can be difficult to decipher. It is Turing Complete and thus has the potential for non-termination. Debugging can be challenging.
INVOCATION
When m4 is invoked with file arguments, it processes each file in order, concatenating the output to standard output. If no files are specified, m4 processes standard input. M4 searches for macros in the input and expands them according to their definitions. Built-in macros provide basic functionality, such as string manipulation and arithmetic.
EXAMPLE
Define a macro 'VERSION' and use it:
Input File (example.m4):
define(VERSION, 1.2.3)
The current version is VERSION.
Command: m4 example.m4
Output: The current version is 1.2.3.
HISTORY
M4 was originally developed by Brian Kernighan and Dennis Ritchie at Bell Labs in the late 1970s, alongside tools like awk and make. It was designed to be a general-purpose macro processor, simpler than some of its predecessors. It saw widespread use in generating code and configuration files within the Unix environment, becoming a standard tool in many development workflows. Its use has decreased over time as more modern templating languages and scripting languages have emerged, but it continues to be used in legacy systems and in situations where its specific strengths are required.