2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
gbx_project.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2022-09-12 15:13:13 +02:00
|
|
|
(c) 2000-2017 Benoît Minisini <benoit.minisini@gambas-basic.org>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +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
|
|
|
|
2013-01-29 01:53:21 +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
|
|
|
|
2013-01-29 01:53:21 +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
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_PROJECT_C
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gb_buffer.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_stack.h"
|
|
|
|
#include "gb_component.h"
|
|
|
|
#include "gbx_component.h"
|
|
|
|
|
|
|
|
#include "gbx_project.h"
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
char *PROJECT_path = NULL;
|
|
|
|
char *PROJECT_exec_path = NULL;
|
|
|
|
char *PROJECT_name = NULL;
|
|
|
|
char *PROJECT_title = NULL;
|
2014-02-12 23:26:02 +01:00
|
|
|
const char *PROJECT_startup = NULL;
|
2008-01-17 22:39:26 +01:00
|
|
|
char *PROJECT_version = NULL;
|
|
|
|
CLASS *PROJECT_class = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int PROJECT_argc = 0;
|
|
|
|
char **PROJECT_argv = NULL;
|
2017-02-07 01:08:22 +01:00
|
|
|
//char *PROJECT_argname = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
char *PROJECT_oldcwd = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-02 23:57:49 +02:00
|
|
|
bool PROJECT_run_httpd = FALSE;
|
2020-05-07 08:17:27 +02:00
|
|
|
bool PROJECT_run_tests = FALSE;
|
2013-08-02 23:57:49 +02:00
|
|
|
|
2020-11-02 22:09:35 +01:00
|
|
|
const char *PROJECT_override = NULL;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static char *project_buffer;
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
//static char *project_ptr;
|
2007-12-30 17:41:49 +01:00
|
|
|
static int project_line;
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
static const char *_last_component = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static void raise_error(const char *msg)
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
char line[16];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
snprintf(line, sizeof(line), "%d", project_line);
|
|
|
|
THROW(E_PROJECT, line, msg);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void project_title(char *name, int len)
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
name[len] = 0;
|
|
|
|
PROJECT_title = name;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void project_version(char *name, int len)
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
name[len] = 0;
|
|
|
|
PROJECT_version = name;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
static void project_component(char *name, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
_last_component = name;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
name[len] = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
COMPONENT_create(name);
|
2013-01-29 01:53:21 +01:00
|
|
|
|
2013-08-02 23:57:49 +02:00
|
|
|
// If 'gb.httpd' is set explicitely, then always run through it.
|
|
|
|
|
|
|
|
if (strcmp(name, "gb.httpd") == 0)
|
|
|
|
PROJECT_run_httpd = TRUE;
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
_last_component = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
static void project_startup(char *name, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-02-12 23:26:02 +01:00
|
|
|
if (PROJECT_startup)
|
|
|
|
return;
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (len == 0)
|
|
|
|
raise_error("Project startup class name is void");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
name[len] = 0;
|
|
|
|
PROJECT_startup = name;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
static void project_library_path(char *name, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2016-02-10 22:45:33 +01:00
|
|
|
if (!EXEC_debug)
|
|
|
|
{
|
|
|
|
ARCHIVE_path = STRING_new_zero(STRING_conv_file_name(name, len));
|
|
|
|
if (*name != '/')
|
|
|
|
{
|
|
|
|
name = STRING_new_zero(FILE_cat(PROJECT_path, ARCHIVE_path, NULL));
|
|
|
|
STRING_free(&ARCHIVE_path);
|
|
|
|
ARCHIVE_path = name;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
static void check_after_analyze()
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
if (!PROJECT_name || PROJECT_name[0] == 0)
|
|
|
|
raise_error("No project name");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (!PROJECT_startup || PROJECT_startup[0] == 0)
|
|
|
|
raise_error("No startup class");
|
2008-04-17 12:18:25 +02:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (!PROJECT_title || PROJECT_title[0] == 0)
|
|
|
|
PROJECT_title = PROJECT_name;
|
2008-04-17 12:18:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_line(char **addr, const char *end, char **start, int *len)
|
|
|
|
{
|
|
|
|
char *p = *addr;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
if (p >= end)
|
|
|
|
return FALSE;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
while (p < end && *p && *p != '\n')
|
|
|
|
p++;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
*start = *addr;
|
|
|
|
*len = p - *start;
|
|
|
|
*addr = p + 1;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
return (*len > 0);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
void PROJECT_analyze_startup(char *addr, int len, PROJECT_COMPONENT_CALLBACK cb)
|
2008-04-17 12:18:25 +02:00
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
char *end = &addr[len];
|
2008-04-17 12:18:25 +02:00
|
|
|
char *p;
|
2011-08-21 04:27:16 +02:00
|
|
|
int l, i;
|
|
|
|
|
|
|
|
if (!cb)
|
|
|
|
{
|
|
|
|
if (get_line(&addr, end, &p, &l))
|
|
|
|
project_startup(p, l);
|
|
|
|
if (get_line(&addr, end, &p, &l))
|
|
|
|
project_title(p, l);
|
2021-10-20 02:28:22 +02:00
|
|
|
get_line(&addr, end, &p, &l); // Deprecated "Stack"
|
|
|
|
get_line(&addr, end, &p, &l); // Deprecated "StackTrace"
|
2011-08-21 04:27:16 +02:00
|
|
|
if (get_line(&addr, end, &p, &l))
|
|
|
|
project_version(p, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 1; i <= 5; i++)
|
|
|
|
get_line(&addr, end, &p, &l);
|
|
|
|
}
|
2008-04-17 12:18:25 +02:00
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
if (get_line(&addr, end, &p, &l))
|
|
|
|
{
|
|
|
|
project_library_path(p, l);
|
|
|
|
while (get_line(&addr, end, &p, &l));
|
|
|
|
}
|
2008-04-17 12:18:25 +02:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
if (!cb)
|
|
|
|
{
|
|
|
|
while (get_line(&addr, end, &p, &l))
|
|
|
|
project_component(p, l);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
check_after_analyze();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (get_line(&addr, end, &p, &l))
|
|
|
|
{
|
|
|
|
p[l] = 0;
|
|
|
|
(*cb)(p);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROJECT_init(const char *file)
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
int len;
|
|
|
|
const char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
/* Save the working directory */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
PROJECT_oldcwd = STRING_new_zero(FILE_getcwd(NULL));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
/* Gambas installation path */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
path = FILE_find_gambas();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
PROJECT_exec_path = STRING_new_zero(FILE_get_dir(FILE_get_dir(path)));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* Component paths */
|
|
|
|
|
2008-02-24 17:29:02 +01:00
|
|
|
#ifdef OS_64BITS
|
2013-01-29 01:53:21 +01:00
|
|
|
COMPONENT_path = STRING_new_zero(FILE_cat(PROJECT_exec_path, GAMBAS_LIB64_PATH, NULL));
|
|
|
|
if (access(COMPONENT_path, F_OK))
|
2016-02-01 03:18:49 +01:00
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
STRING_free(&COMPONENT_path);
|
|
|
|
COMPONENT_path = STRING_new_zero(FILE_cat(PROJECT_exec_path, GAMBAS_LIB_PATH, NULL));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
COMPONENT_path = STRING_new_zero(FILE_cat(PROJECT_exec_path, GAMBAS_LIB_PATH, NULL));
|
2008-02-24 17:29:02 +01:00
|
|
|
#endif
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
//STRING_new(&COMPONENT_user_path, FILE_cat(PROJECT_get_home(), ".local", GAMBAS_LIB_PATH, NULL), 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
/* Project path & name*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
2010-04-05 03:44:43 +02:00
|
|
|
// "gbx3 -e" case
|
2010-06-05 01:48:53 +02:00
|
|
|
PROJECT_path = STRING_new("", 0);
|
|
|
|
PROJECT_name = STRING_new("", 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-27 03:00:39 +02:00
|
|
|
if (*file == '.' && file[1] == '/')
|
|
|
|
file += 2;
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (EXEC_arch)
|
|
|
|
{
|
2015-05-26 03:19:20 +02:00
|
|
|
if (FILE_is_relative(file))
|
2010-11-03 21:28:54 +01:00
|
|
|
{
|
2015-05-26 03:19:20 +02:00
|
|
|
path = FILE_getcwd(file);
|
|
|
|
if (path == NULL)
|
|
|
|
goto _PANIC;
|
2010-11-03 21:28:54 +01:00
|
|
|
}
|
2015-05-27 03:00:39 +02:00
|
|
|
else
|
|
|
|
path = file;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2015-05-26 03:19:20 +02:00
|
|
|
path = FILE_get_dir(path);
|
|
|
|
FILE_chdir(path);
|
2013-01-29 01:53:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-26 03:19:20 +02:00
|
|
|
if (FILE_is_absolute(file))
|
2013-01-29 01:53:21 +01:00
|
|
|
{
|
|
|
|
path = file;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = FILE_getcwd(file);
|
|
|
|
if (path == NULL)
|
|
|
|
goto _PANIC;
|
|
|
|
|
|
|
|
if (!chdir(path))
|
2010-02-03 16:48:37 +01:00
|
|
|
{
|
|
|
|
path = FILE_getcwd(NULL);
|
|
|
|
if (path == NULL)
|
|
|
|
goto _PANIC;
|
|
|
|
}
|
2013-01-29 01:53:21 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
len = strlen(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
while (len > 1)
|
|
|
|
{
|
|
|
|
if (path[len - 1] != '/')
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
len--;
|
|
|
|
/*path[len] = 0;*/
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
PROJECT_path = STRING_new(path, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
FILE_chdir(PROJECT_path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
/* Project name */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (EXEC_arch)
|
|
|
|
PROJECT_name = STRING_new_zero(FILE_get_basename(file));
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2013-01-29 01:53:21 +01:00
|
|
|
PROJECT_name = STRING_new_zero(FILE_get_name(PROJECT_path));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* Main archive creation */
|
|
|
|
|
|
|
|
ARCHIVE_create_main(EXEC_arch ? FILE_get_name(file) : NULL);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
_PANIC:
|
2013-01-29 01:53:21 +01:00
|
|
|
ERROR_panic("Cannot initialize project: %s", strerror(errno));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
void PROJECT_load()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
const char *file;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Project file analyze */
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
STACK_init();
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
if (EXEC_arch)
|
|
|
|
file = ".startup";
|
|
|
|
else
|
|
|
|
file = FILE_cat(PROJECT_path, ".startup", NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
TRY
|
|
|
|
{
|
2010-01-20 01:37:00 +01:00
|
|
|
STREAM_load(file, &project_buffer, &len);
|
2008-04-17 12:18:25 +02:00
|
|
|
}
|
2010-01-20 01:37:00 +01:00
|
|
|
CATCH
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
ERROR_fatal("unable to find startup file");
|
2010-01-20 01:37:00 +01:00
|
|
|
}
|
|
|
|
END_TRY
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
TRY
|
|
|
|
{
|
|
|
|
PROJECT_analyze_startup(project_buffer, len, NULL);
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (_last_component)
|
|
|
|
ERROR_fatal("unable to load component: %s", _last_component);
|
|
|
|
else
|
|
|
|
ERROR_fatal("unable to analyze startup file");
|
|
|
|
}
|
|
|
|
END_TRY
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2012-09-27 22:52:08 +02:00
|
|
|
// Loads all component
|
2007-12-30 17:41:49 +01:00
|
|
|
COMPONENT_load_all();
|
2012-09-27 22:52:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROJECT_load_finish(void)
|
|
|
|
{
|
|
|
|
// Load exported class of components written in Gambas
|
|
|
|
COMPONENT_load_all_finish();
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2012-09-27 22:52:08 +02:00
|
|
|
// Loads main archive
|
2007-12-30 17:41:49 +01:00
|
|
|
ARCHIVE_load_main();
|
|
|
|
|
2013-01-29 01:53:21 +01:00
|
|
|
// Startup class
|
2020-05-07 08:17:27 +02:00
|
|
|
PROJECT_class = CLASS_find(PROJECT_run_tests ? "Test" : PROJECT_startup);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PROJECT_exit(void)
|
|
|
|
{
|
2013-01-29 01:53:21 +01:00
|
|
|
if (project_buffer)
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&project_buffer);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-07 01:08:22 +01:00
|
|
|
//STRING_free(&PROJECT_argname);
|
2010-04-25 12:46:57 +02:00
|
|
|
STRING_free(&PROJECT_name);
|
|
|
|
STRING_free(&PROJECT_path);
|
2013-01-29 01:53:21 +01:00
|
|
|
STRING_free(&PROJECT_oldcwd);
|
|
|
|
STRING_free(&PROJECT_exec_path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|