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.

Data Structures

struct  Smain
 

Macros

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

Functions

static void lstop (lua_State *L, lua_Debug *ar)
 
static void laction (int i)
 
static void print_usage (void)
 
static void l_message (const char *pname, const char *msg)
 
static int report (lua_State *L, int status)
 
static int traceback (lua_State *L)
 
static int docall (lua_State *L, int narg, int clear)
 
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 *pi, int *pv, int *pe)
 
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

◆ lua_c

#define lua_c

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

◆ notail

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

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

Referenced by collectargs().

Function Documentation

◆ collectargs()

static int collectargs ( char ** argv,
int * pi,
int * pv,
int * pe )
static

Definition at line 261 of file lua-5.1.5/src/lua.c.

261 {
262 int i;
263 for (i = 1; argv[i] != NULL; i++) {
264 if (argv[i][0] != '-') /* not an option? */
265 return i;
266 switch (argv[i][1]) { /* option */
267 case '-':
268 notail(argv[i]);
269 return (argv[i+1] != NULL ? i+1 : 0);
270 case '\0':
271 return i;
272 case 'i':
273 notail(argv[i]);
274 *pi = 1; /* go through */
275 case 'v':
276 notail(argv[i]);
277 *pv = 1;
278 break;
279 case 'e':
280 *pe = 1; /* go through */
281 case 'l':
282 if (argv[i][2] == '\0') {
283 i++;
284 if (argv[i] == NULL) return -1;
285 }
286 break;
287 default: return -1; /* invalid option */
288 }
289 }
290 return 0;
291}
#define NULL
Definition gmacros.h:924
#define notail(x)

References notail, and NULL.

Referenced by pmain().

◆ docall()

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

Definition at line 96 of file lua-5.1.5/src/lua.c.

96 {
97 int status;
98 int base = lua_gettop(L) - narg; /* function index */
99 lua_pushcfunction(L, traceback); /* push traceback function */
100 lua_insert(L, base); /* put it under chunk and args */
101 signal(SIGINT, laction);
102 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
103 signal(SIGINT, SIG_DFL);
104 lua_remove(L, base); /* remove traceback function */
105 /* force a complete garbage collection in case of errors */
106 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
107 return status;
108}
LUA_API int lua_gc(lua_State *L, int what, int data)
LUA_API int lua_gettop(lua_State *L)
static int traceback(lua_State *L)
static void laction(int i)
#define lua_pushcfunction(L, f)
#define LUA_GCCOLLECT
#define LUA_MULTRET
#define lua_pcall(L, n, r, f)
#define lua_insert(L, idx)
#define lua_remove(L, idx)

References laction(), lua_gc(), LUA_GCCOLLECT, lua_gettop(), lua_insert, LUA_MULTRET, 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 134 of file lua-5.1.5/src/lua.c.

134 {
135 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
136 return report(L, status);
137}
const char * name
Definition lsqlite3.c:2154
static int report(lua_State *L, int status)
static int docall(lua_State *L, int narg, int clear)
#define luaL_loadfile(L, f)

References docall(), 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 146 of file lua-5.1.5/src/lua.c.

146 {
147 lua_getglobal(L, "require");
149 return report(L, docall(L, 1, 1));
150}
LUA_API void lua_pushstring(lua_State *L, const char *s)
#define lua_getglobal(L, s)

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

Referenced by runargs().

◆ dostring()

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

Definition at line 140 of file lua-5.1.5/src/lua.c.

140 {
141 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
142 return report(L, status);
143}
#define luaL_loadbuffer(L, s, sz, n)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

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

Referenced by handle_luainit(), and runargs().

◆ dotty()

static void dotty ( lua_State * L)
static

Definition at line 216 of file lua-5.1.5/src/lua.c.

216 {
217 int status;
218 const char *oldprogname = progname;
219 progname = NULL;
220 while ((status = loadline(L)) != -1) {
221 if (status == 0) status = docall(L, 0, 0);
222 report(L, status);
223 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
224 lua_getglobal(L, "print");
225 lua_insert(L, 1);
226 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
228 "error calling " LUA_QL("print") " (%s)",
229 lua_tostring(L, -1)));
230 }
231 }
232 lua_settop(L, 0); /* clear stack */
233 fputs("\n", stdout);
234 fflush(stdout);
235 progname = oldprogname;
236}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API void lua_settop(lua_State *L, int idx)
static void l_message(const char *pname, const char *msg)
static const char * progname
static int loadline(lua_State *L)
#define lua_tostring(L, i)
#define LUA_QL(x)

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

Referenced by pmain().

◆ get_prompt()

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

Definition at line 153 of file lua-5.1.5/src/lua.c.

153 {
154 const char *p;
155 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
156 p = lua_tostring(L, -1);
157 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
158 lua_pop(L, 1); /* remove global */
159 return p;
160}
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define lua_pop(L, n)
#define LUA_GLOBALSINDEX
#define LUA_PROMPT
#define LUA_PROMPT2

References lua_getfield(), LUA_GLOBALSINDEX, lua_pop, 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 116 of file lua-5.1.5/src/lua.c.

116 {
117 int narg;
118 int i;
119 int argc = 0;
120 while (argv[argc]) argc++; /* count total number of arguments */
121 narg = argc - (n + 1); /* number of arguments to the script */
122 luaL_checkstack(L, narg + 3, "too many arguments to script");
123 for (i=n+1; i < argc; i++)
124 lua_pushstring(L, argv[i]);
125 lua_createtable(L, narg, n + 1);
126 for (i=0; i < argc; i++) {
127 lua_pushstring(L, argv[i]);
128 lua_rawseti(L, -2, i - n);
129 }
130 return narg;
131}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)

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 323 of file lua-5.1.5/src/lua.c.

323 {
324 const char *init = getenv(LUA_INIT);
325 if (init == NULL) return 0; /* status OK */
326 else if (init[0] == '@')
327 return dofile(L, init+1);
328 else
329 return dostring(L, init, "=" LUA_INIT);
330}
static int dofile(lua_State *L, const char *name)
static int dostring(lua_State *L, const char *s, const char *name)
#define LUA_INIT

References dofile(), dostring(), LUA_INIT, and NULL.

Referenced by pmain().

◆ handle_script()

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

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

239 {
240 int status;
241 const char *fname;
242 int narg = getargs(L, argv, n); /* collect arguments */
243 lua_setglobal(L, "arg");
244 fname = argv[n];
245 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
246 fname = NULL; /* stdin */
247 status = luaL_loadfile(L, fname);
248 lua_insert(L, -(narg+1));
249 if (status == 0)
250 status = docall(L, narg, 0);
251 else
252 lua_pop(L, narg);
253 return report(L, status);
254}
static int getargs(lua_State *L, char **argv, int n)
#define lua_setglobal(L, s)

References docall(), getargs(), lua_insert, 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 163 of file lua-5.1.5/src/lua.c.

163 {
164 if (status == LUA_ERRSYNTAX) {
165 size_t lmsg;
166 const char *msg = lua_tolstring(L, -1, &lmsg);
167 const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
168 if (strstr(msg, LUA_QL("<eof>")) == tp) {
169 lua_pop(L, 1);
170 return 1;
171 }
172 }
173 return 0; /* else... */
174}
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
#define LUA_ERRSYNTAX

References LUA_ERRSYNTAX, lua_pop, LUA_QL, and lua_tolstring().

Referenced by loadline().

◆ l_message()

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

Definition at line 58 of file lua-5.1.5/src/lua.c.

58 {
59 if (pname) fprintf(stderr, "%s: ", pname);
60 fprintf(stderr, "%s\n", msg);
61 fflush(stderr);
62}

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

◆ laction()

static void laction ( int i)
static

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

35 {
36 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
37 terminate process (default action) */
39}
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
static void lstop(lua_State *L, lua_Debug *ar)
static lua_State * globalL
#define LUA_MASKCOUNT
#define LUA_MASKCALL
#define LUA_MASKRET

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 196 of file lua-5.1.5/src/lua.c.

196 {
197 int status;
198 lua_settop(L, 0);
199 if (!pushline(L, 1))
200 return -1; /* no input */
201 for (;;) { /* repeat until gets a complete line */
202 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
203 if (!incomplete(L, status)) break; /* cannot try to add lines? */
204 if (!pushline(L, 0)) /* no more input? */
205 return -1;
206 lua_pushliteral(L, "\n"); /* add a new line... */
207 lua_insert(L, -2); /* ...between the two lines */
208 lua_concat(L, 3); /* join them */
209 }
210 lua_saveline(L, 1);
211 lua_remove(L, 1); /* remove line */
212 return status;
213}
LUA_API void lua_concat(lua_State *L, int n)
static int pushline(lua_State *L, int firstline)
static int incomplete(lua_State *L, int status)
#define lua_pushliteral(L, s)
#define lua_strlen(L, i)
#define lua_saveline(L, idx)

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

Referenced by dotty().

◆ lstop()

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

Definition at line 28 of file lua-5.1.5/src/lua.c.

28 {
29 (void)ar; /* unused arg. */
30 lua_sethook(L, NULL, 0, 0);
31 luaL_error(L, "interrupted!");
32}
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 377 of file lua-5.1.5/src/lua.c.

377 {
378 int status;
379 struct Smain s;
380 lua_State *L = lua_open(); /* create state */
381 if (L == NULL) {
382 l_message(argv[0], "cannot create state: not enough memory");
383 return EXIT_FAILURE;
384 }
385 s.argc = argc;
386 s.argv = argv;
387 status = lua_cpcall(L, &pmain, &s);
388 report(L, status);
389 lua_close(L);
390 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
391}
LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
LUA_API void lua_close(lua_State *L)
static int pmain(lua_State *L)
#define lua_open()
char ** argv

References Smain::argc, Smain::argv, l_message(), lua_close(), lua_cpcall(), lua_open, NULL, pmain(), report(), s, and Smain::status.

◆ pmain()

static int pmain ( lua_State * L)
static

Definition at line 340 of file lua-5.1.5/src/lua.c.

340 {
341 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
342 char **argv = s->argv;
343 int script;
344 int has_i = 0, has_v = 0, has_e = 0;
345 globalL = L;
346 if (argv[0] && argv[0][0]) progname = argv[0];
347 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
348 luaL_openlibs(L); /* open libraries */
349 lua_gc(L, LUA_GCRESTART, 0);
350 s->status = handle_luainit(L);
351 if (s->status != 0) return 0;
352 script = collectargs(argv, &has_i, &has_v, &has_e);
353 if (script < 0) { /* invalid args? */
354 print_usage();
355 s->status = 1;
356 return 0;
357 }
358 if (has_v) print_version();
359 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
360 if (s->status != 0) return 0;
361 if (script)
362 s->status = handle_script(L, argv, script);
363 if (s->status != 0) return 0;
364 if (has_i)
365 dotty(L);
366 else if (script == 0 && !has_e && !has_v) {
367 if (lua_stdin_is_tty()) {
369 dotty(L);
370 }
371 else dofile(L, NULL); /* executes stdin as a file */
372 }
373 return 0;
374}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API void luaL_openlibs(lua_State *L)
static void print_usage(void)
static int collectargs(char **argv, int *pi, int *pv, int *pe)
static void print_version(void)
static int handle_luainit(lua_State *L)
static int runargs(lua_State *L, char **argv, int n)
static int handle_script(lua_State *L, char **argv, int n)
static void dotty(lua_State *L)
#define LUA_GCSTOP
#define LUA_GCRESTART
#define has_e
#define has_v
#define has_i
#define lua_stdin_is_tty()

References Smain::argv, collectargs(), dofile(), dotty(), globalL, handle_luainit(), handle_script(), has_e, has_i, has_v, lua_gc(), LUA_GCRESTART, LUA_GCSTOP, lua_stdin_is_tty, lua_touserdata(), luaL_openlibs(), NULL, print_usage(), print_version(), progname, runargs(), and s.

Referenced by main().

◆ print_usage()

static void print_usage ( void )
static

Definition at line 42 of file lua-5.1.5/src/lua.c.

42 {
43 fprintf(stderr,
44 "usage: %s [options] [script [args]].\n"
45 "Available options are:\n"
46 " -e stat execute string " LUA_QL("stat") "\n"
47 " -l name require library " LUA_QL("name") "\n"
48 " -i enter interactive mode after executing " LUA_QL("script") "\n"
49 " -v show version information\n"
50 " -- stop handling options\n"
51 " - execute stdin and stop handling options\n"
52 ,
53 progname);
54 fflush(stderr);
55}

References LUA_QL, and progname.

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

◆ print_version()

static void print_version ( void )
static

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

111 {
113}
#define LUA_COPYRIGHT
#define LUA_RELEASE

References l_message(), LUA_COPYRIGHT, LUA_RELEASE, and NULL.

Referenced by pmain().

◆ pushline()

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

Definition at line 177 of file lua-5.1.5/src/lua.c.

177 {
178 char buffer[LUA_MAXINPUT];
179 char *b = buffer;
180 size_t l;
181 const char *prmt = get_prompt(L, firstline);
182 if (lua_readline(L, b, prmt) == 0)
183 return 0; /* no input */
184 l = strlen(b);
185 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
186 b[l-1] = '\0'; /* remove it */
187 if (firstline && b[0] == '=') /* first line starts with `=' ? */
188 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
189 else
190 lua_pushstring(L, b);
191 lua_freeline(L, b);
192 return 1;
193}
static const char * get_prompt(lua_State *L, int firstline)
#define LUA_MAXINPUT
#define lua_freeline(L, b)
#define lua_readline(L, b, p)

References get_prompt(), lua_freeline, LUA_MAXINPUT, lua_pushfstring(), lua_pushstring(), and lua_readline.

Referenced by loadline().

◆ report()

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

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

65 {
66 if (status && !lua_isnil(L, -1)) {
67 const char *msg = lua_tostring(L, -1);
68 if (msg == NULL) msg = "(error object is not a string)";
69 l_message(progname, msg);
70 lua_pop(L, 1);
71 }
72 return status;
73}
#define lua_isnil(L, n)

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

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

◆ runargs()

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

Definition at line 294 of file lua-5.1.5/src/lua.c.

294 {
295 int i;
296 for (i = 1; i < n; i++) {
297 if (argv[i] == NULL) continue;
298 lua_assert(argv[i][0] == '-');
299 switch (argv[i][1]) { /* option */
300 case 'e': {
301 const char *chunk = argv[i] + 2;
302 if (*chunk == '\0') chunk = argv[++i];
304 if (dostring(L, chunk, "=(command line)") != 0)
305 return 1;
306 break;
307 }
308 case 'l': {
309 const char *filename = argv[i] + 2;
310 if (*filename == '\0') filename = argv[++i];
311 lua_assert(filename != NULL);
312 if (dolibrary(L, filename))
313 return 1; /* stop if file fails */
314 break;
315 }
316 default: break;
317 }
318 }
319 return 0;
320}
#define lua_assert(c)
static void chunk(LexState *ls)
static int dolibrary(lua_State *L, const char *name)

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

Referenced by pmain().

◆ traceback()

static int traceback ( lua_State * L)
static

Definition at line 76 of file lua-5.1.5/src/lua.c.

76 {
77 if (!lua_isstring(L, 1)) /* 'message' not a string? */
78 return 1; /* keep it intact */
79 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
80 if (!lua_istable(L, -1)) {
81 lua_pop(L, 1);
82 return 1;
83 }
84 lua_getfield(L, -1, "traceback");
85 if (!lua_isfunction(L, -1)) {
86 lua_pop(L, 2);
87 return 1;
88 }
89 lua_pushvalue(L, 1); /* pass error message */
90 lua_pushinteger(L, 2); /* skip this function and traceback */
91 lua_call(L, 2, 1); /* call debug.traceback */
92 return 1;
93}
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_isstring(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
#define lua_istable(L, n)
#define lua_isfunction(L, n)
#define lua_call(L, n, r)

References lua_call, lua_getfield(), LUA_GLOBALSINDEX, lua_isfunction, lua_isstring(), lua_istable, lua_pop, lua_pushinteger(), and lua_pushvalue().

Referenced by docall().

Variable Documentation

◆ globalL

lua_State* globalL = NULL
static

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

Referenced by laction(), and pmain().

◆ progname

const char* progname = LUA_PROGNAME
static

Definition at line 24 of file lua-5.1.5/src/lua.c.

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