Skip to content

Commit

Permalink
refactor(lua): move lua init functions from main
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattaa committed Nov 25, 2024
1 parent 9952036 commit 2ae3375
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
13 changes: 13 additions & 0 deletions include/init_lua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef LASER_INIT_LUA_H
#define LASER_INIT_LUA_H

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

extern lua_State *L;

lua_State *laser_init_lua(void);
void laser_lua_load_script(const char *script_path);

#endif
10 changes: 0 additions & 10 deletions include/main.h

This file was deleted.

47 changes: 47 additions & 0 deletions src/init_lua.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "init_lua.h"
#include "laser.h"
#include <string.h>

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);
}
}
54 changes: 5 additions & 49 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "main.h"
#include "cli.h"
#include "colors.h"
#include "init_lua.h"
#include "laser.h"
#include "utils.h"
#include <lauxlib.h>
Expand All @@ -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;
Expand All @@ -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();

Expand Down

0 comments on commit 2ae3375

Please sign in to comment.