Support for environment variables set at startup.

[INTERPRETER]
* NEW: Set environment variables at startup from the '.environment' file.

[COMPILER]
* NEW: Generate the '.environment' files with the list of environment variables that must be set at startup.

[ARCHIVER]
* NEW: Add the '.environment' file to the archive.
This commit is contained in:
Benoît Minisini 2023-03-18 01:36:19 +01:00
parent d44be704d3
commit 44bdb9486f
3 changed files with 119 additions and 34 deletions

View file

@ -349,14 +349,17 @@ int main(int argc, char **argv)
len_prefix = strlen(file);
path_init(file);
/* .startup and .project file always first ! */
// '.startup', '.project' and '.environment' files are always first!
path = FILE_cat(FILE_get_dir(ARCH_project), ".startup", NULL);
if (FILE_exist(path)) ARCH_add_file(path);
path = FILE_cat(FILE_get_dir(ARCH_project), ".project", NULL);
if (FILE_exist(path)) ARCH_add_file(path);
path = FILE_cat(FILE_get_dir(ARCH_project), ".environment", NULL);
if (FILE_exist(path)) ARCH_add_file(path);
for(;;)
{
if (path_current >= path_count())

View file

@ -70,6 +70,9 @@ bool COMP_do_not_lock = TRUE;
#define STARTUP_MAX_LINE 256
static FILE *_file = NULL;
static const char *_file_name = NULL;
const FORM_FAMILY COMP_form_families[] =
{
{ "form", FORM_NORMAL },
@ -260,7 +263,7 @@ static bool line_begins_with(const char *line, const char *key, int len)
return strncmp(line, key, len) == 0;
}
static void startup_print(FILE *fs, const char *key, const char *def)
static void find_lines(const char *key, const char *def, void (*print_func)(const char *line))
{
FILE *fp;
char line[256];
@ -276,7 +279,7 @@ static void startup_print(FILE *fs, const char *key, const char *def)
if (line_begins_with(line, key, len))
{
fprintf(fs, "%s\n", &line[len]);
(*print_func)(&line[len]);
print = TRUE;
}
}
@ -284,7 +287,7 @@ static void startup_print(FILE *fs, const char *key, const char *def)
fclose(fp);
if (!print && def)
fprintf(fs, "%s\n", def);
(*print_func)(def);
}
static char *find_version_in_file(void)
@ -329,7 +332,7 @@ static char *find_version_in_file(void)
return STR_copy(line);
}
static void startup_print_version(FILE *fs)
static void print_version()
{
FILE *fp;
char line[256];
@ -363,48 +366,88 @@ static void startup_print_version(FILE *fs)
if (version)
{
fputs(version, fs);
fputs(version, _file);
if (add_branch && branch)
fputs(branch, fs);
fputc('\n', fs);
fputs(branch, _file);
fputc('\n', _file);
STR_free(version);
}
else
fputs("0.0.0\n", fs);
fputs("0.0.0\n", _file);
}
static void create_file(const char *file)
{
const char *path = FILE_cat(FILE_get_dir(COMP_project), file, NULL);
_file_name = file;
_file = fopen(path, "w");
if (!_file)
THROW("Cannot create '&1' file", _file_name);
FILE_set_owner(path, COMP_project);
}
static void close_file()
{
const char *path = FILE_cat(FILE_get_dir(COMP_project), _file_name, NULL);
if (fclose(_file))
THROW("Cannot create '&1' file", _file_name);
_file = NULL;
_file_name = NULL;
if (FILE_get_size(path) == 0)
FILE_unlink(path);
}
static void print_line(const char *line)
{
fputs(line, _file);
fputc('\n', _file);
}
static void print_environment(const char *line)
{
if (*line && *line != ' ')
print_line(line);
}
static void create_startup_file()
{
const char *name;
FILE *fs;
create_file(".startup");
name = FILE_cat(FILE_get_dir(COMP_project), ".startup", NULL);
fs = fopen(name, "w");
if (!fs)
THROW("Cannot create .startup file");
find_lines("Startup=", "", print_line);
find_lines("Title=", "", print_line);
// Do that now, otherwise file buffer can be erased
FILE_set_owner(name, COMP_project);
startup_print(fs, "Startup=", "");
startup_print(fs, "Title=", "");
fputc('#', fs);
fputs(COMP_project_name, fs);
fputc('\n', fs);
fputc('#', _file);
fputs(COMP_project_name, _file);
fputc('\n', _file);
//startup_print(fs, "Stack=", "0");
startup_print(fs, "StackTrace=", "0");
find_lines("StackTrace=", "0", print_line);
startup_print_version(fs);
print_version();
fputc('\n', fs);
startup_print(fs, "Component=", NULL);
startup_print(fs, "Library=", NULL);
fputc('\n', fs);
fputc('\n', _file);
find_lines("Component=", NULL, print_line);
find_lines("Library=", NULL, print_line);
fputc('\n', _file);
if (fclose(fs))
THROW("Cannot create .startup file");
close_file();
create_file(".environment");
find_lines("StartupEnvironment=", NULL, print_environment);
close_file();
}
#undef isdigit
@ -825,4 +868,3 @@ void COMPILE_create_file(FILE **fw, const char *file)
}
}

View file

@ -65,6 +65,7 @@ bool PROJECT_run_tests = FALSE;
const char *PROJECT_override = NULL;
static char *project_buffer;
static char *environment_buffer;
//static char *project_ptr;
static int project_line;
@ -148,6 +149,7 @@ static void project_library_path(char *name, int len)
}
}
static void check_after_analyze()
{
if (!PROJECT_name || PROJECT_name[0] == 0)
@ -160,6 +162,7 @@ static void check_after_analyze()
PROJECT_title = PROJECT_name;
}
static bool get_line(char **addr, const char *end, char **start, int *len)
{
char *p = *addr;
@ -177,6 +180,7 @@ static bool get_line(char **addr, const char *end, char **start, int *len)
return (*len > 0);
}
void PROJECT_analyze_startup(char *addr, int len, PROJECT_COMPONENT_CALLBACK cb)
{
char *end = &addr[len];
@ -224,6 +228,21 @@ void PROJECT_analyze_startup(char *addr, int len, PROJECT_COMPONENT_CALLBACK cb)
}
}
static void init_environment(char *addr, int len)
{
char *end = &addr[len];
char *p;
int l;
while (get_line(&addr, end, &p, &l))
{
p[l] = 0;
putenv(p);
}
}
void PROJECT_init(const char *file)
{
int len;
@ -372,6 +391,27 @@ void PROJECT_load()
}
END_TRY
if (EXEC_arch)
file = ".environment";
else
file = FILE_cat(PROJECT_path, ".environment", NULL);
if (FILE_exist(file))
{
TRY
{
STREAM_load(file, &environment_buffer, &len);
}
CATCH
{
ERROR_fatal("unable to load environment file");
}
END_TRY
init_environment(environment_buffer, len);
}
// Loads all component
COMPONENT_load_all();
}