From 2c7d28aaa222a84df9519251fd893152b1eb1517 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Thu, 12 Sep 2024 14:16:14 -0400 Subject: added non interative mode for running lua scripts --- src/lush.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index bc03726..2e7ed66 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1159,11 +1159,38 @@ int main(int argc, char *argv[]) { #endif return 0; } + // init lua state lua_State *L = luaL_newstate(); luaL_openlibs(L); lua_register_api(L); lua_run_init(L); + + // 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); + } + + for (int i = 0; args[i]; i++) { + free(args[i]); + } + free(args); + return status; + } + } + } + // eat ^C in main struct sigaction sa; sa.sa_handler = SIG_IGN; -- cgit v1.2.3-59-g8ed1b