setting locales

2008-07-07 Thread Roman Rakus
In file locale.c, function get_locale_var, locale = 
default_locale;/* system-dependent; not really portable.  should it 
be "C"? */
default_locale contains string returned by calling 
setlocale(LC_ALL,NULL); and we then use this string to assign locale for 
LC_'every_other' and this is not right way. Why we simple don't call 
setlocale(LC_ALL,default_locale)? The final code will be shorter and 
more readable...
begin:vcard
fn:Roman Rakus
n:Rakus;Roman
org:Red Hat;BaseOS
adr:;;;Brno;;;Czech Republic
email;internet:[EMAIL PROTECTED]
title:Associate software engineer
tel;cell:+420 774 891 861
x-mozilla-html:FALSE
version:2.1
end:vcard



Re: setting locales

2008-07-07 Thread Chet Ramey
> In file locale.c, function get_locale_var, locale = 
> default_locale;/* system-dependent; not really portable.  should it 
> be "C"? */
> default_locale contains string returned by calling 
> setlocale(LC_ALL,NULL); and we then use this string to assign locale for 
> LC_'every_other' and this is not right way. Why we simple don't call 
> setlocale(LC_ALL,default_locale)? The final code will be shorter and 
> more readable...

Bash does most of the locale-setting itself for two reasons.

1.  setlocale() calls getenv() to obtain the values for the variables it's
interested in.  Not all systems allow getenv() to be replaced or
interposed, so setlocale() would see stale data.

2.  Users expect shell variables to affect the locale for the shell.  Since
setlocale() looks in the environment, these changes would not be
applied.

In any event, calling setlocale(LC_ALL, default_locale) is rarely the right
thing to do, since it doesn't allow users to mix locales between categories
(e.g., LC_COLLATE=C; LC_CTYPE=C; LC_MESSAGES=de_DE.UTF-8).

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://tiswww.tis.case.edu/~chet/




Eval ${#name[subscript]} incorrect when having multibyte characters.

2008-07-07 Thread Wang Xin
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -DCONF_OSTYPE='linu
x-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/
local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./incl
ude -I./lib   -g -O2
uname output: Linux shan 2.6.24.5-smp #2 SMP Wed Apr 30 13:41:38 CDT 2008 i686 I
ntel(R) Celeron(R) M processor 1.40GHz GenuineIntel GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 3.2
Patch Level: 0
Release Status: release

Description:
When there are multibyte characters in an element of array, the
result of ${#name[subscript]} will be incorrect. It will be evaled
to numbers of bytes, but not numbers of characters.

Repeat-By:
This can be reproduced by:
1. a[0]=你好
2. echo ${#a[0]}

There are only two chinese characters, but the result is 6.

Fix:
Following patch may be helpful.

--- subst.c2008-07-06 15:47:14.0 +0800
+++ bash-3.2/subst.c2008-07-06 15:47:39.0 +0800
@@ -4763,7 +4763,7 @@
   else
 t = (ind == 0) ? value_cell (var) : (char *)NULL;

-  len = STRLEN (t);
+  len = MB_STRLEN (t);
   return (len);
 }
 #endif /* ARRAY_VARS */


Bash/readline enhancement: wish to pre-set initial value of input text

2008-07-07 Thread Richard Neill
Dear All,

When using read, it would be really neat to be able to pre-fill the form
with a default (or previous) value.

For example, a script which wants you to enter your name, and thinks
that my name is Richard, but that I might want to correct it.
Alternatively, this would be useful within a loop, to allow correction
of previously-entered text, without fully typing it again.

So, I propose an extra option, -i, to read, which will set the initial
value of the text following the prompt.

For example,


#!/bin/bash
read -e -p 'Enter your name: ' -i 'Richard' NAME
echo "Hello, $NAME"


This would print:
   Enter your name: Richard
I would then be able to edit the part after the prompt, and change it to:
   Enter your name: R. Neill
This would then print:
   Hello, R. Neill



It is equivalent to the following in PHP/HTML:

Enter your name: 


An alternative syntax might be to make use of stdin for the read
command, eg:
  echo 'Richard' | read -e -p 'Enter your name: ' NAME
though I think I prefer the -i.


I hope you like this idea. Thanks very much for your help.

Richard





Bash substrings: wish for support for negative length (read till n from end)

2008-07-07 Thread Richard Neill
Dear All,

Substrings in bash contain 2 parameters, the start and the length.
Start may be negative, but if length is negative, it throws an error.
My request is that bash should understand negative length. This would be
useful in some occasions, and would be similar to the way PHP does it:
http://uk.php.net/manual/en/function.substr.php

For clarity, here are all the cases; the relevant ones are the last two:


$ stringZ=abcdef


$ echo ${stringZ:2} #Positive start, no length
cdef#Reads from start.


$ echo ${stringZ: -2}   #Negative start, no length
ef  #Reads 2 back from end.


$ echo ${stringZ:-2}#No space before the -
abcdef  #Is this what we expect?
#(or an unrelated bug?)


$ echo ${stringZ:2:1}   #Starts at 2, reads 1 char.
c


$ echo ${stringZ:2: -1} #Wish: start at 2, read till
ERROR   #1 before the end. i.e.
# cde

$ echo ${stringZ: -3: -1}   #Wish: start 3 back, read till
ERROR   #1 before the end. i.e.
# de



i.e. ${string:x:y}
   * returns the string, from start position x for y characters.
   *  but, if x is negative, start from the right hand side
   *  if y is negative, print up to (the end - y)



Thanks very much,

Richard




Re: Bash/readline enhancement: wish to pre-set initial value of input text

2008-07-07 Thread Jan Schampera
Richard Neill wrote:
> Dear All,
> 
> When using read, it would be really neat to be able to pre-fill the form
> with a default (or previous) value.
> 
> For example, a script which wants you to enter your name, and thinks
> that my name is Richard, but that I might want to correct it.
> Alternatively, this would be useful within a loop, to allow correction
> of previously-entered text, without fully typing it again.

A bit of the functionality (in some way) is already there. You can
preload the commandline history and use read -e:

--snipsnap--
If -e is supplied and the shell is interactive, readline is used to
obtain the line.
--snipsnap--

A bit of hard work, though.

J.




Re: Bash substrings: wish for support for negative length (read till n from end)

2008-07-07 Thread Jan Schampera
Richard Neill wrote:

> $ echo ${stringZ:2: -1}   #Wish: start at 2, read till
> ERROR #1 before the end. i.e.
>   # cde
> 
> $ echo ${stringZ: -3: -1} #Wish: start 3 back, read till
> ERROR #1 before the end. i.e.

Use (-1), i.e.

$ echo ${stringZ:2:(-1)}

See also
http://bash-hackers.org/wiki/doku.php/syntax/pe#substring_expansion (at
bottom of the section).

J.




Re: Bash substrings: wish for support for negative length (read till n from end)

2008-07-07 Thread Jan Schampera
Jan Schampera wrote:
> Richard Neill wrote:
> 
>> $ echo ${stringZ:2: -1}  #Wish: start at 2, read till
>> ERROR#1 before the end. i.e.
>>  # cde
>>
>> $ echo ${stringZ: -3: -1}#Wish: start 3 back, read till
>> ERROR#1 before the end. i.e.
> 
> Use (-1), i.e.
> 
> $ echo ${stringZ:2:(-1)}
> 
> See also
> http://bash-hackers.org/wiki/doku.php/syntax/pe#substring_expansion (at
> bottom of the section).
> 
> J.

My bad - it's too early in the morning - forget about that :)

I didn't read carefully.

J.




Re: Bash substrings: wish for support for negative length (read till n from end)

2008-07-07 Thread Richard Neill
Jan Schampera wrote:
> Richard Neill wrote:
> 
>> $ echo ${stringZ:2: -1}  #Wish: start at 2, read till
>> ERROR#1 before the end. i.e.
>>  # cde
>>
>> $ echo ${stringZ: -3: -1}#Wish: start 3 back, read till
>> ERROR#1 before the end. i.e.
> 
> Use (-1), i.e.
> 
> $ echo ${stringZ:2:(-1)}
> 
> See also
> http://bash-hackers.org/wiki/doku.php/syntax/pe#substring_expansion (at
> bottom of the section).
> 

Dear Jan,

Thanks for your comment. I now understand why
echo ${stringZ:-1}   is invalid, and it has to be
echo ${stringZ:(-1)}   or   echo ${stringZ: -1}

BUT, the example that you gave doesn't actually work; it returns with
an error:
 -bash: (-1): substring expression < 0

My point is that, though the first parameter (offset) may be negative,
the second parameter (length) can only be positive. This is what would
be useful to change.

Thanks,

Richard






Re: Bash/readline enhancement: wish to pre-set initial value of input text

2008-07-07 Thread Richard Neill
Jan Schampera wrote:
> Richard Neill wrote:
>> Dear All,
>>
>> When using read, it would be really neat to be able to pre-fill the form
>> with a default (or previous) value.
>>
>> For example, a script which wants you to enter your name, and thinks
>> that my name is Richard, but that I might want to correct it.
>> Alternatively, this would be useful within a loop, to allow correction
>> of previously-entered text, without fully typing it again.
> 
> A bit of the functionality (in some way) is already there. You can
> preload the commandline history and use read -e:
> 
> --snipsnap--
> If -e is supplied and the shell is interactive, readline is used to
> obtain the line.
> --snipsnap--
> 
> A bit of hard work, though.
> 

I do have a sort of workaround, namely to put the default value on the
clipboard, by using:


function copy_to_clipboard () { 
#If we're running KDE, put the text into klipper.
#Else, use xclip.  Fail silently if we can't do it.
{ dcop klipper klipper setClipboardContents "$1" ||  echo "$1" |
 xclip ; } > /dev/null 2>&1
}

copy_to_clipboard INITIAL_VALUE
read -e -p "Enter name" NAME
#User must middle-click

but this is rather ugly, as well as only semi-functional. What I want to
do is just to pre-fill readline's buffer, before it gets presented to
the user.

I also tried to hack something nasty out of xmacroplay, but that doesn't
work.


Richard