Re: Why does this kill my box?

2007-11-01 Thread Tomas Janousek
Hello,

On Thu, Nov 01, 2007 at 11:25:58AM +, Steve P wrote:
> $0 = "test";

expands to "../test.sh = test" and recurses (runs itself with parameters "="
and "test").

> tail -f /var/log/messages
> 
> chmod +x test.sh
> ../test.sh

-- 
Tomas Janousek, SW Engineer, Red Hat, Inc.




Wrong input confuses bash's arithmetic unit permanently

2007-11-02 Thread Tomas Janousek
Hello,

as reported in [286861], certain expressions may confuse bash so that it is no
longer able to evaluate assignments.

[286861] https://bugzilla.redhat.com/show_bug.cgi?id=286861

I tried to debug it and found that if evalerror (thus longjmp) is called while
noeval != 0, it stays nonzero and assignments cease to work. Such expressions
are for example:

let tmp="foo.a"+0   (only in bash 3.2)
let x=(0?(3?4):3)

I think we should reset noeval to zero in the evalexp function (or restore
expr_stack[0], probably).

Regards,
-- 
Tomas Janousek, SW Engineer, Red Hat, Inc.




$RANDOM incorrectly seeded in subshells

2007-11-09 Thread Tomas Janousek
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/local/share/locale' -DPACKAGE='bash' 
-DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib   -g -O2
uname output: Linux tjanouse.englab.brq.redhat.com 2.6.18-8.1.8.el5 #1 SMP Mon 
Jun 25 17:06:19 EDT 2007 i686 i686 i386 GNU/Linux
Machine Type: i686-pc-linux-gnu

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

Description:
If you cause rng to be seeded in a subshell (enclose the command in
parentheses) and in this same subshell spawn another subshell, rng is
not seeded as it should be. See the example in Repeat-by.

Repeat-By:
$ ( echo $RANDOM; ( echo $RANDOM ); ( echo $RANDOM ) )
28804
3664
3664

Fix:
We had a fix in the Fedora package that used subshell_level instead of
just looking at subshell_environment, but that introduced another
oddity, since subshell_level is not increased for simple async
commands.
(bugzilla for this one is
https://bugzilla.redhat.com/show_bug.cgi?id=344411 )

I'm attaching a patch that was applied over that one, so it just shows
    what the final solution is like, it's not applicable to 3.2 tarball.

-- 
Tomas Janousek, SW Engineer, Red Hat, Inc.
344411: $RANDOM stays the same when job executed in the background

In bash 3.0, random was seeded whenever subshell_environment != 0.

In bash 3.2, random was seeded whenever subshell_environment != 0 &&
seeded_subshell == 0. And when it was seeded, seeded_subshell was set to 1.

Therefore, in 3.2, if you seeded random in a subshell and in this subshell
invoked another one, it wasn't reseeded as it should have been. A testcase for
that is this:
( echo $RANDOM; ( echo $RANDOM ); ( echo $RANDOM ) )

Tomas's patch (bash-3.2-rng.patch) changed the code to use subshell_level.
subshell_level is not increased for simple async commands, however. So,
although he fixed the previous case, he introduced another. Here's a testcase:
echo $RANDOM; echo $RANDOM & echo $RANDOM &

I decided to just compare the pids, that should be safe enough.

Written-by: Tomas Janousek <[EMAIL PROTECTED]>
Reviewed-by: Tomas Mraz <[EMAIL PROTECTED]>

--- bash-3.2/variables.c.344411 2007-11-06 19:26:42.0 +0100
+++ bash-3.2/variables.c2007-11-06 20:27:25.0 +0100
@@ -1211,7 +1211,7 @@
  arrayind_t unused;
 {
   sbrand ((unsigned int)strtoul (value, (char **)NULL, 10));
-  seeded_subshell = subshell_level;
+  seeded_subshell = getpid();
   return (self);
 }
 
@@ -1221,10 +1221,10 @@
   int rv;
 
   /* Reset for command and process substitution. */
-  if (seeded_subshell < subshell_level)
+  if (seeded_subshell != getpid())
 {
   seed_random ();
-  seeded_subshell = subshell_level;
+  seeded_subshell = getpid();
 }
 
   do


vi mode is unable to repeat insert actions after deletion etc.

2008-01-15 Thread Tomas Janousek
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-redhat-linux-gnu' 
-DCONF_VENDOR='redhat' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib  -D_GNU_SOURCE 
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -pipe -Wall 
-Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector 
--param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic 
-fasynchronous-unwind-tables
uname output: Linux tjanouse.englab.brq.redhat.com 2.6.18-53.el5 #1 SMP Wed Oct 
10 16:34:02 EDT 2007 i686 i686 i386 GNU/Linux
Machine Type: i686-redhat-linux-gnu

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

Description:
Whenever you do some non-inserting edit command, like 'x' or 'd', that
becomes the command-to-repeat and no further insertions may be
repeated.

Repeat-By:
1. Start a new bash shell
2. 'set -o vi'
2. Type 'kekepophxi\.'
3. You end up with 'kekepp' instead of 'kekep\\p'.

Fix:

This patch fixes it and tries to fix redoing the 'I' command as well.
I'm not sure about 'c', though.

diff --git a/lib/readline/misc.c b/lib/readline/misc.c
index e9c72c5..35d6348 100644
--- a/lib/readline/misc.c
+++ b/lib/readline/misc.c
@@ -560,7 +560,7 @@ rl_vi_editing_mode (count, key)
 #if defined (VI_MODE)
   _rl_set_insert_mode (RL_IM_INSERT, 1);   /* vi mode ignores insert mode 
*/
   rl_editing_mode = vi_mode;
-  rl_vi_insertion_mode (1, key);
+  rl_vi_insert_mode (1, key);
 #endif /* VI_MODE */
 
   return 0;
diff --git a/lib/readline/readline.c b/lib/readline/readline.c
index bd4d263..4b3d91b 100644
--- a/lib/readline/readline.c
+++ b/lib/readline/readline.c
@@ -370,7 +370,7 @@ readline_internal_setup ()
 
 #if defined (VI_MODE)
   if (rl_editing_mode == vi_mode)
-rl_vi_insertion_mode (1, 'i');
+rl_vi_insert_mode (1, 'i');
 #endif /* VI_MODE */
 
   if (rl_pre_input_hook)
diff --git a/lib/readline/readline.h b/lib/readline/readline.h
index b71bf98..8527ebf 100644
--- a/lib/readline/readline.h
+++ b/lib/readline/readline.h
@@ -230,6 +230,7 @@ extern int rl_vi_next_word PARAMS((int, int));
 extern int rl_vi_end_word PARAMS((int, int));
 extern int rl_vi_insert_beg PARAMS((int, int));
 extern int rl_vi_append_mode PARAMS((int, int));
+extern int rl_vi_insert_mode PARAMS((int, int));
 extern int rl_vi_append_eol PARAMS((int, int));
 extern int rl_vi_eof_maybe PARAMS((int, int));
 extern int rl_vi_insertion_mode PARAMS((int, int));
diff --git a/lib/readline/vi_keymap.c b/lib/readline/vi_keymap.c
index 4b48c75..3a017cc 100644
--- a/lib/readline/vi_keymap.c
+++ b/lib/readline/vi_keymap.c
@@ -151,7 +151,7 @@ KEYMAP_ENTRY_ARRAY vi_movement_keymap = {
   { ISFUNC, rl_vi_char_search },   /* f */
   { ISFUNC, (rl_command_func_t *)0x0 },/* g */
   { ISFUNC, rl_backward_char },/* h */
-  { ISFUNC, rl_vi_insertion_mode },/* i */
+  { ISFUNC, rl_vi_insert_mode },   /* i */
   { ISFUNC, rl_get_next_history }, /* j */
   { ISFUNC, rl_get_previous_history }, /* k */
   { ISFUNC, rl_forward_char }, /* l */
diff --git a/lib/readline/vi_mode.c b/lib/readline/vi_mode.c
index b0da0ab..e859062 100644
--- a/lib/readline/vi_mode.c
+++ b/lib/readline/vi_mode.c
@@ -220,6 +220,15 @@ rl_vi_redo (count, c)
   if (rl_point > 0)
_rl_vi_backup ();
 }
+  /* Ditto for redoing an insert with `I', but move to the beginning of line
+ like the `I' command does. */
+  else if (_rl_vi_last_command == 'I' && vi_insert_buffer && *vi_insert_buffer)
+{
+  rl_beg_of_line (1, 'I');
+  _rl_vi_stuff_insert (count);
+  if (rl_point > 0)
+   _rl_vi_backup ();
+}
   else
 r = _rl_dispatch (_rl_vi_last_command, _rl_keymap);
   vi_redoing = 0;
@@ -584,7 +593,7 @@ rl_vi_insert_beg (count, key)
  int count, key;
 {
   rl_beg_of_line (1, key);
-  rl_vi_insertion_mode (1, key);
+  rl_vi_insert_mode (1, key);
   return (0);
 }
 
@@ -618,6 +627,14 @@ rl_vi_append_mode (count, key)
 }
 
 int
+rl_vi_insert_mode (count, key)
+ int count, key;
+{
+  rl_vi_start_inserting (key, 1, rl_arg_sign);
+  return (0);
+}
+
+int
 rl_vi_append_eol (count, key)
  int count, key;
 {
@@ -690,7 +707,7 @@ _rl_vi_done_inserting ()
 }
   else
 {
-  if ((_rl_vi_last_key_before_insert == 'i' || 
_rl_vi_last_key_before_insert == 'a') && rl_undo_list)
+  if ((_rl_vi_last_key_before_insert == 'i' || 
_rl_vi_last_key_before_insert == 'a' || _rl_vi_last_key_before_insert == 'I') 
&& rl_undo_list)
 _rl_vi_save_insert (rl_undo_list);
   /* XXX - Other keys probably need to be checked. */
   else if (_rl_vi_last_key_before_insert == 'C')

-- 
Tomas Janousek, SW Engineer, Red Hat, Inc.




A few man page fixes from RHEL-5

2008-02-04 Thread Tomas Janousek
Hello,

these three patches have been accepted into RHEL-5.2 and I think they may be
worth including in bash upstream.

More info at ,
 and
.

Regards,
-- 
TJ. (Brno, CZ), BaseOS, Red Hat
--- bash-3.1/doc/bash.1.253673  2007-08-14 19:03:29.0 +0900
+++ bash-3.1/doc/bash.1 2007-08-14 19:03:45.0 +0900
@@ -5978,6 +5978,11 @@
 is supplied, the name and value of the alias is printed.
 \fBAlias\fP returns true unless a \fIname\fP is given for which
 no alias has been defined.
+.sp 1
+Note aliases are not expanded by default in non-interactive shell, and it can 
be enabled by setting the
+.B expand_aliases
+shell option using
+.BR shopt .
 .TP
 \fBbg\fP [\fIjobspec\fP ...]
 Resume each suspended job \fIjobspec\fP in the background, as if it
--- bash-3.2/builtins/ulimit.def.ulimit-m   2006-03-23 20:51:51.0 
+
+++ bash-3.2/builtins/ulimit.def2007-07-04 16:42:12.0 +0100
@@ -38,7 +38,7 @@
 -f the maximum size of files written by the shell and its children
 -i the maximum number of pending signals
 -l the maximum size a process may lock into memory
--m the maximum resident set size
+-m the maximum resident set size (has no effect on Linux)
 -n the maximum number of open file descriptors
 -p the pipe buffer size
 -q the maximum number of bytes in POSIX message queues
--- bash-3.2/doc/bash.1.ulimit-m2007-07-04 16:41:59.0 +0100
+++ bash-3.2/doc/bash.1 2007-07-04 16:42:12.0 +0100
@@ -8545,7 +8545,7 @@
 The maximum size that may be locked into memory
 .TP
 .B \-m
-The maximum resident set size
+The maximum resident set size (has no effect on Linux)
 .TP
 .B \-n
 The maximum number of open file descriptors (most systems do not
--- bash-3.2/doc/bash.1.245641  2008-01-15 16:17:17.0 +0100
+++ bash-3.2/doc/bash.1 2008-01-17 13:07:36.0 +0100
@@ -5916,6 +5916,13 @@
 to signify the end of the options.
 For example, the \fB:\fP, \fBtrue\fP, \fBfalse\fP, and \fBtest\fP builtins
 do not accept options.
+Also, please note that while executing in non-interactive mode and while in
+.I posix
+mode, any special builtin (like \fB.\fP, \fB:\fP, \fBbreak\fP,
+\fBcontinue\fP, \fBeval\fP, \fBexec\fP, \fBexit\fP, \fBexport\fP,
+\fBreadonly\fP, \fBreturn\fP, \fBset\fP, \fBshift\fP, \fBsource\fP,
+\fBtimes\fP, \fBtrap\fP, \fBunset\fP) exiting with a non-zero status
+causes the shell to stop execution.
 .sp .5
 .PD 0
 .TP


Re: SEGV on unbounded recursion

2008-03-09 Thread Tomas Janousek
Hello,

On Sat, Mar 08, 2008 at 04:35:02PM -0500, Chet Ramey wrote:
> I'm not inclined to change the current behavior.  Bash is perfectly
> happy to allow people to shoot themselves in the foot.  We all agree
> that fixed-limit recursion is not the way to go, and I don't think the
> effort involved in handling tail recursion is well spent.

Okay, I'm ok with this, thanks for your opinion.

-- 
Tomas Janousek, SW Engineer, Red Hat, Inc.




Re: propagating environment variable via internal command

2018-06-20 Thread Tomas Janousek
Hi Chet and Tomáš,

On Wed, Jun 20, 2018 at 10:42:07AM -0400, Chet Ramey wrote:
> On 6/20/18 9:25 AM, Tomáš Čech wrote:
> >  $ /bin/sh
> >  sh-4.4$ VARIABLE=value set -o noglob
> >  sh-4.4$ env | grep VARIABLE
> >  VARIABLE=value
> >  sh-4.4$
> 
> Posix requires this behavior, which dates back to the Bourne shell, for
> assignment statements that precede special builtins:
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14
> 
> It used to require the same behavior for assignment statements preceding
> shell function calls, but that was removed in the most recent update.

Thanks for the quick reply!

I followed the link in item 2 and there is:

  If the command name is a special built-in utility, variable assignments
  shall affect the current execution environment. Unless the set -a option is
  on (see set), it is unspecified:

Whether or not the variables gain the export attribute during the
execution of the special built-in utility

Whether or not export attributes gained as a result of the variable
assignments persist after the completion of the special built-in utility

It seems that dash implements this without the export, which means this
"surprising" example we came up with behaves "correctly", but there's still
this one, possibly more subtle:

  $ {da,}sh -c 'VAR=val set -o noglob; echo $VAR'
  val

So even though this does look a bit like behaviour defined by historic
implementations, I guess the conclusion is that we should just read up on
POSIX shells. :-)

Thank you Chet.

-- 
Tomáš Janoušek, DEV/SETI/CEP @ GoodData, +420 608 876 277



History not saved when closing terminal while bash is sourcing a script

2019-08-03 Thread Tomas Janousek
Hello,

today I was wondering why I'm sometimes losing history entries and found out
that bash (5.0.3(1)-release, on Debian testing) won't save the history file
on closing the terminal window if it's currently sourcing something. I'm
attaching a script that uses tmux to simulate the scenario. On my system, only
"test 1" is printed, not "test 2" as expected. I think this is a bug.

A more realistic example (what actually caused my losing of history entries)
is Midnight Commander: many distros install the following file into
/etc/profile.d: 
https://github.com/MidnightCommander/mc/blob/master/contrib/mc.sh.in
which sources 
https://github.com/MidnightCommander/mc/blob/master/contrib/mc-wrapper.sh.in
so that when one exits mc, the outer shell chdirs to the last directory
browsed in mc. But this means one loses all history entries from the session
before invoking mc.

(Workaround for mc: invoke history -a before sourcing mc-wrapper. Or disable
this wrapper entirely.)

Regards,
-- 
Tomáš Janoušek, a.k.a. Pivník, a.k.a. Liskni_si, http://work.lisk.in/


test2.sh
Description: Bourne shell script


Re: History not saved when closing terminal while bash is sourcing a script

2019-08-08 Thread Tomas Janousek
Hi,

On Sun, Aug 04, 2019 at 01:42:12PM -0400, Chet Ramey wrote:
> On 8/3/19 11:05 AM, Tomas Janousek wrote:
> I don't have or use tmux, but I'm going to guess that `kill-window' sends
> a SIGHUP to the process group and follows it up with a SIGKILL. The
> `source' turns off saving to history -- the `source' gets saved in the
> history list, not the commands it runs -- and is still running when the
> SIGHUP arrives. The SIGHUP causes the shell to exit without saving the
> history because history is turned off when the signal handler runs.

So this means `source' shouldn't ever be used in interactive shells if one
cares about their shell history, right? I'll file an issue in the Midnight
Commander bug tracker to use a shell function instead. It might be worth
documenting the behaviour, though, as it is quite suprising -- shell functions
don't need to turn off saving to history, so why should `source'?

-- 
Tomáš Janoušek, a.k.a. Pivník, a.k.a. Liskni_si, http://work.lisk.in/



Re: Bash-5.1-rc1 available

2020-11-02 Thread Tomas Janousek
Hi Chet,

On Mon, Oct 26, 2020 at 10:12:59AM -0400, Chet Ramey wrote:
> Yes, you can disable bracketed paste mode. I'll make sure that turning off
> bracketed paste mode disables the active region for incremental searches.

Would you please consider making this configurable separately? I'd love to
keep bracketed paste mode enabled, but I find the highlighting annoying (and a
bit buggy, more on that later). It seems I'm not alone:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=972344

Also, the changelog says "when enabled", but there's currently no way to
disable it:

> f. New active mark and face feature: when enabled, it will highlight the text
>inserted by a bracketed paste (the `active region') and the text found by
>incremental and non-incremental history searches.

Now for the "bit buggy" part:

1. PS1='$ '
2. echo -n x
3. paste something
4. press left arrow

Now the terminal shows "x$somethingg" instead of "x$ something".

(Yeah, I can work around this by having PROMPT_COMMAND detect that current
column is not 0 and output an additional newline, but I'd really prefer to
just disable the highlighting as I don't like it.)

Thanks for considering it!

Regards,
-- 
Tomáš Janoušek, a.k.a. Pivník, a.k.a. Liskni_si, https://work.lisk.in/



lastpipe doesn't lastpipe when stdin (fd 0) is closed

2020-12-16 Thread Tomas Janousek

Hi,

bash-5.1 (and judging from git blame, all versions that ever supported 
lastpipe, so 4.2+) fails to honor lastpipe when run without fd 0:


$ bash -O lastpipe -c 'echo x | read x; echo x=$x'
x=x

$ bash -O lastpipe -c 'echo x | read x; echo x=$x' <&-
x=

$ bash -O lastpipe -c 'exec The issue seems to be that execute_cmd.c:execute_pipeline insists on 
saving/restoring stdin which obviously fails when there's no stdin open 
at all:


lstdin = move_to_high_fd (0, 1, -1);
if (lstdin > 0)
/* lastpipe in effect here */

(I got bitten by this because acpid runs action scripts with closed 
stdin, so lastpipe doesn't work in those while the same script invoked 
manually works. I wish bash gave me a warning instead of silently 
falling back to non-lastpipe behaviour.)


--
Tomáš Janoušek, a.k.a. Pivník, a.k.a. Liskni_si, https://work.lisk.in/