diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 33802c8..9976a90 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -1 +1,11 @@ -print("hello from our runtime file") +local env_conf = os.getenv("XDG_CONFIG_HOME") +if not env_conf then + env_conf = os.getenv("HOME") + if not env_conf then + error("Couldn't determine potential config directory is $HOME set?") + end + env_conf = env_conf.."/.config/" +end + +mez.path.config = env_conf.."/mez/init.lua" +package.path = package.path..";"..env_conf.."/mez/lua/?.lua" diff --git a/src/lua.zig b/src/lua.zig deleted file mode 100644 index 357874c..0000000 --- a/src/lua.zig +++ /dev/null @@ -1,43 +0,0 @@ -const Lua = @This(); - -const std = @import("std"); -const config = @import("config"); -const zlua = @import("zlua"); - -const gpa = std.heap.c_allocator; - -state: *zlua.Lua, - -fn loadRuntimeDir(self: *Lua) !void { - const tmppath = try std.fs.path.join(gpa, &[_][]const u8{ - config.runtime_path_prefix, - "share", - "mezzaluna", - "init.lua", - }); - var path_buffer = try gpa.alloc(u8, tmppath.len + 1); - std.mem.copyForwards(u8, path_buffer[0..tmppath.len], tmppath); - path_buffer[tmppath.len] = 0; - const path: [:0]u8 = path_buffer[0..tmppath.len :0]; - - try self.state.doFile(path); -} - -pub fn init(self: *Lua) !void { - self.state = try zlua.Lua.init(gpa); - errdefer self.state.deinit(); - self.state.openLibs(); - - { - self.state.newTable(); - defer _ = self.state.pushString("mez"); - } - - try loadRuntimeDir(self); - - std.log.debug("Loaded lua", .{}); -} - -pub fn deinit(self: *Lua) void { - self.state.deinit(); -} diff --git a/src/lua/bridge.zig b/src/lua/bridge.zig new file mode 100644 index 0000000..37868d3 --- /dev/null +++ b/src/lua/bridge.zig @@ -0,0 +1,28 @@ +const Bridge = @This(); + +const std = @import("std"); +const Lua = @import("lua.zig"); + +const gpa = std.heap.c_allocator; + +pub fn getNestedField(L: *Lua, path: []u8) bool { + var tokens = std.mem.tokenizeScalar(u8, path, '.'); + var first = true; + + while (tokens.next()) |token| { + const tok = gpa.dupeZ(u8, token) catch return false; + if (first) { + _ = L.state.getGlobal(tok) catch return false; + first = false; + } else { + _ = L.state.getField(-1, tok); + L.state.remove(-2); + } + + if (L.state.isNil(-1)) { + return false; + } + } + + return true; +} diff --git a/src/lua/fs.zig b/src/lua/fs.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/lua/lua.zig b/src/lua/lua.zig new file mode 100644 index 0000000..ae5c23b --- /dev/null +++ b/src/lua/lua.zig @@ -0,0 +1,72 @@ +const Lua = @This(); + +const std = @import("std"); +const config = @import("config"); +const zlua = @import("zlua"); + +const Bridge = @import("bridge.zig"); + +const gpa = std.heap.c_allocator; + +state: *zlua.Lua, + +fn loadRuntimeDir(self: *Lua) !void { + const tmppath = try std.fs.path.join(gpa, &[_][]const u8{ + config.runtime_path_prefix, + "share", + "mezzaluna", + "init.lua", + }); + const path = try gpa.dupeZ(u8, tmppath); + + self.state.doFile(path) catch { + const err = try self.state.toString(-1); + std.log.debug("Failed to run lua file: {s}", .{err}); + }; +} + +fn loadConfigDir(self: *Lua) !void { + const lua_path = "mez.path.config"; + if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) { + std.log.err("Config path not found. is your runtime dir setup?", .{}); + return; + } + const path = self.state.toString(-1) catch |err| { + std.log.err("Failed to pop the config path from the lua stack. {}", .{err}); + return; + }; + self.state.pop(-1); + try self.state.doFile(path); +} + +pub fn init(self: *Lua) !void { + self.state = try zlua.Lua.init(gpa); + errdefer self.state.deinit(); + self.state.openLibs(); + + { + self.state.newTable(); + defer _ = self.state.setGlobal("mez"); + { + self.state.newTable(); + defer _ = self.state.setField(-2, "path"); + } + } + + loadRuntimeDir(self) catch |err| { + if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try self.state.toString(-1)}); + } + }; + loadConfigDir(self) catch |err| { + if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try self.state.toString(-1)}); + } + }; + + std.log.debug("Loaded lua", .{}); +} + +pub fn deinit(self: *Lua) void { + self.state.deinit(); +} diff --git a/src/main.zig b/src/main.zig index 44fa4a7..586fc6c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,7 +2,7 @@ const std = @import("std"); const wlr = @import("wlroots"); const Server = @import("server.zig"); -const Lua = @import("lua.zig"); +const Lua = @import("lua/lua.zig"); const gpa = std.heap.c_allocator;