parse_format_string(): Explicitly set the result pointer to NULL on errors.
[adu.git] / error.h
1 /**
2 * This bit indicates whether a number is considered a system error code.
3 * If yes, the system errno is just the result of clearing this bit from
4 * the given number.
5 */
6 #define SYSTEM_ERROR_BIT 30
7
8 /** Check whether the system error bit is set. */
9 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
10
11 /** Set the system error bit for the given number. */
12 #define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
13
14 #define ALL_ERRORS \
15 _ERROR(SUCCESS, "success") \
16 _ERROR(SYNTAX, "syntax error") \
17 _ERROR(LOOP_COMPLETE, "loop complete") \
18 _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
19 _ERROR(BAD_UID, "uid not found in hash table") \
20 _ERROR(ATOI_OVERFLOW, "value too large") \
21 _ERROR(STRTOLL, "unknown strtoll error") \
22 _ERROR(ATOI_NO_DIGITS, "no digits found in string") \
23 _ERROR(ATOI_JUNK_AT_END, "further characters after number") \
24 _ERROR(EMPTY, "file empty") \
25 _ERROR(MMAP, "mmap error") \
26 _ERROR(OSL, "osl error") \
27 _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
28 _ERROR(OUTPUT, "error writing output file") \
29 _ERROR(MALFORMED_FORMAT, "malformed format string") \
30 _ERROR(BAD_ALIGN_SPEC, "bad alignment specifier") \
31 _ERROR(TRAILING_GARBAGE, "trailing garbage after specifier") \
32 _ERROR(UNIT, "no unit allowed here") \
33 _ERROR(BAD_UNIT, "invalid unit specifier") \
34 _ERROR(BAD_ATOM, "invalid atom") \
35
36
37 /**
38 * This is temporarily defined to expand to its first argument (prefixed by
39 * 'E_') and gets later redefined to expand to the error text only
40 */
41 #define _ERROR(err, msg) E_ ## err,
42
43 enum error_codes {
44 ALL_ERRORS
45 };
46 #undef _ERROR
47 #define _ERROR(err, msg) msg,
48 #define DEFINE_ERRLIST char *adu_errlist[] = {ALL_ERRORS}
49
50 extern int osl_errno;
51 extern char *adu_errlist[];
52
53
54 /**
55 * adu's version of strerror(3).
56 *
57 * \param num The error number.
58 *
59 * \return The error text of \a num.
60 */
61 static inline const char *adu_strerror(int num)
62 {
63 assert(num > 0);
64 if (num == E_OSL) {
65 assert(osl_errno > 0);
66 return osl_strerror((osl_errno));
67 }
68 if (IS_SYSTEM_ERROR(num))
69 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
70 return adu_errlist[num];
71 }
72
73 /**
74 * Wrapper for osl library calls.
75 *
76 * This should be used for all calls to osl functions that return an osl error
77 * code. It changes the return value to \p -E_OSL appropriately so that it can
78 * be used for printing the correct error message.
79 *
80 * \return \a ret if \a ret >= 0, \p -E_OSL otherwise.
81 */
82 static inline int osl(int ret)
83 {
84 if (ret >= 0)
85 return ret;
86 osl_errno = -ret;
87 return -E_OSL;
88 }