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
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ fn uu_main() -> Result<(), Box<dyn Error>> {
writeln!(out, "{table}")?;
}

let intrp = Interpreter::new(ExecMode::Uu, cg, metadata);
// `--posix` / `--traditional` select ExecMode::Posix (RS first-char-only,
// FS="" whole-record field, stricter regex). Keep this mapping out of RS-only PRs.
let mode = if args.posix || args.traditional {
ExecMode::Posix
} else {
ExecMode::Uu
};
let intrp = Interpreter::new(mode, cg, metadata);
AwkRt::new(intrp, bc, &args.read_queue, diagnostics).main_event_loop()?;

Ok(())
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_awk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,35 @@ fn ascii_char_fs_field_split() {
.stdout_is("foo bar baz\n");
}

/// gawk `rscompat`: under `--traditional`, multi-character `RS` uses only the
/// first character (not a regexp). Requires `ExecMode::Posix` from `-c`.
#[test]
fn traditional_rs_uses_first_character_only() {
ucmd()
.args(&["-c", r#"BEGIN { RS = "bar" } { print $1, $2 }"#])
.pipe_in("0foobar0faz\n1foobar1faz")
.succeeds()
.stdout_is("0foo \nar0faz 1foo\nar1faz \n");
}

#[test]
fn posix_rs_uses_first_character_only() {
ucmd()
.args(&["-P", r#"BEGIN { RS = "bar" } { print $1, $2 }"#])
.pipe_in("0foobar0faz\n1foobar1faz")
.succeeds()
.stdout_is("0foo \nar0faz 1foo\nar1faz \n");
}

/// POSIX `FS = ""`: the whole record is `$1` (not per-character fields).
#[test]
fn posix_empty_fs_is_whole_record_field() {
ucmd()
.args(&["-P", r#"BEGIN { FS = ""; $0 = "abc"; print NF, $1 }"#])
.succeeds()
.stdout_is("1 abc\n");
}

#[test]
fn return_stmnt_sets_typedness_eagerly() {
ucmd()
Expand Down
Loading