Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
18 KiB

  1. The macro language is a subset of m4. The description of the language
  2. comes from the m4 man page, with annotations in square brackets.
  3. Macro calls have the form:
  4. name(arg1,arg2, ..., argn)
  5. The left parenthesis (() must immediately follow the name of
  6. the macro. If a defined macro name is not followed by a
  7. left parenthesis ((), it is deemed to have no arguments.
  8. Leading unquoted blanks, tabs, and newlines are ignored
  9. while collecting arguments. Potential macro names consist
  10. of alphabetic letters, digits, and underscore _, where the
  11. first character is not a digit.
  12. [ Suffixing an empty set of parentheses to a macro, e.g., `foo()'
  13. results in a macro called with one argument which happens to
  14. be null. This is not the same as `foo', which is a macro called
  15. with zero arguments, although the only way to tell the difference
  16. is to look at `$#' or `$@'. ]
  17. Left and right single quotation marks are used to quote
  18. strings. The value of a quoted string is the string
  19. stripped of the quotation marks.
  20. [ Quotation marks *do* nest. ]
  21. When a macro name is recognized, its arguments are collected
  22. by searching for a matching right parenthesis.
  23. [ `Matching' means that nested parentheses are respected during
  24. argument scanning. ]
  25. Macro
  26. evaluation proceeds normally during the collection of the
  27. arguments, and any commas or right parentheses which happen
  28. to turn up within the value of a nested call are as
  29. effective as those in the original input text. After
  30. argument collection, the value of the macro is pushed back
  31. onto the input stream and rescanned.
  32. [ This means that all macro arguments are scanned at least once.
  33. There is no implicit quoting. This means that you probably want to
  34. quote the arguments to most of the builtin macros. For example,
  35. assuming that `foo' and `baz' are not already defined, then the input
  36. define(foo, bar)
  37. define(baz, blurfl)
  38. define(foo, baz)
  39. undefine(foo, baz)
  40. first defines macros `foo' (which expands to `bar') and `baz'
  41. (which expands to `blurfl'). The third line defines a macro
  42. named `baz' which expands to `blurfl', because the arguments
  43. are macro-expanded before interpretation. And similarly, the
  44. fourth line removes the definitions of `bar' and `blurfl'.
  45. Play it safe. Quote everything that should not be expanded.
  46. Adding quotation marks
  47. define(`foo', `bar')
  48. define(`baz', `blurfl')
  49. define(`foo', `baz')
  50. undefine(`foo', baz')
  51. does not change the meanings of the first two lines, but the
  52. second line redefines `foo' to expand to `baz', and the third
  53. line removes the definitions of `foo' and `baz'.
  54. Note also that the value of the macro is inspected *after*
  55. argument expansion is complete. This means that
  56. define(`foo', `bar')
  57. foo(define(`foo', `baz'))
  58. expands to `baz'. However, under the AT&T implementation,
  59. define(`foo', `bar')
  60. foo(undefine(`foo'))
  61. still expands to `bar' for some reason I do not understand.
  62. Even worse, the GNU implementation displays garbage!
  63. My implementation expands this to `foo'.
  64. Similarly,
  65. define(`foo', `bar')
  66. pushdef(`foo', `baz')
  67. foo(popdef(`foo'))
  68. expands to `baz' under AT&T and to garbage under GNU.
  69. I expand this to `bar'. ]
  70. M4 makes available the following built-in macros. They may
  71. be redefined, but once this is done the original meaning is
  72. lost. Their values are null unless otherwise stated:
  73. define The second argument is installed as the value of
  74. the macro whose name is the first argument.
  75. Each occurrence of $n in the replacement text,
  76. where n is a digit, is replaced by the n-th
  77. argument. Argument 0 is the name of the macro;
  78. missing arguments are replaced by the null
  79. string; $# is replaced by the number of
  80. arguments; $* is replaced by a list of all the
  81. arguments separated by commas; @ $@ is like $*,
  82. but each argument is quoted (with the current
  83. quotation marks).
  84. [ Don't forget to quote both arguments unless you really know
  85. what you're doing. Note also that macro substitution does
  86. not respect quotation marks. Therefore, the macro
  87. define(`quote',``$1'')
  88. quotes its argument. Arguments beyond the second are ignored.
  89. If passed no arguments, does nothing. ]
  90. undefine Removes the definition of the macro named in its
  91. argument.
  92. [ Not documented is that you can pass multiple names to `undefine'
  93. and they will all become undefined. Again, remember to quote
  94. the arguments.
  95. Undefine'ing a macro also destroys its popdef stack. ]
  96. defn Returns the quoted definition of its
  97. argument(s). It is useful for renaming macros,
  98. especially built-ins.
  99. [ Also handy for augmenting an existing macro. Again, remember to
  100. quote the arguments.
  101. Implementation note: AT&T m4 uses bytes with the high bit set
  102. to represent built-ins. If an input file contains characters
  103. with the high bit set, AT&T m4 may core dump.
  104. This implementation uses byte 0x00 to represent built-ins.
  105. An occurrence of 0x00 in the input stream is quoted to 0x00 0x00
  106. so that the out-of-band marker is truly out-of-band.
  107. [ SOMEDAY - Except that they don't look pretty in the output ]
  108. AT&T and GNU do not support embedding defn's of builtins into
  109. macros; you must do a pure definition or it doesn't count.
  110. For example, you can't say
  111. define(`defx',defn(`define')`(x,`$1')')
  112. to make `defx' define the symbol `x' independent of what happened
  113. to the macro `define'.
  114. I support this.
  115. ]
  116. pushdef Like define, but saves any previous definition.
  117. popdef Removes current definition of its argument(s),
  118. exposing the previous one if any.
  119. [ If no definition was pushed, then it acts like `undefine'. ]
  120. ifdef If the first argument is defined, the value is
  121. the second argument, otherwise the third. If
  122. there is no third argument, the value is null.
  123. [ Again, remember to quote the first argument. And you probably want
  124. to quote the second and third arguments to delay their evaluation
  125. until time they are actually needed. ]
  126. shift Returns all but its first argument. The other
  127. arguments are quoted and pushed back with commas
  128. in between. The quoting nullifies the effect of
  129. the extra scan that will subsequently be
  130. performed.
  131. [ Shift is typically used as `shift($@)' to delete the first argument. ]
  132. changequote Changes quotation marks to the first and second
  133. arguments. The symbols may be up to five
  134. characters long. Changequote without arguments
  135. restores the original values.
  136. [ Not implemented. Quotation marks may not contain alphanumerics,
  137. underscores, or whitespace. ]
  138. changecom Changes left and right comment markers from the
  139. default # and newline. With no arguments, the
  140. comment mechanism is effectively disabled. With
  141. one argument, the left marker becomes the
  142. argument and the right marker becomes newline.
  143. With two arguments, both markers are affected.
  144. Comment markers may be up to five characters
  145. long.
  146. [ Not implemented. Comment marks may not contain alphanumerics,
  147. underscores, or whitespace. ]
  148. divert M4 maintains 10 output streams, numbered 0-9.
  149. The final output is the concatenation of the
  150. streams in numerical order; initially stream 0
  151. is the current stream. The divert macro changes
  152. the current output stream to its (digit-string)
  153. argument. Output diverted to a stream other
  154. than 0 through 9 is discarded.
  155. [ Only diversions 0 and -1 are currently supported. ]
  156. undivert Causes immediate output of text from diversions
  157. named as arguments, or all diversions if no
  158. argument. Text may be undiverted into another
  159. diversion. Undiverting discards the diverted
  160. text.
  161. [ Not implemented.
  162. Text being undiverted is not subject to macro scanning. ]
  163. divnum Returns the value of the current output stream.
  164. [ If output is being discarded, then divnum returns -1. ]
  165. dnl Reads and discards characters up to and
  166. including the next newline.
  167. [ dnl is used to embed m4 comments into the file. ]
  168. ifelse Has three or more arguments. If the first
  169. argument is the same string as the second, then
  170. the value is the third argument. If not, and if
  171. there are more than four arguments, the process
  172. is repeated with arguments 4, 5, 6 and 7.
  173. Otherwise, the value is either the fourth
  174. string, or if it is not present, null.
  175. [ In other words,
  176. ifelse(l1,r1,v1,l2,r2,v2,...,ln,rn,vn,v)
  177. If l1 equals r1, then return v1.
  178. Else if l2 equals r2, then return v2.
  179. ...
  180. Else if ln equals rn, then return vn.
  181. Else return v (if provided).
  182. Note that all the v's will be expanded at least once (during scanning
  183. of arguments), and the winning v will be expanded twice (the second
  184. during rescan). Therefore, you probably want to quote all the v's.
  185. Similarly, all the l's and r's will be expanded exactly once,
  186. even the ones that are never used.
  187. AT&T does not raise an error if fewer than three arguments are
  188. provided. ]
  189. incr Returns the value of its argument incremented by
  190. 1. The value of the argument is calculated by
  191. interpreting an initial digit-string as a
  192. decimal number.
  193. decr Returns the value of its argument decremented by
  194. 1.
  195. eval Evaluates its argument as an arithmetic
  196. expression, using 32-bit arithmetic. Operators
  197. include +, -, *, /, %, ^ (exponentiation),
  198. bitwise &, |, ^, and ~; relationals;
  199. parentheses. Octal and hex numbers may be
  200. specified as in C. The second argument
  201. specifies the radix for the result; the default
  202. is 10. The third argument may be used to
  203. specify the minimum number of digits in the
  204. result.
  205. [ The documentation on eval operators is wrong. Eval supports
  206. C operators, which means that ^ denotes bitwise exclusive-or
  207. rather than exponentiation. The exponentiation operator is `**'.
  208. AT&T does not support the ternary ?: operator.
  209. A radix less than 2 is treated as radix 10.
  210. AT&T considers `0x' to eval to zero. I consider it an error.
  211. AT&T allows 8 and 9 as octal digits. I don't.
  212. AT&T generates peculiar output when given extremely large radices,
  213. sometimes resulting in a core dump. Try `eval(100,101)' for example.
  214. This implementation will treat radices greater than 201 as errors.
  215. 201 is the magic number, because base 201 uses digits 0' ... '9',
  216. then 'A' ... '\377'.
  217. Since a leading zero causes the value to be parsed as octal, this
  218. causes problems when parsing values which have been zero-padded.
  219. To force evaluation as decimal, you can use `incr($1)-1', since
  220. `incr' accepts only decimal input. ]
  221. len Returns the number of characters in its
  222. argument.
  223. [ AT&T does not raise an error if too many or too few arguments
  224. are passed. ]
  225. index Returns the position in its first argument where
  226. the second argument begins (zero origin), or -1
  227. if the second argument does not occur.
  228. [ AT&T does not raise an error if too many or too few arguments
  229. are passed. If `index' appears as a bareword, AT&T generates
  230. a null expansion. GNU emits the word `index'.
  231. I side with GNU on this, only because the word `index' frequently
  232. appears in plaintext and it would be bad to see it disappear
  233. randomly. (On the grounds of purity, I should raise an error.) ]
  234. substr Returns a substring of its first argument. The
  235. second argument is a zero origin number
  236. selecting the first character; the third
  237. argument indicates the length of the substring.
  238. A missing third argument is taken to be large
  239. enough to extend to the end of the first string.
  240. translit Transliterates the characters in its first
  241. argument from the set given by the second
  242. argument to the set given by the third. No
  243. abbreviations are permitted.
  244. [ GNU supports ranges in the translit, e.g., 0-9 as shorthand for
  245. 0123456789. I do not.
  246. As a convenience, a bare `translit' is left unexpanded. (GNU)
  247. ]
  248. include Returns the contents of the file named in the
  249. argument.
  250. [ It is not an error if the argument is the null string, in which
  251. case no file is included.
  252. As a convenience, a bare `include' is left unexpanded. (GNU)
  253. ]
  254. sinclude Identical to include, except that it says
  255. nothing if the file is inaccessible.
  256. syscmd Executes the UNIX command given in the first
  257. argument. No value is returned.
  258. [ Not implemented. ]
  259. sysval Is the return code from the last call to syscmd.
  260. [ Not implemented. ]
  261. maketemp Fills in a string of XXXXX in its argument with
  262. the current process ID.
  263. [ Not implemented. ]
  264. m4exit Causes immediate exit from m4. Argument 1, if
  265. given, is the exit code; the default is 0.
  266. [ Not implemented. ]
  267. m4wrap Argument 1 will be pushed back at final end-of-
  268. file; for example, m4wrap(`cleanup()').
  269. [ Not implemented.
  270. The AT&T implementation supports only one wrapup token.
  271. If you try to wrap twice, the second overwrites the first. ]
  272. errprint Prints its argument on the diagnostic output
  273. file.
  274. dumpdef Prints current names and definitions, for the
  275. named items, or for all if no arguments are
  276. given.
  277. [ The dump is made to the diagnostic output file.
  278. The format of the output is
  279. macroname:\tvalue
  280. where \t is an ASCII `tab' character. Built-ins are dumped as
  281. <originalname>
  282. so, for example, after `define(`foo',`baz'defn(`define')`blatz')'
  283. a `dumpdef(`foo')' will display
  284. foo: baz<define>blatz
  285. ]
  286. traceon With no arguments, turns on tracing for all
  287. macros (including built-ins). Otherwise, turns
  288. on tracing for named macros.
  289. [ Not fully implemented.
  290. Trace output is of the form
  291. Trace(n): macroname(args)
  292. where `n' is the macro depth (top-level is depth 0).
  293. We currently do not dump the depth.
  294. ]
  295. traceoff Turns off trace globally and for any macros
  296. specified. Macros specifically traced by
  297. traceon can be untraced only by specific calls
  298. to traceoff.
  299. [ In other words, each macro has a specific `trace' bit which is
  300. controlled by specifying the macro's name in the argument list
  301. of a `traceon' or `traceoff'. There is also a global trace bit
  302. which is turned on by `traceon' with no arguments, and is turned off
  303. by `traceoff' with any number of arguments.
  304. A macro is traced if the global trace bit is on, or if the macro's
  305. private trace bit is on.
  306. The state of the macro's trace bit is pushed and popped by `pushdef'
  307. and `popdef'. If a macro is defined, pushdef'd or undefined,
  308. its trace bit is reset. ]
  309. [ patsubst is a GNU extension which is partially implemented.
  310. (Just barely enough to keep the build happy.)
  311. patsubst Starting with $1, replace all occurrences of $2
  312. with $3. If $3 is omitted, it is taken as the
  313. null string. If $2 is the null string, then $3
  314. is inserted before each character of $1.
  315. NOTE! That this is not the same as GNU. In GNU, $2 is a regular
  316. expression. In our implementation, it is merely a literal.
  317. ]
  318. ******************************************************************************
  319. Implementation details
  320. Input streams
  321. A stack of input streams is maintained, up to the file handle limit
  322. imposed by the operating system. The `include' and `sinclude' macros
  323. push a new file onto the head of the stream. Macro expansion pushes
  324. a string onto the head of the stream.
  325. Comments and quoted strings
  326. Comments and quoted strings are essentially the same thing: They
  327. enclose text that should be passed through without interpretation.
  328. The difference is that quotation marks nest and are stripped,
  329. whereas comments do not nest and are not stripped.