Search code examples
m4text-manipulation

String-length in the M4 preprocessor


So, with this simple input,

define(foo, len($1)) foo(abcdef)

I get, as output:

 2

How can I get this to print 5, instead? I can't figure out any combination of quoting that makes len() actually receive the value of $1, abcdef, instead of the literal string `$1'.

Edit 1: The actual code in question looks something like this:

define(`FILE', `#' /!\ $1 /!\
`#' ====substr(==============================,0,len($1))====)dnl
FILE(`UTILITY.ASM')

Solution

  • The under-quoting of len($1) leads to its immediate evaluation during the define step so foo is defined as 2.

    I.e. this is equivalent to:

    define(foo, 2) foo(abcdef)
    

    When full quoting, the results are what you are expecting:

    define(`foo', `len($1)') foo(`abcdef')
    > 6