# Path: VM/ctypes_types.py

# 
# Path: VM/kernel/kernel.py
# class KernelMeta(type):   
# class Kernel(metaclass=KernelMeta):
#     def register(cls, syscall_number: int):
#         def actually_register(function: Callable[..., int]):
#     def __init__(self, cpu):
#     def __getitem__(self, syscall_number: int):
#     def kernel_read_string(self, address: int) -> bytes:


import os
import logging
    import enum
from io import UnsupportedOperation
from ..ctypes_types import udword, ctypes
from .kernel import Kernel, Int, Uint

    TAKEN FROM: http://man7.org/linux/man-pages/man2/writev.2.html
    """

    logger.info('sys_writev(fd=%d, iov=0x%x, iovcnt=%d)', fd, iov_addr, iovcnt)

    size = 0
    for x in range(iovcnt):
        iovec = StructIovec.from_address(kernel.cpu.mem.calc_address(iov_addr))

        logger.info(
            'struct iovec {\n\tvoid *iov_base=0x%08x;\n\tsize_t iov_len=%d;\n}',
            iovec.iov_base, iovec.iov_len
        )

        if not iovec.iov_len:
            iov_addr += ctypes.sizeof(StructIovec)
            continue

        buf = kernel.cpu.mem.get_bytes(iovec.iov_base, iovec.iov_len)

        logger.info('iov_%d=0x%08x; iov_len=%d, buf=%s', x, iovec.iov_base, iovec.iov_len, buf)

        try:
            ret = os.write(kernel.cpu.descriptors[fd].fileno(), buf)
        except (AttributeError, UnsupportedOperation):
            ret = kernel.cpu.descriptors[fd].write(buf.decode('ascii'))
            kernel.cpu.descriptors[fd].flush()

        size += ret if ret is not None else iovec.iov_len

# Path: VM/ctypes_types.py

# 
# Path: VM/kernel/kernel.py
# class KernelMeta(type):   
# class Kernel(metaclass=KernelMeta):
#     def register(cls, syscall_number: int):
#         def actually_register(function: Callable[..., int]):
#     def __init__(self, cpu):
#     def __getitem__(self, syscall_number: int):
#     def kernel_read_string(self, address: int) -> bytes:


# Path: VM/kernel/kernel_filesystem.py
import os
import logging
    import enum
from io import UnsupportedOperation
from ..ctypes_types import udword, ctypes
from .kernel import Kernel, Int, Uint


    TAKEN FROM: http://man7.org/linux/man-pages/man2/writev.2.html
    """

    logger.info('sys_writev(fd=%d, iov=0x%x, iovcnt=%d)', fd, iov_addr, iovcnt)

    size = 0
    for x in range(iovcnt):
        iovec = StructIovec.from_address(kernel.cpu.mem.calc_address(iov_addr))

        logger.info(
            'struct iovec {\n\tvoid *iov_base=0x%08x;\n\tsize_t iov_len=%d;\n}',
            iovec.iov_base, iovec.iov_len
        )

        if not iovec.iov_len:
            iov_addr += ctypes.sizeof(StructIovec)
            continue

        buf = kernel.cpu.mem.get_bytes(iovec.iov_base, iovec.iov_len)

        logger.info('iov_%d=0x%08x; iov_len=%d, buf=%s', x, iovec.iov_base, iovec.iov_len, buf)

        try:
            ret = os.write(kernel.cpu.descriptors[fd].fileno(), buf)
        except (AttributeError, UnsupportedOperation):
            ret = kernel.cpu.descriptors[fd].write(buf.decode('ascii'))
            kernel.cpu.descriptors[fd].flush()

        size += ret if ret is not None else iovec.iov_len

        iov_addr += ctypes.sizeof(StructIovec)  # address of the next struct!