From c2d481c2bde92a0bb6ecf009f7344604fd397c0c Mon Sep 17 00:00:00 2001 From: Polite Kiwi Date: Sun, 2 Sep 2018 05:21:53 -0400 Subject: [PATCH] Major updates... - Increased REPL_BUFFER_SIZE to 2048 just incase you paste dump ... zzz - Updated comments to be clear on it - Also tabbed it out to look nice - Added a TODO: for preperation to add LUA_INIT by lua version (not imp) - Replaced standard imports - Now uses one single if chunk of includes for unix - Removed outside the chunk cross-platform libs - Corrected libraries so that it builds with MSVS - Now uses posix compliant MSVS suggested names for functions - Removed error for not finding your platform zzz - Updated comments galore - Made lua includes include just the headers rather than enforce base folder name - Inserted a hack to fix a bug that lua51 doesn't have luaL_traceback - Fixed command help output to better display commands more compactly - Also purdyfied LuaJIT only commands - Updated date too - Fixed area where jitsupport.h was included - Swapped a lot of size_t to int because lua_gettop(L) needs int duh - Removed false else that would reach when you inputed a command - There was a ghost-like crashing bug that didn't allow me to reach it - Downcasted size_t to int when appropriate, there is no difference - The other change in tandem with this is because MSVS is a whiney botantic piece of cracked pepper - Added win32 MSVS specific environment variable getenv() because it whined, ugly held together by preprocessor... needs to be free too... so that is too... - Copyright no loner pops up when not running tty or post-existing --- src/consolew.c | 155 +++++++++++++++++++++---------------------------- 1 file changed, 66 insertions(+), 89 deletions(-) diff --git a/src/consolew.c b/src/consolew.c index 84e5fd3..5a6c87f 100644 --- a/src/consolew.c +++ b/src/consolew.c @@ -22,10 +22,9 @@ */ // REPL line buffer length -#define REPL_BUFFER_SIZE (1024) -#define PRIMARY_REPL_BUFFER_SIZE (REPL_BUFFER_SIZE + 1) -#define SECONDARY_REPL_BUFFER_SIZE (PRIMARY_REPL_BUFFER_SIZE + 8) // + 8 is for `return ;` - +#define REPL_BUFFER_SIZE (2048) // REPL input buffer size +#define PRIMARY_REPL_BUFFER_SIZE (REPL_BUFFER_SIZE + 1) // + 1 is for `\0` +#define SECONDARY_REPL_BUFFER_SIZE (PRIMARY_REPL_BUFFER_SIZE + 8) // + 8 is for `return ;` // dynamic array initialization sizes for darr's #define DEFINES_INIT (4) @@ -41,77 +40,47 @@ #define DO_EXT_ERROR_RETS (0) // environment variable for lua usage +// TODO: I want to support LUA_INIT_5_2 LUA_INIT_5_1 and LUA_INIT_5_3 (ENV_VAR_EXT) which version takes precedence and falls back to LUA_INIT afterward #define ENV_VAR "LUA_INIT" +// #define ENV_VAR_EXT (0) + // standard libraries per OS -#if defined(linux) || defined(__linux__) || defined(__linux) -# include -# include -# include -# define IS_ATTY isatty(fileno(stdin)) -# define LUA_BIN_EXT_NAME "" -# define LUA_DLL_SO_NAME ".so" -#elif defined(unix) || defined(__unix__) || defined(__unix) -# include -# include -# include -# define IS_ATTY isatty(fileno(stdin)) -# define LUA_BIN_EXT_NAME "" -# define LUA_DLL_SO_NAME ".so" -#elif defined(__APPLE__) || defined(__MACH__) -# include -# include -# include -# define IS_ATTY isatty(fileno(stdin)) -# define LUA_BIN_EXT_NAME "" -# define LUA_DLL_SO_NAME ".so" -#elif defined(_WIN32) || defined(_WIN64) +#if defined(_WIN32) || defined(_WIN64) +# define WIN32_LEAN_AND_MEAN # include -# include -# include -# define IS_ATTY _isatty(_fileno(stdin)) +# include +# include +# define IS_ATTY _isatty(_fileno(stdin)) # define LUA_BIN_EXT_NAME ".exe" # define LUA_DLL_SO_NAME ".dll" #else -# error "OS not familiar. Set up headers accordingly, or -D__linux__ or -Dunix or -D__APPLE__ or -D_WIN32" +# include +# include +# define IS_ATTY isatty(fileno(stdin)) +# define LUA_BIN_EXT_NAME "" +# define LUA_DLL_SO_NAME ".so" #endif -// stdlib includes -#include +// 'cross-platform' includes +#include +#include #include #include #include - -// compiler/project includes -// Please migrate your lua h's to a proper directory so versions don't collide +// project includes +// Please migrate your lua h's to a proper local directory so versions don't collide // luajit `make install` puts them in /usr/local/include/luajit-X.X/* // lua51/52/53 `make install` puts them in /usr/local/include/* with version collision -#if defined(LUA_51) -# include "lua51/lua.h" -# include "lua51/lualib.h" -# include "lua51/lauxlib.h" -#elif defined(LUA_52) -# include "lua52/lua.h" -# include "lua52/lualib.h" -# include "lua52/lauxlib.h" -#elif defined(LUA_53) -# include "lua53/lua.h" -# include "lua53/lualib.h" -# include "lua53/lauxlib.h" -#elif defined(LUA_JIT_51) -# include "luajit-2.0/lua.h" -# include "luajit-2.0/lualib.h" -# include "luajit-2.0/lauxlib.h" -# include "luajit-2.0/luajit.h" +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +#if defined(LUA_JIT_51) # include "jitsupport.h" -// TODO: support latest version of luajit -#else -# warning "Lua version not defined." -# error "Define the version to use. '-DLUA_53' '-DLUA_52' '-DLUA_51' '-DLUA_JIT_51'" #endif @@ -119,6 +88,14 @@ #include "darr.h" +// TODO: This is a hack bc I didn't realize lua51 didn't have traceback +#if LUA_VERSION_NUM <= 501 + void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { + lua_pushlstring(L, " ", 1); + } +#endif + + // copyright information #define LUA_CONSOLE_COPYRIGHT "LuaConsole Copyright (C) 2017-2018, Cody Tilkins" @@ -127,7 +104,7 @@ // usage message const char HELP_MESSAGE[] = - "LuaConsole | Version: 8/21/2018\n\n" + "LuaConsole | Version: 9/2/2018\n\n" LUA_VERSION " " LUA_COPYRIGHT "\n" LUA_CONSOLE_COPYRIGHT @@ -142,7 +119,7 @@ const char HELP_MESSAGE[] = "\t[-Dtb.var=val] [-Lfile.lua] [-Llualib" LUA_DLL_SO_NAME "] [-t{a,b}] [-r \"string\"]\n" "\t[-R \"string\"] " #if defined(LUA_JIT_51) - "[-j{cmd,cmd=arg},...]\n\t[-O{level,+flag,-flag,cmd=arg}] [-b{l,s,g,n,t,a,o,e,-} {IN,OUT}]\n\t" + "[-j{cmd,cmd=arg},...] [-O{level,+flag,-flag,cmd=arg}]\n\t[-b{l,s,g,n,t,a,o,e,-} {IN,OUT}] " #endif "[-?] [-n {arg1 ...}]\n" "\n" @@ -161,19 +138,15 @@ const char HELP_MESSAGE[] = "- \t\tProcesses input from stdin\n" "-- \t\tStops processing parameters\n" #if defined(LUA_JIT_51) - "-j \t\t LuaJIT Performs a control command loads an extension module\n" - "-O \t\t LuaJIT Sets an optimization level/parameters\n" - "-b \t\t LuaJIT Saves or lists bytecode\n" + "-j \t\t [LuaJIT] Performs a control command loads an extension module\n" + "-O \t\t [LuaJIT] Sets an optimization level/parameters\n" + "-b \t\t [LuaJIT] Saves or lists bytecode\n" #endif "-? --help \tDisplays this help message\n" "-n \t\tStart of parameter section\n"; -#if defined(LUA_JIT_51) - #include "jitsupport.h" -#endif - // struct for args to be seen across functions @@ -319,7 +292,7 @@ int stack_dump(lua_State *L) { } -// internal enums, represent lua error category +// internal enums, represents lua error category typedef enum LuaConsoleError { INTERNAL_ERROR = 0, SYNTAX_ERROR = 1, @@ -443,7 +416,7 @@ static int lua_main_postexist(lua_State* L) { lua_pop(L, 1); // err msg } else { // attempt first test, return seems to work - size_t top = lua_gettop(L); + int top = lua_gettop(L); status = lua_pcall(L, 0, LUA_MULTRET, 0); if(status == 0) { // on success top = lua_gettop(L) - top + 1; // + 1 = ignore pushed function @@ -466,14 +439,8 @@ static int lua_main_postexist(lua_State* L) { print_error(SYNTAX_ERROR, 1); lua_pop(L, 2); // err msg, err handler } else { - if((status = lua_pcall(L, 0, LUA_MULTRET, base)) != 0) { + if((status = lua_pcall(L, 0, LUA_MULTRET, base)) != 0) lua_pop(L, 2); // err msg, err handler, also ignore it - no relevance - } else { - // code can't be `return %s;`'d and it doesn't syntax out, but it still works - // by design, it always returns even nil so idk if this is even possible - // in practice, I can't figure out how to trigger this else clause, so undefined behavior? - fprintf(stderr, "Please submit a github issue with this command attached."); - } } } @@ -482,15 +449,15 @@ static int lua_main_postexist(lua_State* L) { -// append parameters to the stack for a (p)call to consume +// append parameters to the stack for a pcall to consume static inline void inject_parameters() { for(size_t i=0; i 0) inject_parameters(); - if((status = lua_pcall(L, params, LUA_MULTRET, base)) != 0) { + if((status = lua_pcall(L, (int)params, LUA_MULTRET, base)) != 0) { lua_pop(L, 2); // err msg, err handler return status; } @@ -547,7 +514,7 @@ static int start_protective_mode_string(const char* str, size_t params) { return status; } -// handle execution of files +// handles execution of files static int start_protective_mode_file(const char* file, size_t params) { signal(SIGINT, SIG_IGN); // Ignore for now @@ -561,7 +528,7 @@ static int start_protective_mode_file(const char* file, size_t params) { } if(params > 0) inject_parameters(); - if((status = lua_pcall(L, params, 0, base)) != 0) { + if((status = lua_pcall(L, (int)params, 0, base)) != 0) { lua_pop(L, 2); // err msg, err handler return status; } @@ -569,7 +536,7 @@ static int start_protective_mode_file(const char* file, size_t params) { return status; } -// handle execution of anything to be required +// handles execution of anything to be required static inline int start_protective_mode_require(const char* file) { signal(SIGINT, SIG_IGN); // Ignore for now @@ -626,7 +593,7 @@ static inline void load_globals(Array* globals, void* data) { } lua_pushlstring(L, arg2, strlen(arg2)); lua_setfield(L, -2, strnxt(cur_arg)); - lua_pop(L, dot_count-1); // everything but root table + lua_pop(L, (int)dot_count - 1); // everything but root table lua_setglobal(L, tabs); free(tabs); } @@ -809,12 +776,21 @@ int main(int argc, char* argv[]) // handle init environment variable if(ARGS.no_env_var == 0) { - const char* env_init = getenv(ENV_VAR); + #if defined(_MSC_VER) + char* env_init; + size_t len; + errno_t err = _dupenv_s(&env_init, &len, ENV_VAR); + #else + char* env_init = getenv(ENV_VAR); + #endif if(env_init != NULL) { if(env_init[0] == '@') start_protective_mode_file(env_init + 1, 0); else start_protective_mode_string(env_init, 0); } + #if defined(_MSC_VER) + free(env_init); + #endif } else { lua_pushboolean(L, 1); lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); @@ -823,7 +799,7 @@ int main(int argc, char* argv[]) // copyright - if(ARGS.copyright_squelch == 0) { + if(ARGS.copyright_squelch == 0 && ARGS.post_exist == 1) { fputs(LUA_VERSION " " LUA_COPYRIGHT "\n", stdout); fputs(LUA_CONSOLE_COPYRIGHT "\n", stdout); #if defined(LUA_JIT_51) @@ -880,7 +856,7 @@ int main(int argc, char* argv[]) } // make sure to start in the requested directory, if any - check_error(ARGS.start != NULL && chdir(ARGS.start) == -1, "Error: Invalid start directory supplied."); + check_error(ARGS.start != NULL && _chdir(ARGS.start) == -1, "Error: Invalid start directory supplied."); // initiate global variables set up @@ -922,6 +898,7 @@ int main(int argc, char* argv[]) if(ARGS.delay_parameters == 1) load_parameters(); + // files if(ARGS.no_file == 0) { for(size_t i=0; i