This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepl.c
319 lines (292 loc) · 9.04 KB
/
repl.c
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
/*
repl.c
system startup, main(), and console interaction
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <signal.h>
#include <assert.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <libgen.h>
#endif
#include <limits.h>
#include <errno.h>
#include <math.h>
#include <getopt.h>
#include <ctype.h>
#include "uv.h"
#define WHOLE_ARCHIVE
#include "../src/julia.h"
#ifndef JL_SYSTEM_IMAGE_PATH
#error "JL_SYSTEM_IMAGE_PATH not defined!"
#endif
#ifdef _MSC_VER
#define PATH_MAX MAX_PATH
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _MSC_VER
DLLEXPORT char * dirname(char *);
#endif
extern int tab_width;
extern DLLEXPORT char *julia_home;
char system_image[256] = JL_SYSTEM_IMAGE_PATH;
static int lisp_prompt = 0;
static int codecov=0;
static char *program = NULL;
char *image_file = NULL;
int tab_width = 2;
static const char *usage = "julia [options] [program] [args...]\n";
static const char *opts =
" -v --version Display version information\n"
" -h --help Print this message\n"
" -q --quiet Quiet startup without banner\n"
" -H --home <dir> Set location of julia executable\n"
" -T --tab <size> Set REPL tab width to <size>\n\n"
" -e --eval <expr> Evaluate <expr>\n"
" -E --print <expr> Evaluate and show <expr>\n"
" -P --post-boot <expr> Evaluate <expr> right after boot\n"
" -L --load file Load <file> right after boot on all processors\n"
" -J --sysimage file Start up with the given system image file\n\n"
" -p n Run n local processes\n"
" --machinefile file Run processes on hosts listed in file\n\n"
" --no-history-file Don't load or save history\n"
" -f --no-startup Don't load ~/.juliarc.jl\n"
" -F Load ~/.juliarc.jl, then handle remaining inputs\n"
" --color=yes|no Enable or disable color text\n\n"
" --code-coverage Count executions of source lines\n"
" --check-bounds=yes|no Emit bounds checks always or never (ignoring declarations)\n"
" --int-literals=32|64 Select integer literal size independent of platform\n";
void parse_opts(int *argcp, char ***argvp)
{
static char* shortopts = "+H:T:hJ:";
static struct option longopts[] = {
{ "home", required_argument, 0, 'H' },
{ "tab", required_argument, 0, 'T' },
{ "build", required_argument, 0, 'b' },
{ "lisp", no_argument, &lisp_prompt, 1 },
{ "help", no_argument, 0, 'h' },
{ "sysimage", required_argument, 0, 'J' },
{ "code-coverage", no_argument, &codecov, 1 },
{ "check-bounds", required_argument, 0, 300 },
{ "int-literals", required_argument, 0, 301 },
{ 0, 0, 0, 0 }
};
int c;
opterr = 0;
int imagepathspecified=0;
image_file = system_image;
int skip = 0;
int lastind = optind;
while ((c = getopt_long(*argcp,*argvp,shortopts,longopts,0)) != -1) {
switch (c) {
case 0:
break;
case '?':
if (optind != lastind) skip++;
lastind = optind;
break;
case 'H':
julia_home = strdup(optarg);
break;
case 'T':
// TODO: more robust error checking.
tab_width = atoi(optarg);
break;
case 'b':
jl_compileropts.build_path = strdup(optarg);
if (!imagepathspecified)
image_file = NULL;
break;
case 'J':
image_file = strdup(optarg);
imagepathspecified = 1;
break;
case 'h':
printf("%s%s", usage, opts);
exit(0);
case 300:
if (!strcmp(optarg,"yes"))
jl_compileropts.check_bounds = JL_COMPILEROPT_CHECK_BOUNDS_ON;
else if (!strcmp(optarg,"no"))
jl_compileropts.check_bounds = JL_COMPILEROPT_CHECK_BOUNDS_OFF;
break;
case 301:
if (!strcmp(optarg,"32"))
jl_compileropts.int_literals = 32;
else if (!strcmp(optarg,"64"))
jl_compileropts.int_literals = 64;
else {
ios_printf(ios_stderr, "julia: invalid integer literal size (%s)\n", optarg);
exit(1);
}
break;
default:
ios_printf(ios_stderr, "julia: unhandled option -- %c\n", c);
ios_printf(ios_stderr, "This is a bug, please report it.\n");
exit(1);
}
}
jl_compileropts.code_coverage = codecov;
if (!julia_home) {
julia_home = getenv("JULIA_HOME");
if (julia_home) {
julia_home = strdup(julia_home);
}
else {
char *julia_path = (char*)malloc(PATH_MAX);
size_t path_size = PATH_MAX;
uv_exepath(julia_path, &path_size);
julia_home = strdup(dirname(julia_path));
free(julia_path);
}
}
optind -= skip;
*argvp += optind;
*argcp -= optind;
if (image_file==NULL && *argcp > 0) {
if (strcmp((*argvp)[0], "-")) {
program = (*argvp)[0];
}
}
if (image_file) {
if (image_file[0] != PATHSEP) {
uv_stat_t stbuf;
char path[512];
if (!imagepathspecified) {
// build time path relative to JULIA_HOME
snprintf(path, sizeof(path), "%s%s%s",
julia_home, PATHSEPSTRING, system_image);
image_file = strdup(path);
}
else if (jl_stat(image_file, (char*)&stbuf) != 0) {
// otherwise try julia_home/../lib/julia/%s
snprintf(path, sizeof(path), "%s%s%s",
julia_home,
PATHSEPSTRING ".." PATHSEPSTRING "lib" PATHSEPSTRING "julia" PATHSEPSTRING,
image_file);
image_file = strdup(path);
}
}
}
}
static int exec_program(void)
{
int err = 0;
again: ;
JL_TRY {
if (err) {
jl_value_t *errs = jl_stderr_obj();
jl_value_t *e = jl_exception_in_transit;
if (errs != NULL) {
jl_show(jl_stderr_obj(), e);
}
else {
jl_printf(JL_STDERR, "error during bootstrap: ");
jl_static_show(JL_STDERR, e);
}
jl_printf(JL_STDERR, "\n");
JL_EH_POP();
return 1;
}
jl_load(program);
}
JL_CATCH {
err = 1;
goto again;
}
return 0;
}
void jl_lisp_prompt();
#ifndef _WIN32
int jl_repl_raise_sigtstp(void)
{
return raise(SIGTSTP);
}
#endif
#ifdef JL_GF_PROFILE
static void print_profile(void)
{
size_t i;
void **table = jl_base_module->bindings.table;
for(i=1; i < jl_base_module->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->value != NULL && jl_is_function(b->value) &&
jl_is_gf(b->value)) {
ios_printf(ios_stdout, "%d\t%s\n",
jl_gf_mtable(b->value)->ncalls,
jl_gf_name(b->value)->name);
}
}
}
}
#endif
int true_main(int argc, char *argv[])
{
if (jl_base_module != NULL) {
jl_array_t *args = (jl_array_t*)jl_get_global(jl_base_module, jl_symbol("ARGS"));
assert(jl_array_len(args) == 0);
jl_array_grow_end(args, argc);
int i;
for (i=0; i < argc; i++) {
jl_value_t *s = (jl_value_t*)jl_cstr_to_string(argv[i]);
s->type = (jl_value_t*)jl_utf8_string_type;
jl_arrayset(args, s, i);
}
}
// run program if specified, otherwise enter REPL
if (program) {
int ret = exec_program();
uv_tty_reset_mode();
return ret;
}
jl_function_t *start_client =
(jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start"));
if (start_client) {
jl_apply(start_client, NULL, 0);
return 0;
}
int iserr = 0;
again:
;
JL_TRY {
if (iserr) {
//jl_show(jl_exception_in_transit);# What if the error was in show?
jl_printf(JL_STDERR, "\n\n");
iserr = 0;
}
uv_run(jl_global_event_loop(),UV_RUN_DEFAULT);
}
JL_CATCH {
iserr = 1;
JL_PUTS("error during run:\n",JL_STDERR);
jl_show(jl_stderr_obj(),jl_exception_in_transit);
JL_PUTS("\n",JL_STDOUT);
goto again;
}
uv_tty_reset_mode();
return iserr;
}
int main(int argc, char *argv[])
{
libsupport_init();
parse_opts(&argc, &argv);
if (lisp_prompt) {
jl_lisp_prompt();
return 0;
}
julia_init(lisp_prompt ? NULL : image_file);
return julia_trampoline(argc, argv, true_main);
}
#ifdef __cplusplus
}
#endif