Add memtest support.
[syslinux-debian/hramrach.git] / com32 / lua / src / lua.c
blobdaa73c1f6ec420bed3dba7a21204fbf858ad9f0c
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 */
7 #ifndef SYSLINUX
8 #include <signal.h>
9 #else
10 #define signal(x,y)
11 #endif
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #ifdef SYSLINUX
17 #include <console.h>
18 #endif
20 #define lua_c
22 #include "lua.h"
24 #include "lauxlib.h"
25 #include "lualib.h"
29 static lua_State *globalL = NULL;
31 static const char *progname = LUA_PROGNAME;
34 #ifndef SYSLINUX
35 static void lstop (lua_State *L, lua_Debug *ar) {
36 (void)ar; /* unused arg. */
37 lua_sethook(L, NULL, 0, 0);
38 luaL_error(L, "interrupted!");
41 static void laction (int i) {
42 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
43 terminate process (default action) */
44 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
46 #endif
48 static void print_usage (void) {
49 fprintf(stderr,
50 "usage: %s [options] [script [args]].\n"
51 "Available options are:\n"
52 " -e stat execute string " LUA_QL("stat") "\n"
53 " -l name require library " LUA_QL("name") "\n"
54 " -i enter interactive mode after executing " LUA_QL("script") "\n"
55 " -v show version information\n"
56 " -- stop handling options\n"
57 " - execute stdin and stop handling options\n"
59 progname);
60 fflush(stderr);
64 static void l_message (const char *pname, const char *msg) {
65 if (pname) fprintf(stderr, "%s: ", pname);
66 fprintf(stderr, "%s\n", msg);
67 fflush(stderr);
71 static int report (lua_State *L, int status) {
72 if (status && !lua_isnil(L, -1)) {
73 const char *msg = lua_tostring(L, -1);
74 if (msg == NULL) msg = "(error object is not a string)";
75 l_message(progname, msg);
76 lua_pop(L, 1);
78 return status;
82 static int traceback (lua_State *L) {
83 if (!lua_isstring(L, 1)) /* 'message' not a string? */
84 return 1; /* keep it intact */
85 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
86 if (!lua_istable(L, -1)) {
87 lua_pop(L, 1);
88 return 1;
90 lua_getfield(L, -1, "traceback");
91 if (!lua_isfunction(L, -1)) {
92 lua_pop(L, 2);
93 return 1;
95 lua_pushvalue(L, 1); /* pass error message */
96 lua_pushinteger(L, 2); /* skip this function and traceback */
97 lua_call(L, 2, 1); /* call debug.traceback */
98 return 1;
102 static int docall (lua_State *L, int narg, int clear) {
103 int status;
104 int base = lua_gettop(L) - narg; /* function index */
105 lua_pushcfunction(L, traceback); /* push traceback function */
106 lua_insert(L, base); /* put it under chunk and args */
107 signal(SIGINT, laction);
108 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
109 signal(SIGINT, SIG_DFL);
110 lua_remove(L, base); /* remove traceback function */
111 /* force a complete garbage collection in case of errors */
112 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
113 return status;
116 static void print_version (void) {
117 l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
121 static int getargs (lua_State *L, char **argv, int n) {
122 int narg;
123 int i;
124 int argc = 0;
125 while (argv[argc]) argc++; /* count total number of arguments */
126 narg = argc - (n + 1); /* number of arguments to the script */
127 luaL_checkstack(L, narg + 3, "too many arguments to script");
128 for (i=n+1; i < argc; i++)
129 lua_pushstring(L, argv[i]);
130 lua_createtable(L, narg, n + 1);
131 for (i=0; i < argc; i++) {
132 lua_pushstring(L, argv[i]);
133 lua_rawseti(L, -2, i - n);
135 return narg;
139 static int dofile (lua_State *L, const char *name) {
140 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
141 return report(L, status);
145 static int dostring (lua_State *L, const char *s, const char *name) {
146 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
147 return report(L, status);
151 static int dolibrary (lua_State *L, const char *name) {
152 lua_getglobal(L, "require");
153 lua_pushstring(L, name);
154 return report(L, docall(L, 1, 1));
158 static const char *get_prompt (lua_State *L, int firstline) {
159 const char *p;
160 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
161 p = lua_tostring(L, -1);
162 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
163 lua_pop(L, 1); /* remove global */
164 return p;
168 static int incomplete (lua_State *L, int status) {
169 if (status == LUA_ERRSYNTAX) {
170 size_t lmsg;
171 const char *msg = lua_tolstring(L, -1, &lmsg);
172 const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
173 if (strstr(msg, LUA_QL("<eof>")) == tp) {
174 lua_pop(L, 1);
175 return 1;
178 return 0; /* else... */
182 static int pushline (lua_State *L, int firstline) {
183 char buffer[LUA_MAXINPUT];
184 char *b = buffer;
185 size_t l;
186 const char *prmt = get_prompt(L, firstline);
187 if (lua_readline(L, b, prmt) == 0)
188 return 0; /* no input */
189 l = strlen(b);
190 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
191 b[l-1] = '\0'; /* remove it */
192 if (firstline && b[0] == '=') /* first line starts with `=' ? */
193 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
194 else
195 lua_pushstring(L, b);
196 lua_freeline(L, b);
197 return 1;
201 static int loadline (lua_State *L) {
202 int status;
203 lua_settop(L, 0);
204 if (!pushline(L, 1))
205 return -1; /* no input */
206 for (;;) { /* repeat until gets a complete line */
207 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
208 if (!incomplete(L, status)) break; /* cannot try to add lines? */
209 if (!pushline(L, 0)) /* no more input? */
210 return -1;
211 lua_pushliteral(L, "\n"); /* add a new line... */
212 lua_insert(L, -2); /* ...between the two lines */
213 lua_concat(L, 3); /* join them */
215 lua_saveline(L, 1);
216 lua_remove(L, 1); /* remove line */
217 return status;
221 static void dotty (lua_State *L) {
222 int status;
223 const char *oldprogname = progname;
224 progname = NULL;
225 while ((status = loadline(L)) != -1) {
226 if (status == 0) status = docall(L, 0, 0);
227 report(L, status);
228 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
229 lua_getglobal(L, "print");
230 lua_insert(L, 1);
231 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
232 l_message(progname, lua_pushfstring(L,
233 "error calling " LUA_QL("print") " (%s)",
234 lua_tostring(L, -1)));
237 lua_settop(L, 0); /* clear stack */
238 fputs("\n", stdout);
239 fflush(stdout);
240 progname = oldprogname;
244 static int handle_script (lua_State *L, char **argv, int n) {
245 int status;
246 const char *fname;
247 int narg = getargs(L, argv, n); /* collect arguments */
248 lua_setglobal(L, "arg");
249 fname = argv[n];
250 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
251 fname = NULL; /* stdin */
252 status = luaL_loadfile(L, fname);
253 lua_insert(L, -(narg+1));
254 if (status == 0)
255 status = docall(L, narg, 0);
256 else
257 lua_pop(L, narg);
258 return report(L, status);
262 /* check that argument has no extra characters at the end */
263 #define notail(x) {if ((x)[2] != '\0') return -1;}
266 static int collectargs (char **argv, int *pi, int *pv, int *pe) {
267 int i;
268 for (i = 1; argv[i] != NULL; i++) {
269 if (argv[i][0] != '-') /* not an option? */
270 return i;
271 switch (argv[i][1]) { /* option */
272 case '-':
273 notail(argv[i]);
274 return (argv[i+1] != NULL ? i+1 : 0);
275 case '\0':
276 return i;
277 case 'i':
278 notail(argv[i]);
279 *pi = 1; /* go through */
280 case 'v':
281 notail(argv[i]);
282 *pv = 1;
283 break;
284 case 'e':
285 *pe = 1; /* go through */
286 case 'l':
287 if (argv[i][2] == '\0') {
288 i++;
289 if (argv[i] == NULL) return -1;
291 break;
292 default: return -1; /* invalid option */
295 return 0;
299 static int runargs (lua_State *L, char **argv, int n) {
300 int i;
301 for (i = 1; i < n; i++) {
302 if (argv[i] == NULL) continue;
303 lua_assert(argv[i][0] == '-');
304 switch (argv[i][1]) { /* option */
305 case 'e': {
306 const char *chunk = argv[i] + 2;
307 if (*chunk == '\0') chunk = argv[++i];
308 lua_assert(chunk != NULL);
309 if (dostring(L, chunk, "=(command line)") != 0)
310 return 1;
311 break;
313 case 'l': {
314 const char *filename = argv[i] + 2;
315 if (*filename == '\0') filename = argv[++i];
316 lua_assert(filename != NULL);
317 if (dolibrary(L, filename))
318 return 1; /* stop if file fails */
319 break;
321 default: break;
324 return 0;
328 static int handle_luainit (lua_State *L) {
329 #ifndef SYSLINUX
330 const char *init = getenv(LUA_INIT);
331 #else
332 const char *init = NULL;
333 #endif
334 if (init == NULL) return 0; /* status OK */
335 else if (init[0] == '@')
336 return dofile(L, init+1);
337 else
338 return dostring(L, init, "=" LUA_INIT);
342 struct Smain {
343 int argc;
344 char **argv;
345 int status;
349 static int pmain (lua_State *L) {
350 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
351 char **argv = s->argv;
352 int script;
353 int has_i = 0, has_v = 0, has_e = 0;
354 globalL = L;
355 if (argv[0] && argv[0][0]) progname = argv[0];
356 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
357 luaL_openlibs(L); /* open libraries */
358 lua_gc(L, LUA_GCRESTART, 0);
359 s->status = handle_luainit(L);
360 if (s->status != 0) return 0;
361 script = collectargs(argv, &has_i, &has_v, &has_e);
362 if (script < 0) { /* invalid args? */
363 print_usage();
364 s->status = 1;
365 return 0;
367 if (has_v) print_version();
368 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
369 if (s->status != 0) return 0;
370 if (script)
371 s->status = handle_script(L, argv, script);
372 if (s->status != 0) return 0;
373 if (has_i)
374 dotty(L);
375 else if (script == 0 && !has_e && !has_v) {
376 if (lua_stdin_is_tty()) {
377 print_version();
378 dotty(L);
380 else dofile(L, NULL); /* executes stdin as a file */
382 return 0;
386 int main (int argc, char **argv) {
387 int status;
388 struct Smain s;
389 #ifdef SYSLINUX
390 openconsole(&dev_stdcon_r, &dev_stdcon_w);
391 #endif
392 lua_State *L = lua_open(); /* create state */
393 if (L == NULL) {
394 l_message(argv[0], "cannot create state: not enough memory");
395 return EXIT_FAILURE;
397 s.argc = argc;
398 s.argv = argv;
399 status = lua_cpcall(L, &pmain, &s);
400 report(L, status);
401 lua_close(L);
402 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;