From 1ea2f68e6a9af135e6cb3274fdf28d4ddc5812d5 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 1 Aug 2026 18:25:55 +0200 Subject: fix pi-lens warnings: unwraps, shell SC2046, dup code (fmtbuf/scripts/hexdump/cat/ahci/pci) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - memory.rs: self-test Layout::from_size_align no longer panics — the test fails cleanly on an invalid layout - fat.rs: v() no longer unwraps the active-volume slot — invariant violation logs + halts instead of panicking mid-disk-io - strbuf: shared FmtBuf replaces the duplicated PanicBuf/FmtBuf (main.rs + sched.rs) - test-disk.sh: SC2046 — conditional --fresh arg no longer relies on word splitting - scripts: boot.sh/boot-image.sh share lib-boot-args.sh (dup args loop) - shell: cat/hexdump stdin pairs unified with Option<&str> marker; hexdump format_row folded into format_partial - ahci: read/write_sectors unified behind dma_sectors - pci: config_read/write share config_addr --- shell/src/lib.rs | 94 +++++++++++++------------------------------------------- 1 file changed, 21 insertions(+), 73 deletions(-) (limited to 'shell') diff --git a/shell/src/lib.rs b/shell/src/lib.rs index 617e5a4..4640697 100644 --- a/shell/src/lib.rs +++ b/shell/src/lib.rs @@ -515,7 +515,12 @@ impl Pipeline { } } Filter::Pause => { - let StageState::Pause { shown, screen, cols } = st else { + let StageState::Pause { + shown, + screen, + cols, + } = st + else { unreachable!("stage state does not match filter") }; for l in cur.drain(..) { @@ -871,7 +876,7 @@ fn cmd_cat(p: &Parsed) { if p.args.is_empty() { // Path-less `cat` with no input keyword reads the keyboard // until ^D. - cat_stdin(&mut pipe); + cat_stdin(&mut pipe, None); } else { for path in &p.args { if !cat_file(&mut pipe, path) { @@ -885,7 +890,7 @@ fn cmd_cat(p: &Parsed) { } Input::Til(marker) => { if p.args.is_empty() { - cat_stdin_until(&mut pipe, marker); + cat_stdin(&mut pipe, Some(marker)); } else { // `cat TIL ` — the paths are the input // source, truncated at the first marker line. @@ -904,34 +909,21 @@ fn cmd_cat(p: &Parsed) { pipe.end(); } -fn cat_stdin(pipe: &mut Pipeline) -> bool { +/// Read lines from the tty until ^C/^D (or an exact marker line, when +/// `marker` is Some — the marker itself is not part of the input). +fn cat_stdin(pipe: &mut Pipeline, marker: Option<&str>) -> bool { loop { match tty::read_line() { LineInput::Line(l) => { - if !pipe.line(l.as_str()) { - return false; - } - } - LineInput::Interrupted => return false, // ^C aborts the stream - LineInput::Eof => return true, // ^D = EOF - } - } -} - -fn cat_stdin_until(pipe: &mut Pipeline, marker: &str) -> bool { - loop { - match tty::read_line() { - LineInput::Line(l) => { - // Marker line is not part of the input. - if l.as_str() == marker { + if marker.is_some() && l.as_str() == marker.unwrap() { return true; } if !pipe.line(l.as_str()) { return false; } } - LineInput::Interrupted => return false, - LineInput::Eof => return true, + LineInput::Interrupted => return false, // ^C aborts the stream + LineInput::Eof => return true, // ^D = EOF } } } @@ -1030,25 +1022,6 @@ struct HexCtx<'a> { alive: bool, } -fn format_row(addr: u64, row: &[u8; 16]) -> String { - let mut out = String::new(); - use core::fmt::Write as _; - let _ = core::write!(out, "{:08x} ", addr); - for &b in row { - let _ = core::write!(out, "{:02x} ", b); - } - out.push_str(" |"); - for &b in row { - out.push(if b.is_ascii_graphic() || b == b' ' { - b as char - } else { - '.' - }); - } - out.push('|'); - out -} - fn format_partial(addr: u64, row: &[u8]) -> String { let mut out = String::new(); use core::fmt::Write as _; @@ -1081,7 +1054,7 @@ impl HexCtx<'_> { self.row[self.row_len] = b; self.row_len += 1; if self.row_len == 16 { - let line = format_row(self.pos, &self.row); + let line = format_partial(self.pos, &self.row); self.alive = self.pipe.line(&line); self.pos += 16; self.row_len = 0; @@ -1106,7 +1079,7 @@ fn cmd_hexdump(p: &Parsed) { match &p.input { Input::None => { if p.args.is_empty() { - hexdump_stdin(&mut pipe); + hexdump_stdin(&mut pipe, None); } else { for path in &p.args { if !hexdump_file(&mut pipe, path) { @@ -1120,7 +1093,7 @@ fn cmd_hexdump(p: &Parsed) { } Input::Til(marker) => { if p.args.is_empty() { - hexdump_stdin_until(&mut pipe, marker); + hexdump_stdin(&mut pipe, Some(marker)); } else { // `hexdump TIL ` — the paths are the input // source, truncated at the first marker line. @@ -1176,34 +1149,9 @@ fn hexdump_file(pipe: &mut Pipeline, path: &str) -> bool { ctx.alive } -fn hexdump_stdin(pipe: &mut Pipeline) { - let mut ctx = HexCtx { - pipe, - pos: 0, - row: [0u8; 16], - row_len: 0, - alive: true, - }; - loop { - match tty::read_line() { - LineInput::Line(l) => { - ctx.feed(l.as_str().as_bytes()); - if !ctx.alive { - break; - } - ctx.feed(b"\n"); - if !ctx.alive { - break; - } - } - LineInput::Interrupted => break, - LineInput::Eof => break, - } - } - ctx.flush(); -} - -fn hexdump_stdin_until(pipe: &mut Pipeline, marker: &str) { +/// Hex-dump lines from the tty until ^C/^D (or an exact marker line, +/// when `marker` is Some — the marker itself is not dumped). +fn hexdump_stdin(pipe: &mut Pipeline, marker: Option<&str>) { let mut ctx = HexCtx { pipe, pos: 0, @@ -1214,7 +1162,7 @@ fn hexdump_stdin_until(pipe: &mut Pipeline, marker: &str) { loop { match tty::read_line() { LineInput::Line(l) => { - if l.as_str() == marker { + if marker.is_some() && l.as_str() == marker.unwrap() { break; } ctx.feed(l.as_str().as_bytes()); -- cgit v1.2.3