The branch main has been updated by pstef:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=755a1be6d015287763b0f336d4e9b8179615f511

commit 755a1be6d015287763b0f336d4e9b8179615f511
Author:     Piotr Pawel Stefaniak <ps...@freebsd.org>
AuthorDate: 2022-08-20 10:15:05 +0000
Commit:     Piotr Pawel Stefaniak <ps...@freebsd.org>
CommitDate: 2022-08-20 11:26:05 +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.
    
    Reviewed by:    jilles
    Differential Revision:  https://reviews.freebsd.org/D35836
---
 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 453260a27e54..8812200279f0 100644
--- a/bin/sh/histedit.c
+++ b/bin/sh/histedit.c
@@ -255,7 +255,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;
@@ -277,25 +276,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 97171d32bff1..55bfd9d483b7 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 f9b464a6da4b..727a7bc6c848 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

Reply via email to