diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2018-06-18 17:03:59 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2018-06-18 17:03:59 +0000 |
commit | 5a7fce5cfbf462cd5ab0d73e2eeab540f9595595 (patch) | |
tree | 4bdf3ce8795284d778c36c62a1b8e896cd369a08 /bin/ksh | |
parent | d801d9541d10c6568476226b0e5a9f593bb1a332 (diff) |
Add clear-screen emacs editing command, currently not bound to a
key by default. The shell will query the terminfo database to
find the escape sequence to clear the screen. OK deraadt@
Diffstat (limited to 'bin/ksh')
-rw-r--r-- | bin/ksh/Makefile | 5 | ||||
-rw-r--r-- | bin/ksh/edit.c | 6 | ||||
-rw-r--r-- | bin/ksh/edit.h | 4 | ||||
-rw-r--r-- | bin/ksh/emacs.c | 37 | ||||
-rw-r--r-- | bin/ksh/ksh.1 | 13 | ||||
-rw-r--r-- | bin/ksh/table.h | 3 | ||||
-rw-r--r-- | bin/ksh/var.c | 21 |
7 files changed, 72 insertions, 17 deletions
diff --git a/bin/ksh/Makefile b/bin/ksh/Makefile index 66419f2311d..621c38bcfd0 100644 --- a/bin/ksh/Makefile +++ b/bin/ksh/Makefile @@ -1,6 +1,9 @@ -# $OpenBSD: Makefile,v 1.38 2018/01/06 16:28:58 millert Exp $ +# $OpenBSD: Makefile,v 1.39 2018/06/18 17:03: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 \ diff --git a/bin/ksh/edit.c b/bin/ksh/edit.c index 42514b9ec76..bbb1b3aeb8f 100644 --- a/bin/ksh/edit.c +++ b/bin/ksh/edit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: edit.c,v 1.65 2018/04/09 17:53:36 tobias Exp $ */ +/* $OpenBSD: edit.c,v 1.66 2018/06/18 17:03:58 millert Exp $ */ /* * Command line editing - common code @@ -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 diff --git a/bin/ksh/edit.h b/bin/ksh/edit.h index 41b411e8935..0b604cd64fb 100644 --- a/bin/ksh/edit.h +++ b/bin/ksh/edit.h @@ -1,4 +1,4 @@ -/* $OpenBSD: edit.h,v 1.11 2016/01/26 17:39:31 mmcc Exp $ */ +/* $OpenBSD: edit.h,v 1.12 2018/06/18 17:03:58 millert Exp $ */ /* NAME: * edit.h - globals for edit modes @@ -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 **); diff --git a/bin/ksh/emacs.c b/bin/ksh/emacs.c index b88219d7440..dd5875c1a10 100644 --- a/bin/ksh/emacs.c +++ b/bin/ksh/emacs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: emacs.c,v 1.84 2018/01/16 17:17:18 jca Exp $ */ +/* $OpenBSD: emacs.c,v 1.85 2018/06/18 17:03:58 millert Exp $ */ /* * Emacs-like command line editing and history @@ -21,6 +21,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifndef SMALL +# include <term.h> +# include <curses.h> +#endif #include "sh.h" #include "edit.h" @@ -28,6 +32,7 @@ static Area aedit; #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 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) x_e_putc('\n'); - else + else if (limit >= 0) x_e_putc('\r'); x_flush(); if (xbp == xbuf) { diff --git a/bin/ksh/ksh.1 b/bin/ksh/ksh.1 index 3d50e753d6d..5dcefe1d456 100644 --- a/bin/ksh/ksh.1 +++ b/bin/ksh/ksh.1 @@ -1,8 +1,8 @@ -.\" $OpenBSD: ksh.1,v 1.200 2018/05/30 21:20:52 benno Exp $ +.\" $OpenBSD: ksh.1,v 1.201 2018/06/18 17:03:58 millert Exp $ .\" .\" Public Domain .\" -.Dd $Mdocdate: May 30 2018 $ +.Dd $Mdocdate: June 18 2018 $ .Dt KSH 1 .Os .Sh NAME @@ -1717,6 +1717,10 @@ loops to store the value that is read from standard input. The number of seconds since the shell started or, if the parameter has been assigned an integer value, the number of seconds since the assignment plus the value that was assigned. +.It Ev TERM +The user's terminal type. +If set, it will be used to determine the escape sequence used to +clear the screen. .It Ev TMOUT If set to a positive integer in an interactive shell, it specifies the maximum number of seconds the shell will wait for input after printing the primary @@ -4690,6 +4694,11 @@ Moves the cursor to the beginning of the edited input line. 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 if the +.Ev TERM +parameter is set and the terminal supports clearing the screen, then +reprints the prompt string and the 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 diff --git a/bin/ksh/table.h b/bin/ksh/table.h index 04df8de2666..63762502259 100644 --- a/bin/ksh/table.h +++ b/bin/ksh/table.h @@ -1,4 +1,4 @@ -/* $OpenBSD: table.h,v 1.14 2018/04/09 17:53:36 tobias Exp $ */ +/* $OpenBSD: table.h,v 1.15 2018/06/18 17:03:58 millert Exp $ */ /* $From: table.h,v 1.3 1994/05/31 13:34:34 michael Exp $ */ @@ -170,6 +170,7 @@ extern const struct builtin shbuiltins [], kshbuiltins []; #define V_TMOUT 16 #define V_TMPDIR 17 #define V_LINENO 18 +#define V_TERM 19 /* values for set_prompt() */ #define PS1 0 /* command */ diff --git a/bin/ksh/var.c b/bin/ksh/var.c index e3d8589aa96..846200a469c 100644 --- a/bin/ksh/var.c +++ b/bin/ksh/var.c @@ -1,4 +1,4 @@ -/* $OpenBSD: var.c,v 1.68 2018/04/13 18:18:36 cheloha Exp $ */ +/* $OpenBSD: var.c,v 1.69 2018/06/18 17:03:58 millert Exp $ */ #include <sys/stat.h> #include <sys/time.h> @@ -11,6 +11,10 @@ #include <string.h> #include <time.h> #include <unistd.h> +#ifndef SMALL +# include <term.h> +# include <curses.h> +#endif #include "sh.h" @@ -111,12 +115,13 @@ initvar(void) { "SECONDS", V_SECONDS }, { "TMOUT", V_TMOUT }, { "LINENO", V_LINENO }, + { "TERM", V_TERM }, { NULL, 0 } }; int i; struct tbl *tp; - ktinit(&specials, APERM, 32); /* must be 2^n (currently 17 specials) */ + ktinit(&specials, APERM, 32); /* must be 2^n (currently 19 specials) */ for (i = 0; names[i].name; i++) { tp = ktenter(&specials, names[i].name, hash(names[i].name)); tp->flag = DEFINED|ISSET; @@ -1057,6 +1062,18 @@ setspec(struct tbl *vp) user_lineno = (unsigned int) intval(vp) - current_lineno - 1; vp->flag |= SPECIAL; break; + case V_TERM: +#ifndef SMALL + { + int ret; + + vp->flag &= ~SPECIAL; + if (setupterm(str_val(vp), STDOUT_FILENO, &ret) == ERR) + del_curterm(cur_term); + vp->flag |= SPECIAL; + } +#endif + break; } } |