1.\" Copyright (c) 1990, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" The code is derived from software contributed to Berkeley by 5.\" Ozan Yigit at York University. 6.\" 7.\" %sccs.include.proprietary.man% 8.\" 9.\" @(#)m4.1 8.1 (Berkeley) 06/06/93 10.\" 11.Dd 12.Os ATT 7 13.Dt M4 1 14.Sh NAME 15.Nm m4 16.Nd macro language preprocessor for 17.Xr ratfor 1 18and 19.Xr pascal 1 20.Sh SYNOPSIS 21.Nm m4 22.Op options 23.Sh DESCRIPTION 24.Nm M4 25is a macro language preprocessor for Ratfor, Pascal, and similar languages 26which do not have a built-in macro processing capability. 27.Nm M4 28reads standard input, and writes the results to the standard output. 29.Pp 30The options and their effects are as follows: 31.Pp 32.Sm off 33.Bl -tag -width _Dname[=Val] 34.It Fl D Ar name Op Ar \&=Val 35.Sm on 36Defines 37.Ar name 38to 39.Ar val 40or to null in 41the absence of 42.Ar val . 43.It Fl U Ns Ar name 44Undefines 45.Ar name . 46.El 47.Pp 48The 49.Nm m4 50processor provides a kind of 51.Nm C 52like syntax and 53some of the macro functions will 54be familiar: 55.Bl -tag -width \&undiver 56.It Ic define 57.Ar define(name [, val]) 58the second argument is installed 59as the value of the macro 60whose name is the first argument. 61If there is no second argument, 62the value is null. 63Each occurrence of 64.Ic $ Ns Ar n 65in the replacement text, 66where 67.Ar n 68is a digit, 69is replaced by the 70.Ar n Ns 'th 71argument. 72Argument 0 is the name of the macro; 73missing arguments are replaced by the null string. 74.It Ic defn 75.Ar defn(name [, name ...]) 76returns the quoted definition 77of its argument(s). 78Useful in renaming macros. 79.It Ic undefine 80.Ar undefine(name [, name ...]) 81removes the definition of 82the macro(s) named. 83If there is 84more than one definition for the named macro, (due to previous use of 85.Ic pushdef ) 86all definitions are removed. 87.It Ic pushdef 88.Ar pushdef(name [, val]) 89like 90.Ic define , 91but saves any previous definition by stacking the current definition. 92.It Ic popdef 93.Ar popdef(name [, name ...]) 94removes current definition of its argument(s), 95exposing the previous one if any. 96.It Ic ifdef 97.Ar ifdef(name, if-def [, ifnot-def]) 98if the first argument is defined, 99the value is the second argument, 100otherwise the third. 101If there is no third argument, the value is null. 102A word indicating the current operating system is predefined 103(e.g. 104.I unix 105or 106.IR vms ). 107.It Ic shift 108.Ar shift(arg, arg, arg, ...) 109returns all but its first argument. 110The other arguments are quoted and pushed back with 111commas in between. 112The quoting nullifies the effect of the extra scan that 113will subsequently be performed. 114.It Ic changequote 115.Ar changequote(lqchar, rqchar) 116change quote symbols to the 117first and second arguments. 118With no arguments, the quotes are reset back to the default 119characters (i.e., 120.Ic \*(ga 121and 122.Ic \*(aa ). 123.It Ic changecom 124.Ar changecom(lcchar, rcchar) 125change left and right 126comment markers from the default 127.Ic # 128and 129.Ic newline . 130With no arguments, the comment mechanism is reset back to 131the default characters. 132With one argument, the left marker becomes the argument and 133the right marker becomes newline. 134With two arguments, both markers are affected. 135.It Ic divert 136.Ar divert(divnum) 137.Nm m4 138maintains 10 output streams, 139numbered 0-9. initially stream 0 is the current stream. 140The 141.Ic divert 142macro changes the current output stream to its (digit-string) 143argument. 144Output diverted to a stream other than 0 through 9 145disappears into bitbucket. 146.It Ic undivert 147.Ar undivert([divnum [, divnum ...]) 148causes immediate output 149of text from diversions named as 150argument(s), or all diversions if no argument. 151Text may be undiverted into another diversion. 152Undiverting discards the diverted text. At the end of input processing, 153.Nm M4 154forces an automatic 155.Ic undivert , 156unless 157.Ic m4wrap 158is defined. 159.It Ic divnum 160.Ar divnum() 161returns the value of the current output stream. 162.It Ic dnl 163.Ar dnl() 164reads and discards characters up to and including the next newline. 165.It Ic ifelse 166.Ar ifelse(arg, arg, if-same [, ifnot-same \&| arg,\ arg\ ...]) 167has 168three or more arguments. 169If the first argument is the same string as the second, 170then the value is the third argument. 171If not, and if there are more than four arguments, the process is 172repeated with arguments 4, 5, 6 and 7. 173Otherwise, the value is either the fourth string, or, if it is not present, 174null. 175.It Ic incr 176.Ar incr(num) 177returns the value of its argument incremented by 1. 178The value of the argument is calculated 179by interpreting an initial digit-string as a decimal number. 180.It Ic decr 181.Ar decr(num) 182returns the value of its argument decremented by 1. 183.It Ic eval 184.Ar eval(expression) 185evaluates its argument as a constant expression, 186using integer arithmetic. 187The evaluation mechanism is very similar to that of 188.Xr cpp 189(#if expression). 190The expression can involve only integer constants and character constants, 191possibly connected by the binary operators 192.Bd -literal -offset indent 193* / % + - >> << < > 194 <= >= == != & ^ && 195.Ed 196.Pp 197or the unary operators 198.Ic \&~ \&! 199or by the ternary operator 200.Ic \&? \&: . 201Parentheses may be used for grouping. Octal numbers may be specified as 202in C. 203.It Ic len 204.Ar len(string) 205returns the number of characters in its argument. 206.It Ic index 207.Ar index(search-string, string) 208returns the position 209in its first argument where the second argument 210begins (zero origin), 211or \-1 if the second argument does not occur. 212.It Ic substr 213.Ar substr(string, index [, length]) 214returns a substring of its first argument. 215The second argument is a zero origin 216number selecting the first character (internally treated as an expression); 217the third argument indicates the length of the substring. 218A missing third argument is taken to be large enough to extend to 219the end of the first string. 220.It Ic translit 221.Ar translit(source, from [, to]) 222transliterates 223the characters in its first argument 224from the set given by the second argument to the set given by the third. 225If the third argument is shorter than the second, all extra characters 226in the second argument are deleted from the first argument. If the third 227argument is missing altogether, all characters in the second argument are 228deleted from the first argument. 229.It Ic include 230.Ar include(filename) 231returns 232the contents of the file named in the argument. 233.It Ic sinclude 234.Ar sinclude(filename) 235is identical to 236.Ic include , 237except that it 238says nothing if the file is inaccessible. 239.It Ic paste 240.Ar paste(filename) 241returns 242the contents of the file named in the argument without any 243processing, unlike 244.Ic include . 245.It Ic spaste 246.Ar spaste(filename) 247is identical to 248.Ic paste , 249except that it says nothing if the file is inaccessible. 250.It Ic syscmd 251.Ar syscmd(command) 252executes the 253.Ux 254command given in the first argument. 255No value is returned. 256.It Ic sysval 257.Ar sysval() 258is the return code from the last call to 259.Ic syscmd . 260.It Ic maketemp 261.Ar maketemp(string) 262fills in a string of 263.Li XXXXXX 264in its argument with the current process 265ID. 266.It Ic m4exit 267.Ar m4exit([exitcode]) 268causes immediate exit from 269.Nm m4 . 270Argument 1, if given, is the exit code; 271the default is 0. 272.It Ic m4wrap 273.Ar m4wrap(m4-macro-or-built-in) 274argument 1 will be pushed back at final 275.Ic EOF ; 276.Dl example: m4wrap(`dumptable()'). 277.It Ic errprint 278.Ar errprint(str [, str, str, ...]) 279prints its 280argument(s) on stderr. If there is more than one argument, 281each argument is separated by a space during the output. 282.It Ic dumpdef 283.Ar dumpdef([name, name, ...]) 284prints current names and definitions, 285for the named items, or for all if no arguments are given. 286.El 287.Sh AUTHOR 288Ozan S. Yigit (oz) 289.Sh BUGS 290A sufficiently complex 291.Nm M4 292macro set is about as readable 293as 294.Tn APL . 295.Pp 296All complex uses of 297.Nm M4 298require the ability to program in deep recursion. 299Previous lisp experience is recommended. 300.Sh EXAMPLES 301The following macro program illustrates the type of things that 302can be done with 303.Nm M4 . 304.Bd -literal -offset indent 305changequote(<,>) define(HASHVAL,99) dnl 306define(hash,<expr(str(substr($1,1),0)%HASHVAL)>) dnl 307define(str, 308 <ifelse($1,",$2, 309 \t<str(substr(<$1>,1),<expr($2+'substr($1,0,1)')>)>) 310 >) dnl 311define(KEYWORD,<$1,hash($1),>) dnl 312define(TSTART, 313<struct prehash { 314 char *keyword; 315 int hashval; 316} keytab[] = {>) dnl 317define(TEND,< "",0 318};>) 319dnl 320.Ed 321.Pp 322Thus a keyword table containing the keyword string and its pre-calculated 323hash value may be generated thus: 324.Bd -literal -offset indent 325TSTART 326 KEYWORD("foo") 327 KEYWORD("bar") 328 KEYWORD("baz") 329TEND 330.Ed 331.Pp 332which will expand into: 333.Bd -literal -offset indent 334struct prehash { 335 char *keyword; 336 int hashval; 337} keytab[] = { 338 "foo",27, 339 "bar",12, 340 "baz",20, 341 "",0 342}; 343.Ed 344.Pp 345Presumably, such a table would speed up the installation of the 346keywords into a dynamic hash table. (Note that the above macro 347cannot be used with 348.Nm m4 , 349since 350.Ic eval 351does not handle character constants.) 352.Sh SEE ALSO 353.Xr cpp 1 354.Rs 355.%T "The M4 Macro Processor" 356.%A B. W. Kernighan 357.%A D. M. Ritchie. 358.Re 359.Sh HISTORY 360An 361.Nm M4 362command appeared in 363.At v7 . 364The 365.Nm M4 366command this page describes is derived from code contributed by 367Ozan S. Yigit. 368