m4 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

m4 命令安装:


-bash/zsh: m4 command not found

#Debian
apt-get install m4

#Ubuntu
apt-get install m4

#Alpine
apk add m4

#Arch Linux
pacman -S m4

#Kali Linux
apt-get install m4

#CentOS
yum install m4

#Fedora
dnf install m4

#OS X
brew install m4

#Raspbian
apt-get install m4

#Docker
docker run cmd.cat/m4 m4

m4 命令补充说明:


GNU M4 是传统 Unix 宏处理器的实现。尽管它有一些扩展(例如,处理宏的 9 个以上的位置参数),但它主要与 SVR4 兼容。GNU M4 还具有用于包含文件、运行 shell 命令、进行算术等的内置函数。

GNU M4 是一个宏处理器,因为它在运行时将其输入复制到输出扩展宏。宏要么是内置的,要么是用户定义的,可以接受任意数量的参数。除了只进行宏扩展,m4 还具有内置函数,用于包含命名文件、运行 UNIX 命令、进行整数运算、以各种方式处理文本、递归等...... m4 既可以用作编译器的前端,也可以用作编译器的前端宏处理器本身。

GNU M4 的最大用户之一是 GNU Autoconf项目。

m4 命令语法:


m4 [ -e] [ -l ] [ -s ] [ -B Number ] [ -D Name [ =Value ] ] ... [ -H Number ] [ -I Directory ] [ -S Number ] [ -T Number ] [ -U Name ] ... [ File ... ]

m4 命令操作模式:


--help
display this help and exit

--version
output version information and exit

-E, --fatal-warnings
once: warnings become errors, twice: stop execution at first error

-i, --interactive
unbuffer output, ignore interrupts

-P, --prefix-builtins
force a `m4_' prefix to all builtins

-Q, --quiet, --silent
suppress some warnings for builtins

--warn-macro-sequence[=REGEXP]
warn if macro definition matches REGEXP,
default \$\({[^}]*}\|[0-9][0-9]+\)

m4 命令特点:


-D, --define=NAME[=VALUE]
define NAME as having VALUE, or empty

-I, --include=DIRECTORY
append DIRECTORY to include path

-s, --synclines
generate `#line NUM "FILE"' lines

-U, --undefine=NAME
undefine NAME

m4 命令 Limits control:


-g, --gnu
override -G to re-enable GNU extensions

-G, --traditional
suppress all GNU extensions

-H, --hashsize=PRIME
set symbol lookup hash table size [509]

-L, --nesting-limit=NUMBER
change nesting limit, 0 for unlimited [0]

m4 命令 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

m4 命令 Debugging:


-d, --debug[=FLAGS]
set debug level (no FLAGS implies `aeq')

--debugfile[=FILE]
redirect debug and trace output to FILE (default stderr, discard if empty string)

-l, --arglength=NUM
restrict macro tracing size

-t, --trace=NAME
trace NAME when it is defined

m4 命令 FLAGS:


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.  

m4 命令参数:


目标文件

m4 命令实例:


m4 处理文件中的宏:

m4 path/to/file

m4 在处理文件之前定义一个宏:

m4 -Dmacro_name=macro_value path/to/file

m4 应用 HTML 例子,首先,启动一个 HTML 页面的宏:

define(`START_HTML',
`<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;
   charset=iso-8859-1">
  <meta name="Author" content="D. Robert Adams">
  <title>$1</title>
</head>
<body text="#000000"
  ifdef(`BACKGROUND_IMAGE',
        `background="BACKGROUND_IMAGE"')
  bgcolor="#e5e5e5" link="#3333ff"
vlink="#000099"
  alink="#ffffff">
')

接下来是页眉宏:

define(`PAGE_HEADER',
`<table border="0" background="steel.jpg"
width="100%">
  <tr>
    <td align="left">$1</td>
    <td align="right">$2</td>
  </tr>
</table>
<div align=right>
  Last Modified: esyscmd(`date')
</div>
')

接下来,我们创建宏以在页面内制作部分横幅:

define(`HTML_BANNER',
`<table border="0"
background="steel.jpg"
width="100%">
  <tr>
    <td>
      <img src="$2">
      <h2>$1</h2>
    </td>
  </tr>
</table>
')

最后一部分是关闭 HTML “body” 和 “html” 标签的宏:

define(`END_HTML', `</body></html>')

鉴于以上 4 个宏,创建一个网页是非常容易的。创建一个文件 index.m4,其中包含对我们的 HTML 宏的调用。唯一新的是插入我们的 HTML 宏的包含宏:

include(`html.m4')
define(`BACKGROUND_IMAGE', `bg.jpg')
START_HTML(`Sample Page')
PAGE_HEADER(`computer.jpg', `Sample HTML
Page')
HTML_BANNER(`img1.jpg', `News')
HTML_BANNER(`img2.jpg', `Downloads')
END_HTML

最后一步就是运行 m4 来创建 HTML 页面,命令是:

m4 index.m4 > index.html

m4 命令扩展阅读:




m4 命令评论