Fix user summary accounting if pattern is given.
[adu.git] / error.h
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file error.h \brief Error handling functions and macros. */
8
9 /**
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
12 * the given number.
13 */
14 #define SYSTEM_ERROR_BIT 30
15
16 /** Check whether the system error bit is set. */
17 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
18
19 /** Set the system error bit for the given number. */
20 #define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
21
22 /** The list of all adu error codes with descriptions. */
23 #define ALL_ERRORS \
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(SIGACTION, "could not install signal handler") \
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") \
46 _ERROR(MKDIR, "could not create directory")
47
48
49 /**
50 * This is temporarily defined to expand to its first argument (prefixed by
51 * 'E_') and gets later redefined to expand to the error text only
52 */
53 #define _ERROR(err, msg) E_ ## err,
54
55 /**
56 * \cond (doxygen can not handle multiple definitions of the same macro).
57 *
58 * This just creates an enum consisting of the first argument of the above
59 * error list.
60 */
61 enum error_codes {
62 ALL_ERRORS
63 };
64 #undef _ERROR
65
66 /*
67 * Here we define the array of error texts used by adu_strerror().
68 */
69 #define _ERROR(err, msg) msg,
70 #define DEFINE_ERRLIST char *adu_errlist[] = {ALL_ERRORS}
71 /** \endcond */
72
73 extern int osl_errno;
74
75 /** Contains the description of all adu error codes. */
76 extern char *adu_errlist[];
77
78
79 /**
80 * adu's version of strerror(3).
81 *
82 * \param num The error number.
83 *
84 * \return The error text of \a num.
85 */
86 _static_inline_ const char *adu_strerror(int num)
87 {
88 assert(num > 0);
89 if (num == E_OSL) {
90 assert(osl_errno > 0);
91 return osl_strerror((osl_errno));
92 }
93 if (IS_SYSTEM_ERROR(num))
94 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
95 return adu_errlist[num];
96 }
97
98 /**
99 * Wrapper for osl library calls.
100 *
101 * \param ret The return value of an osl library function.
102 *
103 * This should be used for all calls to osl functions that return an osl error
104 * code. It changes the return value to \p -E_OSL appropriately so that it can
105 * be used for printing the correct error message.
106 *
107 * \return \a ret if \a ret >= 0, \p -E_OSL otherwise.
108 */
109 _static_inline_ int osl(int ret)
110 {
111 if (ret >= 0)
112 return ret;
113 osl_errno = -ret;
114 return -E_OSL;
115 }