2007-12-30 17:41:49 +01:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
|
gbx_library.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
|
(c) 2000-2013 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
|
2011-06-03 02:51:09 +02:00
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#define __GBX_LIBRARY_C
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
|
#include "gb_replace.h"
|
|
|
|
|
#include "gb_magic.h"
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
|
|
#include "gbx_class.h"
|
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
|
#include "gbx_event.h"
|
|
|
|
|
#include "gbx_stack.h"
|
|
|
|
|
#include "gb_file.h"
|
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
|
#include "gbx_project.h"
|
|
|
|
|
#include "gbx_api.h"
|
|
|
|
|
|
|
|
|
|
#include "gbx_string.h"
|
|
|
|
|
#include "gbx_object.h"
|
|
|
|
|
#include "gbx_component.h"
|
|
|
|
|
|
|
|
|
|
#include "gbx_library.h"
|
|
|
|
|
|
2009-12-24 16:09:08 +01:00
|
|
|
|
//#define DEBUG
|
2007-12-30 17:41:49 +01:00
|
|
|
|
//#define DEBUG_PRELOAD
|
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
|
// Maximum size of a project or startup file
|
|
|
|
|
// This avoids reading too much in the archive file!
|
|
|
|
|
|
|
|
|
|
#define MAX_SIZE 2048
|
|
|
|
|
|
|
|
|
|
int _bytes_read = 0;
|
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
#if 0
|
|
|
|
|
static lt_ptr library_malloc(size_t size)
|
|
|
|
|
{
|
|
|
|
|
lt_ptr ptr;
|
|
|
|
|
ALLOC(&ptr, size, "library_malloc");
|
|
|
|
|
printf("library_malloc: %d -> %p\n", size, ptr);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void library_free(lt_ptr ptr)
|
|
|
|
|
{
|
|
|
|
|
printf("library_free -> %p\n", ptr);
|
|
|
|
|
FREE(&ptr, "library_free");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void *get_symbol(LIBRARY *lib, const char *symbol, bool err)
|
|
|
|
|
{
|
|
|
|
|
void *sym;
|
|
|
|
|
|
|
|
|
|
sym = lt_dlsym(lib->handle, symbol);
|
|
|
|
|
if (sym == NULL && err)
|
|
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
|
strcpy(COMMON_buffer, lt_dlerror());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
lt_dlclose(lib->handle);
|
|
|
|
|
lib->handle = NULL;
|
|
|
|
|
THROW(E_LIBRARY, lib->name, COMMON_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
|
static void copy_interface(void **src, void **dst)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
|
if (!*src)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
*dst++ = *src++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
/*if (putenv("LD_BIND_NOW=true"))
|
|
|
|
|
ERROR_panic("Cannot set LD_BIND_NOW: &1", strerror(errno));
|
|
|
|
|
|
|
|
|
|
if (putenv("KDE_MALLOC=0"))
|
|
|
|
|
ERROR_panic("Cannot set KDE_MALLOC: &1", strerror(errno));*/
|
|
|
|
|
|
|
|
|
|
/*lt_dlmalloc = library_malloc;
|
|
|
|
|
lt_dlfree = library_free;*/
|
|
|
|
|
|
|
|
|
|
#ifndef DONT_USE_LTDL
|
|
|
|
|
if (lt_dlinit())
|
|
|
|
|
ERROR_panic("Cannot initialize plug-in management: %s", lt_dlerror());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
#ifndef DONT_USE_LTDL
|
|
|
|
|
lt_dlexit();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_get_interface(LIBRARY *lib, int version, void *iface)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
char symbol[32];
|
|
|
|
|
int i, len;
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
len = strlen(lib->name);
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
c = toupper(lib->name[i]);
|
2010-03-06 04:17:47 +01:00
|
|
|
|
if (!isalnum((unsigned char)c))
|
2007-12-30 17:41:49 +01:00
|
|
|
|
c = '_';
|
|
|
|
|
|
|
|
|
|
symbol[i] = c;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
sprintf(&symbol[len], "_%d", version);
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
|
copy_interface((void **)get_symbol(lib, symbol, TRUE), iface);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
|
bool LIBRARY_get_interface_by_name(const char *name, int version, void *iface)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
COMPONENT *comp;
|
|
|
|
|
|
|
|
|
|
comp = COMPONENT_find(name);
|
|
|
|
|
if (!comp || !comp->library)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
LIBRARY_get_interface(comp->library, version, iface);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
LIBRARY *LIBRARY_create(const char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
LIBRARY *lib;
|
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
|
ALLOC_ZERO(&lib, sizeof(LIBRARY));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
lib->handle = NULL;
|
|
|
|
|
lib->name = name;
|
|
|
|
|
|
|
|
|
|
/*if (name)
|
|
|
|
|
{
|
|
|
|
|
lib->persistent = FALSE;
|
|
|
|
|
lib->preload = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lib->persistent = TRUE;
|
|
|
|
|
lib->preload = TRUE;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
return lib;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_delete(LIBRARY *lib)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
LIBRARY_unload(lib);
|
2013-03-30 00:33:01 +01:00
|
|
|
|
FREE(&lib);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-01-10 14:40:48 +01:00
|
|
|
|
int LIBRARY_load(LIBRARY *lib)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
int (*func)();
|
2009-12-07 18:14:32 +01:00
|
|
|
|
void **iface;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
GB_DESC **desc;
|
|
|
|
|
char *path;
|
2010-01-10 14:40:48 +01:00
|
|
|
|
int order = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
2009-12-24 16:09:08 +01:00
|
|
|
|
if (lib->handle)
|
2010-01-10 14:40:48 +01:00
|
|
|
|
return 0;
|
2009-12-24 16:09:08 +01:00
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
clock_t t = clock();
|
|
|
|
|
fprintf(stderr, "Loading library %s\n", lib->name);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
path = FILE_buffer();
|
2008-01-17 22:39:26 +01:00
|
|
|
|
sprintf(path, LIB_PATTERN, COMPONENT_path, lib->name);
|
2008-08-08 16:41:57 +02:00
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
|
//if (!FILE_exist(path))
|
|
|
|
|
// sprintf(path, LIB_PATTERN, COMPONENT_user_path, lib->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
#ifndef DONT_USE_LTDL
|
2008-01-03 21:44:46 +01:00
|
|
|
|
/* no more available in libltld ?
|
2007-12-30 17:41:49 +01:00
|
|
|
|
lt_dlopen_flag = RTLD_LAZY;
|
2008-01-03 21:44:46 +01:00
|
|
|
|
*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
lib->handle = lt_dlopenext(path);
|
|
|
|
|
#else
|
|
|
|
|
lib->handle = dlopen(path, RTLD_LAZY);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (lib->handle == NULL)
|
2009-07-15 01:25:52 +02:00
|
|
|
|
THROW(E_LIBRARY, lib->name, lt_dlerror());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
func = get_symbol(lib, LIB_INIT, TRUE);
|
|
|
|
|
|
|
|
|
|
/* Interface de Gambas */
|
|
|
|
|
|
2012-08-25 17:13:39 +02:00
|
|
|
|
iface = get_symbol(lib, LIB_GAMBAS_PTR, FALSE);
|
|
|
|
|
if (iface)
|
|
|
|
|
*((void **)iface) = &GAMBAS_Api;
|
|
|
|
|
else
|
2009-08-28 23:42:11 +02:00
|
|
|
|
{
|
|
|
|
|
iface = get_symbol(lib, LIB_GAMBAS, TRUE);
|
2009-12-07 18:14:32 +01:00
|
|
|
|
copy_interface(GAMBAS_Api, iface);
|
2009-08-28 23:42:11 +02:00
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
/* Signal function */
|
|
|
|
|
lib->signal = (void(*)())get_symbol(lib, LIB_SIGNAL, FALSE);
|
|
|
|
|
lib->info = (int(*)())get_symbol(lib, LIB_INFO, FALSE);
|
|
|
|
|
|
|
|
|
|
/* Initialisation */
|
2010-01-10 14:40:48 +01:00
|
|
|
|
order = (*func)();
|
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
/* Déclaration des classes */
|
|
|
|
|
desc = get_symbol(lib, LIB_CLASS, FALSE);
|
|
|
|
|
if (desc)
|
|
|
|
|
LIBRARY_declare(desc);
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
fprintf(stderr, "Library %s loaded ", lib->name);
|
|
|
|
|
fprintf(stderr, "in %g s\n", ((double)(clock() - t) / CLOCKS_PER_SEC));
|
|
|
|
|
#endif
|
2010-01-10 14:40:48 +01:00
|
|
|
|
|
|
|
|
|
return order;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 00:18:58 +02:00
|
|
|
|
void LIBRARY_after_init(LIBRARY *lib)
|
|
|
|
|
{
|
|
|
|
|
void (*func)();
|
|
|
|
|
|
|
|
|
|
func = get_symbol(lib, LIB_AFTER_INIT, FALSE);
|
|
|
|
|
if (func) (*func)();
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-27 22:52:08 +02:00
|
|
|
|
void LIBRARY_exec(LIBRARY *lib, int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
void (*func)();
|
|
|
|
|
|
|
|
|
|
func = get_symbol(lib, LIB_MAIN, FALSE);
|
|
|
|
|
if (func)
|
|
|
|
|
(*func)(argc, argv);
|
|
|
|
|
}
|
|
|
|
|
|
[INTERPRETER]
* NEW: Add a new GB.Component.Declare() API, that allows a component to
dynamically register a class description.
[INFORMER]
* NEW: A component now can declare classes through a GB_OPTIONAL_CLASSES
public symbol. Theses classes are included into the information file,
but are not automatically loaded as the ones defined through the
GB_CLASSES symbol.
[GB.FORM.STATUSICON]
* NEW: This component has been removed and replaced by gb.dbus.trayicon.
* NEW: This component now provide TrayIcon and TrayIcons classes that have
exactly the same interface as the old ones.
[GB.DBUS]
* NEW: Remove the now useless DBusConnection.Raise(). The DBusConnection
Register() and Unregister() are kept for backward-compatibility reason.
* NEW: Add DBus.Raise() and DBus[].Raise() to raise signals. At the moment,
there is no difference between the two methods, as there no mean to
indicate the sender raising a signal. I don't know yet how DBus decides
which service sends a signal!
[GB.DBUS.TRAYICON]
* NEW: This component implements the tray icon DBus protocol. It is
automatically loaded by the GUI components.
[GB.GTK]
* NEW: Load the 'gb.dbus' and 'gb.dbus.trayicon' components if the current
desktop is KDE >= 4. Otherwise, use the old TrayIcon implementation.
* BUG: Fix the old TrayIcon implementation, so that it matches the QT one.
[GB.GTK3]
* NEW: Load the 'gb.dbus' and 'gb.dbus.trayicon' components if the current
desktop is KDE >= 4. Otherwise, use the old TrayIcon implementation.
* BUG: Fix the component name in deprecation warning messages.
* BUG: Fix the old TrayIcon implementation, so that it matches the QT one.
[GB.QT4]
* NEW: Load the 'gb.dbus' and 'gb.dbus.trayicon' components if the current
desktop is KDE >= 4. Otherwise, use the old TrayIcon implementation.
[GB.QT5]
* NEW: Load the 'gb.dbus' and 'gb.dbus.trayicon' components if the current
desktop is KDE >= 4. Otherwise, use the old TrayIcon implementation.
git-svn-id: svn://localhost/gambas/trunk@7154 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2015-06-29 03:46:51 +02:00
|
|
|
|
void LIBRARY_declare_one(GB_DESC *desc)
|
|
|
|
|
{
|
|
|
|
|
CLASS_find_global(desc->name);
|
|
|
|
|
if (CLASS_register(desc) == NULL)
|
|
|
|
|
THROW(E_REGISTER, desc->name);
|
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_declare(GB_DESC **desc)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
GB_DESC **p;
|
|
|
|
|
|
|
|
|
|
p = desc;
|
|
|
|
|
while (*p != NULL)
|
|
|
|
|
{
|
|
|
|
|
CLASS_find_global((*p)->name);
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = desc;
|
|
|
|
|
while (*p != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (CLASS_register(*p) == NULL)
|
|
|
|
|
THROW(E_REGISTER, (*p)->name);
|
|
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
void LIBRARY_unload(LIBRARY *lib)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
{
|
|
|
|
|
void (*gambas_exit)();
|
|
|
|
|
|
|
|
|
|
if (lib->handle == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Pas de lib<69>ation des classes pr<70>harg<72> ! */
|
|
|
|
|
|
|
|
|
|
/* V<>ification qu'aucune classe de la librairie n'est instanci<63> ! */
|
|
|
|
|
|
|
|
|
|
gambas_exit = lt_dlsym(lib->handle, LIB_EXIT);
|
|
|
|
|
if (gambas_exit != NULL)
|
|
|
|
|
(*gambas_exit)();
|
|
|
|
|
|
|
|
|
|
if (lib->persistent)
|
|
|
|
|
{
|
|
|
|
|
gambas_exit = lt_dlsym(lib->handle, "_fini");
|
|
|
|
|
if (gambas_exit != NULL)
|
|
|
|
|
(*gambas_exit)();
|
|
|
|
|
}
|
|
|
|
|
else
|
2012-05-26 00:24:13 +02:00
|
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
|
lt_dlclose(lib->handle);
|
2012-05-26 00:24:13 +02:00
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
lib->handle = NULL;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
printf("Unloading library %s\n", lib->name);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|