Skip to content

Commit

Permalink
A couple things cleaned up and patched to compile, and uid/euid/gid s…
Browse files Browse the repository at this point in the history
…upplied

 to Lua scripts.
  • Loading branch information
icculus committed Jan 20, 2008
1 parent 06d8799 commit b97c20f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 38 deletions.
62 changes: 28 additions & 34 deletions lua_glue.c
Expand Up @@ -86,6 +86,15 @@ static inline void set_number(lua_State *L, lua_Number x, const char *sym)
lua_setfield(L, -2, sym);
} // set_string


// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_integer(lua_State *L, lua_Integer x, const char *sym)
{
lua_pushinteger(L, x);
lua_setfield(L, -2, sym);
} // set_string

// !!! FIXME: why is this a different naming convention?
static inline void set_string_array(lua_State *L, int argc, const char **argv,
const char *sym)
Expand Down Expand Up @@ -1483,43 +1492,25 @@ static int luahook_date(lua_State *L)
boolean MojoLua_initLua(void)
{
const char *envr = cmdlinestr("locale", "MOJOSETUP_LOCALE", NULL);
char *homedir = NULL;
char *binarypath = NULL;
char *locale = NULL
char *ostype = NULL;
char *osversion = NULL;

if (envr != NULL)
locale = xstrdup(envr);
else if ((locale = MojoPlatform_locale()) == NULL)
locale = xstrdup("???");

if ((ostype = MojoPlatform_osType()) == NULL)
ostype = xstrdup("???");

if ((osversion = MojoPlatform_osVersion()) == NULL)
osversion = xstrdup("???");
char *homedir = MojoPlatform_homedir();
char *binarypath = MojoPlatform_appBinaryPath();
char *locale = (envr != NULL) ? xstrdup(envr) : MojoPlatform_locale();
char *ostype = MojoPlatform_osType();
char *osversion = MojoPlatform_osVersion();
lua_Integer uid = MojoPlatform_getuid();
lua_Integer euid = MojoPlatform_geteuid();
lua_Integer gid = MojoPlatform_getgid();

if (locale == NULL) locale = xstrdup("???");
if (ostype == NULL) ostype = xstrdup("???");
if (osversion == NULL) osversion = xstrdup("???");

assert(luaState == NULL);

luaState = lua_newstate(MojoLua_alloc, NULL);
if (luaState == NULL)
return false;

luaState = lua_newstate(MojoLua_alloc, NULL); // calls fatal() on failure.
lua_atpanic(luaState, luahook_fatal);

if (!lua_checkstack(luaState, 20)) // Just in case.
{
lua_close(luaState);
luaState = NULL;
return false;
} // if

assert(lua_checkstack(luaState, 20)); // Just in case.
registerLuaLibs(luaState);

homedir = MojoPlatform_homedir();
binarypath = MojoPlatform_appBinaryPath();

// !!! FIXME: I'd like to change the function name case for the lua hooks.

// Build MojoSetup namespace for Lua to access and fill in C bridges...
Expand Down Expand Up @@ -1568,6 +1559,9 @@ boolean MojoLua_initLua(void)
set_string(luaState, homedir, "homedir");
set_string(luaState, binarypath, "binarypath");
set_string(luaState, GBaseArchivePath, "basearchivepath");
set_integer(luaState, uid, "uid");
set_integer(luaState, euid, "euid");
set_integer(luaState, gid, "gid");
set_string_array(luaState, GArgc, GArgv, "argv");
lua_newtable(luaState);
set_string(luaState, "base", "base");
Expand Down Expand Up @@ -1621,11 +1615,11 @@ boolean MojoLua_initLua(void)
lua_setfield(luaState, -2, "archive");
lua_setglobal(luaState, MOJOSETUP_NAMESPACE);

free(binarypath);
free(homedir);
free(osversion);
free(ostype);
free(locale);
free(binarypath);
free(homedir);

// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
Expand Down
2 changes: 1 addition & 1 deletion lua_glue.h
Expand Up @@ -34,7 +34,7 @@ boolean MojoLua_initialized(void);
// Will return false if the file couldn't be loaded, or true if the chunk
// successfully ran. Will not return if there's a runtime error in the
// chunk, as it will call fatal() instead.
boolean MojoLua_runFileFromDir(const char *dir, const char *name)
boolean MojoLua_runFileFromDir(const char *dir, const char *name);

// This is shorthand for MojoLua_runFileFromDir("scripts", fname);
boolean MojoLua_runFile(const char *fname);
Expand Down
9 changes: 9 additions & 0 deletions platform.h
Expand Up @@ -274,6 +274,15 @@ char *MojoPlatform_osType(void);
// Caller must free() the returned pointer!
char *MojoPlatform_osVersion(void);

// !!! FIXME: document me.
uint64 MojoPlatform_getuid(void);

// !!! FIXME: document me.
uint64 MojoPlatform_geteuid(void);

// !!! FIXME: document me.
uint64 MojoPlatform_getgid(void);


// Basic platform detection.
#if PLATFORM_WINDOWS
Expand Down
27 changes: 24 additions & 3 deletions platform_unix.c
Expand Up @@ -370,6 +370,7 @@ char *MojoPlatform_homedir(void)
char *MojoPlatform_locale(void)
{
char *retval = NULL;
char *ptr = NULL;
const char *envr = getenv("LANG");
if (envr != NULL)
{
Expand Down Expand Up @@ -399,7 +400,7 @@ char *MojoPlatform_locale(void)
if (locale != NULL)
{
const CFIndex len = (CFStringGetLength(locale) + 1) * 6;
char *ptr = (char*) xmalloc(len);
ptr = (char*) xmalloc(len);
CFStringGetCString(locale, ptr, len, kCFStringEncodingUTF8);
CFRelease(locale);
retval = xrealloc(ptr, strlen(ptr) + 1);
Expand Down Expand Up @@ -454,10 +455,12 @@ char *MojoPlatform_osVersion()
long ver = 0x0000;
if (Gestalt(gestaltSystemVersion, &ver) == noErr)
{
char *retval = (char *) xmalloc(16);
const size_t len = 8;
char *buf = (char *) xmalloc(len);
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 retval;
return buf;
} // if
#else
// This information may or may not actually MEAN anything. On BeOS, it's
Expand Down Expand Up @@ -1114,6 +1117,24 @@ uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
} // MojoPlatform_decodeImage


uint64 MojoPlatform_getuid(void)
{
return (uint64) getuid();
} // MojoPlatform_getuid


uint64 MojoPlatform_geteuid(void)
{
return (uint64) geteuid();
} // MojoPlatform_geteuid


uint64 MojoPlatform_getgid(void)
{
return (uint64) getgid();
} // MojoPlatform_getgid


static void signal_catcher(int sig)
{
static boolean first_shot = true;
Expand Down
19 changes: 19 additions & 0 deletions platform_windows.c
Expand Up @@ -1586,6 +1586,25 @@ void MojoPlatform_dlclose(void *_lib)
} // MojoPlatform_dlclose


uint64 MojoPlatform_getuid(void)
{
return 0; // !!! FIXME
} // MojoPlatform_getuid


uint64 MojoPlatform_geteuid(void)
{
return 0; // !!! FIXME
} // MojoPlatform_geteuid


uint64 MojoPlatform_getgid(void)
{
return 0; // !!! FIXME
} // MojoPlatform_getgid



// Get OS info and save the important parts.
// Returns non-zero if successful, otherwise it returns zero on failure.
static boolean getOSInfo(void)
Expand Down

0 comments on commit b97c20f

Please sign in to comment.