aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Lance Borden 2025-08-19 14:53:18 -0400
committerGravatar GitHub 2025-08-19 14:53:18 -0400
commit810c1f8f943e9683e4d2efedba98dca0010af202 (patch)
tree2507acdf5cad8bc65e32c037bdf436f1500c1b74 /src
parentv0.3.2 (diff)
parentRevert to old hashmap: fixed gitmodule index (diff)
Merge pull request #6 from ItsMeForLua/my-feature-branch
Added Lua compat53: fixed the non-interactive mode: added .gitmodules to build.yml
Diffstat (limited to '')
-rw-r--r--src/lush.c76
1 files changed, 42 insertions, 34 deletions
diff --git a/src/lush.c b/src/lush.c
index 1c42139..b512a70 100644
--- a/src/lush.c
+++ b/src/lush.c
@@ -21,6 +21,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include "lua.h"
#include "lua_api.h"
#include "lualib.h"
+#include "compat-5.3.h"
#include <asm-generic/ioctls.h>
#include <bits/time.h>
#include <ctype.h>
@@ -42,6 +43,10 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <time.h>
#include <unistd.h>
+// Forward declare the open functions for the compat C modules we need to preload
+int luaopen_bit32 (lua_State *L);
+int luaopen_utf8 (lua_State *L);
+
#define BUFFER_SIZE 1024
#define MAX_GLOB 512
@@ -1485,13 +1490,26 @@ int main(int argc, char *argv[]) {
return 0;
}
+ // init lua state
+ lua_State *L = luaL_newstate();
+ if (!L) {
+ fprintf(stderr, "Failed to create Lua state\n");
+ return -1; // or handle appropriately
+ }
+ luaL_openlibs(L);
+
+ // --- Load compat modules and make them global ---
+ luaL_requiref(L, "bit32", luaopen_bit32, 1);
+ lua_pop(L, 1); // luaL_requiref leaves the module on the stack
+
+ luaL_requiref(L, "utf8", luaopen_utf8, 1);
+ lua_pop(L, 1);
+ // --- End pre-loading ---
+ lua_register_api(L);
+ lua_run_init(L);
+
// check if being run in command string mode
if (argc > 2 && strcmp(argv[1], "-c") == 0) {
- // init Lua state
- lua_State *L = luaL_newstate();
- luaL_openlibs(L);
- lua_register_api(L);
- lua_run_init(L);
// execute the command provided
char *command = argv[2];
@@ -1521,36 +1539,26 @@ int main(int argc, char *argv[]) {
return 0;
}
- // init lua state
- lua_State *L = luaL_newstate();
- luaL_openlibs(L);
- lua_register_api(L);
- lua_run_init(L);
+ // This is the corrected logic for running a script file non-interactively.
+ if (argc > 1) {
+ char *ext = strrchr(argv[1], '.');
+ if (ext && strcmp(ext, ".lua") == 0) {
+ const char *script_name = argv[1];
+ // The arguments for the script start at argv[2].
+ // We create a pointer to that part of the array.
+ char **script_args = (argc > 2) ? &argv[2] : NULL;
- // if a lua function is passed on load run non-interactively
- if (argc > 1) {
- char *ext = strrchr(argv[1], '.');
- if (ext) {
- ext++;
- if (strcmp(ext, "lua") == 0) {
- int status = 0;
- argv++;
- char ***args = lush_split_args(argv, &status);
-
- if (status == -1) {
- fprintf(stderr, "lush: Expected end of quoted string\n");
- } else if (lush_run(L, args, status) != 0) {
- exit(1);
- }
+ // Call the script loader directly with the script and its arguments.
+ if (lua_load_script(L, script_name, script_args) != 0) {
+ exit(1); // Exit if the script had an error
+ }
- for (int i = 0; args[i]; i++) {
- free(args[i]);
- }
- free(args);
- return 0;
- }
- }
- }
+ lua_close(L); // Clean up and exit
+ return 0;
+ }
+ }
+
+ // --- Interactive Shell Mode ---
// eat ^C in main
struct sigaction sa_int;
@@ -1617,4 +1625,4 @@ int main(int argc, char *argv[]) {
if (alt_shell != NULL)
free(alt_shell);
return 0;
-}
+} \ No newline at end of file