diff --git a/include/init_lua.h b/include/init_lua.h new file mode 100644 index 0000000..38feab0 --- /dev/null +++ b/include/init_lua.h @@ -0,0 +1,13 @@ +#ifndef LASER_INIT_LUA_H +#define LASER_INIT_LUA_H + +#include +#include +#include + +extern lua_State *L; + +lua_State *laser_init_lua(void); +void laser_lua_load_script(const char *script_path); + +#endif diff --git a/include/main.h b/include/main.h deleted file mode 100644 index 349d718..0000000 --- a/include/main.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef LASER_MAIN_H -#define LASER_MAIN_H - -#include -#include -#include - -extern lua_State *L; - -#endif diff --git a/src/init_lua.c b/src/init_lua.c new file mode 100644 index 0000000..cce0d50 --- /dev/null +++ b/src/init_lua.c @@ -0,0 +1,47 @@ +#include "init_lua.h" +#include "laser.h" +#include + +lua_State *L; + +lua_State *laser_init_lua(void) +{ + L = luaL_newstate(); + luaL_openlibs(L); + + return L; +} + +void laser_lua_load_script(const char *script_path) +{ + char script_dir[LASER_PATH_MAX]; + + // set the package.path so that user can do relative requires + const char *last_slash = strrchr(script_path, '/'); + if (last_slash != NULL) + { + size_t len = last_slash - script_path; + strncpy(script_dir, script_path, len); + script_dir[len] = '\0'; + + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + const char *current_path = lua_tostring(L, -1); + + char new_path[2 * LASER_PATH_MAX]; + snprintf(new_path, sizeof(new_path), "%s;%s/?.lua", current_path, + script_dir); + lua_pop(L, 1); + + lua_pushstring(L, new_path); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); + } + + if (luaL_dofile(L, script_path) != LUA_OK) + { + fprintf(stderr, "lsr: error loading Lua script: %s\n", + lua_tostring(L, -1)); + lua_pop(L, 1); + } +} diff --git a/src/main.c b/src/main.c index 94f3c90..be296a8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ -#include "main.h" #include "cli.h" #include "colors.h" +#include "init_lua.h" #include "laser.h" #include "utils.h" #include @@ -14,53 +14,9 @@ #define DEFAULT_SCRIPT_PATH "/usr/local/share/lsr/lsr.lua" #endif -lua_State *L; - -lua_State *initialize_lua(void) -{ - L = luaL_newstate(); - luaL_openlibs(L); - - return L; -} - -void lua_load_script(const char *script_path) -{ - char script_dir[LASER_PATH_MAX]; - - // set the package.path so that user can do relative requires - const char *last_slash = strrchr(script_path, '/'); - if (last_slash != NULL) - { - size_t len = last_slash - script_path; - strncpy(script_dir, script_path, len); - script_dir[len] = '\0'; - - lua_getglobal(L, "package"); - lua_getfield(L, -1, "path"); - const char *current_path = lua_tostring(L, -1); - - char new_path[2 * LASER_PATH_MAX]; - snprintf(new_path, sizeof(new_path), "%s;%s/?.lua", current_path, - script_dir); - lua_pop(L, 1); - - lua_pushstring(L, new_path); - lua_setfield(L, -2, "path"); - lua_pop(L, 1); - } - - if (luaL_dofile(L, script_path) != LUA_OK) - { - fprintf(stderr, "lsr: error loading Lua script: %s\n", - lua_tostring(L, -1)); - lua_pop(L, 1); - } -} - int main(int argc, char **argv) { - if (!initialize_lua()) + if (!laser_init_lua()) return 1; const char *default_script = DEFAULT_SCRIPT_PATH; @@ -80,9 +36,9 @@ int main(int argc, char **argv) script_to_load = user_config_path; if (script_to_load != default_script) - lua_load_script(script_to_load); - lua_load_script(default_script); // load the default script cuz user may - // not be defining everything + laser_lua_load_script(script_to_load); + laser_lua_load_script(default_script); // load the default script cuz user + // may not be defining everything laser_colors_init();