]>
Commit | Line | Data |
---|---|---|
70ad1dc8 A |
1 | /* |
2 | * Copyright (c) 2018 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /*! | |
25 | * @header | |
26 | * Non-standard, Darwin-specific additions to the string(3) family of APIs. | |
27 | */ | |
28 | #ifndef __DARWIN_STRING_H | |
29 | #define __DARWIN_STRING_H | |
30 | ||
31 | #include <os/base.h> | |
32 | #include <os/api.h> | |
33 | #include <sys/cdefs.h> | |
34 | ||
35 | __BEGIN_DECLS; | |
36 | ||
37 | /*! | |
38 | * @typedef os_flag_t | |
39 | * A type describing a flag's human-readable name. | |
40 | * | |
41 | * @property ohf_flag | |
42 | * The flag value. | |
43 | * | |
44 | * @property ohf_human | |
45 | * The human-readable, string representation of the flag value. | |
46 | */ | |
47 | DARWIN_API_AVAILABLE_20170407 | |
48 | typedef struct _os_flag { | |
49 | const uint64_t ohf_flag; | |
50 | const char *const ohf_human_flag; | |
51 | } os_flag_t; | |
52 | ||
53 | /*! | |
54 | * @define OS_FLAGSET_COUNT | |
55 | * The maximum number of flags that a flagset can represent. | |
56 | */ | |
57 | #define OS_FLAGSET_COUNT (sizeof(uint64_t) * BYTE_SIZE) | |
58 | ||
59 | /*! | |
60 | * @typedef os_flagset_t | |
61 | * A type describing an array of human flags. Can accommodate up to 64 flags. | |
62 | */ | |
63 | DARWIN_API_AVAILABLE_20170407 | |
64 | typedef os_flag_t os_flagset_t[OS_FLAGSET_COUNT]; | |
65 | ||
66 | /*! | |
67 | * @macro os_flag_init | |
68 | * Initializer for a {@link os_flag_t} structure which stringifies the | |
69 | * flag value. | |
70 | * | |
71 | * @param __flag | |
72 | * The name of the flag to initialize. | |
73 | */ | |
74 | #define os_flag_init(__flag) { \ | |
75 | .ohf_flag = __flag, \ | |
76 | .ohf_human_flag = #__flag, \ | |
77 | } | |
78 | ||
79 | /*! | |
80 | * @function strerror_np | |
81 | * Returns a human-readable string for the given {@link errno_t} or | |
82 | * POSIX error code. | |
83 | * | |
84 | * @param code | |
85 | * The error code for which to obtain the string. | |
86 | * | |
87 | * @result | |
88 | * A human-readable string describing the error condition. If a POSIX error code | |
89 | * is given, this is equivalent to a call to strerror(3). | |
90 | */ | |
91 | DARWIN_API_AVAILABLE_20170407 | |
92 | OS_EXPORT OS_WARN_RESULT OS_PURE | |
93 | const char * | |
94 | strerror_np(int code); | |
95 | ||
96 | /*! | |
97 | * @function symerror_np | |
98 | * Returns the token name of the given {@link errno_t} or POSIX error | |
99 | * code. | |
100 | * | |
101 | * @param code | |
102 | * The error code for which to obtain the token string. | |
103 | * | |
104 | * @result | |
105 | * The string describing the error token. For example, if code 2 is passed, the | |
106 | * string "EPERM" is returned. | |
107 | */ | |
108 | DARWIN_API_AVAILABLE_20170407 | |
109 | OS_EXPORT OS_WARN_RESULT OS_PURE | |
110 | const char * | |
111 | symerror_np(int code); | |
112 | ||
113 | /*! | |
114 | * @function symexit_np | |
115 | * Returns the token name of the given sysexits(3) code. | |
116 | * | |
117 | * @param code | |
118 | * The sysexits(3) code for which to obtain the token string. | |
119 | * | |
120 | * @result | |
121 | * The string describing the exit code. For example, if 64 is passed, the string | |
122 | * "EX_USAGE" is returned. If the code is unrecognized, "EX_UNAVAILABLE" is | |
123 | * returned, which is more or less documented in sysexits(3) as the ¯\_(ツ)_/¯ | |
124 | * exit code. | |
125 | */ | |
126 | DARWIN_API_AVAILABLE_20170407 | |
127 | OS_EXPORT OS_WARN_RESULT OS_PURE | |
128 | const char * | |
129 | symexit_np(int code); | |
130 | ||
131 | /*! | |
132 | * @function os_flagset_copy_string | |
133 | * Returns a human-readable representation of the flags set for a given word. | |
134 | * | |
135 | * @param flagset | |
136 | * The human flagset which describes how to interpret the {@link flags} | |
137 | * parameter. | |
138 | * | |
139 | * @param flags | |
140 | * The flags to interpret. | |
141 | * | |
142 | * @result | |
143 | * The human-readable representation of all flags set in the {@link flags} | |
144 | * parameter, separated by the pipe character. The caller is responsible for | |
145 | * calling free(3) on this object when it is no longer needed. | |
146 | * | |
147 | * @discussion | |
148 | * This API is to be used in combination with {@link os_flag_init} to make | |
149 | * printing the contents of flags fields simple. For example, this code can | |
150 | * easily print a human-readable representation of the bits set in a Mach | |
151 | * message header: | |
152 | * | |
153 | * static const os_flagset_t _mach_msgh_bits = { | |
154 | * os_flag_init(MACH_MSGH_BITS_COMPLEX), | |
155 | * os_flag_init(MACH_MSGH_BITS_RAISEIMP), | |
156 | * os_flag_init(MACH_MSGH_BITS_IMPHOLDASRT), | |
157 | * }; | |
158 | * | |
159 | * char *flags_string = os_flagset_copy_string(&_mach_msgh_bits, | |
160 | * hdr->msgh_bits); | |
161 | * | |
162 | * For a message header with the MACH_MSGH_BITS_COMPLEX and | |
163 | * MACH_MSGH_BITS_RAISEIMP bits set, this will return the string | |
164 | * | |
165 | * MACH_MSGH_BITS_COMPLEX|MACH_MSGH_BITS_RAISEIMP | |
166 | */ | |
167 | DARWIN_API_AVAILABLE_20170407 | |
168 | OS_EXPORT OS_WARN_RESULT OS_MALLOC | |
169 | char * | |
170 | os_flagset_copy_string(const os_flagset_t flagset, uint64_t flags); | |
171 | ||
172 | __END_DECLS; | |
173 | ||
174 | #endif // __DARWIN_STRING_H |