From 822c2979050009f726888eb07b8b4320e6e81a22 Mon Sep 17 00:00:00 2001 From: Hydroque Date: Sun, 31 Dec 2017 08:07:42 -0500 Subject: [PATCH] Added library definitions for -l, updated and fixed -? dialog, added darr.c and darr.h for dynamic void pointer table, improved switch statement greatly for parameter discection, improved print error - tells you what file errored (if any), ensured code format, swapped parameter setting to add a -b which lets you specify when parameters get loaded into the environment for support with the added -l switch, added -l switch which executes files preemptively (no support for setting result to variables yet), rewrote the code for -D switch (globals) to be more efficient, changed where the copywrite printed --- build.bat | 12 ++-- src/console.c | 173 +++++++++++++++++++++++++++------------------- src/consolew.c | 182 ++++++++++++++++++++++++++++--------------------- 3 files changed, 213 insertions(+), 154 deletions(-) diff --git a/build.bat b/build.bat index ca39d05..20d7941 100644 --- a/build.bat +++ b/build.bat @@ -9,22 +9,22 @@ set resdir=..\res cd src rem Compile everything release w/ additions -gcc -std=gnu99 -Wall -O2 -g0 -DLUACON_ADDITIONS -D__USE_MINGW_ANSI_STDIO=1 -c console.c consolew.c additions.c +gcc -std=gnu99 -Wall -O2 -g0 -DLUACON_ADDITIONS -D__USE_MINGW_ANSI_STDIO=1 -c console.c consolew.c additions.c darr.c rem Link luaw.exe -gcc -std=gnu99 -s -Wall -O2 -g0 -o lua_add.exe console.o additions.o -llua +gcc -std=gnu99 -s -Wall -O2 -g0 -o lua_add.exe console.o additions.o darr.o -llua rem Link lua.exe -gcc -std=gnu99 -s -Wall -O2 -g0 -o luaw_add.exe consolew.o additions.o -llua +gcc -std=gnu99 -s -Wall -O2 -g0 -o luaw_add.exe consolew.o additions.o darr.o -llua rem Compile everything release w/o additions -gcc -std=gnu99 -Wall -O2 -g0 -D__USE_MINGW_ANSI_STDIO=1 -c console.c consolew.c +gcc -std=gnu99 -Wall -O2 -g0 -D__USE_MINGW_ANSI_STDIO=1 -c console.c consolew.c darr.c rem Link luaw.exe -gcc -std=gnu99 -s -Wall -O2 -g0 -o lua.exe console.o -llua +gcc -std=gnu99 -s -Wall -O2 -g0 -o lua.exe console.o darr.o -llua rem Link lua.exe -gcc -std=gnu99 -s -Wall -O2 -g0 -o luaw.exe consolew.o -llua +gcc -std=gnu99 -s -Wall -O2 -g0 -o luaw.exe consolew.o darr.o -llua if EXIST %root% ( rmdir /S /Q %root% ) mkdir %root% diff --git a/src/console.c b/src/console.c index 4ea5173..035fd7f 100644 --- a/src/console.c +++ b/src/console.c @@ -16,9 +16,14 @@ #define PRIMARY_BUFFER_SIZE (1024 + 1) #define SECONDARY_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 @@ -46,6 +51,8 @@ #include "lualib.h" #include "lauxlib.h" +#include "darr.h" + #if defined(LUACON_ADDITIONS) # include "additions.h" #endif @@ -64,7 +71,7 @@ typedef enum LuaConsoleError { // usage message const char HELP_MESSAGE[] = - "Lua Console | Version: 10/02/2017\n" + "Lua Console | Version: 12/31/2017\n" LUA_COPYRIGHT "\n" LUA_CONSOLE_COPYRIGHT @@ -74,28 +81,33 @@ const char HELP_MESSAGE[] = "\n" "\t- Files executed by passing\n" "\t- Global variable defintions\n" + "\t- Dynamic module loading\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] [-?] [-n]{parameter1 ...} \n" + "Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-p] [-a] [-c] [-Dvar=val] [-Lfilepath.lua] [-b] [-?] [-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 \t Load specified parameters by -n before -l modules execute\n" "-n \t Start of parameter section\n" "-? \t Displays this help message\n"; // one environment per process -static lua_State* L; +static lua_State* L = 0; // necessary to put this outside of main, print doesn't work @@ -105,7 +117,7 @@ static int no_libraries = 0; // comprehensive error output -static void print_error(LuaConsoleError error) { +static void print_error(LuaConsoleError error, const char* file) { const char* msg = lua_tostring(L, 1); switch(error) { case INTERNAL_ERROR: @@ -119,7 +131,7 @@ static void print_error(LuaConsoleError error) { break; } size_t top = lua_gettop(L); - fprintf(stderr, " | Stack Top: %zu | %s\n", top, msg); + fprintf(stderr, " | Stack Top: %zu | %s | %s\n", top, file, msg); #if defined(LUACON_ADDITIONS) if(top > 1) // other than error message stack_dump(L); @@ -133,35 +145,35 @@ static int lua_main_dofile(lua_State* L) { const char* file = lua_tostring(L, 1); lua_pop(L, 1); if(luaL_loadfile(L, file) != 0) { - print_error(SYNTAX_ERROR); + print_error(SYNTAX_ERROR, file); return 0; } if(lua_pcall(L, 0, 0, 0) != 0) { - print_error(RUNTIME_ERROR); + print_error(RUNTIME_ERROR, file); return 0; } return 0; } +void lua_load_parameters(char** parameters_argv, size_t param_len) { + lua_createtable(L, param_len, 0); + size_t i; + for(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; size_t i; - for (i=1; i<(size_t)argc; i++) { + for(i=1; i<(size_t)argc; i++) { // if we have args around, break if(parameters_argv != 0) break; @@ -263,24 +279,17 @@ int main(int argc, char* argv[]) copyright_squelch = 1; break; case 'd': case 'D': - if(globals_argv == 0) { // reserve default space for globals - globals_argv = malloc(DEFINES_INIT * sizeof(char*)); - if(globals_argv == 0) { - fputs("Error: Out of memory.", stderr); - return EXIT_FAILURE; - } - globals_argv_len = DEFINES_INIT; - } - if(globals == globals_argv_len) { // expand default space if needed - globals_argv_len += DEFINES_EXPANSION; - globals_argv = realloc(globals_argv, (globals_argv_len + DEFINES_EXPANSION) * sizeof(char*)); - if(globals_argv == 0) { - fputs("Error: Out of memory.", stderr); - return EXIT_FAILURE; - } - } - globals_argv[globals] = argv[i]; - globals++; + if(globals == 0) + globals = array_new(DEFINES_INIT, DEFINES_EXPANSION, sizeof(char*)); + array_push(globals, argv[i]); + break; + case 'l': case 'L': + if(libraries == 0) + libraries = array_new(LIBRARIES_INIT, LIBRARIES_EXPANSION, sizeof(char*)); + array_push(libraries, argv[i]); + break; + case 'b': case 'B': + delay_parameters = 1; break; case 'n': case 'N': parameters = (argc - i) - 1; @@ -312,10 +321,10 @@ int main(int argc, char* argv[]) // initiate global variables set up if(globals != 0) { - for (size_t i=0; isize; i++) { + char* str = (char*) array_get(globals, i) + 2; - char* m_args = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 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; @@ -323,16 +332,14 @@ int main(int argc, char* argv[]) char* arg1 = m_args; char* arg2 = arg1 + (strlen(arg1) + 1); - // check for .'s to nest in table size_t dot_count = strcnt(arg1, '.'); - if(dot_count != 0) { - dot_count++; // counts number of .'s, not splits - char** args = malloc(dot_count * sizeof(char*)); + if(dot_count > 0) { + dot_count++; + Array* args = array_new(dot_count, 4, sizeof(char*)); if(args == 0) { fputs("Error: Out of memory.", stderr); return EXIT_FAILURE; } - char* d_args = strsplit(arg1, '.', strlen(arg1) + 1, -1); if(d_args == 0) { fputs("Error: Incorrect -D specified. Use format 'name.sub=value'.", stderr); @@ -340,44 +347,43 @@ int main(int argc, char* argv[]) } size_t offset = 0; - for (size_t l=0; lsize; i++) { + const char* str = (char*) array_get(libraries, i) + 2; + if(luaL_loadfile(L, str) != 0) { + print_error(SYNTAX_ERROR, str); + continue; + } + if(lua_pcall(L, 0, 0, 0) != 0) { + print_error(RUNTIME_ERROR, str); + continue; + } + } + 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) + lua_load_parameters(parameters_argv, parameters); int status = start_protective_mode(&lua_main_dofile, argv[1], parameters_argv, parameters); diff --git a/src/consolew.c b/src/consolew.c index a2b5500..febad56 100644 --- a/src/consolew.c +++ b/src/consolew.c @@ -16,9 +16,14 @@ #define PRIMARY_BUFFER_SIZE (1024 + 1) #define SECONDARY_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 @@ -46,6 +51,8 @@ #include "lualib.h" #include "lauxlib.h" +#include "darr.h" + #if defined(LUACON_ADDITIONS) # include "additions.h" #endif @@ -64,7 +71,7 @@ typedef enum LuaConsoleError { // usage message const char HELP_MESSAGE[] = - "Lua Console | Version: 10/02/2017\n" + "Lua Console | Version: 12/31/2017\n" LUA_COPYRIGHT "\n" LUA_CONSOLE_COPYRIGHT @@ -75,12 +82,14 @@ const char HELP_MESSAGE[] = "\t- Line by Line interpretation\n" "\t- Files executed by passing\n" "\t- Global variable defintions\n" + "\t- Dynamic module loading\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] [-?] [-n]{parameter1 ...} \n" + "Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-p] [-a] [-c] [-Dvar=val] [-Lfilepath.lua] [-b] [-?] [-n]{parameter1 ...} \n" "\n" "-v \t Prints the Lua version in use\n" "-e \t Prevents lua core libraries from loading\n" @@ -91,13 +100,15 @@ const char HELP_MESSAGE[] = #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" + "-b \t Load specified parameters by -n before -l modules execute\n" "-n \t Start of parameter section\n" "-? \t Displays this help message\n"; // one environment per process -static lua_State* L; +static lua_State* L = 0; // variable to end line by line iterpretation loop, for adaption @@ -110,7 +121,7 @@ static int no_libraries = 0; // comprehensive error output -static void print_error(LuaConsoleError error) { +static void print_error(LuaConsoleError error, const char* file) { const char* msg = lua_tostring(L, 1); switch(error) { case INTERNAL_ERROR: @@ -124,7 +135,7 @@ static void print_error(LuaConsoleError error) { break; } size_t top = lua_gettop(L); - fprintf(stderr, " | Stack Top: %zu | %s\n", top, msg); + fprintf(stderr, " | Stack Top: %zu | %s | %s\n", top, file, msg); #if defined(LUACON_ADDITIONS) if(top > 1) // other than error message stack_dump(L); @@ -186,14 +197,14 @@ static int lua_main_postexist(lua_State* L) { // load originally inserted code status = luaL_loadstring(L, buffer); if(status != 0) { - print_error(SYNTAX_ERROR); + print_error(SYNTAX_ERROR, "TTY"); continue; } // attempt originally inserted code status = lua_pcall(L, 0, 0, 0); if(status != 0) { - print_error(RUNTIME_ERROR); + print_error(RUNTIME_ERROR, "TTY"); } } @@ -208,31 +219,31 @@ static int lua_main_dofile(lua_State* L) { const char* file = lua_tostring(L, 1); lua_pop(L, 1); if(luaL_loadfile(L, file) != 0) { - print_error(SYNTAX_ERROR); + print_error(SYNTAX_ERROR, file); return 0; } if(lua_pcall(L, 0, 0, 0) != 0) { - print_error(RUNTIME_ERROR); + print_error(RUNTIME_ERROR, file); return 0; } return 0; } +void lua_load_parameters(char** parameters_argv, size_t param_len) { + lua_createtable(L, param_len, 0); + size_t i; + for(i=0; i 1) @@ -312,7 +325,7 @@ int main(int argc, char* argv[]) if(argv[1][0] == '-') no_file = 1; size_t i; - for (i=1; i<(size_t)argc; i++) { + for(i=1; i<(size_t)argc; i++) { // if we have args around, break if(parameters_argv != 0) break; @@ -348,24 +361,17 @@ int main(int argc, char* argv[]) copyright_squelch = 1; break; case 'd': case 'D': - if(globals_argv == 0) { // reserve default space for globals - globals_argv = malloc(DEFINES_INIT * sizeof(char*)); - if(globals_argv == 0) { - fputs("Error: Out of memory.", stderr); - return EXIT_FAILURE; - } - globals_argv_len = DEFINES_INIT; - } - if(globals == globals_argv_len) { // expand default space if needed - globals_argv_len += DEFINES_EXPANSION; - globals_argv = realloc(globals_argv, (globals_argv_len + DEFINES_EXPANSION) * sizeof(char*)); - if(globals_argv == 0) { - fputs("Error: Out of memory.", stderr); - return EXIT_FAILURE; - } - } - globals_argv[globals] = argv[i]; - globals++; + if(globals == 0) + globals = array_new(DEFINES_INIT, DEFINES_EXPANSION, sizeof(char*)); + array_push(globals, argv[i]); + break; + case 'l': case 'L': + if(libraries == 0) + libraries = array_new(LIBRARIES_INIT, LIBRARIES_EXPANSION, sizeof(char*)); + array_push(libraries, argv[i]); + break; + case 'b': case 'B': + delay_parameters = 1; break; case 'n': case 'N': parameters = (argc - i) - 1; @@ -397,10 +403,10 @@ int main(int argc, char* argv[]) // initiate global variables set up if(globals != 0) { - for (size_t i=0; isize; i++) { + char* str = (char*) array_get(globals, i) + 2; - char* m_args = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 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; @@ -408,16 +414,14 @@ int main(int argc, char* argv[]) char* arg1 = m_args; char* arg2 = arg1 + (strlen(arg1) + 1); - // check for .'s to nest in table size_t dot_count = strcnt(arg1, '.'); - if(dot_count != 0) { - dot_count++; // counts number of .'s, not splits - char** args = malloc(dot_count * sizeof(char*)); + if(dot_count > 0) { + dot_count++; + Array* args = array_new(dot_count, 4, sizeof(char*)); if(args == 0) { fputs("Error: Out of memory.", stderr); return EXIT_FAILURE; } - char* d_args = strsplit(arg1, '.', strlen(arg1) + 1, -1); if(d_args == 0) { fputs("Error: Incorrect -D specified. Use format 'name.sub=value'.", stderr); @@ -425,44 +429,43 @@ int main(int argc, char* argv[]) } size_t offset = 0; - for (size_t l=0; lsize; i++) { + const char* str = (char*) array_get(libraries, i) + 2; + if(luaL_loadfile(L, str) != 0) { + print_error(SYNTAX_ERROR, str); + continue; + } + if(lua_pcall(L, 0, 0, 0) != 0) { + print_error(RUNTIME_ERROR, str); + continue; + } + } + array_free(libraries); + } + + // if there is nothing to do, then exit, as there is nothing left to do if(no_file == 1 && post_exist != 1) { lua_close(L); @@ -503,20 +535,16 @@ int main(int argc, char* argv[]) } - #if defined(LUACON_ADDITIONS) - // add additions - if(no_additions == 0) - additions_add(L); - #endif - - // load function into protected mode (pcall) + // load parameters late + if(delay_parameters == 0) + lua_load_parameters(parameters_argv, parameters); int status = 0; if(post_exist == 1) { if(no_file == 0) - status = start_protective_mode(&lua_main_dofile, argv[1], parameters_argv, parameters); - status = start_protective_mode(&lua_main_postexist, NULL, parameters_argv, parameters); - } else status = start_protective_mode(&lua_main_dofile, argv[1], parameters_argv, parameters); + status = start_protective_mode(&lua_main_dofile, argv[1]); + status = start_protective_mode(&lua_main_postexist, NULL); + } else status = start_protective_mode(&lua_main_dofile, argv[1]); // free resources