hostapps/src/main.zig
2024-12-13 10:39:38 -06:00

83 lines
2.8 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options");
const Options = @import("options.zig");
const Config = @import("config.zig");
const connect = @import("connect.zig").connect;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const gpa_alloc = gpa.allocator();
var arena = std.heap.ArenaAllocator.init(gpa_alloc);
const arena_alloc = arena.allocator();
defer arena.deinit();
var config: ?*Config = null;
var options: Options = .{};
var identities = std.ArrayList([]const u8).init(arena_alloc);
errdefer identities.deinit();
{
const OptionNames = enum {
config,
identity,
agent,
@"proxy-jump",
@"ssh-command",
@"telnet-command",
};
const args = try std.process.argsAlloc(arena_alloc);
defer std.process.argsFree(arena_alloc, args);
var index: usize = 1;
while (index < args.len) {
if (std.mem.startsWith(u8, args[index], "--")) {
const key, const value = kv: {
if (std.mem.indexOfScalarPos(u8, args[index], 2, '=')) |end| {
break :kv .{ args[index][2..end], args[index][end + 1 .. args.len] };
} else {
const key = args[index][2..];
index += 1;
const value = args[index];
break :kv .{ key, value };
}
};
if (std.meta.stringToEnum(OptionNames, key)) |option| switch (option) {
.config => {
config = try Config.read(arena_alloc, value);
},
.agent => {
options.agent = try arena_alloc.dupe(u8, value);
},
.identity => {
try identities.append(try arena_alloc.dupe(u8, value));
},
.@"proxy-jump" => {
options.proxy_jump = try arena_alloc.dupe(u8, value);
},
.@"ssh-command" => {
options.ssh_command = try arena_alloc.dupe(u8, value);
},
.@"telnet-command" => {
options.telnet_command = try arena_alloc.dupe(u8, value);
},
} else {
std.log.err("unknown option: {s}", .{key});
return;
}
}
index += 1;
}
if (identities.items.len > 0) {
options.identities = identities.items;
}
}
try connect(arena_alloc, &options, config orelse {
std.log.err("no configuration!", .{});
return;
});
}