BSD Sysexits for Zig
1// SPDX-License-Identifier: AGPL-3.0-only AND BSD-3-Clause
2// SPDX-FileCopyrightText: 2025 @matrixfurry.com <did:plc:zmjoeu3stwcn44647rhxa44o>
3// SPDX-FileCopyrightText: 1987 Regents of the University of California
4//
5// Exit code constants and their descriptions are derived from BSD sysexits.h
6// under the BSD-3-Clause license.
7
8const testing = @import("std").testing;
9
10pub const metadata = struct {
11 /// Base value for error messages
12 pub const base = 64;
13
14 /// Maximum listed value
15 pub const max = 78;
16};
17
18/// Successful
19pub const ok: u8 = 0;
20
21/// The command was used incorrectly (e.g. wrong number of arguments,
22/// bad flag, bad syntax in a parameter, etc).
23pub const usage: u8 = 64;
24
25/// The input data was incorrect in some way. This should only be used
26/// for user's data and not system files (which should use `os_file`
27/// instead).
28pub const data_err: u8 = 65;
29
30/// An input file (not a system file, use `os_file` for that) did not
31/// exist or was not readable.
32pub const no_input: u8 = 66;
33
34/// The user specified did not exist.
35pub const no_user: u8 = 67;
36
37/// The host specified did not exist. This is used in network requests.
38pub const no_host: u8 = 68;
39
40/// A service is unavailable. This can occur if a support program or
41/// file does not exist. This can also be used as a catchall message
42/// when something you wanted to do doesn't work, but you don't know
43/// why.
44pub const unavailable: u8 = 69;
45
46/// An internal software error has been detected. This should be limited
47/// to non-operating system related errors if possible.
48pub const software: u8 = 70;
49
50/// An operating system error has been detected. This is intended to be
51/// used for such things as "cannot fork", "cannot create pipe", etc.
52pub const os_err: u8 = 71;
53
54/// Some system file (e.g. /etc/passwd, /var/run/utmp, etc.) does not
55/// exist, cannot be opened, or has some sort of error (e.g. syntax
56/// error).
57pub const os_file: u8 = 72;
58
59/// A user-specified output file cannot be created.
60pub const cant_create: u8 = 73;
61
62/// An error occurred while doing I/O on some file.
63pub const io_err: u8 = 74;
64
65/// Temporary failure, indicating something that is not really an error.
66/// For example, if a connection could not be created and should be
67/// reattempted later.
68pub const temp_fail: u8 = 75;
69
70/// The remote system returned something illegal during a protocol
71/// exchange.
72pub const protocol: u8 = 76;
73
74/// You did not have sufficient permission to perform the operation.
75/// This is not intended for filesystem problems, which should use
76/// `no_input` or `cant_create`. but rather for higher level
77/// permissions.
78pub const no_permission: u8 = 77;
79
80/// Something was found in an unconfigured or misconfigured state.
81pub const config: u8 = 78;
82
83test "metadata matches BSD sysexits" {
84 try testing.expectEqual(metadata.base, 64);
85 try testing.expectEqual(metadata.max, 78);
86}
87
88test "values match BSD sysexits" {
89 try testing.expectEqual(0, ok);
90
91 try testing.expectEqual(metadata.base, usage);
92 try testing.expectEqual(metadata.base + 1, data_err);
93 try testing.expectEqual(metadata.base + 2, no_input);
94 try testing.expectEqual(metadata.base + 3, no_user);
95 try testing.expectEqual(metadata.base + 4, no_host);
96 try testing.expectEqual(metadata.base + 5, unavailable);
97 try testing.expectEqual(metadata.base + 6, software);
98 try testing.expectEqual(metadata.base + 7, os_err);
99 try testing.expectEqual(metadata.base + 8, os_file);
100 try testing.expectEqual(metadata.base + 9, cant_create);
101 try testing.expectEqual(metadata.base + 10, io_err);
102 try testing.expectEqual(metadata.base + 11, temp_fail);
103 try testing.expectEqual(metadata.base + 12, protocol);
104 try testing.expectEqual(metadata.base + 13, no_permission);
105 try testing.expectEqual(metadata.base + 14, config);
106}
107
108test "metadata.max is correct" {
109 try testing.expect(metadata.max >= ok);
110 try testing.expect(metadata.max >= data_err);
111 try testing.expect(metadata.max >= no_input);
112 try testing.expect(metadata.max >= no_user);
113 try testing.expect(metadata.max >= no_host);
114 try testing.expect(metadata.max >= unavailable);
115 try testing.expect(metadata.max >= software);
116 try testing.expect(metadata.max >= os_err);
117 try testing.expect(metadata.max >= os_file);
118 try testing.expect(metadata.max >= cant_create);
119 try testing.expect(metadata.max >= io_err);
120 try testing.expect(metadata.max >= temp_fail);
121 try testing.expect(metadata.max >= protocol);
122 try testing.expect(metadata.max >= config);
123}