Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 2008-2010, Gabriel Dos Reis.
3
All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are
7
met:
8
9
- Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
11
12
- Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the
15
distribution.
16
17
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
18
names of its contributors may be used to endorse or promote products
19
derived from this software without specific prior written permission.
20
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
/* This program is a driver for the OpenAxiom core executable.
35
It pretends to be the OpenAxiom interpreter when, in fact, the actual
36
work is done by the Core Executable. It also occasionally masquerades
37
as the session manager. */
38
39
40
#include <stdlib.h>
41
#include <string.h>
42
#include <errno.h>
43
#include <stdio.h>
44
#include <locale.h>
45
46
#include "cfuns.h"
47
#include "open-axiom.h"
48
49
#define OPENAXIOM_GLOBAL_ENV "AXIOM"
50
51
namespace OpenAxiom {
52
53
// Publish the system exec prefix for use by sub-processes.
54
static void
55
publish_systemdir(const char* dir)
56
{
57
if (!oa_setenv(OPENAXIOM_GLOBAL_ENV, dir)) {
58
perror("publish_systemdir");
59
abort();
60
}
61
}
62
63
static void
64
augment_variable(const char* name, const char* value) {
65
const char* oldval = oa_getenv(name);
66
const int value_length = strlen(value);
67
const int oldval_length = oldval == 0 ? 0 : strlen(oldval);
68
const int newval_length = value_length + 1 + oldval_length;
69
char* newval = (char*) malloc(newval_length + 1);
70
71
strcpy(newval,value);
72
if (oldval != 0) {
73
newval[value_length] = openaxiom_ifs;
74
strcpy(newval + value_length + 1, oldval);
75
}
76
77
if (!oa_setenv(name, newval))
78
perror("oa_augment_environment_variable");
79
}
80
81
static void
82
upgrade_environment(const Command* command) {
83
const char* sysdir = command->root_dir;
84
augment_variable("TEXINPUTS",
85
oa_concatenate_string(sysdir, OPENAXIOM_TEXINPUTS_PATH));
86
augment_variable("BIBINPUTS",
87
oa_concatenate_string(sysdir, OPENAXIOM_BIBINPUTS_PATH));
88
const char* ldd_path = option_value(command, "--syslib");
89
if (ldd_path == 0)
90
ldd_path = oa_concatenate_string(sysdir, "/lib");
91
#ifdef OPENAXIOM_MS_WINDOWS_HOST
92
augment_variable("PATH", ldd_path);
93
#else
94
augment_variable("LD_LIBRARY_PATH", ldd_path);
95
#endif
96
publish_systemdir(sysdir);
97
}
98
99
// Print configuration info.
100
static int
101
print_configuration_info(Command* command) {
102
int i;
103
for (i = 1; i < command->core.argc; ++i) {
104
if (strcmp(command->core.argv[i], "include") == 0)
105
printf("\"%s\"/include ", command->root_dir);
106
else if (strcmp(command->core.argv[i], "lib") == 0)
107
printf(" -L\"%s\"/lib -lOpenAxiom", command->root_dir);
108
else {
109
fprintf(stderr, "open-axiom: invalid request %s\n",
110
command->core.argv[i]);
111
return 1;
112
}
113
}
114
fflush(stdout);
115
return 0;
116
}
117
}
118
119
int
120
main(int argc, char* argv[])
121
{
122
using namespace OpenAxiom;
123
Command command;
124
Driver driver = preprocess_arguments(&command, argc, argv);
125
upgrade_environment(&command);
126
oa_setenv("LC_ALL", "C");
127
setlocale(LC_ALL, "");
128
129
switch (driver) {
130
case Driver::null:
131
return 0; /* Bye. */
132
133
case Driver::config:
134
return print_configuration_info(&command);
135
136
case Driver::core:
137
case Driver::script:
138
case Driver::compiler:
139
case Driver::translator:
140
case Driver::linker:
141
case Driver::gui:
142
return execute_core(&command, driver);
143
144
case Driver::execute:
145
return oa_spawn(&command.core,
146
SpawnFlags::search_path | SpawnFlags::replace);
147
148
case Driver::sman:
149
break;
150
151
default:
152
abort();
153
}
154
155
#ifdef __WIN32__
156
/* Should not happen on MS platforms. */
157
abort();
158
#else /* __WIN32__ */
159
execv(make_path_for(command.root_dir, driver), argv);
160
perror(strerror(errno));
161
return -1;
162
#endif /* __WIN32__ */
163
}
164
165