Re: misleading error message from variable modifier

2018-03-17 Thread don fong
will the coverage target be in an upcoming release?

On Sat, Mar 17, 2018 at 7:13 PM, Chet Ramey  wrote:

> On 3/17/18 6:39 PM, don fong wrote:
> > Chet, thanks for the tip about where to find the tests for subst.c .  i
> > still think that my tests cover some cases that aren't covered by
> > posixexp.tests .
>
> There are other test cases.
>
> > it's cool that you increased the coverage of subst.c .  how did you
> produce
> > the report?  i didn't see a script or makefile target to do it.
>
> I wrote a Makefile target to build a gcov-enabled binary and ran the
> test suite with it.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
>


Re: question on retrieving map(-A) value w/indirect name

2018-03-17 Thread L A Walsh



David Margerison wrote:

processSrvState() {
  local cur_up="$1[cur_up]"
  local max_up="$1[max_up]"
  if [[ "${!cur_up}" == "${!max_up}" ]] ; then
echo ok
  fi
}

declare -A foo=([cur_up]=11 [max_up]=11)

processSrvState foo

# note that the array name must not conflict with any keys
  

Thanks for the idea. I put it into a general sub for my use
(no error checking in this version):

hval() { # 1st=mapname, 2nd=key, 3rd=opt. outvar name
 dcl _hkey="$1[$2]" nl=""
 [[ ! $3 ]] && nl="\n"
 printf ${3:+-v $3} "%s$nl" ${!_hkey}
}


so for:


 dcl A foo=([one]=1 [two]=2)
 hval foo two

2

 hval foo two outv
 echo $outv

2

Thanks much!





Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Clark Wang
On Sun, Mar 18, 2018 at 1:18 AM, Chet Ramey  wrote:

> On 3/17/18 12:16 AM, Clark Wang wrote:
> > On Sat, Mar 17, 2018 at 1:00 AM,  wrote:
> >
> >>
> >> It works correctly if the single quote is itself quoted. Our test
> wordlist
> >> would then be: "foo\'bar aaa bbb"
> >>
> >> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- a
> >> aaa
> >> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- b
> >> bbb
> >> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- f
> >> foo'bar
> >>
> >
> > This seems to work but it does not. For example in the command line
> >
> >   # some-cmd f
> >
> > will become
> >
> >   # some-cmd foo'bar
> >
> > then you press ENTER and it'll still wait for another ' char.
>
> You haven't told complete that you want the words to be quoted. If you
> install the completion using the `-o filenames' option, you will get
> filename-like quoting.
>

`-o filenames' would append `/' to a word if the word is coincidentally a
real existing dir name. How can I ask it not to append the `/'?

-clark

>
> Readline doesn't provide a way to quote completion matches unless you
> say they're filenames. Maybe it should, but that is the way things are.
>


Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Paulo Marcel Coelho Aragão
On Sat, Mar 17, 2018 at 2:06 PM, Chet Ramey  wrote:
> Processing the word argument to -W honors shell quoting, in order to
> provide a simple way to return words containing shell metacharacters and
> characters in $IFS. There is a sentence to this effect in the texinfo
> documentation, but that sentence is missing from the manual page.  I will
> add some clarifying text and make sure it appears in the man page.

I confess it took me quite a while to grasp what "honors shell quoting" meant
here, I think now I get it. The single quote in:

compgen -W "foo'bar aaa bbb"

opens up a single-quoted sequence (not sure what the formal, correct word
should be used here) which extends up to bbb, as if it were:

foo'bar aaa bbb'

Even though the sequence doesn't close with a single quote, it's
treated as such.
Then quote removal is applied, but there's nothing to be split on IFS, and
the final result of the splitting/expansion is the single word:

foobar aaa bbb

That's why completing "a" and "b" generates nothing, and "f" generates that
word.

The man page does mention it:

"The string is first split using the characters in the IFS special
variable as delimiters.
Shell quoting is honored. Each word is then expanded using brace expansion,
tilde expansion, parameter and variable expansion, command substitution, and
arithmetic expansion, as described above under EXPANSION.  The results are split
using the rules described above under Word Splitting"

Perhaps it would be useful to expand "Shell quoting is honored" a bit,
mentioning
quote removal. It bit me because I glanced over that sentence and
didn't analyze it more
attentively.

This discussion has been most illuminating, I must say ! Thanks to
Chet and Clark !

Paulo



Re: misleading error message from variable modifier

2018-03-17 Thread Chet Ramey
On 3/17/18 6:39 PM, don fong wrote:
> Chet, thanks for the tip about where to find the tests for subst.c .  i
> still think that my tests cover some cases that aren't covered by
> posixexp.tests .

There are other test cases.

> it's cool that you increased the coverage of subst.c .  how did you produce
> the report?  i didn't see a script or makefile target to do it.

I wrote a Makefile target to build a gcov-enabled binary and ran the
test suite with it.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: misleading error message from variable modifier

2018-03-17 Thread don fong
Chet, thanks for the tip about where to find the tests for subst.c .  i
still think that my tests cover some cases that aren't covered by
posixexp.tests .

it's cool that you increased the coverage of subst.c .  how did you produce
the report?  i didn't see a script or makefile target to do it.




On Wed, Mar 14, 2018 at 7:08 PM, Chet Ramey  wrote:

> On 3/9/18 3:14 AM, don fong wrote:
>
> >
> > my question was whether you have tests for the variable modifiers.
> > i don't see any.  that's the area of code i was touching, and that's why
> i
> > wrote a few tests of that area.
>
> Thank you for the inspiration. I ran the devel version of the test suite
> through gcov, added some tests, and was able to increase the coverage of
> subst.c (the word expansion code, plus) from 83% to 86%. It's tough to get
> it higher than that due to the functions that exist only for readline
> support and the debugging and system call error-handling code.
>
> Chet
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
>


Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Chet Ramey
On 3/17/18 12:16 AM, Clark Wang wrote:
> On Sat, Mar 17, 2018 at 1:00 AM,  wrote:
> 
>>
>> It works correctly if the single quote is itself quoted. Our test wordlist
>> would then be: "foo\'bar aaa bbb"
>>
>> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- a
>> aaa
>> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- b
>> bbb
>> paulo@monk:~/tmp$ compgen -W "foo\'bar aaa bbb" -- f
>> foo'bar
>>
> 
> This seems to work but it does not. For example in the command line
> 
>   # some-cmd f
> 
> will become
> 
>   # some-cmd foo'bar
> 
> then you press ENTER and it'll still wait for another ' char.

You haven't told complete that you want the words to be quoted. If you
install the completion using the `-o filenames' option, you will get
filename-like quoting.

Readline doesn't provide a way to quote completion matches unless you
say they're filenames. Maybe it should, but that is the way things are.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Chet Ramey
On 3/16/18 1:00 PM, marcelpa...@gmail.com wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
> 
> Description:
> compgen -W "wordlist" doesn't split wordlist on $IFS if wordlist contains
> unquoted single quotes. For instance, this wordlist, "foo'bar aaa bbb", won't
> be split using the default value of IFS: it will be treated as a single word.

Processing the word argument to -W honors shell quoting, in order to
provide a simple way to return words containing shell metacharacters and
characters in $IFS. There is a sentence to this effect in the texinfo
documentation, but that sentence is missing from the manual page.  I will
add some clarifying text and make sure it appears in the man page.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Clark Wang
On Sat, Mar 17, 2018 at 7:32 PM, Paulo Marcel Coelho Aragão <
marcelpa...@gmail.com> wrote:

> > Actually every word in the -W "wordlist" needs to be sh-quoted twice
> (with
> > ``printf %q'' or the new ``${var@Q}'' syntax). It'll be a bit easier if
> you
> > use an array.
>
> On a second thought, sh-quoting twice with ${var@Q} won't work as expected
> in this case, since it encloses the expanded value with single quotes, so
> that:
>
> paulo@monk:~/tmp$ arr=(foo\'bar aaa bbb)
> paulo@monk:~/tmp$ arr=(${arr[*]@Q})
> paulo@monk:~/tmp$ arr=(${arr[*]@Q})
> paulo@monk:~/tmp$ echo "${arr[*]}"
> ''\''foo'\''\'\'''\''bar'\''' ''\''aaa'\''' ''\''bbb'\'''
> paulo@monk:~/tmp$ compgen -W "${arr[*]}" -- f
> paulo@monk:~/tmp$
> paulo@monk:~/tmp$ compgen -W "${arr[*]}"
> 'foo'\''bar'
> 'aaa'
> 'bbb'
>
> To produce the desired result, it has to be your original solution, using
> 'printf %q'.
>

Did not know it would break this. I'm still using ``printf %q'' for my
auto-completion code.

Yes ${var@Q} is not compatible with ``printf %q''. The following is what
I've found:

  http://lists.gnu.org/archive/html/bug-bash/2017-10/msg00113.html
  http://lists.gnu.org/archive/html/bug-bash/2017-10/msg00111.html

-clark


Re: question on retrieving map(-A) value w/indirect name

2018-03-17 Thread David Margerison
On 17 March 2018 at 20:40, L A Walsh  wrote:
>
> I seebut that begs the question, how do you access an array's
> members using a var holding the array's name?
>
> I wanted to be able to do something like have a set of
> values in an assoc. map, and pass the name to a generic
> processing routine with the map name as a param, like:
>
> sub processSrvState() {
>my stat=${1:?}
>if [[ ${!stat[cur_up]} == ${!stat[max_up]} &&
>  ${!stat[cur_down]} == ${!stat[max_down]} ]]; then
>  ...
>fi

processSrvState() {
  local cur_up="$1[cur_up]"
  local max_up="$1[max_up]"
  if [[ "${!cur_up}" == "${!max_up}" ]] ; then
echo ok
  fi
}

declare -A foo=([cur_up]=11 [max_up]=11)

processSrvState foo

# note that the array name must not conflict with any keys



Re: question on retrieving map(-A) value w/indirect name

2018-03-17 Thread David Margerison
On 17 March 2018 at 11:50, L A Walsh  wrote:
>
> I'm a bit confused ...
> If I have assoc-array:
>
>  declare -A foo=([one]=11 [two]=22)
>
> and am passing name in another var, like "fee"
>
>  fee=foo
>
> I tried echoing the val:
>
>  echo ${!fee[one]}
>
> but got nothing -- tried a few other syntaxes.

I think this does what you want ...

$ declare -A foo=([one]=11 [two]=22)
$ fee=foo[one]
$ echo "${!fee}"
11



Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Paulo Marcel Coelho Aragão
> Actually every word in the -W "wordlist" needs to be sh-quoted twice (with
> ``printf %q'' or the new ``${var@Q}'' syntax). It'll be a bit easier if
you
> use an array.

On a second thought, sh-quoting twice with ${var@Q} won't work as expected
in this case, since it encloses the expanded value with single quotes, so
that:

paulo@monk:~/tmp$ arr=(foo\'bar aaa bbb)
paulo@monk:~/tmp$ arr=(${arr[*]@Q})
paulo@monk:~/tmp$ arr=(${arr[*]@Q})
paulo@monk:~/tmp$ echo "${arr[*]}"
''\''foo'\''\'\'''\''bar'\''' ''\''aaa'\''' ''\''bbb'\'''
paulo@monk:~/tmp$ compgen -W "${arr[*]}" -- f
paulo@monk:~/tmp$
paulo@monk:~/tmp$ compgen -W "${arr[*]}"
'foo'\''bar'
'aaa'
'bbb'

To produce the desired result, it has to be your original solution, using
'printf %q'.

On Sat, Mar 17, 2018 at 7:51 AM, Paulo Marcel Coelho Aragão <
marcelpa...@gmail.com> wrote:

> On Sat, Mar 17, 2018 at 12:16:24PM +0800, Clark Wang wrote:
>
> > This seems to work but it does not. For example in the command line
> >
> >   # some-cmd f
> >
> > will become
> >
> >   # some-cmd foo'bar
> >
> > then you press ENTER and it'll still wait for another ' char.
>
> Oh, I hadn't realized that, you're right. My example was incomplete
> because when I realized that I had to sh-quote the words in wordlist, I
> used '-o filenames' in the comspec (I hit the original problem when I was
> completing filenames), which works:
>
> complete -o filenames -W "foo\'bar aaa bbb" some-cmd
>
> > Actually every word in the -W "wordlist" needs to be sh-quoted twice
> (with
> > ``printf %q'' or the new ``${var@Q}'' syntax). It'll be a bit easier if
> you
> > use an array.
>
> You're right: it does work ! Hey, thanks for pointing out the ${var@Q}
> syntax: I wasn't aware of it. Had a look at the man page, and realized I
> didn't know about the ${parameter@operator} syntax.
>
> This discussion was enlightening, thanks for that, Clark !
>
> Paulo
>


Re: compgen -W doesn't split wordlist containing single quotes

2018-03-17 Thread Paulo Marcel Coelho Aragão
On Sat, Mar 17, 2018 at 12:16:24PM +0800, Clark Wang wrote:

> This seems to work but it does not. For example in the command line
> 
>   # some-cmd f
> 
> will become
> 
>   # some-cmd foo'bar
> 
> then you press ENTER and it'll still wait for another ' char.

Oh, I hadn't realized that, you're right. My example was incomplete because 
when I realized that I had to sh-quote the words in wordlist, I used '-o 
filenames' in the comspec (I hit the original problem when I was completing 
filenames), which works:

complete -o filenames -W "foo\'bar aaa bbb" some-cmd

> Actually every word in the -W "wordlist" needs to be sh-quoted twice (with
> ``printf %q'' or the new ``${var@Q}'' syntax). It'll be a bit easier if you
> use an array.

You're right: it does work ! Hey, thanks for pointing out the ${var@Q} syntax: 
I wasn't aware of it. Had a look at the man page, and realized I didn't know 
about the ${parameter@operator} syntax.

This discussion was enlightening, thanks for that, Clark !

Paulo



Re: question on retrieving map(-A) value w/indirect name

2018-03-17 Thread L A Walsh



Chet Ramey wrote:

On 3/16/18 8:50 PM, L A Walsh wrote:
  

I'm a bit confused ...
If I have assoc-array:

 declare -A foo=([one]=11 [two]=22)

and am passing name in another var, like "fee"

 fee=foo

I tried echoing the val:

 echo ${!fee[one]}

but got nothing -- tried a few other syntaxes.



This has come up a number of times. Here's a message from 2002 explaining
the details:

https://lists.gnu.org/archive/html/bug-bash/2002-08/msg00119.html
  

I seebut that begs the question, how do you access an array's
members using a var holding the array's name?

I wanted to be able to do something like have a set of
values in an assoc. map, and pass the name to a generic
processing routine with the map name as a param, like:



sub processSrvState() {
   my stat=${1:?}
   if [[ ${!stat[cur_up]} == ${!stat[max_up]} &&
 ${!stat[cur_down]} == ${!stat[max_down]} ]]; then
 ...
   fi

but it seems less than straight-forward.  Is this possible?

I can likely think of ways around it, but none are very str8-forward
or attractive(sigh)...
Tnx & tnx for the pointer...