On Wed, 2003-02-26 at 13:58, Andreas Schwab wrote:
> "Robert Anderson" <[EMAIL PROTECTED]> writes:
> 
> |> 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"| \
> 
> Look out for echo meta character.
> 
> |>    sed -e 's,\\,\\\\,g' | \
> |>    sed -e 's,&,\\&,g' | \
> |>    sed -e 's,\,,\\\,,g'
> |> }
> 
>         sed -e 's/[\&,]/\\&/g'
> 
> is much faster and works as well.

Yep.  Both problems ostensibly fixed below.  Can someone get this to
fail using their favorite crappy sh implementation?

#!/bin/ash

_quote_for_sed()
{
    cat<<_ACEOF | sed -e 's,[\&,],\\&,g'
$1
_ACEOF
}

_test()
{
    if [ "$substvar" = "$var" ]; then
        cat<<EOF
pass: $substvar == $var
EOF
    else
        cat<<EOF
FAIL: $substvar == $var
EOF
    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='(&)[].*\~&<>/[EMAIL PROTECTED]|;:"'
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


Thanks,
Bob




Reply via email to