diff --git a/i.c b/i.c index f9084a0..8b8b40a 100644 --- a/i.c +++ b/i.c @@ -3890,6 +3890,16 @@ define_instruction(getenv) { gonexti(); } +define_instruction(envvref) { + extern const char *environ_ref(int idx); + int i; char *s; ckk(ac); + i = get_fixnum(ac); /* todo: range-check */ + s = (char *)environ_ref(i); + if (s) ac = string_obj(newstring(s)); + else ac = bool_obj(0); + gonexti(); +} + define_instruction(clock) { double d = (double)clock(); ac = flonum_obj(d); diff --git a/i.h b/i.h index b8e379a..c1a1184 100644 --- a/i.h +++ b/i.h @@ -519,6 +519,7 @@ declare_instruction(frem, "F1", 0, "delete-file", declare_instruction(fren, "F2", 0, "rename-file", '2', AUTOGL) declare_instruction(argvref, "Z0", 0, "%argv-ref", '1', AUTOGL) declare_instruction(getenv, "Z1", 0, "get-environment-variable", '1', AUTOGL) +declare_instruction(envvref, "Z2", 0, "%envv-ref", '1', AUTOGL) declare_instruction(clock, "Z3", 0, "current-jiffy", '0', AUTOGL) declare_instruction(clops, "Z4", 0, "jiffies-per-second", '0', AUTOGL) declare_instruction(cursec, "Z5", 0, "current-second", '0', AUTOGL) diff --git a/s.c b/s.c index b931381..be91040 100644 --- a/s.c +++ b/s.c @@ -17,6 +17,23 @@ int dirsep = '\\'; int dirsep = '/'; #endif +#if defined(WIN32) +#define sxc_environ _environ +#elif defined(__linux) || defined(__APPLE__) +#define sxc_environ environ +#else /* add more systems? */ +char **sxc_environ = { NULL }; +#endif + +extern const char *environ_ref(int idx) +{ + const char **pe = sxc_environ; + /* be careful with indexing! */ + if (idx < 0) return NULL; + while (idx-- > 0) if (*pe++ == NULL) return NULL; + return *pe; +} + char *s_code[] = { "S", "let-syntax",