From 1108480bd6e34885d05f690ae93aedd2e6944dc6 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Thu, 17 Oct 2019 19:25:49 +0300 Subject: [PATCH] Move version stuff from main.c into env.c --- c/env.c | 233 +++++++++++++++++++++++++++++++++++++++++++++++ c/main.c | 221 -------------------------------------------- c/scheme.h | 11 ++- scripts/build.sh | 2 + 4 files changed, 243 insertions(+), 224 deletions(-) create mode 100644 c/env.c diff --git a/c/env.c b/c/env.c new file mode 100644 index 0000000..444e86a --- /dev/null +++ b/c/env.c @@ -0,0 +1,233 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "scheme.h" + +static value_t get_features_list(void) +{ + static struct accum acc; + static int initialized; + + if (!initialized) { + initialized = 1; + accum_init(&acc); +#ifdef BITS64 + accum_elt(&acc, symbol("64-bit")); +#endif +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + accum_elt(&acc, symbol("big-endian")); +#endif +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + accum_elt(&acc, symbol("little-endian")); +#endif + accum_elt(&acc, symbol("r7rs")); + } + return acc.list; +} + +static value_t build_c_type_bits_list(void) +{ + struct accum acc; + + accum_init(&acc); + accum_name_value1(&acc, "int", fixnum(sizeof(int) * CHAR_BIT)); + accum_name_value1(&acc, "long", fixnum(sizeof(long) * CHAR_BIT)); + accum_name_value1(&acc, "float", fixnum(sizeof(float) * CHAR_BIT)); + accum_name_value1(&acc, "double", fixnum(sizeof(double) * CHAR_BIT)); + accum_name_value1(&acc, "pointer", fixnum(sizeof(void *) * CHAR_BIT)); + accum_name_value1(&acc, "size_t", fixnum(sizeof(size_t) * CHAR_BIT)); + accum_name_value1(&acc, "value_t", fixnum(sizeof(value_t) * CHAR_BIT)); + return acc.list; +} + +static value_t build_stable_specs_list(void) +{ + struct accum acc; + const int *p; + + accum_init(&acc); + for (p = upscheme_stable_specs; *p; p++) { + accum_elt(&acc, fixnum(*p)); + } + return acc.list; +} + +static value_t build_platform_list(void) +{ + struct accum acc; + const char *kernel; + const char *userland; + const char *computer; + const char *endian; + + // + + kernel = userland = computer = endian = "unknown"; + +#ifdef __FreeBSD__ + kernel = userland = "freebsd"; +#endif +#ifdef __FreeBSD_kernel__ + kernel = "freebsd"; +#endif +#ifdef __GLIBC__ + userland = "gnu"; +#endif +#ifdef __OpenBSD__ + userland = kernel = "openbsd"; +#endif +#ifdef __NetBSD__ + userland = kernel = "netbsd"; +#endif +#ifdef __DragonFly__ + userland = kernel = "dragonfly"; +#endif +#ifdef __sun + userland = kernel = "solaris"; +#endif +#ifdef __minix + userland = kernel = "minix"; +#endif +#ifdef __HAIKU__ + userland = "beos"; + kernel = "haiku"; +#endif +#ifdef __APPLE__ + userland = kernel = "darwin"; +#endif +#ifdef _WIN32 + kernel = userland = "windows-nt"; +#endif + +#ifdef __i386 + computer = "x86"; +#endif +#ifdef _M_IX86 + computer = "x86"; +#endif +#ifdef __X86__ + computer = "x86"; +#endif +#ifdef __I86__ + computer = "x86"; +#endif +#ifdef __amd64 + computer = "x86-64"; +#endif +#ifdef __x86_64 + computer = "x86-64"; +#endif +#ifdef _M_AMD64 + computer = "x86-64"; +#endif +#ifdef __ppc__ + computer = "ppc"; +#endif +#ifdef _M_PPC + computer = "ppc"; +#endif +#ifdef __PPC64__ + computer = "ppc-64"; +#endif +#ifdef __mips64__ + computer = "mips-64"; +#endif +#ifdef __arm__ + computer = "arm"; +#endif +#ifdef __aarch64__ + computer = "arm"; +#endif +#ifdef __sparc + computer = "sparc"; +#endif +#ifdef __sparc__ + computer = "sparc"; +#endif +#ifdef __sparc64__ + computer = "sparc"; +#endif +#ifdef __mips__ + computer = "mips"; +#endif +#ifdef __mips64__ + computer = "mips"; +#endif + +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + endian = "big"; +#endif +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + endian = "little"; +#endif + + accum_init(&acc); + accum_name_value1(&acc, "kernel", symbol(kernel)); + accum_name_value1(&acc, "userland", symbol(userland)); + accum_name_value1(&acc, "computer", symbol(computer)); + accum_name_value1(&acc, "endian", symbol(endian)); + return acc.list; +} + +value_t get_version_alist(void) +{ + static struct accum acc; + static int initialized; + + if (!initialized) { + initialized = 1; + accum_init(&acc); + accum_name_value1(&acc, "command", string_from_cstr("upscheme")); + accum_name_value1(&acc, "scheme-id", symbol("upscheme")); + accum_name_value( + &acc, "language", + fl_cons(symbol("scheme"), fl_cons(symbol("r7rs"), FL_NIL))); + accum_name_value(&acc, "features", get_features_list()); + accum_name_value(&acc, "platform", build_platform_list()); + accum_name_value(&acc, "c-type-bits", build_c_type_bits_list()); + accum_name_value1(&acc, "c-compiler-version", + string_from_cstr(SCHEME_C_COMPILER_NAME + " " SCHEME_C_COMPILER_VERSION)); + accum_name_value1(&acc, "c-compiler-command", + string_from_cstr(env_build_cc)); + accum_name_value1(&acc, "c-compiler-flags", + string_from_cstr(env_build_cflags)); + accum_name_value1(&acc, "c-linker-flags", + string_from_cstr(env_build_lflags)); + accum_name_value1(&acc, "revision", + string_from_cstr(env_build_revision)); + accum_name_value1(&acc, "build-date", + string_from_cstr(env_build_date)); + accum_name_value1(&acc, "release", string_from_cstr(env_release)); + accum_name_value1(&acc, "release-date", + string_from_cstr(env_release_date)); + accum_name_value(&acc, "upscheme/stable-specs", + build_stable_specs_list()); + accum_name_value1(&acc, "upscheme/unstable-spec", + fixnum(upscheme_unstable_spec)); + } + return acc.list; +} + +value_t builtin_features(value_t *args, uint32_t nargs) +{ + (void)args; + argcount("features", nargs, 0); + return get_features_list(); +} + +value_t builtin_version_alist(value_t *args, uint32_t nargs) +{ + (void)args; + argcount("version-alist", nargs, 0); + return get_version_alist(); +} diff --git a/c/main.c b/c/main.c index 58e9d7a..8083c61 100644 --- a/c/main.c +++ b/c/main.c @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -16,226 +15,6 @@ #define BOOT_ENV_R7RS 1 #define BOOT_ENV_UNSTABLE 2 -static value_t get_features_list(void) -{ - static struct accum acc; - static int initialized; - - if (!initialized) { - initialized = 1; - accum_init(&acc); -#ifdef BITS64 - accum_elt(&acc, symbol("64-bit")); -#endif -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - accum_elt(&acc, symbol("big-endian")); -#endif -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - accum_elt(&acc, symbol("little-endian")); -#endif - accum_elt(&acc, symbol("r7rs")); - } - return acc.list; -} - -static value_t build_c_type_bits_list(void) -{ - struct accum acc; - - accum_init(&acc); - accum_name_value1(&acc, "int", fixnum(sizeof(int) * CHAR_BIT)); - accum_name_value1(&acc, "long", fixnum(sizeof(long) * CHAR_BIT)); - accum_name_value1(&acc, "float", fixnum(sizeof(float) * CHAR_BIT)); - accum_name_value1(&acc, "double", fixnum(sizeof(double) * CHAR_BIT)); - accum_name_value1(&acc, "pointer", fixnum(sizeof(void *) * CHAR_BIT)); - accum_name_value1(&acc, "size_t", fixnum(sizeof(size_t) * CHAR_BIT)); - accum_name_value1(&acc, "value_t", fixnum(sizeof(value_t) * CHAR_BIT)); - return acc.list; -} - -static value_t build_stable_specs_list(void) -{ - struct accum acc; - const int *p; - - accum_init(&acc); - for (p = upscheme_stable_specs; *p; p++) { - accum_elt(&acc, fixnum(*p)); - } - return acc.list; -} - -static value_t build_platform_list(void) -{ - struct accum acc; - const char *kernel; - const char *userland; - const char *computer; - const char *endian; - - // - - kernel = userland = computer = endian = "unknown"; - -#ifdef __FreeBSD__ - kernel = userland = "freebsd"; -#endif -#ifdef __FreeBSD_kernel__ - kernel = "freebsd"; -#endif -#ifdef __GLIBC__ - userland = "gnu"; -#endif -#ifdef __OpenBSD__ - userland = kernel = "openbsd"; -#endif -#ifdef __NetBSD__ - userland = kernel = "netbsd"; -#endif -#ifdef __DragonFly__ - userland = kernel = "dragonfly"; -#endif -#ifdef __sun - userland = kernel = "solaris"; -#endif -#ifdef __minix - userland = kernel = "minix"; -#endif -#ifdef __HAIKU__ - userland = "beos"; - kernel = "haiku"; -#endif -#ifdef __APPLE__ - userland = kernel = "darwin"; -#endif -#ifdef _WIN32 - kernel = userland = "windows-nt"; -#endif - -#ifdef __i386 - computer = "x86"; -#endif -#ifdef _M_IX86 - computer = "x86"; -#endif -#ifdef __X86__ - computer = "x86"; -#endif -#ifdef __I86__ - computer = "x86"; -#endif -#ifdef __amd64 - computer = "x86-64"; -#endif -#ifdef __x86_64 - computer = "x86-64"; -#endif -#ifdef _M_AMD64 - computer = "x86-64"; -#endif -#ifdef __ppc__ - computer = "ppc"; -#endif -#ifdef _M_PPC - computer = "ppc"; -#endif -#ifdef __PPC64__ - computer = "ppc-64"; -#endif -#ifdef __mips64__ - computer = "mips-64"; -#endif -#ifdef __arm__ - computer = "arm"; -#endif -#ifdef __aarch64__ - computer = "arm"; -#endif -#ifdef __sparc - computer = "sparc"; -#endif -#ifdef __sparc__ - computer = "sparc"; -#endif -#ifdef __sparc64__ - computer = "sparc"; -#endif -#ifdef __mips__ - computer = "mips"; -#endif -#ifdef __mips64__ - computer = "mips"; -#endif - -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - endian = "big"; -#endif -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - endian = "little"; -#endif - - accum_init(&acc); - accum_name_value1(&acc, "kernel", symbol(kernel)); - accum_name_value1(&acc, "userland", symbol(userland)); - accum_name_value1(&acc, "computer", symbol(computer)); - accum_name_value1(&acc, "endian", symbol(endian)); - return acc.list; -} - -static value_t get_version_alist(void) -{ - static struct accum acc; - static int initialized; - - if (!initialized) { - initialized = 1; - accum_init(&acc); - accum_name_value1(&acc, "command", string_from_cstr("upscheme")); - accum_name_value1(&acc, "scheme-id", symbol("upscheme")); - accum_name_value( - &acc, "language", - fl_cons(symbol("scheme"), fl_cons(symbol("r7rs"), FL_NIL))); - accum_name_value(&acc, "features", get_features_list()); - accum_name_value(&acc, "platform", build_platform_list()); - accum_name_value(&acc, "c-type-bits", build_c_type_bits_list()); - accum_name_value1(&acc, "c-compiler-version", - string_from_cstr(SCHEME_C_COMPILER_NAME - " " SCHEME_C_COMPILER_VERSION)); - accum_name_value1(&acc, "c-compiler-command", - string_from_cstr(env_build_cc)); - accum_name_value1(&acc, "c-compiler-flags", - string_from_cstr(env_build_cflags)); - accum_name_value1(&acc, "c-linker-flags", - string_from_cstr(env_build_lflags)); - accum_name_value1(&acc, "revision", - string_from_cstr(env_build_revision)); - accum_name_value1(&acc, "build-date", - string_from_cstr(env_build_date)); - accum_name_value1(&acc, "release", string_from_cstr(env_release)); - accum_name_value1(&acc, "release-date", - string_from_cstr(env_release_date)); - accum_name_value(&acc, "upscheme/stable-specs", - build_stable_specs_list()); - accum_name_value1(&acc, "upscheme/unstable-spec", - fixnum(upscheme_unstable_spec)); - } - return acc.list; -} - -value_t builtin_features(value_t *args, uint32_t nargs) -{ - (void)args; - argcount("features", nargs, 0); - return get_features_list(); -} - -value_t builtin_version_alist(value_t *args, uint32_t nargs) -{ - (void)args; - argcount("version-alist", nargs, 0); - return get_version_alist(); -} - static value_t argv_list(int argc, char *argv[]) { int i; diff --git a/c/scheme.h b/c/scheme.h index 9a68c37..8a0a924 100644 --- a/c/scheme.h +++ b/c/scheme.h @@ -1003,6 +1003,14 @@ void argcount(const char *fname, uint32_t nargs, uint32_t c); const char *env_get_os_name(void); +// env.c + +value_t get_version_alist(void); +value_t builtin_features(value_t *args, uint32_t nargs); +value_t builtin_version_alist(value_t *args, uint32_t nargs); + +// env_*.c + value_t builtin_environment_stack(value_t *args, uint32_t nargs); extern const char env_build_cc[]; @@ -1023,9 +1031,6 @@ value_t builtin_import(value_t *args, uint32_t nargs); //// #include "builtins.h" -value_t builtin_features(value_t *args, uint32_t nargs); -value_t builtin_version_alist(value_t *args, uint32_t nargs); - value_t builtin_pid(value_t *args, uint32_t nargs); value_t builtin_parent_pid(value_t *args, uint32_t nargs); value_t builtin_process_group(value_t *args, uint32_t nargs); diff --git a/scripts/build.sh b/scripts/build.sh index f408dbd..01320e6 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -16,6 +16,7 @@ o_files="$o_files buf.o" o_files="$o_files builtins.o" o_files="$o_files char.o" o_files="$o_files dump.o" +o_files="$o_files env.o" o_files="$o_files env_build.o" o_files="$o_files env_release.o" o_files="$o_files env_unix.o" @@ -115,6 +116,7 @@ $CC $CFLAGS -c ../c/buf.c $CC $CFLAGS -c ../c/builtins.c $CC $CFLAGS -c ../c/char.c $CC $CFLAGS -c ../c/dump.c +$CC $CFLAGS -c ../c/env.c $CC $CFLAGS -c ../c/env_build.c $CC $CFLAGS -c ../c/env_release.c $CC $CFLAGS -c ../c/env_unix.c