| 1 |
/* |
| 2 |
|
| 3 |
LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc. |
| 4 |
Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h |
| 5 |
|
| 6 |
Major portions taken verbatim or adapted from the Lua interpreter. |
| 7 |
Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h |
| 8 |
|
| 9 |
*/ |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#define luajit_c |
| 16 |
|
| 17 |
#include "lua.h" |
| 18 |
#include "lauxlib.h" |
| 19 |
#include "lualib.h" |
| 20 |
#include "luajit.h" |
| 21 |
|
| 22 |
#include "lj_arch.h" |
| 23 |
|
| 24 |
#if LJ_TARGET_POSIX |
| 25 |
#include <unistd.h> |
| 26 |
#define lua_stdin_is_tty() isatty(0) |
| 27 |
#elif LJ_TARGET_WINDOWS |
| 28 |
#include <io.h> |
| 29 |
#ifdef __BORLANDC__ |
| 30 |
#define lua_stdin_is_tty() isatty(_fileno(stdin)) |
| 31 |
#else |
| 32 |
#define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
| 33 |
#endif |
| 34 |
#else |
| 35 |
#define lua_stdin_is_tty() 1 |
| 36 |
#endif |
| 37 |
|
| 38 |
#if !LJ_TARGET_CONSOLE |
| 39 |
#include <signal.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "lua/luatex-api.h" |
| 43 |
|
| 44 |
static lua_State *globalL = NULL; |
| 45 |
static const char *progname = LUA_PROGNAME; |
| 46 |
|
| 47 |
#if !LJ_TARGET_CONSOLE |
| 48 |
|
| 49 |
static void lstop(lua_State *L, lua_Debug *ar) |
| 50 |
{ |
| 51 |
/*tex Unused arg. */ |
| 52 |
(void)ar; |
| 53 |
lua_sethook(L, NULL, 0, 0); |
| 54 |
/*tex Avoid luaL_error -- a C hook doesn't add an extra frame. */ |
| 55 |
luaL_where(L, 0); |
| 56 |
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); |
| 57 |
lua_error(L); |
| 58 |
} |
| 59 |
|
| 60 |
/*tex if another SIGINT happens before lstop, terminate process (default action) */ |
| 61 |
|
| 62 |
static void laction(int i) |
| 63 |
{ |
| 64 |
signal(i, SIG_DFL); |
| 65 |
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); |
| 66 |
} |
| 67 |
|
| 68 |
#endif |
| 69 |
|
| 70 |
static void print_usage(void) |
| 71 |
{ |
| 72 |
fprintf(stderr, |
| 73 |
"usage: %s [options]... [script [args]...].\n" |
| 74 |
"Available options are:\n" |
| 75 |
" -e chunk Execute string " LUA_QL("chunk") ".\n" |
| 76 |
" -l name Require library " LUA_QL("name") ".\n" |
| 77 |
" -b ... Save or list bytecode.\n" |
| 78 |
" -j cmd Perform LuaJIT control command.\n" |
| 79 |
" -O[opt] Control LuaJIT optimizations.\n" |
| 80 |
" -i Enter interactive mode after executing " LUA_QL("script") ".\n" |
| 81 |
" -v Show version information.\n" |
| 82 |
" -E Ignore environment variables.\n" |
| 83 |
" -- Stop handling options.\n" |
| 84 |
" - Execute stdin and stop handling options.\n" |
| 85 |
, |
| 86 |
progname); |
| 87 |
fflush(stderr); |
| 88 |
} |
| 89 |
|
| 90 |
static void l_message(const char *pname, const char *msg) |
| 91 |
{ |
| 92 |
if (pname) fprintf(stderr, "%s: ", pname); |
| 93 |
fprintf(stderr, "%s\n", msg); |
| 94 |
fflush(stderr); |
| 95 |
} |
| 96 |
|
| 97 |
static int report(lua_State *L, int status) |
| 98 |
{ |
| 99 |
if (status && !lua_isnil(L, -1)) { |
| 100 |
const char *msg = lua_tostring(L, -1); |
| 101 |
if (msg == NULL) |
| 102 |
msg = "(error object is not a string)"; |
| 103 |
l_message(progname, msg); |
| 104 |
lua_pop(L, 1); |
| 105 |
} |
| 106 |
return status; |
| 107 |
} |
| 108 |
|
| 109 |
static int traceback(lua_State *L) |
| 110 |
{ |
| 111 |
if (!lua_isstring(L, 1)) { |
| 112 |
/*tex Non-string error object? Try metamethod. */ |
| 113 |
if (lua_isnoneornil(L, 1) || !luaL_callmeta(L, 1, "__tostring") || !lua_isstring(L, -1)) { |
| 114 |
/*tex Return non-string error object. */ |
| 115 |
return 1; |
| 116 |
} |
| 117 |
/*tex Replace object by result of __tostring metamethod. */ |
| 118 |
lua_remove(L, 1); |
| 119 |
} |
| 120 |
luaL_traceback(L, L, lua_tostring(L, 1), 1); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
static int docall(lua_State *L, int narg, int clear) |
| 125 |
{ |
| 126 |
int status; |
| 127 |
/*tex function index */ |
| 128 |
int base = lua_gettop(L) - narg; |
| 129 |
/*tex push traceback function */ |
| 130 |
lua_pushcfunction(L, traceback); |
| 131 |
/*tex put it under chunk and args */ |
| 132 |
lua_insert(L, base); |
| 133 |
#if !LJ_TARGET_CONSOLE |
| 134 |
signal(SIGINT, laction); |
| 135 |
#endif |
| 136 |
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); |
| 137 |
#if !LJ_TARGET_CONSOLE |
| 138 |
signal(SIGINT, SIG_DFL); |
| 139 |
#endif |
| 140 |
/*tex remove traceback function */ |
| 141 |
lua_remove(L, base); |
| 142 |
/*tex force a complete garbage collection in case of errors */ |
| 143 |
if (status != 0) |
| 144 |
lua_gc(L, LUA_GCCOLLECT, 0); |
| 145 |
return status; |
| 146 |
} |
| 147 |
|
| 148 |
static void print_version(void) |
| 149 |
{ |
| 150 |
fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout); |
| 151 |
} |
| 152 |
|
| 153 |
static void print_jit_status(lua_State *L) |
| 154 |
{ |
| 155 |
int n; |
| 156 |
const char *s; |
| 157 |
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 158 |
/*tex Get jit.* module table. */ |
| 159 |
lua_getfield(L, -1, "jit"); |
| 160 |
lua_remove(L, -2); |
| 161 |
lua_getfield(L, -1, "status"); |
| 162 |
lua_remove(L, -2); |
| 163 |
n = lua_gettop(L); |
| 164 |
lua_call(L, 0, LUA_MULTRET); |
| 165 |
fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); |
| 166 |
for (n++; (s = lua_tostring(L, n)); n++) { |
| 167 |
putc(' ', stdout); |
| 168 |
fputs(s, stdout); |
| 169 |
} |
| 170 |
putc('\n', stdout); |
| 171 |
} |
| 172 |
|
| 173 |
static int getargs(lua_State *L, char **argv, int n) |
| 174 |
{ |
| 175 |
int narg = 0; |
| 176 |
int i; |
| 177 |
/*tex count total number of arguments */ |
| 178 |
while (argv[argc]) argc++; |
| 179 |
/*tex number of arguments to the script */ |
| 180 |
narg = argc - (n + 1); |
| 181 |
luaL_checkstack(L, narg + 3, "too many arguments to script"); |
| 182 |
for (i = n+1; i < argc; i++) { |
| 183 |
lua_pushstring(L, argv[i]); |
| 184 |
} |
| 185 |
lua_createtable(L, narg, n + 1); |
| 186 |
for (i = 0; i < argc; i++) { |
| 187 |
lua_pushstring(L, argv[i]); |
| 188 |
lua_rawseti(L, -2, i - n); |
| 189 |
} |
| 190 |
return narg; |
| 191 |
} |
| 192 |
|
| 193 |
static int dofile(lua_State *L, const char *name) |
| 194 |
{ |
| 195 |
int status = luaL_loadfile(L, name) || docall(L, 0, 1); |
| 196 |
return report(L, status); |
| 197 |
} |
| 198 |
|
| 199 |
static int dostring(lua_State *L, const char *s, const char *name) |
| 200 |
{ |
| 201 |
int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); |
| 202 |
return report(L, status); |
| 203 |
} |
| 204 |
|
| 205 |
static int dolibrary(lua_State *L, const char *name) |
| 206 |
{ |
| 207 |
lua_getglobal(L, "require"); |
| 208 |
lua_pushstring(L, name); |
| 209 |
return report(L, docall(L, 1, 1)); |
| 210 |
} |
| 211 |
|
| 212 |
static void write_prompt(lua_State *L, int firstline) |
| 213 |
{ |
| 214 |
const char *p; |
| 215 |
lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); |
| 216 |
p = lua_tostring(L, -1); |
| 217 |
if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2; |
| 218 |
fputs(p, stdout); |
| 219 |
fflush(stdout); |
| 220 |
/*tex remove global */ |
| 221 |
lua_pop(L, 1); |
| 222 |
} |
| 223 |
|
| 224 |
static int incomplete(lua_State *L, int status) |
| 225 |
{ |
| 226 |
if (status == LUA_ERRSYNTAX) { |
| 227 |
size_t lmsg; |
| 228 |
const char *msg = lua_tolstring(L, -1, &lmsg); |
| 229 |
const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1); |
| 230 |
if (strstr(msg, LUA_QL("<eof>")) == tp) { |
| 231 |
lua_pop(L, 1); |
| 232 |
return 1; |
| 233 |
} |
| 234 |
} |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static int pushline(lua_State *L, int firstline) |
| 239 |
{ |
| 240 |
char buf[LUA_MAXINPUT]; |
| 241 |
write_prompt(L, firstline); |
| 242 |
if (fgets(buf, LUA_MAXINPUT, stdin)) { |
| 243 |
size_t len = strlen(buf); |
| 244 |
if (len > 0 && buf[len-1] == '\n') |
| 245 |
buf[len-1] = '\0'; |
| 246 |
if (firstline && buf[0] == '=') |
| 247 |
lua_pushfstring(L, "return %s", buf+1); |
| 248 |
else |
| 249 |
lua_pushstring(L, buf); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static int loadline(lua_State *L) |
| 256 |
{ |
| 257 |
int status; |
| 258 |
lua_settop(L, 0); |
| 259 |
if (!pushline(L, 1)) { |
| 260 |
/*tex no input */ |
| 261 |
return -1; |
| 262 |
} |
| 263 |
for (;;) { |
| 264 |
/*tex repeat until gets a complete line */ |
| 265 |
status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); |
| 266 |
/*tex cannot try to add lines? */ |
| 267 |
if (!incomplete(L, status)) |
| 268 |
break; |
| 269 |
/*tex no more input? */ |
| 270 |
if (!pushline(L, 0)) |
| 271 |
return -1; |
| 272 |
/*tex add a new line... */ |
| 273 |
lua_pushliteral(L, "\n"); |
| 274 |
/*tex ...between the two lines */ |
| 275 |
lua_insert(L, -2); |
| 276 |
/*tex join them */ |
| 277 |
lua_concat(L, 3); |
| 278 |
} |
| 279 |
/*tex remove line */ |
| 280 |
lua_remove(L, 1); |
| 281 |
return status; |
| 282 |
} |
| 283 |
|
| 284 |
static void dotty(lua_State *L) |
| 285 |
{ |
| 286 |
int status; |
| 287 |
const char *oldprogname = progname; |
| 288 |
progname = NULL; |
| 289 |
while ((status = loadline(L)) != -1) { |
| 290 |
if (status == 0) |
| 291 |
status = docall(L, 0, 0); |
| 292 |
report(L, status); |
| 293 |
if (status == 0 && lua_gettop(L) > 0) { |
| 294 |
/*tex any result to print? */ |
| 295 |
lua_getglobal(L, "print"); |
| 296 |
lua_insert(L, 1); |
| 297 |
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) |
| 298 |
l_message(progname, |
| 299 |
lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", |
| 300 |
lua_tostring(L, -1))); |
| 301 |
} |
| 302 |
} |
| 303 |
/*tex clear stack */ |
| 304 |
lua_settop(L, 0); |
| 305 |
fputs("\n", stdout); |
| 306 |
fflush(stdout); |
| 307 |
progname = oldprogname; |
| 308 |
} |
| 309 |
|
| 310 |
static int handle_script(lua_State *L, char **argv, int n) |
| 311 |
{ |
| 312 |
int status; |
| 313 |
const char *fname; |
| 314 |
/*tex collect arguments */ |
| 315 |
int narg = getargs(L, argv, n); |
| 316 |
lua_setglobal(L, "arg"); |
| 317 |
fname = argv[n]; |
| 318 |
if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) { |
| 319 |
/*tex use stdin */ |
| 320 |
fname = NULL; |
| 321 |
} |
| 322 |
status = luaL_loadfile(L, fname); |
| 323 |
lua_insert(L, -(narg+1)); |
| 324 |
if (status == 0) |
| 325 |
status = docall(L, narg, 0); |
| 326 |
else |
| 327 |
lua_pop(L, narg); |
| 328 |
return report(L, status); |
| 329 |
} |
| 330 |
|
| 331 |
/*tex Load add-on module. */ |
| 332 |
|
| 333 |
static int loadjitmodule(lua_State *L) |
| 334 |
{ |
| 335 |
lua_getglobal(L, "require"); |
| 336 |
lua_pushliteral(L, "jit."); |
| 337 |
lua_pushvalue(L, -3); |
| 338 |
lua_concat(L, 2); |
| 339 |
if (lua_pcall(L, 1, 1, 0)) { |
| 340 |
const char *msg = lua_tostring(L, -1); |
| 341 |
if (msg && !strncmp(msg, "module ", 7)) { |
| 342 |
err: |
| 343 |
l_message(progname, |
| 344 |
"unknown luaJIT command or jit.* modules not installed"); |
| 345 |
return 1; |
| 346 |
} else { |
| 347 |
return report(L, 1); |
| 348 |
} |
| 349 |
} |
| 350 |
lua_getfield(L, -1, "start"); |
| 351 |
if (lua_isnil(L, -1)) |
| 352 |
goto err; |
| 353 |
/*tex Drop module table. */ |
| 354 |
lua_remove(L, -2); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
/*tex Run command with options. */ |
| 359 |
|
| 360 |
static int runcmdopt(lua_State *L, const char *opt) |
| 361 |
{ |
| 362 |
int narg = 0; |
| 363 |
if (opt && *opt) { |
| 364 |
/*tex Split arguments. */ |
| 365 |
for (;;) { |
| 366 |
const char *p = strchr(opt, ','); |
| 367 |
narg++; |
| 368 |
if (!p) |
| 369 |
break; |
| 370 |
if (p == opt) |
| 371 |
lua_pushnil(L); |
| 372 |
else |
| 373 |
lua_pushlstring(L, opt, (size_t)(p - opt)); |
| 374 |
opt = p + 1; |
| 375 |
} |
| 376 |
if (*opt) |
| 377 |
lua_pushstring(L, opt); |
| 378 |
else |
| 379 |
lua_pushnil(L); |
| 380 |
} |
| 381 |
return report(L, lua_pcall(L, narg, 0, 0)); |
| 382 |
} |
| 383 |
|
| 384 |
/*tex JIT engine control command: try jit library first or load add-on module. */ |
| 385 |
|
| 386 |
static int dojitcmd(lua_State *L, const char *cmd) |
| 387 |
{ |
| 388 |
const char *opt = strchr(cmd, '='); |
| 389 |
lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); |
| 390 |
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 391 |
/*tex Get jit.* module table. */ |
| 392 |
lua_getfield(L, -1, "jit"); |
| 393 |
lua_remove(L, -2); |
| 394 |
/*tex Lookup library function. */ |
| 395 |
lua_pushvalue(L, -2); |
| 396 |
lua_gettable(L, -2); |
| 397 |
if (!lua_isfunction(L, -1)) { |
| 398 |
/*tex Drop non-function and jit.* table, keep module name. */ |
| 399 |
lua_pop(L, 2); |
| 400 |
if (loadjitmodule(L)) |
| 401 |
return 1; |
| 402 |
} else { |
| 403 |
/*tex Drop jit.* table. */ |
| 404 |
lua_remove(L, -2); |
| 405 |
} |
| 406 |
/*tex Drop module name. */ |
| 407 |
lua_remove(L, -2); |
| 408 |
return runcmdopt(L, opt ? opt+1 : opt); |
| 409 |
} |
| 410 |
|
| 411 |
/*tex Optimization flags. */ |
| 412 |
|
| 413 |
static int dojitopt(lua_State *L, const char *opt) |
| 414 |
{ |
| 415 |
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 416 |
lua_getfield(L, -1, "jit.opt"); |
| 417 |
lua_remove(L, -2); |
| 418 |
lua_getfield(L, -1, "start"); |
| 419 |
lua_remove(L, -2); |
| 420 |
return runcmdopt(L, opt); |
| 421 |
} |
| 422 |
|
| 423 |
/*tex Save or list bytecode. */ |
| 424 |
|
| 425 |
static int dobytecode(lua_State *L, char **argv) |
| 426 |
{ |
| 427 |
int narg = 0; |
| 428 |
lua_pushliteral(L, "bcsave"); |
| 429 |
if (loadjitmodule(L)) |
| 430 |
return 1; |
| 431 |
if (argv[0][2]) { |
| 432 |
narg++; |
| 433 |
argv[0][1] = '-'; |
| 434 |
lua_pushstring(L, argv[0]+1); |
| 435 |
} |
| 436 |
for (argv++; *argv != NULL; narg++, argv++) |
| 437 |
lua_pushstring(L, *argv); |
| 438 |
return report(L, lua_pcall(L, narg, 0, 0)); |
| 439 |
} |
| 440 |
|
| 441 |
/*tex Check that argument has no extra characters at the end */ |
| 442 |
|
| 443 |
#define notail(x) {if ((x)[2] != '\0') return -1;} |
| 444 |
|
| 445 |
#define FLAGS_INTERACTIVE 1 |
| 446 |
#define FLAGS_VERSION 2 |
| 447 |
#define FLAGS_EXEC 4 |
| 448 |
#define FLAGS_OPTION 8 |
| 449 |
#define FLAGS_NOENV 16 |
| 450 |
|
| 451 |
static int collectargs(char **argv, int *flags) |
| 452 |
{ |
| 453 |
int i; |
| 454 |
for (i = 1; argv[i] != NULL; i++) { |
| 455 |
/*tex Not an option? */ |
| 456 |
if (argv[i][0] != '-') |
| 457 |
return i; |
| 458 |
/*tex Check option. */ |
| 459 |
switch (argv[i][1]) { |
| 460 |
case '-': |
| 461 |
notail(argv[i]); |
| 462 |
return (argv[i+1] != NULL ? i+1 : 0); |
| 463 |
case '\0': |
| 464 |
return i; |
| 465 |
case 'i': |
| 466 |
notail(argv[i]); |
| 467 |
*flags |= FLAGS_INTERACTIVE; |
| 468 |
/*tex fallthrough */ |
| 469 |
case 'v': |
| 470 |
notail(argv[i]); |
| 471 |
*flags |= FLAGS_VERSION; |
| 472 |
break; |
| 473 |
case 'e': |
| 474 |
*flags |= FLAGS_EXEC; |
| 475 |
case 'j': |
| 476 |
/*tex LuaJIT extension */ |
| 477 |
case 'l': |
| 478 |
*flags |= FLAGS_OPTION; |
| 479 |
if (argv[i][2] == '\0') { |
| 480 |
i++; |
| 481 |
if (argv[i] == NULL) |
| 482 |
return -1; |
| 483 |
} |
| 484 |
break; |
| 485 |
case 'O': |
| 486 |
/*tex LuaJIT extension */ |
| 487 |
break; |
| 488 |
case 'b': |
| 489 |
/*tex LuaJIT extension */ |
| 490 |
if (*flags) return -1; |
| 491 |
*flags |= FLAGS_EXEC; |
| 492 |
return 0; |
| 493 |
case 'E': |
| 494 |
*flags |= FLAGS_NOENV; |
| 495 |
break; |
| 496 |
default: |
| 497 |
/*tex invalid option */ |
| 498 |
return -1; |
| 499 |
} |
| 500 |
} |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
static int runargs(lua_State *L, char **argv, int n) |
| 505 |
{ |
| 506 |
int i; |
| 507 |
for (i = 1; i < n; i++) { |
| 508 |
if (argv[i] == NULL) |
| 509 |
continue; |
| 510 |
lua_assert(argv[i][0] == '-'); |
| 511 |
switch (argv[i][1]) { |
| 512 |
/*tex Options */ |
| 513 |
case 'e': { |
| 514 |
const char *chunk = argv[i] + 2; |
| 515 |
if (*chunk == '\0') |
| 516 |
chunk = argv[++i]; |
| 517 |
lua_assert(chunk != NULL); |
| 518 |
if (dostring(L, chunk, "=(command line)") != 0) |
| 519 |
return 1; |
| 520 |
break; |
| 521 |
} |
| 522 |
case 'l': { |
| 523 |
const char *filename = argv[i] + 2; |
| 524 |
if (*filename == '\0') |
| 525 |
filename = argv[++i]; |
| 526 |
lua_assert(filename != NULL); |
| 527 |
if (dolibrary(L, filename)) { |
| 528 |
/*tex stop if file fails */ |
| 529 |
return 1; |
| 530 |
} |
| 531 |
break; |
| 532 |
} |
| 533 |
case 'j': { |
| 534 |
/*tex LuaJIT extension */ |
| 535 |
const char *cmd = argv[i] + 2; |
| 536 |
if (*cmd == '\0') |
| 537 |
cmd = argv[++i]; |
| 538 |
lua_assert(cmd != NULL); |
| 539 |
if (dojitcmd(L, cmd)) |
| 540 |
return 1; |
| 541 |
break; |
| 542 |
} |
| 543 |
case 'O': |
| 544 |
/*tex LuaJIT extension */ |
| 545 |
if (dojitopt(L, argv[i] + 2)) |
| 546 |
return 1; |
| 547 |
break; |
| 548 |
case 'b': |
| 549 |
/*tex LuaJIT extension */ |
| 550 |
return dobytecode(L, argv+i); |
| 551 |
default: break; |
| 552 |
} |
| 553 |
} |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
static int handle_luainit(lua_State *L) |
| 558 |
{ |
| 559 |
#if LJ_TARGET_CONSOLE |
| 560 |
const char *init = NULL; |
| 561 |
#else |
| 562 |
const char *init = getenv(LUA_INIT); |
| 563 |
#endif |
| 564 |
if (init == NULL) { |
| 565 |
/*tex status OK */ |
| 566 |
return 0; |
| 567 |
} else if (init[0] == 64) { |
| 568 |
return dofile(L, init+1); |
| 569 |
} else { |
| 570 |
return dostring(L, init, "=" LUA_INIT); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
struct Smain { |
| 575 |
char **argv; |
| 576 |
int argc; |
| 577 |
int status; |
| 578 |
}; |
| 579 |
|
| 580 |
static int pmain(lua_State *L) |
| 581 |
{ |
| 582 |
struct Smain *s = (struct Smain *)lua_touserdata(L, 1); |
| 583 |
char **argv = s->argv; |
| 584 |
int script; |
| 585 |
int flags = 0; |
| 586 |
globalL = L; |
| 587 |
if (argv[0] && argv[0][0]) |
| 588 |
progname = argv[0]; |
| 589 |
/*tex linker-enforced version check */ |
| 590 |
LUAJIT_VERSION_SYM(); |
| 591 |
script = collectargs(argv, &flags); |
| 592 |
if (script < 0) { |
| 593 |
/*tex invalid args? */ |
| 594 |
print_usage(); |
| 595 |
s->status = 1; |
| 596 |
return 0; |
| 597 |
} |
| 598 |
if ((flags & FLAGS_NOENV)) { |
| 599 |
lua_pushboolean(L, 1); |
| 600 |
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); |
| 601 |
} |
| 602 |
/*tex stop collector during initialization */ |
| 603 |
lua_gc(L, LUA_GCSTOP, 0); |
| 604 |
/*tex open libraries */ |
| 605 |
luaL_openlibs(L); |
| 606 |
lua_gc(L, LUA_GCRESTART, -1); |
| 607 |
if (!(flags & FLAGS_NOENV)) { |
| 608 |
s->status = handle_luainit(L); |
| 609 |
if (s->status != 0) return 0; |
| 610 |
} |
| 611 |
if ((flags & FLAGS_VERSION)) print_version(); |
| 612 |
s->status = runargs(L, argv, (script > 0) ? script : s->argc); |
| 613 |
if (s->status != 0) return 0; |
| 614 |
if (script) { |
| 615 |
s->status = handle_script(L, argv, script); |
| 616 |
if (s->status != 0) return 0; |
| 617 |
} |
| 618 |
if ((flags & FLAGS_INTERACTIVE)) { |
| 619 |
print_jit_status(L); |
| 620 |
dotty(L); |
| 621 |
} else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) { |
| 622 |
if (lua_stdin_is_tty()) { |
| 623 |
print_version(); |
| 624 |
print_jit_status(L); |
| 625 |
dotty(L); |
| 626 |
} else { |
| 627 |
/*tex executes stdin as a file */ |
| 628 |
dofile(L, NULL); |
| 629 |
} |
| 630 |
} |
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
int luac_main(int argc, char **argv) |
| 635 |
{ |
| 636 |
int status; |
| 637 |
struct Smain s; |
| 638 |
lua_State *L = lua_open(); /* create state */ |
| 639 |
if (L == NULL) { |
| 640 |
l_message(argv[0], "cannot create state: not enough memory"); |
| 641 |
return EXIT_FAILURE; |
| 642 |
} |
| 643 |
s.argc = argc; |
| 644 |
s.argv = argv; |
| 645 |
status = lua_cpcall(L, pmain, &s); |
| 646 |
report(L, status); |
| 647 |
lua_close(L); |
| 648 |
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; |
| 649 |
} |
| 650 |
|