xref: /netbsd-src/external/gpl3/binutils/dist/libctf/ctf-error.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
16f4ced0bSchristos /* Error table.
2*cb63e24eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
36f4ced0bSchristos 
46f4ced0bSchristos    This file is part of libctf.
56f4ced0bSchristos 
66f4ced0bSchristos    libctf is free software; you can redistribute it and/or modify it under
76f4ced0bSchristos    the terms of the GNU General Public License as published by the Free
86f4ced0bSchristos    Software Foundation; either version 3, or (at your option) any later
96f4ced0bSchristos    version.
106f4ced0bSchristos 
116f4ced0bSchristos    This program is distributed in the hope that it will be useful, but
126f4ced0bSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
136f4ced0bSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
146f4ced0bSchristos    See the GNU General Public License for more details.
156f4ced0bSchristos 
166f4ced0bSchristos    You should have received a copy of the GNU General Public License
176f4ced0bSchristos    along with this program; see the file COPYING.  If not see
186f4ced0bSchristos    <http://www.gnu.org/licenses/>.  */
196f4ced0bSchristos 
206f4ced0bSchristos #include <ctf-impl.h>
214f645668Schristos #include <stddef.h>
224f645668Schristos #include <string.h>
236f4ced0bSchristos 
244f645668Schristos /* This construct is due to Bruno Haible: much thanks.  */
254f645668Schristos 
264f645668Schristos /* Give each structure member a unique name.  The name does not matter, so we
274f645668Schristos    use the enum constant to uniquify them.  */
284f645668Schristos 
294f645668Schristos #define ERRSTRFIELD(N) ctf_errstr##N
304f645668Schristos 
314f645668Schristos /* In this file, we want to treat the first item of the ctf error
324f645668Schristos    macro like subsequent items.  */
334f645668Schristos #define _CTF_FIRST(NAME, VALUE) _CTF_ITEM(NAME, VALUE)
344f645668Schristos 
354f645668Schristos /* The error message strings, each in a unique structure member precisely big
364f645668Schristos    enough for that error, plus a str member to access them all as a string
374f645668Schristos    table.  */
384f645668Schristos 
394f645668Schristos static const union _ctf_errlist_t
404f645668Schristos {
414f645668Schristos   __extension__ struct
424f645668Schristos   {
434f645668Schristos #define _CTF_ITEM(n, s) char ERRSTRFIELD (n) [sizeof (s)];
444f645668Schristos _CTF_ERRORS
454f645668Schristos #undef _CTF_ITEM
464f645668Schristos   };
474f645668Schristos   char str[1];
484f645668Schristos } _ctf_errlist =
494f645668Schristos   {
504f645668Schristos    {
514f645668Schristos #define _CTF_ITEM(n, s) N_(s),
524f645668Schristos _CTF_ERRORS
534f645668Schristos #undef _CTF_ITEM
544f645668Schristos    }
556f4ced0bSchristos   };
566f4ced0bSchristos 
574f645668Schristos /* Offsets to each member in the string table, computed using offsetof.  */
584f645668Schristos 
594f645668Schristos static const unsigned int _ctf_erridx[] =
604f645668Schristos   {
614f645668Schristos #define _CTF_ITEM(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (n)),
624f645668Schristos _CTF_ERRORS
634f645668Schristos #undef _CTF_ITEM
644f645668Schristos   };
656f4ced0bSchristos 
666f4ced0bSchristos const char *
ctf_errmsg(int error)676f4ced0bSchristos ctf_errmsg (int error)
686f4ced0bSchristos {
696f4ced0bSchristos   const char *str;
706f4ced0bSchristos 
714f645668Schristos   if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR)
724f645668Schristos     str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE];
736f4ced0bSchristos   else
744f645668Schristos     str = (const char *) strerror (error);
756f4ced0bSchristos 
764f645668Schristos   return (str ? gettext (str) : _("Unknown error"));
776f4ced0bSchristos }
786f4ced0bSchristos 
796f4ced0bSchristos int
ctf_errno(ctf_dict_t * fp)804f645668Schristos ctf_errno (ctf_dict_t * fp)
816f4ced0bSchristos {
826f4ced0bSchristos   return fp->ctf_errno;
836f4ced0bSchristos }
84