Skip to content

Commit

Permalink
read -s: initialise history file in time for reading
Browse files Browse the repository at this point in the history
When emacs or vi mode is activated in a script, 'read -s' should
allow the user to arrow up to the current shell history file
($HISTFILE, default ~/.sh_history). But it looks like the history
file is not properly initialised for 'read -s' as it does not work
on the first invocation:

$ ksh -c 'set -o emacs; read -s foo'
(type up arrow; beep -- before 7bfcd1e, infinite loop)

$ ksh -c 'set -o emacs; read -s foo; read -s foo'
(type return, then up arrow; shell history appears)

src/cmd/ksh93/bltins/read.c: sh_readline():
- Move the code block that initialises the shell history file to
  immediately before where the first read operation is performed.

Discussion: #701 (comment)
  • Loading branch information
McDutchie committed Jan 15, 2024
1 parent 7bfcd1e commit 43634d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.
an infinite loop after using the up arrow in emacs mode with an empty
history file.

- Fixed a bug in 'read -s': in a script, the first invocation of that
command, with the emacs or vi shell option on, did not allow the user to
navigate through the shell history file.

2024-01-11:

- Fixed ancient incorrect behaviour in the 'kill' built-in: it sent SIGCONT
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/ksh93/bltins/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, long tim
if(timeout)
timeslot = sh_timeradd(timeout,0,timedout,iop);
}
#if !SHOPT_SCRIPTONLY
if((flags&S_FLAG) && !sh.hist_ptr)
{
sh_histinit();
if(!sh.hist_ptr)
flags &= ~S_FLAG;
}
#endif
if(flags&(N_FLAG|NN_FLAG))
{
char buf[256],*var=buf,*cur,*end,*up,*v;
Expand Down Expand Up @@ -516,14 +524,6 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, long tim
}
if(timeslot)
sh_timerdel(timeslot);
#if !SHOPT_SCRIPTONLY
if((flags&S_FLAG) && !sh.hist_ptr)
{
sh_histinit();
if(!sh.hist_ptr)
flags &= ~S_FLAG;
}
#endif
if(cp)
{
cpmax = cp + c;
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/ksh93/tests/pty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1267,5 +1267,16 @@ r ^:child-1: kill -s HUP \$\$\r\n$
r ^Hangup\r\n$
!

((SHOPT_VSH)) && HISTFILE=$tmp/tmp_histfile tst $LINENO <<"!"
L 'read -s' reads from history file on first go
d 40
p :test-1:
w "$SHELL" -o vi -c 'read -s "foo?:prompt: "'
p :prompt:
c \Ek
r ^:prompt: "\$SHELL" -o vi -c 'read -s "foo\?:prompt: "'$
!

# ======
exit $((Errors<125?Errors:125))

0 comments on commit 43634d8

Please sign in to comment.