Concatenating/inserting strings with backslashes

Brian Candler B.Candler at pobox.com
Fri Nov 9 16:39:23 CET 2012


Here's something weird. I'm trying to concatenate some strings which contain
<backslash> <n> (i.e.  not a newline).

In a normal string literal, I have to enter four backslashes:

update reply {
    Reply-Message := "a\\\\nb"
}

("\\n" gives a newline, "\\\n" gives backslash followed by newline)

But when I try to insert one string into another it goes completely haywire.

update reply {
    Reply-Message := "foo\\\\nbar"
}
update reply {
    Reply-Message := "%{reply:Reply-Message}\\\\nbaz"
}

This gives me "foo" <newline> "bar" <newline> "baz". That is, even the
second \\\\n is being collapsed into a newline!

Some more test cases:

update reply {
    Reply-Message := "foo\\\\nbar"
}
update reply {
    Reply-Message := "qux\\\\nbaz"
}

correctly gives me "qux" <backslash> <n> "baz"

update reply {
    Reply-Message := "foo\\\\nbar"
}
update reply {
    Reply-Message := "%{Wibble:-qux}\\\\nbaz"
}

gives me <newline> "baz". In fact, I need *eight* backslashes to get a
literal backslash here:

    Reply-Message := "%{Wibble:-qux}\\\\\\\\nbaz"

So somehow, the presence of a string expansion within a string affects the
interpretation of subsequent backslashes within that string.

Now, this works:

update reply {
    Reply-Message := "foo\\\\\\\\nbar"
}
update reply {
    Reply-Message := "%{reply:Reply-Message}\\\\\\\\nbaz"
}

But then if I do another layer of string insertion they get translated to
newlines again.

update reply {
    Reply-Message := "foo\\\\\\\\nbar"
}
update reply {
    Reply-Message := "%{reply:Reply-Message}\\\\\\\\nbaz"
}
update reply { 
    Reply-Message := "%{reply:Reply-Message}" 
} 

This seems pretty broken to me, but if someone would care to explain how to
deal with it, please do.

Or is there another way I can concatenate strings, which doesn't involve
expanding them into another string?

Thanks,

Brian.


More information about the Freeradius-Users mailing list