Skip to content

Commit

Permalink
Platform stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Dec 4, 2006
1 parent d934255 commit c746b6f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lua_glue.c
Expand Up @@ -159,10 +159,17 @@ static inline void set_string(lua_State *L, const char *str, const char *sym)
boolean MojoLua_initLua(void)
{
char locale[16];
assert(luaState == NULL);
char ostype[64];
char osversion[64];

if (!MojoPlatform_locale(locale, sizeof (locale)))
xstrncpy(locale, "???", sizeof (locale));
if (!MojoPlatform_osType(ostype, sizeof (ostype)))
xstrncpy(ostype, "???", sizeof (ostype));
if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
xstrncpy(osversion, "???", sizeof (osversion));

assert(luaState == NULL);

luaState = luaL_newstate(); // !!! FIXME: define our own allocator?
if (luaState == NULL)
Expand All @@ -184,6 +191,10 @@ boolean MojoLua_initLua(void)
// Set up initial C functions, etc we want to expose to Lua code...
set_cfunc(luaState, luahook_runfile, "runfile");
set_string(luaState, locale, "locale");
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
lua_setglobal(luaState, "MojoSetup");

// Set up localization table, if possible.
Expand Down
28 changes: 28 additions & 0 deletions platform.h
Expand Up @@ -21,6 +21,34 @@ const char *MojoPlatform_appBinaryPath(void);
// Return true if locale is known, false otherwise.
boolean MojoPlatform_locale(char *buf, size_t len);

boolean MojoPlatform_osType(char *buf, size_t len);
boolean MojoPlatform_osVersion(char *buf, size_t len);

// Basic platform detection.
#if PLATFORM_WINDOWS
#define PLATFORM_NAME "windows"
#elif PLATFORM_MACOSX
#define PLATFORM_NAME "macosx"
#elif PLATFORM_UNIX
#define PLATFORM_NAME "unix"
#else
#error Unknown processor architecture.
#endif

// Basic architecture detection.

#if defined(__powerpc64__)
#define PLATFORM_ARCH "powerpc64"
#elif defined(__ppc__) || defined(__powerpc__) || defined(__POWERPC__)
#define PLATFORM_ARCH "powerpc"
#elif defined(__x86_64__) || defined(_M_X64)
#define PLATFORM_ARCH "x86-64"
#elif defined(__X86__) || defined(__i386__) || defined(i386) || defined (_M_IX86) || defined(__386__)
#define PLATFORM_ARCH "x86"
#else
#error Unknown processor architecture.
#endif

#ifdef __cplusplus
}
#endif
Expand Down
51 changes: 51 additions & 0 deletions platform/unix.c
@@ -1,3 +1,7 @@
#if PLATFORM_MACOSX
#include <Carbon/Carbon.h>
#endif

#include "../platform.h"

#include <unistd.h>
Expand Down Expand Up @@ -116,5 +120,52 @@ boolean MojoPlatform_locale(char *buf, size_t len)
return retval;
} // MojoPlatform_locale


boolean MojoPlatform_osType(char *buf, size_t len)
{
#if PLATFORM_MACOSX
xstrncpy(buf, "macosx", len); // !!! FIXME: better string?
#elif defined(linux) || defined(__linux) || defined(__linux__)
xstrncpy(buf, "linux", len);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
xstrncpy(buf, "freebsd", len);
#elif defined(__NetBSD__)
xstrncpy(buf, "netbsd", len);
#elif defined(__OpenBSD__)
xstrncpy(buf, "openbsd", len);
#elif defined(bsdi) || defined(__bsdi) || defined(__bsdi__)
xstrncpy(buf, "bsdi", len);
#elif defined(_AIX)
xstrncpy(buf, "aix", len);
#elif defined(hpux) || defined(__hpux) || defined(__hpux__)
xstrncpy(buf, "hpux", len);
#elif defined(sgi) || defined(__sgi) || defined(__sgi__) || defined(_SGI_SOURCE)
xstrncpy(buf, "irix", len);
#else
# error Please define your platform.
#endif

return true;
} // MojoPlatform_ostype


boolean MojoPlatform_osVersion(char *buf, size_t len)
{
#if PLATFORM_MACOSX
long ver = 0x0000;
if (Gestalt(gestaltSystemVersion, &ver) == noErr)
{
char str[16];
snprintf(str, sizeof (str), "%X", (int) ver);
snprintf(buf, len, "%c%c.%c.%c", str[0], str[1], str[2], str[3]);
return true;
} // if
#endif

// At this time, there isn't any way to determine the correct version of
// the OS on Unix that works everywhere or necessarily means anything.
return false;
} // MojoPlatform_osversion

// end of unix.c ...

0 comments on commit c746b6f

Please sign in to comment.