From 4c194047d210ad9cf571c6019731d00d1e3615a4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:14:58 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/upgrade.zig | 79 +++++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 42 deletions(-) (limited to 'src/upgrade.zig') 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; -- cgit v1.3