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 -0000      1.84
+++ bin/ksh/emacs.c     16 Jun 2018 12:31:59 -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,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 -0000      1.200
+++ bin/ksh/ksh.1       16 Jun 2018 12:29:34 -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