help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Performing quote removal on data


From: Maarten Billemont
Subject: Re: [Help-bash] Performing quote removal on data
Date: Wed, 28 May 2014 12:11:07 -0400

On May 28, 2014, at 12:02 PM, Dennis Williamson <address@hidden> wrote:

> 
> On May 28, 2014 1:54 AM, "Maarten Billemont" <address@hidden> wrote:
> >
> > Suppose you have a variable whose value contains bash-escapes and quotes, 
> > and you need to perform the operations bash would perform on that string to 
> > turn it into a literal word.
> >
> > var=“foo\ bar/“
> >
> > How would you proceed to get the literal “foo bar/“ out of that, in a safe 
> > way (ie. perform just pathname expansion and quote removal, maybe even word 
> > splitting but that taking into account the quoted whitespace)?  
> > Specifically, I do NOT want any risk of performing command execution or any 
> > other expansions on the data (eg. “foo $(rm -rf ~)/“ should not run rm, 
> > “foo\ */“ should not expand */, etc.).  Essentially, I just want to turn a 
> > bash word into a literal.
> >
> > I need this, because I’m trying to write a safe programmable completion 
> > function and I need to be able to test the word that’s being completed (eg. 
> > foo\ ba) against the possible literals that can be completed (eg. foo bar/).
> >
> > It seems like everyone who’s writing a programmable completion function 
> > needs this facility yet it appears to be lacking in bash.  If I haven’t 
> > overlooked the solution, it appears a glaring issue which might be at the 
> > root of all bash completion functions being so buggy, and I should probably 
> > continue this topic on bug-bash.
> >
> >
> > — Maarten Billemont (lhunath) —
> > me: http://www.lhunath.com – business: http://www.lyndir.com
> > http://masterpasswordapp.com
> >
> 
> Can you post some code that's failing?
> 
# Perform pathname completion.
#
# 1. Populate COMPREPLY with pathnames.
# 2. Shell-escape the COMPREPLY words so they remain syntactical words when 
injected into the completed command.
# 3. Add a space after file names so successful completions advance to the next 
word.
#    Directory names are suffixed with a / instead so we can keep completing 
the files inside.
_gac_complete_path() {
    local partial=${COMP_WORDS[COMP_CWORD]}
    local path

    COMPREPLY=()
    for path in "$partial"*; do
        if [[ -d $path ]]; then
            COMPREPLY+=( "$(printf '%q/' "$path")" )

        elif [[ -e $path ]]; then
            COMPREPLY+=( "$(printf '%q ' "$path")" )

        fi
    done
}

complete -o nospace -F _gac_complete_path foo

##
Source this code and then do this:

mkdir “dir 1”; touch “dir 1/file”
foo dir[tab] ## completes into: foo dir\ 1/
foo dir\ [tab] ## fails to complete into: foo dir\ 1/
foo dir\ 1[tab] ## fails to complete into: foo dir\ 1/
foo dir\ 1/[tab] ## fails to complete into: foo dir\ 1/file



— Maarten Billemont (lhunath) —
me: http://www.lhunath.com – business: http://www.lyndir.comhttp://masterpasswordapp.com

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail


reply via email to

[Prev in Thread] Current Thread [Next in Thread]