klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit cbeffb8e1ebdd3b53491b728477ae9d08bf2ac44
parent d2b2f4a88a4e25ff0669b9d1d47f41eaa3987053
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Fri, 18 Nov 2011 17:49:32 -0300

Added support for string ports to write and display. Work on bytevector and string ports is complete.

Diffstat:
Msrc/ktoken.c | 3++-
Msrc/kwrite.c | 35+++++++++++++++++++++++++++++++++--
2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/src/ktoken.c b/src/ktoken.c @@ -884,7 +884,8 @@ TValue ktok_read_special(klisp_State *K) has_radixp = true; break; default: - ktok_error(K, "unexpected char in number after #"); + ktok_error(K, "unknown # constant or " + "unexpected char in number after #"); /* avoid warning */ return KINERT; } diff --git a/src/kwrite.c b/src/kwrite.c @@ -58,9 +58,40 @@ void kw_printf(klisp_State *K, const char *format, ...) kwrite_error(K, "error writing"); return; } + } else if (ttismport(port)) { + /* bytevector ports shouldn't write chars */ + klisp_assert(kport_is_textual(port)); + /* string port */ + uint32_t size; + int written; + uint32_t off = kmport_off(port); + + size = kstring_size(kmport_buf(port)) - + kmport_off(port) + 1; + + /* size is always at least 1 (for the '\0') */ + va_start(argp, format); + written = vsnprintf(kstring_buf(kmport_buf(port)) + off, + size, format, argp); + va_end(argp); + + if (written >= size) { /* space wasn't enough */ + kmport_resize_buffer(K, port, off + written); + /* size may be greater than off + written, so get again */ + size = kstring_size(kmport_buf(port)) - off + 1; + va_start(argp, format); + written = vsnprintf(kstring_buf(kmport_buf(port)) + off, + size, format, argp); + va_end(argp); + if (written < 0 || written >= size) { + /* shouldn't happen */ + kwrite_error(K, "error writing"); + return; + } + } + kmport_off(port) = off + written; } else { - /* vsnprintf(buf, size, format, argp); */ - kwrite_error(K, "mem ports not yet supported"); + kwrite_error(K, "unknown port type"); return; } }