1.\" @(#) $OpenBSD: m4.1,v 1.59 2010/10/21 13:20:51 jmc Exp $ 2.\" 3.\" Copyright (c) 1989, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Ozan Yigit at York University. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.Dd $Mdocdate: October 21 2010 $ 34.Dt M4 1 35.Os 36.Sh NAME 37.Nm m4 38.Nd macro language processor 39.Sh SYNOPSIS 40.Nm 41.Op Fl gPs 42.Oo 43.Sm off 44.Fl D Ar name Op No = Ar value 45.Sm on 46.Oc 47.Op Fl d Ar flags 48.Op Fl I Ar dirname 49.Op Fl o Ar filename 50.Bk -words 51.Op Fl t Ar macro 52.Op Fl U Ns Ar name 53.Op Ar 54.Ek 55.Sh DESCRIPTION 56The 57.Nm 58utility is a macro processor that can be used as a front end to any 59language (e.g., C, ratfor, fortran, lex, and yacc). 60If no input files are given, 61.Nm 62reads from the standard input, 63otherwise files specified on the command line are 64processed in the given order. 65Input files can be regular files, files in the m4 include paths, or a 66single dash 67.Pq Sq - , 68denoting standard input. 69.Nm 70writes 71the processed text to the standard output, unless told otherwise. 72.Pp 73Macro calls have the form name(argument1[, argument2, ..., argumentN]). 74.Pp 75There cannot be any space following the macro name and the open 76parenthesis 77.Pq Sq \&( . 78If the macro name is not followed by an open 79parenthesis it is processed with no arguments. 80.Pp 81Macro names consist of a leading alphabetic or underscore 82possibly followed by alphanumeric or underscore characters, e.g., 83valid macro names match the pattern 84.Dq [a-zA-Z_][a-zA-Z0-9_]* . 85.Pp 86In arguments to macros, leading unquoted space, tab, and newline 87.Pq Sq \en 88characters are ignored. 89To quote strings, use left and right single quotes 90.Po e.g.,\ \& 91.Sq "\ this is a string with a leading space" 92.Pc . 93You can change the quote characters with the 94.Ic changequote 95built-in macro. 96.Pp 97Most built-ins don't make any sense without arguments, and hence are not 98recognized as special when not followed by an open parenthesis. 99.Pp 100The options are as follows: 101.Bl -tag -width Ds 102.It Fl D Ns Ar name Ns Oo 103.Pf = Ns Ar value 104.Oc 105Define the symbol 106.Ar name 107to have some value (or 108.Dv NULL ) . 109.It Fl d Ar "flags" 110Set trace flags. 111.Ar flags 112may hold the following: 113.Bl -tag -width Ds 114.It Ar a 115print macro arguments. 116.It Ar c 117print macro expansion over several lines. 118.It Ar e 119print result of macro expansion. 120.It Ar f 121print filename location. 122.It Ar l 123print line number. 124.It Ar q 125quote arguments and expansion with the current quotes. 126.It Ar t 127start with all macros traced. 128.It Ar x 129number macro expansions. 130.It Ar V 131turn on all options. 132.El 133.Pp 134By default, trace is set to 135.Qq eq . 136.It Fl g 137Activate GNU-m4 compatibility mode. 138In this mode, translit handles simple character 139ranges (e.g., a-z), regular expressions mimic emacs behavior, 140multiple m4wrap calls are handled as a stack, 141the number of diversions is unlimited, 142empty names for macro definitions are allowed, 143and eval understands 144.Sq 0rbase:value 145numbers. 146.It Fl I Ar "dirname" 147Add directory 148.Ar dirname 149to the include path. 150.It Fl o Ar filename 151Send trace output to 152.Ar filename . 153.It Fl P 154Prefix all built-in macros with 155.Sq m4_ . 156For example, instead of writing 157.Ic define , 158use 159.Ic m4_define . 160.It Fl s 161Output line synchronization directives, suitable for 162.Xr cpp 1 . 163.It Fl t Ar macro 164Turn tracing on for 165.Ar macro . 166.It Fl "U" Ns Ar "name" 167Undefine the symbol 168.Ar name . 169.El 170.Sh SYNTAX 171.Nm 172provides the following built-in macros. 173They may be redefined, losing their original meaning. 174Return values are null unless otherwise stated. 175.Bl -tag -width changequote 176.It Fn builtin name 177Calls a built-in by its 178.Fa name , 179overriding possible redefinitions. 180.It Fn changecom startcomment endcomment 181Changes the start comment and end comment sequences. 182Comment sequences may be up to five characters long. 183The default values are the hash sign 184and the newline character. 185.Bd -literal -offset indent 186# This is a comment 187.Ed 188.Pp 189With no arguments, comments are turned off. 190With one single argument, the end comment sequence is set 191to the newline character. 192.It Fn changequote beginquote endquote 193Defines the open quote and close quote sequences. 194Quote sequences may be up to five characters long. 195The default values are the backquote character and the quote 196character. 197.Bd -literal -offset indent 198`Here is a quoted string' 199.Ed 200.Pp 201With no arguments, the default quotes are restored. 202With one single argument, the close quote sequence is set 203to the newline character. 204.It Fn decr arg 205Decrements the argument 206.Fa arg 207by 1. 208The argument 209.Fa arg 210must be a valid numeric string. 211.It Fn define name value 212Define a new macro named by the first argument 213.Fa name 214to have the 215value of the second argument 216.Fa value . 217Each occurrence of 218.Sq $n 219(where 220.Ar n 221is 0 through 9) is replaced by the 222.Ar n Ns 'th 223argument. 224.Sq $0 225is the name of the calling macro. 226Undefined arguments are replaced by a null string. 227.Sq $# 228is replaced by the number of arguments; 229.Sq $* 230is replaced by all arguments comma separated; 231.Sq $@ 232is the same as 233.Sq $* 234but all arguments are quoted against further expansion. 235.It Fn defn name ... 236Returns the quoted definition for each argument. 237This can be used to rename 238macro definitions (even for built-in macros). 239.It Fn divert num 240There are 10 output queues (numbered 0-9). 241At the end of processing 242.Nm 243concatenates all the queues in numerical order to produce the 244final output. 245Initially the output queue is 0. 246The divert 247macro allows you to select a new output queue (an invalid argument 248passed to divert causes output to be discarded). 249.It Ic divnum 250Returns the current output queue number. 251.It Ic dnl 252Discard input characters up to and including the next newline. 253.It Fn dumpdef name ... 254Prints the names and definitions for the named items, or for everything 255if no arguments are passed. 256.It Fn errprint msg 257Prints the first argument on the standard error output stream. 258.It Fn esyscmd cmd 259Passes its first argument to a shell and returns the shell's standard output. 260Note that the shell shares its standard input and standard error with 261.Nm . 262.It Fn eval expr 263Computes the first argument as an arithmetic expression using 32-bit 264arithmetic. 265Operators are the standard C ternary, arithmetic, logical, 266shift, relational, bitwise, and parentheses operators. 267You can specify 268octal, decimal, and hexadecimal numbers as in C. 269The second argument (if any) 270specifies the radix for the result and the third argument (if any) 271specifies the minimum number of digits in the result. 272.It Fn expr expr 273This is an alias for 274.Ic eval . 275.It Fn format formatstring arg1 ... 276Returns 277.Fa formatstring 278with escape sequences substituted with 279.Fa arg1 280and following arguments, in a way similar to 281.Xr printf 3 . 282This built-in is only available in GNU-m4 compatibility mode, and the only 283parameters implemented are there for autoconf compatibility: 284left-padding flag, an optional field width, a maximum field width, 285*-specified field widths, and the %s and %c data type. 286.It Fn ifdef name yes no 287If the macro named by the first argument is defined then return the second 288argument, otherwise the third. 289If there is no third argument, the value is 290.Dv NULL . 291The word 292.Qq unix 293is predefined. 294.It Fn ifelse a b yes ... 295If the first argument 296.Fa a 297matches the second argument 298.Fa b 299then 300.Fn ifelse 301returns 302the third argument 303.Fa yes . 304If the match fails the three arguments are 305discarded and the next three arguments are used until there is 306zero or one arguments left, either this last argument or 307.Dv NULL 308is returned if no other matches were found. 309.It Fn include name 310Returns the contents of the file specified in the first argument. 311If the file is not found as is, look through the include path: 312first the directories specified with 313.Fl I 314on the command line, then the environment variable 315.Ev M4PATH , 316as a colon-separated list of directories. 317Include aborts with an error message if the file cannot be included. 318.It Fn incr arg 319Increments the argument by 1. 320The argument must be a valid numeric string. 321.It Fn index string substring 322Returns the index of the second argument in the first argument (e.g., 323.Ic index(the quick brown fox jumped, fox) 324returns 16). 325If the second 326argument is not found index returns \-1. 327.It Fn indir macro arg1 ... 328Indirectly calls the macro whose name is passed as the first argument, 329with the remaining arguments passed as first, ... arguments. 330.It Fn len arg 331Returns the number of characters in the first argument. 332Extra arguments 333are ignored. 334.It Fn m4exit code 335Immediately exits with the return value specified by the first argument, 3360 if none. 337.It Fn m4wrap todo 338Allows you to define what happens at the final 339.Dv EOF , 340usually for cleanup purposes (e.g., 341.Ic m4wrap("cleanup(tempfile)") 342causes the macro cleanup to be 343invoked after all other processing is done). 344.Pp 345Multiple calls to 346.Fn m4wrap 347get inserted in sequence at the final 348.Dv EOF . 349.It Fn maketemp template 350Like 351.Ic mkstemp . 352.It Fn mkstemp template 353Invokes 354.Xr mkstemp 3 355on the first argument, and returns the modified string. 356This can be used to create unique 357temporary file names. 358.It Fn paste file 359Includes the contents of the file specified by the first argument without 360any macro processing. 361Aborts with an error message if the file cannot be 362included. 363.It Fn patsubst string regexp replacement 364Substitutes a regular expression in a string with a replacement string. 365Usual substitution patterns apply: an ampersand 366.Pq Sq \&& 367is replaced by the string matching the regular expression. 368The string 369.Sq \e# , 370where 371.Sq # 372is a digit, is replaced by the corresponding back-reference. 373.It Fn popdef arg ... 374Restores the 375.Ic pushdef Ns ed 376definition for each argument. 377.It Fn pushdef macro def 378Takes the same arguments as 379.Ic define , 380but it saves the definition on a 381stack for later retrieval by 382.Fn popdef . 383.It Fn regexp string regexp replacement 384Finds a regular expression in a string. 385If no further arguments are given, 386it returns the first match position or \-1 if no match. 387If a third argument 388is provided, it returns the replacement string, with sub-patterns replaced. 389.It Fn shift arg1 ... 390Returns all but the first argument, the remaining arguments are 391quoted and pushed back with commas in between. 392The quoting 393nullifies the effect of the extra scan that will subsequently be 394performed. 395.It Fn sinclude file 396Similar to 397.Ic include , 398except it ignores any errors. 399.It Fn spaste file 400Similar to 401.Fn paste , 402except it ignores any errors. 403.It Fn substr string offset length 404Returns a substring of the first argument starting at the offset specified 405by the second argument and the length specified by the third argument. 406If no third argument is present it returns the rest of the string. 407.It Fn syscmd cmd 408Passes the first argument to the shell. 409Nothing is returned. 410.It Ic sysval 411Returns the return value from the last 412.Ic syscmd . 413.It Fn traceon arg ... 414Enables tracing of macro expansions for the given arguments, or for all 415macros if no argument is given. 416.It Fn traceoff arg ... 417Disables tracing of macro expansions for the given arguments, or for all 418macros if no argument is given. 419.It Fn translit string mapfrom mapto 420Transliterate the characters in the first argument from the set 421given by the second argument to the set given by the third. 422You cannot use 423.Xr tr 1 424style abbreviations. 425.It Fn undefine name1 ... 426Removes the definition for the macros specified by its arguments. 427.It Fn undivert arg ... 428Flushes the named output queues (or all queues if no arguments). 429.It Ic unix 430A pre-defined macro for testing the OS platform. 431.It Ic __line__ 432Returns the current file's line number. 433.It Ic __file__ 434Returns the current file's name. 435.El 436.Sh EXIT STATUS 437.Ex -std m4 438.Pp 439But note that the 440.Ic m4exit 441macro can modify the exit status. 442.Sh STANDARDS 443The 444.Nm 445utility is mostly compliant with the 446.St -p1003.1-2008 447specification. 448.Pp 449The flags 450.Op Fl dgIot 451and the macros 452.Ic builtin , 453.Ic esyscmd , 454.Ic expr , 455.Ic format , 456.Ic indir , 457.Ic paste , 458.Ic patsubst , 459.Ic regexp , 460.Ic spaste , 461.Ic unix , 462.Ic __line__ , 463and 464.Ic __file__ 465are extensions to that specification. 466.Pp 467.Ic maketemp 468is not supposed to be a synonym for 469.Ic mkstemp , 470but instead to be an insecure temporary file name creation function. 471The change causes no known compatibility issues. 472.Pp 473The output format of tracing and of 474.Ic dumpdef 475are not specified in any standard, 476are likely to change and should not be relied upon. 477The current format of tracing is closely modelled on 478.Nm gnu-m4 , 479to allow 480.Nm autoconf 481to work. 482.Pp 483The built-ins 484.Ic pushdef 485and 486.Ic popdef 487handle macro definitions as a stack. 488However, 489.Ic define 490interacts with the stack in an undefined way. 491In this implementation, 492.Ic define 493replaces the top-most definition only. 494Other implementations may erase all definitions on the stack instead. 495.Pp 496All built-ins do expand without arguments in many other 497.Nm . 498.Pp 499Many other 500.Nm 501have dire size limitations with respect to buffer sizes. 502.Sh AUTHORS 503.An -nosplit 504.An Ozan Yigit Aq oz@sis.yorku.ca 505and 506.An Richard A. O'Keefe Aq ok@goanna.cs.rmit.OZ.AU . 507.Pp 508GNU-m4 compatibility extensions by 509.An Marc Espie Aq espie@cvs.openbsd.org . 510