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

Go to the source code of this file.

Macros

#define lua_c
 
#define LUA_PROGNAME   "lua"
 
#define LUA_INIT_VAR   "LUA_INIT"
 
#define LUA_INITVARVERSION   LUA_INIT_VAR LUA_VERSUFFIX
 
#define setsignal   signal
 
#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 */
 
#define LUA_PROMPT   "> "
 
#define LUA_PROMPT2   ">> "
 
#define LUA_MAXINPUT   512
 
#define lua_stdin_is_tty()   1 /* assume stdin is a tty */
 
#define lua_initreadline(L)   ((void)L)
 
#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)
 

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 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 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 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 454 of file lua-5.4.3/src/lua.c.

Referenced by incomplete().

◆ has_e

#define has_e   8 /* -e */

Definition at line 258 of file lua-5.4.3/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_E

#define has_E   16 /* -E */

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

Referenced by collectargs(), and pmain().

◆ has_error

#define has_error   1 /* bad option */

Definition at line 255 of file lua-5.4.3/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_i

#define has_i   2 /* -i */

Definition at line 256 of file lua-5.4.3/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_v

#define has_v   4 /* -v */

Definition at line 257 of file lua-5.4.3/src/lua.c.

Referenced by collectargs(), and pmain().

◆ lua_c

#define lua_c

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

◆ lua_freeline

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

Definition at line 431 of file lua-5.4.3/src/lua.c.

Referenced by pushline().

◆ LUA_INIT_VAR

#define LUA_INIT_VAR   "LUA_INIT"

Definition at line 29 of file lua-5.4.3/src/lua.c.

Referenced by handle_luainit().

◆ lua_initreadline

#define lua_initreadline ( L)    ((void)L)

Definition at line 426 of file lua-5.4.3/src/lua.c.

Referenced by doREPL().

◆ LUA_INITVARVERSION

#define LUA_INITVARVERSION   LUA_INIT_VAR LUA_VERSUFFIX

Definition at line 32 of file lua-5.4.3/src/lua.c.

Referenced by handle_luainit().

◆ LUA_MAXINPUT

#define LUA_MAXINPUT   512

Definition at line 375 of file lua-5.4.3/src/lua.c.

Referenced by pushline().

◆ LUA_PROGNAME

#define LUA_PROGNAME   "lua"

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

◆ LUA_PROMPT

#define LUA_PROMPT   "> "

Definition at line 370 of file lua-5.4.3/src/lua.c.

Referenced by get_prompt().

◆ LUA_PROMPT2

#define LUA_PROMPT2   ">> "

Definition at line 371 of file lua-5.4.3/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 427 of file lua-5.4.3/src/lua.c.

427#define lua_readline(L,b,p) \
428 ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
429 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 430 of file lua-5.4.3/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 400 of file lua-5.4.3/src/lua.c.

Referenced by pmain().

◆ marklen

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

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

Referenced by incomplete().

◆ setsignal

#define setsignal   signal

Definition at line 55 of file lua-5.4.3/src/lua.c.

Referenced by docall(), and laction().

Function Documentation

◆ addreturn()

static int addreturn ( lua_State * L)
static

Definition at line 504 of file lua-5.4.3/src/lua.c.

504 {
505 const char *line = lua_tostring(L, -1); /* original line */
506 const char *retline = lua_pushfstring(L, "return %s;", line);
507 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
508 if (status == LUA_OK) {
509 lua_remove(L, -2); /* remove modified line */
510 if (line[0] != '\0') /* non empty? */
511 lua_saveline(L, line); /* keep history */
512 }
513 else
514 lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
515 return status;
516}
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_remove(L, idx)
#define lua_saveline(L, line)

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 268 of file lua-5.4.3/src/lua.c.

268 {
269 int args = 0;
270 int i;
271 for (i = 1; argv[i] != NULL; i++) {
272 *first = i;
273 if (argv[i][0] != '-') /* not an option? */
274 return args; /* stop handling options */
275 switch (argv[i][1]) { /* else check option */
276 case '-': /* '--' */
277 if (argv[i][2] != '\0') /* extra characters after '--'? */
278 return has_error; /* invalid option */
279 *first = i + 1;
280 return args;
281 case '\0': /* '-' */
282 return args; /* script "name" is '-' */
283 case 'E':
284 if (argv[i][2] != '\0') /* extra characters? */
285 return has_error; /* invalid option */
286 args |= has_E;
287 break;
288 case 'W':
289 if (argv[i][2] != '\0') /* extra characters? */
290 return has_error; /* invalid option */
291 break;
292 case 'i':
293 args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
294 case 'v':
295 if (argv[i][2] != '\0') /* extra characters? */
296 return has_error; /* invalid option */
297 args |= has_v;
298 break;
299 case 'e':
300 args |= has_e; /* FALLTHROUGH */
301 case 'l': /* both options need an argument */
302 if (argv[i][2] == '\0') { /* no concatenated argument? */
303 i++; /* try next 'argv' */
304 if (argv[i] == NULL || argv[i][0] == '-')
305 return has_error; /* no next argument or it is another option */
306 }
307 break;
308 default: /* invalid option */
309 return has_error;
310 }
311 }
312 *first = i; /* no script name */
313 return args;
314}
#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 180 of file lua-5.4.3/src/lua.c.

180 {
181 int i, narg;
182 if (script == argc) script = 0; /* no script name? */
183 narg = argc - (script + 1); /* number of positive indices */
184 lua_createtable(L, narg, script + 1);
185 for (i = 0; i < argc; i++) {
186 lua_pushstring(L, argv[i]);
187 lua_rawseti(L, -2, i - script);
188 }
189 lua_setglobal(L, "arg");
190}
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 152 of file lua-5.4.3/src/lua.c.

152 {
153 int status;
154 int base = lua_gettop(L) - narg; /* function index */
155 lua_pushcfunction(L, msghandler); /* push message handler */
156 lua_insert(L, base); /* put it under function and args */
157 globalL = L; /* to be available to 'laction' */
158 setsignal(SIGINT, laction); /* set C-signal handler */
159 status = lua_pcall(L, narg, nres, base);
160 setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
161 lua_remove(L, base); /* remove message handler from the stack */
162 return status;
163}
LUA_API int lua_gettop(lua_State *L)
#define lua_pushcfunction(L, f)
#define lua_pcall(L, n, r, f)
#define lua_insert(L, idx)
#define setsignal
static int msghandler(lua_State *L)
static void laction(int i)
static lua_State * globalL

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

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

◆ dochunk()

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

Definition at line 193 of file lua-5.4.3/src/lua.c.

193 {
194 if (status == LUA_OK) status = docall(L, 0, 0);
195 return report(L, status);
196}
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 199 of file lua-5.4.3/src/lua.c.

199 {
200 return dochunk(L, luaL_loadfile(L, name));
201}
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 213 of file lua-5.4.3/src/lua.c.

213 {
214 int status;
215 lua_getglobal(L, "require");
217 status = docall(L, 1, 1); /* call 'require(name)' */
218 if (status == LUA_OK)
219 lua_setglobal(L, name); /* global[name] = require return */
220 return report(L, status);
221}
#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 577 of file lua-5.4.3/src/lua.c.

577 {
578 int status;
579 const char *oldprogname = progname;
580 progname = NULL; /* no 'progname' on errors in interactive mode */
582 while ((status = loadline(L)) != -1) {
583 if (status == LUA_OK)
584 status = docall(L, 0, LUA_MULTRET);
585 if (status == LUA_OK) l_print(L);
586 else report(L, status);
587 }
588 lua_settop(L, 0); /* clear stack */
590 progname = oldprogname;
591}
LUA_API void lua_settop(lua_State *L, int idx)
#define LUA_MULTRET
#define lua_writeline()
#define lua_initreadline(L)
static void l_print(lua_State *L)
static const char * progname
static int loadline(lua_State *L)

References docall(), l_print(), loadline(), lua_initreadline, 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 204 of file lua-5.4.3/src/lua.c.

204 {
205 return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
206}
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 443 of file lua-5.4.3/src/lua.c.

443 {
444 if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
445 return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
446 else { /* apply 'tostring' over the value */
447 const char *p = luaL_tolstring(L, -1, NULL);
448 lua_remove(L, -2); /* remove original value */
449 return p;
450 }
451}
#define LUA_TNIL
LUALIB_API const char * luaL_tolstring(lua_State *L, int idx, size_t *len)
#define LUA_PROMPT
#define LUA_PROMPT2

References lua_getglobal, LUA_PROMPT, LUA_PROMPT2, lua_remove, LUA_TNIL, luaL_tolstring(), and NULL.

Referenced by pushline().

◆ handle_luainit()

static int handle_luainit ( lua_State * L)
static

Definition at line 348 of file lua-5.4.3/src/lua.c.

348 {
349 const char *name = "=" LUA_INITVARVERSION;
350 const char *init = getenv(name + 1);
351 if (init == NULL) {
352 name = "=" LUA_INIT_VAR;
353 init = getenv(name + 1); /* try alternative name */
354 }
355 if (init == NULL) return LUA_OK;
356 else if (init[0] == '@')
357 return dofile(L, init+1);
358 else
359 return dostring(L, init, name);
360}
#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 240 of file lua-5.4.3/src/lua.c.

240 {
241 int status;
242 const char *fname = argv[0];
243 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
244 fname = NULL; /* stdin */
245 status = luaL_loadfile(L, fname);
246 if (status == LUA_OK) {
247 int n = pushargs(L); /* push arguments to script */
248 status = docall(L, n, LUA_MULTRET);
249 }
250 return report(L, status);
251}
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 463 of file lua-5.4.3/src/lua.c.

463 {
464 if (status == LUA_ERRSYNTAX) {
465 size_t lmsg;
466 const char *msg = lua_tolstring(L, -1, &lmsg);
467 if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
468 lua_pop(L, 1);
469 return 1;
470 }
471 }
472 return 0; /* else... */
473}
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 109 of file lua-5.4.3/src/lua.c.

109 {
110 if (pname) lua_writestringerror("%s: ", pname);
111 lua_writestringerror("%s\n", msg);
112}
#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 560 of file lua-5.4.3/src/lua.c.

560 {
561 int n = lua_gettop(L);
562 if (n > 0) { /* any result to be printed? */
563 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
564 lua_getglobal(L, "print");
565 lua_insert(L, 1);
566 if (lua_pcall(L, n, 0, 0) != LUA_OK)
567 l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
568 lua_tostring(L, -1)));
569 }
570}
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 76 of file lua-5.4.3/src/lua.c.

76 {
78 setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
79 lua_sethook(globalL, lstop, flag, 1);
80}
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
#define LUA_MASKCOUNT
#define LUA_MASKCALL
#define LUA_MASKLINE
#define LUA_MASKRET
static void lstop(lua_State *L, lua_Debug *ar)

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

Referenced by docall().

◆ loadline()

static int loadline ( lua_State * L)
static

Definition at line 544 of file lua-5.4.3/src/lua.c.

544 {
545 int status;
546 lua_settop(L, 0);
547 if (!pushline(L, 1))
548 return -1; /* no input */
549 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
550 status = multiline(L); /* try as command, maybe with continuation lines */
551 lua_remove(L, 1); /* remove line from the stack */
552 lua_assert(lua_gettop(L) == 1);
553 return status;
554}
#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 63 of file lua-5.4.3/src/lua.c.

63 {
64 (void)ar; /* unused arg. */
65 lua_sethook(L, NULL, 0, 0); /* reset hook */
66 luaL_error(L, "interrupted!");
67}
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 643 of file lua-5.4.3/src/lua.c.

643 {
644 int status, result;
645 lua_State *L = luaL_newstate(); /* create state */
646 if (L == NULL) {
647 l_message(argv[0], "cannot create state: not enough memory");
648 return EXIT_FAILURE;
649 }
650 lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
651 lua_pushinteger(L, argc); /* 1st argument */
652 lua_pushlightuserdata(L, argv); /* 2nd argument */
653 status = lua_pcall(L, 2, 1, 0); /* do the call */
654 result = lua_toboolean(L, -1); /* get result */
655 report(L, status);
656 lua_close(L);
657 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
658}
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 133 of file lua-5.4.3/src/lua.c.

133 {
134 const char *msg = lua_tostring(L, 1);
135 if (msg == NULL) { /* is error object not a string? */
136 if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
137 lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
138 return 1; /* that is the message */
139 else
140 msg = lua_pushfstring(L, "(error object is a %s value)",
141 luaL_typename(L, 1));
142 }
143 luaL_traceback(L, L, msg, 1); /* append a standard traceback */
144 return 1; /* return the traceback */
145}
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 522 of file lua-5.4.3/src/lua.c.

522 {
523 for (;;) { /* repeat until gets a complete statement */
524 size_t len;
525 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
526 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
527 if (!incomplete(L, status) || !pushline(L, 0)) {
528 lua_saveline(L, line); /* keep history */
529 return status; /* cannot or should not try to add continuation line */
530 }
531 lua_pushliteral(L, "\n"); /* add newline... */
532 lua_insert(L, -2); /* ...between the two lines */
533 lua_concat(L, 3); /* join them */
534 }
535}
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 600 of file lua-5.4.3/src/lua.c.

600 {
601 int argc = (int)lua_tointeger(L, 1);
602 char **argv = (char **)lua_touserdata(L, 2);
603 int script;
604 int args = collectargs(argv, &script);
605 luaL_checkversion(L); /* check that interpreter has correct version */
606 if (argv[0] && argv[0][0]) progname = argv[0];
607 if (args == has_error) { /* bad arg? */
608 print_usage(argv[script]); /* 'script' has index of bad arg. */
609 return 0;
610 }
611 if (args & has_v) /* option '-v'? */
613 if (args & has_E) { /* option '-E'? */
614 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
615 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
616 }
617 luaL_openlibs(L); /* open standard libraries */
618 createargtable(L, argv, argc, script); /* create table 'arg' */
619 lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */
620 if (!(args & has_E)) { /* no option '-E'? */
621 if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
622 return 0; /* error running LUA_INIT */
623 }
624 if (!runargs(L, argv, script)) /* execute arguments -e and -l */
625 return 0; /* something failed */
626 if (script < argc && /* execute main script (if there is one) */
627 handle_script(L, argv + script) != LUA_OK)
628 return 0;
629 if (args & has_i) /* -i option? */
630 doREPL(L); /* do read-eval-print loop */
631 else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
632 if (lua_stdin_is_tty()) { /* running in interactive mode? */
634 doREPL(L); /* do read-eval-print loop */
635 }
636 else dofile(L, NULL); /* executes stdin as a file */
637 }
638 lua_pushboolean(L, 1); /* signal no errors */
639 return 1;
640}
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)
LUA_API int lua_gc(lua_State *L, int what, int data)
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)
#define LUA_GCGEN
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_gc(), LUA_GCGEN, 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 83 of file lua-5.4.3/src/lua.c.

83 {
85 if (badoption[1] == 'e' || badoption[1] == 'l')
86 lua_writestringerror("'%s' needs argument\n", badoption);
87 else
88 lua_writestringerror("unrecognized option '%s'\n", badoption);
90 "usage: %s [options] [script [args]]\n"
91 "Available options are:\n"
92 " -e stat execute string 'stat'\n"
93 " -i enter interactive mode after executing 'script'\n"
94 " -l name require library 'name' into global 'name'\n"
95 " -v show version information\n"
96 " -E ignore environment variables\n"
97 " -W turn warnings on\n"
98 " -- stop handling options\n"
99 " - stop handling options and execute stdin\n"
100 ,
101 progname);
102}

References lua_writestringerror, and progname.

◆ print_version()

static void print_version ( void )
static

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

166 {
169}
#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 227 of file lua-5.4.3/src/lua.c.

227 {
228 int i, n;
229 if (lua_getglobal(L, "arg") != LUA_TTABLE)
230 luaL_error(L, "'arg' is not a table");
231 n = (int)luaL_len(L, -1);
232 luaL_checkstack(L, n + 3, "too many arguments to script");
233 for (i = 1; i <= n; i++)
234 lua_rawgeti(L, -i, i);
235 lua_remove(L, -i); /* remove table from the stack */
236 return n;
237}
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 479 of file lua-5.4.3/src/lua.c.

479 {
480 char buffer[LUA_MAXINPUT];
481 char *b = buffer;
482 size_t l;
483 const char *prmt = get_prompt(L, firstline);
484 int readstatus = lua_readline(L, b, prmt);
485 if (readstatus == 0)
486 return 0; /* no input (prompt will be popped by caller) */
487 lua_pop(L, 1); /* remove prompt */
488 l = strlen(b);
489 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
490 b[--l] = '\0'; /* remove it */
491 if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
492 lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
493 else
494 lua_pushlstring(L, b, l);
495 lua_freeline(L, b);
496 return 1;
497}
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 120 of file lua-5.4.3/src/lua.c.

120 {
121 if (status != LUA_OK) {
122 const char *msg = lua_tostring(L, -1);
123 l_message(progname, msg);
124 lua_pop(L, 1); /* remove message */
125 }
126 return status;
127}

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 322 of file lua-5.4.3/src/lua.c.

322 {
323 int i;
324 for (i = 1; i < n; i++) {
325 int option = argv[i][1];
326 lua_assert(argv[i][0] == '-'); /* already checked */
327 switch (option) {
328 case 'e': case 'l': {
329 int status;
330 const char *extra = argv[i] + 2; /* both options need an argument */
331 if (*extra == '\0') extra = argv[++i];
332 lua_assert(extra != NULL);
333 status = (option == 'e')
334 ? dostring(L, extra, "=(command line)")
335 : dolibrary(L, extra);
336 if (status != LUA_OK) return 0;
337 break;
338 }
339 case 'W':
340 lua_warning(L, "@on", 0); /* warnings on */
341 break;
342 }
343 }
344 return 1;
345}
void lua_warning(lua_State *L, const char *msg, int tocont)
static int dolibrary(lua_State *L, const char *name)

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

Referenced by pmain().

Variable Documentation

◆ globalL

lua_State* globalL = NULL
static

Definition at line 35 of file lua-5.4.3/src/lua.c.

Referenced by docall(), and laction().

◆ progname

const char* progname = LUA_PROGNAME
static

Definition at line 37 of file lua-5.4.3/src/lua.c.

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