|
Go to the documentation of this file.
20 #ifndef FAUDES_DOXYGEN
47 static void lstop (lua_State *L, lua_Debug *ar) {
49 lua_sethook(L, NULL, 0, 0);
50 luaL_error(L, "interrupted!");
57 lua_sethook( globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
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)";
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;
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);
134 std::stringstream sstr;
136 << "Welcome to luafaudes console." << std::endl
137 << "Versions: " << VersionString() << " / " << LUA_VERSION << std::endl
140 << "Credits: This libFAUDES interpreter is based on the projects Lua and SWIG." << std::endl
141 << "Type 'faudes.Help()' for a list of faudes related types and functions." << std::endl
142 << "Enter Ctrl-C to exit the luafaudes interpreter" << std::endl;
147 static int getargs (lua_State *L, char **argv, int n) {
151 while (argv[argc]) argc++;
152 narg = argc - (n + 1);
153 luaL_checkstack(L, narg + 3, "too many arguments to script");
154 for (i=n+1; i < argc; i++)
155 lua_pushstring(L, argv[i]);
156 lua_createtable(L, narg, n + 1);
157 for (i=0; i < argc; i++) {
158 lua_pushstring(L, argv[i]);
159 lua_rawseti(L, -2, i - n);
165 static int dofile (lua_State *L, const char *name) {
166 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
171 static int dostring (lua_State *L, const char *s, const char *name) {
172 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
178 lua_getglobal(L, "require");
179 lua_pushstring(L, name);
187 #ifdef LUA_USE_READLINE
216 static char *lua_rl_hist;
217 static int lua_rl_histsize;
219 static lua_State *lua_rl_L;
222 static char **faudes_complete_L( const char *text, int start, int end) {
234 rl_readline_name = "lua";
236 rl_completer_word_break_characters = ( char *)
237 "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
239 rl_completer_quote_characters = "\"'";
240 rl_completion_append_character = '\0';
241 rl_attempted_completion_function = faudes_complete_L;
246 if ((s = getenv( "LUA_HISTSIZE")) &&
247 (lua_rl_histsize = atoi(s))) stifle_history(lua_rl_histsize);
248 if ((lua_rl_hist = getenv( "LUA_HISTORY"))) read_history(lua_rl_hist);
255 if (lua_rl_hist) write_history(lua_rl_hist);
258 #define lua_rl_init(L) ((void)L)
259 #define lua_rl_exit(L) ((void)L)
265 static const char * get_prompt (lua_State *L, int firstline) {
267 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
268 p = lua_tostring(L, -1);
269 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
276 if (status == LUA_ERRSYNTAX) {
278 const char *msg = lua_tolstring(L, -1, &lmsg);
279 const char *tp = msg + lmsg - ( sizeof(LUA_QL( "<eof>")) - 1);
280 if (strstr(msg, LUA_QL( "<eof>")) == tp) {
289 static int pushline (lua_State *L, int firstline) {
290 char buffer[LUA_MAXINPUT];
294 if (lua_readline(L, b, prmt) == 0)
297 if (l > 0 && b[l-1] == '\n')
299 if (firstline && b[0] == '=')
300 lua_pushfstring(L, "return %s", b+1);
302 lua_pushstring(L, b);
314 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
318 lua_pushliteral(L, "\n");
333 while ((status = loadline(L)) != -1) {
334 if (status == 0) status = docall(L, 0, 0);
336 if (status == 0 && lua_gettop(L) > 0) {
337 lua_getglobal(L, "print");
339 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
341 "error calling " LUA_QL( "print") " (%s)",
342 lua_tostring(L, -1)));
355 int narg = getargs(L, argv, n);
356 lua_setglobal(L, "arg");
358 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
360 status = luaL_loadfile(L, fname);
361 lua_insert(L, -(narg+1));
363 status = docall(L, narg, 0);
371 #define notail(x) {if ((x)[2] != '\0') return -1;}
375 static int collectargs ( char **argv, int *pi, int *pv, int *pe, int *pd, int *px) {
377 for (i = 1; argv[i] != NULL; i++) {
378 if (argv[i][0] != '-')
380 switch (argv[i][1]) {
383 return (argv[i+1] != NULL ? i+1 : 0);
396 if (argv[i][2] == '\0') {
398 if (argv[i] == NULL) return -1;
406 if (argv[i][2] == '\0') {
408 if (argv[i] == NULL) return -1;
420 static int runargs (lua_State *L, char **argv, int n) {
422 for (i = 1; i < n; i++) {
423 if (argv[i] == NULL) continue;
424 lua_assert(argv[i][0] == '-');
425 switch (argv[i][1]) {
427 const char *chunk = argv[i] + 2;
428 if (*chunk == '\0') chunk = argv[++i];
429 lua_assert(chunk != NULL);
430 if ( dostring(L, chunk, "=(command line)") != 0)
435 const char *filename = argv[i] + 2;
436 if (*filename == '\0') filename = argv[++i];
437 lua_assert(filename != NULL);
443 const char *filename = argv[i] + 2;
444 if (*filename == '\0') filename = argv[++i];
445 lua_assert(filename != NULL);
447 l_message( "fatal error: failed to load extension", filename);
460 const char *init = getenv(LUA_INIT);
461 if (init == NULL) return 0;
462 else if (init[0] == '@')
465 return dostring(L, init, "=" LUA_INIT);
477 struct Smain *s = ( struct Smain *)lua_touserdata(L, 1);
480 int has_i = 0, has_v = 0, has_e = 0, has_d=0, has_x=0;
491 if (s-> status != 0) return 0;
502 if (s-> status != 0) return 0;
505 if (s-> status != 0) return 0;
508 else if (script == 0 && !has_e && !has_v) {
509 if (lua_stdin_is_tty()) {
522 lua_State *L = lua_open();
524 l_message( argv[0], "cannot create state: not enough memory");
532 return ( status || s. status) ? EXIT_FAILURE : EXIT_SUCCESS;
static int report(lua_State *L, int status)
static void print_usage(void)
static int getargs(lua_State *L, char **argv, int n)
static void print_version(void)
int main(int argc, char **argv)
static int docall(lua_State *L, int narg, int clear)
static int handle_luainit(lua_State *L)
static void lstop(lua_State *L, lua_Debug *ar)
static int pushline(lua_State *L, int firstline)
static int traceback(lua_State *L)
static int runargs(lua_State *L, char **argv, int n)
static int dofile(lua_State *L, const char *name)
static int pmain(lua_State *L)
static int collectargs(char **argv, int *pi, int *pv, int *pe, int *pd, int *px)
static void laction(int i)
static void l_message(const char *pname, const char *msg)
static int dolibrary(lua_State *L, const char *name)
static const char * progname
static int loadline(lua_State *L)
static int dostring(lua_State *L, const char *s, const char *name)
static int handle_script(lua_State *L, char **argv, int n)
static void dotty(lua_State *L)
static lua_State * globalL
static const char * get_prompt(lua_State *L, int firstline)
static int incomplete(lua_State *L, int status)
std::string VersionString()
FAUDES_API void faudes_mute(bool on)
std::string PluginsString()
int faudes_loadext(lua_State *pL, const char *filename)
std::string BuildString()
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.33b
--- 2025.05.07
--- c++ api documentaion by doxygen
|