Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lua.c File Reference
#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   "LUA_INIT"
 
#define LUA_INITVERSION    LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
 
#define lua_stdin_is_tty()   1 /* assume stdin is a tty */
 
#define lua_readline(L, b, p)
 
#define lua_saveline(L, idx)   { (void)L; (void)idx; }
 
#define lua_freeline(L, b)   { (void)L; (void)b; }
 
#define EOFMARK   "<eof>"
 
#define marklen   (sizeof(EOFMARK)/sizeof(char) - 1)
 
#define noextrachars(x)   {if ((x)[2] != '\0') return -1;}
 
#define has_i   0 /* -i */
 
#define has_v   1 /* -v */
 
#define has_e   2 /* -e */
 
#define has_E   3 /* -E */
 
#define num_has   4 /* number of 'has_*' */
 

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 void finalreport (lua_State *L, int status)
 
static int traceback (lua_State *L)
 
static int docall (lua_State *L, int narg, int nres)
 
static void print_version (void)
 
static int getargs (lua_State *L, char **argv, int n)
 
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 loadline (lua_State *L)
 
static void dotty (lua_State *L)
 
static int handle_script (lua_State *L, char **argv, int n)
 
static int collectargs (char **argv, int *args)
 
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 244 of file lua-5.2.4/src/lua.c.

Referenced by incomplete().

◆ has_e

#define has_e   2 /* -e */

Definition at line 351 of file lua-5.2.4/src/lua.c.

Referenced by collectargs(), pmain(), and pmain().

◆ has_E

#define has_E   3 /* -E */

Definition at line 352 of file lua-5.2.4/src/lua.c.

Referenced by collectargs(), and pmain().

◆ has_i

#define has_i   0 /* -i */

Definition at line 349 of file lua-5.2.4/src/lua.c.

Referenced by collectargs(), pmain(), and pmain().

◆ has_v

#define has_v   1 /* -v */

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

Referenced by collectargs(), pmain(), and pmain().

◆ lua_c

#define lua_c

Definition at line 13 of file lua-5.2.4/src/lua.c.

◆ lua_freeline

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

Definition at line 81 of file lua-5.2.4/src/lua.c.

Referenced by pushline(), and pushline().

◆ LUA_INIT

#define LUA_INIT   "LUA_INIT"

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

Referenced by handle_luainit().

◆ LUA_INITVERSION

#define LUA_INITVERSION    LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR

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

38#define LUA_INITVERSION \
39 LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR

Referenced by handle_luainit().

◆ LUA_MAXINPUT

#define LUA_MAXINPUT   512

Definition at line 31 of file lua-5.2.4/src/lua.c.

Referenced by pushline(), and pushline().

◆ LUA_PROGNAME

#define LUA_PROGNAME   "lua"

Definition at line 27 of file lua-5.2.4/src/lua.c.

◆ LUA_PROMPT

#define LUA_PROMPT   "> "

Definition at line 22 of file lua-5.2.4/src/lua.c.

Referenced by get_prompt(), and get_prompt().

◆ LUA_PROMPT2

#define LUA_PROMPT2   ">> "

Definition at line 23 of file lua-5.2.4/src/lua.c.

Referenced by get_prompt(), and 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 77 of file lua-5.2.4/src/lua.c.

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

Referenced by pushline(), and pushline().

◆ lua_saveline

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

Definition at line 80 of file lua-5.2.4/src/lua.c.

Referenced by loadline(), and loadline().

◆ lua_stdin_is_tty

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

Definition at line 54 of file lua-5.2.4/src/lua.c.

Referenced by pmain(), and pmain().

◆ marklen

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

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

Referenced by incomplete().

◆ noextrachars

#define noextrachars ( x)    {if ((x)[2] != '\0') return -1;}

Definition at line 345 of file lua-5.2.4/src/lua.c.

Referenced by collectargs().

◆ num_has

#define num_has   4 /* number of 'has_*' */

Definition at line 354 of file lua-5.2.4/src/lua.c.

Referenced by pmain().

Function Documentation

◆ collectargs()

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

Definition at line 357 of file lua-5.2.4/src/lua.c.

357 {
358 int i;
359 for (i = 1; argv[i] != NULL; i++) {
360 if (argv[i][0] != '-') /* not an option? */
361 return i;
362 switch (argv[i][1]) { /* option */
363 case '-':
364 noextrachars(argv[i]);
365 return (argv[i+1] != NULL ? i+1 : 0);
366 case '\0':
367 return i;
368 case 'E':
369 args[has_E] = 1;
370 break;
371 case 'i':
372 noextrachars(argv[i]);
373 args[has_i] = 1; /* go through */
374 case 'v':
375 noextrachars(argv[i]);
376 args[has_v] = 1;
377 break;
378 case 'e':
379 args[has_e] = 1; /* go through */
380 case 'l': /* both options need an argument */
381 if (argv[i][2] == '\0') { /* no concatenated argument? */
382 i++; /* try next 'argv' */
383 if (argv[i] == NULL || argv[i][0] == '-')
384 return -(i - 1); /* no next argument or it is another option */
385 }
386 break;
387 default: /* invalid option; return its index... */
388 return -i; /* ...as a negative value */
389 }
390 }
391 return 0;
392}
#define has_e
#define noextrachars(x)
#define has_v
#define has_E
#define has_i

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

Referenced by pmain().

◆ docall()

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

Definition at line 172 of file lua-5.2.4/src/lua.c.

172 {
173 int status;
174 int base = lua_gettop(L) - narg; /* function index */
175 lua_pushcfunction(L, traceback); /* push traceback function */
176 lua_insert(L, base); /* put it under chunk and args */
177 globalL = L; /* to be available to 'laction' */
178 signal(SIGINT, laction);
179 status = lua_pcall(L, narg, nres, base);
180 signal(SIGINT, SIG_DFL);
181 lua_remove(L, base); /* remove traceback function */
182 return status;
183}
LUA_API int lua_gettop(lua_State *L)
#define lua_pushcfunction(L, f)
static int traceback(lua_State *L)
static void laction(int i)
static lua_State * globalL
#define lua_pcall(L, n, r, f)
#define lua_insert(L, idx)
#define lua_remove(L, idx)

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

Referenced by dofile(), dolibrary(), dostring(), dotty(), and handle_script().

◆ dofile()

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

Definition at line 210 of file lua-5.2.4/src/lua.c.

210 {
211 int status = luaL_loadfile(L, name);
212 if (status == LUA_OK) status = docall(L, 0, 0);
213 return report(L, status);
214}
const char * name
Definition lsqlite3.c:2154
#define luaL_loadfile(L, f)
static int report(lua_State *L, int status)
static int docall(lua_State *L, int narg, int nres)
#define LUA_OK

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

Referenced by handle_luainit(), and pmain().

◆ dolibrary()

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

Definition at line 224 of file lua-5.2.4/src/lua.c.

224 {
225 int status;
226 lua_getglobal(L, "require");
228 status = docall(L, 1, 1); /* call 'require(name)' */
229 if (status == LUA_OK)
230 lua_setglobal(L, name); /* global[name] = require return */
231 return report(L, status);
232}
LUA_API void lua_pushstring(lua_State *L, const char *s)
#define lua_setglobal(L, s)
#define lua_getglobal(L, s)

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

Referenced by runargs().

◆ dostring()

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

Definition at line 217 of file lua-5.2.4/src/lua.c.

217 {
218 int status = luaL_loadbuffer(L, s, strlen(s), name);
219 if (status == LUA_OK) status = docall(L, 0, 0);
220 return report(L, status);
221}
#define luaL_loadbuffer(L, s, sz, n)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References docall(), LUA_OK, luaL_loadbuffer, name, report(), and s.

Referenced by handle_luainit(), and runargs().

◆ dotty()

static void dotty ( lua_State * L)
static

Definition at line 303 of file lua-5.2.4/src/lua.c.

303 {
304 int status;
305 const char *oldprogname = progname;
306 progname = NULL;
307 while ((status = loadline(L)) != -1) {
308 if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
309 report(L, status);
310 if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
311 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
312 lua_getglobal(L, "print");
313 lua_insert(L, 1);
314 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK)
316 "error calling " LUA_QL("print") " (%s)",
317 lua_tostring(L, -1)));
318 }
319 }
320 lua_settop(L, 0); /* clear stack */
321 luai_writeline();
322 progname = oldprogname;
323}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API void lua_settop(lua_State *L, int idx)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
#define lua_tostring(L, i)
#define LUA_MULTRET
#define LUA_MINSTACK
#define LUA_QL(x)
static void l_message(const char *pname, const char *msg)
static const char * progname
static int loadline(lua_State *L)

References docall(), l_message(), loadline(), lua_getglobal, lua_gettop(), lua_insert, LUA_MINSTACK, LUA_MULTRET, LUA_OK, lua_pcall, lua_pushfstring(), LUA_QL, lua_settop(), lua_tostring, luaL_checkstack(), NULL, progname, and report().

Referenced by pmain().

◆ finalreport()

static void finalreport ( lua_State * L,
int status )
static

Definition at line 149 of file lua-5.2.4/src/lua.c.

149 {
150 if (status != LUA_OK) {
151 const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
152 : NULL;
153 if (msg == NULL) msg = "(error object is not a string)";
154 l_message(progname, msg);
155 lua_pop(L, 1);
156 }
157}
LUA_API int lua_type(lua_State *L, int idx)
#define LUA_TSTRING
#define lua_pop(L, n)

References l_message(), LUA_OK, lua_pop, lua_tostring, LUA_TSTRING, lua_type(), NULL, and progname.

Referenced by main().

◆ get_prompt()

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

Definition at line 235 of file lua-5.2.4/src/lua.c.

235 {
236 const char *p;
237 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
238 p = lua_tostring(L, -1);
239 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
240 return p;
241}
#define LUA_PROMPT
#define LUA_PROMPT2

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

Referenced by pushline().

◆ getargs()

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

Definition at line 192 of file lua-5.2.4/src/lua.c.

192 {
193 int narg;
194 int i;
195 int argc = 0;
196 while (argv[argc]) argc++; /* count total number of arguments */
197 narg = argc - (n + 1); /* number of arguments to the script */
198 luaL_checkstack(L, narg + 3, "too many arguments to script");
199 for (i=n+1; i < argc; i++)
200 lua_pushstring(L, argv[i]);
201 lua_createtable(L, narg, n + 1);
202 for (i=0; i < argc; i++) {
203 lua_pushstring(L, argv[i]);
204 lua_rawseti(L, -2, i - n);
205 }
206 return narg;
207}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)

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

Referenced by handle_script().

◆ handle_luainit()

static int handle_luainit ( lua_State * L)
static

Definition at line 423 of file lua-5.2.4/src/lua.c.

423 {
424 const char *name = "=" LUA_INITVERSION;
425 const char *init = getenv(name + 1);
426 if (init == NULL) {
427 name = "=" LUA_INIT;
428 init = getenv(name + 1); /* try alternative name */
429 }
430 if (init == NULL) return LUA_OK;
431 else if (init[0] == '@')
432 return dofile(L, init+1);
433 else
434 return dostring(L, init, name);
435}
#define LUA_INITVERSION
static int dofile(lua_State *L, const char *name)
#define LUA_INIT
static int dostring(lua_State *L, const char *s, const char *name)

References dofile(), dostring(), LUA_INIT, LUA_INITVERSION, LUA_OK, name, and NULL.

Referenced by pmain().

◆ handle_script()

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

Definition at line 326 of file lua-5.2.4/src/lua.c.

326 {
327 int status;
328 const char *fname;
329 int narg = getargs(L, argv, n); /* collect arguments */
330 lua_setglobal(L, "arg");
331 fname = argv[n];
332 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
333 fname = NULL; /* stdin */
334 status = luaL_loadfile(L, fname);
335 lua_insert(L, -(narg+1));
336 if (status == LUA_OK)
337 status = docall(L, narg, LUA_MULTRET);
338 else
339 lua_pop(L, narg);
340 return report(L, status);
341}
static int getargs(lua_State *L, char **argv, int n)

References docall(), getargs(), lua_insert, LUA_MULTRET, LUA_OK, lua_pop, lua_setglobal, luaL_loadfile, NULL, and report().

Referenced by pmain().

◆ incomplete()

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

Definition at line 247 of file lua-5.2.4/src/lua.c.

247 {
248 if (status == LUA_ERRSYNTAX) {
249 size_t lmsg;
250 const char *msg = lua_tolstring(L, -1, &lmsg);
251 if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
252 lua_pop(L, 1);
253 return 1;
254 }
255 }
256 return 0; /* else... */
257}
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 loadline().

◆ l_message()

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

Definition at line 129 of file lua-5.2.4/src/lua.c.

129 {
130 if (pname) luai_writestringerror("%s: ", pname);
131 luai_writestringerror("%s\n", msg);
132}
#define luai_writestringerror(s, p)

References luai_writestringerror.

Referenced by dotty(), finalreport(), main(), and report().

◆ laction()

static void laction ( int i)
static

Definition at line 101 of file lua-5.2.4/src/lua.c.

101 {
102 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
103 terminate process (default action) */
105}
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 281 of file lua-5.2.4/src/lua.c.

281 {
282 int status;
283 lua_settop(L, 0);
284 if (!pushline(L, 1))
285 return -1; /* no input */
286 for (;;) { /* repeat until gets a complete line */
287 size_t l;
288 const char *line = lua_tolstring(L, 1, &l);
289 status = luaL_loadbuffer(L, line, l, "=stdin");
290 if (!incomplete(L, status)) break; /* cannot try to add lines? */
291 if (!pushline(L, 0)) /* no more input? */
292 return -1;
293 lua_pushliteral(L, "\n"); /* add a new line... */
294 lua_insert(L, -2); /* ...between the two lines */
295 lua_concat(L, 3); /* join them */
296 }
297 lua_saveline(L, 1);
298 lua_remove(L, 1); /* remove line */
299 return status;
300}
LUA_API void lua_concat(lua_State *L, int n)
#define lua_pushliteral(L, s)
static int pushline(lua_State *L, int firstline)
#define lua_saveline(L, idx)
static int incomplete(lua_State *L, int status)

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

Referenced by dotty().

◆ lstop()

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

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

94 {
95 (void)ar; /* unused arg. */
96 lua_sethook(L, NULL, 0, 0);
97 luaL_error(L, "interrupted!");
98}
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 480 of file lua-5.2.4/src/lua.c.

480 {
481 int status, result;
482 lua_State *L = luaL_newstate(); /* create state */
483 if (L == NULL) {
484 l_message(argv[0], "cannot create state: not enough memory");
485 return EXIT_FAILURE;
486 }
487 /* call 'pmain' in protected mode */
489 lua_pushinteger(L, argc); /* 1st argument */
490 lua_pushlightuserdata(L, argv); /* 2nd argument */
491 status = lua_pcall(L, 2, 1, 0);
492 result = lua_toboolean(L, -1); /* get result */
493 finalreport(L, status);
494 lua_close(L);
495 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
496}
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 void finalreport(lua_State *L, int status)
static int pmain(lua_State *L)

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

◆ pmain()

static int pmain ( lua_State * L)
static

Definition at line 438 of file lua-5.2.4/src/lua.c.

438 {
439 int argc = (int)lua_tointeger(L, 1);
440 char **argv = (char **)lua_touserdata(L, 2);
441 int script;
442 int args[num_has];
443 args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0;
444 if (argv[0] && argv[0][0]) progname = argv[0];
445 script = collectargs(argv, args);
446 if (script < 0) { /* invalid arg? */
447 print_usage(argv[-script]);
448 return 0;
449 }
450 if (args[has_v]) print_version();
451 if (args[has_E]) { /* option '-E'? */
452 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
453 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
454 }
455 /* open standard libraries */
457 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
458 luaL_openlibs(L); /* open libraries */
459 lua_gc(L, LUA_GCRESTART, 0);
460 if (!args[has_E] && handle_luainit(L) != LUA_OK)
461 return 0; /* error running LUA_INIT */
462 /* execute arguments -e and -l */
463 if (!runargs(L, argv, (script > 0) ? script : argc)) return 0;
464 /* execute main script (if there is one) */
465 if (script && handle_script(L, argv, script) != LUA_OK) return 0;
466 if (args[has_i]) /* -i option? */
467 dotty(L);
468 else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */
469 if (lua_stdin_is_tty()) {
471 dotty(L);
472 }
473 else dofile(L, NULL); /* executes stdin as a file */
474 }
475 lua_pushboolean(L, 1); /* signal no errors */
476 return 1;
477}
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 LUA_GCSTOP
#define LUA_GCRESTART
#define luaL_checkversion(L)
static void print_version(void)
static int handle_luainit(lua_State *L)
static int collectargs(char **argv, int *args)
static int runargs(lua_State *L, char **argv, int n)
#define lua_stdin_is_tty()
#define num_has
static int handle_script(lua_State *L, char **argv, int n)
static void dotty(lua_State *L)
#define lua_tointeger(L, i)

References collectargs(), dofile(), dotty(), handle_luainit(), handle_script(), has_e, has_E, has_i, has_v, lua_gc(), LUA_GCRESTART, LUA_GCSTOP, LUA_OK, lua_pushboolean(), LUA_REGISTRYINDEX, lua_setfield(), lua_stdin_is_tty, lua_tointeger, lua_touserdata(), luaL_checkversion, luaL_openlibs(), NULL, num_has, print_usage(), print_version(), progname, and runargs().

Referenced by main().

◆ print_usage()

static void print_usage ( const char * badoption)
static

Definition at line 108 of file lua-5.2.4/src/lua.c.

108 {
110 if (badoption[1] == 'e' || badoption[1] == 'l')
111 luai_writestringerror("'%s' needs argument\n", badoption);
112 else
113 luai_writestringerror("unrecognized option '%s'\n", badoption);
115 "usage: %s [options] [script [args]]\n"
116 "Available options are:\n"
117 " -e stat execute string " LUA_QL("stat") "\n"
118 " -i enter interactive mode after executing " LUA_QL("script") "\n"
119 " -l name require library " LUA_QL("name") "\n"
120 " -v show version information\n"
121 " -E ignore environment variables\n"
122 " -- stop handling options\n"
123 " - stop handling options and execute stdin\n"
124 ,
125 progname);
126}

References LUA_QL, luai_writestringerror, and progname.

◆ print_version()

static void print_version ( void )
static

Definition at line 186 of file lua-5.2.4/src/lua.c.

186 {
187 luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
188 luai_writeline();
189}
#define LUA_COPYRIGHT

References LUA_COPYRIGHT.

Referenced by pmain().

◆ pushline()

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

Definition at line 260 of file lua-5.2.4/src/lua.c.

260 {
261 char buffer[LUA_MAXINPUT];
262 char *b = buffer;
263 size_t l;
264 const char *prmt = get_prompt(L, firstline);
265 int readstatus = lua_readline(L, b, prmt);
266 lua_pop(L, 1); /* remove result from 'get_prompt' */
267 if (readstatus == 0)
268 return 0; /* no input */
269 l = strlen(b);
270 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
271 b[l-1] = '\0'; /* remove it */
272 if (firstline && b[0] == '=') /* first line starts with `=' ? */
273 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
274 else
275 lua_pushstring(L, b);
276 lua_freeline(L, b);
277 return 1;
278}
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_pushstring(), and lua_readline.

Referenced by loadline().

◆ report()

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

Definition at line 135 of file lua-5.2.4/src/lua.c.

135 {
136 if (status != LUA_OK && !lua_isnil(L, -1)) {
137 const char *msg = lua_tostring(L, -1);
138 if (msg == NULL) msg = "(error object is not a string)";
139 l_message(progname, msg);
140 lua_pop(L, 1);
141 /* force a complete garbage collection in case of errors */
142 lua_gc(L, LUA_GCCOLLECT, 0);
143 }
144 return status;
145}
#define LUA_GCCOLLECT
#define lua_isnil(L, n)

References l_message(), lua_gc(), LUA_GCCOLLECT, lua_isnil, LUA_OK, lua_pop, lua_tostring, NULL, and progname.

Referenced by dofile(), dolibrary(), dostring(), dotty(), and handle_script().

◆ runargs()

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

Definition at line 395 of file lua-5.2.4/src/lua.c.

395 {
396 int i;
397 for (i = 1; i < n; i++) {
398 lua_assert(argv[i][0] == '-');
399 switch (argv[i][1]) { /* option */
400 case 'e': {
401 const char *chunk = argv[i] + 2;
402 if (*chunk == '\0') chunk = argv[++i];
404 if (dostring(L, chunk, "=(command line)") != LUA_OK)
405 return 0;
406 break;
407 }
408 case 'l': {
409 const char *filename = argv[i] + 2;
410 if (*filename == '\0') filename = argv[++i];
411 lua_assert(filename != NULL);
412 if (dolibrary(L, filename) != LUA_OK)
413 return 0; /* stop if file fails */
414 break;
415 }
416 default: break;
417 }
418 }
419 return 1;
420}
#define lua_assert(c)
static void chunk(LexState *ls)
static int dolibrary(lua_State *L, const char *name)

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

Referenced by pmain().

◆ traceback()

static int traceback ( lua_State * L)
static

Definition at line 160 of file lua-5.2.4/src/lua.c.

160 {
161 const char *msg = lua_tostring(L, 1);
162 if (msg)
163 luaL_traceback(L, L, msg, 1);
164 else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */
165 if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
166 lua_pushliteral(L, "(no error message)");
167 }
168 return 1;
169}
LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event)
#define lua_isnoneornil(L, n)
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level)

References lua_isnoneornil, lua_pushliteral, lua_tostring, luaL_callmeta(), and luaL_traceback().

Referenced by docall().

Variable Documentation

◆ globalL

lua_State* globalL = NULL
static

Definition at line 88 of file lua-5.2.4/src/lua.c.

Referenced by docall(), and laction().

◆ progname

const char* progname = LUA_PROGNAME
static

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

Referenced by dotty(), finalreport(), pmain(), print_usage(), and report().