add command line args

This commit is contained in:
Squibid 2025-12-12 12:46:39 -05:00
parent 354b915300
commit ced6189a64
Signed by: squibid
GPG key ID: BECE5684D3C4005D
5 changed files with 113 additions and 11 deletions

View file

@ -44,6 +44,34 @@ pub fn loadRuntimeDir(self: *zlua.Lua) !void {
};
}
pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void {
{
_ = try self.getGlobal("mez");
_ = self.getField(-1, "path");
defer self.pop(2);
const new_path = try std.fs.path.join(gpa, &[_][]const u8{path, "init.lua"});
defer gpa.free(new_path);
_ = self.pushString(new_path);
self.setField(-2, "config");
}
{
_ = try self.getGlobal("mez");
_ = self.getField(-1, "path");
defer self.pop(2);
const cur_path = self.toString(-1) catch "";
const unsentinel: []const u8 = std.mem.span(cur_path.ptr);
const new_path = try std.mem.concat(gpa, u8, &[_][]const u8{
unsentinel,
";",
path,
});
defer gpa.free(new_path);
_ = self.pushString(new_path);
_ = self.setField(-2, "path");
}
}
fn loadBaseConfig(self: *zlua.Lua) !void {
const lua_path = "mez.path.base_config";
if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) {
@ -113,13 +141,15 @@ pub fn openLibs(self: *zlua.Lua) void {
}
}
pub fn init(self: *Lua) !void {
pub const Config = struct { str: ?[]const u8, enabled: bool, };
pub fn init(self: *Lua, cfg: Config) !void {
self.state = try zlua.Lua.init(gpa);
errdefer self.state.deinit();
self.state.openLibs();
openLibs(self.state);
if (!cfg.enabled) try setBaseConfig(self.state, "");
loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
@ -128,9 +158,15 @@ pub fn init(self: *Lua) !void {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
loadConfigDir(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
if (cfg.str) |path| {
defer gpa.free(path);
try setBaseConfig(self.state, path);
}
if (cfg.enabled) {
loadConfigDir(self.state) catch |err| if (err == error.LuaRuntime) {
std.log.warn("{s}", .{try self.state.toString(-1)});
};
}
std.log.debug("Loaded lua", .{});
}

View file

@ -1,4 +1,6 @@
const std = @import("std");
const config = @import("config");
const clap = @import("clap");
const wlr = @import("wlroots");
const Server = @import("Server.zig");
@ -10,14 +12,62 @@ pub var server: Server = undefined;
pub var lua: Lua = undefined;
pub var env_map: std.process.EnvMap = undefined;
pub fn main() !void {
wlr.log.init(.err, null);
const usage =
\\Usage: mez [options]
\\
\\Options:
++ "\n" ++ args ++ "\n";
const args =
\\ -u <path> Use this config
\\ -c <command> Runs this command at startup
\\ -v, --version Print the version and exit
\\ -h, --help Print this help and exit
\\
\\ --clean "Factory defaults" (skip user config)
;
pub fn main() !void {
const params = comptime clap.parseParamsComptime(args);
var diag = clap.Diagnostic{};
const parsers = comptime .{
.path = clap.parsers.string,
.command = clap.parsers.string,
};
var res = clap.parse(clap.Help, &params, parsers, .{
.diagnostic = &diag,
.allocator = gpa,
}) catch |err| {
try diag.reportToFile(.stderr(), err);
return err;
};
defer res.deinit();
if (res.args.help == 1) {
try @constCast(&std.fs.File.stdout().writer(&[_]u8{}).interface).writeAll(usage);
std.process.exit(0);
}
if (res.args.version == 1) {
try @constCast(&std.fs.File.stdout().writer(&[_]u8{}).interface).writeAll(config.version);
std.process.exit(0);
}
var lua_config: Lua.Config = .{ .enabled = true, .str = null };
if (res.args.u != null and res.args.clean == 1) {
std.debug.panic("You cannot set both -u and --clean", .{});
} else if (res.args.u != null) {
// this is freed in lua/lua.zig
const path = try std.fs.cwd().realpathAlloc(gpa, res.args.u.?);
lua_config.str = path;
} else if (res.args.clean == 1) {
lua_config.enabled = false;
}
wlr.log.init(.err, null);
std.log.info("Starting mezzaluna", .{});
server.init();
defer server.deinit();
try lua.init();
try lua.init(lua_config);
var buf: [11]u8 = undefined;
const socket = try server.wl_server.addSocketAuto(&buf);
@ -25,8 +75,7 @@ pub fn main() !void {
env_map = try std.process.getEnvMap(gpa);
try env_map.put("WAYLAND_DISPLAY", socket);
if (std.os.argv.len >= 2) {
const cmd = std.mem.span(std.os.argv[1]);
if (res.args.c) |cmd| {
var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa);
child.env_map = &env_map;
try child.spawn();