1.\" @(#) $OpenBSD: m4.1,v 1.63 2015/09/14 20:06:58 schwarze 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: September 14 2015 $ 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.Op Fl t Ar macro 51.Op Fl U Ns Ar name 52.Op Ar 53.Sh DESCRIPTION 54The 55.Nm 56utility is a macro processor that can be used as a front end to any 57language (e.g., C, ratfor, fortran, lex, and yacc). 58If no input files are given, 59.Nm 60reads from the standard input, 61otherwise files specified on the command line are 62processed in the given order. 63Input files can be regular files, files in the m4 include paths, or a 64single dash 65.Pq Sq - , 66denoting standard input. 67.Nm 68writes 69the processed text to the standard output, unless told otherwise. 70.Pp 71Macro calls have the form name(argument1[, argument2, ..., argumentN]). 72.Pp 73There cannot be any space following the macro name and the open 74parenthesis 75.Pq Sq \&( . 76If the macro name is not followed by an open 77parenthesis it is processed with no arguments. 78.Pp 79Macro names consist of a leading alphabetic or underscore 80possibly followed by alphanumeric or underscore characters, e.g., 81valid macro names match the pattern 82.Dq [a-zA-Z_][a-zA-Z0-9_]* . 83.Pp 84In arguments to macros, leading unquoted space, tab, and newline 85.Pq Sq \en 86characters are ignored. 87To quote strings, use left and right single quotes 88.Pq e.g., Sq \ \&this is a string with a leading space . 89You can change the quote characters with the 90.Ic changequote 91built-in macro. 92.Pp 93Most built-ins don't make any sense without arguments, and hence are not 94recognized as special when not followed by an open parenthesis. 95.Pp 96The options are as follows: 97.Bl -tag -width Ds 98.It Fl D Ns Ar name Ns Op = Ns Ar value 99Define the symbol 100.Ar name 101to have some value (or 102.Dv NULL ) . 103.It Fl d Ar "flags" 104Set trace flags. 105.Ar flags 106may hold the following: 107.Bl -tag -width Ds 108.It Ar a 109print macro arguments. 110.It Ar c 111print macro expansion over several lines. 112.It Ar e 113print result of macro expansion. 114.It Ar f 115print filename location. 116.It Ar l 117print line number. 118.It Ar q 119quote arguments and expansion with the current quotes. 120.It Ar t 121start with all macros traced. 122.It Ar x 123number macro expansions. 124.It Ar V 125turn on all options. 126.El 127.Pp 128By default, trace is set to 129.Qq eq . 130.It Fl g 131Activate GNU-m4 compatibility mode. 132In this mode, translit handles simple character 133ranges (e.g., a-z), regular expressions mimic emacs behavior, 134multiple m4wrap calls are handled as a stack, 135the number of diversions is unlimited, 136empty names for macro definitions are allowed, 137and eval understands 138.Sq 0rbase:value 139numbers. 140.It Fl I Ar "dirname" 141Add directory 142.Ar dirname 143to the include path. 144.It Fl o Ar filename 145Send trace output to 146.Ar filename . 147.It Fl P 148Prefix all built-in macros with 149.Sq m4_ . 150For example, instead of writing 151.Ic define , 152use 153.Ic m4_define . 154.It Fl s 155Output line synchronization directives, suitable for 156.Xr cpp 1 . 157.It Fl t Ar macro 158Turn tracing on for 159.Ar macro . 160.It Fl "U" Ns Ar "name" 161Undefine the symbol 162.Ar name . 163.El 164.Sh SYNTAX 165.Nm 166provides the following built-in macros. 167They may be redefined, losing their original meaning. 168Return values are null unless otherwise stated. 169.Bl -tag -width changequote 170.It Fn builtin name 171Calls a built-in by its 172.Fa name , 173overriding possible redefinitions. 174.It Fn changecom startcomment endcomment 175Changes the start comment and end comment sequences. 176Comment sequences may be up to five characters long. 177The default values are the hash sign 178and the newline character. 179.Bd -literal -offset indent 180# This is a comment 181.Ed 182.Pp 183With no arguments, comments are turned off. 184With one single argument, the end comment sequence is set 185to the newline character. 186.It Fn changequote beginquote endquote 187Defines the open quote and close quote sequences. 188Quote sequences may be up to five characters long. 189The default values are the backquote character and the quote 190character. 191.Bd -literal -offset indent 192`Here is a quoted string' 193.Ed 194.Pp 195With no arguments, the default quotes are restored. 196With one single argument, the close quote sequence is set 197to the newline character. 198.It Fn decr arg 199Decrements the argument 200.Fa arg 201by 1. 202The argument 203.Fa arg 204must be a valid numeric string. 205.It Fn define name value 206Define a new macro named by the first argument 207.Fa name 208to have the 209value of the second argument 210.Fa value . 211Each occurrence of 212.Sq $n 213(where 214.Ar n 215is 0 through 9) is replaced by the 216.Ar n Ns 'th 217argument. 218.Sq $0 219is the name of the calling macro. 220Undefined arguments are replaced by a null string. 221.Sq $# 222is replaced by the number of arguments; 223.Sq $* 224is replaced by all arguments comma separated; 225.Sq $@ 226is the same as 227.Sq $* 228but all arguments are quoted against further expansion. 229.It Fn defn name ... 230Returns the quoted definition for each argument. 231This can be used to rename 232macro definitions (even for built-in macros). 233.It Fn divert num 234There are 10 output queues (numbered 0-9). 235At the end of processing 236.Nm 237concatenates all the queues in numerical order to produce the 238final output. 239Initially the output queue is 0. 240The divert 241macro allows you to select a new output queue (an invalid argument 242passed to divert causes output to be discarded). 243.It Ic divnum 244Returns the current output queue number. 245.It Ic dnl 246Discard input characters up to and including the next newline. 247.It Fn dumpdef name ... 248Prints the names and definitions for the named items, or for everything 249if no arguments are passed. 250.It Fn errprint msg 251Prints the first argument on the standard error output stream. 252.It Fn esyscmd cmd 253Passes its first argument to a shell and returns the shell's standard output. 254Note that the shell shares its standard input and standard error with 255.Nm . 256.It Fn eval expr[,radix[,minimum]] 257Computes the first argument as an arithmetic expression using 32-bit 258arithmetic. 259Operators are the standard C ternary, arithmetic, logical, 260shift, relational, bitwise, and parentheses operators. 261You can specify 262octal, decimal, and hexadecimal numbers as in C. 263The optional second argument 264.Fa radix 265specifies the radix for the result and the optional third argument 266.Fa minimum 267specifies the minimum number of digits in the result. 268.It Fn expr expr 269This is an alias for 270.Ic eval . 271.It Fn format formatstring arg1 ... 272Returns 273.Fa formatstring 274with escape sequences substituted with 275.Fa arg1 276and following arguments, in a way similar to 277.Xr printf 3 . 278This built-in is only available in GNU-m4 compatibility mode, and the only 279parameters implemented are there for autoconf compatibility: 280left-padding flag, an optional field width, a maximum field width, 281*-specified field widths, and the %s and %c data type. 282.It Fn ifdef name yes no 283If the macro named by the first argument is defined then return the second 284argument, otherwise the third. 285If there is no third argument, the value is 286.Dv NULL . 287The word 288.Qq unix 289is predefined. 290.It Fn ifelse a b yes ... 291If the first argument 292.Fa a 293matches the second argument 294.Fa b 295then 296.Fn ifelse 297returns 298the third argument 299.Fa yes . 300If the match fails the three arguments are 301discarded and the next three arguments are used until there is 302zero or one arguments left, either this last argument or 303.Dv NULL 304is returned if no other matches were found. 305.It Fn include name 306Returns the contents of the file specified in the first argument. 307If the file is not found as is, look through the include path: 308first the directories specified with 309.Fl I 310on the command line, then the environment variable 311.Ev M4PATH , 312as a colon-separated list of directories. 313Include aborts with an error message if the file cannot be included. 314.It Fn incr arg 315Increments the argument by 1. 316The argument must be a valid numeric string. 317.It Fn index string substring 318Returns the index of the second argument in the first argument (e.g., 319.Ic index(the quick brown fox jumped, fox) 320returns 16). 321If the second 322argument is not found index returns \-1. 323.It Fn indir macro arg1 ... 324Indirectly calls the macro whose name is passed as the first argument, 325with the remaining arguments passed as first, ... arguments. 326.It Fn len arg 327Returns the number of characters in the first argument. 328Extra arguments 329are ignored. 330.It Fn m4exit code 331Immediately exits with the return value specified by the first argument, 3320 if none. 333.It Fn m4wrap todo 334Allows you to define what happens at the final 335.Dv EOF , 336usually for cleanup purposes (e.g., 337.Ic m4wrap("cleanup(tempfile)") 338causes the macro cleanup to be 339invoked after all other processing is done). 340.Pp 341Multiple calls to 342.Fn m4wrap 343get inserted in sequence at the final 344.Dv EOF . 345.It Fn maketemp template 346Like 347.Ic mkstemp . 348.It Fn mkstemp template 349Invokes 350.Xr mkstemp 3 351on the first argument, and returns the modified string. 352This can be used to create unique 353temporary file names. 354.It Fn paste file 355Includes the contents of the file specified by the first argument without 356any macro processing. 357Aborts with an error message if the file cannot be 358included. 359.It Fn patsubst string regexp replacement 360Substitutes a regular expression in a string with a replacement string. 361Usual substitution patterns apply: an ampersand 362.Pq Sq \&& 363is replaced by the string matching the regular expression. 364The string 365.Sq \e# , 366where 367.Sq # 368is a digit, is replaced by the corresponding back-reference. 369.It Fn popdef arg ... 370Restores the 371.Ic pushdef Ns ed 372definition for each argument. 373.It Fn pushdef macro def 374Takes the same arguments as 375.Ic define , 376but it saves the definition on a 377stack for later retrieval by 378.Fn popdef . 379.It Fn regexp string regexp replacement 380Finds a regular expression in a string. 381If no further arguments are given, 382it returns the first match position or \-1 if no match. 383If a third argument 384is provided, it returns the replacement string, with sub-patterns replaced. 385.It Fn shift arg1 ... 386Returns all but the first argument, the remaining arguments are 387quoted and pushed back with commas in between. 388The quoting 389nullifies the effect of the extra scan that will subsequently be 390performed. 391.It Fn sinclude file 392Similar to 393.Ic include , 394except it ignores any errors. 395.It Fn spaste file 396Similar to 397.Fn paste , 398except it ignores any errors. 399.It Fn substr string offset length 400Returns a substring of the first argument starting at the offset specified 401by the second argument and the length specified by the third argument. 402If no third argument is present it returns the rest of the string. 403.It Fn syscmd cmd 404Passes the first argument to the shell. 405Nothing is returned. 406.It Ic sysval 407Returns the return value from the last 408.Ic syscmd . 409.It Fn traceon arg ... 410Enables tracing of macro expansions for the given arguments, or for all 411macros if no argument is given. 412.It Fn traceoff arg ... 413Disables tracing of macro expansions for the given arguments, or for all 414macros if no argument is given. 415.It Fn translit string mapfrom mapto 416Transliterate the characters in the first argument from the set 417given by the second argument to the set given by the third. 418You cannot use 419.Xr tr 1 420style abbreviations. 421.It Fn undefine name1 ... 422Removes the definition for the macros specified by its arguments. 423.It Fn undivert arg ... 424Flushes the named output queues (or all queues if no arguments). 425.It Ic unix 426A pre-defined macro for testing the OS platform. 427.It Ic __line__ 428Returns the current file's line number. 429.It Ic __file__ 430Returns the current file's name. 431.El 432.Sh EXIT STATUS 433.Ex -std m4 434.Pp 435But note that the 436.Ic m4exit 437macro can modify the exit status. 438.Sh STANDARDS 439The 440.Nm 441utility is compliant with the 442.St -p1003.1-2008 443specification. 444.Pp 445The flags 446.Op Fl dgIPot 447and the macros 448.Ic builtin , 449.Ic esyscmd , 450.Ic expr , 451.Ic format , 452.Ic indir , 453.Ic paste , 454.Ic patsubst , 455.Ic regexp , 456.Ic spaste , 457.Ic unix , 458.Ic __line__ , 459and 460.Ic __file__ 461are extensions to that specification. 462.Pp 463.Ic maketemp 464is not supposed to be a synonym for 465.Ic mkstemp , 466but instead to be an insecure temporary file name creation function. 467It is marked by 468.St -p1003.1-2008 469as being obsolescent and should not be used if portability is a concern. 470.Pp 471The output format of 472.Ic traceon 473and 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 Mt oz@sis.yorku.ca 505and 506.An Richard A. O'Keefe Aq Mt ok@goanna.cs.rmit.OZ.AU . 507.Pp 508GNU-m4 compatibility extensions by 509.An Marc Espie Aq Mt espie@cvs.openbsd.org . 510