[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH 2/2] lib/vfscore: Add stdin fop



Hi,

now it is a bit too much of type casting. Makes it bit hard to
read. Here is an idea how to avoid implicit type casting at all.

static ssize_t stdin_read(struct vfscore_file *vfscore_file __unused,
                          void *_buf, size_t count)
{
        ssize_t bytes_read;
        size_t bytes_total = 0;
        char *buf = _buf;

        do {
                while ((bytes_read = ukplat_cink(buf,
                        count - bytes_total)) <= 0)
                        ;

                buf = buf + bytes_read;
                *(buf - 1) = *(buf - 1) == '\r' ?
                                        '\n' : *(buf - 1);

                stdout_write(vfscore_file, (buf - bytes_read),
                                bytes_read);
                bytes_total += bytes_read;

        } while (bytes_total < count && *(buf - 1) != '\n'
                        && *(buf - 1) != VEOF);

        return bytes_total;
}

BR, Yuri.


"Vlad-Andrei BĂDOIU (78692)" <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
writes:

> From: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
>
> This patch adds the stdin operation inside lib/vfscore/stdio.c.
>
> The stdio is mapped by default to fd 0. This implementation mimics
> the normal read behaviour.(read until count bytes or read until VEOF or
> \n is met). The '\r' is replaced with '\n' because ukplat_cink returns
> '\r' on KVM and Xen.
>
> For every successful call of ukplat_cink the input is printed
> to the stdout.
>
> Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
> ---
>  lib/vfscore/stdio.c | 41 ++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 38 insertions(+), 3 deletions(-)
>
> diff --git a/lib/vfscore/stdio.c b/lib/vfscore/stdio.c
> index c631f5f..66898c1 100644
> --- a/lib/vfscore/stdio.c
> +++ b/lib/vfscore/stdio.c
> @@ -35,19 +35,53 @@
>  
>  #include <vfscore/file.h>
>  #include <uk/plat/console.h>
> +#include <uk/essentials.h>
> +#include <termios.h>
>  
>  /* One function for stderr and stdout */
> -static ssize_t stdout_write(struct vfscore_file *vfscore_file, const void 
> *buf,
> -                          size_t count)
> +static ssize_t stdout_write(struct vfscore_file *vfscore_file __unused,
> +                         const void *buf, size_t count)
>  {
> -     (void) vfscore_file;
>       return ukplat_coutk(buf, count);
>  }
>  
> +static ssize_t stdin_read(struct vfscore_file *vfscore_file __unused,
> +                       void *buf, size_t count)
> +{
> +     ssize_t bytes_read, bytes_total = 0;
> +
> +     do {
> +             while ((bytes_read = ukplat_cink(buf,
> +                     count - bytes_total)) <= 0)
> +                     ;
> +
> +             buf = (char *)buf + bytes_read;
> +             *((char *)buf - 1) = *((char *)buf - 1) == '\r' ?
> +                                     '\n' : *((char *)buf - 1);
> +
> +             stdout_write(vfscore_file, ((char *)buf - bytes_read),
> +                             bytes_read);
> +             bytes_total += bytes_read;
> +
> +     } while ((size_t)bytes_total < count && *((char *)buf - 1) != '\n'
> +                     && *((char *)buf - 1) != VEOF);
> +
> +     return bytes_total;
> +}
> +
> +static struct vfscore_fops stdin_fops = {
> +     .read = stdin_read,
> +};
> +
>  static struct vfscore_fops stdout_fops = {
>       .write = stdout_write,
>  };
>  
> +static struct vfscore_file  stdin_file = {
> +     .fd = 0,
> +     .fops = &stdin_fops,
> +};
> +
>  static struct vfscore_file  stdout_file = {
>       .fd = 1,
>       .fops = &stdout_fops,
> @@ -61,6 +95,7 @@ static struct vfscore_file  stderr_file = {
>  
>  void init_stdio(void)
>  {
> +     vfscore_install_fd(0, &stdin_file);
>       vfscore_install_fd(1, &stdout_file);
>       vfscore_install_fd(2, &stderr_file);
>  }
> -- 
> 2.19.2
>

-- 
Yuri Volchkov
Software Specialist

NEC Europe Ltd
Kurfürsten-Anlage 36
D-69115 Heidelberg

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.