2011-09-18 17:21:11 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_c_system.c
|
2011-09-18 17:21:11 +02:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2011-09-18 17:21:11 +02:00
|
|
|
|
2011-12-31 03:39:20 +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
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2011-09-18 17:21:11 +02:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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.
|
2011-09-18 17:21:11 +02:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_SYSTEM_C
|
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-09-09 08:59:03 +02:00
|
|
|
#include <syslog.h>
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
2014-09-09 08:59:03 +02:00
|
|
|
#include "gb_common_case.h"
|
2011-09-18 17:21:11 +02:00
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_class.h"
|
2013-01-05 00:24:37 +01:00
|
|
|
#include "gbx_date.h"
|
2011-09-18 17:21:11 +02:00
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_extern.h"
|
|
|
|
#include "gbx_object.h"
|
2012-02-05 17:18:57 +01:00
|
|
|
#include "gbx_c_process.h"
|
2011-09-18 17:21:11 +02:00
|
|
|
#include "gbx_c_system.h"
|
|
|
|
|
2014-09-09 08:59:03 +02:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
const char *prefix;
|
|
|
|
int prio;
|
|
|
|
}
|
|
|
|
SYSLOG_PREFIX;
|
|
|
|
|
|
|
|
static const SYSLOG_PREFIX _syslog_prefix[] =
|
|
|
|
{
|
|
|
|
{ "[error]", LOG_ERR },
|
|
|
|
{ "[warning]", LOG_WARNING },
|
|
|
|
{ "[notice]", LOG_NOTICE },
|
|
|
|
{ "[info]", LOG_INFO },
|
|
|
|
{ "[debug]", LOG_DEBUG },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
BEGIN_PROPERTY(User_Home)
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
GB_ReturnString(FILE_get_home());
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(User_Name)
|
|
|
|
|
|
|
|
struct passwd *info = getpwuid(getuid());
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
GB_ReturnNewZeroString(info->pw_name);
|
|
|
|
else
|
|
|
|
GB_Error((char *)E_MEMORY);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(User_Id)
|
|
|
|
|
|
|
|
GB_ReturnInteger(getuid());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(User_Group)
|
|
|
|
|
|
|
|
GB_ReturnInteger(getgid());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Language)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnNewZeroString(LOCAL_get_lang());
|
|
|
|
else
|
|
|
|
LOCAL_set_lang(GB_ToZeroString(PROP(GB_STRING)));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_FirstDayOfWeek)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnInteger(LOCAL_get_first_day_of_week());
|
|
|
|
else
|
|
|
|
LOCAL_set_first_day_of_week(VPROP(GB_INTEGER));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Charset)
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
if (LOCAL_is_UTF8)
|
|
|
|
GB_ReturnConstZeroString("UTF-8");
|
|
|
|
else
|
|
|
|
GB_ReturnString(LOCAL_encoding);
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_RightToLeft)
|
|
|
|
|
|
|
|
GB_ReturnBoolean(LOCAL_local.rtl);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Path)
|
|
|
|
|
|
|
|
GB_ReturnString(PROJECT_exec_path);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Host)
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
gethostname(buffer, 255);
|
|
|
|
GB_ReturnNewZeroString(buffer);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Domain)
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
if (getdomainname(buffer, 255))
|
|
|
|
GB_Error("Unable to retrieve domain name: &1", strerror(errno));
|
|
|
|
else
|
|
|
|
GB_ReturnNewZeroString(buffer);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_ByteOrder)
|
|
|
|
|
|
|
|
GB_ReturnInteger(EXEC_big_endian);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Backtrace)
|
|
|
|
|
|
|
|
STACK_BACKTRACE *bt = STACK_get_backtrace();
|
|
|
|
|
|
|
|
GB_ReturnObject(DEBUG_get_string_array_from_backtrace(bt));
|
|
|
|
STACK_free_backtrace(&bt);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Error)
|
|
|
|
|
|
|
|
GB_ReturnInteger(errno);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(System_GetExternSymbol, GB_STRING library; GB_STRING name)
|
|
|
|
|
|
|
|
char *library = GB_ToZeroString(ARG(library));
|
|
|
|
char *name = GB_ToZeroString(ARG(name));
|
|
|
|
void *ptr = NULL;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
if (*library && *name)
|
|
|
|
ptr = EXTERN_get_symbol(library, name);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_ReturnPointer(ptr);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-02-05 17:18:57 +01:00
|
|
|
|
|
|
|
BEGIN_PROPERTY(System_Shell)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
if (!CPROCESS_shell)
|
|
|
|
GB_ReturnConstZeroString("/bin/sh");
|
|
|
|
else
|
|
|
|
GB_ReturnString(CPROCESS_shell);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
GB_StoreString(PROP(GB_STRING), &CPROCESS_shell);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-05-22 00:13:58 +02:00
|
|
|
BEGIN_PROPERTY(System_Profile)
|
2012-02-27 00:07:14 +01:00
|
|
|
|
2012-05-22 00:13:58 +02:00
|
|
|
if (READ_PROPERTY)
|
2012-05-23 23:35:58 +02:00
|
|
|
GB_ReturnBoolean(EXEC_profile_instr);
|
|
|
|
else
|
2012-06-04 00:21:24 +02:00
|
|
|
EXEC_profile_instr = EXEC_profile && VPROP(GB_BOOLEAN);
|
2012-02-27 00:07:14 +01:00
|
|
|
|
2012-05-22 00:13:58 +02:00
|
|
|
END_PROPERTY
|
2012-02-27 00:07:14 +01:00
|
|
|
|
2012-10-15 00:42:53 +02:00
|
|
|
BEGIN_METHOD_VOID(System_Breakpoint)
|
|
|
|
|
|
|
|
BREAKPOINT();
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
BEGIN_PROPERTY(System_TimeZone)
|
|
|
|
|
2014-05-18 14:31:50 +02:00
|
|
|
GB_ReturnInteger(DATE_get_timezone());
|
2013-01-05 00:24:37 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2013-12-18 12:21:01 +01:00
|
|
|
BEGIN_PROPERTY(System_BreakOnError)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnBoolean(EXEC_break_on_error);
|
|
|
|
else if (EXEC_debug)
|
|
|
|
EXEC_break_on_error = VPROP(GB_BOOLEAN);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2014-09-09 08:59:03 +02:00
|
|
|
BEGIN_METHOD(System_Log, GB_STRING message)
|
|
|
|
|
|
|
|
static bool _opened = FALSE;
|
|
|
|
|
|
|
|
const SYSLOG_PREFIX *p;
|
|
|
|
char *msg;
|
|
|
|
int len;
|
|
|
|
int lenp;
|
|
|
|
int prio;
|
|
|
|
|
|
|
|
msg = STRING(message);
|
|
|
|
len = LENGTH(message);
|
|
|
|
prio = LOG_INFO;
|
|
|
|
|
|
|
|
for (p = _syslog_prefix; p->prefix; p++)
|
|
|
|
{
|
|
|
|
lenp = strlen(p->prefix);
|
|
|
|
if (len >= (lenp + 2) && strncasecmp(msg, p->prefix, lenp) == 0)
|
|
|
|
{
|
|
|
|
msg += lenp + 2;
|
|
|
|
len -= lenp + 2;
|
|
|
|
prio = p->prio;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0 && *msg == ' ')
|
|
|
|
msg++, len--;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_opened)
|
|
|
|
{
|
|
|
|
_opened = TRUE;
|
|
|
|
openlog(PROJECT_name, LOG_PID, LOG_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
syslog(prio, "%.*s", len, msg);
|
|
|
|
|
|
|
|
END_METHOD
|
2013-12-18 12:21:01 +01:00
|
|
|
|
2014-09-15 22:05:43 +02:00
|
|
|
BEGIN_METHOD(System_Find, GB_STRING program)
|
|
|
|
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
path = CPROCESS_search_program_in_path(GB_ToZeroString(ARG(program)));
|
|
|
|
if (!path)
|
|
|
|
GB_ReturnNull();
|
|
|
|
else
|
|
|
|
GB_ReturnNewZeroString(path);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD(System_Exist, GB_STRING program)
|
|
|
|
|
|
|
|
GB_ReturnBoolean(CPROCESS_search_program_in_path(GB_ToZeroString(ARG(program))) != NULL);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-09 08:59:03 +02:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_DESC NATIVE_User[] =
|
|
|
|
{
|
2014-09-07 23:21:14 +02:00
|
|
|
GB_DECLARE_STATIC("User"),
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("Name", "s", User_Name),
|
|
|
|
GB_STATIC_PROPERTY_READ("Id", "i", User_Id),
|
|
|
|
GB_STATIC_PROPERTY_READ("Group", "i", User_Group),
|
|
|
|
GB_STATIC_PROPERTY_READ("Home", "s", User_Home),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
GB_DESC NATIVE_System[] =
|
|
|
|
{
|
2014-09-07 23:21:14 +02:00
|
|
|
GB_DECLARE_STATIC("System"),
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("Path", "s", System_Path),
|
|
|
|
GB_CONSTANT("Version", "s", GAMBAS_VERSION_STRING),
|
2012-01-11 02:49:57 +01:00
|
|
|
GB_CONSTANT("FullVersion", "s", VERSION),
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_STATIC_PROPERTY_READ("Backtrace", "String[]", System_Backtrace),
|
2013-12-18 12:21:01 +01:00
|
|
|
GB_STATIC_PROPERTY("BreakOnError", "b", System_BreakOnError),
|
2011-09-18 17:21:11 +02:00
|
|
|
|
|
|
|
GB_STATIC_PROPERTY("Language", "s", System_Language),
|
|
|
|
GB_STATIC_PROPERTY("FirstDayOfWeek", "i", System_FirstDayOfWeek),
|
2012-02-05 17:18:57 +01:00
|
|
|
GB_STATIC_PROPERTY("Shell", "s", System_Shell),
|
2012-05-22 00:13:58 +02:00
|
|
|
GB_STATIC_PROPERTY("Profile", "b", System_Profile),
|
2013-10-01 11:12:51 +02:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_STATIC_PROPERTY_READ("RightToLeft", "b", System_RightToLeft),
|
|
|
|
GB_STATIC_PROPERTY_READ("Charset", "s", System_Charset),
|
|
|
|
GB_STATIC_PROPERTY_READ("Host", "s", System_Host),
|
|
|
|
GB_STATIC_PROPERTY_READ("Domain", "s", System_Domain),
|
|
|
|
GB_STATIC_PROPERTY_READ("ByteOrder", "i", System_ByteOrder),
|
|
|
|
GB_STATIC_PROPERTY_READ("Error", "i", System_Error),
|
2013-01-05 00:24:37 +01:00
|
|
|
GB_STATIC_PROPERTY_READ("TimeZone", "i", System_TimeZone),
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_CONSTANT("Family", "s", SYSTEM),
|
|
|
|
GB_CONSTANT("Architecture", "s", ARCHITECTURE),
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_STATIC_METHOD("GetExternSymbol", "p", System_GetExternSymbol, "(Library)s(Symbol)s"),
|
2012-10-15 00:42:53 +02:00
|
|
|
GB_STATIC_METHOD("_Breakpoint", NULL, System_Breakpoint, NULL),
|
2014-09-09 08:59:03 +02:00
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_STATIC_PROPERTY_SELF("User", "User"),
|
|
|
|
|
2014-09-09 08:59:03 +02:00
|
|
|
GB_STATIC_METHOD("Log", NULL, System_Log, "(Message)s"),
|
|
|
|
|
2014-09-15 22:05:43 +02:00
|
|
|
GB_STATIC_METHOD("Exist", "b", System_Exist, "(Program)s"),
|
|
|
|
GB_STATIC_METHOD("Find", "s", System_Find, "(Program)s"),
|
|
|
|
|
2011-09-18 17:21:11 +02:00
|
|
|
GB_END_DECLARE
|
|
|
|
};
|