2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01: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.
|
2007-12-30 17:41:49 +01: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.
|
2007-12-30 17:41:49 +01: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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-07-25 02:50:11 +02:00
|
|
|
//#define USE_PROFILE 1
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2010-02-03 16:48:37 +01:00
|
|
|
#include <stdarg.h>
|
2012-03-28 23:55:19 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2011-07-25 02:50:11 +02:00
|
|
|
#if USE_PROFILE
|
|
|
|
#include "gbx_profile.h"
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#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;
|
2010-05-22 20:02:34 +02:00
|
|
|
static bool _quit_after_main = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
static void NORETURN my_exit(int ret)
|
2010-01-20 01:37:00 +01:00
|
|
|
{
|
2011-03-03 16:05:54 +01:00
|
|
|
LOCAL_exit();
|
|
|
|
COMPONENT_exit();
|
|
|
|
EXTERN_exit();
|
2010-01-20 01:37:00 +01:00
|
|
|
//fclose(log_file);
|
2011-03-03 16:05:54 +01:00
|
|
|
exit(ret);
|
2010-01-20 01:37:00 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
static void NORETURN fatal(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
fputs(EXEC_arch ? "gbr" GAMBAS_VERSION_STRING : "gbx" GAMBAS_VERSION_STRING, stderr);
|
|
|
|
fputs(": ", stderr);
|
|
|
|
vfprintf(stderr, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
putc('\n', stderr);
|
|
|
|
my_exit(1);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static void init(const char *file)
|
|
|
|
{
|
2011-07-25 02:50:11 +02:00
|
|
|
#if USE_PROFILE
|
|
|
|
if (EXEC_profile)
|
|
|
|
PROFILE_init();
|
|
|
|
#endif
|
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
COMPONENT_init();
|
|
|
|
FILE_init();
|
|
|
|
EXEC_init();
|
|
|
|
CLASS_init();
|
|
|
|
CFILE_init();
|
|
|
|
WATCH_init();
|
|
|
|
MATH_init();
|
2007-12-30 17:41:49 +01:00
|
|
|
PROJECT_init(file);
|
2011-03-03 16:05:54 +01:00
|
|
|
DEBUG_init();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
LOCAL_init();
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (file)
|
2010-01-20 01:37:00 +01:00
|
|
|
{
|
|
|
|
if (PROJECT_load()) // Call STACK_init()
|
|
|
|
{
|
2010-02-03 16:48:37 +01:00
|
|
|
if (!strcmp(file, "."))
|
|
|
|
fatal("no project file in current directory.");
|
|
|
|
else
|
|
|
|
fatal("no project file in '%s'.", file);
|
2010-01-20 01:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
STACK_init();
|
|
|
|
|
2009-08-18 00:36:54 +02:00
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
DEBUG.Welcome();
|
|
|
|
DEBUG.Main(FALSE);
|
|
|
|
}
|
|
|
|
_welcome = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-26 11:49:06 +02:00
|
|
|
static void main_exit(bool silent)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-20 01:37:00 +01:00
|
|
|
// If the stack has not been initialized because the project could not be started, do it know
|
|
|
|
if (!SP)
|
|
|
|
STACK_init();
|
|
|
|
|
2010-08-29 22:51:10 +02:00
|
|
|
TRY
|
|
|
|
{
|
|
|
|
EXTERN_release();
|
|
|
|
STREAM_exit();
|
|
|
|
OBJECT_exit();
|
|
|
|
CLASS_clean_up(silent);
|
|
|
|
SUBR_exit();
|
|
|
|
DEBUG_exit();
|
|
|
|
CFILE_exit();
|
|
|
|
WATCH_exit();
|
2011-07-25 02:50:11 +02:00
|
|
|
#if USE_PROFILE
|
|
|
|
PROFILE_exit();
|
|
|
|
#endif
|
2010-08-29 22:51:10 +02:00
|
|
|
CLASS_exit();
|
|
|
|
COMPONENT_exit();
|
|
|
|
EXTERN_exit();
|
|
|
|
PROJECT_exit();
|
|
|
|
LOCAL_exit();
|
|
|
|
EVENT_exit();
|
|
|
|
FILE_exit();
|
|
|
|
STACK_exit();
|
|
|
|
ERROR_exit();
|
|
|
|
STRING_exit();
|
|
|
|
}
|
|
|
|
END_TRY
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 03:30:37 +01:00
|
|
|
static bool is_option(const char *arg, char option)
|
|
|
|
{
|
|
|
|
return (arg[0] == '-' && arg[1] == option && arg[2] == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_long_option(const char *arg, char option, char *long_option)
|
|
|
|
{
|
|
|
|
if (is_option(arg, option))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return (arg[0] == '-' && arg[1] == '-' && !strcmp(&arg[2], long_option));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-30 02:04:51 +02:00
|
|
|
int main(int argc, char *argv[])
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-03-03 16:05:54 +01:00
|
|
|
//CLASS *class = NULL;
|
|
|
|
CLASS_DESC_METHOD *startup = NULL;
|
|
|
|
int i, n;
|
|
|
|
char *file = NULL;
|
2011-07-26 02:28:52 +02:00
|
|
|
int ret = 0;
|
2011-03-03 16:05:54 +01:00
|
|
|
|
|
|
|
//char log_path[256];
|
|
|
|
//sprintf(log_path, "/tmp/gambas-%d.log", getuid());
|
2009-02-06 13:05:39 +01:00
|
|
|
//log_file = freopen(log_path, "w+", stderr);
|
2011-03-03 16:05:54 +01:00
|
|
|
//fprintf(stderr, "Fichier log Gambas\n");
|
2012-03-28 23:55:19 +02:00
|
|
|
|
|
|
|
//struct rlimit rl = { 32000000, 32000000 };
|
|
|
|
//if (setrlimit(RLIMIT_CORE, &rl))
|
|
|
|
// perror(strerror(errno));
|
2009-02-06 13:05:39 +01:00
|
|
|
|
2012-04-28 04:53:51 +02:00
|
|
|
/*VALUE c, s, a, b, r;
|
|
|
|
double v = (argc - 2) / 2.0;
|
|
|
|
c._float.value = cos(v);
|
|
|
|
s._float.value = __builtin_sin(v);
|
|
|
|
a._float.value = c._float.value * c._float.value;
|
|
|
|
b._float.value = s._float.value * s._float.value;
|
|
|
|
r._float.value = a._float.value + b._float.value;
|
2012-04-28 04:31:48 +02:00
|
|
|
|
2012-04-28 04:53:51 +02:00
|
|
|
fprintf(stderr, "%.24g %.24g / %.24g + %.24g = %.24g %d\n", c._float.value, s._float.value, a._float.value, b._float.value, r._float.value, r._float.value == 1.0);
|
|
|
|
*/
|
2012-04-28 04:31:48 +02:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
MEMORY_init();
|
|
|
|
COMMON_init();
|
|
|
|
//STRING_init();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-10 13:20:47 +02:00
|
|
|
EXEC_arch = (strcmp(FILE_get_name(argv[0]), "gbr" GAMBAS_VERSION_STRING) == 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-01 13:17:38 +01:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
2010-02-03 03:30:37 +01:00
|
|
|
if (is_long_option(argv[1], 'h', "help"))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
if (EXEC_arch)
|
|
|
|
{
|
|
|
|
printf(
|
2010-04-12 14:24:29 +02:00
|
|
|
"Usage: gbr" GAMBAS_VERSION_STRING " [options] <executable file> [<arguments>]\n\n"
|
2010-02-01 13:17:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: gbx" GAMBAS_VERSION_STRING " [options] [<project file>] [-- <arguments>]\n"
|
|
|
|
" gbx" GAMBAS_VERSION_STRING " -e <expression>\n\n"
|
2010-02-03 03:30:37 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
printf(
|
|
|
|
"Options:\n"
|
|
|
|
" -V --version display version\n"
|
|
|
|
" -h --help display this help\n"
|
|
|
|
" -L --license display license\n"
|
|
|
|
" -g enter debugging mode\n"
|
|
|
|
" -k do not unload shared libraries\n"
|
|
|
|
);
|
|
|
|
if (!EXEC_arch)
|
|
|
|
{
|
|
|
|
printf(" -e evaluate an expression\n");
|
2010-02-01 13:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my_exit(0);
|
|
|
|
}
|
2010-02-03 03:30:37 +01:00
|
|
|
else if (is_long_option(argv[1], 'V', "version"))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
printf(VERSION "\n");
|
|
|
|
my_exit(0);
|
|
|
|
}
|
2010-02-03 03:30:37 +01:00
|
|
|
else if (is_long_option(argv[1], 'L', "license"))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Gambas interpreter version " VERSION " " __DATE__ " " __TIME__ "\n"
|
|
|
|
COPYRIGHT
|
|
|
|
);
|
|
|
|
my_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-03 16:48:37 +01:00
|
|
|
if (!EXEC_arch && argc >= 2 && is_option(argv[1], 'e'))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
2010-02-03 16:48:37 +01:00
|
|
|
if (argc < 3)
|
|
|
|
fatal("-e option needs an expression.");
|
|
|
|
|
2010-02-01 13:17:38 +01:00
|
|
|
TRY
|
|
|
|
{
|
|
|
|
init(NULL);
|
|
|
|
EVAL_string(argv[2]);
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (ERROR_current->info.code && ERROR_current->info.code != E_ABORT)
|
|
|
|
ERROR_print_at(stderr, TRUE, TRUE);
|
|
|
|
main_exit(TRUE);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
|
|
|
main_exit(FALSE);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2010-02-03 03:30:37 +01:00
|
|
|
if (is_option(argv[i], 'g'))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
EXEC_debug = TRUE;
|
|
|
|
}
|
2011-07-25 02:50:11 +02:00
|
|
|
#if USE_PROFILE
|
2011-07-24 18:55:09 +02:00
|
|
|
else if (is_option(argv[i], 'p'))
|
|
|
|
{
|
|
|
|
EXEC_profile = TRUE;
|
|
|
|
}
|
2011-07-25 02:50:11 +02:00
|
|
|
#endif
|
2010-02-03 03:30:37 +01:00
|
|
|
else if (is_option(argv[i], 'f'))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
EXEC_fifo = TRUE;
|
|
|
|
if (i < (argc - 1) && *argv[i + 1] && *argv[i + 1] != '-')
|
|
|
|
{
|
|
|
|
EXEC_fifo_name = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 03:30:37 +01:00
|
|
|
else if (is_option(argv[i], 'k'))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
|
|
|
EXEC_keep_library = TRUE;
|
|
|
|
}
|
2010-05-22 20:02:34 +02:00
|
|
|
else if (is_option(argv[i], 'q'))
|
|
|
|
{
|
|
|
|
_quit_after_main = TRUE;
|
|
|
|
}
|
2010-02-03 16:48:37 +01:00
|
|
|
else if (is_option(argv[i], '-'))
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
2010-04-12 14:24:29 +02:00
|
|
|
i++;
|
2010-02-01 13:17:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-02-03 16:48:37 +01:00
|
|
|
else
|
2010-02-01 13:17:38 +01:00
|
|
|
{
|
2010-02-03 16:48:37 +01:00
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
fatal("too many %s.", EXEC_arch ? "executable files" : "project directories");
|
|
|
|
my_exit(1);
|
|
|
|
}
|
|
|
|
file = argv[i];
|
2010-02-04 17:00:01 +01:00
|
|
|
|
|
|
|
if (EXEC_arch)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-01 13:17:38 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-01 13:17:38 +01:00
|
|
|
n = i;
|
2010-02-03 16:48:37 +01:00
|
|
|
if (!file)
|
|
|
|
file = ".";
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-12 14:47:31 +02:00
|
|
|
if (EXEC_arch)
|
|
|
|
argv[0] = file;
|
|
|
|
|
2010-02-01 13:17:38 +01:00
|
|
|
for (i = 1; i <= (argc - n); i++)
|
|
|
|
argv[i] = argv[i + n - 1];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-01 13:17:38 +01:00
|
|
|
argc -= n - 1;
|
2010-04-12 14:47:31 +02:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
TRY
|
|
|
|
{
|
|
|
|
init(file);
|
|
|
|
|
2010-04-12 14:47:31 +02:00
|
|
|
if (!EXEC_arch)
|
|
|
|
argv[0] = PROJECT_name;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-30 02:04:51 +02:00
|
|
|
HOOK(main)(&argc, &argv);
|
2011-03-03 16:05:54 +01:00
|
|
|
EXEC_main_hook_done = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* Startup class */
|
2011-03-03 16:05:54 +01:00
|
|
|
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);
|
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
//CAPP_init(); /* needs startup class */
|
2011-03-03 16:05:54 +01:00
|
|
|
CFILE_init_watch();
|
|
|
|
|
|
|
|
PROJECT_argc = argc;
|
|
|
|
PROJECT_argv = argv;
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (EXEC_debug)
|
|
|
|
{
|
|
|
|
if (!_welcome)
|
|
|
|
DEBUG.Main(TRUE);
|
|
|
|
DEBUG.Main(TRUE);
|
|
|
|
main_exit(FALSE);
|
|
|
|
_exit(0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-03 16:05:54 +01:00
|
|
|
if (ERROR->info.code && ERROR->info.code != E_ABORT)
|
|
|
|
ERROR_print();
|
|
|
|
main_exit(TRUE);
|
|
|
|
_exit(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2011-03-03 16:05:54 +01:00
|
|
|
}
|
|
|
|
END_TRY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
TRY
|
|
|
|
{
|
2010-01-01 15:55:13 +01:00
|
|
|
EXEC_public_desc(PROJECT_class, NULL, startup, 0);
|
2011-07-26 02:28:52 +02:00
|
|
|
|
|
|
|
if (TYPE_is_boolean(startup->type))
|
|
|
|
ret = RP->_boolean.value ? 1 : 0;
|
|
|
|
else if (TYPE_is_integer(startup->type))
|
|
|
|
ret = RP->_integer.value & 0xFF;
|
|
|
|
|
2010-01-01 15:55:13 +01:00
|
|
|
EXEC_release_return_value();
|
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
if (_quit_after_main)
|
|
|
|
{
|
|
|
|
main_exit(TRUE);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2011-07-26 02:28:52 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
HOOK_DEFAULT(loop, WATCH_loop)();
|
|
|
|
EVENT_check_post();
|
|
|
|
}
|
2011-03-03 16:05:54 +01:00
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2011-03-03 16:05:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TRY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
main_exit(FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
MEMORY_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-03 16:05:54 +01:00
|
|
|
fflush(NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-07-26 02:28:52 +02:00
|
|
|
exit(ret);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|