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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/*****
* util.cc
* John Bowman
*
* A place for useful utility functions.
*****/
#ifdef __CYGWIN__
#define _POSIX_C_SOURCE 200809L
#endif
#include <cassert>
#include <iostream>
#include <cstdio>
#include <cfloat>
#include <sstream>
#include <cerrno>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <algorithm>
#include "util.h"
#include "settings.h"
#include "errormsg.h"
#include "camperror.h"
#include "interact.h"
using namespace settings;
bool False=false;
namespace vm {
void error(const char* message);
}
#if __GNUC__
#include <cxxabi.h>
string demangle(const char *s)
{
int status;
char *demangled = abi::__cxa_demangle(s,NULL,NULL,&status);
if (status == 0 && demangled) {
string str(demangled);
free(demangled);
return str;
} else if (status == -2) {
free(demangled);
return s;
} else {
free(demangled);
return string("Unknown(") + s + ")";
}
}
#else
string demangle(const char* s)
{
return s;
}
#endif
char *Strdup(string s)
{
size_t size=s.size()+1;
char *dest=new(UseGC) char[size];
std::memcpy(dest,s.c_str(),size*sizeof(char));
return dest;
}
char *StrdupNoGC(string s)
{
size_t size=s.size()+1;
char *dest=new char[size];
std::memcpy(dest,s.c_str(),size*sizeof(char));
return dest;
}
char *StrdupMalloc(string s)
{
size_t size=s.size()+1;
char *dest=(char *) std::malloc(size);
std::memcpy(dest,s.c_str(),size*sizeof(char));
return dest;
}
string stripDir(string name)
{
size_t p;
#ifdef __MSDOS__
p=name.rfind('\\');
if(p < string::npos) name.erase(0,p+1);
#endif
p=name.rfind('/');
if(p < string::npos) name.erase(0,p+1);
return name;
}
string stripFile(string name)
{
size_t p;
bool dir=false;
#ifdef __MSDOS__
p=name.rfind('\\');
if(p < string::npos) {
dir=true;
while(p > 0 && name[p-1] == '\\') --p;
name.erase(p+1);
}
#endif
p=name.rfind('/');
if(p < string::npos) {
dir=true;
while(p > 0 && name[p-1] == '/') --p;
name.erase(p+1);
}
return dir ? name : "";
}
string stripExt(string name, const string& ext)
{
string suffix="."+ext;
size_t p=name.rfind(suffix);
size_t n=suffix.length();
if(n == 1 || p == name.length()-n)
return name.substr(0,p);
else return name;
}
void backslashToSlash(string& s)
{
size_t p;
while((p=s.find('\\')) < string::npos)
s[p]='/';
}
void spaceToUnderscore(string& s)
{
size_t p;
while((p=s.find(' ')) < string::npos)
s[p]='_';
}
string Getenv(const char *name, bool msdos)
{
char *s=getenv(name);
if(!s) return "";
string S=string(s);
if(msdos) backslashToSlash(S);
return S;
}
void writeDisabled()
{
camp::reportError("Write to other directories disabled; override with option -globalwrite");
}
string cleanpath(string name)
{
string dir=stripFile(name);
name=stripDir(name);
spaceToUnderscore(name);
return dir+name;
}
string outpath(string name)
{
bool global=globalwrite();
string dir=stripFile(name);
if(global && !dir.empty()) return name;
string outdir=stripFile(outname());
if(!(global || dir.empty() || dir == outdir)) writeDisabled();
return outdir+stripDir(name);
}
string buildname(string name, string suffix, string aux)
{
name=stripExt(outpath(name),defaultformat())+aux;
if(!suffix.empty()) name += "."+suffix;
return name;
}
string auxname(string filename, string suffix)
{
return buildname(filename,suffix,"_");
}
sighandler_t Signal(int signum, sighandler_t handler)
{
struct sigaction action,oldaction;
action.sa_handler=handler;
sigemptyset(&action.sa_mask);
action.sa_flags=0;
return sigaction(signum,&action,&oldaction) == 0 ? oldaction.sa_handler :
SIG_ERR;
}
void push_split(mem::vector<string>& a, const string& S)
{
const char *p=S.c_str();
string s;
char c;
while((c=*(p++))) {
if(c == ' ') {
if(s.size() > 0) {
a.push_back(s);
s.clear();
}
} else s += c;
}
if(s.size() > 0)
a.push_back(s);
}
char **args(const mem::vector<string>& s, bool quiet)
{
size_t count=s.size();
char **argv=NULL;
argv=new char*[count+1];
for(size_t i=0; i < count; ++i)
argv[i]=StrdupNoGC(s[i]);
if(!quiet && settings::verbose > 1) {
cerr << argv[0];
for(size_t i=1; i < count; ++i) cerr << " " << argv[i];
cerr << endl;
}
argv[count]=NULL;
return argv;
}
void execError(const char *command, const char *hint, const char *application)
{
cerr << "Cannot execute " << command << endl;
if(*application == 0) application=hint;
if(hint) {
string s=string(hint);
transform(s.begin(), s.end(), s.begin(), toupper);
cerr << "Please put in a file " << getSetting<string>("config")
<< ": " << endl << endl
<< "import settings;" << endl
<< hint << "=\"LOCATION\";" << endl << endl
<< "where LOCATION specifies the location of "
<< application << "." << endl << endl
<< "Alternatively, set the environment variable ASYMPTOTE_" << s
<< endl << "or use the command line option -" << hint
<< "=\"LOCATION\". For further details, see" << endl
<< "http://asymptote.sourceforge.net/doc/Configuring.html" << endl
<< "http://asymptote.sourceforge.net/doc/Search-paths.html" << endl;
}
}
// quiet: 0=none; 1=suppress stdout; 2=suppress stdout+stderr.
int System(const mem::vector<string> &command, int quiet, bool wait,
const char *hint, const char *application, int *ppid)
{
int status;
cout.flush(); // Flush stdout to avoid duplicate output.
char **argv=args(command);
int pid=fork();
if(pid == -1)
camp::reportError("Cannot fork process");
if(pid == 0) {
if(interact::interactive) signal(SIGINT,SIG_IGN);
if(quiet) {
static int null=creat("/dev/null",O_WRONLY);
close(STDOUT_FILENO);
dup2(null,STDOUT_FILENO);
if(quiet == 2) {
close(STDERR_FILENO);
dup2(null,STDERR_FILENO);
}
}
if(argv) {
execvp(argv[0],argv);
execError(argv[0],hint,application);
_exit(-1);
}
}
if(ppid) *ppid=pid;
for(;;) {
if(waitpid(pid, &status, wait ? 0 : WNOHANG) == -1) {
if(errno == ECHILD) return 0;
if(errno != EINTR) {
if(quiet < 2) {
ostringstream msg;
msg << "Command failed: ";
for(size_t i=0; i < command.size(); ++i) msg << command[i] << " ";
camp::reportError(msg);
}
}
} else {
if(!wait) return 0;
if(WIFEXITED(status)) {
if(argv) {
char **p=argv;
char *s;
while((s=*(p++)) != NULL)
delete [] s;
delete [] argv;
}
return WEXITSTATUS(status);
} else {
if(quiet < 2) {
ostringstream msg;
msg << "Command exited abnormally: ";
for(size_t i=0; i < command.size(); ++i) msg << command[i] << " ";
camp::reportError(msg);
}
}
}
}
}
string stripblanklines(const string& s)
{
string S=string(s);
bool blank=true;
const char *t=S.c_str();
size_t len=S.length();
for(size_t i=0; i < len; i++) {
if(t[i] == '\n') {
if(blank) S[i]=' ';
else blank=true;
} else if(t[i] != '\t' && t[i] != ' ') blank=false;
}
return S;
}
char *startpath=NULL;
void noPath()
{
camp::reportError("Cannot get current path");
}
char *getPath(char *p)
{
#ifdef MAXPATHLEN
static size_t size = MAXPATHLEN;
#else
static size_t size = 1024;
#endif
if(!p) p=new(UseGC) char[size];
if(!p) noPath();
else while(getcwd(p,size) == NULL) {
if(errno == ERANGE) {
size *= 2;
p=new(UseGC) char[size];
} else {noPath(); p=NULL;}
}
return p;
}
const char *setPath(const char *s, bool quiet)
{
if(startpath == NULL) startpath=getPath(startpath);
if(s == NULL || *s == 0) s=startpath;
int rc=chdir(s);
if(rc != 0) {
ostringstream buf;
buf << "Cannot change to directory '" << s << "'";
camp::reportError(buf);
}
char *p=getPath();
if(p && (!interact::interactive || quiet) && verbose > 1)
cout << "cd " << p << endl;
return p;
}
void push_command(mem::vector<string>& a, const string& s)
{
a.push_back(s);
#ifdef __MSDOS__
if(s == "cmd") {
a.push_back("/c");
a.push_back("start");
a.push_back("\"\"");
}
#endif
}
void popupHelp() {
// If the popped-up help is already running, pid stores the pid of the viewer.
static int pid=0;
// Status is ignored.
static int status=0;
// If the help viewer isn't running (or its last run has termined), launch the
// viewer again.
if (pid==0 || (waitpid(pid, &status, WNOHANG) == pid)) {
mem::vector<string> cmd;
push_command(cmd,getSetting<string>("pdfviewer"));
string viewerOptions=getSetting<string>("pdfviewerOptions");
if(!viewerOptions.empty())
cmd.push_back(viewerOptions);
cmd.push_back(docdir+dirsep+"asymptote.pdf");
status=System(cmd,0,false,"pdfviewer","your PDF viewer",&pid);
}
}
const char *intrange="integer argument is outside valid range";
const char *uintrange="integer argument is outside valid unsigned range";
unsigned unsignedcast(Int n)
{
if(n < 0 || n/2 > INT_MAX)
vm::error(uintrange);
return (unsigned) n;
}
unsignedInt unsignedIntcast(Int n)
{
if(n < 0)
vm::error(uintrange);
return (unsignedInt) n;
}
int intcast(Int n)
{
if(Abs(n) > INT_MAX)
vm::error(intrange);
return (int) n;
}
Int Intcast(unsignedInt n)
{
if(n > (unsignedInt) Int_MAX)
vm::error(intrange);
return (Int) n;
}
|