sed: fix commandline-given expression when -e is not used

Make explicit sed commands (first on commandline) behave the same
as those given with -e.

Without this patch the following two commands behave differently,
the second one being wrong:
echo ab | sed -e $'1 i\\\n--'
echo ab | sed $'1 i\\\n--'

Reviewed by:	0mp, des, kevans
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D47377
This commit is contained in:
Martin Cracauer 2024-11-06 21:40:02 -06:00 committed by Kyle Evans
parent 5036d9652a
commit 0552fdc62c
2 changed files with 18 additions and 5 deletions

View File

@ -137,10 +137,8 @@ main(int argc, char *argv[])
break;
case 'e':
eflag = 1;
if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
err(1, "malloc");
strcpy(temp_arg, optarg);
strcat(temp_arg, "\n");
if (asprintf(&temp_arg, "%s\n", optarg) == -1)
err(1, "asprintf");
add_compunit(CU_STRING, temp_arg);
break;
case 'f':
@ -173,7 +171,9 @@ main(int argc, char *argv[])
/* First usage case; script is the first arg */
if (!eflag && !fflag && *argv) {
add_compunit(CU_STRING, *argv);
if (asprintf(&temp_arg, "%s\n", *argv) == -1)
err(1, "asprintf");
add_compunit(CU_STRING, temp_arg);
argv++;
}

View File

@ -147,6 +147,18 @@ bracket_y_body()
echo 'bra[ke]' | sed 'y[\[][ct['
}
atf_test_case minus_e
minus_e_head()
{
atf_set "descr" "Verify that -e and implicit arg do the same thing"
}
minus_e_body()
{
printf "ab\n" > a
atf_check -o 'inline:--\nab\n' sed -e $'1 i\\\n--' a
atf_check -o 'inline:--\nab\n' sed $'1 i\\\n--' a
}
atf_init_test_cases()
{
atf_add_test_case inplace_command_q
@ -156,4 +168,5 @@ atf_init_test_cases()
atf_add_test_case commands_on_stdin
atf_add_test_case hex_subst
atf_add_test_case bracket_y
atf_add_test_case minus_e
}