2

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')
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
  • I'm not seeing what this has to do with [c], its [c-preprocessor], or [preprocessor]s in general (m4 is a general-purpose macro language / macro processor). Please update your question either to show how it is related to those tags or to remove those tags. – John Bollinger Jun 22 '16 at 20:41
  • M4 is usually suggested (often on SO!) as a replacement for people trying to do do complex things with the CPP. I'm leaving the tags, as it seems that, pragmatically speaking, any M4 question is an extension of a CPP question; but I won't be offended if someone wishes to remove them. ¯\\_(ツ)_/¯ – ELLIOTTCABLE Jun 22 '16 at 20:42

1 Answers1

3

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
lossleader
  • 13,182
  • 2
  • 31
  • 45