From a2018d7b20b557eeb2e0e59e39e9ad3c0c16e44a Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Thu, 23 Jan 2025 10:34:27 -0500 Subject: [PATCH] bash: handle additional command arguments A '-' or '--' argument signals the end of bash's own options. All remaining arguments are treated as filenames and arguments. We shouldn't perform any additional argument processing once we see this signal. We could also assume a non-interactive shell session in this case unless the '-i' (interactive) shell option has been explicitly specified, but let's wait on that until we know that doing so would solve a real user problem (and avoid any false negatives). --- src/termio/shell_integration.zig | 40 +++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/termio/shell_integration.zig b/src/termio/shell_integration.zig index 915d5be9e..423e2f518 100644 --- a/src/termio/shell_integration.zig +++ b/src/termio/shell_integration.zig @@ -203,8 +203,6 @@ test "setup features" { /// our script's responsibility (along with disabling POSIX /// mode). /// -/// This approach requires bash version 4 or later. -/// /// This returns a new (allocated) shell command string that /// enables the integration or null if integration failed. fn setupBash( @@ -246,12 +244,6 @@ fn setupBash( // Unsupported options: // -c -c is always non-interactive // --posix POSIX mode (a la /bin/sh) - // - // Some additional cases we don't yet cover: - // - // - 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. var rcfile: ?[]const u8 = null; while (iter.next()) |arg| { if (std.mem.eql(u8, arg, "--posix")) { @@ -268,6 +260,14 @@ fn setupBash( return null; } try args.append(arg); + } else if (std.mem.eql(u8, arg, "-") or std.mem.eql(u8, arg, "--")) { + // All remaining arguments should be passed directly to the shell + // command. We shouldn't perform any further option processing. + try args.append(arg); + while (iter.next()) |remaining_arg| { + try args.append(remaining_arg); + } + break; } else { try args.append(arg); } @@ -430,6 +430,30 @@ test "bash: HISTFILE" { } } +test "bash: additional arguments" { + const testing = std.testing; + const alloc = testing.allocator; + + var env = EnvMap.init(alloc); + defer env.deinit(); + + // "-" argument separator + { + const command = try setupBash(alloc, "bash - --arg file1 file2", ".", &env); + defer if (command) |c| alloc.free(c); + + try testing.expectEqualStrings("bash --posix - --arg file1 file2", command.?); + } + + // "--" argument separator + { + const command = try setupBash(alloc, "bash -- --arg file1 file2", ".", &env); + defer if (command) |c| alloc.free(c); + + try testing.expectEqualStrings("bash --posix -- --arg file1 file2", command.?); + } +} + /// Setup automatic shell integration for shells that include /// their modules from paths in `XDG_DATA_DIRS` env variable. ///