Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lua.c File Reference
#include "lprefix.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define lua_c
 
#define LUA_PROMPT   "> "
 
#define LUA_PROMPT2   ">> "
 
#define LUA_PROGNAME   "lua"
 
#define LUA_MAXINPUT   512
 
#define LUA_INIT_VAR   "LUA_INIT"
 
#define LUA_INITVARVERSION   LUA_INIT_VAR LUA_VERSUFFIX
 
#define lua_stdin_is_tty()   1 /* assume stdin is a tty */
 
#define lua_readline(L, b, p)
 
#define lua_saveline(L, line)   { (void)L; (void)line; }
 
#define lua_freeline(L, b)   { (void)L; (void)b; }
 
#define EOFMARK   "<eof>"
 
#define marklen   (sizeof(EOFMARK)/sizeof(char) - 1)
 
#define has_error   1 /* bad option */
 
#define has_i   2 /* -i */
 
#define has_v   4 /* -v */
 
#define has_e   8 /* -e */
 
#define has_E   16 /* -E */
 

Functions

static void lstop (lua_State *L, lua_Debug *ar)
 
static void laction (int i)
 
static void print_usage (const char *badoption)
 
static void l_message (const char *pname, const char *msg)
 
static int report (lua_State *L, int status)
 
static int msghandler (lua_State *L)
 
static int docall (lua_State *L, int narg, int nres)
 
static void print_version (void)
 
static void createargtable (lua_State *L, char **argv, int argc, int script)
 
static int dochunk (lua_State *L, int status)
 
static int dofile (lua_State *L, const char *name)
 
static int dostring (lua_State *L, const char *s, const char *name)
 
static int dolibrary (lua_State *L, const char *name)
 
static const char * get_prompt (lua_State *L, int firstline)
 
static int incomplete (lua_State *L, int status)
 
static int pushline (lua_State *L, int firstline)
 
static int addreturn (lua_State *L)
 
static int multiline (lua_State *L)
 
static int loadline (lua_State *L)
 
static void l_print (lua_State *L)
 
static void doREPL (lua_State *L)
 
static int pushargs (lua_State *L)
 
static int handle_script (lua_State *L, char **argv)
 
static int collectargs (char **argv, int *first)
 
static int runargs (lua_State *L, char **argv, int n)
 
static int handle_luainit (lua_State *L)
 
static int pmain (lua_State *L)
 
int main (int argc, char **argv)
 

Variables

static lua_StateglobalL = NULL
 
static const char * progname = LUA_PROGNAME
 

Macro Definition Documentation

◆ EOFMARK

#define EOFMARK   "<eof>"

Definition at line 282 of file lua-5.3.6/src/lua.c.

Referenced by incomplete().

◆ has_e

#define has_e   8 /* -e */

Definition at line 456 of file lua-5.3.6/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_E

#define has_E   16 /* -E */

Definition at line 457 of file lua-5.3.6/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_error

#define has_error   1 /* bad option */

Definition at line 453 of file lua-5.3.6/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_i

#define has_i   2 /* -i */

Definition at line 454 of file lua-5.3.6/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_v

#define has_v   4 /* -v */

Definition at line 455 of file lua-5.3.6/src/lua.c.

Referenced by collectargs(), and pmain().

◆ lua_c

#define lua_c

Definition at line 7 of file lua-5.3.6/src/lua.c.

◆ lua_freeline

#define lua_freeline ( L,
b )   { (void)L; (void)b; }

Definition at line 94 of file lua-5.3.6/src/lua.c.

Referenced by pushline().

◆ LUA_INIT_VAR

#define LUA_INIT_VAR   "LUA_INIT"

Definition at line 38 of file lua-5.3.6/src/lua.c.

Referenced by handle_luainit().

◆ LUA_INITVARVERSION

#define LUA_INITVARVERSION   LUA_INIT_VAR LUA_VERSUFFIX

Definition at line 41 of file lua-5.3.6/src/lua.c.

Referenced by handle_luainit().

◆ LUA_MAXINPUT

#define LUA_MAXINPUT   512

Definition at line 34 of file lua-5.3.6/src/lua.c.

Referenced by pushline().

◆ LUA_PROGNAME

#define LUA_PROGNAME   "lua"

Definition at line 30 of file lua-5.3.6/src/lua.c.

◆ LUA_PROMPT

#define LUA_PROMPT   "> "

Definition at line 25 of file lua-5.3.6/src/lua.c.

Referenced by get_prompt().

◆ LUA_PROMPT2

#define LUA_PROMPT2   ">> "

Definition at line 26 of file lua-5.3.6/src/lua.c.

Referenced by get_prompt().

◆ lua_readline

#define lua_readline ( L,
b,
p )
Value:
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
#define NULL
Definition gmacros.h:924
#define LUA_MAXINPUT

Definition at line 90 of file lua-5.3.6/src/lua.c.

90#define lua_readline(L,b,p) \
91 ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
92 fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */

Referenced by pushline().

◆ lua_saveline

#define lua_saveline ( L,
line )   { (void)L; (void)line; }

Definition at line 93 of file lua-5.3.6/src/lua.c.

Referenced by addreturn(), and multiline().

◆ lua_stdin_is_tty

#define lua_stdin_is_tty ( )    1 /* assume stdin is a tty */

Definition at line 65 of file lua-5.3.6/src/lua.c.

Referenced by pmain().

◆ marklen

#define marklen   (sizeof(EOFMARK)/sizeof(char) - 1)

Definition at line 283 of file lua-5.3.6/src/lua.c.

Referenced by incomplete().

Function Documentation

◆ addreturn()

static int addreturn ( lua_State * L)
static

Definition at line 332 of file lua-5.3.6/src/lua.c.

332 {
333 const char *line = lua_tostring(L, -1); /* original line */
334 const char *retline = lua_pushfstring(L, "return %s;", line);
335 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
336 if (status == LUA_OK) {
337 lua_remove(L, -2); /* remove modified line */
338 if (line[0] != '\0') /* non empty? */
339 lua_saveline(L, line); /* keep history */
340 }
341 else
342 lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
343 return status;
344}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
#define lua_pop(L, n)
#define lua_tostring(L, i)
#define luaL_loadbuffer(L, s, sz, n)
#define LUA_OK
#define lua_saveline(L, line)
#define lua_remove(L, idx)

References LUA_OK, lua_pop, lua_pushfstring(), lua_remove, lua_saveline, lua_tostring, and luaL_loadbuffer.

Referenced by loadline().

◆ collectargs()

static int collectargs ( char ** argv,
int * first )
static

Definition at line 465 of file lua-5.3.6/src/lua.c.

465 {
466 int args = 0;
467 int i;
468 for (i = 1; argv[i] != NULL; i++) {
469 *first = i;
470 if (argv[i][0] != '-') /* not an option? */
471 return args; /* stop handling options */
472 switch (argv[i][1]) { /* else check option */
473 case '-': /* '--' */
474 if (argv[i][2] != '\0') /* extra characters after '--'? */
475 return has_error; /* invalid option */
476 *first = i + 1;
477 return args;
478 case '\0': /* '-' */
479 return args; /* script "name" is '-' */
480 case 'E':
481 if (argv[i][2] != '\0') /* extra characters after 1st? */
482 return has_error; /* invalid option */
483 args |= has_E;
484 break;
485 case 'i':
486 args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
487 case 'v':
488 if (argv[i][2] != '\0') /* extra characters after 1st? */
489 return has_error; /* invalid option */
490 args |= has_v;
491 break;
492 case 'e':
493 args |= has_e; /* FALLTHROUGH */
494 case 'l': /* both options need an argument */
495 if (argv[i][2] == '\0') { /* no concatenated argument? */
496 i++; /* try next 'argv' */
497 if (argv[i] == NULL || argv[i][0] == '-')
498 return has_error; /* no next argument or it is another option */
499 }
500 break;
501 default: /* invalid option */
502 return has_error;
503 }
504 }
505 *first = i; /* no script name */
506 return args;
507}
#define has_e
#define has_v
#define has_error
#define has_E
#define has_i

References has_e, has_E, has_error, has_i, has_v, and NULL.

Referenced by pmain().

◆ createargtable()

static void createargtable ( lua_State * L,
char ** argv,
int argc,
int script )
static

Definition at line 226 of file lua-5.3.6/src/lua.c.

226 {
227 int i, narg;
228 if (script == argc) script = 0; /* no script name? */
229 narg = argc - (script + 1); /* number of positive indices */
230 lua_createtable(L, narg, script + 1);
231 for (i = 0; i < argc; i++) {
232 lua_pushstring(L, argv[i]);
233 lua_rawseti(L, -2, i - script);
234 }
235 lua_setglobal(L, "arg");
236}
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
#define lua_setglobal(L, s)

References lua_createtable(), lua_pushstring(), lua_rawseti(), and lua_setglobal.

Referenced by pmain().

◆ docall()

static int docall ( lua_State * L,
int narg,
int nres )
static

Definition at line 198 of file lua-5.3.6/src/lua.c.

198 {
199 int status;
200 int base = lua_gettop(L) - narg; /* function index */
201 lua_pushcfunction(L, msghandler); /* push message handler */
202 lua_insert(L, base); /* put it under function and args */
203 globalL = L; /* to be available to 'laction' */
204 signal(SIGINT, laction); /* set C-signal handler */
205 status = lua_pcall(L, narg, nres, base);
206 signal(SIGINT, SIG_DFL); /* reset C-signal handler */
207 lua_remove(L, base); /* remove message handler from the stack */
208 return status;
209}
LUA_API int lua_gettop(lua_State *L)
#define lua_pushcfunction(L, f)
#define lua_pcall(L, n, r, f)
static int msghandler(lua_State *L)
static void laction(int i)
static lua_State * globalL
#define lua_insert(L, idx)

References globalL, laction(), lua_gettop(), lua_insert, lua_pcall, lua_pushcfunction, lua_remove, and msghandler().

Referenced by dochunk(), dolibrary(), doREPL(), and handle_script().

◆ dochunk()

static int dochunk ( lua_State * L,
int status )
static

Definition at line 239 of file lua-5.3.6/src/lua.c.

239 {
240 if (status == LUA_OK) status = docall(L, 0, 0);
241 return report(L, status);
242}
static int report(lua_State *L, int status)
static int docall(lua_State *L, int narg, int nres)

References docall(), LUA_OK, and report().

Referenced by dofile(), and dostring().

◆ dofile()

static int dofile ( lua_State * L,
const char * name )
static

Definition at line 245 of file lua-5.3.6/src/lua.c.

245 {
246 return dochunk(L, luaL_loadfile(L, name));
247}
const char * name
Definition lsqlite3.c:2154
#define luaL_loadfile(L, f)
static int dochunk(lua_State *L, int status)

References dochunk(), luaL_loadfile, and name.

Referenced by handle_luainit(), and pmain().

◆ dolibrary()

static int dolibrary ( lua_State * L,
const char * name )
static

Definition at line 259 of file lua-5.3.6/src/lua.c.

259 {
260 int status;
261 lua_getglobal(L, "require");
263 status = docall(L, 1, 1); /* call 'require(name)' */
264 if (status == LUA_OK)
265 lua_setglobal(L, name); /* global[name] = require return */
266 return report(L, status);
267}
#define lua_getglobal(L, s)

References docall(), lua_getglobal, LUA_OK, lua_pushstring(), lua_setglobal, name, and report().

Referenced by runargs().

◆ doREPL()

static void doREPL ( lua_State * L)
static

Definition at line 405 of file lua-5.3.6/src/lua.c.

405 {
406 int status;
407 const char *oldprogname = progname;
408 progname = NULL; /* no 'progname' on errors in interactive mode */
409 while ((status = loadline(L)) != -1) {
410 if (status == LUA_OK)
411 status = docall(L, 0, LUA_MULTRET);
412 if (status == LUA_OK) l_print(L);
413 else report(L, status);
414 }
415 lua_settop(L, 0); /* clear stack */
417 progname = oldprogname;
418}
LUA_API void lua_settop(lua_State *L, int idx)
#define LUA_MULTRET
#define lua_writeline()
static void l_print(lua_State *L)
static const char * progname
static int loadline(lua_State *L)

References docall(), l_print(), loadline(), LUA_MULTRET, LUA_OK, lua_settop(), lua_writeline, NULL, progname, and report().

Referenced by pmain().

◆ dostring()

static int dostring ( lua_State * L,
const char * s,
const char * name )
static

Definition at line 250 of file lua-5.3.6/src/lua.c.

250 {
251 return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
252}
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References dochunk(), luaL_loadbuffer, name, and s.

Referenced by handle_luainit(), and runargs().

◆ get_prompt()

static const char * get_prompt ( lua_State * L,
int firstline )
static

Definition at line 273 of file lua-5.3.6/src/lua.c.

273 {
274 const char *p;
275 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
276 p = lua_tostring(L, -1);
277 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
278 return p;
279}
#define LUA_PROMPT
#define LUA_PROMPT2

References lua_getglobal, LUA_PROMPT, LUA_PROMPT2, lua_tostring, and NULL.

Referenced by pushline().

◆ handle_luainit()

static int handle_luainit ( lua_State * L)
static

Definition at line 535 of file lua-5.3.6/src/lua.c.

535 {
536 const char *name = "=" LUA_INITVARVERSION;
537 const char *init = getenv(name + 1);
538 if (init == NULL) {
539 name = "=" LUA_INIT_VAR;
540 init = getenv(name + 1); /* try alternative name */
541 }
542 if (init == NULL) return LUA_OK;
543 else if (init[0] == '@')
544 return dofile(L, init+1);
545 else
546 return dostring(L, init, name);
547}
#define LUA_INITVARVERSION
static int dofile(lua_State *L, const char *name)
#define LUA_INIT_VAR
static int dostring(lua_State *L, const char *s, const char *name)

References dofile(), dostring(), LUA_INIT_VAR, LUA_INITVARVERSION, LUA_OK, name, and NULL.

Referenced by pmain().

◆ handle_script()

static int handle_script ( lua_State * L,
char ** argv )
static

Definition at line 437 of file lua-5.3.6/src/lua.c.

437 {
438 int status;
439 const char *fname = argv[0];
440 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
441 fname = NULL; /* stdin */
442 status = luaL_loadfile(L, fname);
443 if (status == LUA_OK) {
444 int n = pushargs(L); /* push arguments to script */
445 status = docall(L, n, LUA_MULTRET);
446 }
447 return report(L, status);
448}
static int pushargs(lua_State *L)

References docall(), LUA_MULTRET, LUA_OK, luaL_loadfile, NULL, pushargs(), and report().

Referenced by pmain().

◆ incomplete()

static int incomplete ( lua_State * L,
int status )
static

Definition at line 291 of file lua-5.3.6/src/lua.c.

291 {
292 if (status == LUA_ERRSYNTAX) {
293 size_t lmsg;
294 const char *msg = lua_tolstring(L, -1, &lmsg);
295 if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
296 lua_pop(L, 1);
297 return 1;
298 }
299 }
300 return 0; /* else... */
301}
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
#define LUA_ERRSYNTAX
#define EOFMARK
#define marklen

References EOFMARK, LUA_ERRSYNTAX, lua_pop, lua_tolstring(), and marklen.

Referenced by multiline().

◆ l_message()

static void l_message ( const char * pname,
const char * msg )
static

Definition at line 155 of file lua-5.3.6/src/lua.c.

155 {
156 if (pname) lua_writestringerror("%s: ", pname);
157 lua_writestringerror("%s\n", msg);
158}
#define lua_writestringerror(s, p)

References lua_writestringerror.

Referenced by l_print(), main(), and report().

◆ l_print()

static void l_print ( lua_State * L)
static

Definition at line 388 of file lua-5.3.6/src/lua.c.

388 {
389 int n = lua_gettop(L);
390 if (n > 0) { /* any result to be printed? */
391 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
392 lua_getglobal(L, "print");
393 lua_insert(L, 1);
394 if (lua_pcall(L, n, 0, 0) != LUA_OK)
395 l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
396 lua_tostring(L, -1)));
397 }
398}
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
#define LUA_MINSTACK
static void l_message(const char *pname, const char *msg)

References l_message(), lua_getglobal, lua_gettop(), lua_insert, LUA_MINSTACK, LUA_OK, lua_pcall, lua_pushfstring(), lua_tostring, luaL_checkstack(), and progname.

Referenced by doREPL().

◆ laction()

static void laction ( int i)
static

Definition at line 124 of file lua-5.3.6/src/lua.c.

124 {
125 signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
127}
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
#define LUA_MASKCOUNT
#define LUA_MASKCALL
#define LUA_MASKRET
static void lstop(lua_State *L, lua_Debug *ar)

References globalL, lstop(), LUA_MASKCALL, LUA_MASKCOUNT, LUA_MASKRET, and lua_sethook().

Referenced by docall().

◆ loadline()

static int loadline ( lua_State * L)
static

Definition at line 372 of file lua-5.3.6/src/lua.c.

372 {
373 int status;
374 lua_settop(L, 0);
375 if (!pushline(L, 1))
376 return -1; /* no input */
377 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
378 status = multiline(L); /* try as command, maybe with continuation lines */
379 lua_remove(L, 1); /* remove line from the stack */
380 lua_assert(lua_gettop(L) == 1);
381 return status;
382}
#define lua_assert(c)
static int pushline(lua_State *L, int firstline)
static int addreturn(lua_State *L)
static int multiline(lua_State *L)

References addreturn(), lua_assert, lua_gettop(), LUA_OK, lua_remove, lua_settop(), multiline(), and pushline().

Referenced by doREPL().

◆ lstop()

static void lstop ( lua_State * L,
lua_Debug * ar )
static

Definition at line 111 of file lua-5.3.6/src/lua.c.

111 {
112 (void)ar; /* unused arg. */
113 lua_sethook(L, NULL, 0, 0); /* reset hook */
114 luaL_error(L, "interrupted!");
115}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)

References lua_sethook(), luaL_error(), and NULL.

Referenced by laction().

◆ main()

int main ( int argc,
char ** argv )

Definition at line 596 of file lua-5.3.6/src/lua.c.

596 {
597 int status, result;
598 lua_State *L = luaL_newstate(); /* create state */
599 if (L == NULL) {
600 l_message(argv[0], "cannot create state: not enough memory");
601 return EXIT_FAILURE;
602 }
603 lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
604 lua_pushinteger(L, argc); /* 1st argument */
605 lua_pushlightuserdata(L, argv); /* 2nd argument */
606 status = lua_pcall(L, 2, 1, 0); /* do the call */
607 result = lua_toboolean(L, -1); /* get result */
608 report(L, status);
609 lua_close(L);
610 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
611}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUALIB_API lua_State * luaL_newstate(void)
LUA_API void lua_close(lua_State *L)
static int pmain(lua_State *L)

References l_message(), lua_close(), LUA_OK, lua_pcall, lua_pushcfunction, lua_pushinteger(), lua_pushlightuserdata(), lua_toboolean(), luaL_newstate(), NULL, pmain(), and report().

◆ msghandler()

static int msghandler ( lua_State * L)
static

Definition at line 179 of file lua-5.3.6/src/lua.c.

179 {
180 const char *msg = lua_tostring(L, 1);
181 if (msg == NULL) { /* is error object not a string? */
182 if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
183 lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
184 return 1; /* that is the message */
185 else
186 msg = lua_pushfstring(L, "(error object is a %s value)",
187 luaL_typename(L, 1));
188 }
189 luaL_traceback(L, L, msg, 1); /* append a standard traceback */
190 return 1; /* return the traceback */
191}
LUA_API int lua_type(lua_State *L, int idx)
LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event)
#define luaL_typename(L, i)
#define LUA_TSTRING
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level)

References lua_pushfstring(), lua_tostring, LUA_TSTRING, lua_type(), luaL_callmeta(), luaL_traceback(), luaL_typename, and NULL.

Referenced by docall().

◆ multiline()

static int multiline ( lua_State * L)
static

Definition at line 350 of file lua-5.3.6/src/lua.c.

350 {
351 for (;;) { /* repeat until gets a complete statement */
352 size_t len;
353 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
354 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
355 if (!incomplete(L, status) || !pushline(L, 0)) {
356 lua_saveline(L, line); /* keep history */
357 return status; /* cannot or should not try to add continuation line */
358 }
359 lua_pushliteral(L, "\n"); /* add newline... */
360 lua_insert(L, -2); /* ...between the two lines */
361 lua_concat(L, 3); /* join them */
362 }
363}
LUA_API void lua_concat(lua_State *L, int n)
#define lua_pushliteral(L, s)
static int incomplete(lua_State *L, int status)

References incomplete(), lua_concat(), lua_insert, lua_pushliteral, lua_saveline, lua_tolstring(), luaL_loadbuffer, and pushline().

Referenced by loadline().

◆ pmain()

static int pmain ( lua_State * L)
static

Definition at line 554 of file lua-5.3.6/src/lua.c.

554 {
555 int argc = (int)lua_tointeger(L, 1);
556 char **argv = (char **)lua_touserdata(L, 2);
557 int script;
558 int args = collectargs(argv, &script);
559 luaL_checkversion(L); /* check that interpreter has correct version */
560 if (argv[0] && argv[0][0]) progname = argv[0];
561 if (args == has_error) { /* bad arg? */
562 print_usage(argv[script]); /* 'script' has index of bad arg. */
563 return 0;
564 }
565 if (args & has_v) /* option '-v'? */
567 if (args & has_E) { /* option '-E'? */
568 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
569 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
570 }
571 luaL_openlibs(L); /* open standard libraries */
572 createargtable(L, argv, argc, script); /* create table 'arg' */
573 if (!(args & has_E)) { /* no option '-E'? */
574 if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
575 return 0; /* error running LUA_INIT */
576 }
577 if (!runargs(L, argv, script)) /* execute arguments -e and -l */
578 return 0; /* something failed */
579 if (script < argc && /* execute main script (if there is one) */
580 handle_script(L, argv + script) != LUA_OK)
581 return 0;
582 if (args & has_i) /* -i option? */
583 doREPL(L); /* do read-eval-print loop */
584 else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
585 if (lua_stdin_is_tty()) { /* running in interactive mode? */
587 doREPL(L); /* do read-eval-print loop */
588 }
589 else dofile(L, NULL); /* executes stdin as a file */
590 }
591 lua_pushboolean(L, 1); /* signal no errors */
592 return 1;
593}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushboolean(lua_State *L, int b)
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API void luaL_openlibs(lua_State *L)
static void print_usage(void)
#define LUA_REGISTRYINDEX
#define luaL_checkversion(L)
#define lua_tointeger(L, i)
static int collectargs(char **argv, int *first)
static void print_version(void)
static int handle_script(lua_State *L, char **argv)
static int handle_luainit(lua_State *L)
static void doREPL(lua_State *L)
static int runargs(lua_State *L, char **argv, int n)
#define lua_stdin_is_tty()
static void createargtable(lua_State *L, char **argv, int argc, int script)

References collectargs(), createargtable(), dofile(), doREPL(), handle_luainit(), handle_script(), has_e, has_E, has_error, has_i, has_v, LUA_OK, lua_pushboolean(), LUA_REGISTRYINDEX, lua_setfield(), lua_stdin_is_tty, lua_tointeger, lua_touserdata(), luaL_checkversion, luaL_openlibs(), NULL, print_usage(), print_version(), progname, and runargs().

Referenced by main().

◆ print_usage()

static void print_usage ( const char * badoption)
static

Definition at line 130 of file lua-5.3.6/src/lua.c.

130 {
132 if (badoption[1] == 'e' || badoption[1] == 'l')
133 lua_writestringerror("'%s' needs argument\n", badoption);
134 else
135 lua_writestringerror("unrecognized option '%s'\n", badoption);
137 "usage: %s [options] [script [args]]\n"
138 "Available options are:\n"
139 " -e stat execute string 'stat'\n"
140 " -i enter interactive mode after executing 'script'\n"
141 " -l name require library 'name' into global 'name'\n"
142 " -v show version information\n"
143 " -E ignore environment variables\n"
144 " -- stop handling options\n"
145 " - stop handling options and execute stdin\n"
146 ,
147 progname);
148}

References lua_writestringerror, and progname.

◆ print_version()

static void print_version ( void )
static

Definition at line 212 of file lua-5.3.6/src/lua.c.

212 {
215}
#define LUA_COPYRIGHT
#define lua_writestring(s, l)

References LUA_COPYRIGHT, lua_writeline, and lua_writestring.

Referenced by pmain().

◆ pushargs()

static int pushargs ( lua_State * L)
static

Definition at line 424 of file lua-5.3.6/src/lua.c.

424 {
425 int i, n;
426 if (lua_getglobal(L, "arg") != LUA_TTABLE)
427 luaL_error(L, "'arg' is not a table");
428 n = (int)luaL_len(L, -1);
429 luaL_checkstack(L, n + 3, "too many arguments to script");
430 for (i = 1; i <= n; i++)
431 lua_rawgeti(L, -i, i);
432 lua_remove(L, -i); /* remove table from the stack */
433 return n;
434}
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
#define LUA_TTABLE
LUALIB_API int luaL_len(lua_State *L, int idx)

References lua_getglobal, lua_rawgeti(), lua_remove, LUA_TTABLE, luaL_checkstack(), luaL_error(), and luaL_len().

Referenced by handle_script().

◆ pushline()

static int pushline ( lua_State * L,
int firstline )
static

Definition at line 307 of file lua-5.3.6/src/lua.c.

307 {
308 char buffer[LUA_MAXINPUT];
309 char *b = buffer;
310 size_t l;
311 const char *prmt = get_prompt(L, firstline);
312 int readstatus = lua_readline(L, b, prmt);
313 if (readstatus == 0)
314 return 0; /* no input (prompt will be popped by caller) */
315 lua_pop(L, 1); /* remove prompt */
316 l = strlen(b);
317 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
318 b[--l] = '\0'; /* remove it */
319 if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
320 lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
321 else
322 lua_pushlstring(L, b, l);
323 lua_freeline(L, b);
324 return 1;
325}
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
static const char * get_prompt(lua_State *L, int firstline)
#define lua_freeline(L, b)
#define lua_readline(L, b, p)

References get_prompt(), lua_freeline, LUA_MAXINPUT, lua_pop, lua_pushfstring(), lua_pushlstring(), and lua_readline.

Referenced by loadline(), and multiline().

◆ report()

static int report ( lua_State * L,
int status )
static

Definition at line 166 of file lua-5.3.6/src/lua.c.

166 {
167 if (status != LUA_OK) {
168 const char *msg = lua_tostring(L, -1);
169 l_message(progname, msg);
170 lua_pop(L, 1); /* remove message */
171 }
172 return status;
173}

References l_message(), LUA_OK, lua_pop, lua_tostring, and progname.

Referenced by dochunk(), dolibrary(), doREPL(), handle_script(), and main().

◆ runargs()

static int runargs ( lua_State * L,
char ** argv,
int n )
static

Definition at line 514 of file lua-5.3.6/src/lua.c.

514 {
515 int i;
516 for (i = 1; i < n; i++) {
517 int option = argv[i][1];
518 lua_assert(argv[i][0] == '-'); /* already checked */
519 if (option == 'e' || option == 'l') {
520 int status;
521 const char *extra = argv[i] + 2; /* both options need an argument */
522 if (*extra == '\0') extra = argv[++i];
523 lua_assert(extra != NULL);
524 status = (option == 'e')
525 ? dostring(L, extra, "=(command line)")
526 : dolibrary(L, extra);
527 if (status != LUA_OK) return 0;
528 }
529 }
530 return 1;
531}
static int dolibrary(lua_State *L, const char *name)

References dolibrary(), dostring(), lua_assert, LUA_OK, and NULL.

Referenced by pmain().

Variable Documentation

◆ globalL

lua_State* globalL = NULL
static

Definition at line 103 of file lua-5.3.6/src/lua.c.

Referenced by docall(), and laction().

◆ progname

const char* progname = LUA_PROGNAME
static

Definition at line 105 of file lua-5.3.6/src/lua.c.

Referenced by doREPL(), l_print(), pmain(), print_usage(), and report().