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 -0000      1.84
+++ emacs.c     12 Jun 2018 00:19:44 -0000
@@ -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 -0000      1.200
+++ ksh.1       12 Jun 2018 00:19:44 -0000
@@ -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

Reply via email to