]> git.tuebingen.mpg.de Git - tfortune.git/blob - err.h
initial
[tfortune.git] / err.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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(TXP, "tag expression parse error"), \
9         TF_ERROR(REGEX, "regular expression error"), \
10         TF_ERROR(LOPSUB, "lopsub error"), \
11
12 /*
13  * This is temporarily defined to expand to its first argument (prefixed by
14  * 'E_') and gets later redefined to expand to the error text only
15  */
16 #define TF_ERROR(err, msg) E_ ## err
17 enum tf_error_codes {TF_ERRORS};
18 #undef TF_ERROR
19 #define TF_ERROR(err, msg) msg
20 #define DEFINE_TF_ERRLIST char *tf_errlist[] = {TF_ERRORS}
21
22 extern char *tf_errlist[];
23
24 /**
25  * This bit indicates whether a number is considered a system error number
26  * If yes, the system errno is just the result of clearing this bit from
27  * the given number.
28  */
29 #define SYSTEM_ERROR_BIT 30
30
31 /** Check whether the system error bit is set. */
32 #define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT)))
33
34 /** Set the system error bit for the given number. */
35 #define ERRNO_TO_TF_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
36
37 static inline char *tf_strerror(int num)
38 {
39         assert(num > 0);
40         if (IS_SYSTEM_ERROR(num))
41                 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
42         else
43                 return tf_errlist[num];
44 }
45