bug-bash
[Top][All Lists]
Advanced

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

Custom word completion, word splitting, bad behavior


From: Nick Patavalis
Subject: Custom word completion, word splitting, bad behavior
Date: Mon, 5 Feb 2018 09:25:06 +0200

I witnessed the following in the word-splitting results passed to
custom completion functions. The word splitting performed goes haywire
when it sees the =' (equal, quote) or =" (equal double-quote)
character sequences. From this point on, practically no word-splitting
is performed. As these sequences are quite frequent (e.g foo
--bar="baz"), this forces the writer of custom completion functions to
do word-splitting himself, something doable, but certainly not trivial.

Take for example the following completion function written for
demonstration purposes:


    _foo()
    {
        local cur prev
        local DEBUG=/dev/pts/8

        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"

        echo > $DEBUG
        echo "------------" > $DEBUG
        echo COMP_LINE $COMP_LINE > $DEBUG
        echo COMP_CWORD $COMP_CWORD > $DEBUG
        for w in "${COMP_WORDS[@]}"; do
            echo -n "$w | "  > $DEBUG
        done
        echo > $DEBUG
        echo "$prev | $cur | " > $DEBUG
    }

    complete -F _foo foo

Once registered, then typing the following:

    foo --bar=aa bb --baz="cc" dd ee ff gg

abd pressing [TAB], results in the following debugging output:

    COMP_LINE foo --bar=aa bb --baz="cc" dd ee ff gg
    COMP_CWORD 7
    foo | --bar | = | aa | bb | --baz | =" | cc" dd ee ff gg |
    =" | cc" dd ee ff gg |

As you can see after seeing the =" sequence the algorithm goes haywire
and stops splitting words.

The current value of COMP_WORDBREAKS is the default one:

    $ echo $COMP_WORDBREAKS
    "'@><=;|&(:

If I remove the double-quotes (") from COMP_WORDBREAKS then things get
back to normal! For the same command line I get the following very
reasonable splitting:

    COMP_LINE foo --bar=aa bb --baz="cc" dd ee ff gg
    COMP_CWORD 11
    foo | --bar | = | aa | bb | --baz | = | "cc" | dd | ee | ff | gg |
    ff | gg |

Obviously, though, I cannot change COMP_WORDBREAKS because this may
interfere with other completion functions.

So, I guess, my questions are:

1. Why are ' and " in the default COMP_WORDBREAKS

2. Why can't I pass wordbreaks specific to my function when I register
it with complete

3. Why does readline gets so messed up when it sees a =" and " is in
COMP_WORDBREAKS?

4. I'm currently writing a function to do word-spitting on COMP_LINE
myself. Since I don't want custom completion to be practically
disabled when the user passes an option value like --bar="baz" (totaly
reasonable and common), this seems the only solution (again, I can't
remove '" from COMP_WORDBREAKS for the reason mentioned above). But
this seems like an overkill.

Tested with:

  Machine: x86_64
  OS: linux-gnu
  Compiler: gcc
  Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='l
  uname output: Linux coal 4.4.0-112-generic #135-Ubuntu SMP Fri Jan
19 11:48:36
  Machine Type: x86_64-unknown-linux-gnu

  Bash Version: 4.4
  Patch Level: 18
  Release Status: release

Thanks in advance.

/npat

Please cc any answers to by personal address, as I'm not subscribed to
the list.



reply via email to

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