/* hosted.c -- StarForth v4 as a Linux program: the nucleus image on the * golden model, its capsules loaded as the system comes up (v4/include/v4/ * boot.h, the same boot the bare-metal kernel runs), the console on stdin * and stdout. * * It runs until its input ends. Blocks are kept in memory and are gone * when it stops. */ #include "v4/boot.h" #include #include #define BLOCKS 64u static v4_node n; static v4_exec_state es; static v4_heat h; static unsigned char disk[BLOCKS * V4_BLOCK_BYTES]; static void console_out(const char *text, unsigned len) { (void)fwrite(text, 1, len, stdout); } int main(void) { const v4_image *im = &v4_capsule_image; unsigned char buf[256]; v4_boot boot; boot.n = &n; boot.es = &es; boot.h = &h; boot.im = im; boot.out = console_out; if (!v4_boot_run(&boot, disk, BLOCKS)) { fflush(stdout); return 1; } for (;;) { (void)v4_exec_step_word(&n, &es, &h); if (n.console_len) { (void)fwrite(n.console, 1, n.console_len, stdout); n.console_len = 0; } if (n.stopped) { fflush(stdout); fprintf(stderr, "starforth4: the node stopped on a fault\n"); return 1; } if (v4_image_waiting(&n, im)) { ssize_t got; fflush(stdout); got = read(0, buf, sizeof buf); if (got <= 0) { putchar('\n'); return 0; } (void)v4_node_console_feed(&n, buf, (unsigned)got); } } }