mzteinit: add support for launch commands

This commit is contained in:
LordMZTE 2023-08-26 15:04:30 +02:00
parent 0b5bd17b69
commit 9037f82659
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6

View file

@ -4,6 +4,8 @@ const env = @import("env.zig");
const run = @import("run.zig");
const util = @import("util.zig");
const msg = @import("message.zig").msg;
pub const std_options = struct {
pub const log_level = .debug;
pub fn logFn(
@ -54,13 +56,26 @@ fn tryMain() !void {
defer _ = gpa.deinit();
const alloc = gpa.allocator();
var launch_cmd: ?[][]const u8 = null;
defer if (launch_cmd) |cmd| alloc.free(cmd);
if (std.os.argv.len >= 2) {
if (!std.mem.eql(u8, std.mem.span(std.os.argv[1]), "cmd") or std.os.argv.len < 3)
return error.InvalidCommand;
launch_cmd = try alloc.alloc([]const u8, std.os.argv[2..].len);
for (launch_cmd.?, std.os.argv[2..]) |*arg, arg_in| {
arg.* = std.mem.span(arg_in);
}
}
var env_map = try std.process.getEnvMap(alloc);
defer env_map.deinit();
if (env_map.get("MZTEINIT")) |_| {
try stdout.writer().writeAll("mzteinit running already, starting shell\n");
try stdout.flush();
var child = std.ChildProcess.init(&.{"fish"}, alloc);
var child = std.ChildProcess.init(launch_cmd orelse &.{"fish"}, alloc);
_ = try child.spawnAndWait();
return;
} else {
@ -71,6 +86,13 @@ fn tryMain() !void {
try env.populateSysdaemonEnvironment(&env_map);
}
if (launch_cmd) |cmd| {
try msg("using launch command", .{});
var child = std.ChildProcess.init(cmd, alloc);
_ = try child.spawnAndWait();
return;
}
while (true) {
try stdout.writer().writeAll(util.ansi_clear);