# SPDX-License-Identifier: LSL-1.0
# Copyright (c) 2026 Markus Maiwald
# Stewardship: Self Sovereign Society Foundation
#
# This file is part of the Nexus Sovereign Core.
# See legal/LICENSE_SOVEREIGN.md for license terms.

## Rumpk Layer 1: Console Writer Infrastructure


# Forward declarations for C symbols
proc console_write(p: pointer, len: csize_t) {.importc, cdecl.}
proc ion_vfs_write(fd: int32, buf: pointer, count: uint64): int64 {.importc, cdecl.}
proc kprint(s: cstring) {.importc, cdecl.}
proc kprint_hex(n: uint64) {.importc, cdecl.}
proc kprintln(s: cstring) {.importc, cdecl.}

# Wrapper for VFS write to handle stdout/stderr
proc wrapper_vfs_write(fd: int32, buf: pointer, count: uint64): int64 {.cdecl.} =
  # kprint("[WRAPPER] write fd="); kprint_hex(uint64(fd)); kprintln("")
  if fd == 1 or fd == 2:
    console_write(buf, csize_t(count))
    return int64(count)
  return ion_vfs_write(fd, buf, count)
