aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/LuaJIT-1.1.7/src/lua.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/LuaJIT-1.1.7/src/lua.c')
-rw-r--r--libraries/LuaJIT-1.1.7/src/lua.c463
1 files changed, 0 insertions, 463 deletions
diff --git a/libraries/LuaJIT-1.1.7/src/lua.c b/libraries/LuaJIT-1.1.7/src/lua.c
deleted file mode 100644
index a4b413f..0000000
--- a/libraries/LuaJIT-1.1.7/src/lua.c
+++ /dev/null
@@ -1,463 +0,0 @@
1/*
2** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h
5*/
6
7
8#include <signal.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define lua_c
14
15#include "lua.h"
16
17#include "lauxlib.h"
18#include "lualib.h"
19#include "luajit.h"
20
21
22
23static lua_State *globalL = NULL;
24
25static const char *progname = LUA_PROGNAME;
26
27
28
29static void lstop (lua_State *L, lua_Debug *ar) {
30 (void)ar; /* unused arg. */
31 lua_sethook(L, NULL, 0, 0);
32 luaL_error(L, "interrupted!");
33}
34
35
36static void laction (int i) {
37 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
38 terminate process (default action) */
39 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
40}
41
42
43static void print_usage (void) {
44 fprintf(stderr,
45 "usage: %s [options] [script [args]].\n"
46 "Available options are:\n"
47 " -e stat execute string " LUA_QL("stat") "\n"
48 " -l name require library " LUA_QL("name") "\n"
49 " -j cmd perform LuaJIT control command\n"
50 " -O[lvl] set LuaJIT optimization level\n"
51 " -i enter interactive mode after executing " LUA_QL("script") "\n"
52 " -v show version information\n"
53 " -- stop handling options\n"
54 " - execute stdin and stop handling options\n"
55 ,
56 progname);
57 fflush(stderr);
58}
59
60
61static void l_message (const char *pname, const char *msg) {
62 if (pname) fprintf(stderr, "%s: ", pname);
63 fprintf(stderr, "%s\n", msg);
64 fflush(stderr);
65}
66
67
68static int report (lua_State *L, int status) {
69 if (status && !lua_isnil(L, -1)) {
70 const char *msg = lua_tostring(L, -1);
71 if (msg == NULL) msg = "(error object is not a string)";
72 l_message(progname, msg);
73 lua_pop(L, 1);
74 }
75 return status;
76}
77
78
79static int traceback (lua_State *L) {
80 if (!lua_isstring(L, 1)) /* 'message' not a string? */
81 return 1; /* keep it intact */
82 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
83 if (!lua_istable(L, -1)) {
84 lua_pop(L, 1);
85 return 1;
86 }
87 lua_getfield(L, -1, "traceback");
88 if (!lua_isfunction(L, -1)) {
89 lua_pop(L, 2);
90 return 1;
91 }
92 lua_pushvalue(L, 1); /* pass error message */
93 lua_pushinteger(L, 2); /* skip this function and traceback */
94 lua_call(L, 2, 1); /* call debug.traceback */
95 return 1;
96}
97
98
99static int docall (lua_State *L, int narg, int clear) {
100 int status;
101 int base = lua_gettop(L) - narg; /* function index */
102 lua_pushcfunction(L, traceback); /* push traceback function */
103 lua_insert(L, base); /* put it under chunk and args */
104 signal(SIGINT, laction);
105 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
106 signal(SIGINT, SIG_DFL);
107 lua_remove(L, base); /* remove traceback function */
108 /* force a complete garbage collection in case of errors */
109 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
110 return status;
111}
112
113
114static void print_version (void) {
115 l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
116 l_message(NULL, LUAJIT_VERSION " " LUAJIT_COPYRIGHT ", " LUAJIT_URL);
117}
118
119
120static int getargs (lua_State *L, char **argv, int n) {
121 int narg;
122 int i;
123 int argc = 0;
124 while (argv[argc]) argc++; /* count total number of arguments */
125 narg = argc - (n + 1); /* number of arguments to the script */
126 luaL_checkstack(L, narg + 3, "too many arguments to script");
127 for (i=n+1; i < argc; i++)
128 lua_pushstring(L, argv[i]);
129 lua_createtable(L, narg, n + 1);
130 for (i=0; i < argc; i++) {
131 lua_pushstring(L, argv[i]);
132 lua_rawseti(L, -2, i - n);
133 }
134 return narg;
135}
136
137
138static int dofile (lua_State *L, const char *name) {
139 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
140 return report(L, status);
141}
142
143
144static int dostring (lua_State *L, const char *s, const char *name) {
145 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
146 return report(L, status);
147}
148
149
150static int dolibrary (lua_State *L, const char *name) {
151 lua_getglobal(L, "require");
152 lua_pushstring(L, name);
153 return report(L, docall(L, 1, 1));
154}
155
156
157static const char *get_prompt (lua_State *L, int firstline) {
158 const char *p;
159 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
160 p = lua_tostring(L, -1);
161 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
162 lua_pop(L, 1); /* remove global */
163 return p;
164}
165
166
167static int incomplete (lua_State *L, int status) {
168 if (status == LUA_ERRSYNTAX) {
169 size_t lmsg;
170 const char *msg = lua_tolstring(L, -1, &lmsg);
171 const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
172 if (strstr(msg, LUA_QL("<eof>")) == tp) {
173 lua_pop(L, 1);
174 return 1;
175 }
176 }
177 return 0; /* else... */
178}
179
180
181static int pushline (lua_State *L, int firstline) {
182 char buffer[LUA_MAXINPUT];
183 char *b = buffer;
184 size_t l;
185 const char *prmt = get_prompt(L, firstline);
186 if (lua_readline(L, b, prmt) == 0)
187 return 0; /* no input */
188 l = strlen(b);
189 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
190 b[l-1] = '\0'; /* remove it */
191 if (firstline && b[0] == '=') /* first line starts with `=' ? */
192 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
193 else
194 lua_pushstring(L, b);
195 lua_freeline(L, b);
196 return 1;
197}
198
199
200static int loadline (lua_State *L) {
201 int status;
202 lua_settop(L, 0);
203 if (!pushline(L, 1))
204 return -1; /* no input */
205 for (;;) { /* repeat until gets a complete line */
206 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
207 if (!incomplete(L, status)) break; /* cannot try to add lines? */
208 if (!pushline(L, 0)) /* no more input? */
209 return -1;
210 lua_pushliteral(L, "\n"); /* add a new line... */
211 lua_insert(L, -2); /* ...between the two lines */
212 lua_concat(L, 3); /* join them */
213 }
214 lua_saveline(L, 1);
215 lua_remove(L, 1); /* remove line */
216 return status;
217}
218
219
220static void dotty (lua_State *L) {
221 int status;
222 const char *oldprogname = progname;
223 progname = NULL;
224 while ((status = loadline(L)) != -1) {
225 if (status == 0) status = docall(L, 0, 0);
226 report(L, status);
227 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
228 lua_getglobal(L, "print");
229 lua_insert(L, 1);
230 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
231 l_message(progname, lua_pushfstring(L,
232 "error calling " LUA_QL("print") " (%s)",
233 lua_tostring(L, -1)));
234 }
235 }
236 lua_settop(L, 0); /* clear stack */
237 fputs("\n", stdout);
238 fflush(stdout);
239 progname = oldprogname;
240}
241
242
243static int handle_script (lua_State *L, char **argv, int n) {
244 int status;
245 const char *fname;
246 int narg = getargs(L, argv, n); /* collect arguments */
247 lua_setglobal(L, "arg");
248 fname = argv[n];
249 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
250 fname = NULL; /* stdin */
251 status = luaL_loadfile(L, fname);
252 lua_insert(L, -(narg+1));
253 if (status == 0)
254 status = docall(L, narg, 0);
255 else
256 lua_pop(L, narg);
257 return report(L, status);
258}
259
260/* ---- start of LuaJIT extensions */
261
262static int loadjitmodule (lua_State *L, const char *notfound) {
263 lua_getglobal(L, "require");
264 lua_pushliteral(L, "jit.");
265 lua_pushvalue(L, -3);
266 lua_concat(L, 2);
267 if (lua_pcall(L, 1, 1, 0)) {
268 const char *msg = lua_tostring(L, -1);
269 if (msg && !strncmp(msg, "module ", 7)) {
270 l_message(progname, notfound);
271 return 1;
272 }
273 else
274 return report(L, 1);
275 }
276 lua_getfield(L, -1, "start");
277 lua_remove(L, -2); /* drop module table */
278 return 0;
279}
280
281/* JIT engine control command: try jit library first or load add-on module */
282static int dojitcmd (lua_State *L, const char *cmd) {
283 const char *val = strchr(cmd, '=');
284 lua_pushlstring(L, cmd, val ? val - cmd : strlen(cmd));
285 lua_getglobal(L, "jit"); /* get jit.* table */
286 lua_pushvalue(L, -2);
287 lua_gettable(L, -2); /* lookup library function */
288 if (!lua_isfunction(L, -1)) {
289 lua_pop(L, 2); /* drop non-function and jit.* table, keep module name */
290 if (loadjitmodule(L, "unknown luaJIT command"))
291 return 1;
292 }
293 else {
294 lua_remove(L, -2); /* drop jit.* table */
295 }
296 lua_remove(L, -2); /* drop module name */
297 if (val) lua_pushstring(L, val+1);
298 return report(L, lua_pcall(L, val ? 1 : 0, 0, 0));
299}
300
301/* start optimizer */
302static int dojitopt (lua_State *L, const char *opt) {
303 lua_pushliteral(L, "opt");
304 if (loadjitmodule(L, "LuaJIT optimizer module not installed"))
305 return 1;
306 lua_remove(L, -2); /* drop module name */
307 if (*opt) lua_pushstring(L, opt);
308 return report(L, lua_pcall(L, *opt ? 1 : 0, 0, 0));
309}
310
311/* ---- end of LuaJIT extensions */
312
313/* check that argument has no extra characters at the end */
314#define notail(x) {if ((x)[2] != '\0') return -1;}
315
316
317static int collectargs (char **argv, int *pi, int *pv, int *pe) {
318 int i;
319 for (i = 1; argv[i] != NULL; i++) {
320 if (argv[i][0] != '-') /* not an option? */
321 return i;
322 switch (argv[i][1]) { /* option */
323 case '-':
324 notail(argv[i]);
325 return (argv[i+1] != NULL ? i+1 : 0);
326 case '\0':
327 return i;
328 case 'i':
329 notail(argv[i]);
330 *pi = 1; /* go through */
331 case 'v':
332 notail(argv[i]);
333 *pv = 1;
334 break;
335 case 'e':
336 *pe = 1; /* go through */
337 case 'j': /* LuaJIT extension */
338 case 'l':
339 if (argv[i][2] == '\0') {
340 i++;
341 if (argv[i] == NULL) return -1;
342 }
343 break;
344 case 'O': break; /* LuaJIT extension */
345 default: return -1; /* invalid option */
346 }
347 }
348 return 0;
349}
350
351
352static int runargs (lua_State *L, char **argv, int n) {
353 int i;
354 for (i = 1; i < n; i++) {
355 if (argv[i] == NULL) continue;
356 lua_assert(argv[i][0] == '-');
357 switch (argv[i][1]) { /* option */
358 case 'e': {
359 const char *chunk = argv[i] + 2;
360 if (*chunk == '\0') chunk = argv[++i];
361 lua_assert(chunk != NULL);
362 if (dostring(L, chunk, "=(command line)") != 0)
363 return 1;
364 break;
365 }
366 case 'l': {
367 const char *filename = argv[i] + 2;
368 if (*filename == '\0') filename = argv[++i];
369 lua_assert(filename != NULL);
370 if (dolibrary(L, filename))
371 return 1; /* stop if file fails */
372 break;
373 }
374 case 'j': { /* LuaJIT extension */
375 const char *cmd = argv[i] + 2;
376 if (*cmd == '\0') cmd = argv[++i];
377 lua_assert(cmd != NULL);
378 if (dojitcmd(L, cmd))
379 return 1;
380 break;
381 }
382 case 'O': /* LuaJIT extension */
383 if (dojitopt(L, argv[i] + 2))
384 return 1;
385 break;
386 default: break;
387 }
388 }
389 return 0;
390}
391
392
393static int handle_luainit (lua_State *L) {
394 const char *init = getenv(LUA_INIT);
395 if (init == NULL) return 0; /* status OK */
396 else if (init[0] == '@')
397 return dofile(L, init+1);
398 else
399 return dostring(L, init, "=" LUA_INIT);
400}
401
402
403struct Smain {
404 int argc;
405 char **argv;
406 int status;
407};
408
409
410static int pmain (lua_State *L) {
411 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
412 char **argv = s->argv;
413 int script;
414 int has_i = 0, has_v = 0, has_e = 0;
415 globalL = L;
416 if (argv[0] && argv[0][0]) progname = argv[0];
417 LUAJIT_VERSION_SYM(); /* linker-enforced version check */
418 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
419 luaL_openlibs(L); /* open libraries */
420 lua_gc(L, LUA_GCRESTART, 0);
421 s->status = handle_luainit(L);
422 if (s->status != 0) return 0;
423 script = collectargs(argv, &has_i, &has_v, &has_e);
424 if (script < 0) { /* invalid args? */
425 print_usage();
426 s->status = 1;
427 return 0;
428 }
429 if (has_v) print_version();
430 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
431 if (s->status != 0) return 0;
432 if (script)
433 s->status = handle_script(L, argv, script);
434 if (s->status != 0) return 0;
435 if (has_i)
436 dotty(L);
437 else if (script == 0 && !has_e && !has_v) {
438 if (lua_stdin_is_tty()) {
439 print_version();
440 dotty(L);
441 }
442 else dofile(L, NULL); /* executes stdin as a file */
443 }
444 return 0;
445}
446
447
448int main (int argc, char **argv) {
449 int status;
450 struct Smain s;
451 lua_State *L = lua_open(); /* create state */
452 if (L == NULL) {
453 l_message(argv[0], "cannot create state: not enough memory");
454 return EXIT_FAILURE;
455 }
456 s.argc = argc;
457 s.argv = argv;
458 status = lua_cpcall(L, &pmain, &s);
459 report(L, status);
460 lua_close(L);
461 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
462}
463