diff mbox series

lib: utils/serial: Simplify semihosting_getc() function

Message ID 20221222020403.23209-1-peterlin@andestech.com
State Not Applicable
Headers show
Series lib: utils/serial: Simplify semihosting_getc() function | expand

Commit Message

Yu Chien Peter Lin Dec. 22, 2022, 2:04 a.m. UTC
Simply pass the character returned by semihosting_trap(SYSREADC, NULL)
on success, or -1 on failure. This also fixes issue [1] of comparison
of constant -1 with char type.

[1] https://github.com/riscv-software-src/opensbi/issues/269

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
---
 lib/utils/serial/semihosting.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

Comments

Anup Patel Jan. 6, 2023, 12:16 p.m. UTC | #1
On Thu, Dec 22, 2022 at 7:34 AM Yu Chien Peter Lin
<peterlin@andestech.com> wrote:
>
> Simply pass the character returned by semihosting_trap(SYSREADC, NULL)
> on success, or -1 on failure. This also fixes issue [1] of comparison
> of constant -1 with char type.
>
> [1] https://github.com/riscv-software-src/opensbi/issues/269
>
> Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
> ---
>  lib/utils/serial/semihosting.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/lib/utils/serial/semihosting.c b/lib/utils/serial/semihosting.c
> index 5012fa1..cd15f91 100644
> --- a/lib/utils/serial/semihosting.c
> +++ b/lib/utils/serial/semihosting.c
> @@ -151,15 +151,11 @@ static void semihosting_putc(char ch)
>  static int semihosting_getc(void)
>  {
>         char ch = 0;
> -       int ret;
>
> -       if (semihosting_infd < 0)  {
> -               ch = semihosting_trap(SYSREADC, NULL);
> -               ret = ch > -1 ? ch : -1;

We have to return -1 upon any failure or no character. This is
as-per expectation of SBI v0.1 console_getchar()

Regards,
Anup

> -       } else
> -               ret = semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
> -
> -       return ret;
> +       if (semihosting_infd < 0)
> +               return semihosting_trap(SYSREADC, NULL);
> +       else
> +               return semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
>  }
>
>  static struct sbi_console_device semihosting_console = {
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
diff mbox series

Patch

diff --git a/lib/utils/serial/semihosting.c b/lib/utils/serial/semihosting.c
index 5012fa1..cd15f91 100644
--- a/lib/utils/serial/semihosting.c
+++ b/lib/utils/serial/semihosting.c
@@ -151,15 +151,11 @@  static void semihosting_putc(char ch)
 static int semihosting_getc(void)
 {
 	char ch = 0;
-	int ret;
 
-	if (semihosting_infd < 0)  {
-		ch = semihosting_trap(SYSREADC, NULL);
-		ret = ch > -1 ? ch : -1;
-	} else
-		ret = semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
-
-	return ret;
+	if (semihosting_infd < 0)
+		return semihosting_trap(SYSREADC, NULL);
+	else
+		return semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
 }
 
 static struct sbi_console_device semihosting_console = {