#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <locale.h>
#include "cfuns.h"
#include "open-axiom.h"
#define OPENAXIOM_GLOBAL_ENV "AXIOM"
namespace OpenAxiom {
static void
publish_systemdir(const char* dir)
{
if (!oa_setenv(OPENAXIOM_GLOBAL_ENV, dir)) {
perror("publish_systemdir");
abort();
}
}
static void
augment_variable(const char* name, const char* value) {
const char* oldval = oa_getenv(name);
const int value_length = strlen(value);
const int oldval_length = oldval == 0 ? 0 : strlen(oldval);
const int newval_length = value_length + 1 + oldval_length;
char* newval = (char*) malloc(newval_length + 1);
strcpy(newval,value);
if (oldval != 0) {
newval[value_length] = openaxiom_ifs;
strcpy(newval + value_length + 1, oldval);
}
if (!oa_setenv(name, newval))
perror("oa_augment_environment_variable");
}
static void
upgrade_environment(const Command* command) {
const char* sysdir = command->root_dir;
augment_variable("TEXINPUTS",
oa_concatenate_string(sysdir, OPENAXIOM_TEXINPUTS_PATH));
augment_variable("BIBINPUTS",
oa_concatenate_string(sysdir, OPENAXIOM_BIBINPUTS_PATH));
const char* ldd_path = option_value(command, "--syslib");
if (ldd_path == 0)
ldd_path = oa_concatenate_string(sysdir, "/lib");
#ifdef OPENAXIOM_MS_WINDOWS_HOST
augment_variable("PATH", ldd_path);
#else
augment_variable("LD_LIBRARY_PATH", ldd_path);
#endif
publish_systemdir(sysdir);
}
static int
print_configuration_info(Command* command) {
int i;
for (i = 1; i < command->core.argc; ++i) {
if (strcmp(command->core.argv[i], "include") == 0)
printf("\"%s\"/include ", command->root_dir);
else if (strcmp(command->core.argv[i], "lib") == 0)
printf(" -L\"%s\"/lib -lOpenAxiom", command->root_dir);
else {
fprintf(stderr, "open-axiom: invalid request %s\n",
command->core.argv[i]);
return 1;
}
}
fflush(stdout);
return 0;
}
}
int
main(int argc, char* argv[])
{
using namespace OpenAxiom;
Command command;
Driver driver = preprocess_arguments(&command, argc, argv);
upgrade_environment(&command);
oa_setenv("LC_ALL", "C");
setlocale(LC_ALL, "");
switch (driver) {
case Driver::null:
return 0;
case Driver::config:
return print_configuration_info(&command);
case Driver::core:
case Driver::script:
case Driver::compiler:
case Driver::translator:
case Driver::linker:
case Driver::gui:
return execute_core(&command, driver);
case Driver::execute:
return oa_spawn(&command.core,
SpawnFlags::search_path | SpawnFlags::replace);
case Driver::sman:
break;
default:
abort();
}
#ifdef __WIN32__
abort();
#else
execv(make_path_for(command.root_dir, driver), argv);
perror(strerror(errno));
return -1;
#endif
}