1867d70fcSchristos /* Error table.
2*c42dbd0eSchristos Copyright (C) 2019-2022 Free Software Foundation, Inc.
3867d70fcSchristos
4867d70fcSchristos This file is part of libctf.
5867d70fcSchristos
6867d70fcSchristos libctf is free software; you can redistribute it and/or modify it under
7867d70fcSchristos the terms of the GNU General Public License as published by the Free
8867d70fcSchristos Software Foundation; either version 3, or (at your option) any later
9867d70fcSchristos version.
10867d70fcSchristos
11867d70fcSchristos This program is distributed in the hope that it will be useful, but
12867d70fcSchristos WITHOUT ANY WARRANTY; without even the implied warranty of
13867d70fcSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14867d70fcSchristos See the GNU General Public License for more details.
15867d70fcSchristos
16867d70fcSchristos You should have received a copy of the GNU General Public License
17867d70fcSchristos along with this program; see the file COPYING. If not see
18867d70fcSchristos <http://www.gnu.org/licenses/>. */
19867d70fcSchristos
20867d70fcSchristos #include <ctf-impl.h>
21*c42dbd0eSchristos #include <stddef.h>
22*c42dbd0eSchristos #include <string.h>
23867d70fcSchristos
24*c42dbd0eSchristos /* This construct is due to Bruno Haible: much thanks. */
25*c42dbd0eSchristos
26*c42dbd0eSchristos /* Give each structure member a unique name. The name does not matter, so we
27*c42dbd0eSchristos use the enum constant to uniquify them. */
28*c42dbd0eSchristos
29*c42dbd0eSchristos #define ERRSTRFIELD(N) ctf_errstr##N
30*c42dbd0eSchristos
31*c42dbd0eSchristos /* In this file, we want to treat the first item of the ctf error
32*c42dbd0eSchristos macro like subsequent items. */
33*c42dbd0eSchristos #define _CTF_FIRST(NAME, VALUE) _CTF_ITEM(NAME, VALUE)
34*c42dbd0eSchristos
35*c42dbd0eSchristos /* The error message strings, each in a unique structure member precisely big
36*c42dbd0eSchristos enough for that error, plus a str member to access them all as a string
37*c42dbd0eSchristos table. */
38*c42dbd0eSchristos
39*c42dbd0eSchristos static const union _ctf_errlist_t
40*c42dbd0eSchristos {
41*c42dbd0eSchristos __extension__ struct
42*c42dbd0eSchristos {
43*c42dbd0eSchristos #define _CTF_ITEM(n, s) char ERRSTRFIELD (n) [sizeof (s)];
44*c42dbd0eSchristos _CTF_ERRORS
45*c42dbd0eSchristos #undef _CTF_ITEM
46*c42dbd0eSchristos };
47*c42dbd0eSchristos char str[1];
48*c42dbd0eSchristos } _ctf_errlist =
49*c42dbd0eSchristos {
50*c42dbd0eSchristos {
51*c42dbd0eSchristos #define _CTF_ITEM(n, s) N_(s),
52*c42dbd0eSchristos _CTF_ERRORS
53*c42dbd0eSchristos #undef _CTF_ITEM
54*c42dbd0eSchristos }
55867d70fcSchristos };
56867d70fcSchristos
57*c42dbd0eSchristos /* Offsets to each member in the string table, computed using offsetof. */
58*c42dbd0eSchristos
59*c42dbd0eSchristos static const unsigned int _ctf_erridx[] =
60*c42dbd0eSchristos {
61*c42dbd0eSchristos #define _CTF_ITEM(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (n)),
62*c42dbd0eSchristos _CTF_ERRORS
63*c42dbd0eSchristos #undef _CTF_ITEM
64*c42dbd0eSchristos };
65867d70fcSchristos
66867d70fcSchristos const char *
ctf_errmsg(int error)67867d70fcSchristos ctf_errmsg (int error)
68867d70fcSchristos {
69867d70fcSchristos const char *str;
70867d70fcSchristos
71*c42dbd0eSchristos if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR)
72*c42dbd0eSchristos str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE];
73867d70fcSchristos else
74*c42dbd0eSchristos str = (const char *) strerror (error);
75867d70fcSchristos
76*c42dbd0eSchristos return (str ? gettext (str) : _("Unknown error"));
77867d70fcSchristos }
78867d70fcSchristos
79867d70fcSchristos int
ctf_errno(ctf_dict_t * fp)80*c42dbd0eSchristos ctf_errno (ctf_dict_t * fp)
81867d70fcSchristos {
82867d70fcSchristos return fp->ctf_errno;
83867d70fcSchristos }
84