2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx_c_application.c
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_APPLICATION_C
|
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
|
|
|
|
#include "gbx_object.h"
|
|
|
|
|
|
|
|
#include "gbx_c_application.h"
|
|
|
|
|
|
|
|
static GB_FUNCTION signal_func;
|
|
|
|
static bool has_signal_func;
|
|
|
|
static bool _daemon = FALSE;
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void got_signal_after(intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_Call(&signal_func, 0, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void CAPP_got_signal(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (!has_signal_func)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EVENT_post(got_signal_after, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void CAPP_init()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
CLASS *class = PROJECT_class; //CLASS_find(PROJECT_startup);
|
|
|
|
|
|
|
|
has_signal_func = GB_GetFunction(&signal_func, class, "Application_Signal", "", "") == 0;
|
|
|
|
|
|
|
|
/*printf("has_signal_func = %d\n", has_signal_func);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_path)
|
|
|
|
|
|
|
|
GB_ReturnString(PROJECT_path);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CUSER_home)
|
|
|
|
|
2008-03-19 15:32:30 +01:00
|
|
|
GB_ReturnString(PROJECT_get_home());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CUSER_name)
|
|
|
|
|
|
|
|
struct passwd *info = getpwuid(getuid());
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
GB_ReturnNewString(info->pw_name, 0);
|
|
|
|
else
|
|
|
|
GB_Error((char *)E_MEMORY);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CUSER_id)
|
|
|
|
|
|
|
|
GB_ReturnInteger(getuid());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CUSER_group)
|
|
|
|
|
|
|
|
GB_ReturnInteger(getgid());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_name)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(PROJECT_name);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_title)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(LOCAL_gettext(PROJECT_title));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Language)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnNewZeroString(LOCAL_get_lang());
|
|
|
|
else
|
|
|
|
LOCAL_set_lang(GB_ToZeroString(PROP(GB_STRING)));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Charset)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_ReturnString(LOCAL_encoding);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Rtl)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_ReturnBoolean(LOCAL_local.rtl);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_id)
|
|
|
|
|
|
|
|
GB_ReturnInt(getpid());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_dir)
|
|
|
|
|
|
|
|
GB_ReturnString(PROJECT_oldcwd);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_version)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(PROJECT_version);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_args)
|
|
|
|
|
|
|
|
static OBJECT args;
|
|
|
|
|
|
|
|
args.class = CLASS_AppArgs;
|
|
|
|
GB_ReturnObject(&args);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_args_count)
|
|
|
|
|
|
|
|
GB_ReturnInt(PROJECT_argc);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CAPPLICATION_args_get, GB_INTEGER index)
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int index = VARG(index);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
GB_Error((char *)E_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= PROJECT_argc)
|
|
|
|
GB_ReturnNull();
|
|
|
|
else
|
|
|
|
GB_ReturnConstZeroString(PROJECT_argv[index]);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CAPPLICATION_args_next)
|
|
|
|
|
|
|
|
int *index = (int*)GB_GetEnum();
|
|
|
|
|
|
|
|
if (*index >= PROJECT_argc)
|
|
|
|
GB_StopEnum();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GB_ReturnConstZeroString(PROJECT_argv[*index]);
|
|
|
|
(*index)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_env_count)
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while(environ[n])
|
|
|
|
n++;
|
|
|
|
|
|
|
|
GB_ReturnInt(n);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CAPPLICATION_env_get, GB_STRING key)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(getenv(GB_ToZeroString(ARG(key))));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CAPPLICATION_env_put, GB_STRING value; GB_STRING key)
|
|
|
|
|
|
|
|
setenv(GB_ToZeroString(ARG(key)), GB_ToZeroString(ARG(value)), 1);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CAPPLICATION_env_next)
|
|
|
|
|
|
|
|
int *index = (int*)GB_GetEnum();
|
|
|
|
char *pos;
|
|
|
|
char *key;
|
|
|
|
|
|
|
|
if (environ[*index] == NULL)
|
|
|
|
GB_StopEnum();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key = environ[*index];
|
|
|
|
pos = strchr(key, '=');
|
|
|
|
if (!pos)
|
|
|
|
GB_ReturnNull();
|
|
|
|
else
|
|
|
|
GB_ReturnConstString(key, pos - key);
|
|
|
|
(*index)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_return)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnInteger(EXEC_return_value);
|
|
|
|
else
|
|
|
|
EXEC_return_value = VPROP(GB_INTEGER) & 0xFF;
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
static void init_again(int old_pid)
|
|
|
|
{
|
2010-01-28 11:55:24 +01:00
|
|
|
char old[PATH_MAX];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
FILE_remove_temp_file();
|
2008-01-06 21:06:44 +01:00
|
|
|
snprintf(old, sizeof(old),FILE_TEMP_DIR, getuid(), old_pid);
|
|
|
|
rename(old, FILE_make_temp(NULL, NULL));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CAPPLICATION_daemon)
|
|
|
|
|
|
|
|
int old_pid;
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnBoolean(_daemon);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!_daemon && VPROP(GB_BOOLEAN))
|
|
|
|
{
|
|
|
|
old_pid = getpid();
|
|
|
|
if (daemon(FALSE, FALSE))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
// Argh! daemon() changes the current process id...
|
|
|
|
_daemon = TRUE;
|
|
|
|
init_again(old_pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_ReturnString(PROJECT_exec_path);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Version)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
//ERROR_deprecated("System.Version");
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_ReturnInteger(GAMBAS_VERSION);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Host)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
gethostname(buffer, 255);
|
|
|
|
GB_ReturnNewZeroString(buffer);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Domain)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
getdomainname(buffer, 255);
|
|
|
|
GB_ReturnNewZeroString(buffer);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_ByteOrder)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_ReturnInteger(EXEC_big_endian);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Backtrace)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
DEBUG_BACKTRACE *bt = DEBUG_backtrace();
|
|
|
|
|
|
|
|
GB_ReturnObject(DEBUG_get_string_array_from_backtrace(bt));
|
|
|
|
DEBUG_free_backtrace(&bt);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Error)
|
2009-11-15 15:13:52 +01:00
|
|
|
|
|
|
|
GB_ReturnInteger(errno);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
BEGIN_PROPERTY(System_Family)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(SYSTEM);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Architecture)
|
|
|
|
|
|
|
|
GB_ReturnConstZeroString(ARCHITECTURE);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_AppArgs[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE(".ApplicationArgs", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("Count", "i", CAPPLICATION_args_count),
|
|
|
|
GB_STATIC_METHOD("_get", "s", CAPPLICATION_args_get, "(Index)i"),
|
|
|
|
GB_STATIC_METHOD("_next", "s", CAPPLICATION_args_next, NULL),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_AppEnv[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE(".ApplicationEnv", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("Count", "i", CAPPLICATION_env_count),
|
|
|
|
GB_STATIC_METHOD("_get", "s", CAPPLICATION_env_get, "(Key)s"),
|
|
|
|
GB_STATIC_METHOD("_put", NULL, CAPPLICATION_env_put, "(Value)s(Key)s"),
|
|
|
|
GB_STATIC_METHOD("_next", "s", CAPPLICATION_env_next, NULL),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_App[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE("Application", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_SELF("Args", ".ApplicationArgs"),
|
|
|
|
GB_STATIC_PROPERTY_SELF("Env", ".ApplicationEnv"),
|
|
|
|
GB_STATIC_PROPERTY_READ("Path", "s", CAPPLICATION_path),
|
|
|
|
GB_STATIC_PROPERTY_READ("Name", "s", CAPPLICATION_name),
|
|
|
|
GB_STATIC_PROPERTY_READ("Title", "s", CAPPLICATION_title),
|
|
|
|
GB_STATIC_PROPERTY_READ("Id", "i", CAPPLICATION_id),
|
|
|
|
GB_STATIC_PROPERTY_READ("Handle", "i", CAPPLICATION_id),
|
|
|
|
GB_STATIC_PROPERTY_READ("Version", "s", CAPPLICATION_version),
|
|
|
|
GB_STATIC_PROPERTY_READ("Dir", "i", CAPPLICATION_dir),
|
|
|
|
GB_STATIC_PROPERTY("Return", "i", CAPPLICATION_return),
|
|
|
|
GB_STATIC_PROPERTY("Daemon", "b", CAPPLICATION_daemon),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_System[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE("System", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
GB_STATIC_PROPERTY("Language", "s", System_Language),
|
|
|
|
GB_STATIC_PROPERTY_READ("RightToLeft", "b", System_Rtl),
|
|
|
|
GB_STATIC_PROPERTY_READ("Charset", "s", System_Charset),
|
|
|
|
GB_STATIC_PROPERTY_READ("Path", "s", System_Path),
|
|
|
|
GB_STATIC_PROPERTY_READ("Version", "s", System_Version),
|
|
|
|
GB_STATIC_PROPERTY_READ("Host", "s", System_Host),
|
|
|
|
GB_STATIC_PROPERTY_READ("Domain", "s", System_Domain),
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_STATIC_PROPERTY_SELF("User", "User"),
|
2010-02-03 16:48:37 +01:00
|
|
|
GB_STATIC_PROPERTY_READ("ByteOrder", "i", System_ByteOrder),
|
|
|
|
GB_STATIC_PROPERTY_READ("Backtrace", "String[]", System_Backtrace),
|
|
|
|
GB_STATIC_PROPERTY_READ("Error", "i", System_Error),
|
|
|
|
GB_STATIC_PROPERTY_READ("Family", "s", System_Family),
|
|
|
|
GB_STATIC_PROPERTY_READ("Architecture", "s", System_Architecture),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_User[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE("User", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("Name", "s", CUSER_name),
|
|
|
|
GB_STATIC_PROPERTY_READ("Id", "i", CUSER_id),
|
|
|
|
GB_STATIC_PROPERTY_READ("Group", "i", CUSER_group),
|
|
|
|
GB_STATIC_PROPERTY_READ("Home", "s", CUSER_home),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|