M4(1P)

๐ŸŒŸ M4(1P) โ€” POSIX Programmer's Manual ๐ŸŒŸ

okay bestie, buckle up, because we're about to get into m4 and it's honestly so much more than it looks ๐Ÿ’… this is the POSIX Programmer's Manual page for it, so every single detail matters, no skimming allowed. โœจ


๐Ÿ’– PROLOG ๐Ÿ’–

ok so before literally anything else, the important disclaimer:

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ โ€” so pls consult the corresponding Linux manual page for the details on Linux behavior โ€” or, and I cannot stress this enough, the interface may not be implemented on Linux at all. ๐Ÿ˜ญ


๐ŸŒธ NAME ๐ŸŒธ

m4 โ€” macro processor. that's it. that's the whole vibe. a macro processor. iconic. ๐ŸŽ€


โœจ SYNOPSIS โœจ

m4 [-s] [-D name[=val]]... [-U name]... file...

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So the m4 utility is a macro processor, right? What it does is read one or more text files, process them according to their included macro statements, and write the results to standard output. that's the whole main character arc. ๐Ÿ”ฅ


๐Ÿ’… OPTIONS ๐Ÿ’…

The m4 utility shall conform to the Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines โ€” except (and this is the plot twist) that the order of the -D and -U options shall be significant, and options can be interspersed with operands. so like, order matters here, bestie, don't get it twisted. ๐Ÿ™ˆ

The following options shall be supported:

  • -s โ€” Enable line synchronization output for the c99 preprocessor phase (that is, #line directives). yes queen, give us the #line directives. โœจ
  • -D name[=val] โ€” Define name to val, or to null if =val is omitted. no =val? then it's just null, and that's on periodt.
  • -U name โ€” Undefine name. gone. deleted. not me erasing a macro. ๐Ÿ’€

๐Ÿซถ OPERANDS ๐Ÿซถ

The following operand shall be supported:

  • file โ€” A pathname of a text file to be processed. If no file is given, or if it is -, the standard input shall be read. yes, - is the classic "read stdin" move, iykyk. ๐ŸŒˆ

๐ŸŽ€ STDIN ๐ŸŽ€

The standard input shall be a text file that is used if no file operand is given, or if it is -. so basically stdin is the understudy that goes on whenever nobody names a file. ๐Ÿ’œ


๐ŸŒธ INPUT FILES ๐ŸŒธ

The input file named by the file operand shall be a text file. text only, bestie. no funny business. โœจ


๐ŸŒŸ ENVIRONMENT VARIABLES ๐ŸŒŸ

The following environment variables shall affect the execution of m4:

  • LANG โ€” Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ€2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) it's giving backup default energy. ๐Ÿซถ
  • LC_ALL โ€” If set to a non-empty string value, override the values of all the other internationalization variables. main character energy fr fr, it overrides everybody. ๐Ÿ‘‘
  • LC_CTYPE โ€” Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files). ๐Ÿฆ‹
  • LC_MESSAGES โ€” Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. ๐Ÿ’…
  • NLSPATH โ€” Determine the location of message catalogs for the processing of LC_MESSAGES. ๐Ÿ“

๐Ÿ’€ ASYNCHRONOUS EVENTS ๐Ÿ’€

Default. yeah. that's literally the whole section. nothing to see here, moving on bestie. ๐Ÿ™ˆ


๐ŸŒˆ STDOUT ๐ŸŒˆ

The standard output shall be the same as the input files, after being processed for macro expansion. so it's the input, but make it fashion. โœจ


๐Ÿ’œ STDERR ๐Ÿ’œ

The standard error shall be used to display strings with the errprint macro, macro tracing enabled by the traceon macro, the defined text for macros written by the dumpdef macro, or for diagnostic messages. basically stderr is where all the ~drama~ lives. ๐Ÿ˜ญ


๐Ÿ“ OUTPUT FILES ๐Ÿ“

None. nothing. zero. the void. ๐Ÿ•ณ๏ธ


๐Ÿ”ฅ EXTENDED DESCRIPTION ๐Ÿ”ฅ

okay so here's the actual lore, bestie. The m4 utility shall compare each token from the input against the set of built-in and user-defined macros. If the token matches the name of a macro, then the token shall be replaced by the macro's defining text, if any, and rescanned for matching macro names. Once no portion of the token matches the name of a macro, it shall be written to standard output. Macros may have arguments, in which case the arguments shall be substituted into the defining text before it is rescanned. it keeps rescanning until nothing matches anymore โ€” obsessed with the persistence honestly. ๐Ÿ’…

Macro calls have the form:

name(arg1, arg2, ..., argn)

Macro names shall consist of letters, digits, and underscores, where the first character is not a digit. Tokens not of this form shall not be treated as macros. so like, no starting with a digit, that's the rule. periodt. โœจ

The application shall ensure that the <left-parenthesis> immediately follows the name of the macro. If a token matching the name of a macro is not followed by a <left-parenthesis>, it is handled as a use of that macro without arguments.

If a macro name is followed by a <left-parenthesis>, its arguments are the <comma>-separated tokens between the <left-parenthesis> and the matching <right-parenthesis>. Unquoted white-space characters preceding each argument shall be ignored. All other characters, including trailing white-space characters, are retained. <comma> characters enclosed between <left-parenthesis> and <right-parenthesis> characters do not delimit arguments. so leading whitespace gets ghosted ๐Ÿ‘ป but trailing whitespace stays, and commas inside parens are just, like, commas, not separators. got it? ๐Ÿซถ

Arguments are positionally defined and referenced. The string "$1" in the defining text shall be replaced by the first argument. Systems shall support at least nine arguments; only the first nine can be referenced, using the strings "$1" to "$9", inclusive. The string "$0" is replaced with the name of the macro. The string "$#" is replaced by the number of arguments as a string. The string "$*" is replaced by a list of all of the arguments, separated by <comma> characters. The string "$@" is replaced by a list of all of the arguments separated by <comma> characters, and each argument is quoted using the current left and right quoting strings. The string "${" produces unspecified behavior.

ngl, "${" producing unspecified behavior is giving "don't test me, I will not be predictable" energy. ๐Ÿ’€

If fewer arguments are supplied than are in the macro definition, the omitted arguments are taken to be null. It is not an error if more arguments are supplied than are in the macro definition. extra args? fine. missing args? they're just null, no big deal, we're chill here. ๐ŸŒธ

No special meaning is given to any characters enclosed between matching left and right quoting strings, but the quoting strings are themselves discarded. By default, the left quoting string consists of a grave accent (backquote) and the right quoting string consists of an acute accent (single-quote); see also the changequote macro. so by default it's ` and ' doing the quoting, and the quote marks themselves get yeeted. โœจ

Comments are written but not scanned for matching macro names; by default, the begin-comment string consists of the <number-sign> character and the end-comment string consists of a <newline>. See also the changecom and dnl macros. comments pass through untouched, they don't get macro-checked. they're just vibing. ๐Ÿฆ‹

The m4 utility shall make available the following built-in macros. They can be redefined, but once this is done the original meaning is lost. Their values shall be null unless otherwise stated. In the descriptions below, the term defining text refers to the value of the macro: the second argument to the define macro, among other things. Except for the first argument to the eval macro, all numeric arguments to built-in macros shall be interpreted as decimal values. The string values produced as the defining text of the decr, divnum, incr, index, len, and sysval built-in macros shall be in the form of a decimal-constant as defined in the C language.

heads up bestie: redefine a built-in and the original meaning is gone gone, like it never existed. don't say I didn't warn you. ๐Ÿ’…
  • changecom โ€” The changecom macro shall set the begin-comment and end-comment strings. With no arguments, the comment mechanism shall be disabled. With a single non-null argument, that argument shall become the begin-comment and the <newline> shall become the end-comment string. With two non-null arguments, the first argument shall become the begin-comment string and the second argument shall become the end-comment string. The behavior is unspecified if either argument is provided but null. Systems shall support comment strings of at least five characters. so: zero args = comments off, one arg = that arg + newline, two args = begin + end. and yes, five characters minimum, systems gotta support that. ๐ŸŽ€
  • changequote โ€” The changequote macro shall set the begin-quote and end-quote strings. With no arguments, the quote strings shall be set to the default values (that is, `'). The behavior is unspecified if there is a single argument or either argument is null. With two non-null arguments, the first argument shall become the begin-quote string and the second argument shall become the end-quote string. Systems shall support quote strings of at least five characters. one arg or a null arg? unspecified, so like, don't. ๐Ÿ™ˆ
  • decr โ€” The defining text of the decr macro shall be its first argument decremented by 1. It shall be an error to specify an argument containing any non-numeric characters. The behavior is unspecified if decr is not immediately followed by a <left-parenthesis>. minus one, that's the whole job. non-numeric arg = error, no cap. ๐Ÿ˜ญ
  • define โ€” The second argument shall become the defining text of the macro whose name is the first argument. It is unspecified whether the define macro deletes all prior definitions of the macro named by its first argument or preserves all but the current definition of the macro. The behavior is unspecified if define is not immediately followed by a <left-parenthesis>. it's giving "new definition, who dis" and honestly the old ones may or may not still be lurking. ๐Ÿ’€
  • defn โ€” The defining text of the defn macro shall be the quoted definition (using the current quoting strings) of its arguments. The behavior is unspecified if defn is not immediately followed by a <left-parenthesis>. ๐Ÿฆ‹
  • divert โ€” The m4 utility maintains nine temporary buffers, numbered 1 to 9, inclusive. When the last of the input has been processed, any output that has been placed in these buffers shall be written to standard output in buffer-numerical order. The divert macro shall divert future output to the buffer specified by its argument. Specifying no argument or an argument of 0 shall resume the normal output process. Output diverted to a stream with a negative number shall be discarded. Behavior is implementation-defined if a stream number larger than 9 is specified. It shall be an error to specify an argument containing any non-numeric characters. nine buffers, numbered 1 to 9, dumped to stdout in order at the end. negative stream = output gets yeeted into the void. >9 = implementation-defined, so like, roll the dice at your own risk. ๐ŸŽฒ
  • divnum โ€” The defining text of the divnum macro shall be the number of the current output stream as a string. just tellin' you which stream you're on, bestie. โœจ
  • dnl โ€” The dnl macro shall cause m4 to discard all input characters up to and including the next <newline>. it's the "delete to newline" button and it's lowkey so useful. ๐ŸŒˆ
  • dumpdef โ€” The dumpdef macro shall write the defined text to standard error for each of the macros specified as arguments, or, if no arguments are specified, for all macros. no args = dump everything. chaos mode. ๐Ÿ‘‘
  • errprint โ€” The errprint macro shall write its arguments to standard error. The behavior is unspecified if errprint is not immediately followed by a <left-parenthesis>. ๐Ÿ“ข
  • eval โ€” The eval macro shall evaluate its first argument as an arithmetic expression, using signed integer arithmetic with at least 32-bit precision. At least the following C-language operators shall be supported, with precedence, associativity, and behavior as described in Section 1.1.2.1, Arithmetic Precision and Operations:
()
unary +
unary -
~

and bestie, we are so not done with eval yet โ€” there's more where that came from. ๐Ÿ’…โœจ

  • !
  • binary *
  • /
  • %
  • binary +
  • binary -
  • <<
  • >>
  • <
  • <=
  • >
  • >=
  • ==
  • !=
  • binary &
  • ^
  • |
  • &&
  • ||

Bestie, systems shall support octal and hexadecimal numbers, exactly like the ISO C standard does. Slay. โœจ

The second argument, if you give one, sets the radix for the result โ€” and if that argument is blank or you just don't pass it at all, the default is 10. But heads up: behavior is unspecified if the radix falls outside the range 2 to 36, inclusive. So don't be chaotic with it. ๐Ÿซถ

The third argument, if you give one, sets the minimum number of digits in the result. And behavior is unspecified if that third argument is less than zero. Also, it's an error to give a second or third argument containing any non-numeric characters. And the behavior is unspecified if eval is not immediately followed by a <left-parenthesis>. Noted, fr fr.


  • ifdef โ€” if the first argument is defined, the defining text is the second argument. Otherwise the defining text is the third argument if you gave one, or the null string if you didn't. Behavior is unspecified if ifdef is not immediately followed by a <left-parenthesis>. ๐Ÿ’…
  • ifelse โ€” takes three or more arguments, okay?
    • If the first two arguments compare as equal strings (after macro expansion of both of them), the defining text is the third argument.
    • If the first two arguments don't compare as equal strings and there are three arguments, the defining text is null.
    • If the first two arguments don't compare as equal strings and there are four or five arguments, the defining text is the fourth argument.
    • If the first two arguments don't compare as equal strings and there are six or more arguments, the first three arguments get discarded and processing restarts with the remaining arguments.

Behavior is unspecified if ifelse is not immediately followed by a <left-parenthesis>.

  • include โ€” the defining text is the contents of the file named by the first argument. It's an error if the file can't be read. ๐Ÿ˜ญ Behavior is unspecified if include is not immediately followed by a <left-parenthesis>.
  • incr โ€” the defining text is its first argument incremented by 1. It's an error to give an argument containing any non-numeric characters. Behavior is unspecified if incr is not immediately followed by a <left-parenthesis>.
  • index โ€” the defining text is the first character position (as a string) in the first argument where a string matching the second argument begins, zero origin, or -1 if the second argument doesn't occur. Behavior is unspecified if index is not immediately followed by a <left-parenthesis>.
  • len โ€” the defining text is the length (as a string) of the first argument. Behavior is unspecified if len is not immediately followed by a <left-parenthesis>.
  • m4exit โ€” exit from the m4 utility, bye bestie ๐Ÿ‘‹. If the first argument is given, that's the exit code. If no argument is given, the exit code is zero. It's an error to give an argument containing any non-numeric characters. And if the first argument is zero, or no argument is given, and an error has previously occurred (like, say, a file operand that couldn't be opened), it is unspecified whether the exit status is zero or non-zero. Sneaky.
  • m4wrap โ€” the first argument gets processed when EOF is reached. If you use the m4wrap macro multiple times, the arguments you gave get processed in the order in which the m4wrap macros were processed. Behavior is unspecified if m4wrap is not immediately followed by a <left-parenthesis>.
  • maketemp โ€” the defining text is the first argument, with any trailing 'X' characters replaced with the current process ID as a string. Behavior is unspecified if maketemp is not immediately followed by a <left-parenthesis>.
  • mkstemp โ€” the defining text is as if it were the resulting pathname after a successful call to the mkstemp() function defined in the System Interfaces volume of POSIX.1โ€2017, called with the first argument to the macro invocation. If a file is created, that file gets closed. ๐ŸŽ€ If a file could not be created, the m4 utility writes a diagnostic message to standard error and keeps processing input, but its final exit status is non-zero โ€” and the defining text of the macro is the empty string. Behavior is unspecified if mkstemp is not immediately followed by a <left-parenthesis>.
  • popdef โ€” deletes the current definition of its arguments, replacing that definition with the previous one. If there's no previous definition, the macro is undefined. Behavior is unspecified if popdef is not immediately followed by a <left-parenthesis>.
  • pushdef โ€” equivalent to the define macro, except it preserves any current definition for future retrieval using the popdef macro. Behavior is unspecified if pushdef is not immediately followed by a <left-parenthesis>. It's giving "save file before you overwrite," iconic.
  • shift โ€” the defining text is a comma-separated list of its arguments except the first one. Each argument gets quoted using the current quoting strings. Behavior is unspecified if shift is not immediately followed by a <left-parenthesis>.
  • sinclude โ€” equivalent to the include macro, except it's not an error if the file is inaccessible. Chill vibes. Behavior is unspecified if sinclude is not immediately followed by a <left-parenthesis>.
  • substr โ€” the defining text is the substring of the first argument beginning at the zero-offset character position specified by the second argument. The third argument, if given, is the number of characters to select; if you don't give one, the characters from the starting point to the end of the first argument become the defining text. It is not an error to give a starting point beyond the end of the first argument, and in that case the defining text is null. It is an error to give an argument containing any non-numeric characters. Behavior is unspecified if substr is not immediately followed by a <left-parenthesis>.
  • syscmd โ€” interprets its first argument as a shell command line. The defining text is the string result of that command. That string result shall not be rescanned for macros while setting the defining text. No output redirection is performed by the m4 utility. The exit status value from the command can be retrieved using the sysval macro. Behavior is unspecified if syscmd is not immediately followed by a <left-parenthesis>. ๐Ÿš
  • sysval โ€” the defining text is the exit value of the utility last invoked by the syscmd macro, as a string.
  • traceon โ€” enables tracing for the macros specified as arguments, or, if no arguments are specified, for all macros. The trace output is written to standard error in an unspecified format. ๐Ÿ‘€
  • traceoff โ€” disables tracing for the macros specified as arguments, or, if no arguments are specified, for all macros.
  • translit โ€” the defining text is the first argument with every character that occurs in the second argument replaced with the corresponding character from the third argument. If no replacement character is specified for some source character because the second argument is longer than the third argument, that character gets deleted from the first argument in translit's defining text. ๐Ÿ’€

Behavior is unspecified if the '-' character appears within the second or third argument anywhere besides the first or last character. Behavior is unspecified if the same character appears more than once in the second argument. And behavior is unspecified if translit is not immediately followed by a <left-parenthesis>.

  • undefine โ€” deletes all definitions (including those preserved using the pushdef macro) of the macros named by its arguments. Behavior is unspecified if undefine is not immediately followed by a <left-parenthesis>.
  • undivert โ€” causes immediate output of any text in temporary buffers named as arguments, or all temporary buffers if no arguments are specified. Buffers can be undiverted into other temporary buffers. Undiverting discards the contents of the temporary buffer. Behavior is unspecified if an argument contains any non-numeric characters. ๐Ÿฆ‹

๐Ÿ’– EXIT STATUS ๐Ÿ’–

The following exit values shall be returned:

  • 0 โ€” successful completion. โœจ
  • >0 โ€” an error occurred. ๐Ÿ˜ญ

If the m4exit macro is used, the exit value can be specified by the input file.


๐ŸŒธ CONSEQUENCES OF ERRORS ๐ŸŒธ

Default.

The following sections are informative.

๐Ÿ’– APPLICATION USAGE ๐Ÿ’–

okay bestie so the defn macro? iconic for renaming macros, especially the built-ins. that's literally its whole thing. โœจ

now, since eval defers to the ISO C standard, some operations have undefined behavior, and we do NOT play with those ๐Ÿ™ˆ. in some implementations, division or remainder by zero cause a fatal signal โ€” even if that division happens on the short-circuited branch of && or ||. like, rude. any operation that overflows in signed arithmetic produces undefined behavior. likewise, using the shift operators with a shift amount that is not positive and smaller than the precision is undefined, and so is shifting a negative number to the right. no cap, just don't.

and historically? not all implementations obeyed C-language precedence rules, which is giving chaos:

  • ~ and ! were lower than ==
  • == and != were not lower than <
  • | was not lower than ^

the liberal use of () can force the desired precedence even with these non-compliant implementations. so like, parenthesize everything, it's free ๐Ÿ’….

furthermore, some traditional implementations treated ^ as an exponentiation operator, although most implementations now use ** as an extension for this purpose. glow up era fr.

when a macro has been multiply defined via the pushdef macro, it is unspecified whether the define macro will alter only the most recent definition (as though by popdef and pushdef), or replace the entire stack of definitions with a single definition (as though by undefine and pushdef). an application desiring particular behavior for the define macro in this case can redefine it accordingly. ๐Ÿ‘‘

applications should use the mkstemp macro instead of the obsolescent maketemp macro for creating temporary files. maketemp is so last season, bestie, we're not doing that anymore.


๐ŸŒธ EXAMPLES ๐ŸŒธ

if the file m4src contains the lines:

The value of `VER' is "VER".
ifdef(`VER', ``VER'' is defined to be VER., VER is not defined.)
ifelse(VER, 1, ``VER'' is `VER'.)
ifelse(VER, 2, ``VER'' is `VER'., ``VER'' is not 2.)
end

then the command

m4 m4src

or the command:

m4 -U VER m4src

produces the output:

The value of VER is "VER".
VER is not defined.

VER is not 2.
end

the command:

m4 -D VER m4src

produces the output:

The value of VER is "".
VER is defined to be .

VER is not 2.
end

the command:

m4 -D VER=1 m4src

produces the output:

The value of VER is "1".
VER is defined to be 1.
VER is 1.
VER is not 2.
end

the command:

m4 -D VER=2 m4src

produces the output:

The value of VER is "2".
VER is defined to be 2.

VER is 2.
end

like, look at that transformation arc. character development. ๐Ÿฆ‹


๐Ÿง  RATIONALE ๐Ÿง 

historic System V-based behavior treated "${" in a macro definition as two literal characters. however, this sequence is left unspecified so that implementations may offer extensions such as "${11}" meaning the eleventh positional parameter. macros can still be defined with appropriate uses of nested quoting to result in a literal "${" in the output after rescanning removes the nested quotes. iykyk ๐ŸŽ€

in the translit built-in, historic System V-based behavior treated - as a literal; GNU behavior treats it as a range. this version of the standard allows either behavior. both vibes are valid here, we're not picking sides. ๐Ÿ’œ


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None.

that's it. that's the whole section. the future is empty. we love a minimal queen. ๐Ÿ’€


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

  • c99

the Base Definitions volume of POSIX.1โ€2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay so that's the whole page, bestie, we did it ๐Ÿซถ and the little footer at the bottom is from IEEE/The Open Group, dated 2017, for M4(1P) โ€” go forth and macro responsibly, you're the main character of this shell ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from