git: 6480563d0678 - stable/13 - sh: accept fc options grouped behind one '-'

From: Piotr Pawel Stefaniak <pstef_at_FreeBSD.org>
Date: Wed, 24 Aug 2022 18:22:25 UTC
The branch stable/13 has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=6480563d0678662301b04c010d4f96f05283fc12

commit 6480563d0678662301b04c010d4f96f05283fc12
Author:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
AuthorDate: 2022-08-20 10:15:05 +0000
Commit:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
CommitDate: 2022-08-24 18:21:23 +0000

    sh: accept fc options grouped behind one '-'
    
    As per Utility Syntax Guidelines, accept both forms: -l -n and -ln.
    
    To do that, anticipate the source string for the next option that will
    be parsed by nextopt(). It's not always *argptr, sometimes it is
    nextopt_optptr.
    
    To simplify the check for not_fcnumber, slightly modify nextopt() to
    always nullify nextopt_optptr in cases where it would have been set
    to point to a NUL character.
    
    (cherry picked from commit 755a1be6d015287763b0f336d4e9b8179615f511)
---
 bin/sh/histedit.c                  | 43 ++++++++++++++++++++------------------
 bin/sh/options.c                   |  5 ++++-
 bin/sh/tests/builtins/Makefile     |  1 +
 bin/sh/tests/builtins/fc3.0        |  9 ++++++++
 bin/sh/tests/builtins/fc3.0.stderr |  1 +
 bin/sh/tests/builtins/fc3.0.stdout |  3 +++
 6 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c
index 2001b80bd8ed..b9af20e6e9ab 100644
--- a/bin/sh/histedit.c
+++ b/bin/sh/histedit.c
@@ -256,7 +256,6 @@ setterm(const char *term)
 int
 histcmd(int argc, char **argv __unused)
 {
-	int ch;
 	const char *editor = NULL;
 	HistEvent he;
 	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
@@ -278,25 +277,29 @@ histcmd(int argc, char **argv __unused)
 	if (argc == 1)
 		error("missing history argument");
 
-	while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
-		switch ((char)ch) {
-		case 'e':
-			editor = shoptarg;
-			break;
-		case 'l':
-			lflg = 1;
-			break;
-		case 'n':
-			nflg = 1;
-			break;
-		case 'r':
-			rflg = 1;
-			break;
-		case 's':
-			sflg = 1;
-			break;
-		}
-
+	while (not_fcnumber(*argptr))
+		do {
+			switch (nextopt("e:lnrs")) {
+			case 'e':
+				editor = shoptarg;
+				break;
+			case 'l':
+				lflg = 1;
+				break;
+			case 'n':
+				nflg = 1;
+				break;
+			case 'r':
+				rflg = 1;
+				break;
+			case 's':
+				sflg = 1;
+				break;
+			case '\0':
+				goto operands;
+			}
+		} while (nextopt_optptr != NULL);
+operands:
 	savehandler = handler;
 	/*
 	 * If executing...
diff --git a/bin/sh/options.c b/bin/sh/options.c
index 73d79ca9a74f..32c504b3fc75 100644
--- a/bin/sh/options.c
+++ b/bin/sh/options.c
@@ -589,6 +589,9 @@ nextopt(const char *optstring)
 		shoptarg = p;
 		p = NULL;
 	}
-	nextopt_optptr = p;
+	if (p != NULL && *p != '\0')
+		nextopt_optptr = p;
+	else
+		nextopt_optptr = NULL;
 	return c;
 }
diff --git a/bin/sh/tests/builtins/Makefile b/bin/sh/tests/builtins/Makefile
index 197d735920e6..7f8615d5cc1b 100644
--- a/bin/sh/tests/builtins/Makefile
+++ b/bin/sh/tests/builtins/Makefile
@@ -94,6 +94,7 @@ ${PACKAGE}FILES+=		exit3.0
 ${PACKAGE}FILES+=		export1.0
 ${PACKAGE}FILES+=		fc1.0
 ${PACKAGE}FILES+=		fc2.0
+${PACKAGE}FILES+=		fc3.0 fc3.0.stdout fc3.0.stderr
 ${PACKAGE}FILES+=		for1.0
 ${PACKAGE}FILES+=		for2.0
 ${PACKAGE}FILES+=		for3.0
diff --git a/bin/sh/tests/builtins/fc3.0 b/bin/sh/tests/builtins/fc3.0
new file mode 100644
index 000000000000..daa615bcc3ca
--- /dev/null
+++ b/bin/sh/tests/builtins/fc3.0
@@ -0,0 +1,9 @@
+export PS1='_ ' # cannot predict whether ran by root or not
+
+echo ': command1
+: command2
+: command3
+: command4
+fc -l -n -1
+fc -ln 2 3
+' | ENV= HISTFILE=/dev/null ${SH} +m -i
diff --git a/bin/sh/tests/builtins/fc3.0.stderr b/bin/sh/tests/builtins/fc3.0.stderr
new file mode 100644
index 000000000000..e05147fb8209
--- /dev/null
+++ b/bin/sh/tests/builtins/fc3.0.stderr
@@ -0,0 +1 @@
+_ _ _ _ _ _ _ _ 
diff --git a/bin/sh/tests/builtins/fc3.0.stdout b/bin/sh/tests/builtins/fc3.0.stdout
new file mode 100644
index 000000000000..8c23c913635d
--- /dev/null
+++ b/bin/sh/tests/builtins/fc3.0.stdout
@@ -0,0 +1,3 @@
+: command4
+: command2
+: command3