Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nshlib: add print negative number support for echo command #1870

Merged
merged 1 commit into from
Jul 31, 2023
Merged
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
82 changes: 52 additions & 30 deletions nshlib/nsh_envcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,49 +300,71 @@ int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
int newline = 1;
int escape = 0;
int opt;
int i;

while ((opt = getopt(argc, argv, "neE")) != ERROR)
--argc;
++argv;

while (argc > 0 && argv[0][0] == '-')
{
switch (opt)
FAR char const *temp = argv[0] + 1;
size_t i;

for (i = 0; temp[i]; i++)
{
case 'n':
newline = 0;
break;

case 'e':
escape = 1;
break;

case 'E':
escape = 0;
break;

case '?':
default:
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
switch (temp[i])
{
case 'e':
case 'E':
case 'n':
break;
default:
goto do_echo;
}
}
}

/* echo each argument, separated by a space as it must have been on the
* command line.
*/
if (i == 0)
{
goto do_echo;
}

for (i = optind; i < argc; i++)
{
if (i != optind)
while (*temp)
{
nsh_output(vtbl, " ");
switch (*temp++)
{
case 'e':
escape = 1;
break;

case 'E':
escape = 0;
break;

case 'n':
newline = 0;
break;
}
}

--argc;
++argv;
}

do_echo:
while (argc > 0)
{
if (escape)
{
str_escape(argv[i]);
str_escape(argv[0]);
}

nsh_output(vtbl, "%s", argv[i]);
nsh_output(vtbl, "%s", argv[0]);

--argc;
++argv;
if (argc > 0)
{
nsh_output(vtbl, " ");
}
}

if (newline)
Expand Down
Loading