diff --git a/src/event.rs b/src/event.rs index 629faf6..fdd8f90 100644 --- a/src/event.rs +++ b/src/event.rs @@ -74,7 +74,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.begin_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "BEGIN").into()) + } } } @@ -94,7 +96,7 @@ impl<'a> AwkRt<'a> { CtrlSig::End => Ok(false), CtrlSig::NextFile => Ok(true), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| false), - CtrlSig::Next => unreachable!(), + sig @ CtrlSig::Next => Err(misplaced_ctrl_sig(&sig, "BEGINFILE").into()), } } @@ -102,7 +104,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_file_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "ENDFILE").into()) + } } } @@ -112,7 +116,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_code())? { CtrlSig::Exit(end_code) => exit(end_code.or(code).unwrap_or(0)), CtrlSig::End => exit(code.unwrap_or(0)), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "END").into()) + } } } @@ -174,6 +180,29 @@ impl<'a> AwkRt<'a> { } } +#[derive(Debug, thiserror::Error)] +enum EventError { + /// `next` and `nextfile` are rejected by the parser in BEGIN, END, BEGINFILE and ENDFILE, but + /// a function called from there can still reach one. gawk makes that fatal at run time. + #[error("fatal: `{stmt}' cannot be called from a `{rule}' rule")] + MisplacedCtrlSig { stmt: &'static str, rule: &'static str }, +} + +impl From for io::Error { + fn from(err: EventError) -> Self { + Self::other(err) + } +} + +const fn misplaced_ctrl_sig(sig: &CtrlSig, rule: &'static str) -> EventError { + let stmt = if matches!(sig, CtrlSig::Next) { + "next" + } else { + "nextfile" + }; + EventError::MisplacedCtrlSig { stmt, rule } +} + enum ReadSource<'a> { Stdin(StdinLock<'a>), File(File), diff --git a/tests/by-util/test_awk.rs b/tests/by-util/test_awk.rs index 820622d..7ac927f 100644 --- a/tests/by-util/test_awk.rs +++ b/tests/by-util/test_awk.rs @@ -1289,6 +1289,29 @@ fn invalid_regex_literal_is_fatal() { } } +#[test] +fn next_and_nextfile_from_a_function_in_a_special_rule_are_fatal() { + // (rule, statement) + let cases = [ + ("BEGIN", "next"), + ("BEGIN", "nextfile"), + ("END", "next"), + ("END", "nextfile"), + ("BEGINFILE", "next"), + ("ENDFILE", "next"), + ("ENDFILE", "nextfile"), + ]; + for (rule, stmt) in cases { + ucmd() + .arg(format!("function g() {{ {stmt} }} {rule} {{ g() }}")) + .pipe_in("x\n") + .fails_with_code(2) + .stdout_is("") + .stderr_contains(format!("`{stmt}' cannot be called from a `{rule}' rule")) + .stderr_does_not_contain("panicked"); + } +} + #[test] fn bracket_range_ending_in_dash() { ucmd() @@ -1296,3 +1319,21 @@ fn bracket_range_ending_in_dash() { .succeeds() .stdout_only("1\n"); } + +#[test] +fn nextfile_from_a_function_in_beginfile_skips_the_file() { + ucmd() + .arg("function g() { nextfile } BEGINFILE { g() } { print \"record\" } END { print NR }") + .pipe_in("x\n") + .succeeds() + .stdout_is("0\n"); +} + +#[test] +fn next_from_a_function_in_a_rule_skips_the_record() { + ucmd() + .arg("function g() { next } { g(); print \"after\" } END { print NR }") + .pipe_in("x\ny\n") + .succeeds() + .stdout_is("2\n"); +}