|
Go to the documentation of this file.
20 #ifndef FAUDES_DOXYGEN
41 static lua_State *globalL = NULL;
42 static const char *progname = LUA_PROGNAME;
45 static void lstop (lua_State *L, lua_Debug *ar) {
47 lua_sethook(L, NULL, 0, 0);
48 luaL_error(L, "interrupted!");
52 static void laction ( int i) {
55 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
59 static void print_usage ( void) {
61 "usage: %s [options] [script [args]].\n"
62 "Available options are:\n"
63 " -e stat execute string " LUA_QL( "stat") "\n"
64 " -l name require library " LUA_QL( "name") "\n"
65 " -i enter interactive mode after executing " LUA_QL( "script") "\n"
66 " -v show version information\n"
67 " -d pass on libFAUDES messages to console\n"
68 " -x load libFAUDES extension\n"
69 " -- stop handling options\n"
70 " - execute stdin and stop handling options\n"
77 static void l_message ( const char *pname, const char *msg) {
78 if (pname) fprintf(stderr, "%s: ", pname);
79 fprintf(stderr, "%s\n", msg);
84 static int report (lua_State *L, int status) {
85 if (status && !lua_isnil(L, -1)) {
86 const char *msg = lua_tostring(L, -1);
87 if (msg == NULL) msg = "(error object is not a string)";
88 l_message(progname, msg);
95 static int traceback (lua_State *L) {
96 if (!lua_isstring(L, 1))
98 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
99 if (!lua_istable(L, -1)) {
103 lua_getfield(L, -1, "traceback");
104 if (!lua_isfunction(L, -1)) {
109 lua_pushinteger(L, 2);
115 static int docall (lua_State *L, int narg, int clear) {
117 int base = lua_gettop(L) - narg;
118 lua_pushcfunction(L, traceback);
120 signal(SIGINT, laction);
121 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
122 signal(SIGINT, SIG_DFL);
125 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
131 static void print_version ( void) {
132 std::stringstream sstr;
134 << "Welcome to luafaudes console." << std::endl
137 << "Credits: This libFAUDES interpreter is based on the projects Lua and SWIG." << std::endl
138 << "Type 'faudes.Help()' for a list of faudes related types and functions." << std::endl
139 << "Enter Ctrl-C to exit the luafaudes interpreter" << std::endl;
140 l_message(NULL, sstr.str().c_str());
144 static int getargs (lua_State *L, char **argv, int n) {
148 while (argv[argc]) argc++;
149 narg = argc - (n + 1);
150 luaL_checkstack(L, narg + 3, "too many arguments to script");
151 for (i=n+1; i < argc; i++)
152 lua_pushstring(L, argv[i]);
153 lua_createtable(L, narg, n + 1);
154 for (i=0; i < argc; i++) {
155 lua_pushstring(L, argv[i]);
156 lua_rawseti(L, -2, i - n);
162 static int dofile (lua_State *L, const char *name) {
163 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
164 return report(L, status);
168 static int dostring (lua_State *L, const char *s, const char *name) {
169 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
170 return report(L, status);
174 static int dolibrary (lua_State *L, const char *name) {
175 lua_getglobal(L, "require");
176 lua_pushstring(L, name);
177 return report(L, docall(L, 1, 1));
184 #ifdef LUA_USE_READLINE
213 static char *lua_rl_hist;
214 static int lua_rl_histsize;
216 static lua_State *lua_rl_L;
219 static char **luafaudes_complete_L( const char *text, int start, int end) {
224 static void lua_rl_init(lua_State *L)
231 rl_readline_name = "lua";
233 rl_completer_word_break_characters = ( char *)
234 "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
236 rl_completer_quote_characters = "\"'";
237 rl_completion_append_character = '\0';
238 rl_attempted_completion_function = luafaudes_complete_L;
243 if ((s = getenv( "LUA_HISTSIZE")) &&
244 (lua_rl_histsize = atoi(s))) stifle_history(lua_rl_histsize);
245 if ((lua_rl_hist = getenv( "LUA_HISTORY"))) read_history(lua_rl_hist);
249 static void lua_rl_exit(lua_State *L)
252 if (lua_rl_hist) write_history(lua_rl_hist);
255 #define lua_rl_init(L) ((void)L)
256 #define lua_rl_exit(L) ((void)L)
262 static const char *get_prompt (lua_State *L, int firstline) {
264 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
265 p = lua_tostring(L, -1);
266 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
272 static int incomplete (lua_State *L, int status) {
273 if (status == LUA_ERRSYNTAX) {
275 const char *msg = lua_tolstring(L, -1, &lmsg);
276 const char *tp = msg + lmsg - ( sizeof(LUA_QL( "<eof>")) - 1);
277 if (strstr(msg, LUA_QL( "<eof>")) == tp) {
286 static int pushline (lua_State *L, int firstline) {
287 char buffer[LUA_MAXINPUT];
290 const char *prmt = get_prompt(L, firstline);
291 if (lua_readline(L, b, prmt) == 0)
295 if (l > 0 && b[l-1] == '\n')
297 if (firstline && b[0] == '=')
298 lua_pushfstring(L, "return %s", b+1);
300 lua_pushstring(L, b);
306 static int loadline (lua_State *L) {
312 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
313 if (!incomplete(L, status)) break;
316 lua_pushliteral(L, "\n");
326 static void dotty (lua_State *L) {
328 const char *oldprogname = progname;
331 while ((status = loadline(L)) != -1) {
332 if (status == 0) status = docall(L, 0, 0);
335 if (status == 0 && lua_gettop(L) > 0) {
336 lua_getglobal(L, "print");
338 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
339 l_message(progname, lua_pushfstring(L,
340 "error calling " LUA_QL( "print") " (%s)",
341 lua_tostring(L, -1)));
348 progname = oldprogname;
351 static int handle_script (lua_State *L, char **argv, int n) {
354 int narg = getargs(L, argv, n);
355 lua_setglobal(L, "arg");
357 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
359 status = luaL_loadfile(L, fname);
360 lua_insert(L, -(narg+1));
362 status = docall(L, narg, 0);
365 return report(L, status);
370 #define notail(x) {if ((x)[2] != '\0') return -1;}
374 static int collectargs ( char **argv, int *pi, int *pv, int *pe, int *pd, int *px) {
376 for (i = 1; argv[i] != NULL; i++) {
377 if (argv[i][0] != '-')
379 switch (argv[i][1]) {
382 return (argv[i+1] != NULL ? i+1 : 0);
395 if (argv[i][2] == '\0') {
397 if (argv[i] == NULL) return -1;
405 if (argv[i][2] == '\0') {
407 if (argv[i] == NULL) return -1;
419 static int runargs (lua_State *L, char **argv, int n) {
421 for (i = 1; i < n; i++) {
422 if (argv[i] == NULL) continue;
423 lua_assert(argv[i][0] == '-');
424 switch (argv[i][1]) {
426 const char *chunk = argv[i] + 2;
427 if (*chunk == '\0') chunk = argv[++i];
428 lua_assert(chunk != NULL);
429 if (dostring(L, chunk, "=(command line)") != 0)
434 const char *filename = argv[i] + 2;
435 if (*filename == '\0') filename = argv[++i];
436 lua_assert(filename != NULL);
437 if (dolibrary(L, filename))
442 const char *filename = argv[i] + 2;
443 if (*filename == '\0') filename = argv[++i];
444 lua_assert(filename != NULL);
446 l_message( "fatal error: failed to load extension", filename);
458 static int handle_luainit (lua_State *L) {
459 const char *init = getenv(LUA_INIT);
460 if (init == NULL) return 0;
461 else if (init[0] == '@')
462 return dofile(L, init+1);
464 return dostring(L, init, "=" LUA_INIT);
475 static int pmain (lua_State *L) {
476 struct Smain *s = ( struct Smain *)lua_touserdata(L, 1);
477 char **argv = s->argv;
479 int has_i = 0, has_v = 0, has_e = 0, has_d=0, has_x=0;
481 if (argv[0] && argv[0][0]) progname = argv[0];
489 s->status = handle_luainit(L);
490 if (s->status != 0) return 0;
491 script = collectargs(argv, &has_i, &has_v, &has_e, &has_d, &has_x);
497 if (has_v) print_version();
500 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
501 if (s->status != 0) return 0;
503 s->status = handle_script(L, argv, script);
504 if (s->status != 0) return 0;
507 else if (script == 0 && !has_e && !has_v) {
508 if (lua_stdin_is_tty()) {
512 else dofile(L, NULL);
518 int main ( int argc, char **argv) {
521 lua_State *L = lua_open();
523 l_message(argv[0], "cannot create state: not enough memory");
528 status = lua_cpcall(L, &pmain, &s);
531 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
libFAUDES 2.26g
--- 2015.08.17
--- c++ api documentaion by doxygen
|