diff mbox series

[2/6] fc -s: don't loop forever when executing the latest entry

Message ID 7166822efa32d227a28d86bd1fb505f09bb7cf0c.1675798292.git.nabijaczleweli@nabijaczleweli.xyz (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series [1/6] fc -s: refuse multiple events instead of ignoring | expand

Commit Message

Ahelenia ZiemiaƄska Feb. 7, 2023, 7:33 p.m. UTC
Quoting CVS comment:
  Ensure [...] that we break out of the loop after executing it.
  With the previous code, because the re-executed command was added to
  the history, it would think that it still had to execute it,
  leading to an infinite loop.

  The last thing which differs from ksh is that we get the "fc -s"
  command into the history, but this is actually rather a feature
  in my humble opinion.

POSIX disagrees:
  [W]hen the -l option is not specified, the resulting lines shall be
  entered at the end of the history list and then re-executed by sh.
  The fc command that caused the editing shall not be entered into the
  history list.

But this is a separate issue, since even without -s dash puts fc into
the history (i.e. id; ls; fc 1 2; fc -l; yields 1=id, 2=ls, 3=fc -l).

Test cases:
  $ id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  $ fc -s 1
  id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  $ fc -l
      1 id
      2 fc -s 1
      3 id
-- >8 --
  $ ls
  autogen.sh  ChangeLog  ...
  $ id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  $ fc -s -1
  id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
-- >8 --
  $ ls
  autogen.sh  ChangeLog  ...
  $ id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  $ fc -s -2
  ls
  autogen.sh  ChangeLog  ...
-- >8 --
  $ ls
  autogen.sh  ChangeLog  ...
  $ id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  $ fc -s -2
  ls
  autogen.sh  ChangeLog  ...
  $ id 1
  uid=1(daemon) gid=1(daemon) groups=1(daemon)
  $ id 2
  uid=2(bin) gid=2(bin) groups=2(bin)
  $ fc -s 3
  fc -s -2
  id 1
  uid=1(daemon) gid=1(daemon) groups=1(daemon)
  $ fc -l
      1 ls
      2 id
      3 fc -s -2
      4 ls
      5 id 1
      6 id 2
      7 fc -s 3
      8 id 1
-- >8 --
  $ id 1
  uid=1(daemon) gid=1(daemon) groups=1(daemon)
  $ fc -s 2
  fc -s 2
  fc -s 2
  fc -s 2
  fc -s 2
  src/dash: 1: fc: called recursively too many times
and I'm happy to call this "behaving exactly as I expected when I was
typing it in", so removing the XXX.

Adapted-from: NetBSD src bin/sh/histedit.c rev 1.38 by aymeric@
---
 src/histedit.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/src/histedit.c b/src/histedit.c
index fc87283..28956ec 100644
--- a/src/histedit.c
+++ b/src/histedit.c
@@ -382,12 +382,10 @@  histcmd(int argc, char **argv)
 
 				evalstring(s, 0);
 				if (displayhist && hist) {
-					/*
-					 *  XXX what about recursive and
-					 *  relative histnums.
-					 */
 					history(hist, &he, H_ENTER, s);
 				}
+
+				break;
 			} else
 				fputs(s, efp);
 		}