From be598be6de3a581da0f555d7c354fce84997da3a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 Jun 2024 14:50:04 -0700 Subject: [PATCH] config: xdg-terminal-exec parsing should ignore the initial "-e" --- src/config/Config.zig | 48 +++++++++++++++++++++---------------------- src/os/xdg.zig | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 83931fb52..cf38788cb 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1665,34 +1665,34 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void { // styling, etc. based on the command. // // See: https://github.com/Vladimir-csp/xdg-terminal-exec - if (comptime builtin.os.tag == .linux) xdg: { - if (!std.mem.eql( - u8, - std.fs.path.basename(std.mem.sliceTo(std.os.argv[0], 0)), - "xdg-terminal-exec", - )) break :xdg; + if (comptime builtin.os.tag == .linux) { + if (internal_os.xdg.parseTerminalExec(std.os.argv)) |args| { + const arena_alloc = self._arena.?.allocator(); - const arena_alloc = self._arena.?.allocator(); + // First, we add an artificial "-e" so that if we + // replay the inputs to rebuild the config (i.e. if + // a theme is set) then we will get the same behavior. + try self._replay_steps.append(arena_alloc, .{ .arg = "-e" }); - // First, we add an artificial "-e" so that if we - // replay the inputs to rebuild the config (i.e. if - // a theme is set) then we will get the same behavior. - try self._replay_steps.append(arena_alloc, .{ .arg = "-e" }); + // Next, take all remaining args and use that to build up + // a command to execute. + var command = std.ArrayList(u8).init(arena_alloc); + errdefer command.deinit(); + for (args) |arg_raw| { + const arg = std.mem.sliceTo(arg_raw, 0); + try self._replay_steps.append( + arena_alloc, + .{ .arg = try arena_alloc.dupe(u8, arg) }, + ); - // Next, take all remaining args and use that to build up - // a command to execute. - var command = std.ArrayList(u8).init(arena_alloc); - errdefer command.deinit(); - for (std.os.argv[1..]) |arg_raw| { - const arg = std.mem.sliceTo(arg_raw, 0); - try self._replay_steps.append(arena_alloc, .{ .arg = try arena_alloc.dupe(u8, arg) }); - try command.appendSlice(arg); - try command.append(' '); + try command.appendSlice(arg); + try command.append(' '); + } + + self.@"_xdg-terminal-exec" = true; + self.command = command.items[0 .. command.items.len - 1]; + return; } - - self.@"_xdg-terminal-exec" = true; - self.command = command.items[0 .. command.items.len - 1]; - return; } // Parse the config from the CLI args diff --git a/src/os/xdg.zig b/src/os/xdg.zig index 118f10df0..3f35324ff 100644 --- a/src/os/xdg.zig +++ b/src/os/xdg.zig @@ -78,6 +78,23 @@ pub fn config(alloc: Allocator, opts: Options) ![]u8 { return error.NoHomeDir; } +/// Parses the xdg-terminal-exec specification. This expects argv[0] to +/// be "xdg-terminal-exec". +pub fn parseTerminalExec(argv: []const [*:0]const u8) ?[]const [*:0]const u8 { + if (!std.mem.eql( + u8, + std.fs.path.basename(std.mem.sliceTo(argv[0], 0)), + "xdg-terminal-exec", + )) return null; + + // We expect at least one argument + if (argv.len < 2) return &.{}; + + // If the first argument is "-e" we skip it. + const start: usize = if (std.mem.eql(u8, std.mem.sliceTo(argv[1], 0), "-e")) 2 else 1; + return argv[start..]; +} + test { const testing = std.testing; const alloc = testing.allocator; @@ -88,3 +105,28 @@ test { try testing.expect(value.len > 0); } } + +test parseTerminalExec { + const testing = std.testing; + + { + const actual = parseTerminalExec(&.{ "a", "b", "c" }); + try testing.expect(actual == null); + } + { + const actual = parseTerminalExec(&.{"xdg-terminal-exec"}).?; + try testing.expectEqualSlices([*:0]const u8, actual, &.{}); + } + { + const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "a", "b", "c" }).?; + try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "b", "c" }); + } + { + const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "-e", "a", "b", "c" }).?; + try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "b", "c" }); + } + { + const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "a", "-e", "b", "c" }).?; + try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "-e", "b", "c" }); + } +}