Jump to content

M4 (computer language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
I'm not were this phrase comes from....
copyediting
Line 1: Line 1:
{{lowercase|m4}}
{{lowercase|m4}}
'''m4''' is a [[general purpose macro processor]] designed by [[Brian Kernighan]] and [[Dennis Ritchie]]. The name m4 stands for macro, i.e. m plus 4 characters.


'''m4''' is a [[general purpose macro processor]] designed by [[Brian Kernighan]] and [[Dennis Ritchie]]. The name "m4" stands for "macro": the letter '''m''' plus '''4''' other characters.
==Introduction==
The m4 macro processor is widely available on all [[UNIX]]es, and has been standardized by [[POSIX]]. The popularity of GNU [[autoconf]], which requires GNU m4 for generating "configure" scripts, is an incentive for many to install it, while these people will not themselves program in m4.


==Use==
==Use==
All [[UNIX]]es make the m4 macro processor available, and [[POSIX]] has standardized it. The popularity of GNU [[autoconf]] (which requires GNU m4 for generating "configure" scripts) has encouraged many to install it, even if these people will not themselves program in m4.
A macro processor (or a [[preprocessor]]) is a text-replacement tool. Its chief use is to re-use text templates, typically in [[programming]] applications, but also in text editing, text processing applications.

A macro processor (or a [[preprocessor]]) operates as a text-replacement tool. End-users often employ it to re-use text [[template]]s, typically in [[programming]] applications, but also in text editing and in text-processing applications.


==History==
==History==
Macro processors were prevalent when [[assembly language]] programming was the common tool of programmers. In these early days of programming, the programmers noted that much of their program was repeated text. Simple means to reuse this text were invented. Programmers soon discovered that it was easier not only to reuse entire blocks of text, but also to substitute different values for similar parameters. This defined the usage range of macro processors.
Macro processors became popular when [[programmer]]s commonly used [[assembly language]]. In those early days of programming, the programmers noted that much of their program 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.


m4 was developed in 1977, based on ideas of [[Christopher Strachey]]. The distinguishing features of this style of macro preprocessing include the fact that it is free form, not line based (as typical for macro preprocessors designed for assembly language processing) and the high degree of re-expansion (e.g., a macro's arguments are expanded twice, once during scanning and once when they are interpolated). It was the original macro engine used to implement [[Ratfor|Rational Fortran]], and is shipped with most [[Unix]] variants. One of the most widespread present-day uses is as part of the [[GNU]] Project's [[autoconf]]. It is also used in the configuration process of the widespread [[mail transfer agent]] [[sendmail]], and for generating footprints in the [[gEDA]] toolsuite. In general, it is very useful for [[code generation]] purposes, owing to its [[Turing completeness]], but can be hard to debug.
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:
* free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing)
* the high degree of re-expansion (e.g., a macro's arguments get expanded twice, once during scanning and once when at interpolation time)
The implementation of [[Ratfor|Rational Fortran]] used m4 as its macro engine from the beginning; and most [[Unix]] variants ship with it.
{{As of | 2009}} 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.
In general, m4 has many uses in [[code generation]], owing to its [[Turing completeness]], but can be hard to debug.


==Features==
==Features==
m4 offers these facilities:
m4 offers these facilities:
*text replacement
*parameter substitution
*file inclusion
*string manipulation
*conditional evaluation
*arithmetic expressions
*system interface
*programmer diagnostics


* text replacement
Unlike most earlier macro processors, it is not targeted at any particular computer or human language; historically, however, it was developed for supporting the [[Ratfor]] dialect of [[Fortran]]. Unlike some other macro processors, m4 is [[Turing-complete]] as well as a practical programming language.
* parameter substitution
* file inclusion
* string manipulation
* conditional evaluation
* arithmetic expressions
* system interface
* programmer diagnostics

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.


==Example==
==Example==
The following fragment is a simple example that could be part of an [[HTML]] generation library. It defines a macro to number sections automatically:
The following fragment gives a simple example that could form part of a library for generating [[HTML]] code. It defines a macro to number sections automatically:


define(`H2_COUNT', 0)dnl
define(`H2_COUNT', 0)dnl
Line 37: Line 47:
H2(Conclusion)
H2(Conclusion)


After being processed with m4, the following text will be generated:
Processing this code with m4 should generate the following text:


<h2>1. First Section</h2>
<h2>1. First Section</h2>
Line 43: Line 53:
<h2>3. Conclusion</h2>
<h2>3. Conclusion</h2>


Note the frequent use of '''dnl''' which deletes up to and including the newline, thus preventing many blank lines appearing in the output.
Note the frequent use of <code>dnl</code>, which deletes up to and including the [[newline]], thus preventing many blank lines appearing in the output.


==Free software implementations==
==Free software implementations==
There is a [[GNU m4|GNU version of m4]]. [[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]].
A [[GNU m4|GNU version of m4]] 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]].


== See also ==
== See also ==
Line 56: Line 66:


==References==
==References==
*Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. [http://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf pdf]
* Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. [http://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf pdf]
*Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. [http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf pdf]
* Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. [http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf pdf]
*René Seindal. GNU M4 Manual. GNU Press. 2004. [http://www.gnu.org/software/m4/manual/]
* René Seindal. GNU M4 Manual. GNU Press. 2004. [http://www.gnu.org/software/m4/manual/]


==External links==
==External links==
*[http://www.gnu.org/software/m4/m4.html GNU M4 website]
* [http://www.gnu.org/software/m4/m4.html GNU M4 website]
*[http://mbreen.com/m4.html M4 tutorial]
* [http://mbreen.com/m4.html M4 tutorial]


[[Category:Macro programming languages]]
[[Category:Macro programming languages]]

Revision as of 00:10, 27 March 2009


m4 is a general purpose macro processor designed by Brian Kernighan and Dennis Ritchie. The name "m4" stands for "macro": the letter m plus 4 other characters.

Use

All UNIXes make the m4 macro processor available, and POSIX has standardized it. The popularity of GNU autoconf (which requires GNU m4 for generating "configure" scripts) has encouraged many to install it, even if these people will not themselves program in m4.

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.

History

Macro processors became popular when programmers commonly used assembly language. In those early days of programming, the programmers noted that much of their program 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:

  • free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing)
  • the high degree of re-expansion (e.g., a macro's arguments get expanded twice, once during scanning and once when at interpolation time)

The implementation of Rational Fortran used m4 as its macro engine from the beginning; and most Unix variants ship with it.

As of 2009 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.

In general, m4 has many uses in code generation, owing to its Turing completeness, but can be hard to debug.

Features

m4 offers these facilities:

  • text replacement
  • parameter substitution
  • file inclusion
  • string manipulation
  • conditional evaluation
  • arithmetic expressions
  • system interface
  • programmer diagnostics

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.

Example

The following fragment gives a simple example that could form part of a library for generating HTML code. It defines a macro to number sections automatically:

define(`H2_COUNT', 0)dnl
define(`H2', `define(`H2_COUNT', incr(H2_COUNT))'dnl
`<h2>H2_COUNT. $1</h2>')dnl
dnl
H2(First Section)
H2(Second Section)
H2(Conclusion)

Processing this code with m4 should generate the following text:

<h2>1. First Section</h2>
<h2>2. Second Section</h2>
<h2>3. Conclusion</h2>

Note the frequent use of dnl, which deletes up to and including the newline, thus preventing many blank lines appearing in the output.

Free software implementations

A GNU version of m4 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.

See also

References

  • Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. pdf
  • Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. pdf
  • René Seindal. GNU M4 Manual. GNU Press. 2004. [1]