summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-11 11:33:34 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-11 18:06:36 -0500
commitb3f1b00510a6be9a4f792b6fbf7fd61797603eaa (patch)
tree9ead508d30200e1e58fe8b7c78c93eaf1dbaec85
parent7a8874ea6ac846eea3bdc35be2f46501877df87c (diff)
start porting to 0.16.0
-rw-r--r--build.zig11
-rw-r--r--build.zig.zon17
-rw-r--r--src/main.zig166
-rw-r--r--src/root.zig2
4 files changed, 105 insertions, 91 deletions
diff --git a/build.zig b/build.zig
index 4b32dd9..c07def0 100644
--- a/build.zig
+++ b/build.zig
@@ -41,13 +41,10 @@ pub fn build(b: *std.Build) void {
.target = target,
});
- mod.addImport("network", b.dependency("network", .{}).module("network"));
- mod.addImport("gatorcat", b.dependency("gatorcat", .{}).module("gatorcat"));
-
// Here we define an executable. An executable needs to have a root module
// which needs to expose a `main` function. While we could add a main function
// to the module defined above, it's sometimes preferable to split business
- // business logic and the CLI into two separate modules.
+ // logic and the CLI into two separate modules.
//
// If your goal is to create a Zig library for others to use, consider if
// it might benefit from also exposing a CLI tool. A parser library for a
@@ -82,7 +79,6 @@ pub fn build(b: *std.Build) void {
// can be extremely useful in case of collisions (which can happen
// importing modules from different packages).
.{ .name = "zaprus", .module = mod },
- .{ .name = "clap", .module = b.dependency("clap", .{}).module("clap") },
},
}),
});
@@ -92,11 +88,6 @@ pub fn build(b: *std.Build) void {
// step). By default the install prefix is `zig-out/` but can be overridden
// by passing `--prefix` or `-p`.
b.installArtifact(exe);
- b.installArtifact(b.addLibrary(.{
- .linkage = .static,
- .name = "zaprus",
- .root_module = mod,
- }));
// This creates a top level step. Top level steps have a name and can be
// invoked by name when running `zig build` (e.g. `zig build run`).
diff --git a/build.zig.zon b/build.zig.zon
index aff5d0d..4fa51c1 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -28,27 +28,14 @@
// Tracks the earliest Zig version that the package considers to be a
// supported use case.
- .minimum_zig_version = "0.14.0",
+ .minimum_zig_version = "0.16.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
- .dependencies = .{
- .network = .{
- .url = "git+https://github.com/ikskuh/zig-network#7947237eec317d9458897f82089f343a05450c2b",
- .hash = "network-0.1.0-Pm-Agl8xAQBmkwohveGOfTk4zQnuqDs0Ptfbms4KP5Ce",
- },
- .clap = .{
- .url = "git+https://github.com/Hejsil/zig-clap#9cfa61596cd44ef7be35f8d2e108d2025e09868e",
- .hash = "clap-0.10.0-oBajB_TnAQB0l5UdW9WYhhJDEswbedvwFOzzZwGknYeR",
- },
- .gatorcat = .{
- .url = "git+https://github.com/jeffective/gatorcat#db73d0f7780331d82e785e85773d1afaf154c2e6",
- .hash = "gatorcat-0.3.11-WcrpTQn0BwArrCFVHy9FPBIPDJQqPrFdJlhiyH7Ng5x4",
- },
- },
+ .dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
diff --git a/src/main.zig b/src/main.zig
index 20aaa3b..064dcea 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,77 +1,115 @@
const is_debug = builtin.mode == .Debug;
-/// This creates a debug allocator that can only be referenced in debug mode.
-/// You should check for is_debug around every reference to dba.
-var dba: DebugAllocator =
- if (is_debug)
- DebugAllocator.init
- else
- @compileError("Should not use debug allocator in release mode");
+const help =
+ \\-h, --help Display this help and exit.
+ \\-r, --relay <str> A relay message to send.
+ \\-d, --dest <str> An IPv4 or <= 4 ASCII byte string.
+ \\-c, --connect <str> A connection message to send.
+ \\
+;
-pub fn main() !void {
- defer if (is_debug) {
- _ = dba.deinit();
- };
-
- const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
+const Option = enum { help, relay, dest, connect };
+const to_option: StaticStringMap(Option) = .initComptime(.{
+ .{ "-h", .help },
+ .{ "--help", .help },
+ .{ "-r", .relay },
+ .{ "--relay", .relay },
+ .{ "-d", .dest },
+ .{ "--dest", .dest },
+ .{ "-c", .connect },
+ .{ "--connect", .connect },
+});
+pub fn main(init: std.process.Init) !void {
// CLI parsing adapted from the example here
- // https://github.com/Hejsil/zig-clap/blob/e47028deaefc2fb396d3d9e9f7bd776ae0b2a43a/README.md#examples
-
- // First we specify what parameters our program can take.
- // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
- const params = comptime clap.parseParamsComptime(
- \\-h, --help Display this help and exit.
- \\-r, --relay <str> A relay message to send.
- \\-d, --dest <str> An IPv4 or <= 4 ASCII byte string.
- \\-c, --connect <str> A connection message to send.
- \\
- );
+ // https://codeberg.org/ziglang/zig/pulls/30644
- // Initialize our diagnostics, which can be used for reporting useful errors.
- // This is optional. You can also pass `.{}` to `clap.parse` if you don't
- // care about the extra information `Diagnostics` provides.
- var diag = clap.Diagnostic{};
- var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
- .diagnostic = &diag,
- .allocator = gpa,
- }) catch |err| {
- // Report useful error and exit.
- try diag.reportToFile(.stderr(), err);
- return err;
- };
- defer res.deinit();
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
- if (res.args.help != 0) {
- return clap.helpToFile(.stderr(), clap.Help, &params, .{});
+ if (args.len == 1) {
+ std.debug.print("{s}", .{help});
+ return;
}
- var sock_buffer: [1500]u8 = undefined;
- var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
- var net_buffer: [1500]u8 = undefined;
- var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer);
- var client = try SaprusClient.init(&net_writer.interface);
- defer client.deinit();
+ var flags: struct {
+ relay: ?[]const u8 = null,
+ dest: ?[]const u8 = null,
+ connect: ?[]const u8 = null,
+ } = .{};
- if (res.args.relay) |r| {
- const dest = parseDest(res.args.dest);
- try client.sendRelay(
- if (r.len > 0) r else "Hello darkness my old friend",
- dest,
- );
- return;
- } else if (res.args.connect) |c| {
- if (false) {
- _ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) {
- error.WouldBlock => null,
- else => return err,
- };
- return;
+ {
+ var i: usize = 1;
+ while (i < args.len) : (i += 1) {
+ if (to_option.get(args[i])) |opt| {
+ switch (opt) {
+ .help => {
+ std.debug.print("{s}\n", .{help});
+ return;
+ },
+ .relay => {
+ i += 1;
+ if (i < args.len) {
+ flags.relay = args[i];
+ } else {
+ std.debug.print("-r/--relay requires a string\n", .{});
+ return;
+ }
+ },
+ .dest => {
+ i += 1;
+ if (i < args.len) {
+ flags.dest = args[i];
+ } else {
+ std.debug.print("-d/--dest requires a string\n", .{});
+ return;
+ }
+ },
+ .connect => {
+ i += 1;
+ if (i < args.len) {
+ flags.connect = args[i];
+ } else {
+ std.debug.print("-c/--connect requires a string\n", .{});
+ return;
+ }
+ },
+ }
+ } else {
+ std.debug.print("Unknown argument: {s}\n", .{args[i]});
+ }
}
- @panic("Not implemented");
}
- return clap.helpToFile(.stderr(), clap.Help, &params, .{});
+ std.debug.print("relay: {s}\n", .{flags.relay orelse "<null>"});
+ std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"});
+ std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"});
+
+ // var sock_buffer: [1500]u8 = undefined;
+ // var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
+ // var net_buffer: [1500]u8 = undefined;
+ // var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer);
+ // var client = try SaprusClient.init(&net_writer.interface);
+ // defer client.deinit();
+
+ // if (res.args.relay) |r| {
+ // const dest = parseDest(res.args.dest);
+ // try client.sendRelay(
+ // if (r.len > 0) r else "Hello darkness my old friend",
+ // dest,
+ // );
+ // return;
+ // } else if (res.args.connect) |c| {
+ // if (false) {
+ // _ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) {
+ // error.WouldBlock => null,
+ // else => return err,
+ // };
+ // return;
+ // }
+ // @panic("Not implemented");
+ // }
+
+ // return clap.helpToFile(.stderr(), clap.Help, &params, .{});
}
fn parseDest(in: ?[]const u8) [4]u8 {
@@ -90,13 +128,11 @@ fn parseDest(in: ?[]const u8) [4]u8 {
const builtin = @import("builtin");
const std = @import("std");
-const DebugAllocator = std.heap.DebugAllocator(.{});
const ArrayList = std.ArrayList;
+const StaticStringMap = std.StaticStringMap;
const zaprus = @import("zaprus");
const SaprusClient = zaprus.Client;
const SaprusMessage = zaprus.Message;
const RawSocketWriter = zaprus.RawSocketWriter;
-const NetWriter = zaprus.NetWriter;
-
-const clap = @import("clap");
+// const NetWriter = zaprus.NetWriter;
diff --git a/src/root.zig b/src/root.zig
index 2005490..ed3648a 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -1,7 +1,7 @@
pub const Client = @import("Client.zig");
pub const Connection = @import("Connection.zig");
pub const RawSocketWriter = @import("RawSocketWriter.zig");
-pub const NetWriter = @import("NetWriter.zig");
+// pub const NetWriter = @import("NetWriter.zig");
const msg = @import("message.zig");