From 5b857bb55fae0a220f54b140bc1b46c3efb991bb Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 05:01:46 -0300 Subject: core: the session's facts are the core's state, read once at its start (the reorganisation's 12b, the design round's step 3; the user's ruling P16 and the lean put with P17: a far client's `~`, default shell and "is this shell installed" are the session's): its home (`~`) and the shell its terminals run when no `Shell` is set ($SHELL if executable, else /bin/sh) are fields beside `session_dir`, `session_home` and `session_shell` (working names, the user's to rule), each read once with `session_dir` by Pardes.readSessionFacts (tape.value(.env): a replay of the boot reads the session's answers), at the session's start and after a load of a dump made before they were dumped, and carried by the dump; `sessionDir` no longer fills itself at first use (it was filled at the start already), and a client's step reads the session's, never its own machine's: Pardes.expandHome (`~` the session's home; `~user` a passwd read, taped, as before) at Save, a tag's name, Theme's path, Get, Incl, a Look of `~/x`, a 9P name and pager, Dump's path, sam's B, e, r and w (sam_edit.World.home) and DumpDir's check (config.Runtime.apply takes the home); bare `Shell`, the shell a terminal runs, the settings report and its 9P twins read `session_shell`; "is this shell installed" (Shell , Term , a 9P exec) is a question to the session's disk, tape.site, so far away it waits a round trip instead of answering from the client's machine and rolling back; deleted: the value reads at config.defaultShell (now the process's read, made at the start alone) and at Core.sessionDir, and the tape's `cwd` tag (the directory is read under `env` at the start); fs.expandHome stays for this process's own home, the launcher's (runtime/main.zig) and the state file's directory and a Restore's name (the session's side: a client's Restore waits, its replay reads the tape); the tape's shape changes (`cwd` gone, `shell` a site), so the semantics word moves (the tape's shape now hashes the sites too: it did not see a site gone or added, and this change alone left it as it was), adacca2db2b39264 -> 517cd1c276996a3c: an old build and a new refuse each other's clients, in words; a test at the end of core/replica.zig (a joined client's own `Look ~/x`, bare `Shell` and Tutor, a word run in the session's directory, record no `env` read; the client's shell and directory are the session's), shown failing on the change before (`Look ~/x` read the environment); not handled: a Restore of a dump carries that dump's facts (as `session_dir` did); tui core-test 833/976 (143 skipped), shell-test 1011/1014, pane-test 183/184, behaviour-test 275/275 and --replay 275/275, process-test 2 of 144 failed (lspcomplete, lspcompletemoved, the base's; hscroll flaked once), replicas --clients 5 diverged 0 unequal 0 deltas wrong 0 quiet differ 0 at 30/80/207 ms, stopped 89/73/108, gui unit-test 1609/1613 (its perf check's BaselineHarnessMismatch the base's), daemon.py plain, --far, --games, --window-clients pass; gate-w seam-b-3 against tree1: 77/77 frames byte-identical in Debug, its unit-test 1609/1613, perfab corpus2x +0.095% (pictures identical in all 10 runs), perfops 97 counts within 0.1% (largest +0.0198%, body-appends on longline) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 60 +++++++++++++++++++++++++++++++++++++++++++---------- core/Output.zig | 2 +- core/builtins.zig | 10 ++++----- core/config.zig | 43 ++++++++++++++++++-------------------- core/dump.zig | 4 +++- core/edit_cmd.zig | 2 +- core/exec.zig | 12 +++++------ core/fs.zig | 18 ++++++++++------ core/look.zig | 2 +- core/ninep/ctl.zig | 8 +++---- core/ninep/pane.zig | 2 +- core/ninep/pty.zig | 2 +- core/replica.zig | 28 +++++++++++++++++++++++++ core/sam_edit.zig | 6 ++++-- core/tagline.zig | 2 +- core/tape.zig | 8 ++++--- 16 files changed, 142 insertions(+), 67 deletions(-) diff --git a/core/Core.zig b/core/Core.zig index 2036fd77..398d5973 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -259,6 +259,14 @@ session_dir: [limits.host_path_cap]u8 = undefined, /// slot: the effect then carries none, and perform gives the host this. spawn_dirs: [MAX_PANES]?[]u8 = @splat(null), session_dir_len: ?u16 = null, +/// The session's home (`~`; null: it had none) and the shell its terminals +/// run when no `Shell` is set ($SHELL if it is executable, else /bin/sh), +/// read once at its start as `session_dir` is (initSession) and dumped with +/// it: a client's step reads the session's, never its own machine's. +session_home: [limits.host_path_cap]u8 = undefined, +session_home_len: ?u16 = null, +session_shell: [limits.host_path_cap]u8 = undefined, +session_shell_len: u16 = 0, settings: config.Runtime = .{ .font = .{ .tagline_percent = config.gui_tagline_font_percent } }, locations_config: locations.Config = .{}, locations_cache: locations.Cache = .{}, @@ -439,7 +447,7 @@ pub const replica = .{ // session's closed panes, and the files of a host with no file // system of its own. .authority = .{ "host", "fs", "store", "recent", "db", "fallback" }, - .len = .{ .{ "look_src", "n_look_src" }, .{ "session_dir", "session_dir_len" } }, + .len = .{ .{ "look_src", "n_look_src" }, .{ "session_dir", "session_dir_len" }, .{ "session_home", "session_home_len" }, .{ "session_shell", "session_shell_len" } }, }; /// The message row's entry points, called from everywhere as @@ -537,8 +545,7 @@ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { const p = try create(gpa, opts); errdefer p.deinit(); p.boot = Boot.of(opts); - // Asked once, at the start, whoever asks first after. - _ = p.sessionDir(); + p.readSessionFacts(); // A core built without a terminal (a test configuration: core-test's) // boots to a scratch pane where a shell would have been, and says why // once. @@ -982,7 +989,7 @@ pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void { pub fn shellBin(p: *const Pardes) []const u8 { const selected = p.settings.shell.requested.get(); - return if (selected.len == 0) config.defaultShell() else selected; + return if (selected.len == 0) p.sessionShell() else selected; } /// A shell a dead session left, taken in (Adopt): a terminal pane made @@ -1736,14 +1743,45 @@ fn fsReport(p: *Pardes) void { } } +/// The session's facts it lacks, read now: at its start (a replay of the +/// boot reads the session's answers), and after a load of a dump made +/// before they were dumped. Its directory, its home, its shell. +pub fn readSessionFacts(p: *Pardes) void { + // A dump made since carries them all: its shell is never empty. + if (p.session_shell_len != 0) return; + if (p.session_dir_len == null) if (tape.value(.env, processDir, .{&p.session_dir})) |dir| { + std.mem.copyForwards(u8, &p.session_dir, dir); + p.session_dir_len = @intCast(dir.len); + }; + if (tape.value(.env, filesystem.processHome, .{})) |home| if (home.len <= p.session_home.len) { + @memcpy(p.session_home[0..home.len], home); + p.session_home_len = @intCast(home.len); + }; + const shell = tape.value(.env, config.defaultShell, .{}); + const kept = shell[0..@min(shell.len, p.session_shell.len)]; + @memcpy(p.session_shell[0..kept.len], kept); + p.session_shell_len = @intCast(kept.len); +} + /// Where pardes started: the directory of what runs from a header. -pub fn sessionDir(p: *Pardes) []const u8 { - if (p.session_dir_len) |n| return p.session_dir[0..n]; - // The directory the session that recorded was started in. - const dir = tape.value(.cwd, processDir, .{&p.session_dir}) orelse return "/"; - std.mem.copyForwards(u8, &p.session_dir, dir); - p.session_dir_len = @intCast(dir.len); - return p.session_dir[0..dir.len]; +pub fn sessionDir(p: *const Pardes) []const u8 { + return if (p.session_dir_len) |n| p.session_dir[0..n] else "/"; +} + +/// The shell a terminal runs when no `Shell` is set: the session's. +pub fn sessionShell(p: *const Pardes) []const u8 { + return p.session_shell[0..p.session_shell_len]; +} + +/// A path's leading `~` (alone, or before `/` or `:`) as the session's home; +/// `~user` that user's (fs.expandHomeFrom). +pub fn expandHome(p: *const Pardes, path: []const u8, out: *[4096]u8) []const u8 { + return filesystem.expandHomeFrom(path, out, p.sessionHome()); +} + +/// The session's home, null for none. +pub fn sessionHome(p: *const Pardes) ?[]const u8 { + return if (p.session_home_len) |n| p.session_home[0..n] else null; } fn processDir(buf: *[limits.host_path_cap]u8) ?[]const u8 { diff --git a/core/Output.zig b/core/Output.zig index 600ab085..1335d8e9 100644 --- a/core/Output.zig +++ b/core/Output.zig @@ -854,7 +854,7 @@ pub fn openConfig(p: *Pardes, id: usize) !void { .platform = builtins.viewPlatform(p), .theme_name = p.theme().name, .theme_file = pardes.colors.loadedThemeFile(p), - .compiled_default_shell = config.defaultShell(), + .compiled_default_shell = p.sessionShell(), .hover_delay_frames = config.look_preview_delay_frames, .native_images = p.view().frontend.native_images, .capabilities = builtins.viewCapabilities(p), diff --git a/core/builtins.zig b/core/builtins.zig index 0d2e4c20..6b291628 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -664,7 +664,7 @@ pub const ThemeFile = struct { pub const requires_arg = true; pub fn run(c: Ctx) void { var home_buf: [4096]u8 = undefined; - const arg = pardes.filesystem.expandHome(std.mem.trim(u8, c.arg orelse return, " \t\r\n"), &home_buf); + const arg = c.p.expandHome(std.mem.trim(u8, c.arg orelse return, " \t\r\n"), &home_buf); // Said with why, not "invalid theme path". if (!std.mem.endsWith(u8, arg, ".zon")) { var said: pardes.Kind.Buf(320) = .{}; @@ -845,7 +845,7 @@ pub const Get = struct { } if (!panes.Output.fileTraits(f.output).saves) return c.p.reportFailure(c.id, "Get: only a file pane takes it"); var home_buf: [4096]u8 = undefined; - const path = if (typed.len == 0) c.pane.file.?.path else std.Io.Dir.path.resolvePosix(c.p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(typed, &home_buf) }) catch |err| return c.p.reportError(c.id, "Get", err); + const path = if (typed.len == 0) c.pane.file.?.path else std.Io.Dir.path.resolvePosix(c.p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), c.p.expandHome(typed, &home_buf) }) catch |err| return c.p.reportError(c.id, "Get", err); // A file that is not there, or a directory, is said before unsaved // edits are asked about: there would be nothing to load over them. if (typed.len > 0) if (@import("fs.zig").localPath(path)) |local| { @@ -908,7 +908,7 @@ pub const Incl = struct { if (dir_word[0] != '/' and dir_word[0] != '~') return p.reportFailure(c.id, said.print("Incl: invalid directory: {s} is relative", .{shown})); var home_buf: [4096]u8 = undefined; - const expanded = @import("fs.zig").expandHome(dir_word, &home_buf); + const expanded = p.expandHome(dir_word, &home_buf); if (@import("fs.zig").localPath(expanded)) |local| if (exec.kindOf(local) != .directory) return p.reportFailure(c.id, if (exec.kindOf(local) != null) said.print("Incl: not a directory: {s}", .{shown}) @@ -918,7 +918,7 @@ pub const Incl = struct { var words = std.mem.tokenizeAny(u8, typed, " \t"); while (words.next()) |dir_word| { var home_buf: [4096]u8 = undefined; - const full = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(dir_word, &home_buf) }) catch |err| return p.reportError(c.id, "Incl", err); + const full = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), p.expandHome(dir_word, &home_buf) }) catch |err| return p.reportError(c.id, "Incl", err); if (!std.Io.Dir.path.isAbsolute(full)) return p.reportFailure(c.id, "Incl: invalid directory"); for (p.incl.items) |dir| { if (std.mem.eql(u8, dir, full)) break; @@ -1182,7 +1182,7 @@ pub const Term = struct { if (arg.len > 0) { var why: [320]u8 = undefined; // The authority's lookup, taped, as Shell's (exec.zig). - if (pardes.tape.value(.shell, @import("dev").host_io.Shell.refusal, .{ arg[0..@min(arg.len, 200)], &why })) |refused| { + if (pardes.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ arg[0..@min(arg.len, 200)], &why })) |refused| { var said: pardes.Kind.Buf(340) = .{}; return c.p.reportFailure(c.id, said.print("Term: {s}", .{refused})); } diff --git a/core/config.zig b/core/config.zig index 5f63268e..5bbe1886 100644 --- a/core/config.zig +++ b/core/config.zig @@ -330,12 +330,8 @@ pub fn wheelTick(guard: *u8, vertical: bool) bool { /// The shell when no `Shell` is set: the user's login shell ($SHELL), as a /// terminal would start, else /bin/sh. An explicit `Shell` overrides it. +/// This process's: read once, at the session's start (Pardes.session_shell). pub fn defaultShell() []const u8 { - // As the session that recorded found it (core/tape.zig). - return pardes.tape.value(.env, defaultShellHost, .{}); -} - -fn defaultShellHost() []const u8 { if (std.c.getenv("SHELL")) |s| { const shell = std.mem.span(s); // One that is not there, or not executable, is no shell: /bin/sh. @@ -837,7 +833,8 @@ pub const Runtime = struct { @compileError("Shell, Theme, Font, TaglineSize and WindowOpacity actions must each occur exactly once"); } - pub fn apply(state: *Runtime, setting: Setting, argument: ?[]const u8) bool { + /// `home`: what `~` is (the session's: Pardes.session_home). + pub fn apply(state: *Runtime, setting: Setting, argument: ?[]const u8, home: ?[]const u8) bool { switch (setting.action) { // Every toggle names a bool field of its own, so one line covers // all of them and a new toggle needs no arm here. Bare, a toggle @@ -869,7 +866,7 @@ pub const Runtime = struct { if (text.len > 0) { if (text[0] != '/' and text[0] != '~') return false; var home_buf: [4096]u8 = undefined; - if (pardes.filesystem.deniedAbove(pardes.filesystem.expandHome(text, &home_buf))) return false; + if (pardes.filesystem.deniedAbove(pardes.filesystem.expandHomeFrom(text, &home_buf, home))) return false; } if (!state.dump_dir.set(text)) return false; }, @@ -1073,28 +1070,28 @@ pub const Runtime = struct { const verbose = find("Verbose").?; try std.testing.expect(state.verbose); // Bare, it flips: that is what a tagline word does. - try std.testing.expect(apply(&state, verbose, null)); + try std.testing.expect(apply(&state, verbose, null, null)); try std.testing.expect(!state.verbose); // Told, it SETS -- twice over, so the second call proves it is not // flipping past the value it was given. - try std.testing.expect(apply(&state, verbose, "on")); + try std.testing.expect(apply(&state, verbose, "on", null)); try std.testing.expect(state.verbose); - try std.testing.expect(apply(&state, verbose, " on\n")); + try std.testing.expect(apply(&state, verbose, " on\n", null)); try std.testing.expect(state.verbose); - try std.testing.expect(apply(&state, verbose, "off")); + try std.testing.expect(apply(&state, verbose, "off", null)); try std.testing.expect(!state.verbose); - try std.testing.expect(apply(&state, verbose, "off")); + try std.testing.expect(apply(&state, verbose, "off", null)); try std.testing.expect(!state.verbose); // And anything else is refused rather than silently flipping. - try std.testing.expect(!apply(&state, verbose, "maybe")); + try std.testing.expect(!apply(&state, verbose, "maybe", null)); try std.testing.expect(!state.verbose); } test "simple setting application mutates only its plain field" { var state: Runtime = .{}; - try std.testing.expect(apply(&state, find("Wrap").?, null)); + try std.testing.expect(apply(&state, find("Wrap").?, null, null)); try std.testing.expect(!state.wrap); - try std.testing.expect(apply(&state, find("Shell").?, " fish\n")); + try std.testing.expect(apply(&state, find("Shell").?, " fish\n", null)); try std.testing.expectEqualStrings("fish", state.shell.requested.get()); try std.testing.expect(state.shell.pending); } @@ -1104,13 +1101,13 @@ pub const Runtime = struct { var state: Runtime = .{}; for ([_][]const u8{ "1", " 82\n", "100" }) |argument| { - try std.testing.expect(apply(&state, setting, argument)); + try std.testing.expect(apply(&state, setting, argument, null)); try std.testing.expectEqual(try std.fmt.parseInt(u8, std.mem.trim(u8, argument, " \t\r\n"), 10), state.font.tagline_percent); } state.font.tagline_percent = 67; for ([_]?[]const u8{ null, "", "0", "101", "-1", "50%", "999999999999999999999" }) |argument| { - try std.testing.expect(!apply(&state, setting, argument)); + try std.testing.expect(!apply(&state, setting, argument, null)); try std.testing.expectEqual(@as(u8, 67), state.font.tagline_percent); } } @@ -1124,7 +1121,7 @@ pub const Runtime = struct { for ([_][]const u8{ "0", " 85\n", "100" }, [_]u8{ 0, 85, 100 }) |argument, expected| { state.window_opacity_pending = false; - try std.testing.expect(apply(&state, setting, argument)); + try std.testing.expect(apply(&state, setting, argument, null)); try std.testing.expectEqual(expected, state.window_opacity); try std.testing.expect(state.window_opacity_pending); } @@ -1132,12 +1129,12 @@ pub const Runtime = struct { state.window_opacity = 85; state.window_opacity_pending = false; for ([_]?[]const u8{ null, "", " \t\n", "101", "-1", "+85", "85%", "0.5", "85.0", "85 20", "8_5", "0x55", "999999999999999999999" }) |argument| { - try std.testing.expect(!apply(&state, setting, argument)); + try std.testing.expect(!apply(&state, setting, argument, null)); try std.testing.expectEqual(@as(u8, 85), state.window_opacity); try std.testing.expect(!state.window_opacity_pending); } // Reissuing the same request still asks the host to apply it again. - try std.testing.expect(apply(&state, setting, "85")); + try std.testing.expect(apply(&state, setting, "85", null)); try std.testing.expect(state.window_opacity_pending); } @@ -1193,7 +1190,7 @@ pub const Runtime = struct { .{ "GripWidth", "200" }, .{ "InactiveDim", "35" }, .{ "CallstackDepth", "16" }, .{ "Tab", "8" }, }; - for (values) |v| try std.testing.expect(apply(&state, find(v[0]).?, v[1])); + for (values) |v| try std.testing.expect(apply(&state, find(v[0]).?, v[1], null)); try std.testing.expect(state.font.requested_name.set("Wanted Mono")); try std.testing.expect(state.font.requested_path.set("/fonts/wanted.ttf")); state.font.requested_size_hundredths = 1850; @@ -1842,11 +1839,11 @@ test "CallstackDepth is bounded and keeps the last valid runtime limit" { var state: Runtime = .{}; try std.testing.expectEqual(@as(u8, 8), state.callstack_depth); for ([_][]const u8{ "1", "8", "16", "64" }) |value| { - try std.testing.expect(Runtime.apply(&state, Runtime.find("CallstackDepth").?, value)); + try std.testing.expect(Runtime.apply(&state, Runtime.find("CallstackDepth").?, value, null)); try std.testing.expectEqual(try std.fmt.parseInt(u8, value, 10), state.callstack_depth); } for ([_][]const u8{ "0", "65", "256", "-1", "+8", "1_6", "eight", "" }) |value| { - try std.testing.expect(!Runtime.apply(&state, Runtime.find("CallstackDepth").?, value)); + try std.testing.expect(!Runtime.apply(&state, Runtime.find("CallstackDepth").?, value, null)); try std.testing.expectEqual(@as(u8, 64), state.callstack_depth); } } diff --git a/core/dump.zig b/core/dump.zig index 58560645..d8d87fc6 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -168,7 +168,7 @@ pub fn dumpTo(p: *Pardes, to: ?[]const u8) !void { p.dump_export = null; if (to) |path| { var home_buf: [4096]u8 = undefined; - p.dump_export = try p.gpa.dupe(u8, filesystem.expandHome(path, &home_buf)); + p.dump_export = try p.gpa.dupe(u8, p.expandHome(path, &home_buf)); } try dumpState(p); } @@ -403,6 +403,7 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: for (others) |o| gpa.free(o.panes); gpa.free(others); l.shared.shrinkRetainingCapacity(at); + p.readSessionFacts(); return p; } @@ -487,6 +488,7 @@ fn loadImage(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?* try views.undumpedOld(p, others, gpa); } l.made.shrinkRetainingCapacity(at); + p.readSessionFacts(); return p; } diff --git a/core/edit_cmd.zig b/core/edit_cmd.zig index 6cd7788d..a87fdfa2 100644 --- a/core/edit_cmd.zig +++ b/core/edit_cmd.zig @@ -177,7 +177,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { var world: World = .{ .p = p, .from = id, .arena = arena }; var why: sam.Why = .{}; var text: pardes.Kind.Buf(260) = .{}; - const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .check = World.check, .open = World.open, .read = World.read, .refuseGet = World.refuseGet }, &why) catch |err| switch (err) { + const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .check = World.check, .open = World.open, .read = World.read, .refuseGet = World.refuseGet, .home = p.sessionHome() }, &why) catch |err| switch (err) { error.Edit => return p.reportFailure(id, text.print("Edit: {s}", .{why.text()})), error.OutOfMemory => return p.reportError(id, "Edit", err), }; diff --git a/core/exec.zig b/core/exec.zig index 6804a22e..5a9bf192 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -333,7 +333,7 @@ pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void { var home_buf: [4096]u8 = undefined; const full = std.Io.Dir.path.resolvePosix( p.scratch.allocator(), - &.{ Pardes.paneDir(pane), @import("fs.zig").expandHome(path, &home_buf) }, + &.{ Pardes.paneDir(pane), p.expandHome(path, &home_buf) }, ) catch |err| return p.reportError(id, "save", err); if (!std.Io.Dir.path.isAbsolute(full)) return p.reportError(id, "save", error.NoDirectory); if (full.len > Pardes.SavePath.cap) return p.reportError(id, "save", error.PathTooLong); @@ -1022,25 +1022,25 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] p.settings.shell.pending = true; if (!p.announce) return; var said: pardes.Kind.Buf(320) = .{}; - return p.setMessage(v.active, said.print("Shell: the default again, {s} ($SHELL, else /bin/sh)", .{config.defaultShell()[0..@min(config.defaultShell().len, 256)]})); + return p.setMessage(v.active, said.print("Shell: the default again, {s} ($SHELL, else /bin/sh)", .{p.sessionShell()[0..@min(p.sessionShell().len, 256)]})); } var why: [320]u8 = undefined; - if (pardes.tape.value(.shell, @import("dev").host_io.Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { + if (pardes.tape.site(.shell, null, @import("dev").host_io.Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { var text: pardes.Kind.Buf(340) = .{}; return p.reportFailure(v.active, text.print("Shell: {s}", .{refused})); } - if (!p.settings.apply(setting, want)) p.reportFailure(v.active, "Shell: takes " ++ comptime config.Runtime.takes(.shell).?); + if (!p.settings.apply(setting, want, p.sessionHome())) p.reportFailure(v.active, "Shell: takes " ++ comptime config.Runtime.takes(.shell).?); }, // Bare, each says its value, as acme's Tab says the width. inline .tab, .callstack_depth => |_, tag| if (arg == null) { var said: pardes.Kind.Buf(40) = .{}; return p.setMessage(v.active, said.print("{s} {d}", .{ setting.word, @field(p.settings, @tagName(tag)) })); - } else if (!p.settings.apply(setting, arg)) { + } else if (!p.settings.apply(setting, arg, p.sessionHome())) { var said: pardes.Kind.Buf(96) = .{}; p.reportFailure(v.active, said.print("{s}{s}: takes {s}", .{ setting.word, if (tag == .tab) ": invalid tab width" else "", comptime config.Runtime.takes(tag).? })); }, // Said with what it takes: its words, its range. - else => if (!p.settings.apply(setting, arg)) { + else => if (!p.settings.apply(setting, arg, p.sessionHome())) { var buf: [160]u8 = undefined; var text: std.Io.Writer = .fixed(&buf); // A list of values too long for the row is cut. diff --git a/core/fs.zig b/core/fs.zig index 333588ce..f58e108b 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -405,15 +405,21 @@ pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 { } fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 { - return expandHomeFrom(path, out, std.c.getenv("HOME")); + return expandHomeFrom(path, out, processHome()); } -/// expandHome with HOME given (null: unset), so a test need not change the -/// process's environment, which a child spawned later reads. -fn expandHomeFrom(path: []const u8, out: *[4096]u8, env_home: ?[*:0]const u8) []const u8 { +/// This process's home: $HOME, else its passwd entry's; null for none. +pub fn processHome() ?[]const u8 { + return homeDir(std.c.getenv("HOME")); +} + +/// expandHome with the home given (null: none, `~` left as typed): a +/// session's (Pardes.expandHome), or this process's. `~user` is that +/// user's passwd entry, as the session that recorded found it. +pub fn expandHomeFrom(path: []const u8, out: *[4096]u8, given: ?[]const u8) []const u8 { if (path.len == 0 or path[0] != '~') return path; const end = std.mem.indexOfAny(u8, path, "/:") orelse path.len; - const home = (if (end == 1) homeDir(env_home) else userHome(path[1..end])) orelse return path; + const home = (if (end == 1) given else pardes.tape.value(.env, userHome, .{path[1..end]})) orelse return path; const trimmed = std.mem.trimEnd(u8, home, "/"); const rest = path[end..]; if (trimmed.len == 0 and (rest.len == 0 or rest[0] != '/')) return std.fmt.bufPrint(out, "/{s}", .{rest}) catch path; @@ -1468,7 +1474,7 @@ test "a leading ~ is home, from HOME or else the passwd entry" { // HOME unset: the passwd entry's home. const pw = std.c.getpwuid(std.c.getuid()) orelse return error.SkipZigTest; var from_passwd: [4096]u8 = undefined; - try std.testing.expectEqualStrings(try std.fmt.bufPrint(&from_passwd, "{s}/y", .{std.mem.trimEnd(u8, std.mem.span(pw.dir.?), "/")}), expandHomeFrom("~/y", &out, null)); + try std.testing.expectEqualStrings(try std.fmt.bufPrint(&from_passwd, "{s}/y", .{std.mem.trimEnd(u8, std.mem.span(pw.dir.?), "/")}), expandHomeFrom("~/y", &out, homeDir(null))); } test "Restore prefers default directory then falls back to original path" { diff --git a/core/look.zig b/core/look.zig index 7ca8914f..170e5ea9 100644 --- a/core/look.zig +++ b/core/look.zig @@ -1435,7 +1435,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // themselves). var home_buf: [4096]u8 = undefined; const stripped = std.mem.trim(u8, operand_raw, " \t\r\n"); - const operand = if (stripped.len > 0 and stripped[0] == '~') filesystem.expandHome(stripped, &home_buf) else std.mem.trimEnd(u8, operand_raw, "\r\n"); + const operand = if (stripped.len > 0 and stripped[0] == '~') p.expandHome(stripped, &home_buf) else std.mem.trimEnd(u8, operand_raw, "\r\n"); // A +History line names a dump of the state file's (`Restore