diff --git a/src/console.c b/src/console.c deleted file mode 100644 index 38a486b..0000000 --- a/src/console.c +++ /dev/null @@ -1,476 +0,0 @@ - -#define PRIMARY_REPL_BUFFER_SIZE (1024 + 1) -#define SECONDARY_REPL_BUFFER_SIZE (1032 + 1) - - -#define DEFINES_INIT 4 -#define DEFINES_EXPANSION 4 - -#define LIBRARIES_INIT 2 -#define LIBRARIES_EXPANSION 2 - - -#if defined(linux) || defined(__linux__) || defined(__linux) -# include -# include -# include -#elif defined(unix) || defined(__unix__) || defined(__unix) -# include -# include -# include -#elif defined(__APPLE__) || defined(__MACH__) -# include -# include -# include -#elif defined(_WIN32) || defined(_WIN64) -# include -# include -# include -# include -#else -# error "Not familiar. Set up headers accordingly, or -D__linux__ of -Dunix or -D__APPLE__ or -D_WIN32" -#endif - -#include - -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" - -#include "darr.h" - -#if defined(LUACON_ADDITIONS) -# include "additions.h" -#endif - - -#define LUA_CONSOLE_COPYRIGHT "LuaConsole Copyright MIT (C) 2017 Hydroque\n" - - -// internal enums, represent lua error category -typedef enum LuaConsoleError { - INTERNAL_ERROR = 0, - SYNTAX_ERROR = 1, - RUNTIME_ERROR = 2, -} LuaConsoleError; - - -// usage message -const char HELP_MESSAGE[] = - "Lua Console | Version: 1/5/2017\n" - LUA_COPYRIGHT - "\n" - LUA_CONSOLE_COPYRIGHT - "\n" - "Supports Lua5.3, Lua5.2, Lua5.1\n" - "\n" - "\t- Files executed by passing\n" - "\t- Global variable defintions\n" - "\t- Dynamic module loading\n" - "\t- PUC-Lua compatibility support\n" - #if defined(LUACON_ADDITIONS) - "\t- Working directory support\n" - "\t- Built in stack-dump\n" - "\t- Console clearing\n" - #endif - "\n" - "Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-p] [-a] [-c] [-Dvar=val] [-Lfilepath.lua] [-b[a,b,c]] [-?] [-n]{parameter1 ...} \n" - "\n" - "-v \t Prints the Lua version in use\n" - "-e \t Prevents lua core libraries from loading\n" - "-s \t Issues a new root path\n" - "-p \t Has console post exist after script in line by line mode\n" - #if defined(LUACON_ADDITIONS) - "-a \t Disables the additions\n" - #endif - "-c \t No copyright on init\n" - "-d \t Defines a global variable as value after '='\n" - "-l \t Executes a module before specified script\n" - "-b[a,b,c] \t Load parameters arg differently. a=before passed -l's, b=give passed -l's a tuple, c=give passed file a tuple\n" - "-n \t Start of parameter section\n" - "-? \t Displays this help message\n"; - - - -// one environment per process -static lua_State* L = NULL; - - -// necessary to put this outside of main, print doesn't work -static int no_libraries = 0; - - - -// easy macro for error handling -static inline void check_error_OOM(int cond, int line) { - if(cond == 1) { - fprintf(stderr, "ERROR: Out of memory! %d\n", line); - exit(EXIT_FAILURE); - } -} - - -// handles out-of-lua error messages -// returns 1 item: -// 1. error message -static void print_error(LuaConsoleError error, int offset) { - switch(error) { - case INTERNAL_ERROR: - fprintf(stderr, " (Internal)"); - break; - case SYNTAX_ERROR: - fprintf(stderr, " (Syntax)"); - break; - case RUNTIME_ERROR: - fprintf(stderr, " (Runtime)"); - break; - } - const char* msg = lua_tostring(L, -1); - size_t top = lua_gettop(L); - fprintf(stderr, " | Stack Top: %zu | %s\n", top - offset, msg); - #if defined(LUACON_ADDITIONS) - if(top - offset > 1) - stack_dump(L); - #endif -} - -// handles in-lua runtime error messages -// returns 1 item: -// 1. error message -static int lua_print_error(lua_State* L) { - const char* msg = lua_tostring(L, -1); - - luaL_traceback(L, L, "--", 1); - const char* tb = lua_tostring(L, -1); - lua_pop(L, 1); - - size_t top = lua_gettop(L); - fprintf(stderr, " (Runtime) | Stack Top: %zu | %s\n %s\n", top, msg, tb); - #if defined(LUACON_ADDITIONS) - if(top > 1) - stack_dump(L); - #endif - return 1; -} - - - -// handle execution of files -static int start_protective_mode_file(const char* file, char** parameters_argv, size_t param_len) { - lua_pushcclosure(L, lua_print_error, 0); - int base = lua_gettop(L); - int status = 0; - if((status = luaL_loadfile(L, file)) != 0) { - print_error(SYNTAX_ERROR, 1); - lua_pop(L, 2); // err msg, err handler - return status; - } - if(parameters_argv != NULL) - for(size_t i=0; i 1) - no_file = 1; - } else { - // don't try to execute file if it isn't first argument - if(argv[1][0] == '-') - no_file = 1; - for(size_t i=1; i<(size_t)argc; i++) { - // if we have args around, break - if(parameters_argv != 0) - break; - // don't parse non-switches - switch(argv[i][0]) { - case '/': - case '-': - break; - default: - continue; - } - // set variables up for later parsing - switch(argv[i][1]) { - case 'v': case 'V': - print_version = 1; - break; - case 'e': case 'E': - no_libraries = 1; - break; - case 's': case 'S': - change_start = 1; - start = argv[i+1]; - break; - #if defined(LUACON_ADDITIONS) - case 'a': case 'A': - no_additions = 1; - break; - #endif - case 'c': case 'C': - copyright_squelch = 1; - break; - case 'd': case 'D': - if(globals == NULL) - globals = array_new(DEFINES_INIT, DEFINES_EXPANSION, sizeof(char*)); - check_error_OOM(globals == NULL, __LINE__); - array_push(globals, argv[i]); - break; - case 'l': case 'L': - if(libraries == NULL) - libraries = array_new(LIBRARIES_INIT, LIBRARIES_EXPANSION, sizeof(char*)); - check_error_OOM(libraries == NULL, __LINE__); - array_push(libraries, argv[i]); - break; - case 'b': case 'B': - for(size_t j=0; jsize; i++) { - char* str = (char*) array_get(globals, i) + 2; - - char* m_args = strsplit(str, '=', strlen(str) + 1, 2); - if(m_args == 0) { - fputs("Error: Incorrect -D specified. Use format 'name=value'.", stderr); - return EXIT_FAILURE; - } - char* arg1 = m_args; - char* arg2 = arg1 + (strlen(arg1) + 1); - - size_t dot_count = strcnt(arg1, '.'); - if(dot_count > 0) { - dot_count++; - Array* args = array_new(dot_count, 4, sizeof(char*)); - check_error_OOM(args == NULL, __LINE__); - - char* d_args = strsplit(arg1, '.', strlen(arg1) + 1, -1); - if(d_args == 0) { - fputs("Error: Incorrect -D specified. Use format 'name.sub=value'.", stderr); - return EXIT_FAILURE; - } - - size_t offset = 0; - for(size_t l=0; lsize; i++) - start_protective_mode_file((char*) array_get(libraries, i) + 2, - (tuple_parameters == 1 ? parameters_argv : NULL), - (tuple_parameters == 1 ? parameters : 0)); - array_free(libraries); - } - - - // if there is nothing to do, then exit, as there is nothing left to do - if(no_file == 1) { - lua_close(L); - return EXIT_SUCCESS; - } - - - // load function into protected mode (pcall) - // load parameters late - if(delay_parameters == 0) - load_parameters(parameters_argv, parameters); - int status = start_protective_mode_file(argv[1], - (core_tuple_parameters == 1 ? parameters_argv : NULL), - (core_tuple_parameters == 1 ? parameters : 0));; - - - // free resources - lua_close(L); - - return status; -} - diff --git a/src/consolew.c b/src/consolew.c index 069a3be..81dc74b 100644 --- a/src/consolew.c +++ b/src/consolew.c @@ -28,7 +28,7 @@ # include # include #else -# error "Not familiar. Set up headers accordingly, or -D__linux__ of -Dunix or -D__APPLE__ or -D_WIN32" +# error "OS not familiar. Set up headers accordingly, or -D__linux__ of -Dunix or -D__APPLE__ or -D_WIN32" #endif #include @@ -39,10 +39,6 @@ #include "darr.h" -#if defined(LUACON_ADDITIONS) -# include "additions.h" -#endif - #define LUA_CONSOLE_COPYRIGHT "LuaConsole Copyright MIT (C) 2017 Hydroque\n" @@ -57,23 +53,19 @@ typedef enum LuaConsoleError { // usage message const char HELP_MESSAGE[] = - "Lua Console | Version: 1/5/2017\n" + "Lua Console | Version: 1/6/2017\n" LUA_COPYRIGHT "\n" LUA_CONSOLE_COPYRIGHT "\n" "Supports Lua5.3, Lua5.2, Lua5.1\n" "\n" - "\t- Line by Line interpretation\n" "\t- Files executed by passing\n" "\t- Global variable defintions\n" + "\t- PUC-Lua and LuaJIT compatible\n" "\t- Dynamic module loading\n" - "\t- PUC-Lua compatibility support\n" - #if defined(LUACON_ADDITIONS) - "\t- Working directory support\n" - "\t- Built in stack-dump\n" - "\t- Console clearing\n" - #endif + "\t- Built-in stack-dump\n" + "\t- Line by Line interpretation\n" "\n" "Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-p] [-a] [-c] [-Dvar=val] [-Lfilepath.lua] [-b[a,b,c]] [-?] [-n]{parameter1 ...} \n" "\n" @@ -81,9 +73,6 @@ const char HELP_MESSAGE[] = "-e \t Prevents lua core libraries from loading\n" "-s \t Issues a new root path\n" "-p \t Has console post exist after script in line by line mode\n" - #if defined(LUACON_ADDITIONS) - "-a \t Disables the additions\n" - #endif "-c \t No copyright on init\n" "-d \t Defines a global variable as value after '='\n" "-l \t Executes a module before specified script or post-exist\n" @@ -113,6 +102,44 @@ static inline void check_error_OOM(int cond, int line) { } } +int stack_dump(lua_State *L) { + int i = lua_gettop(L); + printf("--------------- Stack Dump ----------------\n"); + while(i) { + int t = lua_type(L, i); + switch (t) { + case LUA_TSTRING: + fprintf(stdout, "%d:(String):`%s`\n", i, lua_tostring(L, i)); + break; + case LUA_TBOOLEAN: + fprintf(stdout, "%d:(Boolean):%s\n", i, lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TNUMBER: + fprintf(stdout, "%d:(Number):%g\n", i, lua_tonumber(L, i)); + break; + case LUA_TFUNCTION: + fprintf(stdout, "%d:(Function):@%p\n", i, lua_topointer(L, i)); + break; + case LUA_TTABLE: + fprintf(stdout, "%d:(Table):@%p\n", i, lua_topointer(L, i)); + break; + case LUA_TUSERDATA: + fprintf(stdout, "%d:(Userdata):@%p\n", i, lua_topointer(L, i)); + break; + case LUA_TLIGHTUSERDATA: + fprintf(stdout, "%d:(LUserdata):@%p\n", i, lua_topointer(L, i)); + break; + default: + fprintf(stdout, "%d:(Object):%s:@%p\n", i, lua_typename(L, t), lua_topointer(L, i)); + break; + } + i--; + } + printf("----------- Stack Dump Finished -----------\n"); + return 0; +} + + // handles out-of-lua error messages // returns 1 item: @@ -132,10 +159,8 @@ static void print_error(LuaConsoleError error, int offset) { const char* msg = lua_tostring(L, -1); size_t top = lua_gettop(L); fprintf(stderr, " | Stack Top: %zu | %s\n", top - offset, msg); - #if defined(LUACON_ADDITIONS) - if(top - offset > 1) - stack_dump(L); - #endif + if(top - offset > 1) + stack_dump(L); } // handles in-lua runtime error messages @@ -150,10 +175,8 @@ static int lua_print_error(lua_State* L) { size_t top = lua_gettop(L); fprintf(stderr, " (Runtime) | Stack Top: %zu | %s\n %s\n", top, msg, tb); - #if defined(LUACON_ADDITIONS) - if(top > 1) - stack_dump(L); - #endif + if(top > 1) + stack_dump(L); return 1; } @@ -249,7 +272,7 @@ static int start_protective_mode_file(const char* file, char** parameters_argv, } // handle execution of REPL -static int start_protective_mode_REPL() { +static inline int start_protective_mode_REPL() { lua_pushcclosure(L, lua_main_postexist, 0); lua_pcall(L, 0, 0, 0); return 0; @@ -258,7 +281,7 @@ static int start_protective_mode_REPL() { // load parameters into global arg table -static void load_parameters(char** parameters_argv, size_t param_len) { +static inline void load_parameters(char** parameters_argv, size_t param_len) { lua_createtable(L, param_len, 0); for(size_t i=0; i 0) { @@ -514,13 +541,6 @@ int main(int argc, char* argv[]) } - #if defined(LUACON_ADDITIONS) - // add additions - if(no_additions == 0 && (no_file == 0 || post_exist == 1)) - luaopen_additionsdll(L); - #endif - - // load parameters early if(delay_parameters == 1) load_parameters(parameters_argv, parameters); @@ -528,10 +548,28 @@ int main(int argc, char* argv[]) // do passed libraries/modules if(libraries != NULL) { - for(size_t i=0; isize; i++) - start_protective_mode_file((char*) array_get(libraries, i) + 2, + for(size_t i=0; isize; i++) { + char* name = (char*) array_get(libraries, i) + 2; + char* str1 = strsplit(name, '.', strlen(name), 2); + if(str1 != 0) { + char* str2 = strnxt(str1); + if(no_libraries == 0 && (memcmp(str2, "dll", 3) == 0 || memcmp(str2, "so", 2) == 0)) { + lua_getglobal(L, "require"); + lua_pushlstring(L, str1, strlen(str1)); + int status = 0; + if((status = lua_pcall(L, 1, 0, 0)) != 0) { + const char* str = lua_tostring(L, -1); + lua_pop(L, 1); + fprintf(stderr, "%s\n", str); + } + continue; + } + } + start_protective_mode_file(name, (tuple_parameters == 1 ? parameters_argv : NULL), (tuple_parameters == 1 ? parameters : 0)); + free(str1); + } array_free(libraries); }