22
22
*/
23
23
24
24
// 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 ;`
29
28
30
29
// dynamic array initialization sizes for darr's
31
30
#define DEFINES_INIT (4)
41
40
#define DO_EXT_ERROR_RETS (0)
42
41
43
42
// 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
44
44
#define ENV_VAR "LUA_INIT"
45
+ // #define ENV_VAR_EXT (0)
46
+
45
47
46
48
47
49
// 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
70
52
# 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))
74
56
# define LUA_BIN_EXT_NAME ".exe"
75
57
# define LUA_DLL_SO_NAME ".dll"
76
58
#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"
78
64
#endif
79
65
80
66
81
- // stdlib includes
82
- #include <dirent.h>
67
+ // 'cross-platform' includes
68
+ #include <stdio.h>
69
+ #include <stdlib.h>
83
70
#include <string.h>
84
71
#include <signal.h>
85
72
#include <fcntl.h>
86
73
87
74
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
91
77
// luajit `make install` puts them in /usr/local/include/luajit-X.X/*
92
78
// 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 )
110
83
# 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'"
115
84
#endif
116
85
117
86
118
87
// local src includes
119
88
#include "darr.h"
120
89
121
90
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
+
122
99
123
100
// copyright information
124
101
#define LUA_CONSOLE_COPYRIGHT "LuaConsole Copyright (C) 2017-2018, Cody Tilkins"
127
104
128
105
// usage message
129
106
const char HELP_MESSAGE [] =
130
- "LuaConsole | Version: 8/21 /2018\n\n"
107
+ "LuaConsole | Version: 9/2 /2018\n\n"
131
108
LUA_VERSION " " LUA_COPYRIGHT
132
109
"\n"
133
110
LUA_CONSOLE_COPYRIGHT
@@ -142,7 +119,7 @@ const char HELP_MESSAGE[] =
142
119
"\t[-Dtb.var=val] [-Lfile.lua] [-Llualib" LUA_DLL_SO_NAME "] [-t{a,b}] [-r \"string\"]\n"
143
120
"\t[-R \"string\"] "
144
121
#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}] "
146
123
#endif
147
124
"[-?] [-n {arg1 ...}]\n"
148
125
"\n"
@@ -161,19 +138,15 @@ const char HELP_MESSAGE[] =
161
138
"- \t\tProcesses input from stdin\n"
162
139
"-- \t\tStops processing parameters\n"
163
140
#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"
167
144
#endif
168
145
"-? --help \tDisplays this help message\n"
169
146
"-n \t\tStart of parameter section\n" ;
170
147
171
148
172
149
173
- #if defined(LUA_JIT_51 )
174
- #include "jitsupport.h"
175
- #endif
176
-
177
150
178
151
179
152
// struct for args to be seen across functions
@@ -319,7 +292,7 @@ int stack_dump(lua_State *L) {
319
292
}
320
293
321
294
322
- // internal enums, represent lua error category
295
+ // internal enums, represents lua error category
323
296
typedef enum LuaConsoleError {
324
297
INTERNAL_ERROR = 0 ,
325
298
SYNTAX_ERROR = 1 ,
@@ -443,7 +416,7 @@ static int lua_main_postexist(lua_State* L) {
443
416
lua_pop (L , 1 ); // err msg
444
417
} else {
445
418
// attempt first test, return seems to work
446
- size_t top = lua_gettop (L );
419
+ int top = lua_gettop (L );
447
420
status = lua_pcall (L , 0 , LUA_MULTRET , 0 );
448
421
if (status == 0 ) { // on success
449
422
top = lua_gettop (L ) - top + 1 ; // + 1 = ignore pushed function
@@ -466,14 +439,8 @@ static int lua_main_postexist(lua_State* L) {
466
439
print_error (SYNTAX_ERROR , 1 );
467
440
lua_pop (L , 2 ); // err msg, err handler
468
441
} else {
469
- if ((status = lua_pcall (L , 0 , LUA_MULTRET , base )) != 0 ) {
442
+ if ((status = lua_pcall (L , 0 , LUA_MULTRET , base )) != 0 )
470
443
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
- }
477
444
}
478
445
}
479
446
@@ -482,15 +449,15 @@ static int lua_main_postexist(lua_State* L) {
482
449
483
450
484
451
485
- // append parameters to the stack for a (p)call to consume
452
+ // append parameters to the stack for a pcall to consume
486
453
static inline void inject_parameters () {
487
454
for (size_t i = 0 ; i < ARGS .parameters ; i ++ )
488
455
lua_pushlstring (L , ARGS .parameters_argv [i ], strlen (ARGS .parameters_argv [i ]));
489
456
}
490
457
491
- // load parameters into global arg table
458
+ // load parameters into global ` arg` table
492
459
static inline void load_parameters () {
493
- lua_createtable (L , 0 , ARGS .parameters );
460
+ lua_createtable (L , 0 , ( int ) ARGS .parameters );
494
461
for (size_t i = 0 ; i < ARGS .parameters ; i ++ ) {
495
462
lua_pushinteger (L , i + 1 );
496
463
lua_pushlstring (L , ARGS .parameters_argv [i ], strlen (ARGS .parameters_argv [i ]));
@@ -500,7 +467,7 @@ static inline void load_parameters() {
500
467
}
501
468
502
469
503
- // handle execution of REPL
470
+ // handles execution of REPL
504
471
static inline int start_protective_mode_REPL () {
505
472
signal (SIGINT , SIG_IGN ); // SIGINT handled in lua_main_postexist
506
473
@@ -516,7 +483,7 @@ static inline int start_protective_mode_REPL() {
516
483
return status ;
517
484
}
518
485
519
- // handle execution of strings
486
+ // handles execution of strings
520
487
static int start_protective_mode_string (const char * str , size_t params ) {
521
488
signal (SIGINT , SIG_IGN ); // Ignore for now
522
489
@@ -530,7 +497,7 @@ static int start_protective_mode_string(const char* str, size_t params) {
530
497
}
531
498
if (params > 0 )
532
499
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 ) {
534
501
lua_pop (L , 2 ); // err msg, err handler
535
502
return status ;
536
503
}
@@ -547,7 +514,7 @@ static int start_protective_mode_string(const char* str, size_t params) {
547
514
return status ;
548
515
}
549
516
550
- // handle execution of files
517
+ // handles execution of files
551
518
static int start_protective_mode_file (const char * file , size_t params ) {
552
519
signal (SIGINT , SIG_IGN ); // Ignore for now
553
520
@@ -561,15 +528,15 @@ static int start_protective_mode_file(const char* file, size_t params) {
561
528
}
562
529
if (params > 0 )
563
530
inject_parameters ();
564
- if ((status = lua_pcall (L , params , 0 , base )) != 0 ) {
531
+ if ((status = lua_pcall (L , ( int ) params , 0 , base )) != 0 ) {
565
532
lua_pop (L , 2 ); // err msg, err handler
566
533
return status ;
567
534
}
568
535
lua_pop (L , 1 ); // err handler
569
536
return status ;
570
537
}
571
538
572
- // handle execution of anything to be required
539
+ // handles execution of anything to be required
573
540
static inline int start_protective_mode_require (const char * file ) {
574
541
signal (SIGINT , SIG_IGN ); // Ignore for now
575
542
@@ -626,7 +593,7 @@ static inline void load_globals(Array* globals, void* data) {
626
593
}
627
594
lua_pushlstring (L , arg2 , strlen (arg2 ));
628
595
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
630
597
lua_setglobal (L , tabs );
631
598
free (tabs );
632
599
}
@@ -809,12 +776,21 @@ int main(int argc, char* argv[])
809
776
810
777
// handle init environment variable
811
778
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
813
786
if (env_init != NULL ) {
814
787
if (env_init [0 ] == '@' )
815
788
start_protective_mode_file (env_init + 1 , 0 );
816
789
else start_protective_mode_string (env_init , 0 );
817
790
}
791
+ #if defined(_MSC_VER )
792
+ free (env_init );
793
+ #endif
818
794
} else {
819
795
lua_pushboolean (L , 1 );
820
796
lua_setfield (L , LUA_REGISTRYINDEX , "LUA_NOENV" );
@@ -823,7 +799,7 @@ int main(int argc, char* argv[])
823
799
824
800
825
801
// copyright
826
- if (ARGS .copyright_squelch == 0 ) {
802
+ if (ARGS .copyright_squelch == 0 && ARGS . post_exist == 1 ) {
827
803
fputs (LUA_VERSION " " LUA_COPYRIGHT "\n" , stdout );
828
804
fputs (LUA_CONSOLE_COPYRIGHT "\n" , stdout );
829
805
#if defined(LUA_JIT_51 )
@@ -880,7 +856,7 @@ int main(int argc, char* argv[])
880
856
}
881
857
882
858
// 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." );
884
860
885
861
886
862
// initiate global variables set up
@@ -922,6 +898,7 @@ int main(int argc, char* argv[])
922
898
if (ARGS .delay_parameters == 1 )
923
899
load_parameters ();
924
900
901
+ // files
925
902
if (ARGS .no_file == 0 ) {
926
903
for (size_t i = 0 ; i < ARGS .file_count ; i ++ ) {
927
904
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[])
947
924
#if defined(_WIN32 ) || defined(_WIN64 )
948
925
HANDLE hand_stdin = CreateFile ("CONIN$" , (GENERIC_READ | GENERIC_WRITE ), FILE_SHARE_READ , 0 , OPEN_EXISTING , 0 , 0 );
949
926
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 )));
952
929
_close (hand_stdin_final );
953
930
#else
954
931
fclose (stdin );
0 commit comments