LinuxCommandLibrary

m4

Process text files; expand macros

TLDR

Process macros in a file

$ m4 [path/to/file]
copy

Define a macro before processing files
$ m4 [[-D|--define]] [macro_name]=[macro_value] [path/to/file]
copy

SYNOPSIS

m4 [OPTION]... [FILE]...

PARAMETERS

-D name[=value]
    Defines a macro name with the specified value before processing any input files. If value is omitted, it defaults to an empty string.

-U name
    Undefines a macro name before processing any input files, overriding any previous definitions.

-I directory
    Adds directory to the list of directories to be searched for included files. This is useful for organizing macro libraries.

-s, --sync-output
    Synchronizes output with input, ensuring that output lines appear in the same order relative to non-macro-expanded lines as their corresponding input lines.

-G, --traditional
    Operates in a mode that attempts to mimic the behavior of traditional Unix m4, particularly regarding whitespace handling and macro arguments. Useful for compatibility.

DESCRIPTION

The m4 command is a powerful general-purpose macro processor, available on most Unix-like operating systems. It reads input text, expanding macros as it encounters them, and writes the processed output to standard output.

Developed by Brian Kernighan and Dennis Ritchie at Bell Labs, m4 excels at text transformation, allowing users to define and manipulate macros that can perform tasks such as text substitution, conditional processing, arithmetic operations, and file inclusion. Its recursive macro expansion capabilities make it highly versatile for generating repetitive text, creating custom configuration files, or even generating source code.

While not directly a programming language, m4's ability to manipulate text streams based on complex rules makes it an invaluable tool in various development workflows, most notably as a core component of the GNU Autoconf build system.

CAVEATS

The syntax of m4 can be unintuitive and sensitive to whitespace, making complex macro definitions challenging to write and debug. Improper use of quoting can lead to unexpected macro expansion or infinite recursion. Debugging m4 scripts often requires careful tracing of macro expansions, which can be verbose.

MACRO DEFINITION AND INVOCATION

Macros are defined using the built-in define macro, for example: define(name, value). When m4 encounters name in the input, it replaces it with value. Invoking a macro is as simple as typing its name. Arguments can be passed by separating them with commas, like macro_name(arg1, arg2).

QUOTING

To prevent immediate expansion of text, m4 uses quoting. By default, the quote characters are backtick (`) and apostrophe ('). For example, `define` prevents the literal string 'define' from being interpreted as the built-in macro. The quote characters themselves can be changed using changequote, which is often done to avoid conflicts with programming language syntax.

BUILT-IN MACROS

m4 provides numerous built-in macros for various functionalities beyond simple text substitution. Examples include: ifelse(condition, then_text, else_text) for conditionals, eval(expression) for arithmetic evaluation, include(file) for incorporating content from other files, and dnl (delete to newline) for discarding the rest of the current input line, which is crucial for managing whitespace.

HISTORY

m4 was developed by Brian Kernighan and Dennis Ritchie at Bell Labs in 1977. It emerged from the need for a general-purpose macro processor, influenced by earlier tools like GPM. It became a standard part of Unix systems and gained significant prominence through its integration into the GNU Autoconf build system, where it's used to generate portable shell scripts for configuring software packages.

SEE ALSO

autoconf(1), cpp(1), sed(1), awk(1), make(1)

Copied to clipboard