GNU Make手册之M4宏处理语言入门

  |   0 评论   |   2,118 浏览

简介

m4 is a macro processor, in the sense that it copies its input to the output, expanding macros as it goes. Macros are either builtin or user-defined, and can take any number of arguments. Besides just doing macro expansion, m4 has builtin functions for including named files, running shell commands, doing integer arithmetic, manipulating text in various ways, performing recursion, etc.… m4 can be used either as a front-end to a compiler, or as a macro processor in its own right.

M4是一个宏处理器,从某种意义上说,它将输入复制到输出,随着宏的运行而扩展。宏是内建的或用户定义的,可以接受任意数量的参数。除了进行宏展开,m4还有一些内建函数,包括命名文件、运行 shell 命令、执行整数运算、以各种方式处理文本、执行递归等等... ... m4既可以作为编译器的前端,也可以作为宏处理器。

The m4 macro processor is widely available on all UNIXes, and has been standardized by POSIX. Usually, only a small percentage of users are aware of its existence. However, those who find it often become committed users. The popularity of GNU Autoconf, which requires GNU m4 for generating configure scripts, is an incentive for many to install it, while these people will not themselves program in m4. GNU m4 is mostly compatible with the System V, Release 4 version, except for some minor differences. See Compatibility, for more details.

M4宏处理器在所有 unix 上都可以广泛使用,并且已经由 POSIX 标准化。通常,只有一小部分用户知道它的存在。然而,那些发现它的人通常会成为忠实用户。的流行,需要 GNU m 4来生成配置脚本,是一个激励许多人安装它,而这些人不会自己在 m 4中编程。GNU m4基本上与 System v,Release 4版本兼容,除了一些细微的差别。有关详细信息,请参阅兼容性。

Some people find m4 to be fairly addictive. They first use m4 for simple problems, then take bigger and bigger challenges, learning how to write complex sets of m4 macros along the way. Once really addicted, users pursue writing of sophisticated m4 applications even to solve simple problems, devoting more time debugging their m4 scripts than doing real work. Beware that m4 may be dangerous for the health of compulsive programmers.

有些人发现 m4相当容易上瘾。他们首先使用 m4解决简单的问题,然后接受越来越大的挑战,一路学习如何编写复杂的 m4宏集。一旦真正上瘾,用户追求编写复杂的 m4应用程序,甚至解决简单的问题,花费更多的时间调试他们的 m4脚本比做真正的工作。请注意,m4可能对强迫症程序员的健康有害。

基本语法

example(string,[ count = ‘1’] ,[ argument ] ...)

注意:

  • 宏名与括号之间不能有空格,除非这个宏真的是没有参数.
  • 如上的参数 string是一个必须的参数,因此它至少需要一个参数.
  • 用方括号括起来的参数表明是可选的参数. 如果这个参数count在没有传的时候,表明其有一个默认值.
  • M4中的所有宏参数都是字符串,但有些参数有特殊的解释,例如数字、文件名、正则表达式等。每个宏的文档将说明参数是如何解释的,以及如果参数不能按照所需的解释进行解析会发生什么。除非另有说明,否则指定为数字的参数将被解析为小数,即使参数前面有前导零; 并且将空字符串解析为数字将导致0而不是错误,尽管会发出警告。

下面来看一下M4软件的基本信息.

m4 --help
Usage: /Library/Developer/CommandLineTools/usr/bin/m4 [OPTION]... [FILE]...
Process macros in FILEs.  If no FILE or if FILE is `-', standard input
is read.

Mandatory or optional arguments to long options are mandatory or optional
for short options too.

Operation modes:
      --help                   display this help and exit
      --version                output version information and exit
  -E, --fatal-warnings         stop execution after first warning
  -e, --interactive            unbuffer output, ignore interrupts
  -P, --prefix-builtins        force a `m4_' prefix to all builtins
  -Q, --quiet, --silent        suppress some warnings for builtins

Preprocessor features:
  -D, --define=NAME[=VALUE]    define NAME has having VALUE, or empty
  -I, --include=DIRECTORY      append DIRECTORY to include path
  -s, --synclines              generate `#line NUM "FILE"' lines
  -U, --undefine=NAME          undefine NAME

Limits control:
  -G, --traditional            suppress all GNU extensions
  -H, --hashsize=PRIME         set symbol lookup hash table size [509]
  -L, --nesting-limit=NUMBER   change artificial nesting limit [1024]

Frozen state files:
  -F, --freeze-state=FILE      produce a frozen state on FILE at end
  -R, --reload-state=FILE      reload a frozen state from FILE at start

Debugging:
  -d, --debug[=FLAGS]          set debug level (no FLAGS implies `aeq')
  -l, --arglength=NUM          restrict macro tracing size
  -o, --error-output=FILE      redirect debug and trace output
  -t, --trace=NAME             trace NAME when it will be defined

FLAGS is any of:
  a   show actual arguments
  c   show before collect, after collect and after call
  e   show expansion
  f   say current input file name
  i   show changes in input files
  l   say current input line number
  p   show results of path searches
  q   quote values as necessary, with a or e flag
  t   trace for all macro calls, not only traceon'ed
  x   add a unique macro call id, useful with c flag
  V   shorthand for all of the above flags

If defined, the environment variable `M4PATH' is a colon-separated list
of directories included after any specified by `-I'.

Exit status is 0 for success, 1 for failure, 63 for frozen file version
mismatch, or whatever value was passed to the m4exit macro.

Report bugs to <bug-m4@gnu.org>.
[macbook@lijianhongs-MacBook-Pro-2 ~]$m4 --version
GNU M4 1.4.6
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Rene' Seindal.

调用语法

m4 [option…] [file…]

All options begin with ‘-’, or if long option names are used, with ‘--’. A long option name need not be written completely, any unambiguous prefix is sufficient. POSIX requires m4 to recognize arguments intermixed with files, even when POSIXLY_CORRECT is set in the environment. Most options take effect at startup regardless of their position, but some are documented below as taking effect after any files that occurred earlier in the command line. The argument -- is a marker to denote the end of options.

所有选项都以‘-’开头,如果使用长选项名称,则以‘--’开头。不需要完全编写 long 选项名,任何明确的前缀都足够了。POSIX 要求 m4识别与文件混合的参数,即使在环境中设置了 POSIX _ correct。大多数选项在启动时生效,不管它们的位置如何,但是下面的文档记录了一些选项在命令行中出现的任何文件之后生效。这个参数 -- 是一个标志,表示选项的结束。

With short options, options that do not take arguments may be combined into a single command line argument with subsequent options, options with mandatory arguments may be provided either as a single command line argument or as two arguments, and options with optional arguments must be provided as a single argument. In other words, m4 -QPDfoo -d a -df is equivalent to m4 -Q -P -D foo -d -df -- ./a, although the latter form is considered canonical.

使用 short 选项,不带参数的选项可以与后续选项组合成单个命令行参数,带强制参数的选项可以作为单个命令行参数或两个参数提供,带可选参数的选项必须作为单个参数提供。换句话说,m4-QPDfoo-d a-df等价于 m4-q-p-d foo-d-df -- ./a,尽管后一种形式被认为是规范的。

With long options, options with mandatory arguments may be provided with an equal sign (‘=’) in a single argument, or as two arguments, and options with optional arguments must be provided as a single argument. In other words, m4 --def foo --debug a is equivalent to m4 --define=foo --debug= -- ./a, although the latter form is considered canonical (not to mention more robust, in case a future version of m4 introduces an option named --default).

对于长参数,可以在单个参数中提供具有强制参数的选项,或者提供两个参数中的等号(‘ =’) ,并且必须在单个参数中提供具有可选参数的选项。换句话说,m4 --def foo --debug a 等价于 m4 --define=foo --debug= -- ./a,尽管后一种形式被认为是规范的(更不用说更健壮的了,以防将来的 m4版本引入一个名为 --default 的选项)。

宏名定义规范

A name is any sequence of letters, digits, and the character ‘_’ (underscore), where the first character is not a digit. m4 will use the longest such sequence found in the input. If a name has a macro definition, it will be subject to macro expansion (see Macros). Names are case-sensitive.

名称是任何字母、数字和字符“ _”(下划线)的序列,其中第一个字符不是数字。M4将使用在输入中找到的最长的这样的序列。如果名称具有宏定义,则该名称将进行宏展开(请参阅宏)。名称区分大小写。

Examples of legal names are: ‘foo’, ‘_tmp’, and ‘name01’.

合法名称的例子有: ‘ foo’、‘ tmp’和‘ name01’。

m4输入中的注释

Comments in m4 are normally delimited by the characters ‘#’ and newline. All characters between the comment delimiters are ignored, but the entire comment (including the delimiters) is passed through to the output—comments are not discarded by m4.

M4中的注释通常由“ #”和换行符分隔。注释分隔符之间的所有字符都将被忽略,但是整个注释(包括分隔符)将被传递到输出ー注释不会被 m4丢弃。

Comments cannot be nested, so the first newline after a ‘#’ ends the comment. The commenting effect of the begin-comment string can be inhibited by quoting it.

注释不能嵌套,因此在 # 后面的第一个换行符结束了注释。可以通过引用 begin-comment 字符串来抑制它的注释效果。

Other kinds of input tokens

Any character, that is neither a part of a name, nor of a quoted string, nor a comment, is a token by itself. When not in the context of macro expansion, all of these tokens are just copied to output. However, during macro expansion, whitespace characters (space, tab, newline, formfeed, carriage return, vertical tab), parentheses (‘(’ and ‘)’), comma (‘,’), and dollar (‘$’) have additional roles, explained later.

任何既不是名称的一部分、也不是引用的字符串或注释的字符本身都是标记。当不在宏扩展的上下文中时,所有这些标记都会被复制到输出。然而,在宏展开过程中,空格字符(空格、制表符、换行符、 formfeed、回车符、垂直制表符)、圆括号(‘(’和‘)’)、逗号(‘ ,’)和美元(‘ $’)都有其他的角色,稍后解释。

评论

发表评论


取消