|
Go to the documentation of this file.
22 #ifndef FAUDES_DOXYGEN
31 #if defined(LUA_USE_ISATTY)
33 #define faudes_stdin_is_tty() isatty(0)
35 #define faudes_stdin_is_tty() 1
47 #if !defined(LUA_PROGNAME)
48 #define LUA_PROGNAME "luafaudes"
52 #if !defined(LUA_INIT_VAR)
53 #define LUA_INIT_VAR "LUA_INIT"
56 #define LUA_INITVARVERSION LUA_INIT_VAR
64 #if defined(LUA_USE_POSIX)
69 static void setsignal ( int sig, void (*handler)( int)) {
71 sa.sa_handler = handler;
73 sigemptyset(&sa.sa_mask);
74 sigaction(sig, &sa, NULL);
79 #define setsignal signal
87 static void lstop (lua_State *L, lua_Debug *ar) {
89 lua_sethook(L, NULL, 0, 0);
90 luaL_error(L, "interrupted!");
101 int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
110 lua_writestringerror( "%s: ", progname);
111 if (badoption[1] == 'e' || badoption[1] == 'l')
112 lua_writestringerror( "'%s' needs argument\n", badoption);
114 lua_writestringerror( "unrecognized option '%s'\n", badoption);
115 #if LUA_VERSION_NUM >= 504
117 lua_writestringerror(
118 "usage: %s [options] [script [args]]\n"
119 "available options are:\n"
120 " -e stat execute string 'stat'\n"
121 " -i enter interactive mode after executing 'script'\n"
122 " -l mod require library 'mod' into global 'mod'\n"
123 " -l g=mod require library 'mod' into global 'g'\n"
124 " -v show version information\n"
125 " -E ignore environment variables\n"
126 " -W turn warnings on\n"
127 " -d pass on libFAUDES messages to console\n"
128 " -x flx load libFAUDES extension 'flx'\n"
129 " -- stop handling options\n"
130 " - stop handling options and execute stdin\n"
136 lua_writestringerror(
137 "usage: %s [options] [script [args]]\n"
138 "available options are:\n"
139 " -e stat execute string 'stat'\n"
140 " -i enter interactive mode after executing 'script'\n"
141 " -l mod require library 'mod' into global 'mod'\n"
142 " -l g=mod require library 'mod' into global 'g'\n"
143 " -v show version information\n"
144 " -d pass on libFAUDES messages to console\n"
145 " -x flx load libFAUDES extension 'flx'\n"
146 " -- stop handling options\n"
147 " - stop handling options and execute stdin\n"
159 static void l_message ( const char *pname, const char *msg) {
160 if (pname) lua_writestringerror( "%s: ", pname);
161 lua_writestringerror( "%s\n", msg);
169 static int report (lua_State *L, int status) {
170 if (status != LUA_OK) {
171 const char *msg = lua_tostring(L, -1);
173 msg = "(error message not a string)";
185 const char *msg = lua_tostring(L, 1);
187 if (luaL_callmeta(L, 1, "__tostring") &&
188 lua_type(L, -1) == LUA_TSTRING)
191 msg = lua_pushfstring(L, "(error object is a %s value)",
192 luaL_typename(L, 1));
194 #if LUA_VERSION_NUM >= 504
195 luaL_traceback(L, L, msg, 1);
205 static int docall (lua_State *L, int narg, int nres) {
207 int base = lua_gettop(L) - narg;
212 status = lua_pcall(L, narg, nres, base);
221 std::stringstream sstr;
223 << "welcome to the libFAUDES-Lua console" << std::endl
224 << "version: " << VersionString() << " / " << LUA_RELEASE << std::endl
227 << "credits: this libFAUDES interpreter is based on the projects Lua and SWIG" << std::endl
228 << "enter 'faudes.Help()' for a list of faudes related types and functions" << std::endl
229 << "press ctrl-c to exit the interpreter";
230 lua_writestring(sstr.str().c_str(),sstr.str().length());
247 narg = argc - (script + 1);
248 lua_createtable(L, narg, script + 1);
249 for (i = 0; i < argc; i++) {
250 lua_pushstring(L, argv[i]);
251 lua_rawseti(L, -2, i - script);
253 lua_setglobal(L, "arg");
257 static int dochunk (lua_State *L, int status) {
258 if (status == LUA_OK) status = docall(L, 0, 0);
263 static int dofile (lua_State *L, const char *name) {
264 return dochunk(L, luaL_loadfile(L, name));
268 static int dostring (lua_State *L, const char *s, const char *name) {
269 return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
281 char *modname = strchr(globname, '=');
282 if (modname == NULL) {
284 suffix = strchr(modname, *LUA_IGMARK);
290 lua_getglobal(L, "require");
291 lua_pushstring(L, modname);
293 if (status == LUA_OK) {
296 lua_setglobal(L, globname);
311 lua_getglobal(L, "arg");
312 if(lua_type(L, -1) != LUA_TTABLE)
313 luaL_error(L, "'arg' is not a table");
315 #if LUA_VERSION_NUM >= 504
317 n = (int)luaL_len(L, -1);
321 if(!luaL_callmeta(L, -1, "__len"))
322 n= ( int) lua_objlen(L,-1);
324 luaL_checkstack(L, n + 3, "too many arguments to script");
325 for (i = 1; i <= n; i++)
326 lua_rawgeti(L, -i, i);
334 const char *fname = argv[0];
335 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
337 status = luaL_loadfile(L, fname);
338 if (status == LUA_OK) {
340 status = docall(L, n, LUA_MULTRET);
366 if (argv[0] != NULL) {
374 for (i = 1; argv[i] != NULL; i++) {
376 if (argv[i][0] != '-')
378 switch (argv[i][1]) {
380 if (argv[i][2] != '\0')
387 if (argv[i][2] != '\0')
392 if (argv[i][2] != '\0')
398 if (argv[i][2] != '\0')
405 if (argv[i][2] == '\0') {
407 if (argv[i] == NULL || argv[i][0] == '-')
412 if (argv[i][2] != '\0')
417 if (argv[i][2] == '\0') {
419 if (argv[i] == NULL || argv[i][0] == '-')
438 static int runargs (lua_State *L, char **argv, int n) {
440 for (i = 1; i < n; i++) {
441 int option = argv[i][1];
442 lua_assert(argv[i][0] == '-');
444 case 'e': case 'l': {
446 char *extra = argv[i] + 2;
447 if (*extra == '\0') extra = argv[++i];
448 lua_assert(extra != NULL);
449 status = (option == 'e')
450 ? dostring(L, extra, "=(command line)")
452 if (status != LUA_OK) return 0;
455 #if LUA_VERSION_NUM >= 504
458 lua_warning(L, "@on", 0);
462 const char *filename = argv[i] + 2;
463 if (*filename == '\0') filename = argv[++i];
464 lua_assert(filename != NULL);
466 l_message( "fatal error: failed to load extension", filename);
479 const char *init = getenv(name + 1);
482 init = getenv(name + 1);
484 if (init == NULL) return LUA_OK;
485 else if (init[0] == '@')
498 #if !defined(LUA_PROMPT)
499 #define LUA_PROMPT "> "
500 #define LUA_PROMPT2 ">> "
503 #if !defined(LUA_MAXINPUT)
504 #define LUA_MAXINPUT 512
512 #if !defined(lua_stdin_is_tty)
514 #if defined(LUA_USE_POSIX)
517 #define lua_stdin_is_tty() isatty(0)
519 #elif defined(LUA_USE_WINDOWS)
524 #define lua_stdin_is_tty() _isatty(_fileno(stdin))
529 #define lua_stdin_is_tty() 1
542 #if !defined(lua_readline)
544 #if defined(LUA_USE_READLINE)
561 #include <readline/readline.h>
562 #include <readline/history.h>
564 static char *lua_rl_hist;
565 static int lua_rl_histsize;
567 static lua_State *lua_rl_L;
570 static char **faudes_complete_L( const char *text, int start, int end) {
580 rl_readline_name = "lua";
582 rl_completer_word_break_characters = ( char *)
583 "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
585 rl_completer_quote_characters = "\"'";
586 rl_completion_append_character = '\0';
587 rl_attempted_completion_function = faudes_complete_L;
592 if ((s = getenv( "LUA_HISTSIZE")) &&
593 (lua_rl_histsize = atoi(s))) stifle_history(lua_rl_histsize);
594 if ((lua_rl_hist = getenv( "LUA_HISTORY"))) read_history(lua_rl_hist);
601 if (lua_rl_hist) write_history(lua_rl_hist);
604 #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
605 #define lua_saveline(L,line) ((void)L, add_history(line))
606 #define lua_freeline(L,b) ((void)L, free(b))
611 #define lua_initreadline(L) ((void)L)
612 #define lua_readline(L,b,p) \
613 ((void)L, fputs(p, stdout), fflush(stdout), \
614 fgets(b, LUA_MAXINPUT, stdin) != NULL)
615 #define lua_saveline(L,line) { (void)L; (void)line; }
616 #define lua_freeline(L,b) { (void)L; (void)b; }
617 #define lua_exitreadline(L) { (void)L; }
629 static const char * get_prompt (lua_State *L, int firstline) {
631 #if LUA_VERSION_NUM >= 504
633 if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
636 const char *p = luaL_tolstring(L, -1, NULL);
643 lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
644 p = lua_tostring(L, -1);
652 #define EOFMARK "<eof>"
653 #define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
662 if (status == LUA_ERRSYNTAX) {
664 const char *msg = lua_tolstring(L, -1, &lmsg);
675 static int pushline (lua_State *L, int firstline) {
685 if (l > 0 && b[l-1] == '\n')
687 if (firstline && b[0] == '=')
688 lua_pushfstring(L, "return %s", b + 1);
690 lua_pushlstring(L, b, l);
701 const char *line = lua_tostring(L, -1);
702 const char *retline = lua_pushfstring(L, "return %s;", line);
703 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
704 if (status == LUA_OK) {
721 const char *line = lua_tolstring(L, 1, &len);
722 int status = luaL_loadbuffer(L, line, len, "=stdin");
728 lua_pushliteral(L, "\n");
749 lua_assert(lua_gettop(L) == 1);
758 int n = lua_gettop(L);
760 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
761 lua_getglobal(L, "print");
763 if (lua_pcall(L, n, 0, 0) != LUA_OK)
765 lua_tostring(L, -1)));
779 while ((status = loadline(L)) != -1) {
780 if (status == LUA_OK)
781 status = docall(L, 0, LUA_MULTRET);
782 if (status == LUA_OK) l_print(L);
800 int argc = (int)lua_tointeger(L, 1);
801 char **argv = ( char **)lua_touserdata(L, 2);
804 int optlim = (script > 0) ? script : argc;
807 #if LUA_VERSION_NUM >= 504
808 luaL_checkversion(L);
821 #if LUA_VERSION_NUM >= 504
824 lua_pushboolean(L, 1);
825 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
829 luaopen_faudes_allplugins(L);
831 #if LUA_VERSION_NUM >= 504
833 lua_gc(L, LUA_GCRESTART);
834 lua_gc(L, LUA_GCGEN, 0, 0);
837 lua_gc(L, LUA_GCRESTART,0);
839 if (!(args & has_E)) {
853 else if (script < 1 && !(args & ( has_e | has_v))) {
860 lua_pushboolean(L, 1);
865 int main ( int argc, char **argv) {
867 lua_State *L = luaL_newstate();
869 l_message(argv[0], "cannot create state: not enough memory");
872 #if LUA_VERSION_NUM >= 504
874 lua_gc(L, LUA_GCSTOP);
877 lua_gc(L, LUA_GCSTOP,0);
879 lua_pushcfunction(L, & pmain);
880 lua_pushinteger(L, argc);
881 lua_pushlightuserdata(L, argv);
882 status = lua_pcall(L, 2, 1, 0);
883 result = lua_toboolean(L, -1);
886 return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
static int report(lua_State *L, int status)
#define lua_initreadline(L)
static int pushargs(lua_State *L)
static int collectargs(char **argv, int *first)
#define lua_exitreadline(L)
#define LUA_INITVARVERSION
static void print_version(void)
int main(int argc, char **argv)
static int docall(lua_State *L, int narg, int nres)
#define lua_freeline(L, b)
static int handle_script(lua_State *L, char **argv)
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 dochunk(lua_State *L, int status)
static void print_usage(const char *badoption)
static void doREPL(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 void l_print(lua_State *L)
static int msghandler(lua_State *L)
#define lua_saveline(L, line)
static int addreturn(lua_State *L)
#define faudes_stdin_is_tty()
static int dolibrary(lua_State *L, char *globname)
static void laction(int i)
static void l_message(const char *pname, const char *msg)
static const char * progname
static int loadline(lua_State *L)
static int dostring(lua_State *L, const char *s, const char *name)
static void createargtable(lua_State *L, char **argv, int argc, int script)
static lua_State * globalL
static const char * get_prompt(lua_State *L, int firstline)
static int incomplete(lua_State *L, int status)
#define lua_readline(L, b, p)
static int multiline(lua_State *L)
std::string VersionString()
std::string PluginsString()
int faudes_loadext(lua_State *pL, const char *filename)
std::string BuildString()
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
int faudes_loaddefext(lua_State *pL, const char *arg0)
libFAUDES 2.33l
--- 2025.09.16
--- c++ api documentaion by doxygen
|