xref: /haiku/src/bin/error.c (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <SupportDefs.h>
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 
13 
14 extern const char *__progname;
15 
16 struct error_pair {
17 	status_t value;
18 	const char *name;
19 } kErrorNames[] = {
20 #define E(e) { e, #e }
21 // generated by:
22 // tr ' ,=\t()+' '\n\n\n\n\n\n\n' <headers/os/support/Errors.h | grep '\(^B_\|^E[A-Z0-9]\)' | sort | uniq | awk '{ print "\tE(" $1 ")," }'
23 	E(B_ALREADY_RUNNING),
24 	E(B_AMBIGUOUS_APP_LAUNCH),
25 	E(B_APP_ERROR_BASE),
26 	E(B_BAD_ADDRESS),
27 	E(B_BAD_DATA),
28 	E(B_BAD_HANDLER),
29 	E(B_BAD_IMAGE_ID),
30 	E(B_BAD_INDEX),
31 	E(B_BAD_MIME_SNIFFER_RULE),
32 	E(B_BAD_PORT_ID),
33 	E(B_BAD_REPLY),
34 	E(B_BAD_SCRIPT_SYNTAX),
35 	E(B_BAD_SEM_ID),
36 	E(B_BAD_SUBSCRIBER),
37 	E(B_BAD_TEAM_ID),
38 	E(B_BAD_THREAD_ID),
39 	E(B_BAD_THREAD_STATE),
40 	E(B_BAD_TYPE),
41 	E(B_BAD_VALUE),
42 	E(B_BUFFER_NOT_AVAILABLE),
43 #ifdef B_BUFFER_OVERFLOW
44 	E(B_BUFFER_OVERFLOW),
45 #endif
46 	E(B_BUSTED_PIPE),
47 	E(B_BUSY),
48 	E(B_CANCELED),
49 	E(B_CROSS_DEVICE_LINK),
50 	E(B_DEBUGGER_ALREADY_INSTALLED),
51 	E(B_DEVICE_ERROR_BASE),
52 	E(B_DEVICE_FULL),
53 #ifdef B_DEVICE_NOT_FOUND
54 	E(B_DEVICE_NOT_FOUND),
55 #endif
56 	E(B_DEV_BAD_DRIVE_NUM),
57 	E(B_DEV_BAD_PID),
58 	E(B_DEV_CONFIGURATION_ERROR),
59 	E(B_DEV_CRC_ERROR),
60 	E(B_DEV_DATA_OVERRUN),
61 	E(B_DEV_DATA_UNDERRUN),
62 	E(B_DEV_DISABLED_BY_USER),
63 	E(B_DEV_DOOR_OPEN),
64 	E(B_DEV_FIFO_OVERRUN),
65 	E(B_DEV_FIFO_UNDERRUN),
66 	E(B_DEV_FORMAT_ERROR),
67 	E(B_DEV_ID_ERROR),
68 	E(B_DEV_INVALID_IOCTL),
69 	E(B_DEV_INVALID_PIPE),
70 	E(B_DEV_MEDIA_CHANGED),
71 	E(B_DEV_MEDIA_CHANGE_REQUESTED),
72 	E(B_DEV_MULTIPLE_ERRORS),
73 	E(B_DEV_NOT_READY),
74 	E(B_DEV_NO_MEDIA),
75 	E(B_DEV_NO_MEMORY),
76 	E(B_DEV_PENDING),
77 	E(B_DEV_READ_ERROR),
78 	E(B_DEV_RECALIBRATE_ERROR),
79 	E(B_DEV_RESOURCE_CONFLICT),
80 	E(B_DEV_SEEK_ERROR),
81 	E(B_DEV_STALLED),
82 	E(B_DEV_TIMEOUT),
83 	E(B_DEV_TOO_LATE),
84 	E(B_DEV_UNEXPECTED_PID),
85 	E(B_DEV_UNREADABLE),
86 	E(B_DEV_WRITE_ERROR),
87 	E(B_DIRECTORY_NOT_EMPTY),
88 	E(B_DONT_DO_THAT),
89 	E(B_DUPLICATE_REPLY),
90 	E(B_ENTRY_NOT_FOUND),
91 	E(B_ERROR),
92 	E(B_ERRORS_END),
93 	E(B_FILE_ERROR),
94 	E(B_FILE_EXISTS),
95 	E(B_FILE_NOT_FOUND),
96 #ifdef B_FILE_TOO_LARGE
97 	E(B_FILE_TOO_LARGE),
98 #endif
99 	E(B_GENERAL_ERROR_BASE),
100 	E(B_INTERFACE_ERROR_BASE),
101 	E(B_INTERRUPTED),
102 	E(B_IO_ERROR),
103 	E(B_IS_A_DIRECTORY),
104 	E(B_LAST_BUFFER_ERROR),
105 	E(B_LAUNCH_FAILED),
106 	E(B_LAUNCH_FAILED_APP_IN_TRASH),
107 	E(B_LAUNCH_FAILED_APP_NOT_FOUND),
108 	E(B_LAUNCH_FAILED_EXECUTABLE),
109 	E(B_LAUNCH_FAILED_FILES_APP_NOT_FOUND),
110 	E(B_LAUNCH_FAILED_NO_PREFERRED_APP),
111 	E(B_LAUNCH_FAILED_NO_RESOLVE_LINK),
112 	E(B_LINK_LIMIT),
113 	E(B_MAIL_ACCESS_ERROR),
114 	E(B_MAIL_ERROR_BASE),
115 	E(B_MAIL_INVALID_MAIL),
116 	E(B_MAIL_NO_DAEMON),
117 	E(B_MAIL_NO_RECIPIENT),
118 	E(B_MAIL_UNKNOWN_FIELD),
119 	E(B_MAIL_UNKNOWN_HOST),
120 	E(B_MAIL_UNKNOWN_USER),
121 	E(B_MAIL_WRONG_PASSWORD),
122 	E(B_MEDIA_ERROR_BASE),
123 	E(B_MESSAGE_TO_SELF),
124 	E(B_MIDI_ERROR_BASE),
125 	E(B_MISMATCHED_VALUES),
126 	E(B_MISSING_LIBRARY),
127 	E(B_MISSING_SYMBOL),
128 	E(B_NAME_IN_USE),
129 	E(B_NAME_NOT_FOUND),
130 	E(B_NAME_TOO_LONG),
131 	E(B_NOT_ALLOWED),
132 	E(B_NOT_AN_EXECUTABLE),
133 	E(B_NOT_A_DIRECTORY),
134 	E(B_NOT_A_MESSAGE),
135 #ifdef B_NOT_SUPPORTED
136 	E(B_NOT_SUPPORTED),
137 #endif
138 	E(B_NO_ERROR),
139 	E(B_NO_INIT),
140 	E(B_NO_MEMORY),
141 	E(B_NO_MORE_FDS),
142 	E(B_NO_MORE_PORTS),
143 	E(B_NO_MORE_SEMS),
144 	E(B_NO_MORE_TEAMS),
145 	E(B_NO_MORE_THREADS),
146 	E(B_NO_PRINT_SERVER),
147 	E(B_OK),
148 	E(B_OS_ERROR_BASE),
149 	E(B_PARTITION_TOO_SMALL),
150 	E(B_PERMISSION_DENIED),
151 	E(B_POSIX_ERROR_BASE),
152 	E(B_PRINT_ERROR_BASE),
153 	E(B_READ_ONLY_DEVICE),
154 	E(B_RESOURCE_NOT_FOUND),
155 	E(B_RESOURCE_UNAVAILABLE),
156 #ifdef B_RESULT_NOT_REPRESENTABLE
157 	E(B_RESULT_NOT_REPRESENTABLE),
158 #endif
159 	E(B_SERVER_NOT_FOUND),
160 #ifdef B_SHUTDOWN_CANCELLED
161 	E(B_SHUTDOWN_CANCELLED),
162 #endif
163 #ifdef B_SHUTTING_DOWN
164 	E(B_SHUTTING_DOWN),
165 #endif
166 	E(B_STORAGE_ERROR_BASE),
167 	E(B_STREAM_NOT_FOUND),
168 	E(B_SUBSCRIBER_NOT_ENTERED),
169 	E(B_TIMED_OUT),
170 #ifdef B_TOO_MANY_ARGS
171 	E(B_TOO_MANY_ARGS),
172 #endif
173 	E(B_TRANSLATION_ERROR_BASE),
174 	E(B_UNKNOWN_MIME_TYPE),
175 	E(B_UNSUPPORTED),
176 	E(B_WOULD_BLOCK),
177 	E(E2BIG),
178 	E(EACCES),
179 	E(EADDRINUSE),
180 	E(EADDRNOTAVAIL),
181 	E(EAFNOSUPPORT),
182 	E(EAGAIN),
183 	E(EALREADY),
184 	E(EBADF),
185 #ifdef EBADMSG
186 	E(EBADMSG),
187 #endif
188 	E(EBUSY),
189 #ifdef ECANCELED
190 	E(ECANCELED),
191 #endif
192 	E(ECHILD),
193 	E(ECONNABORTED),
194 	E(ECONNREFUSED),
195 	E(ECONNRESET),
196 	E(EDEADLK),
197 #ifdef EDESTADDRREQ
198 	E(EDESTADDRREQ),
199 #endif
200 	E(EDOM),
201 #ifdef EDQUOT
202 	E(EDQUOT),
203 #endif
204 	E(EEXIST),
205 	E(EFAULT),
206 	E(EFBIG),
207 	E(EFPOS),
208 #ifdef EHOSTDOWN
209 	E(EHOSTDOWN),
210 #endif
211 	E(EHOSTUNREACH),
212 #ifdef EIDRM
213 	E(EIDRM),
214 #endif
215 	E(EILSEQ),
216 	E(EINPROGRESS),
217 	E(EINTR),
218 	E(EINVAL),
219 	E(EIO),
220 	E(EISCONN),
221 	E(EISDIR),
222 	E(ELOOP),
223 	E(EMFILE),
224 	E(EMLINK),
225 	E(EMSGSIZE),
226 #ifdef EMULTIHOP
227 	E(EMULTIHOP),
228 #endif
229 	E(ENAMETOOLONG),
230 	E(ENETDOWN),
231 	E(ENETRESET),
232 	E(ENETUNREACH),
233 	E(ENFILE),
234 	E(ENOBUFS),
235 #ifdef ENODATA
236 	E(ENODATA),
237 #endif
238 	E(ENODEV),
239 	E(ENOENT),
240 	E(ENOEXEC),
241 	E(ENOLCK),
242 #ifdef ENOLINK
243 	E(ENOLINK),
244 #endif
245 	E(ENOMEM),
246 	E(ENOMSG),
247 	E(ENOPROTOOPT),
248 	E(ENOSPC),
249 #ifdef ENOSR
250 	E(ENOSR),
251 #endif
252 #ifdef ENOSTR
253 	E(ENOSTR),
254 #endif
255 	E(ENOSYS),
256 	E(ENOTCONN),
257 	E(ENOTDIR),
258 	E(ENOTEMPTY),
259 	E(ENOTSOCK),
260 #ifdef ENOTSUP
261 	E(ENOTSUP),
262 #endif
263 	E(ENOTTY),
264 	E(ENXIO),
265 	E(EOPNOTSUPP),
266 	E(EOVERFLOW),
267 	E(EPERM),
268 	E(EPFNOSUPPORT),
269 	E(EPIPE),
270 #ifdef EPROTO
271 	E(EPROTO),
272 #endif
273 	E(EPROTONOSUPPORT),
274 	E(EPROTOTYPE),
275 	E(ERANGE),
276 	E(EROFS),
277 	E(ESHUTDOWN),
278 	E(ESIGPARM),
279 	E(ESPIPE),
280 	E(ESRCH),
281 	E(ESTALE),
282 #ifdef ETIME
283 	E(ETIME),
284 #endif
285 	E(ETIMEDOUT),
286 #ifdef ETXTBSY
287 	E(ETXTBSY),
288 #endif
289 	E(EWOULDBLOCK),
290 	E(EXDEV),
291 	{ 0, NULL }
292 };
293 
294 
295 static void
296 usage(void)
297 {
298 	fprintf(stderr, "usage: %s <error number>\n"
299 		"Prints clear text error messages for given error numbers.\n", __progname);
300 	exit(-1);
301 }
302 
303 
304 static void
305 print_error(char *number)
306 {
307 	char *end;
308 	int32 error = (int32)strtoll(number, &end, 0);
309 		// strtol() cuts off hex numbers that have the highest bit set
310 
311 	if (end[0]) {
312 		// not a number, check names
313 		struct error_pair *p = kErrorNames;
314 		while (p->name && strcmp(p->name, number))
315 			p++;
316 		if (!p->name) {
317 			fprintf(stderr, "%s: invalid number (%s)\n", __progname, number);
318 			exit(1);
319 		}
320 		error = p->value;
321 	}
322 
323 	printf("0x%" B_PRIx32 ": %s\n", error, strerror(error));
324 }
325 
326 
327 int
328 main(int argc, char *argv[])
329 {
330 	int32 i;
331 
332 	if (argc < 2)
333 		usage();
334 
335 	for (i = 1; i < argc; i++) {
336 		print_error(argv[i]);
337 	}
338 
339 	return 0;
340 }
341