Changes
4 changed files (+83/-2)
-
-
@@ -22,11 +22,13 @@ const target = b.standardTargetOptions(.{});const optimize = b.standardOptimizeOption(.{}); const man_opt = b.option(bool, "man", "Builds and installs man pages") orelse false; const zig_main = b.option(bool, "zigmain", "Use Zig rewrite") orelse false; const version = b.option([]const u8, "version", "Application version, without \"v\" prefix") orelse "dev"; const exe = addExe(b, .{ .target = target, .optimize = optimize, .zig_main = zig_main, .version = version, }); // "zig build run"
-
@@ -74,12 +76,14 @@const legacy_exe = addExe(b, .{ .target = target, .optimize = optimize, .version = "0.91", }); const new_exe = addExe(b, .{ .target = target, .optimize = optimize, .zig_main = true, .version = version, }); const t = b.addTest(.{
-
@@ -104,6 +108,7 @@ const AddExeOptions = struct {target: ?std.Build.ResolvedTarget = null, optimize: std.builtin.OptimizeMode, zig_main: bool = false, version: []const u8, }; fn addExe(b: *std.Build, opts: AddExeOptions) *std.Build.Step.Compile {
-
@@ -116,6 +121,10 @@if (opts.zig_main) { exe.root_module.root_source_file = b.path("src/main.zig"); exe.root_module.addCMacro("SUNWAIT_NOMAIN", ""); const config = b.addOptions(); config.addOption([]const u8, "version", opts.version); exe.root_module.addOptions("config", config); } exe.linkLibC();
-
-
-
@@ -16,6 +16,7 @@ //// SPDX-License-Identifier: GPL-3.0-only const std = @import("std"); const config = @import("config"); const c = @cImport({ @cInclude("time.h");
-
@@ -27,6 +28,8 @@ ok = 0,generic_error = 1, day = 2, night = 3, out_of_memory = 10, stdout_write_error = 11, pub fn code(self: ExitCode) u8 { return @intFromEnum(self);
-
@@ -42,7 +45,6 @@ now: c.time_t = 0,target_time: c.time_t = 0, now_delta: c_ulong = 0, target_delta: c_ulong = 0, function_version: c.OnOff = c.ONOFF_OFF, function_usage: c.OnOff = c.ONOFF_OFF, function_report: c.OnOff = c.ONOFF_OFF, function_list: c.OnOff = c.ONOFF_OFF,
-
@@ -74,7 +76,7 @@ .nowTimet = self.now,.targetTimet = self.target_time, .now2000 = self.now_delta, .target2000 = self.target_delta, .functionVersion = self.function_version, .functionVersion = c.ONOFF_OFF, .functionUsage = self.function_usage, .functionReport = self.function_report, .functionList = self.function_list,
-
@@ -91,8 +93,30 @@ }}; pub fn main() u8 { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); const opts = RunOptions.init(); var c_opts = opts.toC(); var args = std.process.ArgIterator.initWithAllocator(allocator) catch { return ExitCode.out_of_memory.code(); }; defer args.deinit(); _ = args.skip(); while (args.next()) |arg| { if (std.mem.eql(u8, "-v", arg) or std.mem.eql(u8, "--version", arg)) { std.fmt.format(std.io.getStdOut().writer(), "{s}\n", .{config.version}) catch |err| { std.log.err("Unable to write to stdout: {s}", .{@errorName(err)}); return ExitCode.stdout_write_error.code(); }; return ExitCode.ok.code(); } } return switch (c.sunpoll(&c_opts)) { c.EXIT_DAY => ExitCode.day.code(),
-
-
-
@@ -18,4 +18,5 @@comptime { _ = @import("./report.zig"); _ = @import("./poll.zig"); _ = @import("./version.zig"); }
-
-
tests/version.zig (new)
-
@@ -0,0 +1,47 @@// Copyright (C) 2025 Shota FUJI // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. // // SPDX-License-Identifier: GPL-3.0-only const std = @import("std"); const config = @import("config"); fn version(allocator: std.mem.Allocator, bin: []const u8) !std.process.Child.RunResult { var env = std.process.EnvMap.init(allocator); defer env.deinit(); try env.put("TZ", "UTC"); return try std.process.Child.run(.{ .allocator = allocator, .argv = &.{ bin, "--version" }, .env_map = &env, }); } test { const legacy = try version(std.testing.allocator, config.legacy_bin); defer std.testing.allocator.free(legacy.stderr); defer std.testing.allocator.free(legacy.stdout); const new = try version(std.testing.allocator, config.new_bin); defer std.testing.allocator.free(new.stderr); defer std.testing.allocator.free(new.stdout); try std.testing.expectEqual(legacy.term.Exited, new.term.Exited); try std.testing.expect((legacy.stdout.len > 0) == (new.stdout.len > 0)); try std.testing.expect((legacy.stderr.len > 0) == (new.stderr.len > 0)); }
-