Initial commit.
[tfortune.git] / err.h
1 /* SPDX-License-Identifier: GPL-3.0-only */
2
3 #define TF_ERRORS \
4         TF_ERROR(SUCCESS, "success"), \
5         TF_ERROR(ATOI_OVERFLOW, "value too large"), \
6         TF_ERROR(ATOI_NO_DIGITS, "no digits found in string"), \
7         TF_ERROR(ATOI_JUNK_AT_END, "further characters after number"), \
8         TF_ERROR(LOPSUB, "lopsub error"), \
9         TF_ERROR(REGEX, "regular expression error"), \
10         TF_ERROR(TXP, "tag expression parse error"), \
11         TF_ERROR(LH_EXIST, "item already exists in hash table"), \
12
13 /*
14  * This is temporarily defined to expand to its first argument (prefixed by
15  * 'E_') and gets later redefined to expand to the error text only
16  */
17 #define TF_ERROR(err, msg) E_ ## err
18 enum tf_error_codes {TF_ERRORS};
19 #undef TF_ERROR
20 #define TF_ERROR(err, msg) msg
21 #define DEFINE_TF_ERRLIST char *tf_errlist[] = {TF_ERRORS}
22
23 extern char *tf_errlist[];
24
25 /**
26  * This bit indicates whether a number is considered a system error number
27  * If yes, the system errno is just the result of clearing this bit from
28  * the given number.
29  */
30 #define SYSTEM_ERROR_BIT 30
31
32 /** Check whether the system error bit is set. */
33 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
34
35 /** Set the system error bit for the given number. */
36 #define ERRNO_TO_TF_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
37
38 static inline char *tf_strerror(int num)
39 {
40         assert(num > 0);
41         if (IS_SYSTEM_ERROR(num))
42                 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
43         else
44                 return tf_errlist[num];
45 }
46