GNU M4
M4 = "Macro Processor, Version 4".
1, Introduction to GNU M4
GNU M4 是传统 Unix 宏处理器的实现。它主要与 SVR4 兼容,但也有一些扩展功能(例如,处理超过 9 个位置参数的宏命令)。GNU M4 还内置了包含文件、运行 shell 命令、运算等功能。
GNU M4 is an implementation of the traditional Unix macro processor. It is mostly SVR4 compatible although it has some extensions (for example, handling more than 9 positional parameters to macros). GNU M4 also has built-in functions for including files, running shell commands, doing arithmetic, etc.
GNU M4 是一个宏处理器,它可以将输入复制到输出,并不断扩展宏。宏可以是内置的,也可以是用户自定义的,并且可以接受任意数量的参数。除了进行宏扩展外,m4 还具有内置函数,可用于包含已命名文件、运行 UNIX 命令、进行整数运算、以各种方式处理文本、递归等......m4 既可用作编译器的前端,也可单独用作宏处理器。
GNU 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 UNIX commands, doing integer arithmetic, manipulating text in various ways, recursion etc... m4 can be used either as a front-end to a compiler or as a macro processor in its own right.
GNU M4 的最大用户之一是 GNU Autoconf 项目。
One of the biggest users of GNU M4 is the GNU Autoconf project.
2, Downloading M4
git clone git://git.sv.gnu.org/m4
git clone http://git.savannah.gnu.org/r/m4.git
followed by:
git checkout -b branch-1.4 origin/branch-1.4
3, Documentation
4, Mailing Lists
您可以如下所述通过网络订阅任何 GNU 邮件列表。或者,您也可以向相关请求列表发送一封空邮件,在邮件标题行注明 “订阅”。例如,要订阅 bug-m4 列表,您可以向 <
bug-m4-request@gnu.org> 发送一封没有正文、标题行仅为 “订阅 ”的邮件。
You can subscribe to any GNU mailing list via the web as described below. Or you can send an empty mail with a Subject: header line of just "subscribe" to the relevant request list. For example, to subscribe yourself to the bug-m4 list, you would send mail to <
bug-m4-request@gnu.org > with no body and a Subject: header line of just "subscribe".
为防止垃圾邮件泛滥,有必要对 GNU M4 邮件列表进行管理。向邮件列表发布的内容由列表版主负责发布。有时,版主会在短时间内无法工作。请耐心等待。如果您在列表存档中没有看到该邮件,则说明该邮件没有被发布。
It has been necessary to moderate the GNU M4 mailing lists to prevent the flood of spam. Postings to the lists are held for release by the list moderator. Sometimes the moderators are unavailable for brief periods of time. Please be patient when posting. If you don't see the message in the list archive then it did not get posted.
5, Announcements
低容量邮件列表 m4-announce 包含有关 GNU M4 的所有公告。有关 M4 和大多数其他 GNU 软件的重要公告也在 <
info-gnu@gnu.org> 上发布。
The low-volume mailing list m4-announce contains all announcements about GNU M4. Important announcements about M4 and most other GNU Software are also made on <
info-gnu@gnu.org >.
6, Tracking Development
中型邮件列表 bug-m4 跟踪所有错误报告。有关提交错误报告的更多信息,请参阅下面的 “报告错误 ”部分。
The moderate-volume mailing list bug-m4 tracks all bug reports. For more information on submitting bugs, please see the section Report a Bug below.
中型邮件列表 m4-patches 用于提出和跟踪所有重要补丁。GNU M4 正在积极开发中,2.0 版本将有许多新功能,如更好的输入控制、多精度运算和可加载模块。有关 GNU M4 未来发展的更多信息,请访问
GNU M4 - Summary [Savannah]。您可以使用 git 跟踪开发进程:
The moderate-volume mailing list m4-patches is used to propose and track all significant patches. GNU M4 is being actively developed, and version 2.0 will have many new features, such as better input control, multiple precision arithmetic and loadable modules. More information about the future of GNU M4 is at
GNU M4 - Summary [Savannah]. You can track development in git, using:
git clone git://git.sv.gnu.org/m4
git clone http://git.savannah.gnu.org/r/m4.git
另外,这里还有一个只读的 CVS 镜像:
Alternatively, there is a read-only CVS mirror here:
cvs -d :pserver:anonymous@pserver.git.sv.gnu.org:/m4.git \
co -d m4 HEAD
M4的使用和学习
Here’s a simple usage guide for GNU M4 with basic examples:
Step-by-Step:
Example 1
Simple Usage of GNU M4
1. Install GNU M4
Make sure M4 is installed. You can check by typing:
If not installed, install it via your package manager (as described earlier).
2. Creating a Simple M4 Script
Let's create a basic file `example.m4` with simple macros.
define(`PI', `3.14159')
define(`greeting', `Hello, World!')
PI is the value of pi.
greeting
This file contains two macros:
- `PI` with the value `3.14159`
- `greeting` with the value `Hello, World!`
3. Running the M4 Script
To process the file using M4, open a terminal and run:
The output will be:
3.14159 is the value of pi.
Hello, World!
Example 2: Using Arguments in Macros
You can pass arguments to macros. In this example, let's define a macro that takes a name as an argument.
1. Create a file `greet.m4`:
define(`greet', `Hello, $1!')
greet(`Alice')
greet(`Bob')
2. Run the M4 script:
The output will be:
Hello, Alice!
Hello, Bob!
Example 3: Simple Conditional with `ifelse`
You can use simple conditionals with `ifelse`.
1. Create a file `condition.m4`:
ifelse(`yes', `yes', `The answer is yes.', `The answer is no.')
2. Run the M4 script:
Output:
这些是如何使用 GNU M4 定义宏、使用参数和条件的简单示例。它是一个非常通用的文本处理工具!
These are simple examples of how to define macros, use arguments, and conditionals with GNU M4. It’s a very versatile tool for text processing!
参考: