diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 12:14:58 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:56:36 -0300 |
| commit | 4c194047d210ad9cf571c6019731d00d1e3615a4 (patch) | |
| tree | 257b994e471db3011589acd0cfb4a9168f803019 | |
| parent | 60dd9514c8676e79c625948b5e9d2c6d745e8e87 (diff) | |
| download | pardes-4c194047d210ad9cf571c6019731d00d1e3615a4.tar.gz pardes-4c194047d210ad9cf571c6019731d00d1e3615a4.zip | |
idioms #4, string chains are an enum and a switch: main's flags (each exact one a Flag, the prefixed ones as they were), --ps's rows and manage's verb, the daemon's verbs on both ends and its registry's rows, an upgrade record's words, the window driver's mouse kinds and finger names, a frame script's click target and a mount name's reserved words go through std.meta.stringToEnum; with them main asks std.Io for its working directory, its chdir, a directory's access and /proc's comm, not libc
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/FrameStats.zig | 5 | ||||
| -rw-r--r-- | src/daemon.zig | 283 | ||||
| -rw-r--r-- | src/dump.zig | 9 | ||||
| -rw-r--r-- | src/fs.zig | 3 | ||||
| -rw-r--r-- | src/gui/gui.zig | 70 | ||||
| -rw-r--r-- | src/main.zig | 162 | ||||
| -rw-r--r-- | src/upgrade.zig | 79 |
7 files changed, 290 insertions, 321 deletions
diff --git a/src/FrameStats.zig b/src/FrameStats.zig index 34a629dd..d55b9269 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -379,9 +379,8 @@ pub fn parseScript(arena: std.mem.Allocator, text: []const u8) error{ OutOfMemor var parts = std.mem.tokenizeScalar(u8, arg, ' '); const button = std.meta.stringToEnum(pardes.Mouse.Button, parts.next() orelse "") orelse return error.BadScript; const where = std.fmt.parseFloat(f32, parts.next() orelse "") catch return error.BadScript; - const on = parts.next() orelse ""; - if (!std.mem.eql(u8, on, "rail") and !std.mem.eql(u8, on, "text")) return error.BadScript; - break :click .{ .click = .{ .button = button, .at = where, .rail = std.mem.eql(u8, on, "rail") } }; + const on = std.meta.stringToEnum(enum { rail, text }, parts.next() orelse "") orelse return error.BadScript; + break :click .{ .click = .{ .button = button, .at = where, .rail = on == .rail } }; }, .paste => .{ .paste = try unescaped(arena, arg) }, .exec => .{ .exec = try unescaped(arena, arg) }, diff --git a/src/daemon.zig b/src/daemon.zig index ed558a52..a4e2c2d9 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -418,31 +418,34 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no while (recv(fd, &buf)) |got| { var words = std.mem.tokenizeScalar(u8, got.words, ' '); const verb = words.next() orelse ""; - if (std.mem.eql(u8, verb, "ok")) { - up.store(true, .release); - } else if (std.mem.eql(u8, verb, "attach")) { - deliver(ctx, .{ .attach = .{ - .tty = if (got.nfds >= 2) got.fds[0] else -1, - .stub = if (got.nfds >= 2) got.fds[1] else -1, - } }); - for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); - } else if (std.mem.eql(u8, verb, "replica")) { - const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; - if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); - for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); - } else if (std.mem.eql(u8, verb, "adopted")) { - var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; - const a = ¬e.adopted; - for (got.fds[0..got.nfds]) |f| { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { - _ = libc.close(f); - continue; - }; - a.pids[a.n] = pid; - a.fds[a.n] = f; - a.n += 1; - } - deliver(ctx, note); + if (std.meta.stringToEnum(enum { ok, attach, replica, adopted }, verb)) |v| switch (v) { + .ok => up.store(true, .release), + .attach => { + deliver(ctx, .{ .attach = .{ + .tty = if (got.nfds >= 2) got.fds[0] else -1, + .stub = if (got.nfds >= 2) got.fds[1] else -1, + } }); + for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); + }, + .replica => { + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); + for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); + }, + .adopted => { + var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; + const a = ¬e.adopted; + for (got.fds[0..got.nfds]) |f| { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { + _ = libc.close(f); + continue; + }; + a.pids[a.n] = pid; + a.fds[a.n] = f; + a.n += 1; + } + deliver(ctx, note); + }, } else for (got.fds[0..got.nfds]) |f| { _ = libc.close(f); } @@ -805,97 +808,87 @@ const Daemon = struct { }; const c = &d.conns[i]; var words = std.mem.tokenizeScalar(u8, got.words, ' '); - const verb = words.next() orelse return; - if (std.mem.eql(u8, verb, "hello")) { - const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0; - const k = std.meta.stringToEnum(Kind, words.next() orelse ""); - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const state = words.next() orelse ""; - const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; - const rest = std.mem.trim(u8, words.rest(), " "); - if (v != version or k == null or pid <= 0) { - // Another build's: it runs without a daemon. - d.reply(i, "version " ++ std.fmt.comptimePrint("{d}", .{version}), &.{}); - return if (d.conns[i].fd >= 0) d.hangUp(i); - } - c.kind = k; - c.pid = pid; - c.detached = std.mem.eql(u8, state, "detached"); - c.semantics = semantics; - c.nclients = 0; - c.name_len = @min(rest.len, c.name.len); - @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); - return d.reply(i, "ok", &.{}); - } - if (std.mem.eql(u8, verb, "state")) { - c.detached = std.mem.eql(u8, words.next() orelse "", "detached"); - return; - } - if (std.mem.eql(u8, verb, "clients")) { - c.nclients = 0; - while (words.next()) |w| if (c.nclients < max_clients) { - c.clients[c.nclients] = std.fmt.parseInt(libc.pid_t, w, 10) catch 0; - c.nclients += 1; - }; - return; - } - if (std.mem.eql(u8, verb, "pty")) { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; - if (got.nfds < 1) return; - for (&d.held) |*h| if (h.fd < 0) { - h.* = .{ .fd = got.fds[0], .pid = pid, .owner = i }; - fds_used = 1; - return; - }; - return; - } - if (std.mem.eql(u8, verb, "close")) { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; - for (&d.held) |*h| if (h.fd >= 0 and h.pid == pid and h.owner == i) drop(h); - return; - } - if (std.mem.eql(u8, verb, "quit")) { - for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) drop(h); - return; - } - if (std.mem.eql(u8, verb, "upgrade")) { - d.upgrade_asked = true; - return; - } - if (std.mem.eql(u8, verb, "list")) return d.list(i); - if (std.mem.eql(u8, verb, "kill")) return d.kill(i, std.mem.trim(u8, words.rest(), " ")); - if (std.mem.eql(u8, verb, "killall")) return d.kill(i, null); - if (std.mem.eql(u8, verb, "adopt")) { - var line: [msg_max]u8 = undefined; - var w: std.Io.Writer = .fixed(&line); - w.writeAll("adopted") catch {}; - var fds: [max_fds]c_int = undefined; - var given: [max_fds]*Held = undefined; - var n: usize = 0; - for (&d.held) |*h| if (h.fd >= 0 and h.owner == null and n < max_fds) { - w.print(" {d}", .{h.pid}) catch break; - fds[n] = h.fd; - given[n] = h; - n += 1; - }; - if (!send(c.fd, w.buffered(), fds[0..n])) return d.hangUp(i); - for (given[0..n]) |h| h.owner = i; - return; - } - if (std.mem.eql(u8, verb, "attach")) { - const k = std.meta.stringToEnum(Kind, words.next() orelse "") orelse return d.reply(i, "no bad attach", &.{}); - const wanted_name = std.mem.trim(u8, words.rest(), " "); - var why: [512]u8 = undefined; - // "other <name>": the client joins it through the replica socket. - const target = d.find(k, wanted_name, &why) orelse return d.reply(i, std.mem.sliceTo(&why, 0), &.{}); - const t = &d.conns[target]; - if (!send(t.fd, "attach", got.fds[0..got.nfds])) { - d.hangUp(target); - return d.reply(i, "no that session does not answer", &.{}); - } - t.detached = false; - var line: [32]u8 = undefined; - return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch "ok 0", &.{}); + const Verb = enum { hello, state, clients, pty, close, quit, upgrade, list, kill, killall, adopt, attach }; + switch (std.meta.stringToEnum(Verb, words.next() orelse return) orelse return) { + .hello => { + const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const state = words.next() orelse ""; + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + if (v != version or k == null or pid <= 0) { + // Another build's: it runs without a daemon. + d.reply(i, "version " ++ std.fmt.comptimePrint("{d}", .{version}), &.{}); + return if (d.conns[i].fd >= 0) d.hangUp(i); + } + c.kind = k; + c.pid = pid; + c.detached = std.mem.eql(u8, state, "detached"); + c.semantics = semantics; + c.nclients = 0; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + return d.reply(i, "ok", &.{}); + }, + .state => c.detached = std.mem.eql(u8, words.next() orelse "", "detached"), + .clients => { + c.nclients = 0; + while (words.next()) |w| if (c.nclients < max_clients) { + c.clients[c.nclients] = std.fmt.parseInt(libc.pid_t, w, 10) catch 0; + c.nclients += 1; + }; + }, + .pty => { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; + if (got.nfds < 1) return; + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = got.fds[0], .pid = pid, .owner = i }; + fds_used = 1; + return; + }; + }, + .close => { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; + for (&d.held) |*h| if (h.fd >= 0 and h.pid == pid and h.owner == i) drop(h); + }, + .quit => for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) drop(h), + .upgrade => d.upgrade_asked = true, + .list => d.list(i), + .kill => d.kill(i, std.mem.trim(u8, words.rest(), " ")), + .killall => d.kill(i, null), + .adopt => { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll("adopted") catch unreachable; // the first word of msg_max + var fds: [max_fds]c_int = undefined; + var given: [max_fds]*Held = undefined; + var n: usize = 0; + for (&d.held) |*h| if (h.fd >= 0 and h.owner == null and n < max_fds) { + w.print(" {d}", .{h.pid}) catch break; + fds[n] = h.fd; + given[n] = h; + n += 1; + }; + if (!send(c.fd, w.buffered(), fds[0..n])) return d.hangUp(i); + for (given[0..n]) |h| h.owner = i; + }, + .attach => { + const k = std.meta.stringToEnum(Kind, words.next() orelse "") orelse return d.reply(i, "no bad attach", &.{}); + const wanted_name = std.mem.trim(u8, words.rest(), " "); + var why: [512]u8 = undefined; + // "other <name>": the client joins it through the replica socket. + const target = d.find(k, wanted_name, &why) orelse return d.reply(i, std.mem.sliceTo(&why, 0), &.{}); + const t = &d.conns[target]; + if (!send(t.fd, "attach", got.fds[0..got.nfds])) { + d.hangUp(target); + return d.reply(i, "no that session does not answer", &.{}); + } + t.detached = false; + var line: [32]u8 = undefined; + // "ok " and a pid_t's at most 11 digits fit 32. + return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch unreachable, &.{}); + }, } } @@ -1079,33 +1072,37 @@ const Daemon = struct { while (lines.next()) |line| { var words = std.mem.tokenizeScalar(u8, line, ' '); const what = words.next() orelse continue; - if (std.mem.eql(u8, what, "replicas")) { - replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; - if (replicas >= 0) ninep_io.setCloexec(replicas); - } else if (std.mem.eql(u8, what, "c")) { - const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; - const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; - if (i >= max_conns) continue; - const k = std.meta.stringToEnum(Kind, words.next() orelse ""); - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const detached = std.mem.eql(u8, words.next() orelse "", "1"); - const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; - const rest = std.mem.trim(u8, words.rest(), " "); - ninep_io.setCloexec(fd); - const c = &d.conns[i]; - c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; - c.nclients = 0; - c.name_len = @min(rest.len, c.name.len); - @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); - } else if (std.mem.eql(u8, what, "h")) { - const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; - const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; - ninep_io.setCloexec(fd); - for (&d.held) |*h| if (h.fd < 0) { - h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; - break; - }; + switch (std.meta.stringToEnum(enum { replicas, c, h }, what) orelse continue) { + .replicas => { + replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; + if (replicas >= 0) ninep_io.setCloexec(replicas); + }, + .c => { + const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + if (i >= max_conns) continue; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const detached = std.mem.eql(u8, words.next() orelse "", "1"); + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + ninep_io.setCloexec(fd); + const c = &d.conns[i]; + c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; + c.nclients = 0; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + }, + .h => { + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; + ninep_io.setCloexec(fd); + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; + break; + }; + }, } } return replicas; diff --git a/src/dump.zig b/src/dump.zig index 5c897479..04d08142 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -765,18 +765,21 @@ test "a pane restored with unsaved text is asked about by Exit, Restore, Del and var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var restore_line: [4200]u8 = undefined; - for ([_][]const u8{ "Exit", "Restore", "Del", "get" }) |word| { + for ([_]enum { Exit, Restore, Del, get }{ .Exit, .Restore, .Del, .get }) |word| { const restored = try initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); defer restored.deinit(); const id = for (restored.panes, 0..) |slot, i| { if (slot) |candidate| if (candidate.file != null and candidate.file.?.output == null) break i; } else return error.FileLost; const th = @import("ninep/testing.zig"); - const node = if (std.mem.eql(u8, word, "Del") or std.mem.eql(u8, word, "get")) + const node = if (word == .Del or word == .get) ctlfs.Node.of(restored.panes[id].?.serial, .ctl) else @backingInt(ctlfs.TopFile.ctl); - const line = if (std.mem.eql(u8, word, "Restore")) try std.fmt.bufPrint(&restore_line, "Restore {s}/d.zon\n", .{dir}) else if (std.mem.eql(u8, word, "Exit")) "Exit\n" else if (std.mem.eql(u8, word, "Del")) "Del\n" else "get\n"; + const line = switch (word) { + .Restore => try std.fmt.bufPrint(&restore_line, "Restore {s}/d.zon\n", .{dir}), + inline else => |w| @tagName(w) ++ "\n", + }; const answer = th.wr(restored, node, line); try std.testing.expectEqual(ctlfs.Status.err, answer.reply.status); try std.testing.expect(std.mem.indexOf(u8, answer.reply.ename, "Modified") != null); @@ -281,8 +281,7 @@ fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsRe } pub fn validMountName(name: []const u8) bool { - if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false; - if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false; + if (name.len == 0 or name.len > name_capacity or std.meta.stringToEnum(enum { @".", @"..", os, self }, name) != null) return false; for (name) |c| if (!std.ascii.isAlphanumeric(c) and c != '_' and c != '-' and c != '.') return false; return true; } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 3bb304f0..c149d913 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -856,7 +856,6 @@ fn stopPipe() ![2]c_int { return fds; } - fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { while (true) { var fds = [_]libc.pollfd{ @@ -873,8 +872,6 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } } - - test "GUI worker setup failures finish matching LSP and pipe requests" { const gpa = std.testing.allocator; var failing_vtable = std.testing.io.vtable.*; @@ -2235,29 +2232,33 @@ const StdinFeed = struct { // counts them: shift 4, meta (alt) 8, ctrl 16. const held = std.fmt.parseInt(u8, parts.next() orelse "0", 10) catch 0; var sev = std.mem.zeroes(c.SDL_Event); - if (std.mem.eql(u8, kind, "wheel")) { - // ESC]777;mouse;wheel;<y, SDL's sign: up positive>;<x>;<y>BEL: - // a whole number is a mouse's notches, else a touchpad's. - const amount = std.fmt.parseFloat(f32, second) catch 0; - sev.type = c.SDL_EVENT_MOUSE_WHEEL; - sev.wheel.y = amount; - sev.wheel.integer_y = @intFromFloat(@trunc(amount)); - sev.wheel.mouse_x = x; - sev.wheel.mouse_y = y; - } else if (std.mem.eql(u8, kind, "focus")) { + const k = std.meta.stringToEnum(enum { wheel, focus, motion, down, up }, kind) orelse .up; + switch (k) { + .wheel => { + // ESC]777;mouse;wheel;<y, SDL's sign: up positive>;<x>;<y>BEL: + // a whole number is a mouse's notches, else a touchpad's. + const amount = std.fmt.parseFloat(f32, second) catch 0; + sev.type = c.SDL_EVENT_MOUSE_WHEEL; + sev.wheel.y = amount; + sev.wheel.integer_y = @intFromFloat(@trunc(amount)); + sev.wheel.mouse_x = x; + sev.wheel.mouse_y = y; + }, // ESC]777;mouse;focus;<1 gained, 0 lost>;0;0BEL - sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST; - } else if (std.mem.eql(u8, kind, "motion")) { - sev.type = c.SDL_EVENT_MOUSE_MOTION; - sev.motion.x = x; - sev.motion.y = y; - sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); - } else { - sev.type = if (std.mem.eql(u8, kind, "down")) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; - sev.button.button = button; - sev.button.down = std.mem.eql(u8, kind, "down"); - sev.button.x = x; - sev.button.y = y; + .focus => sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST, + .motion => { + sev.type = c.SDL_EVENT_MOUSE_MOTION; + sev.motion.x = x; + sev.motion.y = y; + sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); + }, + .down, .up => { + sev.type = if (k == .down) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; + sev.button.button = button; + sev.button.down = k == .down; + sev.button.x = x; + sev.button.y = y; + }, } const before = c.SDL_GetModState(); var mods: c.SDL_Keymod = 0; @@ -2367,16 +2368,13 @@ fn parseFinger(payload: []const u8) ?Finger { const pressure_s = parts.next() orelse return null; if (parts.next() != null) return null; _ = std.fmt.parseFloat(f32, pressure_s) catch return null; - const kind: @FieldType(Finger, "kind") = if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_DOWN") or std.mem.eql(u8, type_name, "down")) - .down - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_MOTION") or std.mem.eql(u8, type_name, "motion")) - .motion - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_UP") or std.mem.eql(u8, type_name, "up")) - .up - else if (std.mem.eql(u8, type_name, "SDL_EVENT_FINGER_CANCELED") or std.mem.eql(u8, type_name, "cancel")) - .cancel - else - return null; + const Name = enum { SDL_EVENT_FINGER_DOWN, down, SDL_EVENT_FINGER_MOTION, motion, SDL_EVENT_FINGER_UP, up, SDL_EVENT_FINGER_CANCELED, cancel }; + const kind: @FieldType(Finger, "kind") = switch (std.meta.stringToEnum(Name, type_name) orelse return null) { + .SDL_EVENT_FINGER_DOWN, .down => .down, + .SDL_EVENT_FINGER_MOTION, .motion => .motion, + .SDL_EVENT_FINGER_UP, .up => .up, + .SDL_EVENT_FINGER_CANCELED, .cancel => .cancel, + }; return .{ .kind = kind, .id = std.fmt.parseInt(u64, id_s, 10) catch return null, @@ -3452,7 +3450,6 @@ const Shell = struct { } }; - fn wakeFs(ctx: ?*anyopaque) void { const q: *Queue = @ptrCast(@alignCast(ctx.?)); q.push(.fs_ready); @@ -8176,7 +8173,6 @@ test "a caret eases a cell at a time, keeps up with typing, and glides a jump, l try std.testing.expect(!caret.moving()); } - /// The rows a caret whose line rode the wheel's glide is drawn above its /// cell: the text's own offset, so it moves with its line as a glyph does /// (its springs landed on the cell, Caret.see). diff --git a/src/main.zig b/src/main.zig index 5cbfa7f4..7b6fd6d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -150,7 +150,7 @@ fn pageInto(io: std.Io, gpa: std.mem.Allocator, dial: []const u8, text: []const // The pane's directory: this one, unless its name could not be one line // of a ctl (a newline, a control byte): then the session's. var cwd_buf: [4096]u8 = undefined; - const cwd: []const u8 = if (std.c.getcwd(&cwd_buf, cwd_buf.len)) |c| std.mem.span(@as([*:0]u8, @ptrCast(c))) else ""; + const cwd: []const u8 = cwd_buf[0 .. std.process.currentPath(io, &cwd_buf) catch 0]; // ...or the session's when this one may not be written (`git log` in // /usr/src), which /pager refuses: the text is paged all the same. const plain = for (cwd) |c| { @@ -403,8 +403,44 @@ fn nativeMain(std_init: std.process.Init) !void { var i: usize = if (resuming) 2 else 1; while (i < args.len) : (i += 1) { const a = args[i]; - if (std.mem.eql(u8, a, "--tty")) { - explicit_tty = true; + const Flag = enum { @"--tty", @"-n", @"-l", @"--9p", @"--9p-tcp", @"--nested", @"-w", @"--wait", @"--detach", @"--attach", @"--ps", @"--kill-all", @"-h", @"--help", @"--version", @"--" }; + if (std.meta.stringToEnum(Flag, a)) |flag| switch (flag) { + .@"--tty" => explicit_tty = true, + .@"-n" => { + session_only = false; + new_session = true; + i += 1; + if (i >= args.len) badArgs(init.io, "-n needs a count after it: 1 or 3", .{}); + opts.shells = std.fmt.parseInt(u8, args[i], 10) catch + badArgs(init.io, "-n takes 1 or 3, not '{s}'", .{args[i]}); + }, + .@"-l" => { + session_only = false; + new_session = true; + i += 1; + if (i >= args.len) badArgs(init.io, "-l needs the path of a dump to load", .{}); + opts.load_path = args[i]; + }, + .@"--9p" => badArgs(init.io, "--9p needs a socket name: --9p=<name>", .{}), + .@"--9p-tcp" => badArgs(init.io, "--9p-tcp needs tcp!<numeric-ip>!<port>", .{}), + .@"--nested" => opts.nested = true, + .@"-w", .@"--wait" => wait = true, + .@"--detach" => detach = "", + .@"--attach" => attach = "", + .@"--ps" => manage(init.io, .list, ""), + .@"--kill-all" => manage(init.io, .killall, ""), + .@"-h", .@"--help" => return std.Io.File.stdout().writeStreamingAll(init.io, help_text), + .@"--version" => return std.Io.File.stdout().writeStreamingAll(init.io, version_text), + .@"--" => { + // The end of options: what follows is a name, `-dash.txt` too. + for (args[i + 1 ..]) |name| { + if (name.len == 0) badArgs(init.io, "an empty argument names nothing", .{}); + if (positional != null) badArgs(init.io, "one file or directory at a time, and '{s}' is the second", .{name}); + session_only = false; + positional = name; + } + break; + }, } else if (std.mem.eql(u8, a, "--tty-toggle") or std.mem.startsWith(u8, a, "--tty-toggle=")) { session_only = false; new_session = true; @@ -414,21 +450,6 @@ fn nativeMain(std_init: std.process.Init) !void { break :next args[i]; }; opts.tty_toggle = parseCtrlKey(key) orelse badArgs(init.io, "--tty-toggle wants one letter, not '{s}'", .{key}); - } else if (std.mem.eql(u8, a, "-n")) { - session_only = false; - new_session = true; - i += 1; - if (i >= args.len) badArgs(init.io, "-n needs a count after it: 1 or 3", .{}); - opts.shells = std.fmt.parseInt(u8, args[i], 10) catch - badArgs(init.io, "-n takes 1 or 3, not '{s}'", .{args[i]}); - } else if (std.mem.eql(u8, a, "-l")) { - session_only = false; - new_session = true; - i += 1; - if (i >= args.len) badArgs(init.io, "-l needs the path of a dump to load", .{}); - opts.load_path = args[i]; - } else if (std.mem.eql(u8, a, "--9p")) { - badArgs(init.io, "--9p needs a socket name: --9p=<name>", .{}); } else if (std.mem.startsWith(u8, a, "--9p=")) { new_session = true; const name = a["--9p=".len..]; @@ -444,8 +465,6 @@ fn nativeMain(std_init: std.process.Init) !void { badArgs(init.io, "--9p-tcp needs tcp!<numeric-ip>!<port>", .{}); if (opts.ninep_tcp != null) badArgs(init.io, "--9p-tcp was specified twice", .{}); opts.ninep_tcp = dial; - } else if (std.mem.eql(u8, a, "--9p-tcp")) { - badArgs(init.io, "--9p-tcp needs tcp!<numeric-ip>!<port>", .{}); } else if (std.mem.startsWith(u8, a, "--mount=")) { new_session = true; const mount = a["--mount=".len..]; @@ -464,41 +483,14 @@ fn nativeMain(std_init: std.process.Init) !void { mounts_len += 1; } else if (std.mem.startsWith(u8, a, "--geometry=")) { opts.geometry = a["--geometry=".len..]; - } else if (std.mem.eql(u8, a, "--nested")) { - opts.nested = true; - } else if (std.mem.eql(u8, a, "-w") or std.mem.eql(u8, a, "--wait")) { - wait = true; - } else if (std.mem.eql(u8, a, "--detach")) { - detach = ""; } else if (std.mem.startsWith(u8, a, "--detach=")) { detach = a["--detach=".len..]; - } else if (std.mem.eql(u8, a, "--attach")) { - attach = ""; } else if (std.mem.startsWith(u8, a, "--attach=")) { attach = a["--attach=".len..]; - } else if (std.mem.eql(u8, a, "--ps")) { - manage(init.io, "list", ""); } else if (std.mem.startsWith(u8, a, "--kill=")) { const name = a["--kill=".len..]; if (name.len == 0) badArgs(init.io, "--kill needs a session's name; --kill-all ends every one", .{}); - manage(init.io, "kill", name); - } else if (std.mem.eql(u8, a, "--kill-all")) { - manage(init.io, "killall", ""); - } else if (std.mem.eql(u8, a, "-h") or std.mem.eql(u8, a, "--help")) { - try std.Io.File.stdout().writeStreamingAll(init.io, help_text); - return; - } else if (std.mem.eql(u8, a, "--version")) { - try std.Io.File.stdout().writeStreamingAll(init.io, version_text); - return; - } else if (std.mem.eql(u8, a, "--")) { - // The end of options: what follows is a name, `-dash.txt` too. - for (args[i + 1 ..]) |name| { - if (name.len == 0) badArgs(init.io, "an empty argument names nothing", .{}); - if (positional != null) badArgs(init.io, "one file or directory at a time, and '{s}' is the second", .{name}); - session_only = false; - positional = name; - } - break; + manage(init.io, .kill, name); } else if (a.len > 0 and (a[0] != '-' or std.mem.eql(u8, a, "-")) and positional == null) { // `-` is stdin, as for cat and vim: a text to show. session_only = false; @@ -645,9 +637,8 @@ fn nativeMain(std_init: std.process.Init) !void { // Its directory not there either: the name made absolute as // written, and the pane's Save makes the directories. var cwd_buf: [4096]u8 = undefined; - if (std.c.getcwd(&cwd_buf, cwd_buf.len) == null) Refuse.with(init.io, word, error.NoWorkingDirectory); - const cwd = std.mem.sliceTo(&cwd_buf, 0); - const absolute = std.fs.path.resolvePosix(arena, &.{ cwd, target.path }) catch |err| Refuse.with(init.io, word, err); + const cwd = cwd_buf[0 .. std.process.currentPath(init.io, &cwd_buf) catch Refuse.with(init.io, word, error.NoWorkingDirectory)]; + const absolute = std.Io.Dir.path.resolvePosix(arena, &.{ cwd, target.path }) catch |err| Refuse.with(init.io, word, err); // Missing because one above may not be searched or written: // its Save could never make it, so refused now. if (pardes.filesystem.deniedAbove(std.Io.Dir.path.dirname(absolute) orelse "/")) Refuse.with(init.io, word, error.DirectoryNotWritable); @@ -655,13 +646,7 @@ fn nativeMain(std_init: std.process.Init) !void { }; // A directory that is there but may not be read or written: a // pane for the name could only fail at its Save, so refused now. - var dir_z_buf: [4096]u8 = undefined; - if (std.fmt.bufPrintSentinel(&dir_z_buf, "{s}", .{dir.path}, 0)) |dir_z| { - const R_OK = 4; - const W_OK = 2; - const X_OK = 1; - if (std.c.access(dir_z.ptr, R_OK | W_OK | X_OK) != 0) Refuse.with(init.io, word, error.DirectoryNotWritable); - } else |_| {} + std.Io.Dir.cwd().access(init.io, dir.path, .{ .read = true, .write = true, .execute = true }) catch Refuse.with(init.io, word, error.DirectoryNotWritable); break :named std.fmt.bufPrint(&newbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir.path, "/"), base }) catch |err| Refuse.with(init.io, word, err); }; var made_serial: ?u32 = null; @@ -714,12 +699,12 @@ fn nativeMain(std_init: std.process.Init) !void { { opts.file = try arena.dupe(u8, target.path); opts.file_line = target.at.line; - } else if (std.c.chdir(a.ptr) != 0) { + } else if (std.meta.isError(std.process.setCurrentPath(init.io, a))) { var cwdbuf: [4096]u8 = undefined; - const cwd = std.c.getcwd(&cwdbuf, cwdbuf.len) orelse - badArgs(init.io, "this shell's working directory is gone; cd somewhere that exists", .{}); + const cwd = cwdbuf[0 .. std.process.currentPath(init.io, &cwdbuf) catch + badArgs(init.io, "this shell's working directory is gone; cd somewhere that exists", .{})]; var realbuf: [4096]u8 = undefined; - switch (@import("look.zig").resolve(null, a, std.mem.span(@as([*:0]u8, @ptrCast(cwd))), &realbuf)) { + switch (@import("look.zig").resolve(null, a, cwd, &realbuf)) { .file => |t| { opts.file = try arena.dupe(u8, t.path); opts.file_line = t.at.line; @@ -727,7 +712,7 @@ fn nativeMain(std_init: std.process.Init) !void { .kind => |t| opts.file = try arena.dupe(u8, t.path), .none => opts.missing = .{ .word = try arena.dupe(u8, a), - .dir = try arena.dupe(u8, std.mem.span(@as([*:0]u8, @ptrCast(cwd)))), + .dir = try arena.dupe(u8, cwd), }, .url => badArgs(init.io, "a URL is not something a launch can open; start pardes and click it", .{}), .pane => badArgs(init.io, "@pN addresses a pane of a running pardes, so there is none yet", .{}), @@ -831,17 +816,17 @@ extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; /// `pardes --ps`: what the per-user daemon keeps, as a tree; `--kill=<name>` /// and `--kill-all`: those sessions ended with their shells, a TERM and, /// for one still there 2 s later, a KILL. -fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { +fn manage(io: std.Io, verb: enum { list, kill, killall }, name: []const u8) noreturn { if (comptime !daemon.supported) die(io, "this system has no pardes daemon", .{}); var words_buf: [300]u8 = undefined; - const words = if (std.mem.eql(u8, verb, "kill")) + const words = if (verb == .kill) std.fmt.bufPrint(&words_buf, "kill {s}", .{name}) catch die(io, "that name is too long", .{}) else - verb; + @tagName(verb); var buf: [4096]u8 = undefined; const said = daemon.ask(words, &buf) orelse die(io, "no pardes daemon is running", .{}); if (std.mem.startsWith(u8, said, "no ")) die(io, "{s}", .{said[3..]}); - if (std.mem.eql(u8, verb, "list")) { + if (verb == .list) { var out: [8192]u8 = undefined; var w: std.Io.Writer = .fixed(&out); var lines = std.mem.tokenizeScalar(u8, said, '\n'); @@ -851,21 +836,21 @@ fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { const what = it.next() orelse continue; const pid = it.next() orelse continue; var comm_buf: [64]u8 = undefined; - const comm = commOf(pid, &comm_buf); - if (std.mem.eql(u8, what, "daemon")) { - w.print("pardes daemon {s}\n", .{pid}) catch break; - } else if (std.mem.eql(u8, what, "session")) { - const kind = it.next() orelse ""; - const state = it.next() orelse ""; - w.print(" session {s} {s} ({s}, {s})\n", .{ std.mem.trim(u8, it.rest(), " "), pid, kind, state }) catch break; - } else if (std.mem.eql(u8, what, "client")) { - w.print(" client {s} {s}\n", .{ pid, comm }) catch break; - } else if (std.mem.eql(u8, what, "shell")) { - w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; - } else if (std.mem.eql(u8, what, "orphan")) { - if (!orphans) w.writeAll(" shells no session holds (Adopt takes them, --kill-all ends them)\n") catch break; - orphans = true; - w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + const comm = commOf(io, pid, &comm_buf); + switch (std.meta.stringToEnum(enum { daemon, session, client, shell, orphan }, what) orelse continue) { + .daemon => w.print("pardes daemon {s}\n", .{pid}) catch break, + .session => { + const kind = it.next() orelse ""; + const state = it.next() orelse ""; + w.print(" session {s} {s} ({s}, {s})\n", .{ std.mem.trim(u8, it.rest(), " "), pid, kind, state }) catch break; + }, + .client => w.print(" client {s} {s}\n", .{ pid, comm }) catch break, + .shell => w.print(" shell {s} {s}\n", .{ pid, comm }) catch break, + .orphan => { + if (!orphans) w.writeAll(" shells no session holds (Adopt takes them, --kill-all ends them)\n") catch break; + orphans = true; + w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + }, } } std.Io.File.stdout().writeStreamingAll(io, w.buffered()) catch {}; @@ -909,15 +894,10 @@ fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { } /// The command `pid` runs, as /proc says it (Linux); empty where it does not. -fn commOf(pid: []const u8, buf: *[64]u8) []const u8 { +fn commOf(io: std.Io, pid: []const u8, buf: *[64]u8) []const u8 { var path_buf: [64]u8 = undefined; - const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{s}/comm", .{pid}, 0) catch return ""; - const fd = std.c.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(std.c.mode_t, 0)); - if (fd < 0) return ""; - defer _ = std.c.close(fd); - const got = std.c.read(fd, buf, buf.len); - if (got <= 0) return ""; - return std.mem.trimEnd(u8, buf[0..@intCast(got)], "\n"); + const path = std.fmt.bufPrint(&path_buf, "/proc/{s}/comm", .{pid}) catch return ""; + return std.mem.trimEnd(u8, std.Io.Dir.cwd().readFile(io, path, buf) catch return "", "\n"); } /// The replica client of this build's frontend: a terminal's (tui.zig) or a @@ -1007,7 +987,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, Ssh.path = here; _ = atexit(Ssh.end); var waited: u32 = 0; - while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) { + while (std.meta.isError(std.Io.Dir.cwd().access(io, here, .{}))) : (waited += 1) { var status: c_int = 0; if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) { Ssh.child = 0; diff --git a/src/upgrade.zig b/src/upgrade.zig index 626751ff..0f4532a5 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -177,49 +177,44 @@ pub const Record = struct { const word = line[0..sp]; const rest = line[sp + 1 ..]; var it = std.mem.tokenizeScalar(u8, rest, ' '); - if (std.mem.eql(u8, word, "link")) { - rec.link = try std.fmt.parseInt(c_int, rest, 10); - } else if (std.mem.eql(u8, word, "stub")) { - rec.stub = try std.fmt.parseInt(c_int, rest, 10); - } else if (std.mem.eql(u8, word, "old")) { - rec.old = try std.fmt.parseInt(c_int, rest, 10); - } else if (std.mem.eql(u8, word, "window")) { - rec.window = std.mem.eql(u8, rest, "1"); - } else if (std.mem.eql(u8, word, "pty")) { - if (rec.nptys == rec.ptys.len) return error.BadRecord; - const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); - if (pane >= pardes.MAX_PANES) return error.BadRecord; - rec.ptys[rec.nptys] = .{ - .pane = pane, - .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), - .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - }; - rec.nptys += 1; - } else if (std.mem.eql(u8, word, "tty")) { - if (rec.nttys == rec.ttys.len) return error.BadRecord; - var t: Tty = .{ - .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), - .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), - .termios = undefined, - }; - const hex = it.next() orelse return error.BadRecord; - // Another build's termios is the kernel's, of one size. - if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; - _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); - rec.ttys[rec.nttys] = t; - rec.nttys += 1; - } else if (std.mem.eql(u8, word, "refused")) { - rec.refused = rest; - } else if (std.mem.eql(u8, word, "label")) { - rec.label = rest; - } else if (std.mem.eql(u8, word, "exe")) { - rec.exe = rest; - } else if (std.mem.eql(u8, word, "dump")) { - rec.dump = rest; - } // A word of a later build's record is passed over. + switch (std.meta.stringToEnum(enum { link, stub, old, window, pty, tty, refused, label, exe, dump }, word) orelse continue) { + .link => rec.link = try std.fmt.parseInt(c_int, rest, 10), + .stub => rec.stub = try std.fmt.parseInt(c_int, rest, 10), + .old => rec.old = try std.fmt.parseInt(c_int, rest, 10), + .window => rec.window = std.mem.eql(u8, rest, "1"), + .pty => { + if (rec.nptys == rec.ptys.len) return error.BadRecord; + const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); + if (pane >= pardes.MAX_PANES) return error.BadRecord; + rec.ptys[rec.nptys] = .{ + .pane = pane, + .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + }; + rec.nptys += 1; + }, + .tty => { + if (rec.nttys == rec.ttys.len) return error.BadRecord; + var t: Tty = .{ + .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), + .termios = undefined, + }; + const hex = it.next() orelse return error.BadRecord; + // Another build's termios is the kernel's, of one size. + if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; + _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + }, + .refused => rec.refused = rest, + .label => rec.label = rest, + .exe => rec.exe = rest, + .dump => rec.dump = rest, + } } if (rec.dump.len == 0) return error.BadRecord; return rec; |
