[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
config.status - robust sed substitutions
From: |
Robert Anderson |
Subject: |
config.status - robust sed substitutions |
Date: |
Wed, 26 Feb 2003 14:51:56 -0500 |
I submit for your critique an idea for making the generated sed
commands within config.status be robust to "special" characters
such as the delimiter - which happens to have been chosen as ","
- as well as sed metacharacters '&' and '\'.
This would be implemented as a small standalone script generated
by config.status in /tmp, but the following gives the idea concisely:
#!/bin/ash
_quote_for_sed()
{
echo "$1"| \
sed -e 's,\\,\\\\,g' | \
sed -e 's,&,\\&,g' | \
sed -e 's,\,,\\\,,g'
}
_test()
{
if [ "$substvar" = "$var" ]; then
echo "pass: $substvar == $var"
else
echo "FAIL: $substvar != $var"
fi
}
var='/tmp/file'
substvar=$( echo @var@ | sed -e "s,@var@,`_quote_for_sed $var`," )
_test
var='/tmp/,,file'
substvar=$( echo @var@ | sed -e "s,@var@,`_quote_for_sed $var`," )
_test
var='(&)[].*\~&<>/address@hidden|;:"'
substvar=$( echo @var@ | sed -e "s,@var@,`_quote_for_sed $var`," )
_test
var='\1abc\2\\20'
substvar=$( echo @var@ | sed -e "s,@var@,`_quote_for_sed $var`," )
_test
var="'"
substvar=$( echo @var@ | sed -e "s,@var@,`_quote_for_sed $var`," )
_test
If you play with this you'll see that _quote_for_sed is the
necessary and sufficient preprocessing needed to protect any
variable substitution of the form shown against potentially
problematic printable ASCII characters contained therein.
If this meets with approval, I'll submit the full-blown patch for
config.m4 (which won't be more than a few 10's of lines).
Bob