Re: ksh "clear-screen" editing command

2019-04-02 Thread Sebastian Benoit
Jeremie Courreges-Anglas(j...@wxcvbn.org) on 2019.04.02 10:52:34 +0200:
> On Mon, Apr 01 2019, Jeremie Courreges-Anglas  wrote:
> > On Mon, Jun 18 2018, "Todd C. Miller"  wrote:
> >> On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:
> >>
> >>> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
> >>>
> >>> > If folks indeed think that this is a must-have feature, this is
> >>> > certainly a better approach.  I wonder though if the setupterm() call
> >>> > should happen earlier when interactive mode is initialized?
> >>>
> >>> This turns out to be simpler than expected.  Now whenever TERM is
> >>> set (including at startup) it will call setupterm().
> >>
> >> Hopefully final diff that better handles unknown term types.  Now
> >> if you set TERM=gobbledegook the old cur_term will be invalidated
> >> and clear-screen will not try to the escape sequence for the old
> >> terminal.
> >>
> >> I think this is good enough to commit.
> >
> > Since this went in, I'm using it on my machines instead of a bind -m hack.
> >
> > Can't we make ^L=clear-screen the default behavior?  I don't see
> > discussions about that.
> 
> So here's a diff.  oks/nays?

ok benno@

> 
> 
> NB: regress needs to be handled, as mentioned by Anton.  For now I think
> I'll comment out the ^L test, not sure how to amend it.
> 
> Index: emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.85
> diff -u -p -r1.85 emacs.c
> --- emacs.c   18 Jun 2018 17:03:58 -  1.85
> +++ emacs.c   2 Apr 2019 08:43:28 -
> @@ -1529,7 +1529,7 @@ x_init_emacs(void)
>   kb_add(x_prev_histword, CTRL('['), '_', 0);
>   /* how to handle: quote: ^^ */
>   kb_add(x_literal,   CTRL('^'), 0);
> - kb_add(x_draw_line, CTRL('L'), 0);
> + kb_add(x_clear_screen,  CTRL('L'), 0);
>   kb_add(x_search_char_back,  CTRL('['), CTRL(']'), 0);
>   kb_add(x_search_char_forw,  CTRL(']'), 0);
>   kb_add(x_search_hist,   CTRL('R'), 0);
> Index: ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.202
> diff -u -p -r1.202 ksh.1
> --- ksh.1 16 Dec 2018 13:08:35 -  1.202
> +++ ksh.1 2 Apr 2019 08:43:28 -
> @@ -4694,7 +4694,7 @@ Moves the cursor to the beginning of the
>  Uppercase the first character in the next
>  .Ar n
>  words, leaving the cursor past the end of the last word.
> -.It clear-screen:
> +.It clear-screen: ^L
>  Clears the screen if the
>  .Ev TERM
>  parameter is set and the terminal supports clearing the screen, then
> @@ -4891,7 +4891,7 @@ The last
>  word of the previous command is inserted at the cursor.
>  .It quote: ^^
>  The following character is taken literally rather than as an editing command.
> -.It redraw: ^L
> +.It redraw:
>  Reprints the prompt string and the current input line.
>  .It Xo search-character-backward:
>  .Op Ar n
> 
> 
> -- 
> jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
> 



Re: ksh "clear-screen" editing command

2019-04-02 Thread Klemens Nanni
On Tue, Apr 02, 2019 at 11:39:05AM -0400, Andras Farkas wrote:
> $ set -o vi
> $ true^[^L #redraws the line
> $ true
> 
> vi uses the escape or ^[ character to go into command mode from insert mode
Ooooh... I blatantly tried ^L without ESC in vi mode, of course that
won't work.

Yup, sorry for the noise.

OK kn (again)



Re: ksh "clear-screen" editing command

2019-04-02 Thread Theo Buehler
On Tue, Apr 02, 2019 at 05:31:22PM +0200, Klemens Nanni wrote:
> On Tue, Apr 02, 2019 at 05:20:19PM +0200, Theo Buehler wrote:
> > Yes, ^L is printed in vi insert mode. The text you quoted is about vi
> > command mode which does indeed redraw the current line on ^L. I agree
> > with jca, no need for a change there.
> I'm confused.  Without jca's diff, I did the following:
> 
>   xterm -e /bin/sh
>   $ set -o vi
>   $ ^L^L  # literal escape, nothing happens

You're staying in insert mode. As I said, paragraph is about command
(normal) mode:
The ⟨ESC⟩ key is used to enter command
 mode, where commands similar to those used by vi(1) are available.  A
 Ctrl-L sequence (^L) can be used in this mode to redraw the current
 command line.

$ set -o vi

enter hello^L^L and get the following output:

$ hello
$ hello
$ hello

With each ^L you will get another line containing "$ hello".



Re: ksh "clear-screen" editing command

2019-04-02 Thread Andras Farkas
On Tue, Apr 2, 2019 at 11:32 AM Klemens Nanni  wrote:
> On Tue, Apr 02, 2019 at 05:20:19PM +0200, Theo Buehler wrote:
> > Yes, ^L is printed in vi insert mode. The text you quoted is about vi
> > command mode which does indeed redraw the current line on ^L. I agree
> > with jca, no need for a change there.
> I'm confused.  Without jca's diff, I did the following:
>
> xterm -e /bin/sh
> $ set -o vi
> $ ^L^L  # literal escape, nothing happens
>
> $ set -o emacs
> $ true^L# redraws the line
> $ true
>
> So where does ^L redraw the current line on ^L for you?

$ set -o vi
$ true^[^L #redraws the line
$ true

vi uses the escape or ^[ character to go into command mode from insert mode



Re: ksh "clear-screen" editing command

2019-04-02 Thread Klemens Nanni
On Tue, Apr 02, 2019 at 05:20:19PM +0200, Theo Buehler wrote:
> Yes, ^L is printed in vi insert mode. The text you quoted is about vi
> command mode which does indeed redraw the current line on ^L. I agree
> with jca, no need for a change there.
I'm confused.  Without jca's diff, I did the following:

xterm -e /bin/sh
$ set -o vi
$ ^L^L  # literal escape, nothing happens

$ set -o emacs
$ true^L# redraws the line
$ true

So where does ^L redraw the current line on ^L for you?



Re: ksh "clear-screen" editing command

2019-04-02 Thread Theo Buehler
On Tue, Apr 02, 2019 at 05:04:42PM +0200, Klemens Nanni wrote:
> On Tue, Apr 02, 2019 at 04:56:58PM +0200, Jeremie Courreges-Anglas wrote:
> > The diff changes only the emacs mode.  I don't think sh.1 needs to be
> > adjusted given that the paragraph you quote is about vi mode.
> Sure it's just emacs mode.  But for sh(1), ^L does print a literal "^L"
> in vi mode; in emacs mode, it currently refreshes the line as this
> paragraph states.

Yes, ^L is printed in vi insert mode. The text you quoted is about vi
command mode which does indeed redraw the current line on ^L. I agree
with jca, no need for a change there.



Re: ksh "clear-screen" editing command

2019-04-02 Thread Theo Buehler
On Tue, Apr 02, 2019 at 10:52:34AM +0200, Jeremie Courreges-Anglas wrote:
> On Mon, Apr 01 2019, Jeremie Courreges-Anglas  wrote:
> > On Mon, Jun 18 2018, "Todd C. Miller"  wrote:
> >> On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:
> >>
> >>> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
> >>>
> >>> > If folks indeed think that this is a must-have feature, this is
> >>> > certainly a better approach.  I wonder though if the setupterm() call
> >>> > should happen earlier when interactive mode is initialized?
> >>>
> >>> This turns out to be simpler than expected.  Now whenever TERM is
> >>> set (including at startup) it will call setupterm().
> >>
> >> Hopefully final diff that better handles unknown term types.  Now
> >> if you set TERM=gobbledegook the old cur_term will be invalidated
> >> and clear-screen will not try to the escape sequence for the old
> >> terminal.
> >>
> >> I think this is good enough to commit.
> >
> > Since this went in, I'm using it on my machines instead of a bind -m hack.
> >
> > Can't we make ^L=clear-screen the default behavior?  I don't see
> > discussions about that.
> 
> So here's a diff.  oks/nays?
> 

ok

> 
> NB: regress needs to be handled, as mentioned by Anton.  For now I think
> I'll comment out the ^L test, not sure how to amend it.
> 
> Index: emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.85
> diff -u -p -r1.85 emacs.c
> --- emacs.c   18 Jun 2018 17:03:58 -  1.85
> +++ emacs.c   2 Apr 2019 08:43:28 -
> @@ -1529,7 +1529,7 @@ x_init_emacs(void)
>   kb_add(x_prev_histword, CTRL('['), '_', 0);
>   /* how to handle: quote: ^^ */
>   kb_add(x_literal,   CTRL('^'), 0);
> - kb_add(x_draw_line, CTRL('L'), 0);
> + kb_add(x_clear_screen,  CTRL('L'), 0);
>   kb_add(x_search_char_back,  CTRL('['), CTRL(']'), 0);
>   kb_add(x_search_char_forw,  CTRL(']'), 0);
>   kb_add(x_search_hist,   CTRL('R'), 0);
> Index: ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.202
> diff -u -p -r1.202 ksh.1
> --- ksh.1 16 Dec 2018 13:08:35 -  1.202
> +++ ksh.1 2 Apr 2019 08:43:28 -
> @@ -4694,7 +4694,7 @@ Moves the cursor to the beginning of the
>  Uppercase the first character in the next
>  .Ar n
>  words, leaving the cursor past the end of the last word.
> -.It clear-screen:
> +.It clear-screen: ^L
>  Clears the screen if the
>  .Ev TERM
>  parameter is set and the terminal supports clearing the screen, then
> @@ -4891,7 +4891,7 @@ The last
>  word of the previous command is inserted at the cursor.
>  .It quote: ^^
>  The following character is taken literally rather than as an editing command.
> -.It redraw: ^L
> +.It redraw:
>  Reprints the prompt string and the current input line.
>  .It Xo search-character-backward:
>  .Op Ar n
> 
> 
> -- 
> jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
> 



Re: ksh "clear-screen" editing command

2019-04-02 Thread Klemens Nanni
On Tue, Apr 02, 2019 at 04:56:58PM +0200, Jeremie Courreges-Anglas wrote:
> The diff changes only the emacs mode.  I don't think sh.1 needs to be
> adjusted given that the paragraph you quote is about vi mode.
Sure it's just emacs mode.  But for sh(1), ^L does print a literal "^L"
in vi mode; in emacs mode, it currently refreshes the line as this
paragraph states.



Re: ksh "clear-screen" editing command

2019-04-02 Thread Jeremie Courreges-Anglas
On Tue, Apr 02 2019, Klemens Nanni  wrote:
> On Tue, Apr 02, 2019 at 10:52:34AM +0200, Jeremie Courreges-Anglas wrote:
>> So here's a diff.  oks/nays?
> OK with the one mention in sh(1) adjusted as well:
>
>  There are two modes, interactive and command.  The shell starts in
>  interactive mode.  In this mode text is entered normally.  A ⟨newline⟩
>  executes the current command line.  The command line, unless empty, is
>  entered into command history.  The ⟨ESC⟩ key is used to enter command
>  mode, where commands similar to those used by vi(1) are available.  A
>  Ctrl-L sequence (^L) can be used in this mode to redraw the current
>  command line.

The diff changes only the emacs mode.  I don't think sh.1 needs to be
adjusted given that the paragraph you quote is about vi mode.

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



Re: ksh "clear-screen" editing command

2019-04-02 Thread Klemens Nanni
On Tue, Apr 02, 2019 at 10:52:34AM +0200, Jeremie Courreges-Anglas wrote:
> So here's a diff.  oks/nays?
OK with the one mention in sh(1) adjusted as well:

 There are two modes, interactive and command.  The shell starts in
 interactive mode.  In this mode text is entered normally.  A ⟨newline⟩
 executes the current command line.  The command line, unless empty, is
 entered into command history.  The ⟨ESC⟩ key is used to enter command
 mode, where commands similar to those used by vi(1) are available.  A
 Ctrl-L sequence (^L) can be used in this mode to redraw the current
 command line.



Re: ksh "clear-screen" editing command

2019-04-02 Thread Jeremie Courreges-Anglas
On Mon, Apr 01 2019, Jeremie Courreges-Anglas  wrote:
> On Mon, Jun 18 2018, "Todd C. Miller"  wrote:
>> On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:
>>
>>> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
>>>
>>> > If folks indeed think that this is a must-have feature, this is
>>> > certainly a better approach.  I wonder though if the setupterm() call
>>> > should happen earlier when interactive mode is initialized?
>>>
>>> This turns out to be simpler than expected.  Now whenever TERM is
>>> set (including at startup) it will call setupterm().
>>
>> Hopefully final diff that better handles unknown term types.  Now
>> if you set TERM=gobbledegook the old cur_term will be invalidated
>> and clear-screen will not try to the escape sequence for the old
>> terminal.
>>
>> I think this is good enough to commit.
>
> Since this went in, I'm using it on my machines instead of a bind -m hack.
>
> Can't we make ^L=clear-screen the default behavior?  I don't see
> discussions about that.

So here's a diff.  oks/nays?


NB: regress needs to be handled, as mentioned by Anton.  For now I think
I'll comment out the ^L test, not sure how to amend it.

Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.85
diff -u -p -r1.85 emacs.c
--- emacs.c 18 Jun 2018 17:03:58 -  1.85
+++ emacs.c 2 Apr 2019 08:43:28 -
@@ -1529,7 +1529,7 @@ x_init_emacs(void)
kb_add(x_prev_histword, CTRL('['), '_', 0);
/* how to handle: quote: ^^ */
kb_add(x_literal,   CTRL('^'), 0);
-   kb_add(x_draw_line, CTRL('L'), 0);
+   kb_add(x_clear_screen,  CTRL('L'), 0);
kb_add(x_search_char_back,  CTRL('['), CTRL(']'), 0);
kb_add(x_search_char_forw,  CTRL(']'), 0);
kb_add(x_search_hist,   CTRL('R'), 0);
Index: ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.202
diff -u -p -r1.202 ksh.1
--- ksh.1   16 Dec 2018 13:08:35 -  1.202
+++ ksh.1   2 Apr 2019 08:43:28 -
@@ -4694,7 +4694,7 @@ Moves the cursor to the beginning of the
 Uppercase the first character in the next
 .Ar n
 words, leaving the cursor past the end of the last word.
-.It clear-screen:
+.It clear-screen: ^L
 Clears the screen if the
 .Ev TERM
 parameter is set and the terminal supports clearing the screen, then
@@ -4891,7 +4891,7 @@ The last
 word of the previous command is inserted at the cursor.
 .It quote: ^^
 The following character is taken literally rather than as an editing command.
-.It redraw: ^L
+.It redraw:
 Reprints the prompt string and the current input line.
 .It Xo search-character-backward:
 .Op Ar n


-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



Re: ksh "clear-screen" editing command

2019-04-02 Thread Peter Hessler
On 2019 Apr 01 (Mon) at 09:53:31 -0600 (-0600), Todd C. Miller wrote:
:On Mon, 01 Apr 2019 16:52:34 +0200, Jeremie Courreges-Anglas wrote:
:
:> Since this went in, I'm using it on my machines instead of a bind -m hack.
:>
:> Can't we make ^L=clear-screen the default behavior?  I don't see
:> discussions about that.
:
:AT&T ksh doesn't clear the screen by default on ^L.  Other shells
:like bash, zsh, and tcsh do.  I don't object to making it the default
:but as I'm not a ksh user I'll defer to those who are.
:
: - todd
:

I always wondered why bash felt broken, this is one of the reasons.

I hate it, but I guess I can add a line to my configs.

-- 
There is nothing wrong with Southern California that a rise in the
ocean level wouldn't cure.
-- Ross MacDonald



Re: ksh "clear-screen" editing command

2019-04-01 Thread Sebastian Benoit
Jeremie Courreges-Anglas(j...@wxcvbn.org) on 2019.04.01 16:52:34 +0200:
> On Mon, Jun 18 2018, "Todd C. Miller"  wrote:
> > On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:
> >
> >> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
> >>
> >> > If folks indeed think that this is a must-have feature, this is
> >> > certainly a better approach.  I wonder though if the setupterm() call
> >> > should happen earlier when interactive mode is initialized?
> >>
> >> This turns out to be simpler than expected.  Now whenever TERM is
> >> set (including at startup) it will call setupterm().
> >
> > Hopefully final diff that better handles unknown term types.  Now
> > if you set TERM=gobbledegook the old cur_term will be invalidated
> > and clear-screen will not try to the escape sequence for the old
> > terminal.
> >
> > I think this is good enough to commit.
> 
> Since this went in, I'm using it on my machines instead of a bind -m hack.
> 
> Can't we make ^L=clear-screen the default behavior?  I don't see
> discussions about that.

I dont see why not, one less line in my .kshrc.



Re: ksh "clear-screen" editing command

2019-04-01 Thread Klemens Nanni
On Mon, Apr 01, 2019 at 09:53:31AM -0600, Todd C. Miller wrote:
> AT&T ksh doesn't clear the screen by default on ^L.  Other shells
> like bash, zsh, and tcsh do.  I don't object to making it the default
> but as I'm not a ksh user I'll defer to those who are.
Although I'm mostly using ksh in Vi mode, having ^L default to the same
behaviour as bash and zsh would contribute to a slightly more consistent
feeling across different systems/shells which I do appreciate.



Re: ksh "clear-screen" editing command

2019-04-01 Thread Todd C . Miller
On Mon, 01 Apr 2019 16:52:34 +0200, Jeremie Courreges-Anglas wrote:

> Since this went in, I'm using it on my machines instead of a bind -m hack.
>
> Can't we make ^L=clear-screen the default behavior?  I don't see
> discussions about that.

AT&T ksh doesn't clear the screen by default on ^L.  Other shells
like bash, zsh, and tcsh do.  I don't object to making it the default
but as I'm not a ksh user I'll defer to those who are.

 - todd



Re: ksh "clear-screen" editing command

2019-04-01 Thread Jeremie Courreges-Anglas
On Mon, Jun 18 2018, "Todd C. Miller"  wrote:
> On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:
>
>> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
>>
>> > If folks indeed think that this is a must-have feature, this is
>> > certainly a better approach.  I wonder though if the setupterm() call
>> > should happen earlier when interactive mode is initialized?
>>
>> This turns out to be simpler than expected.  Now whenever TERM is
>> set (including at startup) it will call setupterm().
>
> Hopefully final diff that better handles unknown term types.  Now
> if you set TERM=gobbledegook the old cur_term will be invalidated
> and clear-screen will not try to the escape sequence for the old
> terminal.
>
> I think this is good enough to commit.

Since this went in, I'm using it on my machines instead of a bind -m hack.

Can't we make ^L=clear-screen the default behavior?  I don't see
discussions about that.

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



Re: ksh "clear-screen" editing command

2018-06-23 Thread Nicholas Marriott
Never mind, should have finished reading the later emails ;-).



On Sat, Jun 23, 2018 at 09:02:24AM +0100, Nicholas Marriott wrote:
> Hi
> 
> I think you should not pass NULL to the last argument of setupterm(),
> from terminfo(3):
> 
>   If errret is null, setupterm prints an error message upon finding an
>   error and exits.
> 
> 
> 
> On Sat, Jun 16, 2018 at 04:16:57PM -0600, Todd C. Miller wrote:
> > On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
> > 
> > > To be honest, I find the whole idea of invoking an external program to
> > > clear the screen insane.
> > 
> > Linking with curses doesn't increase the size a huge amount since
> > we just need to query terminfo.
> > 
> > textdatabss dec hex
> > 529120  12584   57024   598728  922c8   /bin/ksh
> > 595671  21904   57024   674599  a4b27   ./obj/ksh
> > 
> >  - todd
> > 
> > Index: bin/ksh/Makefile
> > ===
> > RCS file: /cvs/src/bin/ksh/Makefile,v
> > retrieving revision 1.38
> > diff -u -p -u -r1.38 Makefile
> > --- bin/ksh/Makefile6 Jan 2018 16:28:58 -   1.38
> > +++ bin/ksh/Makefile16 Jun 2018 22:00:32 -
> > @@ -1,6 +1,9 @@
> >  #  $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
> >  
> >  PROG=  ksh
> > +DPADD+=${LIBCURSES}
> > +LDADD+=-lcurses
> > +
> >  SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c 
> > eval.c \
> > exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
> > misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
> > Index: bin/ksh/edit.c
> > ===
> > RCS file: /cvs/src/bin/ksh/edit.c,v
> > retrieving revision 1.65
> > diff -u -p -u -r1.65 edit.c
> > --- bin/ksh/edit.c  9 Apr 2018 17:53:36 -   1.65
> > +++ bin/ksh/edit.c  16 Jun 2018 22:09:17 -
> > @@ -138,10 +138,10 @@ x_flush(void)
> > shf_flush(shl_out);
> >  }
> >  
> > -void
> > +int
> >  x_putc(int c)
> >  {
> > -   shf_putc(c, shl_out);
> > +   return shf_putc(c, shl_out);
> >  }
> >  
> >  void
> > Index: bin/ksh/edit.h
> > ===
> > RCS file: /cvs/src/bin/ksh/edit.h,v
> > retrieving revision 1.11
> > diff -u -p -u -r1.11 edit.h
> > --- bin/ksh/edit.h  26 Jan 2016 17:39:31 -  1.11
> > +++ bin/ksh/edit.h  16 Jun 2018 22:09:27 -
> > @@ -37,7 +37,7 @@ extern X_chars edchars;
> >  /* edit.c */
> >  intx_getc(void);
> >  void   x_flush(void);
> > -void   x_putc(int);
> > +intx_putc(int);
> >  void   x_puts(const char *);
> >  bool   x_mode(bool);
> >  intpromptlen(const char *, const char **);
> > Index: bin/ksh/emacs.c
> > ===
> > RCS file: /cvs/src/bin/ksh/emacs.c,v
> > retrieving revision 1.84
> > diff -u -p -u -r1.84 emacs.c
> > --- bin/ksh/emacs.c 16 Jan 2018 17:17:18 -  1.84
> > +++ bin/ksh/emacs.c 16 Jun 2018 22:12:24 -
> > @@ -21,6 +21,10 @@
> >  #include 
> >  #include 
> >  #include 
> > +#ifndef SMALL
> > +# include 
> > +# include 
> > +#endif
> >  
> >  #include "sh.h"
> >  #include "edit.h"
> > @@ -28,6 +32,7 @@
> >  static Areaaedit;
> >  #defineAEDIT   &aedit  /* area for kill ring and macro defns */
> >  
> > +#undef CTRL
> >  #defineCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
> > ASCII */
> >  #defineUNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
> > ASCII */
> >  
> > @@ -146,6 +151,7 @@ static int  isu8cont(unsigned char);
> >  /* proto's for keybindings */
> >  static int x_abort(int);
> >  static int x_beg_hist(int);
> > +static int x_clear_screen(int);
> >  static int x_comp_comm(int);
> >  static int x_comp_file(int);
> >  static int x_complete(int);
> > @@ -202,6 +208,7 @@ static int  x_debug_info(int);
> >  static const struct x_ftab x_ftab[] = {
> > { x_abort,  "abort",0 },
> > { x_beg_hist,   "beginning-of-history", 0 },
> > +   { x_clear_screen,   "clear-screen", 0 },
> > { x_comp_comm,  "complete-command", 0 },
> > { x_comp_file,  "complete-file",0 },
> > { x_complete,   "complete", 0 },
> > @@ -1004,12 +1011,19 @@ x_draw_line(int c)
> >  {
> > x_redraw(-1);
> > return KSTD;
> > +}
> >  
> > +static int
> > +x_clear_screen(int c)
> > +{
> > +   x_redraw(-2);
> > +   return KSTD;
> >  }
> >  
> > -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> > - * on a NEW line, otherwise limit is the screen column up to which needs
> > - * redrawing.
> > +/* Redraw (part of) the line.
> > + * A non-negative limit is the screen column up to which needs
> > + * redrawing. A limit of -1 redraws on a new line, while a limit
> > + * of -2 (attempts to)

Re: ksh "clear-screen" editing command

2018-06-23 Thread Nicholas Marriott
Hi

I think you should not pass NULL to the last argument of setupterm(),
from terminfo(3):

  If errret is null, setupterm prints an error message upon finding an
  error and exits.
  


On Sat, Jun 16, 2018 at 04:16:57PM -0600, Todd C. Miller wrote:
> On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
> 
> > To be honest, I find the whole idea of invoking an external program to
> > clear the screen insane.
> 
> Linking with curses doesn't increase the size a huge amount since
> we just need to query terminfo.
> 
> textdatabss dec hex
> 529120  12584   57024   598728  922c8   /bin/ksh
> 595671  21904   57024   674599  a4b27   ./obj/ksh
> 
>  - todd
> 
> Index: bin/ksh/Makefile
> ===
> RCS file: /cvs/src/bin/ksh/Makefile,v
> retrieving revision 1.38
> diff -u -p -u -r1.38 Makefile
> --- bin/ksh/Makefile  6 Jan 2018 16:28:58 -   1.38
> +++ bin/ksh/Makefile  16 Jun 2018 22:00:32 -
> @@ -1,6 +1,9 @@
>  #$OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
>  
>  PROG=ksh
> +DPADD+=  ${LIBCURSES}
> +LDADD+=  -lcurses
> +
>  SRCS=alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c 
> eval.c \
>   exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
>   misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
> Index: bin/ksh/edit.c
> ===
> RCS file: /cvs/src/bin/ksh/edit.c,v
> retrieving revision 1.65
> diff -u -p -u -r1.65 edit.c
> --- bin/ksh/edit.c9 Apr 2018 17:53:36 -   1.65
> +++ bin/ksh/edit.c16 Jun 2018 22:09:17 -
> @@ -138,10 +138,10 @@ x_flush(void)
>   shf_flush(shl_out);
>  }
>  
> -void
> +int
>  x_putc(int c)
>  {
> - shf_putc(c, shl_out);
> + return shf_putc(c, shl_out);
>  }
>  
>  void
> Index: bin/ksh/edit.h
> ===
> RCS file: /cvs/src/bin/ksh/edit.h,v
> retrieving revision 1.11
> diff -u -p -u -r1.11 edit.h
> --- bin/ksh/edit.h26 Jan 2016 17:39:31 -  1.11
> +++ bin/ksh/edit.h16 Jun 2018 22:09:27 -
> @@ -37,7 +37,7 @@ extern X_chars edchars;
>  /* edit.c */
>  int  x_getc(void);
>  void x_flush(void);
> -void x_putc(int);
> +int  x_putc(int);
>  void x_puts(const char *);
>  bool x_mode(bool);
>  int  promptlen(const char *, const char **);
> Index: bin/ksh/emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.84
> diff -u -p -u -r1.84 emacs.c
> --- bin/ksh/emacs.c   16 Jan 2018 17:17:18 -  1.84
> +++ bin/ksh/emacs.c   16 Jun 2018 22:12:24 -
> @@ -21,6 +21,10 @@
>  #include 
>  #include 
>  #include 
> +#ifndef SMALL
> +# include 
> +# include 
> +#endif
>  
>  #include "sh.h"
>  #include "edit.h"
> @@ -28,6 +32,7 @@
>  static   Areaaedit;
>  #define  AEDIT   &aedit  /* area for kill ring and macro defns */
>  
> +#undef CTRL
>  #define  CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
> ASCII */
>  #define  UNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
> ASCII */
>  
> @@ -146,6 +151,7 @@ static intisu8cont(unsigned char);
>  /* proto's for keybindings */
>  static int   x_abort(int);
>  static int   x_beg_hist(int);
> +static int   x_clear_screen(int);
>  static int   x_comp_comm(int);
>  static int   x_comp_file(int);
>  static int   x_complete(int);
> @@ -202,6 +208,7 @@ static intx_debug_info(int);
>  static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
> + { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
> @@ -1004,12 +1011,19 @@ x_draw_line(int c)
>  {
>   x_redraw(-1);
>   return KSTD;
> +}
>  
> +static int
> +x_clear_screen(int c)
> +{
> + x_redraw(-2);
> + return KSTD;
>  }
>  
> -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> - * on a NEW line, otherwise limit is the screen column up to which needs
> - * redrawing.
> +/* Redraw (part of) the line.
> + * A non-negative limit is the screen column up to which needs
> + * redrawing. A limit of -1 redraws on a new line, while a limit
> + * of -2 (attempts to) clear the screen.
>   */
>  static void
>  x_redraw(int limit)
> @@ -1018,9 +1032,23 @@ x_redraw(int limit)
>   char*cp;
>  
>   x_adj_ok = 0;
> - if (limit == -1)
> + if (limit == -2) {
> + int ret = -1;
> +#ifndef SMALL
> + char *str, *term = str_val(global("TERM"));
> + if (term && *term) {
> + setupterm(term, 1, 

Re: ksh "clear-screen" editing command

2018-06-18 Thread Alexander Hall



On June 18, 2018 11:00:00 PM GMT+02:00, Richard Procter 
 wrote:
>
>On 6/06/2018, at 10:20 AM, Alexander Hall wrote:
>
>> This adds a "clear-screen" editing command to the emacs editing mode.
>> This is the same name as bash and zsh uses, and then I stopped
>looking.
>> 
>> Thoughts? OK?
>
>It's unclear to me what need is being being addressed here --- are you
>wanting a way to quickly hide sensitive info typed by mistake at a
>commandline?

Hi. Your comments and concerns are appreciated.

My main use case for this is when I'm in the middle of typing a command, and 
then realize I would like have its output on a blank screen to make it easier 
to separate from the previous command.

>
>^Cclear^M is available, of course, or ^A^K, or the "bind -m
>'^L'=^Uclear'^J^Y'"
>that benno@ mentioned. On the console, one can switch VT. I suppose
>some
>window managers offer short-cuts to "lock screen", or can switch
>virtual
>desktops, or minimise the window.
>
>Yes, I could see how the patch might be warranted if this occured
>frequently, but how often do you find yourself needing it? Often enough
>to be annoyed by the side-effects you mentioned of benno's key binding?

Getting used to having it at work, I find it being a useful feature I'm using, 
and thus lacking, several times a day. I'm willing to accept it being 
considered featuritis.

I'd rather not have it pollute my buffers, since it has nothing to do with 
them. Oh, and it's screws up any intentions of using ^O. 

>It seems to me this patch would make ksh into a screen-orientated shell

I don't think this diff puts us there quite yet. 

>for the sake of reimplementing a rarely-used shortcut.

For me it's not rarely used. Unless of course I'm using OpenBSD's ksh.

/Alexander 

>
>best, 
>Richard.  
>
>> 
>> 
>> /Alexander
>> 
>> 
>> Index: emacs.c
>> ===
>> RCS file: /cvs/src/bin/ksh/emacs.c,v
>> retrieving revision 1.84
>> diff -u -p -r1.84 emacs.c
>> --- emacs.c  16 Jan 2018 17:17:18 -  1.84
>> +++ emacs.c  5 Jun 2018 22:03:49 -
>> @@ -146,6 +146,7 @@ static int   isu8cont(unsigned char);
>> /* proto's for keybindings */
>> static int   x_abort(int);
>> static int   x_beg_hist(int);
>> +static int  x_clear_screen(int);
>> static int   x_comp_comm(int);
>> static int   x_comp_file(int);
>> static int   x_complete(int);
>> @@ -202,6 +203,7 @@ static int   x_debug_info(int);
>> static const struct x_ftab x_ftab[] = {
>>  { x_abort,  "abort",0 },
>>  { x_beg_hist,   "beginning-of-history", 0 },
>> +{ x_clear_screen,   "clear-screen", 0 },
>>  { x_comp_comm,  "complete-command", 0 },
>>  { x_comp_file,  "complete-file",0 },
>>  { x_complete,   "complete", 0 },
>> @@ -1004,12 +1006,19 @@ x_draw_line(int c)
>> {
>>  x_redraw(-1);
>>  return KSTD;
>> +}
>> 
>> +static int
>> +x_clear_screen(int c)
>> +{
>> +x_redraw(-2);
>> +return KSTD;
>> }
>> 
>> -/* Redraw (part of) the line.  If limit is < 0, the everything is
>redrawn
>> - * on a NEW line, otherwise limit is the screen column up to which
>needs
>> - * redrawing.
>> +/* Redraw (part of) the line.
>> + * A non-negative limit is the screen column up to which needs
>> + * redrawing. A limit of -1 redraws on a new line, while a limit
>> + * of -2 (attempts to) clear the screen.
>>  */
>> static void
>> x_redraw(int limit)
>> @@ -1018,9 +1027,15 @@ x_redraw(int limit)
>>  char*cp;
>> 
>>  x_adj_ok = 0;
>> -if (limit == -1)
>> +if (limit == -2) {
>> +char *clearstr = str_val(global("CLEARSTR"));
>> +if (clearstr == null)
>> +clearstr = "\033[H\033[J";
>> +x_e_puts(clearstr);
>> +}
>> +else if (limit == -1)
>>  x_e_putc('\n');
>> -else
>> +else if (limit >= 0)
>>  x_e_putc('\r');
>>  x_flush();
>>  if (xbp == xbuf) {
>> Index: ksh.1
>> ===
>> RCS file: /cvs/src/bin/ksh/ksh.1,v
>> retrieving revision 1.200
>> diff -u -p -r1.200 ksh.1
>> --- ksh.130 May 2018 21:20:52 -  1.200
>> +++ ksh.15 Jun 2018 22:03:49 -
>> @@ -1345,6 +1345,8 @@ Also, the
>> .Ic cd
>> built-in command will display the resulting directory when a match is
>found
>> in any search path other than the empty path.
>> +.It Ev CLEARSTR
>> +If set, overrides the default escape sequence to clear the screen.
>> .It Ev COLUMNS
>> Set to the number of columns on the terminal or window.
>> Currently set to the
>> @@ -4690,6 +4692,12 @@ Moves the cursor to the beginning of the
>> Uppercase the first character in the next
>> .Ar n
>> words, leaving the cursor past the end of the last word.
>> +.It clear-screen:
>> +Clears the screen and redraws the prompt and current input line.
>> +If the
>> 

Re: ksh "clear-screen" editing command

2018-06-18 Thread Richard Procter


On 6/06/2018, at 10:20 AM, Alexander Hall wrote:

> This adds a "clear-screen" editing command to the emacs editing mode.
> This is the same name as bash and zsh uses, and then I stopped looking.
> 
> Thoughts? OK?

It's unclear to me what need is being being addressed here --- are you
wanting a way to quickly hide sensitive info typed by mistake at a
commandline?

^Cclear^M is available, of course, or ^A^K, or the "bind -m '^L'=^Uclear'^J^Y'"
that benno@ mentioned. On the console, one can switch VT. I suppose some
window managers offer short-cuts to "lock screen", or can switch virtual
desktops, or minimise the window.

Yes, I could see how the patch might be warranted if this occured
frequently, but how often do you find yourself needing it? Often enough
to be annoyed by the side-effects you mentioned of benno's key binding?

It seems to me this patch would make ksh into a screen-orientated shell
for the sake of reimplementing a rarely-used shortcut.

best, 
Richard.  

> 
> 
> /Alexander
> 
> 
> Index: emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.84
> diff -u -p -r1.84 emacs.c
> --- emacs.c   16 Jan 2018 17:17:18 -  1.84
> +++ emacs.c   5 Jun 2018 22:03:49 -
> @@ -146,6 +146,7 @@ static intisu8cont(unsigned char);
> /* proto's for keybindings */
> static intx_abort(int);
> static intx_beg_hist(int);
> +static int   x_clear_screen(int);
> static intx_comp_comm(int);
> static intx_comp_file(int);
> static intx_complete(int);
> @@ -202,6 +203,7 @@ static intx_debug_info(int);
> static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
> + { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
> @@ -1004,12 +1006,19 @@ x_draw_line(int c)
> {
>   x_redraw(-1);
>   return KSTD;
> +}
> 
> +static int
> +x_clear_screen(int c)
> +{
> + x_redraw(-2);
> + return KSTD;
> }
> 
> -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> - * on a NEW line, otherwise limit is the screen column up to which needs
> - * redrawing.
> +/* Redraw (part of) the line.
> + * A non-negative limit is the screen column up to which needs
> + * redrawing. A limit of -1 redraws on a new line, while a limit
> + * of -2 (attempts to) clear the screen.
>  */
> static void
> x_redraw(int limit)
> @@ -1018,9 +1027,15 @@ x_redraw(int limit)
>   char*cp;
> 
>   x_adj_ok = 0;
> - if (limit == -1)
> + if (limit == -2) {
> + char *clearstr = str_val(global("CLEARSTR"));
> + if (clearstr == null)
> + clearstr = "\033[H\033[J";
> + x_e_puts(clearstr);
> + }
> + else if (limit == -1)
>   x_e_putc('\n');
> - else
> + else if (limit >= 0)
>   x_e_putc('\r');
>   x_flush();
>   if (xbp == xbuf) {
> Index: ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.200
> diff -u -p -r1.200 ksh.1
> --- ksh.1 30 May 2018 21:20:52 -  1.200
> +++ ksh.1 5 Jun 2018 22:03:49 -
> @@ -1345,6 +1345,8 @@ Also, the
> .Ic cd
> built-in command will display the resulting directory when a match is found
> in any search path other than the empty path.
> +.It Ev CLEARSTR
> +If set, overrides the default escape sequence to clear the screen.
> .It Ev COLUMNS
> Set to the number of columns on the terminal or window.
> Currently set to the
> @@ -4690,6 +4692,12 @@ Moves the cursor to the beginning of the
> Uppercase the first character in the next
> .Ar n
> words, leaving the cursor past the end of the last word.
> +.It clear-screen:
> +Clears the screen and redraws the prompt and current input line.
> +If the
> +.Ev CLEARSTR
> +parameter is set, it is used to clear the screen.
> +Otherwise, a default escape sequence (^[[H^[2J) is used.
> .It comment: ^[#
> If the current line does not begin with a comment character, one is added at
> the beginning of the line and the line is entered (as if return had been
> 



Re: ksh "clear-screen" editing command

2018-06-18 Thread Todd C. Miller
On Sun, 17 Jun 2018 15:52:34 -0600, "Todd C. Miller" wrote:

> On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:
>
> > If folks indeed think that this is a must-have feature, this is
> > certainly a better approach.  I wonder though if the setupterm() call
> > should happen earlier when interactive mode is initialized?
>
> This turns out to be simpler than expected.  Now whenever TERM is
> set (including at startup) it will call setupterm().

Hopefully final diff that better handles unknown term types.  Now
if you set TERM=gobbledegook the old cur_term will be invalidated
and clear-screen will not try to the escape sequence for the old
terminal.

I think this is good enough to commit.

 - todd

Index: bin/ksh/Makefile
===
RCS file: /cvs/src/bin/ksh/Makefile,v
retrieving revision 1.38
diff -u -p -u -r1.38 Makefile
--- bin/ksh/Makefile6 Jan 2018 16:28:58 -   1.38
+++ bin/ksh/Makefile16 Jun 2018 22:00:32 -
@@ -1,6 +1,9 @@
 #  $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
 
 PROG=  ksh
+DPADD+=${LIBCURSES}
+LDADD+=-lcurses
+
 SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
Index: bin/ksh/edit.c
===
RCS file: /cvs/src/bin/ksh/edit.c,v
retrieving revision 1.65
diff -u -p -u -r1.65 edit.c
--- bin/ksh/edit.c  9 Apr 2018 17:53:36 -   1.65
+++ bin/ksh/edit.c  16 Jun 2018 22:09:17 -
@@ -138,10 +138,10 @@ x_flush(void)
shf_flush(shl_out);
 }
 
-void
+int
 x_putc(int c)
 {
-   shf_putc(c, shl_out);
+   return shf_putc(c, shl_out);
 }
 
 void
Index: bin/ksh/edit.h
===
RCS file: /cvs/src/bin/ksh/edit.h,v
retrieving revision 1.11
diff -u -p -u -r1.11 edit.h
--- bin/ksh/edit.h  26 Jan 2016 17:39:31 -  1.11
+++ bin/ksh/edit.h  16 Jun 2018 22:09:27 -
@@ -37,7 +37,7 @@ extern X_chars edchars;
 /* edit.c */
 intx_getc(void);
 void   x_flush(void);
-void   x_putc(int);
+intx_putc(int);
 void   x_puts(const char *);
 bool   x_mode(bool);
 intpromptlen(const char *, const char **);
Index: bin/ksh/emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -u -r1.84 emacs.c
--- bin/ksh/emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ bin/ksh/emacs.c 18 Jun 2018 03:30:09 -
@@ -21,6 +21,10 @@
 #include 
 #include 
 #include 
+#ifndef SMALL
+# include 
+# include 
+#endif
 
 #include "sh.h"
 #include "edit.h"
@@ -28,6 +32,7 @@
 static Areaaedit;
 #defineAEDIT   &aedit  /* area for kill ring and macro defns */
 
+#undef CTRL
 #defineCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
ASCII */
 #defineUNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
ASCII */
 
@@ -146,6 +151,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +208,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1011,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1032,20 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   int cleared = 0;
+#ifndef SMALL
+   if (cur_term != NULL && clear_screen != NULL) {
+   if (tputs(clear_screen, 1, x_putc) != ERR)
+   cleared = 1;
+   }
+#endif
+   if (!cleared)
+   x_e_putc('\n');
+   }
+   else if (limit == -1)

Re: ksh "clear-screen" editing command

2018-06-17 Thread Todd C. Miller
On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:

> If folks indeed think that this is a must-have feature, this is
> certainly a better approach.  I wonder though if the setupterm() call
> should happen earlier when interactive mode is initialized?

This turns out to be simpler than expected.  Now whenever TERM is
set (including at startup) it will call setupterm().

 - todd

Index: bin/ksh/Makefile
===
RCS file: /cvs/src/bin/ksh/Makefile,v
retrieving revision 1.38
diff -u -p -u -r1.38 Makefile
--- bin/ksh/Makefile6 Jan 2018 16:28:58 -   1.38
+++ bin/ksh/Makefile16 Jun 2018 22:00:32 -
@@ -1,6 +1,9 @@
 #  $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
 
 PROG=  ksh
+DPADD+=${LIBCURSES}
+LDADD+=-lcurses
+
 SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
Index: bin/ksh/edit.c
===
RCS file: /cvs/src/bin/ksh/edit.c,v
retrieving revision 1.65
diff -u -p -u -r1.65 edit.c
--- bin/ksh/edit.c  9 Apr 2018 17:53:36 -   1.65
+++ bin/ksh/edit.c  16 Jun 2018 22:09:17 -
@@ -138,10 +138,10 @@ x_flush(void)
shf_flush(shl_out);
 }
 
-void
+int
 x_putc(int c)
 {
-   shf_putc(c, shl_out);
+   return shf_putc(c, shl_out);
 }
 
 void
Index: bin/ksh/edit.h
===
RCS file: /cvs/src/bin/ksh/edit.h,v
retrieving revision 1.11
diff -u -p -u -r1.11 edit.h
--- bin/ksh/edit.h  26 Jan 2016 17:39:31 -  1.11
+++ bin/ksh/edit.h  16 Jun 2018 22:09:27 -
@@ -37,7 +37,7 @@ extern X_chars edchars;
 /* edit.c */
 intx_getc(void);
 void   x_flush(void);
-void   x_putc(int);
+intx_putc(int);
 void   x_puts(const char *);
 bool   x_mode(bool);
 intpromptlen(const char *, const char **);
Index: bin/ksh/emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -u -r1.84 emacs.c
--- bin/ksh/emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ bin/ksh/emacs.c 17 Jun 2018 21:43:56 -
@@ -21,6 +21,10 @@
 #include 
 #include 
 #include 
+#ifndef SMALL
+# include 
+# include 
+#endif
 
 #include "sh.h"
 #include "edit.h"
@@ -28,6 +32,7 @@
 static Areaaedit;
 #defineAEDIT   &aedit  /* area for kill ring and macro defns */
 
+#undef CTRL
 #defineCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
ASCII */
 #defineUNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
ASCII */
 
@@ -146,6 +151,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +208,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1011,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1032,20 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   int cleared = 0;
+#ifndef SMALL
+   if (clear_screen != NULL && *clear_screen != '\0') {
+   if (tputs(clear_screen, 1, x_putc) != ERR)
+   cleared = 1;
+   }
+#endif
+   if (!cleared)
+   x_e_putc('\n');
+   }
+   else if (limit == -1)
x_e_putc('\n');
-   else
+   else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Index: bin/ksh/ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.200
diff -u -p -u -r1.20

Re: ksh "clear-screen" editing command

2018-06-17 Thread Alexander Hall
I'm currently on vacation with very limited ability to test, but I do approve 
of this way.

/Alexander 

On June 17, 2018 5:00:17 PM GMT+02:00, "Todd C. Miller"  
wrote:
>On Sat, 16 Jun 2018 16:16:57 -0600, "Todd C. Miller" wrote:
>
>> On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
>>
>> > To be honest, I find the whole idea of invoking an external program
>to
>> > clear the screen insane.
>>
>> Linking with curses doesn't increase the size a huge amount since
>> we just need to query terminfo.
>>
>> textdatabss dec hex
>> 529120  12584   57024   598728  922c8   /bin/ksh
>> 595671  21904   57024   674599  a4b27   ./obj/ksh
>
>Slightly simpler, we can use the clear_screen macro instead of looking
>it up with tigetstr().
>
>textdatabss dec hex
>529120  12584   57024   598728  922c8   /bin/ksh
>594844  21888   57024   673756  a47dc   ./obj/ksh
>
> - todd
>
>Index: Makefile
>===
>RCS file: /cvs/src/bin/ksh/Makefile,v
>retrieving revision 1.38
>diff -u -p -u -r1.38 Makefile
>--- Makefile   6 Jan 2018 16:28:58 -   1.38
>+++ Makefile   16 Jun 2018 22:00:32 -
>@@ -1,6 +1,9 @@
> # $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
> 
> PROG= ksh
>+DPADD+=   ${LIBCURSES}
>+LDADD+=   -lcurses
>+
>SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c
>\
>   exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
>   misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
>Index: edit.c
>===
>RCS file: /cvs/src/bin/ksh/edit.c,v
>retrieving revision 1.65
>diff -u -p -u -r1.65 edit.c
>--- edit.c 9 Apr 2018 17:53:36 -   1.65
>+++ edit.c 16 Jun 2018 22:09:17 -
>@@ -138,10 +138,10 @@ x_flush(void)
>   shf_flush(shl_out);
> }
> 
>-void
>+int
> x_putc(int c)
> {
>-  shf_putc(c, shl_out);
>+  return shf_putc(c, shl_out);
> }
> 
> void
>Index: edit.h
>===
>RCS file: /cvs/src/bin/ksh/edit.h,v
>retrieving revision 1.11
>diff -u -p -u -r1.11 edit.h
>--- edit.h 26 Jan 2016 17:39:31 -  1.11
>+++ edit.h 16 Jun 2018 22:09:27 -
>@@ -37,7 +37,7 @@ extern X_chars edchars;
> /* edit.c */
> int   x_getc(void);
> void  x_flush(void);
>-void  x_putc(int);
>+int   x_putc(int);
> void  x_puts(const char *);
> bool  x_mode(bool);
> int   promptlen(const char *, const char **);
>Index: emacs.c
>===
>RCS file: /cvs/src/bin/ksh/emacs.c,v
>retrieving revision 1.84
>diff -u -p -u -r1.84 emacs.c
>--- emacs.c16 Jan 2018 17:17:18 -  1.84
>+++ emacs.c17 Jun 2018 13:58:36 -
>@@ -21,6 +21,10 @@
> #include 
> #include 
> #include 
>+#ifndef SMALL
>+# include 
>+# include 
>+#endif
> 
> #include "sh.h"
> #include "edit.h"
>@@ -28,6 +32,7 @@
> staticAreaaedit;
> #define   AEDIT   &aedit  /* area for kill ring and macro defns */
> 
>+#undef CTRL
> #define   CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
> ASCII */
> #define   UNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
> ASCII */
> 
>@@ -146,6 +151,7 @@ static int isu8cont(unsigned char);
> /* proto's for keybindings */
> static intx_abort(int);
> static intx_beg_hist(int);
>+static intx_clear_screen(int);
> static intx_comp_comm(int);
> static intx_comp_file(int);
> static intx_complete(int);
>@@ -202,6 +208,7 @@ static int x_debug_info(int);
> static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
>+  { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
>@@ -1004,12 +1011,19 @@ x_draw_line(int c)
> {
>   x_redraw(-1);
>   return KSTD;
>+}
> 
>+static int
>+x_clear_screen(int c)
>+{
>+  x_redraw(-2);
>+  return KSTD;
> }
> 
>-/* Redraw (part of) the line.  If limit is < 0, the everything is
>redrawn
>- * on a NEW line, otherwise limit is the screen column up to which
>needs
>- * redrawing.
>+/* Redraw (part of) the line.
>+ * A non-negative limit is the screen column up to which needs
>+ * redrawing. A limit of -1 redraws on a new line, while a limit
>+ * of -2 (attempts to) clear the screen.
>  */
> static void
> x_redraw(int limit)
>@@ -1018,9 +1032,25 @@ x_redraw(int limit)
>   char*cp;
> 
>   x_adj_ok = 0;
>-  if (limit == -1)
>+  if (limit == -2) {
>+  int cleared = 0;
>+#ifndef SMALL
>+  char *term = str_val(global("TERM"));
>+  if (term && *term) {
>+  int errret

Re: ksh "clear-screen" editing command

2018-06-17 Thread Todd C. Miller
On Sat, 16 Jun 2018 12:41:12 -0700, Il Ka wrote:

> > alternative that doesn't require linking ksh with 
> > curses? 
>
> I'm curious, is it possible to use termcap/terminfo directly here?

My recent diffs use the terminfo api rather than full-blown curses
but it is all the same library.

 - todd



Re: ksh "clear-screen" editing command

2018-06-17 Thread Il Ka
> alternative that doesn't require linking ksh with 
> curses? 

I'm curious, is it possible to use termcap/terminfo directly here?



--
Sent from: 
http://openbsd-archive.7691.n7.nabble.com/openbsd-dev-tech-f151936.html



Re: ksh "clear-screen" editing command

2018-06-17 Thread Todd C. Miller
On Sun, 17 Jun 2018 17:29:31 +0200, Mark Kettenis wrote:

> If folks indeed think that this is a must-have feature, this is
> certainly a better approach.  I wonder though if the setupterm() call
> should happen earlier when interactive mode is initialized?  Probably
> best to link against libterminfo to indicate that we don't really want
> full-blown curses.

You really need to call setupterm() any time TERM is changed, not
just when the shell is started.  We could do that but I was trying
to keep things simple for now.  There is no longer a separate
libterminfo, it is all just libcurses.  Since ksh is statically
linked we only pull in the bits we need anyway.

> The man page needs to be adjusted now that you no longer call clear(1).

I will do that if we agree this is the way forward.

 - todd



Re: ksh "clear-screen" editing command

2018-06-17 Thread Mark Kettenis
> From: "Todd C. Miller" 
> Date: Sun, 17 Jun 2018 09:00:17 -0600
> 
> On Sat, 16 Jun 2018 16:16:57 -0600, "Todd C. Miller" wrote:
> 
> > On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
> >
> > > To be honest, I find the whole idea of invoking an external program to
> > > clear the screen insane.
> >
> > Linking with curses doesn't increase the size a huge amount since
> > we just need to query terminfo.
> >
> > textdatabss dec hex
> > 529120  12584   57024   598728  922c8   /bin/ksh
> > 595671  21904   57024   674599  a4b27   ./obj/ksh
> 
> Slightly simpler, we can use the clear_screen macro instead of looking
> it up with tigetstr().
> 
> textdatabss dec hex
> 529120  12584   57024   598728  922c8   /bin/ksh
> 594844  21888   57024   673756  a47dc   ./obj/ksh

If folks indeed think that this is a must-have feature, this is
certainly a better approach.  I wonder though if the setupterm() call
should happen earlier when interactive mode is initialized?  Probably
best to link against libterminfo to indicate that we don't really want
full-blown curses.

The man page needs to be adjusted now that you no longer call clear(1).

Cheers,

Mark

> Index: Makefile
> ===
> RCS file: /cvs/src/bin/ksh/Makefile,v
> retrieving revision 1.38
> diff -u -p -u -r1.38 Makefile
> --- Makefile  6 Jan 2018 16:28:58 -   1.38
> +++ Makefile  16 Jun 2018 22:00:32 -
> @@ -1,6 +1,9 @@
>  #$OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
>  
>  PROG=ksh
> +DPADD+=  ${LIBCURSES}
> +LDADD+=  -lcurses
> +
>  SRCS=alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c 
> eval.c \
>   exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
>   misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
> Index: edit.c
> ===
> RCS file: /cvs/src/bin/ksh/edit.c,v
> retrieving revision 1.65
> diff -u -p -u -r1.65 edit.c
> --- edit.c9 Apr 2018 17:53:36 -   1.65
> +++ edit.c16 Jun 2018 22:09:17 -
> @@ -138,10 +138,10 @@ x_flush(void)
>   shf_flush(shl_out);
>  }
>  
> -void
> +int
>  x_putc(int c)
>  {
> - shf_putc(c, shl_out);
> + return shf_putc(c, shl_out);
>  }
>  
>  void
> Index: edit.h
> ===
> RCS file: /cvs/src/bin/ksh/edit.h,v
> retrieving revision 1.11
> diff -u -p -u -r1.11 edit.h
> --- edit.h26 Jan 2016 17:39:31 -  1.11
> +++ edit.h16 Jun 2018 22:09:27 -
> @@ -37,7 +37,7 @@ extern X_chars edchars;
>  /* edit.c */
>  int  x_getc(void);
>  void x_flush(void);
> -void x_putc(int);
> +int  x_putc(int);
>  void x_puts(const char *);
>  bool x_mode(bool);
>  int  promptlen(const char *, const char **);
> Index: emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.84
> diff -u -p -u -r1.84 emacs.c
> --- emacs.c   16 Jan 2018 17:17:18 -  1.84
> +++ emacs.c   17 Jun 2018 13:58:36 -
> @@ -21,6 +21,10 @@
>  #include 
>  #include 
>  #include 
> +#ifndef SMALL
> +# include 
> +# include 
> +#endif
>  
>  #include "sh.h"
>  #include "edit.h"
> @@ -28,6 +32,7 @@
>  static   Areaaedit;
>  #define  AEDIT   &aedit  /* area for kill ring and macro defns */
>  
> +#undef CTRL
>  #define  CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
> ASCII */
>  #define  UNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
> ASCII */
>  
> @@ -146,6 +151,7 @@ static intisu8cont(unsigned char);
>  /* proto's for keybindings */
>  static int   x_abort(int);
>  static int   x_beg_hist(int);
> +static int   x_clear_screen(int);
>  static int   x_comp_comm(int);
>  static int   x_comp_file(int);
>  static int   x_complete(int);
> @@ -202,6 +208,7 @@ static intx_debug_info(int);
>  static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
> + { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
> @@ -1004,12 +1011,19 @@ x_draw_line(int c)
>  {
>   x_redraw(-1);
>   return KSTD;
> +}
>  
> +static int
> +x_clear_screen(int c)
> +{
> + x_redraw(-2);
> + return KSTD;
>  }
>  
> -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> - * on a NEW line, otherwise limit is the screen column up to which needs
> - * redrawing.
> +/* Redraw (part of) the line.
> + * A non-negative limit is the screen column up to which needs
> + * redrawing. A limit of -1 redraws on a new line, while a limit
> + * of -2 (a

Re: ksh "clear-screen" editing command

2018-06-17 Thread Todd C. Miller
On Sat, 16 Jun 2018 16:16:57 -0600, "Todd C. Miller" wrote:

> On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
>
> > To be honest, I find the whole idea of invoking an external program to
> > clear the screen insane.
>
> Linking with curses doesn't increase the size a huge amount since
> we just need to query terminfo.
>
> textdatabss dec hex
> 529120  12584   57024   598728  922c8   /bin/ksh
> 595671  21904   57024   674599  a4b27   ./obj/ksh

Slightly simpler, we can use the clear_screen macro instead of looking
it up with tigetstr().

textdatabss dec hex
529120  12584   57024   598728  922c8   /bin/ksh
594844  21888   57024   673756  a47dc   ./obj/ksh

 - todd

Index: Makefile
===
RCS file: /cvs/src/bin/ksh/Makefile,v
retrieving revision 1.38
diff -u -p -u -r1.38 Makefile
--- Makefile6 Jan 2018 16:28:58 -   1.38
+++ Makefile16 Jun 2018 22:00:32 -
@@ -1,6 +1,9 @@
 #  $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
 
 PROG=  ksh
+DPADD+=${LIBCURSES}
+LDADD+=-lcurses
+
 SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
Index: edit.c
===
RCS file: /cvs/src/bin/ksh/edit.c,v
retrieving revision 1.65
diff -u -p -u -r1.65 edit.c
--- edit.c  9 Apr 2018 17:53:36 -   1.65
+++ edit.c  16 Jun 2018 22:09:17 -
@@ -138,10 +138,10 @@ x_flush(void)
shf_flush(shl_out);
 }
 
-void
+int
 x_putc(int c)
 {
-   shf_putc(c, shl_out);
+   return shf_putc(c, shl_out);
 }
 
 void
Index: edit.h
===
RCS file: /cvs/src/bin/ksh/edit.h,v
retrieving revision 1.11
diff -u -p -u -r1.11 edit.h
--- edit.h  26 Jan 2016 17:39:31 -  1.11
+++ edit.h  16 Jun 2018 22:09:27 -
@@ -37,7 +37,7 @@ extern X_chars edchars;
 /* edit.c */
 intx_getc(void);
 void   x_flush(void);
-void   x_putc(int);
+intx_putc(int);
 void   x_puts(const char *);
 bool   x_mode(bool);
 intpromptlen(const char *, const char **);
Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -u -r1.84 emacs.c
--- emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ emacs.c 17 Jun 2018 13:58:36 -
@@ -21,6 +21,10 @@
 #include 
 #include 
 #include 
+#ifndef SMALL
+# include 
+# include 
+#endif
 
 #include "sh.h"
 #include "edit.h"
@@ -28,6 +32,7 @@
 static Areaaedit;
 #defineAEDIT   &aedit  /* area for kill ring and macro defns */
 
+#undef CTRL
 #defineCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
ASCII */
 #defineUNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
ASCII */
 
@@ -146,6 +151,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +208,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1011,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1032,25 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   int cleared = 0;
+#ifndef SMALL
+   char *term = str_val(global("TERM"));
+   if (term && *term) {
+   int errret;
+   if (setupterm(term, 1, &errret) != ERR &&
+   clear_screen != NULL && *clear_screen != '\0') {
+   if (tputs(clear_screen, 1, x_putc) != ERR)
+   cleared = 1;
+  

Re: ksh "clear-screen" editing command

2018-06-16 Thread Todd C. Miller
On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:

> To be honest, I find the whole idea of invoking an external program to
> clear the screen insane.

Linking with curses doesn't increase the size a huge amount since
we just need to query terminfo.

textdatabss dec hex
529120  12584   57024   598728  922c8   /bin/ksh
595671  21904   57024   674599  a4b27   ./obj/ksh

 - todd

Index: bin/ksh/Makefile
===
RCS file: /cvs/src/bin/ksh/Makefile,v
retrieving revision 1.38
diff -u -p -u -r1.38 Makefile
--- bin/ksh/Makefile6 Jan 2018 16:28:58 -   1.38
+++ bin/ksh/Makefile16 Jun 2018 22:00:32 -
@@ -1,6 +1,9 @@
 #  $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $
 
 PROG=  ksh
+DPADD+=${LIBCURSES}
+LDADD+=-lcurses
+
 SRCS=  alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
Index: bin/ksh/edit.c
===
RCS file: /cvs/src/bin/ksh/edit.c,v
retrieving revision 1.65
diff -u -p -u -r1.65 edit.c
--- bin/ksh/edit.c  9 Apr 2018 17:53:36 -   1.65
+++ bin/ksh/edit.c  16 Jun 2018 22:09:17 -
@@ -138,10 +138,10 @@ x_flush(void)
shf_flush(shl_out);
 }
 
-void
+int
 x_putc(int c)
 {
-   shf_putc(c, shl_out);
+   return shf_putc(c, shl_out);
 }
 
 void
Index: bin/ksh/edit.h
===
RCS file: /cvs/src/bin/ksh/edit.h,v
retrieving revision 1.11
diff -u -p -u -r1.11 edit.h
--- bin/ksh/edit.h  26 Jan 2016 17:39:31 -  1.11
+++ bin/ksh/edit.h  16 Jun 2018 22:09:27 -
@@ -37,7 +37,7 @@ extern X_chars edchars;
 /* edit.c */
 intx_getc(void);
 void   x_flush(void);
-void   x_putc(int);
+intx_putc(int);
 void   x_puts(const char *);
 bool   x_mode(bool);
 intpromptlen(const char *, const char **);
Index: bin/ksh/emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -u -r1.84 emacs.c
--- bin/ksh/emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ bin/ksh/emacs.c 16 Jun 2018 22:12:24 -
@@ -21,6 +21,10 @@
 #include 
 #include 
 #include 
+#ifndef SMALL
+# include 
+# include 
+#endif
 
 #include "sh.h"
 #include "edit.h"
@@ -28,6 +32,7 @@
 static Areaaedit;
 #defineAEDIT   &aedit  /* area for kill ring and macro defns */
 
+#undef CTRL
 #defineCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F)/* 
ASCII */
 #defineUNCTRL(x)   ((x) == 0x7F ? '?' : (x) | 0x40)/* 
ASCII */
 
@@ -146,6 +151,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +208,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1011,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1032,23 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   int ret = -1;
+#ifndef SMALL
+   char *str, *term = str_val(global("TERM"));
+   if (term && *term) {
+   setupterm(term, 1, NULL);
+   str = tigetstr("clear");
+   if (str != NULL && str != (char *)-1)
+   ret = tputs(str, 1, x_putc) == ERR;
+   }
+#endif
+   if (ret != 0)
+   x_e_putc('\n');
+   }
+   else if (limit == -1)
x_e_putc('\n');
-   else
+   else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Index: bin/ks

Re: ksh "clear-screen" editing command

2018-06-16 Thread Alexander Hall
Yeah, I've made such stuff, even which preserves the actual position on the 
command line, but I'm not too happy about screwing up the yank buffers...

On June 16, 2018 9:16:50 PM GMT+02:00, Sebastian Benoit  
wrote:
>
>fwiw you can just use
>
>bind -m '^L'=^Uclear'^J^Y'
>
>
>Todd C. Miller(todd.mil...@sudo.ws) on 2018.06.16 06:37:03 -0600:
>> On Tue, 12 Jun 2018 02:35:57 +0200, Alexander Hall wrote:
>> 
>> > The diff below uses system(3) to call /usr/bin/clear, fiddling with
>
>> > *env() to make sure the current TERM value is propagated. The error
>
>> > handling is deliberately sparse, since I don't know what the
>reasonable 
>> > error actions would be.
>> >
>> > It's quite possible there already exists a better function to call 
>> > within the ksh code already, but I was unable to figure out which
>if so.
>> 
>> Using system() within ksh seems wrong.  How about this instead?
>>  
>>  - todd
>> 
>> Index: bin/ksh/emacs.c
>> ===
>> RCS file: /cvs/src/bin/ksh/emacs.c,v
>> retrieving revision 1.84
>> diff -u -p -u -r1.84 emacs.c
>> --- bin/ksh/emacs.c  16 Jan 2018 17:17:18 -  1.84
>> +++ bin/ksh/emacs.c  16 Jun 2018 12:31:59 -
>> @@ -146,6 +146,7 @@ static int   isu8cont(unsigned char);
>>  /* proto's for keybindings */
>>  static int  x_abort(int);
>>  static int  x_beg_hist(int);
>> +static int  x_clear_screen(int);
>>  static int  x_comp_comm(int);
>>  static int  x_comp_file(int);
>>  static int  x_complete(int);
>> @@ -202,6 +203,7 @@ static int   x_debug_info(int);
>>  static const struct x_ftab x_ftab[] = {
>>  { x_abort,  "abort",0 },
>>  { x_beg_hist,   "beginning-of-history", 0 },
>> +{ x_clear_screen,   "clear-screen", 0 },
>>  { x_comp_comm,  "complete-command", 0 },
>>  { x_comp_file,  "complete-file",0 },
>>  { x_complete,   "complete", 0 },
>> @@ -1004,12 +1006,19 @@ x_draw_line(int c)
>>  {
>>  x_redraw(-1);
>>  return KSTD;
>> +}
>>  
>> +static int
>> +x_clear_screen(int c)
>> +{
>> +x_redraw(-2);
>> +return KSTD;
>>  }
>>  
>> -/* Redraw (part of) the line.  If limit is < 0, the everything is
>redrawn
>> - * on a NEW line, otherwise limit is the screen column up to which
>needs
>> - * redrawing.
>> +/* Redraw (part of) the line.
>> + * A non-negative limit is the screen column up to which needs
>> + * redrawing. A limit of -1 redraws on a new line, while a limit
>> + * of -2 (attempts to) clear the screen.
>>   */
>>  static void
>>  x_redraw(int limit)
>> @@ -1018,9 +1027,20 @@ x_redraw(int limit)
>>  char*cp;
>>  
>>  x_adj_ok = 0;
>> -if (limit == -1)
>> +if (limit == -2) {
>> +char *term = str_val(global("TERM"));
>> +int ret = -1;
>> +if (term && *term) {
>> +Source *sold = source;
>> +ret = command("/usr/bin/clear", 0);
>> +source = sold;
>> +}
>> +if (ret != 0)
>> +x_e_putc('\n');
>> +}
>> +else if (limit == -1)
>>  x_e_putc('\n');
>> -else
>> +else if (limit >= 0)
>>  x_e_putc('\r');
>>  x_flush();
>>  if (xbp == xbuf) {
>> Index: bin/ksh/ksh.1
>> ===
>> RCS file: /cvs/src/bin/ksh/ksh.1,v
>> retrieving revision 1.200
>> diff -u -p -u -r1.200 ksh.1
>> --- bin/ksh/ksh.130 May 2018 21:20:52 -  1.200
>> +++ bin/ksh/ksh.116 Jun 2018 12:29:34 -
>> @@ -4690,6 +4690,10 @@ Moves the cursor to the beginning of the
>>  Uppercase the first character in the next
>>  .Ar n
>>  words, leaving the cursor past the end of the last word.
>> +.It clear-screen:
>> +Attempts to clears the screen by invoking
>> +.Xr clear 1
>> +and redraws the prompt and current input line.
>>  .It comment: ^[#
>>  If the current line does not begin with a comment character, one is
>added at
>>  the beginning of the line and the line is entered (as if return had
>been
>> 



Re: ksh "clear-screen" editing command

2018-06-16 Thread Sebastian Benoit


fwiw you can just use

bind -m '^L'=^Uclear'^J^Y'


Todd C. Miller(todd.mil...@sudo.ws) on 2018.06.16 06:37:03 -0600:
> On Tue, 12 Jun 2018 02:35:57 +0200, Alexander Hall wrote:
> 
> > The diff below uses system(3) to call /usr/bin/clear, fiddling with 
> > *env() to make sure the current TERM value is propagated. The error 
> > handling is deliberately sparse, since I don't know what the reasonable 
> > error actions would be.
> >
> > It's quite possible there already exists a better function to call 
> > within the ksh code already, but I was unable to figure out which if so.
> 
> Using system() within ksh seems wrong.  How about this instead?
>  
>  - todd
> 
> Index: bin/ksh/emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.84
> diff -u -p -u -r1.84 emacs.c
> --- bin/ksh/emacs.c   16 Jan 2018 17:17:18 -  1.84
> +++ bin/ksh/emacs.c   16 Jun 2018 12:31:59 -
> @@ -146,6 +146,7 @@ static intisu8cont(unsigned char);
>  /* proto's for keybindings */
>  static int   x_abort(int);
>  static int   x_beg_hist(int);
> +static int   x_clear_screen(int);
>  static int   x_comp_comm(int);
>  static int   x_comp_file(int);
>  static int   x_complete(int);
> @@ -202,6 +203,7 @@ static intx_debug_info(int);
>  static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
> + { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
> @@ -1004,12 +1006,19 @@ x_draw_line(int c)
>  {
>   x_redraw(-1);
>   return KSTD;
> +}
>  
> +static int
> +x_clear_screen(int c)
> +{
> + x_redraw(-2);
> + return KSTD;
>  }
>  
> -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> - * on a NEW line, otherwise limit is the screen column up to which needs
> - * redrawing.
> +/* Redraw (part of) the line.
> + * A non-negative limit is the screen column up to which needs
> + * redrawing. A limit of -1 redraws on a new line, while a limit
> + * of -2 (attempts to) clear the screen.
>   */
>  static void
>  x_redraw(int limit)
> @@ -1018,9 +1027,20 @@ x_redraw(int limit)
>   char*cp;
>  
>   x_adj_ok = 0;
> - if (limit == -1)
> + if (limit == -2) {
> + char *term = str_val(global("TERM"));
> + int ret = -1;
> + if (term && *term) {
> + Source *sold = source;
> + ret = command("/usr/bin/clear", 0);
> + source = sold;
> + }
> + if (ret != 0)
> + x_e_putc('\n');
> + }
> + else if (limit == -1)
>   x_e_putc('\n');
> - else
> + else if (limit >= 0)
>   x_e_putc('\r');
>   x_flush();
>   if (xbp == xbuf) {
> Index: bin/ksh/ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.200
> diff -u -p -u -r1.200 ksh.1
> --- bin/ksh/ksh.1 30 May 2018 21:20:52 -  1.200
> +++ bin/ksh/ksh.1 16 Jun 2018 12:29:34 -
> @@ -4690,6 +4690,10 @@ Moves the cursor to the beginning of the
>  Uppercase the first character in the next
>  .Ar n
>  words, leaving the cursor past the end of the last word.
> +.It clear-screen:
> +Attempts to clears the screen by invoking
> +.Xr clear 1
> +and redraws the prompt and current input line.
>  .It comment: ^[#
>  If the current line does not begin with a comment character, one is added at
>  the beginning of the line and the line is entered (as if return had been
> 



Re: ksh "clear-screen" editing command

2018-06-16 Thread Tomasz Rola
On Sat, Jun 16, 2018 at 07:47:07PM +0200, Mark Kettenis wrote:
> > Date: Sat, 16 Jun 2018 18:15:42 +0200
> > From: Tomasz Rola 
> > 
[...]
> > 
> > Just my cent or two.
> 
> You're missing the point here.  Calling /usr/bin/clear to clear the
> screen from one of your scripts is fone.  Doing so because you press a
> certain key combination while at the shell prompt isn't.

Ok. I stand corrected.

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh "clear-screen" editing command

2018-06-16 Thread Mark Kettenis
> Date: Sat, 16 Jun 2018 18:15:42 +0200
> From: Tomasz Rola 
> 
> On Sat, Jun 16, 2018 at 02:50:40PM +0200, Mark Kettenis wrote:
> [...]
> > 
> > To e honest, I find the whole idea of invoking an external program to
> > clear the screen insane.
> 
> Not necesarilly wanting to quarrel about this, but:
> 
> 1. What would you say to someone who wants to run an old sh script
> which employs exactly this method?
> 
> 2. What am I supposed to use instead when I want to clear the screen
> from the inside of newly written sh script, while (hopefully) staying
> compatible with other systems (some of them old and non-upgradeable)?
> 
> The cost of running external program that only knows one thing and
> knows it well is minimal, even more so on modern platforms. Additional
> benefit is having to change only this one program if such a need
> arises in a future.
> 
> Just my cent or two.

You're missing the point here.  Calling /usr/bin/clear to clear the
screen from one of your scripts is fone.  Doing so because you press a
certain key combination while at the shell prompt isn't.



Re: ksh "clear-screen" editing command

2018-06-16 Thread Tomasz Rola
On Sat, Jun 16, 2018 at 02:50:40PM +0200, Mark Kettenis wrote:
[...]
> 
> To e honest, I find the whole idea of invoking an external program to
> clear the screen insane.

Not necesarilly wanting to quarrel about this, but:

1. What would you say to someone who wants to run an old sh script
which employs exactly this method?

2. What am I supposed to use instead when I want to clear the screen
from the inside of newly written sh script, while (hopefully) staying
compatible with other systems (some of them old and non-upgradeable)?

The cost of running external program that only knows one thing and
knows it well is minimal, even more so on modern platforms. Additional
benefit is having to change only this one program if such a need
arises in a future.

Just my cent or two.

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh "clear-screen" editing command

2018-06-16 Thread Mark Kettenis
> From: "Todd C. Miller" 
> Date: Sat, 16 Jun 2018 06:53:38 -0600
> 
> On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:
> 
> > To be honest, I find the whole idea of invoking an external program to
> > clear the screen insane.
> 
> Do you have an alternative that doesn't require linking ksh with
> curses?

Simply drop the idea of implementing clear-screen?



Re: ksh "clear-screen" editing command

2018-06-16 Thread Todd C. Miller
On Sat, 16 Jun 2018 14:50:40 +0200, Mark Kettenis wrote:

> To be honest, I find the whole idea of invoking an external program to
> clear the screen insane.

Do you have an alternative that doesn't require linking ksh with
curses?

 - todd



Re: ksh "clear-screen" editing command

2018-06-16 Thread Mark Kettenis
> From: "Todd C. Miller" 
> Date: Sat, 16 Jun 2018 06:37:03 -0600
> 
> On Tue, 12 Jun 2018 02:35:57 +0200, Alexander Hall wrote:
> 
> > The diff below uses system(3) to call /usr/bin/clear, fiddling with 
> > *env() to make sure the current TERM value is propagated. The error 
> > handling is deliberately sparse, since I don't know what the reasonable 
> > error actions would be.
> >
> > It's quite possible there already exists a better function to call 
> > within the ksh code already, but I was unable to figure out which if so.
> 
> Using system() within ksh seems wrong.  How about this instead?

To e honest, I find the whole idea of invoking an external program to
clear the screen insane.

> Index: bin/ksh/emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.84
> diff -u -p -u -r1.84 emacs.c
> --- bin/ksh/emacs.c   16 Jan 2018 17:17:18 -  1.84
> +++ bin/ksh/emacs.c   16 Jun 2018 12:31:59 -
> @@ -146,6 +146,7 @@ static intisu8cont(unsigned char);
>  /* proto's for keybindings */
>  static int   x_abort(int);
>  static int   x_beg_hist(int);
> +static int   x_clear_screen(int);
>  static int   x_comp_comm(int);
>  static int   x_comp_file(int);
>  static int   x_complete(int);
> @@ -202,6 +203,7 @@ static intx_debug_info(int);
>  static const struct x_ftab x_ftab[] = {
>   { x_abort,  "abort",0 },
>   { x_beg_hist,   "beginning-of-history", 0 },
> + { x_clear_screen,   "clear-screen", 0 },
>   { x_comp_comm,  "complete-command", 0 },
>   { x_comp_file,  "complete-file",0 },
>   { x_complete,   "complete", 0 },
> @@ -1004,12 +1006,19 @@ x_draw_line(int c)
>  {
>   x_redraw(-1);
>   return KSTD;
> +}
>  
> +static int
> +x_clear_screen(int c)
> +{
> + x_redraw(-2);
> + return KSTD;
>  }
>  
> -/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
> - * on a NEW line, otherwise limit is the screen column up to which needs
> - * redrawing.
> +/* Redraw (part of) the line.
> + * A non-negative limit is the screen column up to which needs
> + * redrawing. A limit of -1 redraws on a new line, while a limit
> + * of -2 (attempts to) clear the screen.
>   */
>  static void
>  x_redraw(int limit)
> @@ -1018,9 +1027,20 @@ x_redraw(int limit)
>   char*cp;
>  
>   x_adj_ok = 0;
> - if (limit == -1)
> + if (limit == -2) {
> + char *term = str_val(global("TERM"));
> + int ret = -1;
> + if (term && *term) {
> + Source *sold = source;
> + ret = command("/usr/bin/clear", 0);
> + source = sold;
> + }
> + if (ret != 0)
> + x_e_putc('\n');
> + }
> + else if (limit == -1)
>   x_e_putc('\n');
> - else
> + else if (limit >= 0)
>   x_e_putc('\r');
>   x_flush();
>   if (xbp == xbuf) {
> Index: bin/ksh/ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.200
> diff -u -p -u -r1.200 ksh.1
> --- bin/ksh/ksh.1 30 May 2018 21:20:52 -  1.200
> +++ bin/ksh/ksh.1 16 Jun 2018 12:29:34 -
> @@ -4690,6 +4690,10 @@ Moves the cursor to the beginning of the
>  Uppercase the first character in the next
>  .Ar n
>  words, leaving the cursor past the end of the last word.
> +.It clear-screen:
> +Attempts to clears the screen by invoking
> +.Xr clear 1
> +and redraws the prompt and current input line.
>  .It comment: ^[#
>  If the current line does not begin with a comment character, one is added at
>  the beginning of the line and the line is entered (as if return had been
> 
> 



Re: ksh "clear-screen" editing command

2018-06-16 Thread Todd C. Miller
On Tue, 12 Jun 2018 02:35:57 +0200, Alexander Hall wrote:

> The diff below uses system(3) to call /usr/bin/clear, fiddling with 
> *env() to make sure the current TERM value is propagated. The error 
> handling is deliberately sparse, since I don't know what the reasonable 
> error actions would be.
>
> It's quite possible there already exists a better function to call 
> within the ksh code already, but I was unable to figure out which if so.

Using system() within ksh seems wrong.  How about this instead?
 
 - todd

Index: bin/ksh/emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -u -r1.84 emacs.c
--- bin/ksh/emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ bin/ksh/emacs.c 16 Jun 2018 12:31:59 -
@@ -146,6 +146,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +203,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1006,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1027,20 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   char *term = str_val(global("TERM"));
+   int ret = -1;
+   if (term && *term) {
+   Source *sold = source;
+   ret = command("/usr/bin/clear", 0);
+   source = sold;
+   }
+   if (ret != 0)
+   x_e_putc('\n');
+   }
+   else if (limit == -1)
x_e_putc('\n');
-   else
+   else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Index: bin/ksh/ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.200
diff -u -p -u -r1.200 ksh.1
--- bin/ksh/ksh.1   30 May 2018 21:20:52 -  1.200
+++ bin/ksh/ksh.1   16 Jun 2018 12:29:34 -
@@ -4690,6 +4690,10 @@ Moves the cursor to the beginning of the
 Uppercase the first character in the next
 .Ar n
 words, leaving the cursor past the end of the last word.
+.It clear-screen:
+Attempts to clears the screen by invoking
+.Xr clear 1
+and redraws the prompt and current input line.
 .It comment: ^[#
 If the current line does not begin with a comment character, one is added at
 the beginning of the line and the line is entered (as if return had been



Re: ksh "clear-screen" editing command

2018-06-11 Thread Alexander Hall
On 06/06/18 13:50, Todd C. Miller wrote:
> On Tue, 05 Jun 2018 16:29:33 -0600, "Theo de Raadt" wrote:
> 
>>> +   clearstr = "\033[H\033[J";
>>
>> I abhor increasing assumptions that the terminal honours that particular
>> ANSI standard.
>>
>> Sorry, but at that point you have to use the libraries and get the
>> information.
> 
> Or just run clear(1).  That may be preferable to linking ksh with
> libcurses.

Yeah, I wasn't keen on adding libcurses...

The diff below uses system(3) to call /usr/bin/clear, fiddling with 
*env() to make sure the current TERM value is propagated. The error 
handling is deliberately sparse, since I don't know what the reasonable 
error actions would be.

It's quite possible there already exists a better function to call 
within the ksh code already, but I was unable to figure out which if so.

Better? Worse?

/Alexander


Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -r1.84 emacs.c
--- emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ emacs.c 12 Jun 2018 00:19:44 -
@@ -146,6 +146,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +203,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1006,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1027,24 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   char *oldterm = getenv("TERM");
+   char *term = str_val(global("TERM"));
+   if (term && *term) {
+   setenv("TERM", term, 1);
+   if (system("/usr/bin/clear") != 0)
+   x_e_putc('\n');
+   if (oldterm)
+   setenv("TERM", oldterm, 1);
+   else
+   unsetenv("TERM");
+   }
+   else
+   x_e_putc('\n');
+   }
+   else if (limit == -1)
x_e_putc('\n');
-   else
+   else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Index: ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.200
diff -u -p -r1.200 ksh.1
--- ksh.1   30 May 2018 21:20:52 -  1.200
+++ ksh.1   12 Jun 2018 00:19:44 -
@@ -4690,6 +4690,10 @@ Moves the cursor to the beginning of the
 Uppercase the first character in the next
 .Ar n
 words, leaving the cursor past the end of the last word.
+.It clear-screen:
+Attempts to clears the screen by invoking
+.Xr clear 1
+and redraws the prompt and current input line.
 .It comment: ^[#
 If the current line does not begin with a comment character, one is added at
 the beginning of the line and the line is entered (as if return had been



Re: ksh "clear-screen" editing command

2018-06-06 Thread Todd C. Miller
On Tue, 05 Jun 2018 16:29:33 -0600, "Theo de Raadt" wrote:

> > +   clearstr = "\033[H\033[J";
>
> I abhor increasing assumptions that the terminal honours that particular
> ANSI standard.
>
> Sorry, but at that point you have to use the libraries and get the
> information.

Or just run clear(1).  That may be preferable to linking ksh with
libcurses.

 - todd



Re: ksh "clear-screen" editing command

2018-06-05 Thread Alexandre Ratchov
On Wed, Jun 06, 2018 at 12:20:50AM +0200, Alexander Hall wrote:
> This adds a "clear-screen" editing command to the emacs editing mode.
> This is the same name as bash and zsh uses, and then I stopped looking.
> 
> The default binding of 'redraw' remains for ^L, for now anyway, so
> you'll need to run
>   $ bind ^L=clear-screen"
> when testing.
> 
> $CLEARSTR can be set to an arbitrary character sequence for clearing the 
> screen, should ^[[H^[[J not fit the bill. For example,
>   $ CLEARSTR=$(clear) 

I don't see the point of the environment variable, for a string
standard since 1974 or 1979, I don't remember. Or is this to make the
command do something else?



Re: ksh "clear-screen" editing command

2018-06-05 Thread Theo de Raadt
> +   clearstr = "\033[H\033[J";

I abhor increasing assumptions that the terminal honours that particular
ANSI standard.

Sorry, but at that point you have to use the libraries and get the
information.



ksh "clear-screen" editing command

2018-06-05 Thread Alexander Hall
This adds a "clear-screen" editing command to the emacs editing mode.
This is the same name as bash and zsh uses, and then I stopped looking.

The default binding of 'redraw' remains for ^L, for now anyway, so
you'll need to run
  $ bind ^L=clear-screen"
when testing.

$CLEARSTR can be set to an arbitrary character sequence for clearing the 
screen, should ^[[H^[[J not fit the bill. For example,
  $ CLEARSTR=$(clear) 
Not sure we want or need this, or what could possibly be a better name
for it.

Thoughts? OK?

/Alexander


Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -r1.84 emacs.c
--- emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ emacs.c 5 Jun 2018 22:03:49 -
@@ -146,6 +146,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int x_abort(int);
 static int x_beg_hist(int);
+static int x_clear_screen(int);
 static int x_comp_comm(int);
 static int x_comp_file(int);
 static int x_complete(int);
@@ -202,6 +203,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
{ x_abort,  "abort",0 },
{ x_beg_hist,   "beginning-of-history", 0 },
+   { x_clear_screen,   "clear-screen", 0 },
{ x_comp_comm,  "complete-command", 0 },
{ x_comp_file,  "complete-file",0 },
{ x_complete,   "complete", 0 },
@@ -1004,12 +1006,19 @@ x_draw_line(int c)
 {
x_redraw(-1);
return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+   x_redraw(-2);
+   return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1027,15 @@ x_redraw(int limit)
char*cp;
 
x_adj_ok = 0;
-   if (limit == -1)
+   if (limit == -2) {
+   char *clearstr = str_val(global("CLEARSTR"));
+   if (clearstr == null)
+   clearstr = "\033[H\033[J";
+   x_e_puts(clearstr);
+   }
+   else if (limit == -1)
x_e_putc('\n');
-   else
+   else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Index: ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.200
diff -u -p -r1.200 ksh.1
--- ksh.1   30 May 2018 21:20:52 -  1.200
+++ ksh.1   5 Jun 2018 22:03:49 -
@@ -1345,6 +1345,8 @@ Also, the
 .Ic cd
 built-in command will display the resulting directory when a match is found
 in any search path other than the empty path.
+.It Ev CLEARSTR
+If set, overrides the default escape sequence to clear the screen.
 .It Ev COLUMNS
 Set to the number of columns on the terminal or window.
 Currently set to the
@@ -4690,6 +4692,12 @@ Moves the cursor to the beginning of the
 Uppercase the first character in the next
 .Ar n
 words, leaving the cursor past the end of the last word.
+.It clear-screen:
+Clears the screen and redraws the prompt and current input line.
+If the
+.Ev CLEARSTR
+parameter is set, it is used to clear the screen.
+Otherwise, a default escape sequence (^[[H^[2J) is used.
 .It comment: ^[#
 If the current line does not begin with a comment character, one is added at
 the beginning of the line and the line is entered (as if return had been