2222 */
2323
2424// REPL line buffer length
25- #define REPL_BUFFER_SIZE (1024)
26- #define PRIMARY_REPL_BUFFER_SIZE (REPL_BUFFER_SIZE + 1)
27- #define SECONDARY_REPL_BUFFER_SIZE (PRIMARY_REPL_BUFFER_SIZE + 8) // + 8 is for `return ;`
28-
25+ #define REPL_BUFFER_SIZE (2048) // REPL input buffer size
26+ #define PRIMARY_REPL_BUFFER_SIZE (REPL_BUFFER_SIZE + 1) // + 1 is for `\0`
27+ #define SECONDARY_REPL_BUFFER_SIZE (PRIMARY_REPL_BUFFER_SIZE + 8) // + 8 is for `return ;`
2928
3029// dynamic array initialization sizes for darr's
3130#define DEFINES_INIT (4)
4140#define DO_EXT_ERROR_RETS (0)
4241
4342// environment variable for lua usage
43+ // 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
4444#define ENV_VAR "LUA_INIT"
45+ // #define ENV_VAR_EXT (0)
46+
4547
4648
4749// standard libraries per OS
48- #if defined(linux ) || defined(__linux__ ) || defined(__linux )
49- # include <unistd.h>
50- # include <stdio.h>
51- # include <stdlib.h>
52- # define IS_ATTY isatty(fileno(stdin))
53- # define LUA_BIN_EXT_NAME ""
54- # define LUA_DLL_SO_NAME ".so"
55- #elif defined(unix ) || defined(__unix__ ) || defined(__unix )
56- # include <unistd.h>
57- # include <stdio.h>
58- # include <stdlib.h>
59- # define IS_ATTY isatty(fileno(stdin))
60- # define LUA_BIN_EXT_NAME ""
61- # define LUA_DLL_SO_NAME ".so"
62- #elif defined(__APPLE__ ) || defined(__MACH__ )
63- # include <unistd.h>
64- # include <stdio.h>
65- # include <stdlib.h>
66- # define IS_ATTY isatty(fileno(stdin))
67- # define LUA_BIN_EXT_NAME ""
68- # define LUA_DLL_SO_NAME ".so"
69- #elif defined(_WIN32 ) || defined(_WIN64 )
50+ #if defined(_WIN32 ) || defined(_WIN64 )
51+ # define WIN32_LEAN_AND_MEAN
7052# include <windows.h>
71- # include <stdio .h>
72- # include <stdlib .h>
73- # define IS_ATTY _isatty(_fileno(stdin))
53+ # include <direct .h>
54+ # include <io .h>
55+ # define IS_ATTY _isatty(_fileno(stdin))
7456# define LUA_BIN_EXT_NAME ".exe"
7557# define LUA_DLL_SO_NAME ".dll"
7658#else
77- # error "OS not familiar. Set up headers accordingly, or -D__linux__ or -Dunix or -D__APPLE__ or -D_WIN32"
59+ # include <unistd.h>
60+ # include <dirent.h>
61+ # define IS_ATTY isatty(fileno(stdin))
62+ # define LUA_BIN_EXT_NAME ""
63+ # define LUA_DLL_SO_NAME ".so"
7864#endif
7965
8066
81- // stdlib includes
82- #include <dirent.h>
67+ // 'cross-platform' includes
68+ #include <stdio.h>
69+ #include <stdlib.h>
8370#include <string.h>
8471#include <signal.h>
8572#include <fcntl.h>
8673
8774
88-
89- // compiler/project includes
90- // Please migrate your lua h's to a proper directory so versions don't collide
75+ // project includes
76+ // Please migrate your lua h's to a proper local directory so versions don't collide
9177// luajit `make install` puts them in /usr/local/include/luajit-X.X/*
9278// lua51/52/53 `make install` puts them in /usr/local/include/* with version collision
93- #if defined(LUA_51 )
94- # include "lua51/lua.h"
95- # include "lua51/lualib.h"
96- # include "lua51/lauxlib.h"
97- #elif defined(LUA_52 )
98- # include "lua52/lua.h"
99- # include "lua52/lualib.h"
100- # include "lua52/lauxlib.h"
101- #elif defined(LUA_53 )
102- # include "lua53/lua.h"
103- # include "lua53/lualib.h"
104- # include "lua53/lauxlib.h"
105- #elif defined(LUA_JIT_51 )
106- # include "luajit-2.0/lua.h"
107- # include "luajit-2.0/lualib.h"
108- # include "luajit-2.0/lauxlib.h"
109- # include "luajit-2.0/luajit.h"
79+ #include "lua.h"
80+ #include "lualib.h"
81+ #include "lauxlib.h"
82+ #if defined(LUA_JIT_51 )
11083# include "jitsupport.h"
111- // TODO: support latest version of luajit
112- #else
113- # warning "Lua version not defined."
114- # error "Define the version to use. '-DLUA_53' '-DLUA_52' '-DLUA_51' '-DLUA_JIT_51'"
11584#endif
11685
11786
11887// local src includes
11988#include "darr.h"
12089
12190
91+ // TODO: This is a hack bc I didn't realize lua51 didn't have traceback
92+ #if LUA_VERSION_NUM <= 501
93+ void luaL_traceback (lua_State * L , lua_State * L1 , const char * msg , int level ) {
94+ lua_pushlstring (L , " " , 1 );
95+ }
96+ #endif
97+
98+
12299
123100// copyright information
124101#define LUA_CONSOLE_COPYRIGHT "LuaConsole Copyright (C) 2017-2018, Cody Tilkins"
127104
128105// usage message
129106const char HELP_MESSAGE [] =
130- "LuaConsole | Version: 8/21 /2018\n\n"
107+ "LuaConsole | Version: 9/2 /2018\n\n"
131108 LUA_VERSION " " LUA_COPYRIGHT
132109 "\n"
133110 LUA_CONSOLE_COPYRIGHT
@@ -142,7 +119,7 @@ const char HELP_MESSAGE[] =
142119 "\t[-Dtb.var=val] [-Lfile.lua] [-Llualib" LUA_DLL_SO_NAME "] [-t{a,b}] [-r \"string\"]\n"
143120 "\t[-R \"string\"] "
144121 #if defined(LUA_JIT_51 )
145- "[-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 "
122+ "[-j{cmd,cmd=arg},...] [-O{level,+flag,-flag,cmd=arg}]\n\t [-b{l,s,g,n,t,a,o,e,-} {IN,OUT}] "
146123 #endif
147124 "[-?] [-n {arg1 ...}]\n"
148125 "\n"
@@ -161,19 +138,15 @@ const char HELP_MESSAGE[] =
161138 "- \t\tProcesses input from stdin\n"
162139 "-- \t\tStops processing parameters\n"
163140 #if defined(LUA_JIT_51 )
164- "-j \t\t LuaJIT Performs a control command loads an extension module\n"
165- "-O \t\t LuaJIT Sets an optimization level/parameters\n"
166- "-b \t\t LuaJIT Saves or lists bytecode\n"
141+ "-j \t\t [ LuaJIT] Performs a control command loads an extension module\n"
142+ "-O \t\t [ LuaJIT] Sets an optimization level/parameters\n"
143+ "-b \t\t [ LuaJIT] Saves or lists bytecode\n"
167144 #endif
168145 "-? --help \tDisplays this help message\n"
169146 "-n \t\tStart of parameter section\n" ;
170147
171148
172149
173- #if defined(LUA_JIT_51 )
174- #include "jitsupport.h"
175- #endif
176-
177150
178151
179152// struct for args to be seen across functions
@@ -319,7 +292,7 @@ int stack_dump(lua_State *L) {
319292}
320293
321294
322- // internal enums, represent lua error category
295+ // internal enums, represents lua error category
323296typedef enum LuaConsoleError {
324297 INTERNAL_ERROR = 0 ,
325298 SYNTAX_ERROR = 1 ,
@@ -443,7 +416,7 @@ static int lua_main_postexist(lua_State* L) {
443416 lua_pop (L , 1 ); // err msg
444417 } else {
445418 // attempt first test, return seems to work
446- size_t top = lua_gettop (L );
419+ int top = lua_gettop (L );
447420 status = lua_pcall (L , 0 , LUA_MULTRET , 0 );
448421 if (status == 0 ) { // on success
449422 top = lua_gettop (L ) - top + 1 ; // + 1 = ignore pushed function
@@ -466,14 +439,8 @@ static int lua_main_postexist(lua_State* L) {
466439 print_error (SYNTAX_ERROR , 1 );
467440 lua_pop (L , 2 ); // err msg, err handler
468441 } else {
469- if ((status = lua_pcall (L , 0 , LUA_MULTRET , base )) != 0 ) {
442+ if ((status = lua_pcall (L , 0 , LUA_MULTRET , base )) != 0 )
470443 lua_pop (L , 2 ); // err msg, err handler, also ignore it - no relevance
471- } else {
472- // code can't be `return %s;`'d and it doesn't syntax out, but it still works
473- // by design, it always returns even nil so idk if this is even possible
474- // in practice, I can't figure out how to trigger this else clause, so undefined behavior?
475- fprintf (stderr , "Please submit a github issue with this command attached." );
476- }
477444 }
478445 }
479446
@@ -482,15 +449,15 @@ static int lua_main_postexist(lua_State* L) {
482449
483450
484451
485- // append parameters to the stack for a (p)call to consume
452+ // append parameters to the stack for a pcall to consume
486453static inline void inject_parameters () {
487454 for (size_t i = 0 ; i < ARGS .parameters ; i ++ )
488455 lua_pushlstring (L , ARGS .parameters_argv [i ], strlen (ARGS .parameters_argv [i ]));
489456}
490457
491- // load parameters into global arg table
458+ // load parameters into global ` arg` table
492459static inline void load_parameters () {
493- lua_createtable (L , 0 , ARGS .parameters );
460+ lua_createtable (L , 0 , ( int ) ARGS .parameters );
494461 for (size_t i = 0 ; i < ARGS .parameters ; i ++ ) {
495462 lua_pushinteger (L , i + 1 );
496463 lua_pushlstring (L , ARGS .parameters_argv [i ], strlen (ARGS .parameters_argv [i ]));
@@ -500,7 +467,7 @@ static inline void load_parameters() {
500467}
501468
502469
503- // handle execution of REPL
470+ // handles execution of REPL
504471static inline int start_protective_mode_REPL () {
505472 signal (SIGINT , SIG_IGN ); // SIGINT handled in lua_main_postexist
506473
@@ -516,7 +483,7 @@ static inline int start_protective_mode_REPL() {
516483 return status ;
517484}
518485
519- // handle execution of strings
486+ // handles execution of strings
520487static int start_protective_mode_string (const char * str , size_t params ) {
521488 signal (SIGINT , SIG_IGN ); // Ignore for now
522489
@@ -530,7 +497,7 @@ static int start_protective_mode_string(const char* str, size_t params) {
530497 }
531498 if (params > 0 )
532499 inject_parameters ();
533- if ((status = lua_pcall (L , params , LUA_MULTRET , base )) != 0 ) {
500+ if ((status = lua_pcall (L , ( int ) params , LUA_MULTRET , base )) != 0 ) {
534501 lua_pop (L , 2 ); // err msg, err handler
535502 return status ;
536503 }
@@ -547,7 +514,7 @@ static int start_protective_mode_string(const char* str, size_t params) {
547514 return status ;
548515}
549516
550- // handle execution of files
517+ // handles execution of files
551518static int start_protective_mode_file (const char * file , size_t params ) {
552519 signal (SIGINT , SIG_IGN ); // Ignore for now
553520
@@ -561,15 +528,15 @@ static int start_protective_mode_file(const char* file, size_t params) {
561528 }
562529 if (params > 0 )
563530 inject_parameters ();
564- if ((status = lua_pcall (L , params , 0 , base )) != 0 ) {
531+ if ((status = lua_pcall (L , ( int ) params , 0 , base )) != 0 ) {
565532 lua_pop (L , 2 ); // err msg, err handler
566533 return status ;
567534 }
568535 lua_pop (L , 1 ); // err handler
569536 return status ;
570537}
571538
572- // handle execution of anything to be required
539+ // handles execution of anything to be required
573540static inline int start_protective_mode_require (const char * file ) {
574541 signal (SIGINT , SIG_IGN ); // Ignore for now
575542
@@ -626,7 +593,7 @@ static inline void load_globals(Array* globals, void* data) {
626593 }
627594 lua_pushlstring (L , arg2 , strlen (arg2 ));
628595 lua_setfield (L , -2 , strnxt (cur_arg ));
629- lua_pop (L , dot_count - 1 ); // everything but root table
596+ lua_pop (L , ( int ) dot_count - 1 ); // everything but root table
630597 lua_setglobal (L , tabs );
631598 free (tabs );
632599 }
@@ -809,12 +776,21 @@ int main(int argc, char* argv[])
809776
810777 // handle init environment variable
811778 if (ARGS .no_env_var == 0 ) {
812- const char * env_init = getenv (ENV_VAR );
779+ #if defined(_MSC_VER )
780+ char * env_init ;
781+ size_t len ;
782+ errno_t err = _dupenv_s (& env_init , & len , ENV_VAR );
783+ #else
784+ char * env_init = getenv (ENV_VAR );
785+ #endif
813786 if (env_init != NULL ) {
814787 if (env_init [0 ] == '@' )
815788 start_protective_mode_file (env_init + 1 , 0 );
816789 else start_protective_mode_string (env_init , 0 );
817790 }
791+ #if defined(_MSC_VER )
792+ free (env_init );
793+ #endif
818794 } else {
819795 lua_pushboolean (L , 1 );
820796 lua_setfield (L , LUA_REGISTRYINDEX , "LUA_NOENV" );
@@ -823,7 +799,7 @@ int main(int argc, char* argv[])
823799
824800
825801 // copyright
826- if (ARGS .copyright_squelch == 0 ) {
802+ if (ARGS .copyright_squelch == 0 && ARGS . post_exist == 1 ) {
827803 fputs (LUA_VERSION " " LUA_COPYRIGHT "\n" , stdout );
828804 fputs (LUA_CONSOLE_COPYRIGHT "\n" , stdout );
829805 #if defined(LUA_JIT_51 )
@@ -880,7 +856,7 @@ int main(int argc, char* argv[])
880856 }
881857
882858 // make sure to start in the requested directory, if any
883- check_error (ARGS .start != NULL && chdir (ARGS .start ) == -1 , "Error: Invalid start directory supplied." );
859+ check_error (ARGS .start != NULL && _chdir (ARGS .start ) == -1 , "Error: Invalid start directory supplied." );
884860
885861
886862 // initiate global variables set up
@@ -922,6 +898,7 @@ int main(int argc, char* argv[])
922898 if (ARGS .delay_parameters == 1 )
923899 load_parameters ();
924900
901+ // files
925902 if (ARGS .no_file == 0 ) {
926903 for (size_t i = 0 ; i < ARGS .file_count ; i ++ ) {
927904 status = start_protective_mode_file (argv [i + 1 ], (ARGS .no_tuple_parameters == 1 ? 0 : ARGS .parameters ));
@@ -947,8 +924,8 @@ int main(int argc, char* argv[])
947924 #if defined(_WIN32 ) || defined(_WIN64 )
948925 HANDLE hand_stdin = CreateFile ("CONIN$" , (GENERIC_READ | GENERIC_WRITE ), FILE_SHARE_READ , 0 , OPEN_EXISTING , 0 , 0 );
949926 int hand_stdin_final = _open_osfhandle ((intptr_t )hand_stdin , _O_TEXT );
950- _dup2 (hand_stdin_final , fileno (stdin ));
951- SetStdHandle (STD_INPUT_HANDLE , (HANDLE ) _get_osfhandle (fileno (stdin )));
927+ _dup2 (hand_stdin_final , _fileno (stdin ));
928+ SetStdHandle (STD_INPUT_HANDLE , (HANDLE ) _get_osfhandle (_fileno (stdin )));
952929 _close (hand_stdin_final );
953930 #else
954931 fclose (stdin );
0 commit comments