home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
bbs
/
gnu
/
m4-1.1-bin.lha
/
info
/
m4.info-2
(
.txt
)
< prev
Wrap
GNU Info File
|
1994-02-24
|
38KB
|
823 lines
This is Info file m4.info, produced by Makeinfo-1.47 from the input
file m4.texinfo.
This file documents the GNU m4 utility.
Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation,
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions.
File: m4.info, Node: Diversions, Next: Text handling, Prev: File Inclusion, Up: Top
Diverting and undiverting output
********************************
Diversions are a way of temporarily saving output. The output of
`m4' can at any time be diverted to a temporary file, and be reinserted
into the output stream, "undiverted", again at a later time.
Up to ten numbered diversions (numbered from 0 to 9) are supported in
`m4', of which diversion number 0 is the normal output stream. The
number of available diversions can be increased with the `-N' option.
* Menu:
* Divert:: Diverting output
* Undivert:: Undiverting output
* Divnum:: Diversion numbers
* Cleardiv:: Discarding diverted text
File: m4.info, Node: Divert, Next: Undivert, Prev: Diversions, Up: Diversions
Diverting output
================
Output is diverted using `divert':
divert(opt NUMBER)
where NUMBER is the diversion to be used. If NUMBER is left out, it is
assumed to be zero.
The expansion of `divert' is void.
Diverted output, that hasn't been explicitly undiverted, will be
automatically undiverted to the main output stream when all the input
has been processed.
divert(1)
This text is diverted.
divert
=>
This text is not diverted.
=>This text is not diverted.
^D
=>
=>This text is diverted.
Several calls of `divert' with the same argument do not overwrite
the previous diverted text, but append to it.
If output is diverted to a non-existent diversion, it is simply
discarded. This can be used to suppress unwanted output. A common
example of unwanted output is the trailing newlines after macro
definitions. Here is how to avoid them.
divert(-1)
define(`foo', `Macro `foo'.')
define(`bar', `Macro `bar'.')
divert
=>
This is a common programming idiom in `m4'.
File: m4.info, Node: Undivert, Next: Divnum, Prev: Divert, Up: Diversions
Undiverting output
==================
Diverted text can be undiverted explicitly using the built-in
`undivert':
undivert(opt NUMBER, ...)
which undiverts the diversions given by the arguments, in the order
given. If no arguments are supplied, all diversions are undiverted, in
numerical order.
The expansion of `undivert' is void.
divert(1)
This text is diverted.
divert
=>
This text is not diverted.
=>This text is not diverted.
undivert(1)
=>
=>This text is diverted.
=>
Notice the last two blank lines. One of them comes from the newline
following `undivert', the other from the newline that followed the
`divert'! A diversion often starts with a blank line like this.
When diverted text is undiverted, it is *not* reread by `m4', but
rather copied directly to the current output, and it is therefore not
an error to undivert into a diversion.
When a diversion has been undiverted, the diverted text is discarded,
and it is not possible to bring back diverted text more than once.
divert(1)
This text is diverted first.
divert(0)undivert(1)dnl
=>
=>This text is diverted first.
undivert(1)
=>
divert(1)
This text is also diverted but not appended.
divert(0)undivert(1)dnl
=>
=>This text is also diverted but not appended.
Attempts to undivert the current diversion are silently ignored.
GNU `m4' allows named files to be undiverted. Given a non-numeric
argument, the contents of the file named will be copied, uninterpreted,
to the current output. This complements the built-in `include' (*note
Include::.). To illustrate the difference, assume the file `foo'
contains the word `bar':
define(`bar', `BAR')
=>
undivert(`foo')
=>bar
=>
include(`foo')
=>BAR
=>
File: m4.info, Node: Divnum, Next: Cleardiv, Prev: Undivert, Up: Diversions
Diversion numbers
=================
The built-in `divnum':
divnum
expands to the number of the current diversion.
Initial divnum
=>Initial 0
divert(1)
Diversion one: divnum
divert(2)
Diversion two: divnum
divert
=>
^D
=>
=>Diversion one: 1
=>
=>Diversion two: 2
The last call of `divert' without argument is necessary, since the
undiverted text would otherwise be diverted itself.
File: m4.info, Node: Cleardiv, Prev: Divnum, Up: Diversions
Discarding diverted text
========================
Often it is not known, when output is diverted, whether the diverted
text is actually needed. Since all non-empty diversion are brought back
on the main output stream when the end of input is seen, a method of
discarding a diversion is needed. If all diversions should be
discarded, the easiest is to end the input to `m4' with `divert(-1)'
followed by an explicit `undivert':
divert(1)
Diversion one: divnum
divert(2)
Diversion two: divnum
divert(-1)
undivert
^D
No output is produced at all.
Clearing selected diversions can be done with the following macro:
define(`cleardivert',
`pushdef(`_num', divnum)divert(-1)undivert($@)divert(_num)popdef(`_num')')
=>
It is called just like `undivert', but the effect is to clear the
diversions, given by the arguments. (This macro has a nasty bug! You
should try to see if you can find it and correct it.)
File: m4.info, Node: Text handling, Next: Arithmetic, Prev: Diversions, Up: Top
Macros for text handling
************************
There are a number of built-ins in `m4' for manipulating text in
various ways, extracting substrings, searching, substituting, and so on.
* Menu:
* Len:: Calculating length of strings
* Index:: Searching for substrings
* Regexp:: Searching for regular expressions
* Substr:: Extracting substrings
* Translit:: Translating characters
* Patsubst:: Substituting text by regular expression
* Format:: Formatting strings (printf-like)
File: m4.info, Node: Len, Next: Index, Prev: Text handling, Up: Text handling
Calculating length of strings
=============================
The length of a string can be calculated by `len':
len(STRING)
which expands to the length of STRING, as a decimal number.
len()
=>0
len(`abcdef')
=>6
The builtin macro `len' is recognized only when given arguments.
File: m4.info, Node: Index, Next: Regexp, Prev: Len, Up: Text handling
Searching for substrings
========================
Searching for substrings is done with `index':
index(STRING, SUBSTRING)
which expands to the index of the first occurrence of SUBSTRING in
STRING. The first character in STRING has index 0. If SUBSTRING does
not occur in STRING, `index' expands to `-1'.
index(`gnus, gnats, and armadillos', `nat')
=>7
index(`gnus, gnats, and armadillos', `dag')
=>-1
The builtin macro `index' is recognized only when given arguments.
File: m4.info, Node: Regexp, Next: Substr, Prev: Index, Up: Text handling
Searching for regular expressions
=================================
Searching for regular expressions is done with the built-in `regexp':
regexp(STRING, REGEXP, opt REPLACEMENT)
which searches for REGEXP in STRING. The syntax for regular
expre