|
Go to the documentation of this file.
20 #ifndef FAUDES_DOXYGEN
43 #if defined(LUA_USE_ISATTY)
45 #define faudes_stdin_is_tty() isatty(0)
47 #define faudes_stdin_is_tty() 1
55 static void lstop (lua_State *L, lua_Debug *ar) {
57 lua_sethook(L, NULL, 0, 0);
58 luaL_error(L, "interrupted!");
65 lua_sethook( globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
71 "usage: %s [options] [script [args]].\n"
72 "Available options are:\n"
73 " -e stat execute string " LUA_QL( "stat") "\n"
74 " -l name require library " LUA_QL( "name") "\n"
75 " -i enter interactive mode after executing " LUA_QL( "script") "\n"
76 " -v show version information\n"
77 " -d pass on libFAUDES messages to console\n"
78 " -x load libFAUDES extension\n"
79 " -- stop handling options\n"
80 " - execute stdin and stop handling options\n"
87 static void l_message ( const char *pname, const char *msg) {
88 if (pname) fprintf(stderr, "%s: ", pname);
89 fprintf(stderr, "%s\n", msg);
94 static int report (lua_State *L, int status) {
95 if (status && !lua_isnil(L, -1)) {
96 const char *msg = lua_tostring(L, -1);
97 if (msg == NULL) msg = "(error object is not a string)";
106 if (!lua_isstring(L, 1))
108 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
109 if (!lua_istable(L, -1)) {
113 lua_getfield(L, -1, "traceback");
114 if (!lua_isfunction(L, -1)) {
119 lua_pushinteger(L, 2);
125 static int docall (lua_State *L, int narg, int clear) {
127 int base = lua_gettop(L) - narg;
131 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
132 signal(SIGINT, SIG_DFL);
135 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
142 std::stringstream sstr;
144 << "Welcome to luafaudes console." << std::endl
145 << "Versions: " << VersionString() << " / " << LUA_VERSION << std::endl
148 << "Credits: This libFAUDES interpreter is based on the projects Lua and SWIG." << std::endl
149 << "Type 'faudes.Help()' for a list of faudes related types and functions." << std::endl
150 << "Enter Ctrl-C to exit the luafaudes interpreter" << std::endl;
155 static int getargs (lua_State *L, char **argv, int n) {
159 while (argv[argc]) argc++;
160 narg = argc - (n + 1);
161 luaL_checkstack(L, narg + 3, "too many arguments to script");
162 for (i=n+1; i < argc; i++)
163 lua_pushstring(L, argv[i]);
164 lua_createtable(L, narg, n + 1);
165 for (i=0; i < argc; i++) {
166 lua_pushstring(L, argv[i]);
167 lua_rawseti(L, -2, i - n);
173 static int dofile (lua_State *L, const char *name) {
174 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
179 static int dostring (lua_State *L, const char *s, const char *name) {
180 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
186 lua_getglobal(L, "require");
187 lua_pushstring(L, name);
195 #ifdef LUA_USE_READLINE
224 static char *lua_rl_hist;
225 static int lua_rl_histsize;
227 static lua_State *lua_rl_L;
230 static char **faudes_complete_L( const char *text, int start, int end) {
242 rl_readline_name = "lua";
244 rl_completer_word_break_characters = ( char *)
245 "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
247 rl_completer_quote_characters = "\"'";
248 rl_completion_append_character = '\0';
249 rl_attempted_completion_function = faudes_complete_L;
254 if ((s = getenv( "LUA_HISTSIZE")) &&
255 (lua_rl_histsize = atoi(s))) stifle_history(lua_rl_histsize);
256 if ((lua_rl_hist = getenv( "LUA_HISTORY"))) read_history(lua_rl_hist);
263 if (lua_rl_hist) write_history(lua_rl_hist);
266 #define lua_rl_init(L) ((void)L)
267 #define lua_rl_exit(L) ((void)L)
273 static const char * get_prompt (lua_State *L, int firstline) {
275 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
276 p = lua_tostring(L, -1);
277 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
284 if (status == LUA_ERRSYNTAX) {
286 const char *msg = lua_tolstring(L, -1, &lmsg);
287 const char *tp = msg + lmsg - ( sizeof(LUA_QL( "<eof>")) - 1);
288 if (strstr(msg, LUA_QL( "<eof>")) == tp) {
297 static int pushline (lua_State *L, int firstline) {
298 char buffer[LUA_MAXINPUT];
302 if (lua_readline(L, b, prmt) == 0)
305 if (l > 0 && b[l-1] == '\n')
307 if (firstline && b[0] == '=')
308 lua_pushfstring(L, "return %s", b+1);
310 lua_pushstring(L, b);
322 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
326 lua_pushliteral(L, "\n");
341 while ((status = loadline(L)) != -1) {
342 if (status == 0) status = docall(L, 0, 0);
344 if (status == 0 && lua_gettop(L) > 0) {
345 lua_getglobal(L, "print");
347 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
349 "error calling " LUA_QL( "print") " (%s)",
350 lua_tostring(L, -1)));
363 int narg = getargs(L, argv, n);
364 lua_setglobal(L, "arg");
366 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
368 status = luaL_loadfile(L, fname);
369 lua_insert(L, -(narg+1));
371 status = docall(L, narg, 0);
379 #define notail(x) {if ((x)[2] != '\0') return -1;}
383 static int collectargs ( char **argv, int *pi, int *pv, int *pe, int *pd, int *px) {
385 for (i = 1; argv[i] != NULL; i++) {
386 if (argv[i][0] != '-')
388 switch (argv[i][1]) {
391 return (argv[i+1] != NULL ? i+1 : 0);
404 if (argv[i][2] == '\0') {
406 if (argv[i] == NULL) return -1;
414 if (argv[i][2] == '\0') {
416 if (argv[i] == NULL) return -1;
428 static int runargs (lua_State *L, char **argv, int n) {
430 for (i = 1; i < n; i++) {
431 if (argv[i] == NULL) continue;
432 lua_assert(argv[i][0] == '-');
433 switch (argv[i][1]) {
435 const char *chunk = argv[i] + 2;
436 if (*chunk == '\0') chunk = argv[++i];
437 lua_assert(chunk != NULL);
438 if ( dostring(L, chunk, "=(command line)") != 0)
443 const char *filename = argv[i] + 2;
444 if (*filename == '\0') filename = argv[++i];
445 lua_assert(filename != NULL);
451 const char *filename = argv[i] + 2;
452 if (*filename == '\0') filename = argv[++i];
453 lua_assert(filename != NULL);
455 l_message( "fatal error: failed to load extension", filename);
468 const char *init = getenv(LUA_INIT);
469 if (init == NULL) return 0;
470 else if (init[0] == '@')
473 return dostring(L, init, "=" LUA_INIT);
485 struct Smain *s = ( struct Smain *)lua_touserdata(L, 1);
488 int has_i = 0, has_v = 0, has_e = 0, has_d=0, has_x=0;
499 if (s-> status != 0) return 0;
510 if (s-> status != 0) return 0;
513 if (s-> status != 0) return 0;
518 if (script == 0 && !has_e) {
532 lua_State *L = lua_open();
534 l_message( argv[0], "cannot create state: not enough memory");
542 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)
#define faudes_stdin_is_tty()
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()
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.33h
--- 2025.06.18
--- c++ api documentaion by doxygen
|