Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}

Expand All @@ -94,15 +96,17 @@ 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()),
}
}

pub fn end_file_event_loop(&mut self) -> Result<()> {
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())
}
}
}

Expand All @@ -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())
}
}
}

Expand Down Expand Up @@ -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<EventError> 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),
Expand Down
41 changes: 41 additions & 0 deletions tests/by-util/test_awk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,10 +1289,51 @@ 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()
.arg(r#"BEGIN { print ("-" ~ /[!---]/) }"#)
.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");
}
Loading