diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index 0d9bda36..c4534606 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -376,7 +376,9 @@ fn compile_address_range( } // Look for second address; a missing ~step counts as 0. - let has_addr2 = !line.eol() && (is_address_char(line.current()) || line.current() == '+'); + // addr1,+N is a GNU extension. + let has_addr2 = !line.eol() + && (is_address_char(line.current()) || (line.current() == '+' && !context.posix)); if !(has_addr2 || is_step_match || is_step_end) { return compilation_error(lines, line, "unexpected `,'"); } @@ -497,8 +499,10 @@ fn compile_address( break; } match line.current() { - 'I' => icase = true, - 'M' => multiline = true, + // The I and M modifiers are GNU extensions: with --posix + // they are left to the command parser, which rejects them. + 'I' if !context.posix => icase = true, + 'M' if !context.posix => multiline = true, _ => break, } line.advance(); @@ -1480,11 +1484,16 @@ fn compile_number_command( lines: &mut ScriptLineProvider, line: &mut ScriptCharProvider, cmd: &mut Command, - _context: &mut ProcessingContext, + context: &mut ProcessingContext, ) -> UResult { line.advance(); // Skip the command character line.eat_spaces(); // Skip any leading whitespace + // An exit code after q is a GNU extension. + if context.posix && cmd.code == 'q' && !line.eol() && line.current().is_ascii_digit() { + return compilation_error(lines, line, "extra characters after command"); + } + match parse_number(lines, line, false)? { Some(n) => { cmd.data = CommandData::Number(n); @@ -1871,7 +1880,7 @@ fn get_cmd_spec( handler: compile_number_command, }), // Q is a GNU extension - 'Q' => Ok(CommandSpec { + 'Q' if !posix => Ok(CommandSpec { n_addr: 1, handler: compile_number_command, }), diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index e7ad2ef0..a4e7450f 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1293,6 +1293,45 @@ fn test_line_address_beyond_usize() { .stdout_is("r\n"); } +#[test] +fn test_posix_rejects_gnu_commands_and_addresses() { + for (script, error) in [ + ("1,+1p", "unexpected `,'"), + ("/a/,+2p", "unexpected `,'"), + ("Q", "invalid command code `Q'"), + ("Q7", "invalid command code `Q'"), + ("q5", "extra characters after command"), + ("$q 5", "extra characters after command"), + ("/A/Ip", "invalid command code `I'"), + ("/a/Mp", "invalid command code `M'"), + ("1,/A/Ip", "invalid command code `I'"), + ] { + // No input: sed stops at the script, before it reads any. + new_ucmd!() + .args(&["--posix", "-n", "-e", script]) + .fails_with_code(1) + .no_stdout() + .stderr_contains(error); + } + // Without --posix they are accepted, and a plain q is POSIX. + for (args, output) in [ + (&["-n", "1,+1p"][..], "a\nb\n"), + (&["-n", "/A/Ip"][..], "a\n"), + (&["Q"][..], ""), + ] { + new_ucmd!() + .args(args) + .pipe_in("a\nb\n") + .succeeds() + .stdout_is(output); + } + new_ucmd!() + .args(&["--posix", "q"]) + .pipe_in("a\nb\n") + .succeeds() + .stdout_is("a\n"); +} + #[test] fn test_posix_disables_gnu_character_classes() { new_ucmd!()