How do I escape text in autoconf/m4?
Asked Answered
M

3

15

The following code from my configuration.ac file does not work (note the nested square brackets with [default=no]):

AC_ARG_ENABLE(debug,
    [  --enable-debug          build with debugging support [default=no].],
    [DEBUG="$enableval"],
    [DEBUG="no"]
)

How can I escape those brackets?

Mimamsa answered 22/2, 2010 at 4:4 Comment(2)
I don't see any Bash. Why the tag?Trexler
Autoconf scripts are parsed first by m4 and then by the bourne shell.Mimamsa
M
25

Found it! From this tutorial:

M4 arguments are quoted with [ and ]. There is NO WAY to escape these, however, you have several options if you wish to insert ['s or ]'s:

  1. Use a `Quadrigraph'. @<:@ gives you [ and @:>@ gives you ].
  2. Balance your quotes. M4 will turn [[]] in to []. Beware of using this in arguments to macros. Sometimes, you need to double quote as well ([[[]]]).
  3. Change the quoting using: changequote(<<,>>) to change the quoting to << and >>. The autoconf documentation (rightly, in my opinion) warns against the (over) use of this, since it can lead to unexpected results.
  4. Avoid [ and ] where ever possible. This is my personal choice.

My new code is therefore:

AC_ARG_ENABLE(debug,
    AS_HELP_STRING(
        [--enable-debug],
        [build with debugging symbols @<:@default=no@:>@]),
    [enable_debug="$enableval"],
    [enable_debug="no"]
)
Mimamsa answered 22/2, 2010 at 7:21 Comment(3)
You should set your own answer as THE answer then.Freshet
Autotools absolutely sucks.Hexameter
The quoted tutorial has a new home.Inferential
H
9

Brackets are kind of escape characters, so as you do for '\', you may escape brackets [] with brackets, eg :

AC_ARG_ENABLE(debug, [ --enable-debug build with debugging support [[default=no]].], [DEBUG="$enableval"], [DEBUG="no"] )

Note: [[ ]default=no[ ]] may not work as you expect as m4 should search the end_bracket from the end. It should so be expanded to [ ]default=no[ ].

Hercule answered 21/11, 2012 at 12:22 Comment(1)
Better to use the 'quadrigraph' escapes, @<:@ and @>:@.Bobbyebobbysocks
R
-1

use AC_HELP_STRING

Rome answered 22/2, 2010 at 4:9 Comment(4)
Thanks (that is definitely cleaner) but still the same problem. In <<AS_HELP_STRING([--enable-debug],[build with debugging support [default=no].])>> the "[" and "]" are missing from output.Mimamsa
Why the insistence on square brackets in the output? Use parentheses; it makes your life easier.Politics
For consistency's sake, because other packages I'm using have square brackets in their output :(Mimamsa
AC_HELP_STRING been deprecated about half a decade ago. Use AS_HELP_STRING.Renown

© 2022 - 2024 — McMap. All rights reserved.