|
Go to the documentation of this file.
20 #ifndef FAUDES_DOXYGEN
43 static lua_State *globalL = NULL;
44 static const char *progname = LUA_PROGNAME;
47 static void lstop (lua_State *L, lua_Debug *ar) {
49 lua_sethook(L, NULL, 0, 0);
50 luaL_error(L, "interrupted!");
54 static void laction ( int i) {
57 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
61 static void print_usage ( void) {
63 "usage: %s [options] [script [args]].\n"
64 "Available options are:\n"
65 " -e stat execute string " LUA_QL( "stat") "\n"
66 " -l name require library " LUA_QL( "name") "\n"
67 " -i enter interactive mode after executing " LUA_QL( "script") "\n"
68 " -v show version information\n"
69 " -d pass on libFAUDES messages to console\n"
70 " -x load libFAUDES extension\n"
71 " -- stop handling options\n"
72 " - execute stdin and stop handling options\n"
79 static void l_message ( const char *pname, const char *msg) {
80 if (pname) fprintf(stderr, "%s: ", pname);
81 fprintf(stderr, "%s\n", msg);
86 static int report (lua_State *L, int status) {
87 if (status && !lua_isnil(L, -1)) {
88 const char *msg = lua_tostring(L, -1);
89 if (msg == NULL) msg = "(error object is not a string)";
90 l_message(progname, msg);
97 static int traceback (lua_State *L) {
98 if (!lua_isstring(L, 1))
100 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
101 if (!lua_istable(L, -1)) {
105 lua_getfield(L, -1, "traceback");
106 if (!lua_isfunction(L, -1)) {
111 lua_pushinteger(L, 2);
117 static int docall (lua_State *L, int narg, int clear) {
119 int base = lua_gettop(L) - narg;
120 lua_pushcfunction(L, traceback);
122 signal(SIGINT, laction);
123 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
124 signal(SIGINT, SIG_DFL);
127 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
133 static void print_version ( void) {
134 std::stringstream sstr;
136 << "Welcome to luafaudes console." << std::endl
137 << "Versions: " << VersionString() << " / " << LUA_VERSION << std::endl
139 << "Credits: This libFAUDES interpreter is based on the projects Lua and SWIG." << std::endl
140 << "Type 'faudes.Help()' for a list of faudes related types and functions." << std::endl
141 << "Enter Ctrl-C to exit the luafaudes interpreter" << std::endl;
142 l_message(NULL, sstr.str().c_str());
146 static int getargs (lua_State *L, char **argv, int n) {
150 while (argv[argc]) argc++;
151 narg = argc - (n + 1);
152 luaL_checkstack(L, narg + 3, "too many arguments to script");
153 for (i=n+1; i < argc; i++)
154 lua_pushstring(L, argv[i]);
155 lua_createtable(L, narg, n + 1);
156 for (i=0; i < argc; i++) {
157 lua_pushstring(L, argv[i]);
158 lua_rawseti(L, -2, i - n);
164 static int dofile (lua_State *L, const char *name) {
165 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
166 return report(L, status);
170 static int dostring (lua_State *L, const char *s, const char *name) {
171 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
172 return report(L, status);
176 static int dolibrary (lua_State *L, const char *name) {
177 lua_getglobal(L, "require");
178 lua_pushstring(L, name);
179 return report(L, docall(L, 1, 1));
186 #ifdef LUA_USE_READLINE
215 static char *lua_rl_hist;
216 static int lua_rl_histsize;
218 static lua_State *lua_rl_L;
221 static char **faudes_complete_L( const char *text, int start, int end) {
226 static void lua_rl_init(lua_State *L)
233 rl_readline_name = "lua";
235 rl_completer_word_break_characters = ( char *)
236 "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
238 rl_completer_quote_characters = "\"'";
239 rl_completion_append_character = '\0';
240 rl_attempted_completion_function = faudes_complete_L;
245 if ((s = getenv( "LUA_HISTSIZE")) &&
246 (lua_rl_histsize = atoi(s))) stifle_history(lua_rl_histsize);
247 if ((lua_rl_hist = getenv( "LUA_HISTORY"))) read_history(lua_rl_hist);
251 static void lua_rl_exit(lua_State *L)
254 if (lua_rl_hist) write_history(lua_rl_hist);
257 #define lua_rl_init(L) ((void)L)
258 #define lua_rl_exit(L) ((void)L)
264 static const char *get_prompt (lua_State *L, int firstline) {
266 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
267 p = lua_tostring(L, -1);
268 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
274 static int incomplete (lua_State *L, int status) {
275 if (status == LUA_ERRSYNTAX) {
277 const char *msg = lua_tolstring(L, -1, &lmsg);
278 const char *tp = msg + lmsg - ( sizeof(LUA_QL( "<eof>")) - 1);
279 if (strstr(msg, LUA_QL( "<eof>")) == tp) {
288 static int pushline (lua_State *L, int firstline) {
289 char buffer[LUA_MAXINPUT];
292 const char *prmt = get_prompt(L, firstline);
293 if (lua_readline(L, b, prmt) == 0)
296 if (l > 0 && b[l-1] == '\n')
298 if (firstline && b[0] == '=')
299 lua_pushfstring(L, "return %s", b+1);
301 lua_pushstring(L, b);
307 static int loadline (lua_State *L) {
313 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
314 if (!incomplete(L, status)) break;
317 lua_pushliteral(L, "\n");
327 static void dotty (lua_State *L) {
329 const char *oldprogname = progname;
332 while ((status = loadline(L)) != -1) {
333 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;
int main(int argc, char *argv[])
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
std::string VersionString() Return FAUDES_VERSION as std::string.
FAUDES_API void faudes_mute(bool on)
std::string PluginsString() Return FAUDES_PLUGINS as std::string.
int faudes_loadext(lua_State *pL, const char *filename)
void faudes_initialize(lua_State *pL)
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
int faudes_loaddefext(lua_State *pL, const char *arg0)
libFAUDES 2.32b
--- 2024.03.01
--- c++ api documentaion by doxygen
|