Next: , Previous: , Up: Miscellaneous   [Contents][Index]


14.2 Printing current location

To make it possible to specify the location of an error, three utility builtins exist:

Builtin: __file__
Builtin: __line__
Builtin: __program__

Expand to the quoted name of the current input file, the current input line number in that file, and the quoted name of the current invocation of m4.

errprint(__program__:__file__:__line__: `input error
')
error→m4:stdin:1: input error
⇒

Line numbers start at 1 for each file. If the file was found due to the -I option or M4PATH environment variable, that is reflected in the file name. The syncline option (-s, see Invoking m4), and the ‘f’ and ‘l’ flags of debugmode (see Debug Levels), also use this notion of current file and line. Redefining the three location macros has no effect on syncline, debug, warning, or error message output.

This example reuses the file incl.m4 mentioned earlier (see Include):

$ m4 -I examples
define(`foo', ``$0' called at __file__:__line__')
⇒
foo
⇒foo called at stdin:2
include(`incl.m4')
⇒Include file start
⇒foo called at examples/incl.m4:2
⇒Include file end
⇒

The location of macros invoked during the rescanning of macro expansion text corresponds to the location in the file where the expansion was triggered, regardless of how many newline characters the expansion text contains. As of GNU M4 1.4.8, the location of text wrapped with m4wrap (see M4wrap) is the point at which the m4wrap was invoked. Previous versions, however, behaved as though wrapped text came from line 0 of the file “”.

define(`echo', `$@')
⇒
define(`foo', `echo(__line__
__line__)')
⇒
echo(__line__
__line__)
⇒4
⇒5
m4wrap(`foo
')
⇒
foo(errprint(__line__
__line__
))
error→8
error→9
⇒8
⇒8
__line__
⇒11
m4wrap(`__line__
')
⇒
^D
⇒12
⇒6
⇒6

The __program__ macro behaves like ‘$0’ in shell terminology. If you invoke m4 through an absolute path or a link with a different spelling, rather than by relying on a PATH search for plain ‘m4’, it will affect how __program__ expands. The intent is that you can use it to produce error messages with the same formatting that m4 produces internally. It can also be used within syscmd (see Syscmd) to pick the same version of m4 that is currently running, rather than whatever version of m4 happens to be first in PATH. It was first introduced in GNU M4 1.4.6.


Next: , Previous: , Up: Miscellaneous   [Contents][Index]