![](http://image.absoluteastronomy.com/images//topicimages/noimage.gif)
M4 (computer language)
Encyclopedia
m4 is a general purpose macro processor
designed by Brian Kernighan
and Dennis Ritchie
. m4 is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.
es make the m4 macro processor available, and POSIX
has standardized it. Most users require m4 simply as a dependency of GNU autoconf
, used by "configure" scripts, although the language can be, and is, used stand-alone.
A macro processor (or a preprocessor
) operates as a text-replacement tool. End-users often employ it to re-use text templates, typically in programming applications, but also in text editing and in text-processing applications.
s commonly used assembly language
. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors.
Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey
. The distinguishing features of this style of macro preprocessing included:
The implementation of Rational Fortran
used m4 as its macro engine from the beginning; and most Unix
variants ship with it.
many applications continue to use m4 as part of the GNU
Project's autoconf
. It also appears in the configuration process of sendmail
(a widespread mail transfer agent
) and for generating footprints in the gEDA
toolsuite.
m4 has many uses in code generation
, but (as with any macro processor) problems can be hard to debug.
Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor
dialect of Fortran
. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language.
code. It defines a commented macro to number sections automatically:
Processing this code with m4 should generate the following text:
Note the frequent use of
, thus preventing many blank lines appearing in the output.
exists. FreeBSD
, NetBSD
, and OpenBSD
also provide independent implementations of the m4 language. Furthermore, the Heirloom Project
Development Tools includes a free version of the m4 language, derived from OpenSolaris
.
General Purpose Macro Processor
A general purpose macro processor is a macro processor that is not tied to or integrated with a particular language or piece of software.A macro processor is a program that copies a stream of text from one place to another, making a systematic set of replacements as it does so. Macro processors...
designed by Brian Kernighan
Brian Kernighan
Brian Wilson Kernighan is a Canadian computer scientist who worked at Bell Labs alongside Unix creators Ken Thompson and Dennis Ritchie and contributed to the development of Unix. He is also coauthor of the AWK and AMPL programming languages. The 'K' of K&R C and the 'K' in AWK both stand for...
and Dennis Ritchie
Dennis Ritchie
Dennis MacAlistair Ritchie , was an American computer scientist who "helped shape the digital era." He created the C programming language and, with long-time colleague Ken Thompson, the UNIX operating system...
. m4 is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.
Use
All UNIXUnix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
es make the m4 macro processor available, and POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
has standardized it. Most users require m4 simply as a dependency of GNU autoconf
Autoconf
GNU Autoconf is a tool for producing configure scripts for building, installing and packaging software on computer systems where a Bourne shell is available....
, used by "configure" scripts, although the language can be, and is, used stand-alone.
A macro processor (or a preprocessor
Preprocessor
In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers...
) operates as a text-replacement tool. End-users often employ it to re-use text templates, typically in programming applications, but also in text editing and in text-processing applications.
History
Macro processors became popular when programmerProgrammer
A programmer, computer programmer or coder is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software. One who practices or professes a formal approach to...
s commonly used assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors.
Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey
Christopher Strachey
Christopher Strachey was a British computer scientist. He was one of the founders of denotational semantics, and a pioneer in programming language design...
. The distinguishing features of this style of macro preprocessing included:
- free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing)
- the high degree of re-expansion (a macro's arguments get expanded twice: once during scanning and once at interpolation time)
The implementation of Rational Fortran
Ratfor
Ratfor is a programming language implemented as a preprocessor for Fortran 66. It provided modern control structures, unavailable in Fortran 66, to replace GOTOs and statement numbers.- Features :...
used m4 as its macro engine from the beginning; and most Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
variants ship with it.
many applications continue to use m4 as part of the GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...
Project's autoconf
Autoconf
GNU Autoconf is a tool for producing configure scripts for building, installing and packaging software on computer systems where a Bourne shell is available....
. It also appears in the configuration process of sendmail
Sendmail
Sendmail is a general purpose internetwork email routing facility that supports many kinds of mail-transfer and -delivery methods, including the Simple Mail Transfer Protocol used for email transport over the Internet....
(a widespread mail transfer agent
Mail transfer agent
Within Internet message handling services , a message transfer agent or mail transfer agent or mail relay is software that transfers electronic mail messages from one computer to another using a client–server application architecture...
) and for generating footprints in the gEDA
GEDA
The term gEDA refers to two things:# A set of software applications used for electronic design released under the GPL. As such, gEDA is an ECAD or EDA application suite. gEDA is mostly oriented towards printed circuit board design...
toolsuite.
m4 has many uses in code generation
Automatic programming
In computer science, the term automatic programming identifies a type of computer programming in which some mechanism generates a computer program to allow human programmers to write the code at a higher abstraction level....
, but (as with any macro processor) problems can be hard to debug.
Features
m4 offers these facilities:- a free-form syntaxFree-form languageIn computer programming, a free-form language is a programming language in which the positioning of characters on the page in program text is not significant. Program text does not need to be placed in specific columns as on old punched card systems, and frequently ends of lines are not significant...
, rather than line based syntax - a high degree of macro expansion (arguments get expanded during scan and again during interpolation)
- text replacement
- parameter substitution
- file inclusion
- string manipulation
- conditional evaluation
- arithmetic expressions
- system interface
- programmer diagnostics
- programming language independent
- human language independent
- provides programming language capabilities
Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor
Ratfor
Ratfor is a programming language implemented as a preprocessor for Fortran 66. It provided modern control structures, unavailable in Fortran 66, to replace GOTOs and statement numbers.- Features :...
dialect of Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language.
Example
The following fragment gives a simple example that could form part of a library for generating HTMLHTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
code. It defines a commented macro to number sections automatically:
divert(-1)
This `divert' discards this text. Note that I had to quote the `divert'
in the comment so it wouldn't get undiverted.
- In a true comment, I'm free to use words such as divert and other builtin
- m4 macros' names without consequence.
- This starts the count at ONE as the incr is a preincrement.
define(`H2_COUNT', 0)
- The H2_COUNT macro is redefined every time the H2 macro is used.
define(`H2',
`define(`H2_COUNT', incr(H2_COUNT))H2_COUNT. $1
')
divert(0)dnl Diversion to 0 means back to normal. dnl macro removes this line.
H2(First Section)
H2(Second Section)
H2(Conclusion)
Processing this code with m4 should generate the following text:
1. First Section
2. Second Section
3. Conclusion
Note the frequent use of
dnl
, which deletes up to and including the newlineNewline
In computing, a newline, also known as a line break or end-of-line marker, is a special character or sequence of characters signifying the end of a line of text. The name comes from the fact that the next character after the newline will appear on a new line—that is, on the next line below the...
, thus preventing many blank lines appearing in the output.
Free software implementations
A GNU version of m4GNU m4
GNU m4 is the GNU version of the m4 macro preprocessor. It is designed to avoid many kinds of limits found in traditional m4s: limits like maximum line lengths, maximum size of a macro, number of macros, etc...
exists. FreeBSD
FreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...
, NetBSD
NetBSD
NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...
, and OpenBSD
OpenBSD
OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...
also provide independent implementations of the m4 language. Furthermore, the Heirloom Project
Heirloom Project
The Heirloom Project is a collection of traditional Unix utilities. Most of them are derived fromoriginal Unix source code, as released as Open Source by Caldera and Sun.The project has the following components:...
Development Tools includes a free version of the m4 language, derived from OpenSolaris
OpenSolaris
OpenSolaris was an open source computer operating system based on Solaris created by Sun Microsystems. It was also the name of the project initiated by Sun to build a developer and user community around the software...
.
See also
- C preprocessorC preprocessorThe C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....
- PreprocessorPreprocessorIn computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers...
- Make
- Template engineTemplate engineA template engine is software that is designed to process web templates and content information to produce output web documents. It runs in the context of a template system.-Types:...
- Web templateWeb templateA web template is a tool used to separate content from presentation in web design, and for mass-production of web documents. It is a basic component of a web template system.Web templates can be used to set up any type of website...
External links
- GNU M4 website
- M4 tutorial
- Macro Magic: m4, Part One and Part Two (requires registration)