aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Andrew D. France 2025-07-14 17:51:23 -0500
committerGravatar Andrew D. France 2025-07-14 17:51:23 -0500
commit6f8c0f3abda585b3ee1d87e3b8e19c729b52b4a6 (patch)
tree8361fd3812991d43adca5b5c81029486aa955478 /src
parentv0.3.2 (diff)
Added compat53 support and fixed bugged non-interactive mode
Diffstat (limited to 'src')
-rw-r--r--src/lush.c60
1 files changed, 26 insertions, 34 deletions
diff --git a/src/lush.c b/src/lush.c
index 1c42139..cadc1bc 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 "../lib/compat53/c-api/compat-5.3.h"
#include <asm-generic/ioctls.h>
#include <bits/time.h>
#include <ctype.h>
@@ -1485,13 +1486,14 @@ 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);
+
// 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 +1523,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 +1609,4 @@ int main(int argc, char *argv[]) {
if (alt_shell != NULL)
free(alt_shell);
return 0;
-}
+} \ No newline at end of file