Commit | Line | Data |
---|---|---|
479604f4 | 1 | /* Error table. |
250d07de | 2 | Copyright (C) 2019-2021 Free Software Foundation, Inc. |
479604f4 NA |
3 | |
4 | This file is part of libctf. | |
5 | ||
6 | libctf is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
14 | See the GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; see the file COPYING. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include <ctf-impl.h> | |
7eea9d3b | 21 | #include <stddef.h> |
e148b730 | 22 | #include <string.h> |
479604f4 | 23 | |
7eea9d3b NA |
24 | /* This construct is due to Bruno Haible: much thanks. */ |
25 | ||
26 | /* Give each structure member a unique name. The name does not matter, so we | |
0d01fbe6 | 27 | use the enum constant to uniquify them. */ |
7eea9d3b | 28 | |
0d01fbe6 TT |
29 | #define ERRSTRFIELD(N) ctf_errstr##N |
30 | ||
31 | /* In this file, we want to treat the first item of the ctf error | |
32 | macro like subsequent items. */ | |
33 | #define _CTF_FIRST(NAME, VALUE) _CTF_ITEM(NAME, VALUE) | |
7eea9d3b NA |
34 | |
35 | /* The error message strings, each in a unique structure member precisely big | |
36 | enough for that error, plus a str member to access them all as a string | |
37 | table. */ | |
38 | ||
39 | static const union _ctf_errlist_t | |
40 | { | |
41 | __extension__ struct | |
42 | { | |
0d01fbe6 TT |
43 | #define _CTF_ITEM(n, s) char ERRSTRFIELD (n) [sizeof (s)]; |
44 | _CTF_ERRORS | |
45 | #undef _CTF_ITEM | |
7eea9d3b NA |
46 | }; |
47 | char str[1]; | |
48 | } _ctf_errlist = | |
49 | { | |
50 | { | |
0d01fbe6 TT |
51 | #define _CTF_ITEM(n, s) N_(s), |
52 | _CTF_ERRORS | |
53 | #undef _CTF_ITEM | |
7eea9d3b NA |
54 | } |
55 | }; | |
56 | ||
57 | /* Offsets to each member in the string table, computed using offsetof. */ | |
58 | ||
59 | static const unsigned int _ctf_erridx[] = | |
60 | { | |
0d01fbe6 TT |
61 | #define _CTF_ITEM(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (n)), |
62 | _CTF_ERRORS | |
63 | #undef _CTF_ITEM | |
7eea9d3b | 64 | }; |
479604f4 NA |
65 | |
66 | const char * | |
67 | ctf_errmsg (int error) | |
68 | { | |
69 | const char *str; | |
70 | ||
7eea9d3b NA |
71 | if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR) |
72 | str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE]; | |
479604f4 | 73 | else |
e148b730 | 74 | str = (const char *) strerror (error); |
479604f4 | 75 | |
987cf30a | 76 | return (str ? gettext (str) : _("Unknown error")); |
479604f4 NA |
77 | } |
78 | ||
79 | int | |
139633c3 | 80 | ctf_errno (ctf_dict_t * fp) |
479604f4 NA |
81 | { |
82 | return fp->ctf_errno; | |
83 | } |