2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file error.h \brief Error handling functions and macros. */
10 * This bit indicates whether a number is considered a system error code.
11 * If yes, the system errno is just the result of clearing this bit from
14 #define SYSTEM_ERROR_BIT 30
16 /** Check whether the system error bit is set. */
17 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
19 /** Set the system error bit for the given number. */
20 #define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
22 /** The list of all adu error codes with descriptions. */
24 _ERROR(SUCCESS, "success") \
25 _ERROR(SYNTAX, "syntax error") \
26 _ERROR(LOOP_COMPLETE, "loop complete") \
27 _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
28 _ERROR(BAD_UID, "uid not found in hash table") \
29 _ERROR(ATOI_OVERFLOW, "value too large") \
30 _ERROR(STRTOLL, "unknown strtoll error") \
31 _ERROR(ATOI_NO_DIGITS, "no digits found in string") \
32 _ERROR(ATOI_JUNK_AT_END, "further characters after number") \
33 _ERROR(EMPTY, "file empty") \
34 _ERROR(MMAP, "mmap error") \
35 _ERROR(OSL, "osl error") \
36 _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
37 _ERROR(OUTPUT, "error writing output") \
38 _ERROR(MALFORMED_FORMAT, "malformed format string") \
39 _ERROR(BAD_ALIGN_SPEC, "bad alignment specifier") \
40 _ERROR(TRAILING_GARBAGE, "trailing garbage after specifier") \
41 _ERROR(UNIT, "no unit allowed here") \
42 _ERROR(BAD_UNIT, "invalid unit specifier") \
43 _ERROR(BAD_ATOM, "invalid atom") \
44 _ERROR(BAD_OUTPUT_ARG, "invalid name for output") \
45 _ERROR(REGEX, "regular expression error")
49 * This is temporarily defined to expand to its first argument (prefixed by
50 * 'E_') and gets later redefined to expand to the error text only
52 #define _ERROR(err, msg) E_ ## err,
55 * \cond (doxygen can not handle multiple definitions of the same macro).
57 * This just creates an enum consisting of the first argument of the above
66 * Here we define the array of error texts used by adu_strerror().
68 #define _ERROR(err, msg) msg,
69 #define DEFINE_ERRLIST char *adu_errlist[] = {ALL_ERRORS}
74 /** Contains the description of all adu error codes. */
75 extern char *adu_errlist[];
79 * adu's version of strerror(3).
81 * \param num The error number.
83 * \return The error text of \a num.
85 _static_inline_ const char *adu_strerror(int num)
89 assert(osl_errno > 0);
90 return osl_strerror((osl_errno));
92 if (IS_SYSTEM_ERROR(num))
93 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
94 return adu_errlist[num];
98 * Wrapper for osl library calls.
100 * \param ret The return value of an osl library function.
102 * This should be used for all calls to osl functions that return an osl error
103 * code. It changes the return value to \p -E_OSL appropriately so that it can
104 * be used for printing the correct error message.
106 * \return \a ret if \a ret >= 0, \p -E_OSL otherwise.
108 _static_inline_ int osl(int ret)