xref: /freebsd/share/doc/psd/17.m4/m4.ms (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code and documentation must retain the above
copyright notice, this list of conditions and the following
disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

All advertising materials mentioning features or use of this software
must display the following acknowledgement:

This product includes software developed or owned by Caldera
International, Inc. Neither the name of Caldera International, Inc.
nor the names of other contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.

USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@(#)m4.ms 6.3 (Berkeley) 6/5/93

$FreeBSD$
Bell Laboratories, Murray Hill, NJ 07974. .. .EH 'PSD:17-%''The M4 Macro Processor' .OH 'The M4 Macro Processor''PSD:17-%' .tr _\(em .tr *\(** \\$3\s-1\\$1\\s0\\$2 .. \\$3\f2\\$1\\$2 .. \\$3\f3\\$1\\$2 .. I 3n
.tr `\(ga'\(aa
.lg 0
..
.vs \\n(VSp
.tr --||''^^!!
.lg .. . \"2=not last lines; 4= no -xx; 8=no xx- . \"special chars in programs . \" start of text .RP
.....TR 59
.....TM 77-1273-6 39199 39199-11
.ND "July 1, 1977"
The M4 Macro Processor .AU "MH 2C-518" 6021 Brian W. Kernighan .AU "MH 2C-517" 3770 Dennis M. Ritchie .AI .MH .AB

M4 is a macro processor available on X and C GCOS . Its primary use has been as a front end for Ratfor for those cases where parameterless macros are not adequately powerful. It has also been used for languages as disparate as C and Cobol. M4 is particularly suited for functional languages like Fortran, PL/I and C since macros are specified in a functional notation.

M4 provides features seldom found even in much larger macro processors, including

" \(bu"
arguments
" \(bu"
condition testing
" \(bu"
arithmetic capabilities
" \(bu"
string and substring functions
" \(bu"
file manipulation

This paper is a user's manual for M4. .AE .CS 6 0 6 0 0 1

Introduction

A macro processor is a useful way to enhance a programming language, to make it more palatable or more readable, or to tailor it to a particular application. The #define statement in C and the analogous define in Ratfor are examples of the basic facility provided by any macro processor _ replacement of text by other text.

The M4 macro processor is an extension of a macro processor called M3 which was written by D. M. Ritchie for the AP-3 minicomputer; M3 was in turn based on a macro processor implemented for [1]. Readers unfamiliar with the basic ideas of macro processing may wish to read some of the discussion there.

M4 is a suitable front end for Ratfor and C, and has also been used successfully with Cobol. Besides the straightforward replacement of one string of text by another, it provides macros with arguments, conditional macro expansion, arithmetic, file manipulation, and some specialized string processing functions.

The basic operation of M4 is to copy its input to its output. As the input is read, however, each alphanumeric ``token'' (that is, string of letters and digits) is checked. If it is the name of a macro, then the name of the macro is replaced by its defining text, and the resulting string is pushed back onto the input to be rescanned. Macros may be called with arguments, in which case the arguments are collected and substituted into the right places in the defining text before it is rescanned.

M4 provides a collection of about twenty built-in macros which perform various useful operations; in addition, the user can define new macros. Built-ins and user-defined macros work exactly the same way, except that some of the built-in macros have side effects on the state of the process.

Usage

On C UNIX , use

1 m4 [files]

2 Each argument file is processed in order; if there are no arguments, or if an argument is `-', the standard input is read at that point. The processed text is written on the standard output, which may be captured for subsequent processing with

1 m4 [files] >outputfile

2 On C GCOS , usage is identical, but the program is called ./m4 .

Defining Macros

The primary built-in function of M4 is define , which is used to define new macros. The input

1 define(name, stuff)

2 causes the string name to be defined as stuff . All subsequent occurrences of name will be replaced by stuff . name must be alphanumeric and must begin with a letter (the underscore \(ul counts as a letter). stuff is any text that contains balanced parentheses; it may stretch over multiple lines.

Thus, as a typical example,

1 define(N, 100) ... if (i > N)

2 defines N to be 100, and uses this ``symbolic constant'' in a later if statement.

The left parenthesis must immediately follow the word define , to signal that define has arguments. If a macro or built-in name is not followed immediately by `(', it is assumed to have no arguments. This is the situation for N above; it is actually a macro with no arguments, and thus when it is used there need be no (...) following it.

You should also notice that a macro name is only recognized as such if it appears surrounded by non-alphanumerics. For example, in

1 define(N, 100) ... if (NNN > 100)

2 the variable NNN is absolutely unrelated to the defined macro N , even though it contains a lot of N 's.

Things may be defined in terms of other things. For example,

1 define(N, 100) define(M, N)

2 defines both M and N to be 100.

What happens if N is redefined? Or, to say it another way, is M defined as N or as 100? In M4, the latter is true _ M is 100, so even if N subsequently changes, M does not.

This behavior arises because M4 expands macro names into their defining text as soon as it possibly can. Here, that means that when the string N is seen as the arguments of define are being collected, it is immediately replaced by 100; it's just as if you had said

1 define(M, 100)

2 in the first place.

If this isn't what you really want, there are two ways out of it. The first, which is specific to this situation, is to interchange the order of the definitions:

1 define(M, N) define(N, 100)

2 Now M is defined to be the string N , so when you ask for M later, you'll always get the value of N at that time (because the M will be replaced by N which will be replaced by 100).

Quoting

The more general solution is to delay the expansion of the arguments of define by .ul quoting them. Any text surrounded by the single quotes \(ga and \(aa is not expanded immediately, but has the quotes stripped off. If you say

1 define(N, 100) define(M, `N')

2 the quotes around the N are stripped off as the argument is being collected, but they have served their purpose, and M is defined as the string N , not 100. The general rule is that M4 always strips off one level of single quotes whenever it evaluates something. This is true even outside of macros. If you want the word define to appear in the output, you have to quote it in the input, as in

1 `define' = 1;

2

As another instance of the same thing, which is a bit more surprising, consider redefining N :

1 define(N, 100) ... define(N, 200)

2 Perhaps regrettably, the N in the second definition is evaluated as soon as it's seen; that is, it is replaced by 100, so it's as if you had written

1 define(100, 200)

2 This statement is ignored by M4, since you can only define things that look like names, but it obviously doesn't have the effect you wanted. To really redefine N , you must delay the evaluation by quoting:

1 define(N, 100) ... define(`N', 200)

2 In M4, it is often wise to quote the first argument of a macro.

If \` and \' are not convenient for some reason, the quote characters can be changed with the built-in changequote :

1 changequote([, ])

2 makes the new quote characters the left and right brackets. You can restore the original characters with just

1 changequote

2

There are two additional built-ins related to define . undefine removes the definition of some macro or built-in:

1 undefine(`N')

2 removes the definition of N . (Why are the quotes absolutely necessary?) Built-ins can be removed with undefine , as in

1 undefine(`define')

2 but once you remove one, you can never get it back.

The built-in ifdef provides a way to determine if a macro is currently defined. In particular, M4 has pre-defined the names unix and gcos on the corresponding systems, so you can tell which one you're using:

1 ifdef(`unix', `define(wordsize,16)' ) ifdef(`gcos', `define(wordsize,36)' )

2 makes a definition appropriate for the particular machine. Don't forget the quotes!

ifdef actually permits three arguments; if the name is undefined, the value of ifdef is then the third argument, as in

1 ifdef(`unix', on UNIX, not on UNIX)

2

Arguments

So far we have discussed the simplest form of macro processing _ replacing one string by another (fixed) string. User-defined macros may also have arguments, so different invocations can have different results. Within the replacement text for a macro (the second argument of its define ) any occurrence of $n will be replaced by the n th argument when the macro is actually used. Thus, the macro bump , defined as

1 define(bump, $1 = $1 + 1)

2 generates code to increment its argument by 1:

1 bump(x)

2 is

1 x = x + 1

2

A macro can have as many arguments as you want, but only the first nine are accessible, through $1 to $9 . (The macro name itself is $0 , although that is less commonly used.) Arguments that are not supplied are replaced by null strings, so we can define a macro cat which simply concatenates its arguments, like this:

1 define(cat, $1$2$3$4$5$6$7$8$9)

2 Thus

1 cat(x, y, z)

2 is equivalent to

1 xyz

2 $4 through $9 are null, since no corresponding arguments were provided.

Leading unquoted blanks, tabs, or newlines that occur during argument collection are discarded. All other white space is retained. Thus

1 define(a, b c)

2 defines a to be b c .

Arguments are separated by commas, but parentheses are counted properly, so a comma ``protected'' by parentheses does not terminate an argument. That is, in

1 define(a, (b,c))

2 there are only two arguments; the second is literally (b,c) . And of course a bare comma or parenthesis can be inserted by quoting it.

Arithmetic Built-ins

M4 provides two built-in functions for doing arithmetic on integers (only). The simplest is incr , which increments its numeric argument by 1. Thus to handle the common programming situation where you want a variable to be defined as ``one more than N'', write

1 define(N, 100) define(N1, `incr(N)')

2 Then N1 is defined as one more than the current value of N .

The more general mechanism for arithmetic is a built-in called eval , which is capable of arbitrary arithmetic on integers. It provides the operators (in decreasing order of precedence) unary + and - ** or ^ (exponentiation) * / % (modulus) + - == != < <= > >= ! (not) & or && (logical and) \(or or \(or\(or (logical or) Parentheses may be used to group operations where needed. All the operands of an expression given to eval must ultimately be numeric. The numeric value of a true relation (like 1>0) is 1, and false is 0. The precision in eval is 32 bits on C UNIX and 36 bits on C GCOS .

As a simple example, suppose we want M to be 2**N+1 . Then

1 define(N, 3) define(M, `eval(2**N+1)')

2 As a matter of principle, it is advisable to quote the defining text for a macro unless it is very simple indeed (say just a number); it usually gives the result you want, and is a good habit to get into.

File Manipulation

You can include a new file in the input at any time by the built-in function include :

1 include(filename)

2 inserts the contents of filename in place of the include command. The contents of the file is often a set of definitions. The value of include (that is, its replacement text) is the contents of the file; this can be captured in definitions, etc.

It is a fatal error if the file named in include cannot be accessed. To get some control over this situation, the alternate form sinclude can be used; sinclude (``silent include'') says nothing and continues if it can't access the file.

It is also possible to divert the output of M4 to temporary files during processing, and output the collected material upon command. M4 maintains nine of these diversions, numbered 1 through 9. If you say

1 divert(n)

2 all subsequent output is put onto the end of a temporary file referred to as n . Diverting to this file is stopped by another divert command; in particular, divert or divert(0) resumes the normal output process.

Diverted text is normally output all at once at the end of processing, with the diversions output in numeric order. It is possible, however, to bring back diversions at any time, that is, to append them to the current diversion.

1 undivert

2 brings back all diversions in numeric order, and undivert with arguments brings back the selected diversions in the order given. The act of undiverting discards the diverted stuff, as does diverting into a diversion whose number is not between 0 and 9 inclusive.

The value of undivert is .ul not the diverted stuff. Furthermore, the diverted material is .ul not rescanned for macros.

The built-in divnum returns the number of the currently active diversion. This is zero during normal processing.

System Command

You can run any program in the local operating system with the syscmd built-in. For example,

1 syscmd(date)

2 on C UNIX runs the date command. Normally syscmd would be used to create a file for a subsequent include .

To facilitate making unique file names, the built-in maketemp is provided, with specifications identical to the system function .ul mktemp: a string of XXXXX in the argument is replaced by the process id of the current process.

Conditionals

There is a built-in called ifelse which enables you to perform arbitrary conditional testing. In the simplest form,

1 ifelse(a, b, c, d)

2 compares the two strings a and b . If these are identical, ifelse returns the string c ; otherwise it returns d . Thus we might define a macro called compare which compares two strings and returns ``yes'' or ``no'' if they are the same or different.

1 define(compare, `ifelse($1, $2, yes, no)')

2 Note the quotes, which prevent too-early evaluation of ifelse .

If the fourth argument is missing, it is treated as empty.

ifelse can actually have any number of arguments, and thus provides a limited form of multi-way decision capability. In the input

1 ifelse(a, b, c, d, e, f, g)

2 if the string a matches the string b , the result is c . Otherwise, if d is the same as e , the result is f . Otherwise the result is g . If the final argument is omitted, the result is null, so

1 ifelse(a, b, c)

2 is c if a matches b , and null otherwise.

String Manipulation

The built-in len returns the length of the string that makes up its argument. Thus

1 len(abcdef)

2 is 6, and len((a,b)) is 5.

The built-in substr can be used to produce substrings of strings. substr(s, i, n) returns the substring of s that starts at the i th position (origin zero), and is n characters long. If n is omitted, the rest of the string is returned, so

1 substr(`now is the time', 1)

2 is

1 ow is the time

2 If i or n are out of range, various sensible things happen.

index(s1, s2) returns the index (position) in s1 where the string s2 occurs, or -1 if it doesn't occur. As with substr , the origin for strings is 0.

The built-in translit performs character transliteration.

1 translit(s, f, t)

2 modifies s by replacing any character found in f by the corresponding character of t . That is,

1 translit(s, aeiou, 12345)

2 replaces the vowels by the corresponding digits. If t is shorter than f , characters which don't have an entry in t are deleted; as a limiting case, if t is not present at all, characters from f are deleted from s . So

1 translit(s, aeiou)

2 deletes vowels from s .

There is also a built-in called dnl which deletes all characters that follow it up to and including the next newline; it is useful mainly for throwing away empty lines that otherwise tend to clutter up M4 output. For example, if you say

1 define(N, 100) define(M, 200) define(L, 300)

2 the newline at the end of each line is not part of the definition, so it is copied into the output, where it may not be wanted. If you add dnl to each of these lines, the newlines will disappear.

Another way to achieve this, due to J. E. Weythman, is

1 divert(-1) define(...) ... divert

2

Printing

The built-in errprint writes its arguments out on the standard error file. Thus you can say

1 errprint(`fatal error')

2

dumpdef is a debugging aid which dumps the current definitions of defined terms. If there are no arguments, you get everything; otherwise you get the ones you name as arguments. Don't forget to quote the names!

Summary of Built-ins

Each entry is preceded by the page number where it is described. .tr '\'`\` 3 changequote(L, R) 1 define(name, replacement) 4 divert(number) 4 divnum 5 dnl 5 dumpdef(`name', `name', ...) 5 errprint(s, s, ...) 4 eval(numeric expression) 3 ifdef(`name', this if true, this if false) 5 ifelse(a, b, c, d) 4 include(file) 3 incr(number) 5 index(s1, s2) 5 len(string) 4 maketemp(...XXXXX...) 4 sinclude(file) 5 substr(string, position, number) 4 syscmd(s) 5 translit(str, from, to) 3 undefine(`name') 4 undivert(number,number,...)

Acknowledgements

We are indebted to Rick Becker, John Chambers, Doug McIlroy, and especially Jim Weythman, whose pioneering use of M4 has led to several valuable improvements. We are also deeply grateful to Weythman for several substantial contributions to the code. .SG

References

[1]
B. W. Kernighan and P. J. Plauger, .ul Software Tools, Addison-Wesley, Inc., 1976.