From 016c58cfe49707f92b9a7139dd66e379195ade17 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Tue, 14 May 2024 10:40:25 -0700 Subject: [PATCH] shell-integration: handle 'bash -c command' When the -c option is present, then commands are read from the first non-option argument command string. Our simple implementation assumes that if we see at least the '-c' option, a command string was given, and the shell is always considered to be non-interactive - even if the '-i' (interactive) option is also given. --- src/termio/shell_integration.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/termio/shell_integration.zig b/src/termio/shell_integration.zig index 60fcc0186..fb57595f0 100644 --- a/src/termio/shell_integration.zig +++ b/src/termio/shell_integration.zig @@ -156,8 +156,6 @@ fn setupBash( // Some additional cases we don't yet cover: // - // - If the `c` shell option is set, interactive mode is disabled, so skip - // loading our shell integration. // - If additional file arguments are provided (after a `-` or `--` flag), // and the `i` shell option isn't being explicitly set, we can assume a // non-interactive shell session and skip loading our shell integration. @@ -173,6 +171,12 @@ fn setupBash( if (iter.next()) |rcfile| { try env.put("GHOSTTY_BASH_RCFILE", rcfile); } + } else if (arg.len > 1 and arg[0] == '-' and arg[1] != '-') { + // '-c command' is always non-interactive + if (std.mem.indexOfScalar(u8, arg, 'c') != null) { + return null; + } + try args.append(arg); } else { try args.append(arg); } @@ -297,6 +301,17 @@ test "bash: rcfile" { } } +test "bash: -c command" { + const testing = std.testing; + const alloc = testing.allocator; + + var env = EnvMap.init(alloc); + defer env.deinit(); + + try testing.expect(try setupBash(alloc, "bash -c script.sh", ".", &env) == null); + try testing.expect(try setupBash(alloc, "bash -ic script.sh", ".", &env) == null); +} + test "bash: HISTFILE" { const testing = std.testing; const alloc = testing.allocator;