2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
gbx_component.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
|
|
|
|
2012-10-27 02:28:55 +02: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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02: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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02: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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_COMPONENT_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_file.h"
|
|
|
|
#include "gbx_debug.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_local.h"
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
#include "gbx_library.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
|
|
|
|
#include "gbx_component.h"
|
|
|
|
|
|
|
|
|
2010-01-10 14:40:48 +01:00
|
|
|
//#define DEBUG_COMP
|
2007-12-30 17:41:49 +01:00
|
|
|
/*#define DEBUG_PRELOAD*/
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
COMPONENT *COMPONENT_current = NULL;
|
|
|
|
COMPONENT *COMPONENT_main;
|
|
|
|
int COMPONENT_count = 0;
|
|
|
|
char *COMPONENT_path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static COMPONENT *_component_list = NULL;
|
2012-10-27 02:28:55 +02:00
|
|
|
static COMPONENT *_component_load = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-09-27 22:52:08 +02:00
|
|
|
static bool _load_all = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
LIBRARY_init();
|
|
|
|
ARCHIVE_init();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
2010-01-10 14:40:48 +01:00
|
|
|
int order;
|
|
|
|
int max_order = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
_component_load = NULL;
|
|
|
|
|
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
2010-01-10 14:40:48 +01:00
|
|
|
if (comp->order > max_order)
|
|
|
|
max_order = comp->order;
|
|
|
|
}
|
|
|
|
|
2012-05-26 00:24:13 +02:00
|
|
|
// if order < 0, the component is not unloaded
|
|
|
|
|
2010-01-10 14:40:48 +01:00
|
|
|
for (order = 0; order <= max_order; order++)
|
|
|
|
{
|
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
if (comp->order == order)
|
|
|
|
COMPONENT_unload(comp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 00:24:13 +02:00
|
|
|
/*LIST_for_each(comp, _component_list)
|
2010-01-10 14:40:48 +01:00
|
|
|
{
|
|
|
|
if (comp->loaded)
|
|
|
|
COMPONENT_unload(comp);
|
2012-05-26 00:24:13 +02:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
while (_component_list)
|
|
|
|
COMPONENT_delete(_component_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
LIBRARY_exit();
|
|
|
|
ARCHIVE_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
STRING_free(&COMPONENT_path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_load_all(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
COMPONENT_create("gb.eval");
|
|
|
|
COMPONENT_create("gb.debug");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-09-27 22:52:08 +02:00
|
|
|
_load_all = TRUE;
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
COMPONENT_load(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
_load_all = FALSE;
|
2012-09-27 22:52:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void COMPONENT_load_all_finish(void)
|
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
2012-09-27 22:52:08 +02:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
LIST_for_each_name(comp, _component_load, load)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "load exported class: %s\n", comp->name);
|
|
|
|
ARCHIVE_load_exported_class(comp->archive);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
COMPONENT *COMPONENT_find(const char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* A null name is the main archive */
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
if (strcmp(comp->name, name) == 0)
|
|
|
|
return comp;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
bool COMPONENT_exist(const char *name)
|
|
|
|
{
|
|
|
|
return COMPONENT_find(name) != NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
COMPONENT *COMPONENT_create(const char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
|
|
|
char *path = NULL;
|
|
|
|
bool can_archive;
|
2010-04-25 12:46:57 +02:00
|
|
|
bool library = FALSE;
|
2010-07-29 12:19:10 +02:00
|
|
|
bool same_name_as_project = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (*name == '/') // user library
|
|
|
|
{
|
|
|
|
library = TRUE;
|
2010-04-26 11:36:47 +02:00
|
|
|
path = (char *)name;
|
2010-04-25 12:46:57 +02:00
|
|
|
name = FILE_get_name(name);
|
|
|
|
}
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
comp = COMPONENT_find(name);
|
|
|
|
if (comp)
|
|
|
|
return comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC_ZERO(&comp, sizeof(COMPONENT));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
comp->class = CLASS_Component;
|
|
|
|
comp->ref = 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
comp->name = STRING_new_zero(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (library)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-04-25 12:46:57 +02:00
|
|
|
comp->archive = ARCHIVE_create(name, path);
|
2011-08-21 04:27:16 +02:00
|
|
|
comp->user = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2010-04-25 12:46:57 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Don't load the archive if it has the same name as the project
|
2010-07-29 12:19:10 +02:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (PROJECT_name)
|
2010-07-29 12:19:10 +02:00
|
|
|
same_name_as_project = strcmp(name, PROJECT_name) == 0;
|
|
|
|
|
|
|
|
can_archive = !same_name_as_project;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-23 18:02:35 +02:00
|
|
|
// System wide component
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
path = FILE_buffer();
|
2010-04-25 12:46:57 +02:00
|
|
|
sprintf(path, LIB_PATTERN, COMPONENT_path, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (FILE_exist(path))
|
2010-04-25 12:46:57 +02:00
|
|
|
comp->library = LIBRARY_create(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (can_archive)
|
|
|
|
{
|
|
|
|
path = FILE_buffer();
|
|
|
|
sprintf(path, ARCH_PATTERN, COMPONENT_path, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (FILE_exist(path))
|
|
|
|
comp->archive = ARCHIVE_create(name, NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
//fprintf(stderr, "insert %s\n", comp->name);
|
|
|
|
LIST_insert(&_component_list, comp, &comp->list);
|
|
|
|
COMPONENT_count++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (!comp->library && !comp->archive && !same_name_as_project)
|
2008-01-17 22:39:26 +01:00
|
|
|
{
|
|
|
|
COMPONENT_delete(comp);
|
2010-04-25 12:46:57 +02:00
|
|
|
THROW(E_LIBRARY, name, "cannot find component");
|
2008-01-17 22:39:26 +01:00
|
|
|
}
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
return comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_delete(COMPONENT *comp)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT_unload(comp);
|
|
|
|
LIST_remove(&_component_list, comp, &comp->list);
|
|
|
|
COMPONENT_count--;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->library)
|
|
|
|
LIBRARY_delete(comp->library);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->archive)
|
|
|
|
ARCHIVE_delete(comp->archive);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
STRING_free(&comp->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&comp);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_load(COMPONENT *comp)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *current;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->loaded || comp->loading)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, "Loading component %s\n", comp->name);
|
|
|
|
#endif
|
2011-08-21 04:27:16 +02:00
|
|
|
|
|
|
|
comp->loading = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
current = COMPONENT_current;
|
|
|
|
COMPONENT_current = comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->library)
|
2011-08-27 03:32:13 +02:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
comp->order = LIBRARY_load(comp->library);
|
|
|
|
comp->library->persistent = _load_all;
|
2011-08-27 03:32:13 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->archive)
|
|
|
|
{
|
|
|
|
if (_load_all)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "load later: %s\n", comp->name);
|
|
|
|
LIST_insert(&_component_load, comp, &comp->load);
|
|
|
|
}
|
|
|
|
|
|
|
|
ARCHIVE_load(comp->archive, !_load_all);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
comp->loading = FALSE;
|
2012-10-27 02:28:55 +02:00
|
|
|
comp->loaded = TRUE;
|
|
|
|
COMPONENT_current = current;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_unload(COMPONENT *comp)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
if (!comp->loaded)
|
|
|
|
return;
|
2010-01-10 14:40:48 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, "Unloading component %s [%d]\n", comp->name, comp->order);
|
|
|
|
#endif
|
2010-01-10 14:40:48 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp->library)
|
|
|
|
LIBRARY_unload(comp->library);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
/* Do not exist yet */
|
|
|
|
//if (comp->archive)
|
|
|
|
// ARCHIVE_unload(comp->archive);
|
2010-01-10 14:40:48 +01:00
|
|
|
|
|
|
|
comp->loaded = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
COMPONENT *COMPONENT_next(COMPONENT *comp)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
if (comp)
|
|
|
|
return (COMPONENT *)(comp->list.next);
|
|
|
|
else
|
|
|
|
return _component_list;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_translation_must_be_reloaded(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
|
|
|
|
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
if (comp->archive)
|
|
|
|
comp->archive->translation_loaded = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ARCHIVE_main)
|
|
|
|
ARCHIVE_main->translation_loaded = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void COMPONENT_signal(int signal, void *param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-27 02:28:55 +02:00
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
if (comp->library && comp->library->signal)
|
|
|
|
(*comp->library->signal)(signal, param);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool COMPONENT_get_info(const char *key, void **value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-10-27 02:28:55 +02:00
|
|
|
COMPONENT *comp;
|
|
|
|
|
|
|
|
LIST_for_each(comp, _component_list)
|
|
|
|
{
|
|
|
|
if (comp->library && comp->library->info)
|
|
|
|
if ((*comp->library->info)(key, value))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2012-09-27 22:52:08 +02:00
|
|
|
|
|
|
|
void COMPONENT_exec(const char *name, int argc, char **argv)
|
|
|
|
{
|
|
|
|
COMPONENT *comp;
|
|
|
|
|
|
|
|
comp = COMPONENT_create(name);
|
|
|
|
|
|
|
|
COMPONENT_load(comp);
|
|
|
|
|
|
|
|
if (comp->library)
|
|
|
|
LIBRARY_exec(comp->library, argc, argv);
|
|
|
|
}
|
|
|
|
|