aboutsummaryrefslogtreecommitdiffstats
path: root/src/lush.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lush.c')
-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
ss='insertions'>+48 2024-09-25v0.2.1Gravatar BanceDev 1-1/+1 2024-09-25fixed semicolon chaining functionalityGravatar BanceDev 1-14/+9 2024-09-25v0.2.0Gravatar BanceDev 1-1/+1 2024-09-25fixed crash from incorrect pointer indexingGravatar BanceDev 1-1/+1 2024-09-25added append chaining operatorGravatar BanceDev 1-3/+7 2024-09-25added output redirection withGravatar BanceDev 1-12/+52 2024-09-25added ; chaining operatorGravatar BanceDev 1-14/+14 2024-09-25added || chaining operatorGravatar BanceDev 1-2/+9 2024-09-25Update README.mdGravatar Lance Borden 1-0/+3 2024-09-23fixed lua api exit status issueGravatar BanceDev 1-1/+1 2024-09-23fixed splitting within quoted stringGravatar BanceDev 1-5/+10 2024-09-23made and operator actually conditionalGravatar BanceDev 2-17/+27 2024-09-23fixed operator chaining to handle commands terminating with an operatorGravatar BanceDev 1-14/+5 2024-09-23implemented background process operatorGravatar BanceDev 6-154/+238 2024-09-23implemented piping into new chaining methodGravatar BanceDev 1-10/+30 2024-09-18fixed && chaining for builtinsGravatar BanceDev 2-23/+31 2024-09-18added basic && chainingGravatar BanceDev 2-8/+73 2024-09-17changed tokenizer to handle all the chaining operatorsGravatar BanceDev 3-20/+78 2024-09-17Update README.mdGravatar Lance Borden 1-0/+1 2024-09-13added better clarification to help menuGravatar BanceDev 1-1/+2 2024-09-13improved installation instructionsGravatar BanceDev 1-3/+3 2024-09-12v0.1.1Gravatar BanceDev 1-1/+1 2024-09-12fixed bug in input buffer handling due to misplaced printGravatar BanceDev 2-5/+8 2024-09-12Update build.yml checkout v4Gravatar Lance Borden 1-1/+1 2024-09-12Update build.yml to artifact v4Gravatar Lance Borden 1-1/+1 2024-09-12fixed exit status issue with non interative modeGravatar BanceDev 1-1/+1 2024-09-12added non interative mode for running lua scriptsGravatar BanceDev 2-4/+28 2024-09-12temporary github action fix until non-interactive mode is implementedGravatar BanceDev 1-2/+2 2024-09-12attempt to update build script to accept input into lush shellGravatar BanceDev 2-4/+6 2024-09-12prevent lush workflow from getting stuck in testsGravatar Lance Borden 1-1/+4 2024-09-12removed chsh in workflowGravatar Lance Borden 1-4/+1