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