// Purpose: native host effects for run.bend: Sys.isatty, Sys.columns and // Sys.has_env (ยง7.3). The compiler splices this file into a native build. // Expected output: none; it registers effects. // Run: bend main.bend -o build/pkg #include Term sys_isatty_run(Env e, Term* f, IoWork* w) { return (Term)(u32)(isatty((int)(u32)f[0]) == 1); } // 0 when fd is not a terminal or reports no size. Term sys_columns_run(Env e, Term* f, IoWork* w) { struct winsize ws; int ok = ioctl((int)(u32)f[0], TIOCGWINSZ, &ws) == 0; return (Term)(u32)(ok ? ws.ws_col : 0); } // 1 if the variable is set; a name IO.get_env rejects, with a NUL or an // =, is not. Term sys_has_env_run(Env e, Term* f, IoWork* w) { uint64_t n = 0; char* name = io_cstr(e, f[0], &n); int set = !io_nul(name, n) && strchr(name, '=') == NULL && getenv(name) != NULL; free(name); return (Term)(u32)set; } // A def the program never reaches has no id (bend guide effects), so each // registration checks for its def's. static void __attribute__((constructor)) sys_use(void) { #ifdef CID(Sys.isatty) io_eff(CID(Sys.isatty), sys_isatty_run); #endif #ifdef CID(Sys.columns) io_eff(CID(Sys.columns), sys_columns_run); #endif #ifdef CID(Sys.has_env) io_eff(CID(Sys.has_env), sys_has_env_run); #endif }