1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/************
*
* This file is part of the vector graphics language Asymptote
* Copyright (C) 2004 Andy Hammerlindl, John C. Bowman, Tom Prince
* http://asymptote.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************/
#ifdef __CYGWIN__
#define _POSIX_C_SOURCE 200809L
#endif
#include <iostream>
#include <cstdlib>
#include <cerrno>
#include <sys/wait.h>
#include <sys/types.h>
#include "common.h"
#ifdef HAVE_LIBSIGSEGV
#include <sigsegv.h>
#endif
#include "errormsg.h"
#include "fpu.h"
#include "settings.h"
#include "locate.h"
#include "interact.h"
#include "process.h"
#include "stack.h"
using namespace settings;
using interact::interactive;
namespace run {
void purge();
}
#ifdef PROFILE
namespace vm {
extern void dumpProfile();
};
#endif
#ifdef HAVE_LIBSIGSEGV
void stackoverflow_handler (int, stackoverflow_context_t)
{
em.runtime(vm::getPos());
cerr << "Stack overflow" << endl;
abort();
}
int sigsegv_handler (void *, int emergency)
{
if(!emergency) return 0; // Really a stack overflow
em.runtime(vm::getPos());
#ifdef HAVE_GL
if(gl::glthread)
cerr << "Stack overflow or segmentation fault: rerun with -nothreads"
<< endl;
else
#endif
cerr << "Segmentation fault" << endl;
abort();
}
#endif
void setsignal(RETSIGTYPE (*handler)(int))
{
#ifdef HAVE_LIBSIGSEGV
char mystack[16384];
stackoverflow_install_handler(&stackoverflow_handler,
mystack,sizeof (mystack));
sigsegv_install_handler(&sigsegv_handler);
#endif
Signal(SIGBUS,handler);
Signal(SIGFPE,handler);
}
void signalHandler(int)
{
// Print the position and trust the shell to print an error message.
em.runtime(vm::getPos());
Signal(SIGBUS,SIG_DFL);
Signal(SIGFPE,SIG_DFL);
}
void interruptHandler(int)
{
em.Interrupt(true);
}
struct Args
{
int argc;
char **argv;
Args(int argc, char **argv) : argc(argc), argv(argv) {}
};
void *asymain(void *A)
{
setsignal(signalHandler);
Args *args=(Args *) A;
fpu_trap(trap());
if(interactive) {
Signal(SIGINT,interruptHandler);
processPrompt();
} else if (getSetting<bool>("listvariables") && numArgs()==0) {
try {
doUnrestrictedList();
} catch(handled_error) {
em.statusError();
}
} else {
int n=numArgs();
if(n == 0)
processFile("-");
else
for(int ind=0; ind < n; ind++) {
processFile(string(getArg(ind)),n > 1);
try {
if(ind < n-1)
setOptions(args->argc,args->argv);
} catch(handled_error) {
em.statusError();
}
}
}
#ifdef PROFILE
vm::dumpProfile();
#endif
if(getSetting<bool>("wait")) {
int status;
while(wait(&status) > 0);
}
#ifdef HAVE_GL
#ifdef HAVE_PTHREAD
if(gl::glthread && !getSetting<bool>("offscreen")) {
pthread_kill(gl::mainthread,SIGUSR2);
pthread_join(gl::mainthread,NULL);
}
#endif
#endif
exit(em.processStatus() || interact::interactive ? 0 : 1);
}
void exitHandler(int)
{
exit(0);
}
int main(int argc, char *argv[])
{
#ifdef HAVE_LIBGSL
unsetenv("GSL_RNG_SEED");
unsetenv("GSL_RNG_TYPE");
#endif
setsignal(signalHandler);
try {
setOptions(argc,argv);
} catch(handled_error) {
em.statusError();
}
Args args(argc,argv);
#ifdef HAVE_GL
#ifdef __APPLE__
bool usethreads=true;
#else
bool usethreads=view();
#endif
gl::glthread=usethreads ? getSetting<bool>("threads") : false;
#if HAVE_PTHREAD
if(gl::glthread) {
pthread_t thread;
try {
if(pthread_create(&thread,NULL,asymain,&args) == 0) {
gl::mainthread=pthread_self();
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &set, NULL);
while(true) {
Signal(SIGUSR2,exitHandler);
camp::glrenderWrapper();
gl::initialize=true;
}
} else gl::glthread=false;
} catch(std::bad_alloc&) {
outOfMemory();
}
}
#endif
gl::glthread=false;
#endif
asymain(&args);
}
|