Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Now libedit supports embedded mode switch sequence, i...



details:   https://anonhg.NetBSD.org/src/rev/1ea2e55d09fc
branches:  trunk
changeset: 825060:1ea2e55d09fc
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Jun 28 13:46:06 2017 +0000

description:
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).

diffstat:

 bin/sh/histedit.c   |   28 ++++++++++-
 bin/sh/myhistedit.h |    3 +-
 bin/sh/sh.1         |  122 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 bin/sh/var.c        |    7 ++-
 bin/sh/var.h        |    5 +-
 5 files changed, 154 insertions(+), 11 deletions(-)

diffs (283 lines):

diff -r b9fbdf84451c -r 1ea2e55d09fc bin/sh/histedit.c
--- a/bin/sh/histedit.c Wed Jun 28 13:22:28 2017 +0000
+++ b/bin/sh/histedit.c Wed Jun 28 13:46:06 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: histedit.c,v 1.51 2017/06/27 23:27:03 christos Exp $   */
+/*     $NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $        */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: histedit.c,v 1.51 2017/06/27 23:27:03 christos Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -134,7 +134,8 @@
                        if (el != NULL) {
                                if (hist)
                                        el_set(el, EL_HIST, history, hist);
-                               el_set(el, EL_PROMPT_ESC, getprompt, L'\1');
+
+                               set_prompt_lit(lookupvar("PSlit"));
                                el_set(el, EL_SIGNAL, 1);
                                el_set(el, EL_ALIAS_TEXT, alias_text, NULL);
                                el_set(el, EL_ADDFN, "rl-complete",
@@ -175,6 +176,27 @@
 }
 
 void
+set_prompt_lit(const char *lit_ch)
+{
+       wchar_t wc;
+
+       if (!(iflag && editing && el))
+               return;
+
+       if (lit_ch == NULL) {
+               el_set(el, EL_PROMPT, getprompt);
+               return;
+       }
+
+       mbtowc(&wc, NULL, 1);           /* state init */
+
+       if (mbtowc(&wc, lit_ch, strlen(lit_ch)) <= 0)
+               el_set(el, EL_PROMPT, getprompt);
+       else
+               el_set(el, EL_PROMPT_ESC, getprompt, (int)wc);
+}
+
+void
 set_editrc(const char *fname)
 {
        if (iflag && editing && el)
diff -r b9fbdf84451c -r 1ea2e55d09fc bin/sh/myhistedit.h
--- a/bin/sh/myhistedit.h       Wed Jun 28 13:22:28 2017 +0000
+++ b/bin/sh/myhistedit.h       Wed Jun 28 13:46:06 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: myhistedit.h,v 1.12 2017/06/27 02:22:08 kre Exp $      */
+/*     $NetBSD: myhistedit.h,v 1.13 2017/06/28 13:46:06 kre Exp $      */
 
 /*-
  * Copyright (c) 1993
@@ -42,6 +42,7 @@
 void setterm(const char *);
 int inputrc(int, char **);
 void set_editrc(const char *);
+void set_prompt_lit(const char *);
 int not_fcnumber(char *);
 int str_to_event(const char *, int);
 
diff -r b9fbdf84451c -r 1ea2e55d09fc bin/sh/sh.1
--- a/bin/sh/sh.1       Wed Jun 28 13:22:28 2017 +0000
+++ b/bin/sh/sh.1       Wed Jun 28 13:46:06 2017 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sh.1,v 1.155 2017/06/27 12:43:44 kre Exp $
+.\"    $NetBSD: sh.1,v 1.156 2017/06/28 13:46:06 kre Exp $
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
 .\"
@@ -31,7 +31,7 @@
 .\"
 .\"    @(#)sh.1        8.6 (Berkeley) 5/4/95
 .\"
-.Dd June 17, 2017
+.Dd June 28, 2017
 .Dt SH 1
 .ds flags abCEeFfhnuvxIimpqV
 .Os
@@ -2756,9 +2756,109 @@
 .Xr editline 7
 in the file named by the
 .Ev EDITRC
-parameter, or using
+parameter,
+or a file used with the
+.Ic inputrc
+built-in command,
+or using
 .Xr editline 7 Ap s
 configuration command line.
+.Pp
+When command line editing is enabled, the
+.Xr editline 7
+functions control printing of the
+.Ev PS1
+and
+.Ev PS2
+prompts when required.
+As, in this mode, the command line editor needs to
+keep track of what characters are in what position on
+the command line, care needs to be taken when setting
+the prompts.
+Normal printing characters are handled automatically,
+however mode setting sequences, which do not actually display
+on the terminal, need to be identified to
+.Xr editline 7 .
+This is done, when needed, by choosing a character that
+is not needed anywhere in the prompt, including in the mode
+setting sequences, any single character is acceptable,
+and assigning it to the shell parameter
+.Ev PSlit .
+Then that character should be used, in pairs, in the
+prompt string.
+Between each pair of
+.Ev PSlit
+characters are mode setting sequences, which affect the printing
+attributes of the following (normal) characters of the prompt,
+but do not themselves appear visibly, nor change the terminal's
+cursor position.
+.Pp
+Each such sequence, that is
+.Ev PSlit
+character, mode setting character sequence, and another
+.Ev PSlit
+character, must currently be followed by at least one following
+normal prompt character, or it will be ignored.
+That is, a
+.Ev PSlit
+character cannot be the final character of
+.Ev PS1
+or
+.Ev PS2 ,
+nor may two
+.Ev PSlit
+delimited sequences appear adjacent to each other.
+Each sequence can contain as many mode altering sequences as are
+required however.
+Only the first character from
+.Ev PSlit
+will be used.
+When set
+.Ev PSlit
+should usually be set to a string containing just one
+character, then it can simply be embedded in
+.Ev PS1
+(or
+.Ev PS2 )
+as in
+.Dl PS1="${PSlit}mset${PSlit}XYZ${PSlit}mclr${PSlit}ABC"
+The prompt visible will be
+.Dq XYZABC
+with the
+.Dq XYZ
+part shown according as defined by the mode setting characters
+.Dq mset ,
+and then cleared again by
+.Dq mclr .
+See
+.Xr tput 1
+for one method to generate appropriate mode sequences.
+Note that both parts, XYZ and ABC, must each contain at least one
+character.
+.Pp
+If
+.Ev PSlit
+is unset, which is its initial state, or set to a null string,
+no literal character will be defined,
+and all characters of the prompt strings will be assumed
+to be visible characters (which includes spaces etc.)
+To allow smooth use of prompts, without needing redefinition, when
+.Xr editline 7
+is disabled, the character chosen should be one which will be
+ignored by the terminal if received, as when
+.Xr edlitline 7
+is not in use, the prompt strings are simply written to the terminal.
+For example, setting:
+.Bd -compact -literal -offset left
+  PSlit="$(printf\ '\e1')"
+  PS1="${PSlit}$(tput\ bold\ blink)${PSlit}\e$${PSlit}$(tput\ sgr0)${PSlit}\ "
+.Ed
+will arrange for the primary prompt to be a bold blinking dollar sign,
+if supported by the current terminal, followed by an (ordinary) space,
+and, as the SOH (Control-A) character ('\e1') will not normally affect
+a terminal, this same prompt will usually work with
+.Xr editline 7
+enabled or disabled.
 .Sh ENVIRONMENT
 .Bl -tag -width MAILCHECK
 .It Ev CDPATH
@@ -2868,6 +2968,22 @@
 Output before each line when execution trace (set -x) is enabled,
 defaults to
 .Dq + \  .
+.It Ev PSlit
+Defines the character which may be embedded in pairs, in
+.Ev PS1
+or
+.Ev PS2
+to indicate to
+.Xr editline 7
+that the characters between each pair of occurrences of the
+.Ev PSlit
+character will not appear in the visible prompt, and will not
+cause the terminal's cursor to change position, but rather set terminal
+attributes for the following prompt character(s) at least one of
+which must be present.
+See
+.Sx Command Line Editing
+above for more information.
 .It Ev TERM
 The default terminal setting for the shell.
 This is inherited by
diff -r b9fbdf84451c -r 1ea2e55d09fc bin/sh/var.c
--- a/bin/sh/var.c      Wed Jun 28 13:22:28 2017 +0000
+++ b/bin/sh/var.c      Wed Jun 28 13:46:06 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.61 2017/06/27 02:22:08 kre Exp $     */
+/*     $NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.61 2017/06/27 02:22:08 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -96,6 +96,7 @@
 struct var vhistsize;
 struct var vterm;
 struct var editrc;
+struct var ps_lit;
 #endif
 struct var vifs;
 struct var vmail;
@@ -142,6 +143,8 @@
           { .set_func= setterm } },
        { &editrc,      VSTRFIXED|VTEXTFIXED|VUNSET,    "EDITRC=",
           { .set_func= set_editrc } },
+       { &ps_lit,      VSTRFIXED|VTEXTFIXED|VUNSET,    "PSlit=",
+          { .set_func= set_prompt_lit } },
 #endif
        { &voptind,     VSTRFIXED|VTEXTFIXED|VNOFUNC,   "OPTIND=1",
           { .set_func= getoptsreset } },
diff -r b9fbdf84451c -r 1ea2e55d09fc bin/sh/var.h
--- a/bin/sh/var.h      Wed Jun 28 13:22:28 2017 +0000
+++ b/bin/sh/var.h      Wed Jun 28 13:46:06 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.h,v 1.33 2017/06/27 02:22:08 kre Exp $     */
+/*     $NetBSD: var.h,v 1.34 2017/06/28 13:46:06 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -89,11 +89,12 @@
 extern struct var vps2;
 extern struct var vps4;
 extern struct var line_num;
+#ifndef SMALL
 extern struct var editrc;
-#ifndef SMALL
 extern struct var vterm;
 extern struct var vtermcap;
 extern struct var vhistsize;
+extern struct var ps_lit;
 #endif
 
 extern int line_number;



Home | Main Index | Thread Index | Old Index