2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx.c
|
|
|
|
|
|
|
|
Interpreter startup
|
|
|
|
|
|
|
|
(c) 2000-2007 Benoit Minisini <gambas@users.sourceforge.net>
|
|
|
|
|
|
|
|
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 1, or (at your option)
|
|
|
|
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.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#ifdef __GNU_LIBRARY__
|
|
|
|
//#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_stack.h"
|
|
|
|
#include "gbx_debug.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gbx_component.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
#include "gbx_watch.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_extern.h"
|
|
|
|
#include "gbx_eval.h"
|
|
|
|
#include "gbx_subr.h"
|
|
|
|
#include "gbx_math.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
|
|
|
|
#include "gbx_api.h"
|
|
|
|
|
|
|
|
#include "gbx_c_file.h"
|
|
|
|
#include "gbx_c_application.h"
|
|
|
|
|
|
|
|
extern void _exit(int) NORETURN;
|
2009-02-06 13:05:39 +01:00
|
|
|
FILE *log_file;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static bool _welcome = FALSE;
|
|
|
|
|
|
|
|
static void init(const char *file)
|
|
|
|
{
|
|
|
|
COMPONENT_init();
|
|
|
|
FILE_init();
|
|
|
|
EXEC_init();
|
|
|
|
CLASS_init();
|
|
|
|
CFILE_init();
|
|
|
|
WATCH_init();
|
|
|
|
MATH_init();
|
|
|
|
PROJECT_init(file);
|
|
|
|
DEBUG_init();
|
|
|
|
|
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
DEBUG.Welcome();
|
|
|
|
DEBUG.Main(FALSE);
|
|
|
|
}
|
|
|
|
_welcome = TRUE;
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
PROJECT_load(); // Call STACK_init()
|
|
|
|
else
|
|
|
|
STACK_init();
|
|
|
|
|
|
|
|
LOCAL_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void my_exit(int ret)
|
|
|
|
{
|
|
|
|
LOCAL_exit();
|
|
|
|
COMPONENT_exit();
|
|
|
|
EXTERN_exit();
|
2009-02-06 13:07:52 +01:00
|
|
|
//fclose(log_file);
|
2007-12-30 17:41:49 +01:00
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-26 11:49:06 +02:00
|
|
|
static void main_exit(bool silent)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-12-31 16:55:40 +01:00
|
|
|
STREAM_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
OBJECT_exit();
|
2008-07-16 12:12:13 +02:00
|
|
|
CLASS_clean_up(silent);
|
|
|
|
SUBR_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
DEBUG_exit();
|
2008-07-10 23:49:11 +02:00
|
|
|
CFILE_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
WATCH_exit();
|
2008-07-16 12:12:13 +02:00
|
|
|
CLASS_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
COMPONENT_exit();
|
|
|
|
EXTERN_exit();
|
|
|
|
PROJECT_exit();
|
|
|
|
LOCAL_exit();
|
|
|
|
EVENT_exit();
|
|
|
|
FILE_exit();
|
|
|
|
STACK_exit();
|
2008-01-27 15:00:04 +01:00
|
|
|
ERROR_exit();
|
2009-02-06 18:40:15 +01:00
|
|
|
STRING_exit();
|
2009-02-06 13:07:52 +01:00
|
|
|
//fclose(log_file);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
//CLASS *class = NULL;
|
|
|
|
CLASS_DESC_METHOD *startup = NULL;
|
|
|
|
|
|
|
|
int i, n;
|
|
|
|
char *file = ".";
|
|
|
|
bool nopreload = FALSE;
|
|
|
|
|
2009-02-06 13:05:39 +01:00
|
|
|
//char log_path[256];
|
|
|
|
//sprintf(log_path, "/tmp/gambas-%d.log", getuid());
|
|
|
|
//log_file = freopen(log_path, "w+", stderr);
|
|
|
|
//fprintf(stderr, "Fichier log Gambas\n");
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
MEMORY_init();
|
|
|
|
COMMON_init();
|
|
|
|
//STRING_init();
|
|
|
|
|
|
|
|
if (strcmp(argv[0], "gbr" GAMBAS_VERSION_STRING) == 0)
|
|
|
|
{
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gbr" GAMBAS_VERSION_STRING ": no archive file.\n");
|
|
|
|
my_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXEC_arch = TRUE;
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-p") == 0)
|
|
|
|
n = 2;
|
|
|
|
else
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
file = argv[n];
|
|
|
|
if (n == 1)
|
|
|
|
LIBRARY_preload(file, argv);
|
|
|
|
|
|
|
|
for (i = 0; i < (argc - n); i++)
|
|
|
|
argv[i] = argv[i + n];
|
|
|
|
|
|
|
|
argc -= n;
|
|
|
|
}
|
|
|
|
/*else if (argc >= 2 && strcmp(argv[1], "-x") == 0)
|
|
|
|
{
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gbx: no archive file.\n");
|
|
|
|
my_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXEC_arch = TRUE;
|
|
|
|
|
|
|
|
if (strcmp(argv[2], "-p") == 0)
|
|
|
|
n = 3;
|
|
|
|
else
|
|
|
|
n = 2;
|
|
|
|
|
|
|
|
file = argv[n];
|
|
|
|
if (n == 2)
|
|
|
|
LIBRARY_preload(file, argv);
|
|
|
|
|
|
|
|
for (i = 1; i < (argc - n); i++)
|
|
|
|
argv[i] = argv[i + n];
|
|
|
|
|
|
|
|
argc -= n;
|
|
|
|
}*/
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"\n"
|
|
|
|
"GAMBAS Interpreter version " VERSION " " __DATE__ " " __TIME__ "\n"
|
|
|
|
COPYRIGHT
|
|
|
|
"Usage: gbx" GAMBAS_VERSION_STRING " [options] [<project file>] -- ...\n"
|
|
|
|
" gbx" GAMBAS_VERSION_STRING " -e <expression>\n\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -V --version display version\n"
|
|
|
|
" -h --help display this help\n"
|
|
|
|
" -e evaluate an expression\n"
|
|
|
|
" -g enter debugging mode\n"
|
|
|
|
#if DO_PRELOADING
|
|
|
|
" -p disable preloading\n"
|
|
|
|
#endif
|
2008-03-19 15:32:30 +01:00
|
|
|
" -k do not unload shared libraries\n"
|
2007-12-30 17:41:49 +01:00
|
|
|
" -x execute an archive\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
my_exit(0);
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))
|
|
|
|
{
|
|
|
|
printf(VERSION "\n");
|
|
|
|
my_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
if (argc == 3 && !strcmp(argv[1], "-e"))
|
|
|
|
{
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
init(NULL);
|
|
|
|
EVAL_string(argv[2]);
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
2008-01-25 16:01:02 +01:00
|
|
|
if (ERROR_current->info.code && ERROR_current->info.code != E_ABORT)
|
2007-12-30 17:41:49 +01:00
|
|
|
ERROR_print_at(stderr, TRUE, TRUE);
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[i], "-g") == 0)
|
|
|
|
{
|
|
|
|
EXEC_debug = TRUE;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "-f") == 0)
|
|
|
|
{
|
|
|
|
EXEC_fifo = TRUE;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "-p") == 0)
|
|
|
|
{
|
|
|
|
nopreload = TRUE;
|
|
|
|
}
|
2008-03-19 15:32:30 +01:00
|
|
|
else if (strcmp(argv[i], "-k") == 0)
|
|
|
|
{
|
|
|
|
EXEC_keep_library = TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strcmp(argv[i], "--"))
|
|
|
|
{
|
|
|
|
file = argv[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < argc)
|
|
|
|
{
|
|
|
|
if (file && strcmp(argv[i], "--"))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gbx" GAMBAS_VERSION_STRING ": too many project files.\n");
|
|
|
|
my_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = i;
|
|
|
|
|
|
|
|
if (!nopreload)
|
|
|
|
LIBRARY_preload(file, argv);
|
|
|
|
|
|
|
|
for (i = 1; i <= (argc - n); i++)
|
|
|
|
argv[i] = argv[i + n - 1];
|
|
|
|
|
|
|
|
argc -= n - 1;
|
|
|
|
|
|
|
|
//printf("argc = %d\n", argc);
|
|
|
|
/*for (i = 0; i < argc; i++)
|
|
|
|
fprintf(stderr, "argv[%d] = '%s'\n", i, argv[i]);
|
|
|
|
fprintf(stderr, "\n");*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
init(file);
|
|
|
|
|
|
|
|
if (!EXEC_arch)
|
|
|
|
argv[0] = PROJECT_name;
|
|
|
|
|
|
|
|
HOOK(main)(&argc, argv);
|
|
|
|
EXEC_main_hook_done = TRUE;
|
|
|
|
|
|
|
|
/* Startup class */
|
|
|
|
CLASS_load(PROJECT_class);
|
|
|
|
startup = (CLASS_DESC_METHOD *)CLASS_get_symbol_desc_kind(PROJECT_class, "main", CD_STATIC_METHOD, 0);
|
|
|
|
if (startup == NULL)
|
|
|
|
THROW(E_MAIN);
|
|
|
|
|
|
|
|
CAPP_init(); /* needs startup class */
|
|
|
|
CFILE_init_watch();
|
|
|
|
|
|
|
|
PROJECT_argc = argc;
|
|
|
|
PROJECT_argv = argv;
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
if (!_welcome)
|
|
|
|
DEBUG.Main(TRUE);
|
|
|
|
DEBUG.Main(TRUE);
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-25 16:01:02 +01:00
|
|
|
if (ERROR->info.code && ERROR->info.code != E_ABORT)
|
2007-12-30 17:41:49 +01:00
|
|
|
ERROR_print();
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
EXEC.class = PROJECT_class;
|
|
|
|
EXEC.object = NULL;
|
|
|
|
EXEC.drop = TRUE;
|
|
|
|
EXEC.nparam = 0;
|
|
|
|
|
|
|
|
if (FUNCTION_is_native(startup))
|
|
|
|
{
|
|
|
|
EXEC.native = TRUE;
|
|
|
|
EXEC.use_stack = FALSE;
|
|
|
|
EXEC.desc = startup;
|
|
|
|
|
|
|
|
EXEC_native();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EXEC.native = FALSE;
|
2008-01-17 22:39:26 +01:00
|
|
|
EXEC.index = (int)(intptr_t)startup->exec;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
EXEC_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
HOOK_DEFAULT(loop, WATCH_loop)();
|
|
|
|
|
|
|
|
EVENT_check_post();
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
2008-01-25 16:01:02 +01:00
|
|
|
if (ERROR->info.code && ERROR->info.code != E_ABORT)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
DEBUG.Main(TRUE);
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_print();
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2008-06-26 11:49:06 +02:00
|
|
|
main_exit(FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
MEMORY_exit();
|
|
|
|
|
|
|
|
fflush(NULL);
|
|
|
|
|
|
|
|
exit(EXEC_return_value);
|
|
|
|
}
|
|
|
|
|